-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTheBeginningObject.ino
259 lines (201 loc) · 7.38 KB
/
TheBeginningObject.ino
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
/*Using LVGL with Arduino requires some extra steps:
*Be sure to read the docs here: https://docs.lvgl.io/master/get-started/platforms/arduino.html */
#include <lvgl.h>
#include <driver/i2c.h>
#include "SD.h"
#include "SPI.h"
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ElegantOTA.h>
#include "include/definitions.h"
#include "include/accessory.c"
const char* WIFI_SSID = "FILMACHINE_WIFI";
const char* WIFI_PASSWORD = "password";
const char* USERNAME = "username";
const char* PASSWORD = "password";
AsyncWebServer otaServer(80);
unsigned long ota_progress_millis = 0;
lv_display_t *lvDisplay;
lv_indev_t *lvInput;
LGFX lcd;
bool stopMotorManTask = false;
uint8_t initErrors = 0;
/*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/
const unsigned int lvBufferSize = (TFT_HOR_RES * TFT_VER_RES * 2) / 10;
void *lvBuffer1 = heap_caps_malloc(lvBufferSize, MALLOC_CAP_INTERNAL);
void *lvBuffer2 = heap_caps_malloc(lvBufferSize, MALLOC_CAP_INTERNAL);
#define LVGL_TICK_PERIOD_MS 1
static void increase_lvgl_tick( void *arg ) {
lv_tick_inc(LVGL_TICK_PERIOD_MS);
}
#define MEM_MSG_DISPLAY_TIME 20000
void sysMan( void *arg ) {
uint16_t msg;
while(1) { // This is a task which runs for ever
/* This will time out after MEM_MSG_DISPLAY_TIME and print memory then wait again */
if( xQueueReceive( gui.sysActionQ, &msg, pdMS_TO_TICKS(MEM_MSG_DISPLAY_TIME) ) ) {
switch(msg) {
case SAVE_PROCESS_CONFIG:
writeConfigFile(SD, FILENAME_SAVE, false);
break;
// Add Further processor intensive tasks here to keep them out of the GUI execution path
case SAVE_MACHINE_STATS:
LV_LOG_USER("Save On EEPROM!");
writeMachineStats(&gui.page.tools.machineStats);
break;
case RELOAD_CFG:
LV_LOG_USER("Reload FilMachine.cfg from backup");
if(copyAndRenameFile(SD, FILENAME_BACKUP, FILENAME_SAVE))
rebootBoard();
default:
LV_LOG_USER( "Unknown System Manager Request!");
break;
}
}
#if FILM_USE_LOG != 0
else {
LV_LOG_USER("\nFree Heap: %u bytes\n"
" MALLOC_CAP_SPIRAM %7zu bytes\n"
" MALLOC_CAP_INTERNAL %7zu bytes\n",
xPortGetFreeHeapSize(),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_free_size(MALLOC_CAP_INTERNAL)
);
}
#endif
}
}
void motorMan(void *arg) {
int8_t rotation = 1;
uint8_t prevRotation = rotation;
uint16_t msg;
while(!stopMotorManTask) { // Questo è un task che gira per sempre
TickType_t interval = pdMS_TO_TICKS(1000); // Intervallo di default
if (xQueueReceive(gui.sysActionQ, &msg, interval)) {
// Aggiungi qui le cose da fare ogni intervallo
} else {
switch(rotation) {
case 1:
runMotorFW(MOTOR_IN1_PIN, MOTOR_IN2_PIN);
prevRotation = 1;
rotation = 0;
interval = pdMS_TO_TICKS(getRandomRotationInterval() * 1000); // Imposta l'intervallo per runMotorFW
break;
case -1:
runMotorRV(MOTOR_IN1_PIN, MOTOR_IN2_PIN);
prevRotation = -1;
rotation = 0;
interval = pdMS_TO_TICKS(getRandomRotationInterval() * 1000); // Imposta l'intervallo per runMotorRV
break;
case 0:
default:
stopMotor(MOTOR_IN1_PIN, MOTOR_IN2_PIN);
if (prevRotation == 1) {
rotation = -1;
} else if (prevRotation == -1) {
rotation = 1;
} else {
rotation = 1; // Valore iniziale se prevRotation non è 1 o -1
}
prevRotation = 0;
interval = pdMS_TO_TICKS(1000); // Intervallo per stopMotor
break;
}
}
// Aggiungi un ritardo per rispettare l'intervallo calcolato
vTaskDelay(interval);
}
// Codice per pulire e fermare il task
stopMotor(MOTOR_IN1_PIN, MOTOR_IN2_PIN);
vTaskDelete(NULL); // Termina il task
}
void stopMotorTask() {
stopMotorManTask = true;
}
void runMotorTask() {
xTaskCreatePinnedToCore( motorMan, "motorMan", 4096, NULL, 8, NULL, 0 );
stopMotorManTask = false;
}
void onOTAStart() {
LV_LOG_USER("OTA update started!");
}
void onOTAProgress(size_t current, size_t final) {
// Log every 1 second
uint8_t percentage;
float_t cur,fin;
cur = (float_t)(current);
fin = (float_t)(final);
if (millis() - ota_progress_millis > 1000) {
ota_progress_millis = millis();
percentage = (uint8_t)((cur / fin) * 100);
LV_LOG_USER("OTA Progress Current: %u %%, %u bytes, Final: %u bytes\n", percentage, current, final);
}
}
void onOTAEnd(bool success) {
if (success) {
LV_LOG_USER("OTA update finished successfully!");
} else {
LV_LOG_USER("There was an error during OTA update!");
}
}
void connectOtaAP(){
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
IPAddress myIP = WiFi.softAPIP();
LV_LOG_USER("AP IP address: %s",myIP.toString());
ElegantOTA.setAutoReboot(true);
ElegantOTA.setAuth(USERNAME, PASSWORD);
ElegantOTA.begin(&otaServer);
// ElegantOTA callbacks
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
otaServer.begin();
LV_LOG_USER("Server started");
}
void setup()
{
Serial.begin(115200);
LV_LOG_USER("Hello FilMachine! - This software uses LVGL V%d.%d.%d",lv_version_major(),lv_version_minor(),lv_version_patch());
pinMode(LCD_CS, OUTPUT);
pinMode(LCD_BLK, OUTPUT);
digitalWrite(LCD_CS, LOW);
digitalWrite(LCD_BLK, HIGH);
lcd.init();
lcd.setRotation(1);
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI);
LV_LOG_USER("Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate LVGL_TICK_PERIOD_MS periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = increase_lvgl_tick,
.name = "lvgl_tick"
};
esp_timer_handle_t lvgl_tick_timer = NULL;
esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer);
esp_timer_start_periodic(lvgl_tick_timer, LVGL_TICK_PERIOD_MS * 1000);
lv_init();
lvDisplay = lv_display_create(TFT_HOR_RES, TFT_VER_RES);
lv_display_set_color_format(lvDisplay, LV_COLOR_FORMAT_RGB565);
lv_display_set_flush_cb(lvDisplay, my_disp_flush);
lv_display_set_buffers(lvDisplay, lvBuffer1, lvBuffer2, lvBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);
lvInput = lv_indev_create();
lv_indev_set_type(lvInput, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(lvInput, my_touchpad_read);
initGlobals();
initPinouts();
homePage();
/* Create System message queue */
gui.sysActionQ = xQueueCreate( 16, sizeof( uint16_t ) );
/* Create task to process external functions which will slow the GUI response */
xTaskCreatePinnedToCore( sysMan, "sysMan", 4096, NULL, 8, NULL, 0 );
readConfigFile(SD, FILENAME_SAVE, false);
readMachineStats(&gui.page.tools.machineStats);
//this with lv_task_handler in loop, cause bootloop
//connectOtaAP();
}
void loop()
{
lv_task_handler(); /* let the GUI do its work */
ElegantOTA.loop();
delay(5);
}