// Wird 100 mal pro Sekunde aufgerufen void usv_timer_handler(void) { static uint8_t timer_counter = 0; // wird mit 100Hz = 10msec decrementiert if (usv_timer > 0) usv_timer--; // alle 100msec den LED timer handler aufrufen: if (led_timer-- == 0) { led_timer = 9; led_timer_handler(); } // Sekundenrythmus: if (timer_counter-- == 0) { timer_counter = 100; // switch_back_counter jede Sekunde dekrementieren: if (switch_back_counter > 0) switch_back_counter--; } }
void operation_handler() { switch(operation_state) { case START_TEMPORIZATION: { timeout_watchdog = 0; operation_state = IDLE; DEBUG_PRINTF(V_OPERATION, "Start operation temporization!\n"); if(operation_type != OPERATION_A) start_operation_timer(OPERATION_TICK, 0); break; } case START_PHOTO_CAPTURE: { DEBUG_PRINTF(V_OPERATION, "Ifconfig down.\n"); //TODO Removed because of the system script that checks connection //system("sudo ifdown --force " INTERFACE); timeout_watchdog = 0; operation_state = IDLE; DEBUG_PRINTF(V_OPERATION, "Start taking photos!\n"); cont_photos_sucess = 0; operation_timeout_reached = false; shouldStopPhotoTimer = false; actualIndex = getLastIndexFromMask((char *)PHOTO_DIR, (char *)PHOTO_FILENAME_MASK); actualIndex++; sprintf(photoFileName, PHOTO_DIR PHOTO_FILENAME_MASK, actualIndex); // PHOTO_FILENAME_MASK: "img_%07d.jpg"; PHOTO_DIR: "./photos/" stop_led_timer(); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { stop_accelgyro_timer(); } led_timer_handler(0, 0, 0); start_photo_timer(photo_period, 0); if(camera_controller.capture(photoFileName) == false) { DEBUG_PRINTF(V_ERROR, "Fail on first camera capture!\n"); } else { DEBUG_PRINTF(V_OPERATION, "Succes on first photo capture!\n"); cont_photos_sucess = 1; } led_timer_handler(0, 0, 0); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { start_accelgyro_timer(0, ACCELGYRO_TICK); } start_led_timer(TIMER_LED_TICK, 0); cont_photos = 1; DEBUG_PRINTF(V_MESSAGE, "Starting photo timer!\n"); break; } case GENERATE_VIDEO: { timeout_watchdog = 0; operation_state = IDLE; DEBUG_PRINTF(V_OPERATION, "Start video generation!\n"); stop_led_timer(); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { stop_accelgyro_timer(); } led_timer_handler(0, 0, 0); if((cont_photos_sucess > 0) && (camera_controller.generateVideo(VIDEO_DIR VIDEO_NAME) == true)) { sprintf(videoFileName, VIDEO_DIR VIDEO_NAME); DEBUG_PRINTF(V_OPERATION, "Video generated! File: %s\n", videoFileName); } else { DEBUG_PRINTF(V_ERROR, "Fail on video generation!\n"); camera_controller.dumpPhotoMemory(); sprintf(videoFileName, VIDEO_DIR ERROR_VIDEO_NAME); } videoGenerated = true; led_timer_handler(0, 0, 0); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { start_accelgyro_timer(0, ACCELGYRO_TICK); } start_led_timer(TIMER_LED_TICK, 0); break; } case SEND_FILE: { timeout_watchdog = 0; operation_state = IDLE; START_CHRONOMETER(); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { stop_accelgyro_timer(); stop_operation_timer(); } led_timer_handler(0, 0, 0); DEBUG_PRINTF(V_OPERATION, "Start send file!\n"); if((wifiConnected = socket_tcp.checkWifiConnection()) == false) { DEBUG_PRINTF(V_OPERATION, "Wifi connection down!\n"); for(int i = 0; i < WIFI_CONNECT_RETRY; i++) { socket_tcp.connectWifi(); DEBUG_PRINTF(V_OPERATION, "Connected wifi on attempt %d!\n", (i + 1)); if((wifiConnected = socket_tcp.checkWifiConnection()) == true) { DEBUG_PRINTF(V_MESSAGE, "Wifi connection up after connection retry number %d!\n", (i + 1)); break; } else { DEBUG_PRINTF(V_MESSAGE, "Wifi connection down after connection up?!\n"); } DEBUG_PRINTF(V_MESSAGE, "Waiting %d seconds to retry wifi connection...\n", WIFI_CONNECT_SLEEP); sleep(WIFI_CONNECT_SLEEP); // Sleep in seconds. timeout_watchdog = 0; } } stop_led_timer(); if(wifiConnected == true) { bool socketOpen = false; for(int i = 0; i < WIFI_CONNECT_RETRY; i++) { DEBUG_PRINTF(V_MESSAGE, "Attempt to open socket number %d.\n", (i + 1)); if(socket_tcp.open_socket() == true) { DEBUG_PRINTF(V_MESSAGE, "Success opening socket on attempt number %d!\n", (i + 1)); socketOpen = true; break; } else { DEBUG_PRINTF(V_WARNING, "Failed to open socket on attempt number %d!\n", (i + 1)); if(i == (WIFI_CONNECT_RETRY - 1)) { DEBUG_PRINTF(V_ERROR, "Failed to open socket!\n"); socketOpen = false; } timeout_watchdog = 0; } } if((socketOpen == true) && (socket_tcp.send_file(videoFileName) == false)) { DEBUG_PRINTF(V_ERROR, "Failed to send the file!\n"); } if(socketOpen == true) { if(socket_tcp.close_socket() == false) { DEBUG_PRINTF(V_ERROR, "Failed on close TCP socket!\n"); } else { DEBUG_PRINTF(V_MESSAGE, "Socket closed!\n"); } } } else { DEBUG_PRINTF(V_ERROR, "Failed to connect WIFI!\n"); } STOP_CHRONOMETER("Send file checking connection"); led_timer_handler(0, 0, 0); start_led_timer(TIMER_LED_TICK, 0); if((operation_type == OPERATION_A) || (operation_type == OPERATION_B)) { accelGyroWaitEvent = ACCELGYRO_WAIT_STOP; start_accelgyro_timer(0, ACCELGYRO_TICK); } else if(operation_type == OPERATION_C) { exit(EXIT_SUCCESS); } else if(operation_type == OPERATION_D) { operation_state = START_TEMPORIZATION; operation_event_flag = 1; } break; } } return; }