/* * fg_start - open the device and initialize data for processing */ static int fg_start( int unit, struct peer *peer ) { struct refclockproc *pp; struct fgunit *up; int fd; char device[20]; /* * Open device file for reading. */ snprintf(device, sizeof(device), DEVICE, unit); DPRINTF(1, ("starting FG with device %s\n",device)); fd = refclock_open(device, SPEED232, LDISC_CLK); if (fd <= 0) return (0); /* * Allocate and initialize unit structure */ up = emalloc(sizeof(struct fgunit)); memset(up, 0, sizeof(struct fgunit)); pp = peer->procptr; pp->unitptr = up; pp->io.clock_recv = fg_receive; pp->io.srcclock = peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { close(fd); pp->io.fd = -1; return 0; } /* * Initialize miscellaneous variables */ peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; memcpy(&pp->refid, REFID, 3); up->pollnum = 0; /* * Setup dating station to use GPS receiver. * GPS receiver should work before this operation. */ if(!fg_init(pp->io.fd)) refclock_report(peer, CEVNT_FAULT); return (1); }
int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer if (CALBC1_16MHZ==0xFF) // If calibration constant erased { while(1); // do not load, trap CPU!! } DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_16MHZ; // Set range DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation*/ //configure ACLK to use clock crystal //DIVA is the ACLK divider. //When DIVA0 and DIVA1 Bits are zero, ACLK is divided //by 1 BCSCTL1 &= ~(DIVA0 | DIVA1); //No touch, BCSTL2, only handles SMCLK and MCLK //clear the Low Freq XTal 1 sel bits 0 and 1 to select //32768Hz clk for LFXT1 oscillator //This also routes LFXT1 into ACLK. if LFXT1Sx = 10, ACLK uses VLOCLK instead BCSCTL3 &= ~(LFXT1S0 | LFXT1S1); //set XCAP to 11 to select a 12.5pF crystal capacitance BCSCTL3 |= XCAP1 | XCAP0; BUTTONS_INIT: //configure P1.3 to switch on falling edge interrupts //force the pin to use the typical GPIO P1SEL &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON); P1SEL2 &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON); //select HIGH to LOW transition P1IES |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON; P1IE |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON; P1IFG &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON); P1DIR &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON); P1REN |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON; lcd_init(); display_reset(); //FUNCTION_GENERATOR_INIT: fg_init(); //why not, just go back to home clear everything again P1IFG |= FREQ_SEL_BUTTON; _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }
/* * @brief Launch fuel gauge initialization * @param[in] bp_status status * @remark Called by battery properties layer to advertise battery properties status */ static void bs_properties_status(bp_status_t bp_status) { fg_event_callback_t fg_event_callback = {}; fg_event_callback.fg_callback = (fg_callback_t)bs_evt_from_fg; if (BP_STATUS_SUCCESS == bp_status) { /* Initialize interface with Fuel Gauge */ fg_init( get_service_queue(), &fg_event_callback, bs_fuel_gauge_init_done); } }
void main_event_loop() { fgbg=(_app?FG:BG); while(!quit) { if(closed_acc) { evnt_timer(1000,0); #ifdef DEBUG form_alert(1,"[1][ACC reopened][Ok]"); #endif fg_init(&windforms[WIND_CTRL]); if(file_was_open) fd=reopen_file(); closed_acc=0; } if(fgbg==FG) fg_event_loop(); else if(fgbg==BG) bg_event_loop(); } }
/* * fg_receive - receive data from the serial interface */ static void fg_receive( struct recvbuf *rbufp ) { struct refclockproc *pp; struct fgunit *up; struct peer *peer; char *bpt; /* * Initialize pointers and read the timecode and timestamp * We can't use gtlin function because we need bynary data in buf */ peer = (struct peer *)rbufp->recv_srcclock; pp = peer->procptr; up = (struct fgunit *)pp->unitptr; /* * Below hug to implement receiving of status information */ if(!up->pollnum) { up->pollnum++; return; } if (rbufp->recv_length < (LENFG-2)) { refclock_report(peer, CEVNT_BADREPLY); return; /* The reply is invalid discard it. */ } /* Below I trying to find a correct reply in buffer. * Sometime GPS reply located in the beginnig of buffer, * sometime you can find it with some offset. */ bpt = (char *)rbufp->recv_space.X_recv_buffer; while(*bpt != '') bpt++; #define BP2(x) ( bpt[x] & 15 ) #define BP1(x) (( bpt[x] & 240 ) >> 4) pp->year = BP1(2)*10 + BP2(2); if(pp->year == 94) { refclock_report(peer, CEVNT_BADREPLY); if(!fg_init(pp->io.fd)) refclock_report(peer, CEVNT_FAULT); return; /* GPS is just powered up. The date is invalid - discarding it. Initilize GPS one more time */ /* Sorry - this driver will broken in 2094 ;) */ } if (pp->year < 99) pp->year += 100; pp->year += 1900; pp->day = 100 * BP2(3) + 10 * BP1(4) + BP2(4); /* After Jan, 10 2000 Forum Graphic GPS receiver had a very strange benahour. It doubles day number for an hours in replys after 10:10:10 UTC and doubles min every hour at HH:10:ss for a minute. Hope it is a problem of my unit only and not a Y2K problem of FG GPS. Below small code to avoid such situation. */ if(up->y2kwarn > 10) pp->hour = BP1(6)*10 + BP2(6); else pp->hour = BP1(5)*10 + BP2(5); if((up->y2kwarn > 10) && (pp->hour == 10)) { pp->minute = BP1(7)*10 + BP2(7); pp->second = BP1(8)*10 + BP2(8); pp->nsec = (BP1(9)*10 + BP2(9)) * 1000000; pp->nsec += BP1(10) * 1000; } else { pp->hour = BP1(5)*10 + BP2(5); pp->minute = BP1(6)*10 + BP2(6); pp->second = BP1(7)*10 + BP2(7); pp->nsec = (BP1(8)*10 + BP2(8)) * 1000000; pp->nsec += BP1(9) * 1000; } if((pp->hour == 10) && (pp->minute == 10)) { up->y2kwarn++; } sprintf(pp->a_lastcode, "%d %d %d %d %d", pp->year, pp->day, pp->hour, pp->minute, pp->second); pp->lencode = strlen(pp->a_lastcode); /*get_systime(&pp->lastrec);*/ #ifdef DEBUG if (debug) printf ("fg: time is %04d/%03d %02d:%02d:%02d UTC\n", pp->year, pp->day, pp->hour, pp->minute, pp->second); #endif if (peer->stratum <= 1) peer->refid = pp->refid; pp->disp = (10e-6); pp->lastrec = rbufp->recv_time; /* Is it better than get_systime()? */ /* pp->leap = LEAP_NOWARNING; */ /* * Process the new sample in the median filter and determine the * timecode timestamp. */ if (!refclock_process(pp)) refclock_report(peer, CEVNT_BADTIME); pp->lastref = pp->lastrec; refclock_receive(peer); return; }
void gr_mode(void) { int errorcode; if (!in_graphics_mode) { msm_hidecursor(); x_coord = x_margin; y_coord = y_margin; errorcode = fg_init(); if (have_been_in_graphics_mode) { in_graphics_mode = TRUE; if (turtle_shown && !refresh_p) draw_turtle(); redraw_graphics(); } else { if (errorcode == 0) err_logo(BAD_GRAPH_INIT, NIL); else { in_graphics_mode = have_been_in_graphics_mode = TRUE; if (can_do_color = (fg.nsimulcolor > 16 /* != fg.ncolormap */ )) { rgb_init(); dull = fg.nsimulcolor-1; bright = 7; } else { turtle_color = FG_HIGHLIGHT; dull = FG_WHITE; bright = FG_HIGHLIGHT; } bg_color = FG_BLACK; ztc_set_penc(7); if (ztc_textcolor == FG_WHITE) ztc_textcolor = dull; back_ground = 0; ztc_box[FG_X1] = ztc_textbox[FG_X1] = text_scroll_box[FG_X1] = text_last_line_box[FG_X1] = clear_box[FG_X1] = fg.displaybox[FG_X1]; ztc_textbox[FG_Y1] = fg.displaybox[FG_Y1]; ztc_box[FG_X2] = ztc_textbox[FG_X2] = text_scroll_box[FG_X2] = text_last_line_box[FG_X2] = clear_box[FG_X2] = MaxX = fg.displaybox[FG_X2]; ztc_box[FG_Y2] = MaxY = clear_box[FG_Y2] = fg.displaybox[FG_Y2]; y_scale = (double)fg.pixelx/(double)fg.pixely; { FILE *fp = fopen("scrunch.dat","r"); if (fp != NULL) { scrunching = TRUE; if (filelength(fileno(fp)) > 0) { fread(&x_scale, sizeof(FLONUM), 1, fp); fread(&y_scale, sizeof(FLONUM), 1, fp); } fclose(fp); } } if (MaxY == 479) texth = 16; else texth = (MaxY+1)/25; ztc_box[FG_Y1] = 4*texth+1; clear_box[FG_Y1] = 4*texth; ibm_screen_bottom = MaxY - (ztc_box[FG_Y1]); ztc_textbox[FG_Y2] = 4*texth-1; text_scroll_box[FG_Y2] = 3*texth-1; text_scroll_box[FG_Y1] = 0; text_last_line_box[FG_Y2] = texth-1; lclearscreen(NIL); lcleartext(NIL); in_splitscreen = TRUE; } } msm_showcursor(); } }
int do_formstuff(int obj_id) { int tfd; switch(obj_id) { case CTRL_STOP: if(Dsp_Hf1(-1)) /* If fast forwarding */ { if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); } if(replay || replay_pause) { if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL); replay_pause=0; exit_replay(); update_time(); } break; case CTRL_PLAY: if(replay_pause || Dsp_Hf1(-1)) { if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL); continue_replay(); replay_pause=0; } else { if(!replay && file_open) { replay_pause=0; init_replay(); } else { if((tfd = open_file()) > 0) { int error; fd=tfd; if((error=getmp2info(fd))==MP2_NOERR) { replay_pause=0; update_time(); setfilename(filename); init_replay(); } else { show_mp2_error(error); close_file(fd); strcpy(windforms[WIND_CTRL].wind_title,"MPEG"); wind_set(windforms[WIND_CTRL].whandle,WF_NAME, windforms[WIND_CTRL].wind_title); setfilename("MPEGFILE"); } } } } break; case CTRL_PAUSE: if(replay_pause) { continue_replay(); replay_pause=0; } else { if(replay) { pause_replay(); replay_pause=1; } } break; case CTRL_LOAD: if((tfd = open_file()) > 0) { int error; if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL); replay_pause=0; if(Dsp_Hf1(-1)) /* If fast forwarding */ { if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); } fd=tfd; if((error=getmp2info(tfd))==MP2_NOERR) { update_time(); setfilename(filename); } else { show_mp2_error(error); close_file(fd); strcpy(windforms[WIND_CTRL].wind_title,"MPEG"); wind_set(windforms[WIND_CTRL].whandle,WF_NAME, windforms[WIND_CTRL].wind_title); setfilename("MPEGFILE"); } } break; case CTRL_LOOP: looping=(windforms[WIND_CTRL].formtree[CTRL_LOOP].ob_state & SELECTED); break; case CTRL_INFO: /* Open info window */ if(windforms[WIND_INFO].wind_open) wind_set(windforms[WIND_INFO].whandle,WF_TOP); else { fg_init(&windforms[WIND_INFO]); wind_open(windforms[WIND_INFO].whandle, windforms[WIND_INFO].wind.x,windforms[WIND_INFO].wind.y, windforms[WIND_INFO].wind.w,windforms[WIND_INFO].wind.h); windforms[WIND_INFO].wind_open=1; } break; case CTRL_NEXT: if(file_open) { int o_replay,o_pause; if(Dsp_Hf1(-1)) /* If fast forwarding */ { if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); } o_replay = replay; o_pause = replay_pause; exit_replay(); while((tfd=next_song(path, filename, 1))!=0) { if(getmp2info(tfd)!=MP2_NOERR) { close_file(tfd); } else { if(file_open) close_file(fd); fd = tfd; file_open=1; update_time(); setfilename(filename); replay = o_replay; replay_pause = o_pause; if(replay) init_replay(); if(replay_pause) { init_replay(); pause_replay(); } break; } } if(!tfd) { update_time(); } } break; case CTRL_PREV: if(file_open) { int o_replay,o_pause; if(Dsp_Hf1(-1)) /* If fast forwarding */ { if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); } o_replay = replay; o_pause = replay_pause; /* if more than or one second of the song has elapsed, go to the beginning of of the same song! */ if(calc_time() > 0) { reset_file(fd); update_time(); replay = o_replay; replay_pause = o_pause; if(replay) init_replay(); if(replay_pause) { init_replay(); pause_replay(); } } else { exit_replay(); while((tfd=next_song(path, filename, 0))!=0) { if(getmp2info(tfd)!=MP2_NOERR) { close_file(tfd); } else { if(file_open) close_file(fd); fd = tfd; file_open=1; update_time(); setfilename(filename); replay = o_replay; replay_pause = o_pause; if(replay) init_replay(); if(replay_pause) { init_replay(); pause_replay(); } break; } } if(!tfd) { update_time(); } } } break; case CTRL_FF: if (windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) Dsp_Hf1(1); else Dsp_Hf1(0); break; case CTRL_TIME: if(windforms[WIND_CTRL].formtree[CTRL_TIME].ob_state & SELECTED) count_dir = 0; /* count up */ else count_dir = 1; /* count down */ if(file_open) update_time(); break; default: break; } return 0; }
int handle_message(int pipe[8]) { static int first_open=0; int wnr,error; /* ,avmsg[8]; */ char *vamsg,*o; #ifdef DEBUG char tmp[128]; #endif switch (pipe[0]) { case AC_OPEN: if (pipe[4] == acc_id) { if(first_open) { if(windforms[WIND_CTRL].wind_open) wind_set(windforms[WIND_CTRL].whandle,WF_TOP); else fgbg=FG; } else { fg_init(&windforms[WIND_CTRL]); fgbg=FG; first_open=1; } } #ifdef DEBUG form_alert(1,"[1][Got AC_OPEN][Ok]"); #endif break; case AC_CLOSE: /* if (pipe[4] == acc_id) */ { #ifdef DEBUG sprintf(tmp,"[1][Got AC_CLOSE|ACC id: %d|pipe4: %d][Ok]",acc_id,pipe[4]); form_alert(1,tmp); #endif if(windforms[WIND_CTRL].wind_open) { wind_close(windforms[WIND_CTRL].whandle); wind_delete(windforms[WIND_CTRL].whandle); windforms[WIND_CTRL].wind_open=0; } if(windforms[WIND_INFO].wind_open) { wind_close(windforms[WIND_INFO].whandle); wind_delete(windforms[WIND_INFO].whandle); windforms[WIND_INFO].wind_open=0; } #ifdef DEBUG sprintf(tmp,"[1][Filepos: %ld][Ok]",filepos); form_alert(1,tmp); #endif file_was_open=file_open; if(file_open) close_file(fd); closed_acc=1; return 1; } /* break; */ case AP_TERM: quit=1; return 1; /* switch(pipe[5]) { case AP_RESCHG: printf("Got AP_RESCHG!\n"); break; case AP_TERM: printf("Got AP_TERM!"); break; default: printf("Got unknown AP_TERM!"); } */ /* break; */ case RESCHG_COMPLETED: /* printf("Got RESCHG_COMPLETED!"); */ break; case WM_REDRAW: if((wnr=find_windform(pipe[3]))>=0) update_objects(&windforms[wnr],windforms[wnr].firstobj, windforms[wnr].objdepth,pipe); break; case WM_MOVED: if((wnr=find_windform(pipe[3]))>=0) { wind_set(windforms[wnr].whandle,WF_CURRXYWH,pipe[4],pipe[5],pipe[6],pipe[7]); windforms[wnr].wind.x=pipe[4]; windforms[wnr].wind.y=pipe[5]; windforms[wnr].wind.w=pipe[6]; windforms[wnr].wind.h=pipe[7]; wind_calc(WC_WORK,windforms[wnr].windkind, windforms[wnr].wind.x,windforms[wnr].wind.y, windforms[wnr].wind.w,windforms[wnr].wind.h, &windforms[wnr].form.x,&windforms[wnr].form.y, &windforms[wnr].form.w,&windforms[wnr].form.h); windforms[wnr].formtree[windforms[wnr].firstobj].ob_x=windforms[wnr].form.x; windforms[wnr].formtree[windforms[wnr].firstobj].ob_y=windforms[wnr].form.y; windforms[wnr].formtree[windforms[wnr].firstobj].ob_width=windforms[wnr].form.w; windforms[wnr].formtree[windforms[wnr].firstobj].ob_height=windforms[wnr].form.h; if(replay) update_time(); } break; case WM_CLOSED: if((wnr=find_windform(pipe[3]))>=0) { wind_close(windforms[wnr].whandle); windforms[wnr].wind_open=0; if(wnr==WIND_CTRL) { if(windforms[WIND_INFO].wind_open) { wind_close(windforms[WIND_INFO].whandle); windforms[WIND_INFO].wind_open=0; } return 1; } else wind_delete(windforms[wnr].whandle); } break; case WM_TOPPED: if((wnr=find_windform(pipe[3]))>=0) wind_set(pipe[3],WF_TOP); break; case AP_DRAGDROP: if(wind_find(pipe[4],pipe[5]) == windforms[WIND_CTRL].whandle) { if(do_dragdrop(pipe,DD_OK)) { exit_replay(); if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL); replay_pause=0; filepos=0; fd=reopen_file(); filesize=Fseek(0L,fd,2); Fseek(0L,fd,0); if((error=getmp2info(fd))==MP2_NOERR) { update_time(); setfilename(filename); if(!(pipe[6] & K_ALT)) init_replay(); } else { exit_replay(); show_mp2_error(error); close_file(fd); strcpy(windforms[WIND_CTRL].wind_title,"MPEG"); wind_set(windforms[WIND_CTRL].whandle,WF_NAME, windforms[WIND_CTRL].wind_title); setfilename("MPEGFILE"); } } } else { do_dragdrop(pipe,DD_NAK); } break; case VA_START: vamsg=*((char **)&pipe[3]); strcpy(path,vamsg); /* This *should* be sent to the application which sent the VA_START msg, but it seems to hang Thing doing it. */ /* avmsg[0]=AV_STARTED; avmsg[1]=app_id; avmsg[2]=0; avmsg[3]=pipe[3]; avmsg[4]=pipe[4]; appl_write(pipe[1],5*2,avmsg); */ if((o=strchr(path,' '))!=NULL) o[0]='\0'; o=strrchr(path,'\\'); strcpy(filename,&o[1]); o=strrchr(path,'\\'); strncpy(o,"\\*.MP?",6); o[6] = '\0'; exit_replay(); if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL); Dsp_Hf1(0); if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED) toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL); replay_pause=0; filepos=0; fd=reopen_file(); filesize=Fseek(0L,fd,2); Fseek(0L,fd,0); if((error=getmp2info(fd))==MP2_NOERR) { update_time(); setfilename(filename); init_replay(); } else { exit_replay(); show_mp2_error(error); close_file(fd); strcpy(windforms[WIND_CTRL].wind_title,"MPEG"); wind_set(windforms[WIND_CTRL].whandle,WF_NAME, windforms[WIND_CTRL].wind_title); setfilename("MPEGFILE"); } break; default: #ifdef DEBUG sprintf(tmp,"[1][Unimplemented| message: %d][Ok]",pipe[0]); form_alert(1,tmp); #endif break; } return 0; }