Initial commit, getting all the stuff from PlatformIO
This commit is contained in:
32
src/actions.cpp
Normal file
32
src/actions.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "actions.h"
|
||||
#include "main.h"
|
||||
|
||||
extern "C" void action_act_butt_1(lv_event_t *e) {
|
||||
mqtt_send_result('1');
|
||||
}
|
||||
|
||||
extern "C" void action_act_butt_2(lv_event_t *e) {
|
||||
mqtt_send_result('2');
|
||||
}
|
||||
|
||||
extern "C" void action_act_butt_3(lv_event_t *e) {
|
||||
mqtt_send_result('3');
|
||||
}
|
||||
|
||||
extern "C" void action_act_butt_4(lv_event_t *e) {
|
||||
mqtt_send_result('4');
|
||||
}
|
||||
|
||||
extern "C" void action_act_butt_5(lv_event_t *e) {
|
||||
mqtt_send_result('5');
|
||||
}
|
||||
|
||||
extern "C" void action_act_badge(lv_event_t *e) {
|
||||
current_screen = 'B';
|
||||
screenNeedsUpdate = true;
|
||||
}
|
||||
|
||||
extern "C" void action_act_processor(lv_event_t *e) {
|
||||
current_screen = 'H';
|
||||
screenNeedsUpdate = true;
|
||||
}
|
||||
413
src/main.cpp
Normal file
413
src/main.cpp
Normal file
@@ -0,0 +1,413 @@
|
||||
#include <Arduino.h>
|
||||
#include "main.h"
|
||||
|
||||
#define START_SCREEN_WAIT 4 * 1000 / 5 // First number is the time in seconds
|
||||
|
||||
const char *ssid = "CIA";
|
||||
const char *password = "passespacarola";
|
||||
|
||||
// Touchscreen pinout
|
||||
#define XPT2046_IRQ 36
|
||||
#define XPT2046_CS 33
|
||||
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
|
||||
// Touchscreen coordinates(x, y) and pressure(z)
|
||||
int x, y, z;
|
||||
|
||||
#define SCREEN_WIDTH 240
|
||||
#define SCREEN_HEIGHT 320
|
||||
|
||||
// MQTT settings
|
||||
const char* mqtt_server = "10.164.67.154";
|
||||
const int mqtt_port = 1883;
|
||||
//const char* mqtt_user = "badge_device";
|
||||
//const char* mqtt_password = "letmein";
|
||||
PicoMQTT::Client mqtt_client(mqtt_server);
|
||||
|
||||
// Global variables
|
||||
int start_screen = 0;
|
||||
int ibvs_m = IBVS_BADGE;
|
||||
bool screenNeedsUpdate = true;
|
||||
bool configDone = false;
|
||||
bool winnerDone = false;
|
||||
char current_screen = 'A';
|
||||
char winner = '0';
|
||||
char Badge_ID[9];
|
||||
LongText question = {"To be or not to be ?", 4, 40};
|
||||
ShortText orgev = {" ATO", 4, 4};
|
||||
ShortText voter = {" Bob", 190, 4};
|
||||
ShortText item1 = {"Yes", 4, 80};
|
||||
ShortText item2 = {"No", 4, 110};
|
||||
ShortText item3 = {"Maybe", 4, 140};
|
||||
ShortText item4 = {"Abstain", 4, 170};
|
||||
ShortText item5 = {"Null", 4, 200};
|
||||
|
||||
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
|
||||
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// Connect to WiFi network
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
// Print the IP address
|
||||
Serial.print("My ip: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
// Create Badge ID from MAC address
|
||||
String mac = WiFi.macAddress();
|
||||
mac.replace(":", ""); // Remove all colons
|
||||
strcpy(Badge_ID, mac.substring(0, 8).c_str()); // Get first 8 characters, 4 bytes
|
||||
|
||||
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
|
||||
Serial.println(LVGL_Arduino);
|
||||
|
||||
// Start LVGL
|
||||
lv_init();
|
||||
// Register print function for debugging
|
||||
lv_log_register_print_cb(log_print);
|
||||
|
||||
// Start the SPI for the touchscreen and init the touchscreen
|
||||
SPI.begin(25, 39, 32, 33); // CLK, MISO, MOSI, CS for touch
|
||||
touchscreen.begin();
|
||||
// Set the Touchscreen rotation in landscape mode
|
||||
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 0: touchscreen.setRotation(0);
|
||||
touchscreen.setRotation(2);
|
||||
|
||||
// Create a display object
|
||||
lv_display_t * disp;
|
||||
// Initialize the TFT display using the TFT_eSPI library
|
||||
disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
|
||||
lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_90);
|
||||
|
||||
// Initialize an LVGL input device object (Touchscreen)
|
||||
lv_indev_t * indev = lv_indev_create();
|
||||
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
|
||||
// Set the callback function to read Touchscreen input
|
||||
lv_indev_set_read_cb(indev, touchscreen_read);
|
||||
|
||||
// Function to draw the GUI (text, buttons and sliders)
|
||||
ui_init();
|
||||
|
||||
|
||||
|
||||
mqtt_subs();
|
||||
|
||||
xTaskCreate(screen_manager, "Manage_screen", 8192, NULL, 1, NULL);
|
||||
xTaskCreate(mqtt_manager, "Manage_mqtt", 8192, NULL, 1, NULL);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!configDone) {
|
||||
serial_config();
|
||||
configDone = true;
|
||||
}
|
||||
|
||||
if (winner != '0' && !winnerDone) {
|
||||
Winner_Screen();
|
||||
winnerDone = true;
|
||||
}
|
||||
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
void serial_config() {
|
||||
char buffer[20];
|
||||
bool confirmed = false;
|
||||
|
||||
while (!confirmed) {
|
||||
// Ask for name
|
||||
Serial.println("Please enter your name:");
|
||||
|
||||
// Clear any existing input
|
||||
while(Serial.available()) Serial.read();
|
||||
|
||||
// Wait for complete line with Enter
|
||||
String input = "";
|
||||
while (true) {
|
||||
if (Serial.available() > 0) {
|
||||
char c = Serial.read();
|
||||
if (c == '\n' || c == '\r') {
|
||||
if (input.length() > 0) {
|
||||
break; // Got Enter with text
|
||||
} else {
|
||||
Serial.println("Please enter your name:");
|
||||
// Continue waiting for input
|
||||
}
|
||||
} else {
|
||||
input += c; // Add character to string
|
||||
}
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// Rest of code unchanged...
|
||||
input.toCharArray(buffer, sizeof(buffer));
|
||||
|
||||
Serial.print("You entered: ");
|
||||
Serial.println(buffer);
|
||||
Serial.println("Is this correct? (y/n)");
|
||||
|
||||
while (Serial.available() == 0) delay(10);
|
||||
|
||||
char response = Serial.read();
|
||||
Serial.println('\n');
|
||||
if (response == 'y' || response == 'Y') {
|
||||
strcpy(voter.text, buffer);
|
||||
set_var_ui_voter(buffer);
|
||||
confirmed = true;
|
||||
Serial.println("Name saved!");
|
||||
}
|
||||
}
|
||||
|
||||
confirmed = false;
|
||||
|
||||
while (!confirmed) {
|
||||
// Ask for name
|
||||
Serial.println("Please enter the Organization or Event name:");
|
||||
|
||||
// Clear any existing input
|
||||
while(Serial.available()) Serial.read();
|
||||
|
||||
// Wait for complete line with Enter
|
||||
String input = "";
|
||||
while (true) {
|
||||
if (Serial.available() > 0) {
|
||||
char c = Serial.read();
|
||||
if (c == '\n' || c == '\r') {
|
||||
if (input.length() > 0) {
|
||||
break; // Got Enter with text
|
||||
} else {
|
||||
Serial.println("Please enter the Organization or Event name:");
|
||||
// Continue waiting for input
|
||||
}
|
||||
} else {
|
||||
input += c; // Add character to string
|
||||
}
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
|
||||
input.toCharArray(buffer, sizeof(buffer));
|
||||
|
||||
Serial.print("You entered: ");
|
||||
Serial.println(buffer);
|
||||
Serial.println("Is this correct? (y/n)");
|
||||
|
||||
while (Serial.available() == 0) delay(10);
|
||||
|
||||
char response = Serial.read();
|
||||
Serial.println('\n');
|
||||
if (response == 'y' || response == 'Y') {
|
||||
strcpy(orgev.text, buffer);
|
||||
set_var_ui_orgev(buffer);
|
||||
confirmed = true;
|
||||
Serial.println("Organization/Event name saved!");
|
||||
}
|
||||
}
|
||||
Serial.print("Here is your ID: ");
|
||||
Serial.println(Badge_ID);
|
||||
Serial.println("Write it down.");
|
||||
|
||||
current_screen = 'C';
|
||||
screenNeedsUpdate = true;
|
||||
Serial.printf("Current screen is: %c\r\n", current_screen); // Debug
|
||||
}
|
||||
|
||||
void mqtt_subs() {
|
||||
|
||||
mqtt_client.subscribe("voting/#", [](const char *topic, const char *payload) {
|
||||
Serial.printf("Received message in topic '%s': %s\r\n", topic, payload);
|
||||
|
||||
if (strcmp(topic, "voting/question") == 0) {
|
||||
strcpy(question.text, payload);
|
||||
set_var_ui_question(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/item1") == 0) {
|
||||
strcpy(item1.text, payload);
|
||||
set_var_ui_item1(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/item2") == 0) {
|
||||
strcpy(item2.text, payload);
|
||||
set_var_ui_item2(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/item3") == 0) {
|
||||
strcpy(item3.text, payload);
|
||||
set_var_ui_item3(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/item4") == 0) {
|
||||
strcpy(item4.text, payload);
|
||||
set_var_ui_item4(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/item5") == 0) {
|
||||
strcpy(item5.text, payload);
|
||||
set_var_ui_item5(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/voter") == 0) {
|
||||
strcpy(voter.text, payload);
|
||||
set_var_ui_voter(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/orgev") == 0) {
|
||||
strcpy(orgev.text, payload);
|
||||
set_var_ui_orgev(payload);
|
||||
}
|
||||
else if (strcmp(topic, "voting/winner") == 0) {
|
||||
winner = payload[0];
|
||||
Serial.println("Im in the mqtt receive winner");
|
||||
}
|
||||
else if (strcmp(topic, "voting/ready") == 0) {
|
||||
if (strcmp(payload, "start") == 0) {
|
||||
current_screen = 'D';
|
||||
screenNeedsUpdate = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void mqtt_send_result(char result) {
|
||||
|
||||
char topic[32];
|
||||
sprintf(topic, "voting/%s", Badge_ID);
|
||||
mqtt_client.publish(topic, String(result).c_str());
|
||||
Serial.printf("Published \'%c\' to topic : ", result);
|
||||
Serial.println(topic);
|
||||
// vote_end = true;
|
||||
current_screen = 'E';
|
||||
screenNeedsUpdate = true;
|
||||
}
|
||||
|
||||
void Winner_Screen() {
|
||||
if (winner == '1')
|
||||
set_var_ui_winner(item1.text);
|
||||
else if (winner == '2')
|
||||
set_var_ui_winner(item1.text);
|
||||
else if (winner == '3')
|
||||
set_var_ui_winner(item3.text);
|
||||
else if (winner == '4')
|
||||
set_var_ui_winner(item4.text);
|
||||
else if (winner == '5')
|
||||
set_var_ui_winner(item5.text);
|
||||
else
|
||||
set_var_ui_winner("All losers...");
|
||||
|
||||
current_screen = 'F';
|
||||
screenNeedsUpdate = true;
|
||||
}
|
||||
|
||||
// LVGL logging
|
||||
void log_print(lv_log_level_t level, const char * buf) {
|
||||
LV_UNUSED(level);
|
||||
Serial.println(buf);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
// Get the Touchscreen data
|
||||
void touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data) {
|
||||
// Checks if Touchscreen was touched, and prints X, Y and Pressure (Z)
|
||||
if(touchscreen.tirqTouched() && touchscreen.touched()) {
|
||||
// Get Touchscreen points
|
||||
TS_Point p = touchscreen.getPoint();
|
||||
// Calibrate Touchscreen points with map function to the correct width and height
|
||||
x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
|
||||
y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
|
||||
z = p.z;
|
||||
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
|
||||
// Set the coordinates
|
||||
data->point.x = x;
|
||||
data->point.y = y;
|
||||
|
||||
// Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor
|
||||
Serial.print("X = ");
|
||||
Serial.print(x);
|
||||
Serial.print(" | Y = ");
|
||||
Serial.print(y);
|
||||
Serial.print(" | Pressure = ");
|
||||
Serial.print(z);
|
||||
Serial.println();
|
||||
}
|
||||
else {
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
}
|
||||
}
|
||||
|
||||
void screen_manager(void *pvParameters) {
|
||||
while (1) {
|
||||
if (start_screen < START_SCREEN_WAIT) {
|
||||
start_screen++;
|
||||
// Serial.printf("Counting: %d\r\n", start_screen); // Debug
|
||||
}
|
||||
else if (start_screen == START_SCREEN_WAIT) {
|
||||
Serial.println("Switching to screen G"); // Debug
|
||||
current_screen = 'G';
|
||||
screenNeedsUpdate = true;
|
||||
start_screen = START_SCREEN_WAIT+10;
|
||||
}
|
||||
|
||||
// Here we update the screen only if there is a change
|
||||
if (screenNeedsUpdate) {
|
||||
Serial.printf("Loading screen: %c\r\n", current_screen); // Debug
|
||||
switch (current_screen) {
|
||||
case 'A':
|
||||
loadScreen(SCREEN_ID_START);
|
||||
// lv_scr_load_anim(objects.start, LV_SCR_LOAD_ANIM_NONE, 0, 0, false);
|
||||
break;
|
||||
case 'B':
|
||||
loadScreen(SCREEN_ID_CONFIGURATIONS);
|
||||
// lv_scr_load_anim(objects.configurations, LV_SCR_LOAD_ANIM_NONE, 0, 0, false);
|
||||
break;
|
||||
case 'C':
|
||||
loadScreen(SCREEN_ID_VOTE_WAIT);
|
||||
// lv_scr_load_anim(objects.vote_wait, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, false);
|
||||
break;
|
||||
case 'D':
|
||||
loadScreen(SCREEN_ID_VOTE_SESSION);
|
||||
// lv_scr_load_anim(objects.vote_session, LV_SCR_LOAD_ANIM_MOVE_TOP, 200, 0, false);
|
||||
// vTaskDelay(100 / portTICK_PERIOD_MS); // Let screen load
|
||||
// tick_screen_vote_session(); // Manually call tick
|
||||
break;
|
||||
case 'E':
|
||||
loadScreen(SCREEN_ID_VOTE_WAIT_RESULTS);
|
||||
// lv_scr_load_anim(objects.vote_wait, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, false);
|
||||
break;
|
||||
case 'F':
|
||||
loadScreen(SCREEN_ID_VOTE_RESULTS);
|
||||
// lv_scr_load_anim(objects.vote_wait, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, false);
|
||||
break;
|
||||
case 'G':
|
||||
loadScreen(SCREEN_ID_IBVS_MODE);
|
||||
// lv_scr_load_anim(objects.vote_wait, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, false);
|
||||
break;
|
||||
case 'H':
|
||||
loadScreen(SCREEN_ID_IBVS_PROCESSOR);
|
||||
// lv_scr_load_anim(objects.vote_wait, LV_SCR_LOAD_ANIM_FADE_IN, 200, 0, false);
|
||||
break;
|
||||
}
|
||||
screenNeedsUpdate = false;
|
||||
}
|
||||
lv_timer_handler();
|
||||
lv_tick_inc(5);
|
||||
ui_tick();
|
||||
vTaskDelay(5 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_manager(void *pvParameters) {
|
||||
while (1) {
|
||||
mqtt_client.loop();
|
||||
vTaskDelay(5 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
87
src/vars.cpp
Normal file
87
src/vars.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <Arduino.h>
|
||||
#include <string>
|
||||
#include "main.h"
|
||||
#include "vars.h"
|
||||
|
||||
std::string ui_question;
|
||||
std::string ui_voter;
|
||||
std::string ui_orgev;
|
||||
std::string ui_item1;
|
||||
std::string ui_item2;
|
||||
std::string ui_item3;
|
||||
std::string ui_item4;
|
||||
std::string ui_item5;
|
||||
std::string ui_winner;
|
||||
|
||||
extern "C" const char *get_var_ui_question() {
|
||||
return ui_question.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_question(const char *value) {
|
||||
ui_question = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_voter() {
|
||||
return ui_voter.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_voter(const char *value) {
|
||||
ui_voter = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_orgev() {
|
||||
return ui_orgev.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_orgev(const char *value) {
|
||||
ui_orgev = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_item1() {
|
||||
return ui_item1.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_item1(const char *value) {
|
||||
ui_item1 = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_item2() {
|
||||
return ui_item2.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_item2(const char *value) {
|
||||
ui_item2 = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_item3() {
|
||||
return ui_item3.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_item3(const char *value) {
|
||||
ui_item3 = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_item4() {
|
||||
return ui_item4.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_item4(const char *value) {
|
||||
ui_item4 = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_item5() {
|
||||
return ui_item5.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_item5(const char *value) {
|
||||
ui_item5 = value;
|
||||
}
|
||||
|
||||
extern "C" const char *get_var_ui_winner() {
|
||||
return ui_winner.c_str();
|
||||
}
|
||||
|
||||
extern "C" void set_var_ui_winner(const char *value) {
|
||||
ui_winner = value;
|
||||
Serial.println("Found the winner");
|
||||
}
|
||||
Reference in New Issue
Block a user