/** * @brief Entry point to start execution at * * This is the main entry point where execution will start. It initializes the * hardware and enters an infinite loop handling any upcoming events not yet * covered. * * @note This function makes use of the attribute "OS_main". For details * refer to [1]. * * [1]: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html * * @return Should never return anything */ __attribute__((OS_main)) int main() { uart_init(); log_main("Init...\n"); preferences_init(); #if (ENABLE_DCF_SUPPORT == 1) dcf77_init(); #endif display_init(); datetime_init(); ldr_init(); pwm_init(); irmp_init(); timer_init(); user_init(); sei(); pwm_on(); log_main("Init finished\n"); while (1) { brightness_handle(); datetime_handle(); handle_ir_code(); #if (ENABLE_UART_PROTOCOL == 1) uart_protocol_handle(); #endif /* (ENABLE_UART_PROTOCOL == 1) */ #if (ENABLE_DCF_SUPPORT == 1) datetime_t dt; // TODO: Make sure dcf77_getDateTime() validates its result if (dcf77_get_datetime(&dt)) { datetime_set(&dt); } #endif } }
/*--------------------------------------------------------------------------------------------------------------------------------------------------- * MAIN: main routine *--------------------------------------------------------------------------------------------------------------------------------------------------- */ //int main(void) __attribute__((noreturn)); /* saves some Bytes but produces warning */ int main (void) { static DATETIME datetime; uart_init(); // initialize uart log_main("Init...\n"); wcEeprom_init(); # if (DCF_PRESENT == 1) dcf77_init (); // initialize dcf77 # endif display_init (); // initialize display { // local to save stack uint8_t i2c_errorcode; uint8_t i2c_status; if (! i2c_rtc_init (&i2c_errorcode, &i2c_status)) // initialize rtc { log_main("RTC init failed\n"); // TODO: error handling } } ldr_init (); // initialize ldr pwm_init (); // initialize pwm irmp_init (); // initialize irmp timer_init (); // initialize timer user_init(); sei (); // enable interrupts pwm_on (); // switch on pwm //pwm_set_base_brightness_step(MAX_PWM_STEPS-1); /// @todo remove if ldr stuff is working log_main("Init finished\n"); for (;;) { handle_brightness (); handle_datetime (&datetime); // check & display new time, if necessary handle_ir_code (); // handle ir user interaction # if (DCF_PRESENT == 1) if (dcf77_getDateTime (&datetime)) // handle dcf77 examination (enable_dcf77_ISR must be TRUE to enable analysis) { i2c_rtc_write (&datetime); soft_seconds = datetime.ss; user_setNewTime (&datetime); } # endif /** (DCF_PRESENT == 1) */ } }
/** * @brief Handles the given user command * * This handles the given user command (user_command_t) either by processing * it directly, or by passing it over to the actual handler using * UserState_HandleUserCommand(). * * g_eepromSaveDelay and g_checkIfAutoOffDelay get reset every time this * function is called to make sure the appropriate functionality works as * intended. * * @param user_command The user command that should be handled * * @see UserState_HandleUserCommand() * @see g_eepromSaveDelay * @see g_checkIfAutoOffDelay */ void handle_user_command(user_command_t user_command) { if (UC_ONOFF == user_command) { log_state("OF\n"); if (user_power_state < UPS_AUTO_OFF) { user_power_state = UPS_MANUAL_OFF; pwm_off(); } else { if (user_power_state == UPS_MANUAL_OFF) { user_power_state = UPS_NORMAL_ON; } else { user_power_state = UPS_OVERRIDE_ON; } pwm_on(); user_setNewTime(NULL); } preferences_save(); } else { int8_t i; bool handled = false; for (i = g_topOfStack - 1; i >= 0 && !handled; --i) { handled |= UserState_HandleUserCommand(g_stateStack[i], user_command); } if (!handled) { if (UC_BRIGHTNESS_UP == user_command) { log_state("B+\n"); pwm_increase_brightness(); } else if (UC_BRIGHTNESS_DOWN == user_command) { log_state("B-\n"); pwm_decrease_brightness(); } else if (UC_NORMAL_MODE == user_command) { addSubState(-1, MS_normalMode, (void*)1); } else if (UC_SET_TIME == user_command) { addState(MS_setSystemTime, NULL); } else if (UC_SET_ONOFF_TIMES == user_command) { addState(MS_setOnOffTime, NULL); } else if (UC_DEMO_MODE == user_command) { menu_state_t curTop = user_get_current_menu_state(); log_state("BS\n"); if (MS_demoMode == curTop) { quitMyself(MS_demoMode, NULL); } else { addState(MS_demoMode, NULL); } } else if (UC_CALIB_BRIGHTNESS == user_command) { pwm_modifyLdrBrightness2pwmStep(); // Indicate the change to user if (pwm_is_enabled()) { pwm_off(); _delay_ms(USER_VISUAL_INDICATION_TOGGLE_MS); pwm_on(); } } else if (UC_PULSE_MODE == user_command) { menu_state_t curTop = user_get_current_menu_state(); log_state("PLS\n"); if (MS_pulse == curTop) { leaveSubState(g_topOfStack - 1); } else { if ((MS_normalMode == curTop) #if (ENABLE_RGB_SUPPORT == 1) || (MS_hueMode == curTop) #endif ) { addState(MS_pulse, NULL); } } DISPLAY_SPECIAL_USER_COMMANDS_HANDLER #if (ENABLE_RGB_SUPPORT == 1) } else if (UC_HUE_MODE == user_command) { log_state("HM"); addSubState(-1, MS_hueMode, NULL); #endif #if (ENABLE_DCF_SUPPORT == 1) } else if (UC_DCF_GET_TIME == user_command) { log_state("DCF\n"); dcf77_enable(); #endif #if (ENABLE_AMBILIGHT_SUPPORT == 1) } else if (UC_AMBILIGHT == user_command) { log_state("AL\n"); PIN(USER_AMBILIGHT) |= _BV(BIT(USER_AMBILIGHT)); #endif #if (ENABLE_BLUETOOTH_SUPPORT == 1) } else if (UC_BLUETOOTH == user_command) { log_state("BT\n"); PIN(USER_BLUETOOTH) |= _BV(BIT(USER_BLUETOOTH)); #endif #if (ENABLE_AUXPOWER_SUPPORT == 1) } else if (UC_AUXPOWER == user_command) { log_state("AUX\n"); PIN(USER_AUXPOWER) |= _BV(BIT(USER_AUXPOWER)); #endif } else { return; } }
int main(int argc, char** argv) { //printf("hi"); pwm_on(1,0,50,50); }