/* * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ #include #include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/timers.h" #include "esp_chip_info.h" #include "esp_flash.h" #include "driver/gpio.h" #include "driver/uart.h" #include "esp_log.h" #include "ptc06.h" #include "buttons.h" #include "display.h" #include "wifi.h" #include "webserver.h" #include "spiffs.h" #include "nvs.h" static const char *TAG = "main"; void print_chip_data() { /* Print chip information */ esp_chip_info_t chip_info; uint32_t flash_size; esp_chip_info(&chip_info); printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", CONFIG_IDF_TARGET, chip_info.cores, (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : ""); unsigned major_rev = chip_info.revision / 100; unsigned minor_rev = chip_info.revision % 100; printf("silicon revision v%d.%d, ", major_rev, minor_rev); if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) { printf("Get flash size failed"); return; } printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024), (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); } /* * Public interface (settings functions) */ #define SPIFFS_ONLY 0 #define WS_ONLY 1 #define BOTH 2 static int32_t is = BOTH; static void display_imagesave() { const char* icon; switch (is) { case SPIFFS_ONLY: icon = CHAR_IS_STORE; break; case WS_ONLY: icon = CHAR_IS_SEND; break; case BOTH: icon = CHAR_IS_STORE_SEND; break; default: return; } display_printat(icon, CONFIG_DISPLAY_ICONS_ROW, CONFIG_SAVEIMAGE_COLUMN); } void main_store_imagesave(int32_t value) { if (value < 0 && value > BOTH) { ESP_LOGW(TAG, "Invalid value for saveimage was passed, not assigning"); return; } nvs_write_i32("imagesave", value); is = value; display_imagesave(); } static int32_t res = RES_640X480; static void display_resolution() { const char* icon; switch (res) { case RES_640X480: icon = " "; break; case RES_320X240: icon = CHAR_HALF_RES; break; case RES_160X120: icon = CHAR_QUART_RES; break; default: return; } display_printat(icon, CONFIG_DISPLAY_ICONS_ROW, CONFIG_RESOLUTION_COLUMN); } void main_store_resolution(int32_t value) { if (value != RES_640X480 && value != RES_320X240 && value != RES_160X120) { ESP_LOGW(TAG, "Invalid value for resolution was passed, not assigning"); return; } nvs_write_i32("resolution", value); res = value; display_resolution(); } static int32_t inv = NORMAL; void main_store_invertion(int32_t value) { if (value != NORMAL && value != INVERSE) { ESP_LOGW(TAG, "Invalid value for invertion was passed, not assigning"); return; } nvs_write_i32("invertion", value); inv = value; display_invertion((enum Inversion)inv); } static TimerHandle_t timelapse_timer = NULL; void main_store_timelapse_period(int32_t value) { if (value < 10) { ESP_LOGE(TAG, "Tried to set a timelapse period, that is less than 10s"); return; } nvs_write_i32("timelapse", pdMS_TO_TICKS(value * 1000)); if (xTimerChangePeriod(timelapse_timer, pdMS_TO_TICKS(value * 1000), 100) != pdPASS) { ESP_LOGE(TAG, "Failed to change timer duration!"); } if (xTimerStop(timelapse_timer, 100) != pdPASS) { ESP_LOGE(TAG, "Failed to stop the timer afte duration change!"); } } void main_start_timelapse() { if (xTimerStart(timelapse_timer, 100) != pdPASS) { ESP_LOGE(TAG, "Failed to start the timer!"); return; } display_printat(CHAR_SELF_TIMER, CONFIG_DISPLAY_ICONS_ROW, CONFIG_SELF_TIMER_COLUMN); } void main_stop_timelapse() { if (xTimerStop(timelapse_timer, 100) != pdPASS) { ESP_LOGE(TAG, "Failed to stop the timer!"); return; } display_printat(" ", CONFIG_DISPLAY_ICONS_ROW, CONFIG_SELF_TIMER_COLUMN); } /* * On button press */ static void debug_serial_print_image(const char* image, size_t length) { for (int i = 0; i < length; i++) { printf("%d ", image[i]); if (i % 12 == 0) printf("\n"); } printf("\n"); } static void take_photo() { ptc06_set_image_resolution((enum Resolution)res); display_printat(DCHAR_NO_CAMERA, CONFIG_DISPLAY_ICONS_ROW, CONFIG_CAMERA_COLUMN); char* image; size_t length = ptc06_get_image(&image); if (is != WS_ONLY) { spiffs_write(SPIFFS_PREFIX "/capture.jpeg", image, length); } if (is != SPIFFS_ONLY) { ws_send_bin(image, length); } display_printat(DCHAR_CAMERA, CONFIG_DISPLAY_ICONS_ROW, CONFIG_CAMERA_COLUMN); free(image); } static void timelapse_callback(TimerHandle_t timer) { press_push_button(CONFIG_BTN_SHOOT_PIN); ESP_LOGI(TAG, "Taking timelapse photo"); } void main_force_photo() { press_push_button(CONFIG_BTN_SHOOT_PIN); } /* * On boot */ static TaskHandle_t main_handle = NULL; #define INIT_CAMERA_BIT 0x01 static void init_camera(void*) { ptc06_init(); if (xTaskNotify(main_handle, INIT_CAMERA_BIT, eSetBits) != pdPASS) { ESP_LOGE(TAG, "Couldnt't notify main of camera init, software will hang"); } display_printat(DCHAR_CAMERA, CONFIG_DISPLAY_ICONS_ROW, CONFIG_CAMERA_COLUMN); vTaskDelete(NULL); } void app_main(void) { main_handle = xTaskGetHandle("main"); nvs_init(); // A whole lotta stuff depend on this // Minor performance improvement, since we always have to wait 2.5 seconds // Must be 2048 size, otherwise we get a LoadStoreError from nvs::HashList::find, // when another task tries to work with NVS xTaskCreate(init_camera, "init_camera", 2048, NULL, 10, NULL); // These inits are really fast, it doesnt make a difference if // they're in a task or not spiffs_init(); buttons_init(); add_push_button(CONFIG_BTN_SHOOT_PIN, take_photo); display_init(); display_printat(DCHAR_NO_CAMERA, CONFIG_DISPLAY_ICONS_ROW, CONFIG_CAMERA_COLUMN); nvs_read_i32("imagesave", &is); display_imagesave(); nvs_read_i32("resolution", &res); display_resolution(); nvs_read_i32("invertion", &inv); display_invertion((enum Inversion)inv); // This is really slow wifi_init(); webserver_init(); wifi_launch(); // Automatically starts server too int32_t time = 10000; nvs_read_i32("timelapse", &time); timelapse_timer = xTimerCreate("timelapse_timer", time, pdTRUE, (void*)0, timelapse_callback); long unsigned int notification = 0; while (!(notification & INIT_CAMERA_BIT)) { xTaskNotifyWait(0, 0, ¬ification, pdMS_TO_TICKS(50)); } while (true) { xTaskNotifyWait(ULONG_MAX, 0, ¬ification, pdMS_TO_TICKS(5000)); } printf("Restarting now.\n"); fflush(stdout); esp_restart(); }