void displaySet(set* st) { if(st == NULL) return; char* token = getTerminalStr(st->val); printf("%s ", token); displaySet(st->left); displaySet(st->right); }
int main(){ int i = 0; PLL_Init(); SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // 50 MHz DisableInterrupts(); SYSCTL_RCGC1_R |= SYSCTL_RCGC1_TIMER0;// activate timer0 SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF+SYSCTL_RCGC2_GPIOG; // activate ports F and G Output_Init(); SysTick_InitSeconds(1); PolledButtons_Init(); EnableInterrupts(); displayMode = 0; ringAlarms = 0; timeMode = 0; while(1){ if (ringAlarms && !soundPlaying && (alarmActive || timeMode == 4)) { playSound(); } else if ((ringAlarms==0 || !alarmActive) && soundPlaying) { stopSound(); } if(displayMode==0) { displayClock(); } else if(displayMode==1){ displaySet(); } } }
//run the time setting state machine. must be called frequently while in set mode. //returns true when setting is complete or has timed out. bool GoldieClock::setClock(void) { static setStates_t SET_STATE; static time_t utc, local; static uint8_t newTzIndex; static int y, mth, d, h, m, maxDays; static uint16_t pixel; static uint32_t rpt; bool retval = false; //see if user wants to cancel set mode or if set mode has timed out if ( SET_STATE != SET_INIT ) { if ( btnSet.pressedFor(SET_LONGPRESS) ) { SET_STATE = SET_INIT; displayClock( getUTC() ); while ( btnSet.isPressed() ) btnSet.read(); //wait for user to release the button return true; } uint32_t ms = millis(); if ( (ms - btnSet.lastChange() >= SET_TIMEOUT) && (ms - btnIncr.lastChange() >= SET_TIMEOUT) ) { SET_STATE = SET_INIT; return true; } } switch ( SET_STATE ) { case SET_INIT: SET_STATE = SET_TZ; rpt = REPEAT_FIRST; utc = getUTC(); local = (*tz).toLocal(utc, &tcr); newTzIndex = tzIndex; displaySet( newTzIndex, WHITE ); while ( btnSet.isPressed() ) btnSet.read(); //wait for user to release the button break; case SET_TZ: if ( btnSet.wasReleased() ) //then move on to set year { SET_STATE = SET_YEAR; rpt = REPEAT_FIRST; if ( newTzIndex != tzIndex ) { tzIndex = newTzIndex; tz = timezones[tzIndex]; eeprom_write_byte( &ee_tzIndex, tzIndex); Serial << F("Time zone changed to ") << tzNames[tzIndex] << endl; } y = year(local); if ( y < 2015 || y > 2074 ) y = 2015; //year must be in range 2015-2074 pixel = y - 2000; //map to pixel if ( pixel > 60 ) pixel -= 60; displaySet( pixel, MAGENTA ); } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++newTzIndex >= sizeof(tzNames) / sizeof(tzNames[0]) ) newTzIndex = 0; displaySet( newTzIndex, WHITE ); } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++newTzIndex >= sizeof(tzNames) / sizeof(tzNames[0]) ) newTzIndex = 0; displaySet( newTzIndex, WHITE ); } break; case SET_YEAR: if ( btnSet.wasReleased() ) { SET_STATE = SET_MON; rpt = REPEAT_FIRST; mth = month(local); displaySet( mth, CYAN ); } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++pixel > LAST_PIXEL ) pixel = 0; displaySet( pixel, MAGENTA ); y = pixel < 15 ? 2060 + pixel : 2000 + pixel; //map pixel to year } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++pixel > LAST_PIXEL ) pixel = 0; displaySet( pixel, MAGENTA ); y = pixel < 15 ? 2060 + pixel : 2000 + pixel; //map pixel to year } break; case SET_MON: if ( btnSet.wasReleased() ) { SET_STATE = SET_DAY; rpt = REPEAT_FIRST; d = day(local); maxDays = monthDays[mth-1]; //number of days in the month if (mth == 2 && isLeap(y)) ++maxDays; //account for leap year if ( d > maxDays ) d = maxDays; displaySet( d, YELLOW ); } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++mth > 12 ) mth = 1; //wrap from dec back to jan displaySet( mth, CYAN ); } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++mth > 12 ) mth = 1; displaySet( mth, CYAN ); } break; case SET_DAY: if ( btnSet.wasReleased() ) { SET_STATE = SET_HOUR; rpt = REPEAT_FIRST; h = hour(local); displaySet( h, RED ); } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++d > maxDays ) d = 1; displaySet( d, YELLOW ); } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++d > maxDays ) d = 1; displaySet( d, YELLOW ); } break; case SET_HOUR: if ( btnSet.wasReleased() ) { SET_STATE = SET_MIN; rpt = REPEAT_FIRST; m = minute(local); displaySet( m, BLUE ); } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++h > 23 ) h = 0; displaySet( h, RED ); } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++h > 23 ) h = 0; displaySet( h, RED ); } break; case SET_MIN: if ( btnSet.wasReleased() ) { SET_STATE = SET_INIT; rpt = REPEAT_FIRST; tmElements_t tm; tm.Year = CalendarYrToTm(y); tm.Month = mth; tm.Day = d; tm.Hour = h; tm.Minute = m; tm.Second = 0; local = makeTime(tm); utc = (*tz).toUTC(local); RTC.set(utc); setUTC(utc); Serial << F("\nTime set to:\n"); printDateTime(utc); Serial << F("UTC") << endl; printDateTime(local); Serial << tcr -> abbrev << endl; retval = true; } else if ( btnIncr.wasReleased() ) { rpt = REPEAT_FIRST; if ( ++m > 59 ) m = 0; displaySet( m, BLUE ); } else if ( btnIncr.pressedFor(rpt) ) { rpt += REPEAT_INCR; if ( ++m > 59 ) m = 0; displaySet( m, BLUE ); } break; } return retval; }
void showOutput(const char* str, Set* set) { printf(str); displaySet(set); printf("\n"); }