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
|
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "image.h"
#include <WiFi.h>
#include <SPI.h>
#include <SD.h>
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 450
#define SD_CS 5
#define JOY_X 34
#define JOY_Y 35
#define JOY_SW 32
// Position du curseur
int cursorX = 0;
int cursorY = 0;
// Sensibilité du joystick
#define JOY_DEADZONE 50
#define JOY_SPEED 4
// Points mesurés
struct Point {
int x, y;
long rssi;
};
#define MAX_POINTS 50
Point points[MAX_POINTS];
int pointCount = 0;
// Couleur selon RSSI
uint16_t colorForRSSI(long rssi) {
if (rssi > -55) return TFT_GREEN;
if (rssi > -70) return TFT_YELLOW;
return TFT_RED;
}
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
TFT_eSPI tft = TFT_eSPI();
// If logging is enabled, it will inform the user about what is happening in the library
void log_print(lv_log_level_t level, const char * buf) {
LV_UNUSED(level);
Serial.println(buf);
Serial.flush();
}
void draw_image(void) {
LV_IMAGE_DECLARE(my_image);
lv_obj_t * img1 = lv_image_create(lv_screen_active());
lv_image_set_src(img1, &my_image);
lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
}
void drawCursor(int x, int y, uint16_t color) {
tft.drawLine(x - 5, y, x + 5, y, color);
tft.drawLine(x, y - 5, x, y + 5, color);
}
void drawPoint(int x, int y, long rssi) {
uint16_t color = colorForRSSI(rssi);
tft.fillCircle(x, y, 5, color);
tft.drawCircle(x, y, 5, TFT_BLACK);
}
//************************************************//
// SETUP //
//************************************************//
void setup() {
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.begin(115200);
Serial.println(LVGL_Arduino);
pinMode(JOY_SW, INPUT_PULLUP);
// Start LVGL
lv_init();
// Register print function for debugging
lv_log_register_print_cb(log_print);
// 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_270);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
WiFi.begin("xxxxx", "xxxxxx");
tft.println("Connexion Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
}
tft.println("Connexion Wi-Fi: OK");
// Function to draw the image
draw_image();
drawCursor(cursorX, cursorY, TFT_BLACK);
}
//************************************************//
// LOOP //
//************************************************//
void loop() {
lv_task_handler(); // laisser l'interface graphique faire son travail
lv_tick_inc(5); // indiquer à LVGL le temps qui s'est écoulé
delay(5); // Laissez passer le temps
int xVal = analogRead(JOY_X);
int yVal = analogRead(JOY_Y);
bool pressed = !digitalRead(JOY_SW);
// Déplacement X/Y
if (xVal < 2048 - JOY_DEADZONE) cursorX -= JOY_SPEED;
else if (xVal > 2048 + JOY_DEADZONE) cursorX += JOY_SPEED;
if (yVal < 2048 - JOY_DEADZONE) cursorY += JOY_SPEED;
else if (yVal > 2048 + JOY_DEADZONE) cursorY -= JOY_SPEED;
// Contraintes écran
cursorX = constrain(cursorX, 5, 445);
cursorY = constrain(cursorY, 5, 315);
// Affiche curseur
drawCursor(cursorX, cursorY, TFT_BLACK);
// Point de validation
static bool lastPressed = false;
if (pressed && !lastPressed && pointCount < MAX_POINTS) {
long rssi = WiFi.RSSI();
points[pointCount++] = {cursorX, cursorY, rssi};
drawPoint(cursorX, cursorY, rssi);
Serial.printf("Point %d : (%d, %d) = %ld dBm\n", pointCount, cursorX, cursorY, rssi);
}
lastPressed = pressed;
delay(50);
} |
Partager