1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <inttypes.h>
#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();
}
|