uint8_t set_preset_variable(uint8_t num) { if (num < 0 || num >= NUMBER_OF_PRESETS) { //Out of range return 1; } while(!bns[1] && (wd_clock <= 60)) { if(bns[0]) { bns[0] = false; presets[num] -= 2; if (presets[num] < SI4705_FM_LOW) presets[num] = SI4705_FM_HIGH; ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if(bns[2]) { bns[2] = false; presets[num] += 2; if (presets[num] > SI4705_FM_HIGH) presets[num] = SI4705_FM_LOW; ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if ((ms_clock+125)%250 == 0) { if (hol[0]) { presets[num] -= 2; if (presets[num] < SI4705_FM_LOW) presets[num] = SI4705_FM_HIGH; ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if (hol[2]) { presets[num] += 2; if (presets[num] > SI4705_FM_HIGH) presets[num] = SI4705_FM_LOW; ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } } if (ms_clock%1000 == 0) { watchdog_entertain(); nokia5110_gotoXY(7, num); snprintf(line, 12, "%d.%d ", presets[num]/10, presets[num]%10); nokia5110_writeString(line); } else if ((ms_clock + 500)%1000 == 0 && is_fresh) { watchdog_entertain(); nokia5110_gotoXY(7, num); nokia5110_writeString(" "); } } if (bns[1]) { eeprom_update_word(&saved_presets[num], presets[num]); bns[1] = false; wd_clock = 0; return 0; } else { wd_clock = 0; return 1; } }
int main(void) { SystemInit(); // Activate settings quartz ina219_init(); nokia5110_init(); while(1) { nokia5110_clear(); nokia5110_gotoXY(0, 0); nokia5110_writeString("Voltage (V):"); nokia5110_gotoXY(0, 1); voltage = ina219_getBusVoltage_V(); result = util_floatToString(voltage); nokia5110_writeString(result); // systick_delayMs(100); nokia5110_gotoXY(0, 2); nokia5110_writeString("Current (mA):"); nokia5110_gotoXY(0, 3); current = ina219_getCurrent_mA(); result = util_floatToString(current); nokia5110_writeString(result); // systick_delayMs(100); nokia5110_gotoXY(0, 4); nokia5110_writeString("Power (mW):"); nokia5110_gotoXY(0, 5); result = util_floatToString(voltage * current); nokia5110_writeString(result); systick_delayMs(300); } }
uint8_t set_time_variable(CLOCK_PREFIX_t selection, time_t *input_time) { uint8_t value = *((uint8_t*)input_time + selection); //get the selected value from the memory address if (input_time == &time) { //If we're setting the clock's time sprintf(line, "%02d/%02d/%02d", input_time->month, input_time->day, input_time->year); nokia5110_gotoXY(0,1); nokia5110_writeString(line); sprintf(line, "%02d:%02d:%02d %s", input_time->hour - 12*(input_time->hour>12), input_time->minute, input_time->second, input_time->hour<12?"am":"pm"); nokia5110_gotoXY(0,2); nokia5110_writeString(line); } else { //If we're setting an alarm time sprintf(line, "%02d:%02d %s", input_time->hour - 12*(input_time->hour>12), input_time->minute, input_time->hour<12?"am":"pm"); nokia5110_gotoXY(0,2); nokia5110_writeString(line); } char str[] = "00"; if (selection == days) { time_limits[selection] = days_in_month[input_time->month-1]; } while(!bns[1] && (wd_clock <= 60)) { if(bns[0]) { bns[0] = false; if (selection == months || selection == days) { value = ((value+time_limits[selection]-2) % (time_limits[selection]) + 1); } else { value = ((value+time_limits[selection]-1) % (time_limits[selection])); } ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if(bns[2]) { bns[2] = false; if (selection == months || selection == days) { value = ((value % time_limits[selection]) + 1); } else { value = ((value+1) % (time_limits[selection])); } ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if ((ms_clock+125)%250 == 0) { if (hol[0]) { if (selection == months || selection == days) { value = ((value+time_limits[selection]-2) % (time_limits[selection]) + 1); } else { value = ((value+time_limits[selection]-1) % (time_limits[selection])); } ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } if (hol[2]) { if (selection == months || selection == days) { value = ((value % time_limits[selection]) + 1); } else { value = ((value+1) % (time_limits[selection])); } ms_clock = 0; //force a redraw, reset activity wd_clock = 0; watchdog_feed(); } } if (ms_clock%1000 == 0) { watchdog_entertain(); nokia5110_gotoXY((selection%3)*21, selection/3+1); if (selection == hours) { snprintf(str, 3, "%02d", value - 12*(value>12)); nokia5110_writeString(str); nokia5110_gotoXY(42 + 21*(input_time == &time), 2); nokia5110_writeString(value<12?"am":"pm"); } else { snprintf(str, 3, "%02d", value); nokia5110_writeString(str); } } else if ((ms_clock + 500)%1000 == 0 && is_fresh) { watchdog_entertain(); nokia5110_gotoXY((selection%3)*21, selection/3+1); nokia5110_writeString(" "); } } if (bns[1]) { bns[1] = false; wd_clock = 0; *((uint8_t*)input_time + selection) = value; //store the edited value in the memory address if (selection == days) { if (time.day > days_in_month[time.month - 1]) { input_time->day = (days_in_month[time.month - 1] - (time.month == 2 && !ds1307_isleapyear(time.year))); //don't let users send the wrong number of days } } if (selection == years) { if ( time.day > (days_in_month[time.month - 1] - (time.month == 2 && !ds1307_isleapyear(time.year))) ) { input_time->day = (days_in_month[time.month - 1] - (time.month == 2 && !ds1307_isleapyear(time.year))); //don't let users send the wrong number of days if it's not a leap year } } if (input_time == &time) { //If we're setting the clock's time sprintf(line, "%02d/%02d/%02d", input_time->month, input_time->day, input_time->year); nokia5110_gotoXY(0,1); nokia5110_writeString(line); sprintf(line, "%02d:%02d:%02d %s", input_time->hour - 12*(input_time->hour>12), input_time->minute, input_time->second, input_time->hour<12?"am":"pm"); nokia5110_gotoXY(0,2); nokia5110_writeString(line); } else { //If we're setting an alarm time sprintf(line, "%02d:%02d %s", input_time->hour - 12*(input_time->hour>12), input_time->minute, input_time->hour<12?"am":"pm"); nokia5110_gotoXY(0,2); nokia5110_writeString(line); } return 0; } else { wd_clock = 0; return 1; } }
void inline radio_state() { if (init) { schedule_insert(system_schedule, 0, ms_clock, get_time); schedule_insert(system_schedule, 0, ms_clock, get_rbds); schedule_insert(system_schedule, 0, ms_clock, get_status); schedule_insert(system_schedule, 0, ms_clock, display_radio_screen); schedule_insert(system_schedule, 0, ms_clock, display_radio_channel); schedule_insert(system_schedule, 0, ms_clock, display_rbds_information); schedule_insert(system_schedule, 0, ms_clock, start_ADC_conversion); if (!radio_is_on) { si4705_power_on(); si4705_set_channel(channel); set_volume(ADC_values[0]); } radio_is_on = true; what_the_beep = false; nokia5110_clear(); wd_clock = 0; //second counter #ifndef USING_PRINTF PORTD &= ~_BV(0); // Turn the radio LED on PORTD |= _BV(1); // Turn the stereo status LED off #endif } if(bns[0]) { bns[0] = false; if (radio_activity == 1) { channel -= 2; if (channel%2 == 0) { channel += 1; } if (channel < SI4705_FM_LOW) channel = SI4705_FM_HIGH; si4705_set_channel(channel); clear_radio_strings(); } radio_activity = 1; wd_clock = 0; //reset activity schedule_insert(system_schedule, 200, ms_clock, button_down); } if(bns[1]) { bns[1] = false; eeprom_update_word(&saved_channel, channel); state = menu; } if(bns[2]) { //printf("I was pressed up\n"); bns[2] = false; if (radio_activity == 2) { channel += 2; if (channel%2 == 0) { channel -= 1; } if (channel < SI4705_FM_LOW) channel = SI4705_FM_HIGH; si4705_set_channel(channel); clear_radio_strings(); } radio_activity = 2; wd_clock = 0; //reset activity ////printf("event scheduled\n"); schedule_insert(system_schedule, 200, ms_clock, button_up); } if (event_is_ready) { switch(next_event.id) { case start_ADC_conversion: ADC_start_conversion(); set_brightness(photo_avg/PHOTO_AVG_RESOLUTION); set_volume(ADC_values[0]); schedule_insert(system_schedule, 20, ms_clock, start_ADC_conversion); break; case get_time: ds1307_getdate_s(&time); schedule_insert(system_schedule, 1000, ms_clock, get_time); //printf("%d:%d:%d\n", time.hour, time.minute, time.second); break; case get_rbds: si4705_get_rdbs(station_text, scroll_text); schedule_insert(system_schedule, 200, ms_clock, get_rbds); break; case get_status: si4705_get_tune_status(&radio_tune_status); si4705_get_rsq_status(&radio_rsq_status); channel = radio_tune_status.tuneFrequency; //printf("%u\n", channel); schedule_insert(system_schedule, 100, ms_clock, get_status); break; case display_radio_screen: nokia5110_gotoXY(14,0); snprintf(line, 10, "%02d:%02d %s", time.hour - 12*(time.hour>12), time.minute, time.hour<12?"am":"pm"); nokia5110_writeString(line); schedule_insert(system_schedule, 1000, ms_clock, display_radio_screen); break; case display_radio_channel: nokia5110_gotoXY(8-8*(channel>=1000),1); snprintf(line, 8, " %d.%d ", channel/10, channel%10); nokia5110_writeString_megaFont(line); nokia5110_gotoXY(0, 2); snprintf(line, 2, "%c", 124+(ADC_values[0]>80)+(ADC_values[0]>180)); nokia5110_writeString(line); nokia5110_gotoXY(0, 3); snprintf(line, 2, "%c", 128 + (radio_rsq_status.rssi>22) + (radio_rsq_status.rssi>35)); nokia5110_writeString(line); nokia5110_gotoXY(70, 3); snprintf(line, 3, "%s", radio_rsq_status.pilot?"st":" "); nokia5110_writeString(line); #ifndef USING_PRINTF if (radio_rsq_status.pilot) { PORTD &= ~(_BV(1)); // Turn the stereo status on } else { PORTD |= _BV(1); // Turn the stereo status off } #endif schedule_insert(system_schedule, 100, ms_clock, display_radio_channel); break; case display_rbds_information: rbds_pixel_offset = (rbds_pixel_offset+1) % 7; if (rbds_pixel_offset == 0) { text_index = (text_index + 1) % (strlen(scroll_text)); } if (strlen(scroll_text) > DISPLAY_OFFSET+1) { // we have a radio string nokia5110_gotoXY(0,5); nokia5110_writeString_L(&scroll_text[text_index], rbds_pixel_offset); //nokia5110_writeString_C(&scroll_text[text_index]); } else if (strlen(station_text) > 0) { // we don't have a radio string, but we have program service! snprintf(line, 12, "%s ", station_text); nokia5110_gotoXY(14,5); nokia5110_writeString_C(line); } else { // we got nothing nokia5110_gotoXY(0,5); nokia5110_writeString(" "); } schedule_insert(system_schedule, 100, ms_clock, display_rbds_information); break; case button_down: if (hol[0]) { si4705_seek(DOWN); clear_radio_strings(); } else { channel -= 2; if (channel%2 == 0) { channel += 1; } if (channel < SI4705_FM_LOW) channel = SI4705_FM_HIGH; si4705_set_channel(channel); clear_radio_strings(); } radio_activity = 0; break; case button_up: //printf("handling event - "); if (hol[2]) { //printf("you were holding\n"); si4705_seek(UP); clear_radio_strings(); } else { //printf("you were tapping\n"); channel += 2; if (channel%2 == 0) { channel -= 1; } if (channel > SI4705_FM_HIGH) channel = SI4705_FM_LOW; si4705_set_channel(channel); clear_radio_strings(); } radio_activity = 0; break; default: break; } } if (wd_clock == 3599) { state = last_state; } }
void inline preset_state() { if (init) { schedule_insert(system_schedule, 0, ms_clock, start_ADC_conversion); menu_index = 0; nokia5110_clear(); for (err = 0; err < NUMBER_OF_PRESETS; err++) { nokia5110_gotoXY(0, err); snprintf(line, 12, " %u.%u", presets[err]/10, presets[err]%10); nokia5110_writeString(line); } nokia5110_gotoXY(0,0); nokia5110_writeString(">"); nokia5110_gotoXY(0,5); nokia5110_writeString(" Back"); } if (bns[0]) { bns[0] = false; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(" "); menu_index = (menu_index+1) % 6; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(">"); wd_clock = 0; // reset activity watchdog_feed(); } if (bns[1]) { bns[1] = false; if(menu_index == 5) { state = last_state; } else { // choose preset if (last_state == home) { nokia5110_gotoXY(0,menu_index); nokia5110_writeString(" "); err = set_preset_variable(menu_index); if (err) { state = last_state; } nokia5110_gotoXY(0,menu_index); snprintf(line, 12, ">%u.%u", presets[menu_index]/10, presets[menu_index]%10); nokia5110_writeString(line); } else { if (channel != presets[menu_index]) { channel = presets[menu_index]; si4705_set_channel(channel); clear_radio_strings(); } state = radio; } } } if (bns[2]) { bns[2] = false; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(" "); menu_index = (menu_index+5) % 6; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(">"); wd_clock = 0; //reset activity watchdog_feed(); } if (event_is_ready) { switch(next_event.id) { case start_ADC_conversion: ADC_start_conversion(); set_brightness(photo_avg/PHOTO_AVG_RESOLUTION); schedule_insert(system_schedule, 20, ms_clock, start_ADC_conversion); break; default: break; } } if(wd_clock == 60) { wd_clock = 0; state = last_state; } }
// This state is blocking, and can't be interrupted by other states void inline set_alarm_state() { nokia5110_clear(); nokia5110_gotoXY(3,0); nokia5110_writeString("Set Alarm "); nokia5110_writeChar(state == set_alarm_A?'A':'B'); nokia5110_gotoXY(0,4); nokia5110_writeString("[ ]Tone"); nokia5110_gotoXY(0,5); nokia5110_writeString("[ ]Radio"); err = 0; time_t *ptr; if (state == set_alarm_A) { time_alarm_A.hour = eeprom_read_byte(&alarm_A_hours); time_alarm_A.minute = eeprom_read_byte(&alarm_A_minutes); ptr = &time_alarm_A; } else { time_alarm_B.hour = eeprom_read_byte(&alarm_B_hours); time_alarm_B.minute = eeprom_read_byte(&alarm_B_minutes); ptr = &time_alarm_B; } if (!err) err |= set_time_variable(hours, ptr); if (!err) err |= set_time_variable(minutes, ptr); if (!err) { if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) { nokia5110_gotoXY(7,4); nokia5110_writeChar(126); } else { nokia5110_gotoXY(7,5); nokia5110_writeChar(127); } wd_clock = 0; // reset activity while(!bns[1] && (wd_clock <= 60)) { if (bns[0] || bns[2]) { if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) { nokia5110_gotoXY(7,4); nokia5110_writeChar(' '); } else { nokia5110_gotoXY(7,5); nokia5110_writeChar(' '); } bns[0] = bns[2] = false; alarm_A_is_beep ^= (state == set_alarm_A); alarm_B_is_beep ^= (state == set_alarm_B); if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) { nokia5110_gotoXY(7,4); nokia5110_writeChar(126); } else { nokia5110_gotoXY(7,5); nokia5110_writeChar(127); } } watchdog_entertain(); } if (bns[1]) { bns[1] = false; } else { err = 1; } } if (!err) { if (state == set_alarm_A) { //alarm_A_enabled = true; eeprom_update_byte(&alarm_A_hours, ptr->hour); eeprom_update_byte(&alarm_A_minutes, ptr->minute); eeprom_update_byte(&alarm_A_tone_setting, alarm_A_is_beep); } else { //alarm_B_enabled = true; eeprom_update_byte(&alarm_B_hours, ptr->hour); eeprom_update_byte(&alarm_B_minutes, ptr->minute); eeprom_update_byte(&alarm_B_tone_setting, alarm_B_is_beep); } } state = last_state; }
void inline menu_state() { if (init) { schedule_insert(system_schedule, 0, ms_clock, start_ADC_conversion); menu_index = 0; nokia5110_clear(); nokia5110_gotoXY(0,0); if (last_state == home) { nokia5110_writeString(">Radio"); } else { nokia5110_writeString(">Radio Off"); } nokia5110_gotoXY(0,1); nokia5110_writeString(" Time-Set"); nokia5110_gotoXY(0,2); nokia5110_writeString(" Set AlarmA"); nokia5110_gotoXY(0,3); nokia5110_writeString(" Set AlarmB"); nokia5110_gotoXY(0,4); if (last_state == home) { nokia5110_writeString(" Set Presets"); } else { nokia5110_writeString(" FM Presets"); } nokia5110_gotoXY(0,5); nokia5110_writeString(" Back"); } if (bns[0]) { bns[0] = false; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(" "); menu_index = (menu_index+1) % 6; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(">"); wd_clock = 0; //reset activity watchdog_feed(); } if(bns[1]) { bns[1] = false; switch(menu_index) { case 0: if (last_state == home) { state = radio; } else { state = home; } break; case 1: state = set_clock; break; case 2: state = set_alarm_A; break; case 3: state = set_alarm_B; break; case 4: state = preset; break; default: state = last_state; break; } } if (bns[2]) { bns[2] = false; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(" "); menu_index = (menu_index+5) % 6; nokia5110_gotoXY(0, menu_index); nokia5110_writeString(">"); wd_clock = 0; //reset activity watchdog_feed(); } if (event_is_ready) { switch(next_event.id) { case start_ADC_conversion: ADC_start_conversion(); set_brightness(photo_avg/PHOTO_AVG_RESOLUTION); schedule_insert(system_schedule, 20, ms_clock, start_ADC_conversion); break; default: break; } } if(wd_clock == 60) { wd_clock = 0; state = last_state; } }
void inline home_state() { if (init) { schedule_insert(system_schedule, 0, ms_clock, get_time); schedule_insert(system_schedule, 0, ms_clock, display_home_screen); schedule_insert(system_schedule, 0, ms_clock, display_temp_and_alarm_settings); schedule_insert(system_schedule, 0, ms_clock, start_ADC_conversion); nokia5110_clear(); if (radio_is_on) { si4705_power_off(); radio_is_on = false; } what_the_beep = false; set_volume(0); #ifndef USING_PRINTF PORTD |= _BV(0); // Turn the radio LED off PORTD |= _BV(1); // Turn the stereo status LED off #endif } if(bns[0]) { bns[0] = false; } if(bns[1]) { bns[1] = false; state = menu; } if(bns[2]) { bns[2] = false; } if (event_is_ready) { switch(next_event.id) { case start_ADC_conversion: ADC_start_conversion(); set_brightness(photo_avg/PHOTO_AVG_RESOLUTION); schedule_insert(system_schedule, 20, ms_clock, start_ADC_conversion); break; case get_time: ds1307_getdate_s(&time); err = ds1307_getdayofweek(time.year, time.month, time.day); schedule_insert(system_schedule, 1000, ms_clock, get_time); //printf("%d:%d:%d\n", time.hour, time.minute, time.second); break; case display_home_screen: snprintf(line, 12, "%02d:%02d", time.hour - 12*(time.hour>12), time.minute); nokia5110_gotoXY(12,1); nokia5110_writeString_megaFont(line); snprintf(line, 12, " %s ", days_of_week[err]); nokia5110_gotoXY(day_pixel_offsets[err],0); nokia5110_writeString(line); snprintf(line, 12, "%s", time.hour<12?"am":"pm"); nokia5110_gotoXY(68,3); nokia5110_writeString(line); snprintf(line, 9, "%d/%d/%d ", time.month, time.day, time.year); nokia5110_gotoXY(0,4); nokia5110_writeString(line); schedule_insert(system_schedule, 1000, ms_clock, display_home_screen); break; case display_temp_and_alarm_settings: init = false; snprintf(line, 8, "%02d.%01d%cF ", (uint8_t)(temperature_avg/THERM_AVG_RESOLUTION/10), (uint8_t)(temperature_avg/THERM_AVG_RESOLUTION%10), 123); nokia5110_gotoXY(0,5); nokia5110_writeString(line); nokia5110_gotoXY(63,4); if (hol[3]) { snprintf(line, 4, "A:%c", alarm_A_is_beep?124+(ADC_values[0]>80)+(ADC_values[0]>180):127); } else { snprintf(line, 4, "A: "); } nokia5110_writeString(line); nokia5110_gotoXY(63,5); if (hol[4]) { snprintf(line, 4, "B:%c", alarm_B_is_beep?124+(ADC_values[0]>80)+(ADC_values[0]>180):127); } else { snprintf(line, 4, "B: "); } nokia5110_writeString(line); schedule_insert(system_schedule, 100, ms_clock, display_temp_and_alarm_settings); break; default: break; } } watchdog_feed(); }