void SleepIfOkay() { // we won't sleep if the main isn't willing to block if (MQ_Main_Willing_to_block == 0) { return; } // check to see if we are handling a low priority interrupt // if so, we are not going to sleep if (in_low_int()) { return; } // we know that we are in a high priority interrupt handler // but we'll check to make sure and return if we are not if (!in_high_int()) { return; } // since we are the only thing executing that could be // putting something into a message queue destined for main() // we can safely check the message queues now // if they are empty, we'll go to sleep if (check_msg(&ToMainHigh_MQ)) { return; } if (check_msg(&ToMainLow_MQ)) { return; } enter_sleep_mode(); }
/* Enter read input function and stay forever */ void enter_read_user_input(void) { xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_pre_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_long_press, SEMAPHORE_BLOCK_TIME_0); while(true) { if(xSemaphoreTake(xSemaphore_single_press, SEMAPHORE_BLOCK_TIME_0)) { // Stop DroneID if(xSemaphoreTake(xSemaphore_droneid_mode_selector, 1000)) { droneid_mode_selector = POWER_ON; xSemaphoreGive(xSemaphore_droneid_mode_selector); } xSemaphoreTake(xSemaphore_pre_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_next_state); } else if(xSemaphoreTake(xSemaphore_double_press, SEMAPHORE_BLOCK_TIME_0)) { // Stop DroneID if(xSemaphoreTake(xSemaphore_droneid_mode_selector, 1000)) { droneid_mode_selector = POWER_OFF; xSemaphoreGive(xSemaphore_droneid_mode_selector); } // Give semaphore and just in case take back old semaphores xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_pre_state); } else if(xSemaphoreTake(xSemaphore_long_press, SEMAPHORE_BLOCK_TIME_0)) { // Stop DroneID if(xSemaphoreTake(xSemaphore_droneid_mode_selector, 1000)) { droneid_mode_selector = POWER_OFF; xSemaphoreGive(xSemaphore_droneid_mode_selector); } // Try send stop msg before shut dowm xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_pre_state); vTaskDelay(1000/portTICK_RATE_MS); enter_sleep_mode(); } vTaskDelay(MS_LOOP_DELAY/portTICK_RATE_MS); } }
/* Enter read input function and stay forever */ void enter_read_user_input(void) { while(true) { if(xSemaphoreTake(xSemaphore_long_press, SEMAPHORE_BLOCK_TIME_0)) { // Give semaphore and just in case take back old semaphores xSemaphoreTake(xSemaphore_new_trial_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_pre_state); } else if(xSemaphoreTake(xSemaphore_double_press, SEMAPHORE_BLOCK_TIME_0)) { // Give semaphore and just in case take back old semaphores xSemaphoreTake(xSemaphore_new_trial_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_pre_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_next_state); } else if(xSemaphoreTake(xSemaphore_single_press, SEMAPHORE_BLOCK_TIME_0)) { xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_pre_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_new_trial_state); } else if(xSemaphoreTake(xSemaphore_very_long_press, SEMAPHORE_BLOCK_TIME_0)) { // Try to send stop msg before shut dowm xSemaphoreTake(xSemaphore_new_trial_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreTake(xSemaphore_next_state, SEMAPHORE_BLOCK_TIME_0); xSemaphoreGive(xSemaphore_pre_state); vTaskDelay(1000/portTICK_RATE_MS); enter_sleep_mode(); } vTaskDelay(MS_LOOP_DELAY/portTICK_RATE_MS); } }
int main() { uint8_t i, change; uint8_t hand; uint32_t timeout = 0; uart_init(); xdev_out(uart_putchar); // Determine which hand this is from PE2 // Left is hand 0, right is hand 1 PORTE = (1 << 2); DDRE = 0; hand = (PINE & 0x04) ? 0 : 1; xprintf("\r\nHand %d\r\n", hand); // Initialise NRF24 // Set the last byte of the address to the hand ID rx_address[4] = hand; nrf24_init(); nrf24_config(CHANNEL, sizeof msg); nrf24_tx_address(tx_address); nrf24_rx_address(rx_address); matrix_init(); msg[0] = hand & 0x01; msg[1] = 0; msg[2] = 0; // Set up LED and flash it briefly DDRE |= 1<<6; PORTE = 1<<6; _delay_ms(500); PORTE = 0; get_voltage(); check_voltage(); // Scan the matrix and detect any changes. // Modified rows are sent to the receiver. while (1) { timeout++; matrix_scan(); for (i=0; i<ROWS; i++) { change = matrix_prev[i] ^ matrix[i]; // If this row has changed, send the row number and its current state if (change) { if (DEBUG) xprintf("%d %08b -> %08b %ld\r\n", i, matrix_prev[i], matrix[i], timeout); msg[1] = i; msg[2] = matrix[i]; nrf24_send(msg); while (nrf24_isSending()); timeout = 0; } matrix_prev[i] = matrix[i]; } // Sleep if there has been no activity for a while if (timeout > SLEEP_TIMEOUT) { timeout = 0; enter_sleep_mode(); } } }
void Task_SmartPHTApp(void) { message_t msg; adc_voltages_t *v; sensor_data_t *c; byte_t comRecvByte; if (knl_mailbox_pend(&taskMailbox, &msg) == RV_OK) { switch (msg.MsgId) { case MSG_ID_SYS_VOLTAGE_UPDATE: v = (adc_voltages_t*) msg.Param0; //tiny_printf("SYS: Vbat=%dmV, Vcc=%dmV, Vdd=%dmV, Vtft=%dmV\n", v->vbat, v->vcc, v->vdd, v->vtft); // if(v->vdd < 3000) // tty_output("SYS: system voltage is low!\n"); // if(v->vbat < 3300) // tty_output("SYS: BATTERY LOW! replace or battery can be damaged\n"); break; case MSG_ID_SYS_BATTERY_STAT: // if(msg.Param0 == BAT_CHARGE_COMPLETE) // tty_output("SYS: battery charge complete.\n"); // if(msg.Param0 == BAT_CHARGING) // tty_output("SYS: charging...\n"); // if(msg.Param0 == BAT_SHUTDOWN) // tty_output("SYS: charger shtdn.\n"); // break; break; case MSG_ID_USB_STATE_CHANGED: handle_usb_change(msg.Param0); break; case MSG_ID_SENSOR_UPDATE: c = (sensor_data_t*) msg.Param0; filter(&dashboardData.pressure, c->compensated.hp_pressure); filter(&dashboardData.humidity, c->compensated.sht_humidity); filter(&dashboardData.temperature, c->compensated.sht_temperature); update_min_max(&dashboardData.pressure); update_min_max(&dashboardData.humidity); update_min_max(&dashboardData.temperature); check_alarms(); // update waveforms wfrm_put_sample(&wfrmPressure, dashboardData.pressure.val); wfrm_put_sample(&wfrmHumidity, dashboardData.humidity.val); wfrm_put_sample(&wfrmTemperature, dashboardData.temperature.val); // Write to file write_samples_to_file(c, (rtc_time_t*) msg.Param1); gui_invalidate(&dashb); gui_invalidate(&lineplot); break; case MSG_ID_MEDIA_DETECTED: //tty_output("SDCARD: inserted."); //if(msg.Param0) tty_output(" Card is write protected!"); //tty_output("\n"); sd_register(); f_close(&samplesFile); // Mount filesystem f_mount(0, &filesystem); // Try open file; sampleFileIsOpen = try_open_file(); // Change statusbar toolbar_set_status(&statusbar, STATUS_SDCARD_PRESENT); break; case MSG_ID_MEDIA_DISAPPEARED: //tty_output("SDCARD: removed\n"); // Dismount file system f_mount(0, NULL); // Change statusbar toolbar_clear_status(&statusbar, STATUS_SDCARD_PRESENT); break; case MSG_ID_WDG_PRESSED: handle_button_press_event(msg.Param0); break; case MSG_ID_WDG_DELAYED_OP: handle_delayed_operation_event(msg.Param0); break; case MSG_ID_WDG_CHECKED: handle_button_checked_event(msg.Param0); break; case MSG_ID_WDG_INVALIDATE: // GUI needs to be redrawn gui_draw(); break; case MSG_ID_LPM_REQUEST: enter_sleep_mode(); break; case MSG_ID_TOUCH_EVENT: // // Draw cursor // if(msg.Param0 == TOUCH_EVENT_PANEL_PRESSED) // { // uint16_t i, j; // i = msg.Param1; // j = msg.Param2; // // if(i < 12) i = 12; // else if(i > 467) i = 467; // if(j < 12) j = 12; // else if(j > 260) j = 260; // // gfx_set_color(COLOR_WHITE); // gfx_draw_circle(i, j , 10); // } if(!darkenScreen) { set_brightness(lightenBrightness); } darkenScreen = bTrue; // no break, gui must get this msg too default: gui_msg(&msg); break; } } // COMMAND PARSER if(usb_device_state == CONFIGURED_STATE) if (poll_getc_cdc(&comRecvByte)) { putc_cdc(comRecvByte); // echo CDC_Flush_In_Now(); cmd_parse_char(comRecvByte); } }