Cailean Finn
3 months ago
9 changed files with 2780 additions and 0 deletions
@ -0,0 +1,10 @@ |
|||
{ |
|||
// See http://go.microsoft.com/fwlink/?LinkId=827846 |
|||
// for the documentation about the extensions.json format |
|||
"recommendations": [ |
|||
"platformio.platformio-ide" |
|||
], |
|||
"unwantedRecommendations": [ |
|||
"ms-vscode.cpptools-extension-pack" |
|||
] |
|||
} |
@ -0,0 +1,39 @@ |
|||
|
|||
This directory is intended for project header files. |
|||
|
|||
A header file is a file containing C declarations and macro definitions |
|||
to be shared between several project source files. You request the use of a |
|||
header file in your project source file (C, C++, etc) located in `src` folder |
|||
by including it, with the C preprocessing directive `#include'. |
|||
|
|||
```src/main.c |
|||
|
|||
#include "header.h" |
|||
|
|||
int main (void) |
|||
{ |
|||
... |
|||
} |
|||
``` |
|||
|
|||
Including a header file produces the same results as copying the header file |
|||
into each source file that needs it. Such copying would be time-consuming |
|||
and error-prone. With a header file, the related declarations appear |
|||
in only one place. If they need to be changed, they can be changed in one |
|||
place, and programs that include the header file will automatically use the |
|||
new version when next recompiled. The header file eliminates the labor of |
|||
finding and changing all the copies as well as the risk that a failure to |
|||
find one copy will result in inconsistencies within a program. |
|||
|
|||
In C, the usual convention is to give header files names that end with `.h'. |
|||
It is most portable to use only letters, digits, dashes, and underscores in |
|||
header file names, and at most one dot. |
|||
|
|||
Read more about using header files in official GCC documentation: |
|||
|
|||
* Include Syntax |
|||
* Include Operation |
|||
* Once-Only Headers |
|||
* Computed Includes |
|||
|
|||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html |
@ -0,0 +1,30 @@ |
|||
#ifndef TCP_H |
|||
#define TCP_H |
|||
|
|||
#include <WiFi.h> |
|||
#include <WiFiClient.h> |
|||
#include <TFT_eSPI.h> |
|||
#include <SPI.h> |
|||
|
|||
class TCP { |
|||
public: |
|||
// Constructor
|
|||
TCP(const char* ssid, const char* password, const char* host, uint16_t port); |
|||
|
|||
// Methods to connect to WiFi and send data
|
|||
void connectWiFi(TFT_eSPI& tft); |
|||
void testConnection(); |
|||
void sendData(float& potData, int& emoteIndex); |
|||
|
|||
|
|||
private: |
|||
const char* ssid; // WiFi SSID
|
|||
const char* password; // WiFi password
|
|||
const char* host; // Host IP address
|
|||
uint16_t port; // Port number
|
|||
WiFiClient client; // WiFi client object
|
|||
TFT_eSPI* tft; |
|||
}; |
|||
|
|||
#endif // TCP_H
|
|||
|
File diff suppressed because it is too large
@ -0,0 +1,46 @@ |
|||
|
|||
This directory is intended for project specific (private) libraries. |
|||
PlatformIO will compile them to static libraries and link into executable file. |
|||
|
|||
The source code of each library should be placed in an own separate directory |
|||
("lib/your_library_name/[here are source files]"). |
|||
|
|||
For example, see a structure of the following two libraries `Foo` and `Bar`: |
|||
|
|||
|--lib |
|||
| | |
|||
| |--Bar |
|||
| | |--docs |
|||
| | |--examples |
|||
| | |--src |
|||
| | |- Bar.c |
|||
| | |- Bar.h |
|||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html |
|||
| | |
|||
| |--Foo |
|||
| | |- Foo.c |
|||
| | |- Foo.h |
|||
| | |
|||
| |- README --> THIS FILE |
|||
| |
|||
|- platformio.ini |
|||
|--src |
|||
|- main.c |
|||
|
|||
and a contents of `src/main.c`: |
|||
``` |
|||
#include <Foo.h> |
|||
#include <Bar.h> |
|||
|
|||
int main (void) |
|||
{ |
|||
... |
|||
} |
|||
|
|||
``` |
|||
|
|||
PlatformIO Library Dependency Finder will find automatically dependent |
|||
libraries scanning project source files. |
|||
|
|||
More information about PlatformIO Library Dependency Finder |
|||
- https://docs.platformio.org/page/librarymanager/ldf.html |
@ -0,0 +1,18 @@ |
|||
; PlatformIO Project Configuration File |
|||
; |
|||
; Build options: build flags, source filter |
|||
; Upload options: custom upload port, speed and extra flags |
|||
; Library options: dependencies, extra library storages |
|||
; Advanced options: extra scripting |
|||
; |
|||
; Please visit documentation for the other options and examples |
|||
; https://docs.platformio.org/page/projectconf.html |
|||
|
|||
[env:esp32dev] |
|||
platform = espressif32 |
|||
board = esp32dev |
|||
framework = arduino |
|||
monitor_speed = 115200 |
|||
lib_deps = |
|||
bodmer/TFT_eSPI@^2.5.43 |
|||
bitbank2/PNGdec@^1.0.2 |
@ -0,0 +1,72 @@ |
|||
#include "TCP.h" |
|||
|
|||
// Constructor
|
|||
TCP::TCP(const char* ssid, const char* password, const char* host, uint16_t port) |
|||
: ssid(ssid), password(password), host(host), port(port) { |
|||
} |
|||
|
|||
// Connect to WiFi
|
|||
void TCP::connectWiFi(TFT_eSPI& tf) { |
|||
// Set pointer to tft
|
|||
tft = &tf; |
|||
tft->setCursor(0, 0, 2); |
|||
tft->setTextColor(TFT_WHITE,TFT_BLACK); |
|||
tft->println("Connecting to: "); |
|||
tft->print(ssid); |
|||
delay(1000); |
|||
|
|||
WiFi.begin(ssid, password); |
|||
|
|||
while (WiFi.status() != WL_CONNECTED ) { |
|||
delay(1000); |
|||
} |
|||
|
|||
tft->println("\n\nConnected to WiFi!"); |
|||
delay(1000); |
|||
tft->println(WiFi.localIP()); |
|||
delay(1000); |
|||
} |
|||
|
|||
void TCP::testConnection(){ |
|||
tft->println("\nPolling server.."); |
|||
int yPos = tft->getCursorY(); // Store the current Y position
|
|||
while (!client.connected()) { |
|||
tft->setCursor(0, yPos); |
|||
tft->fillRect(0, yPos, tft->width(), 32, TFT_BLACK); |
|||
delay(50); |
|||
if (client.connect(host, port)) { |
|||
tft->setTextColor(TFT_GREEN); |
|||
tft->print("\nCONNECTED"); |
|||
delay(2000); |
|||
tft->fillScreen(TFT_BLACK); |
|||
} else { |
|||
tft->setTextColor(TFT_RED); |
|||
tft->println("\nFAILED"); |
|||
delay(250); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// Send array of floats
|
|||
void TCP::sendData(float& potData, int& emoteIndex) { |
|||
|
|||
if(client.connect(host, port)){ |
|||
// 1 float & 1 integer = 4 + 4 bytes (32-bit)
|
|||
// Create buffer
|
|||
uint8_t data[8]; |
|||
|
|||
// Convert values to bytes
|
|||
memcpy(&data[0], &potData, sizeof(float)); |
|||
memcpy(&data[4], &emoteIndex, sizeof(int)); |
|||
|
|||
// Send bytes to server
|
|||
size_t bytesSent = client.write(data, sizeof(data)); |
|||
|
|||
} else { |
|||
tft->setTextColor(TFT_BLACK, TFT_RED); |
|||
tft->drawCentreString("LOST", 62, 80, 1); |
|||
delay(1000); |
|||
return; |
|||
} |
|||
} |
|||
|
@ -0,0 +1,159 @@ |
|||
/*
|
|||
beta 2024 |
|||
esp32 controllers |
|||
|
|||
pin defenitions: |
|||
#define TFT_MOSI 23 // In some display driver board, it might be written as "SDA" and so on.
|
|||
#define TFT_SCLK 18 |
|||
#define TFT_CS 33 // Chip select control pin
|
|||
#define TFT_DC 26 // Data Command control pin
|
|||
#define TFT_RST 25 // Reset pin (could connect to Arduino RESET pin)
|
|||
|
|||
driver: |
|||
#define ST7735_DRIVER |
|||
*/ |
|||
|
|||
#include <TFT_eSPI.h> // Hardware-specific library |
|||
#include <SPI.h> |
|||
|
|||
#include <PNGdec.h> |
|||
#include "icon_clown.h" // Image is stored here in an 8-bit array |
|||
#include "TCP.h" |
|||
|
|||
#define potPin 13 // Potentiometer Pin
|
|||
#define MAX_IMAGE_WIDTH 240 // Adjust for your images
|
|||
|
|||
// Define some colors to match individual emotions
|
|||
const uint16_t emotionColors[] = { |
|||
0xFFE0, // HAPPY = YELLOW
|
|||
0x001F, // SAD = BLUE
|
|||
0xF800, // ANGRY = RED
|
|||
0xFA60, // FEAR = ORANGE
|
|||
0xF8FF, // SURPRISED = PURPLE
|
|||
0x07E0, // DISGUST = GREEN
|
|||
0xFFFF // NEUTRAL = WHITE
|
|||
}; |
|||
|
|||
enum Emotion { |
|||
HAPPY, |
|||
SAD, |
|||
ANGRY, |
|||
FEAR, |
|||
SURPRISED, |
|||
DISGUST, |
|||
NEUTRAL |
|||
}; |
|||
|
|||
float potData = 0; |
|||
Emotion emote; |
|||
int screen_width, screen_height; |
|||
TCP tcp("VM9093853", "kfrzuk8UngxyytUz", "192.168.0.169", 12345); |
|||
PNG png; |
|||
TFT_eSPI tft = TFT_eSPI(); |
|||
|
|||
void pngDraw(PNGDRAW *pDraw) { |
|||
uint16_t lineBuffer[MAX_IMAGE_WIDTH]; // Line buffer for rendering
|
|||
uint8_t maskBuffer[1 + MAX_IMAGE_WIDTH / 8]; // Mask buffer
|
|||
|
|||
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff); |
|||
|
|||
if (png.getAlphaMask(pDraw, maskBuffer, 255)) { |
|||
// Note: pushMaskedImage is for pushing to the TFT and will not work pushing into a sprite
|
|||
tft.pushMaskedImage(0, 0 + pDraw->y, pDraw->iWidth, 1, lineBuffer, maskBuffer); |
|||
} |
|||
} |
|||
|
|||
void setupWiFi(){ |
|||
tcp.connectWiFi(tft); |
|||
|
|||
tcp.testConnection(); |
|||
|
|||
int emotionIndex = static_cast<int>(emote); |
|||
tcp.sendData(potData, emotionIndex); |
|||
} |
|||
|
|||
void drawImage(){ |
|||
// Image sizes will be 128x128, a collection of 10~ per controller
|
|||
uint16_t pngw = 0, pngh = 0; |
|||
|
|||
int16_t rc = png.openFLASH((uint8_t *)bob, sizeof(bob), pngDraw); |
|||
|
|||
if (rc == PNG_SUCCESS) { |
|||
pngw = png.getWidth(); |
|||
pngh = png.getHeight(); |
|||
tft.startWrite(); |
|||
rc = png.decode(NULL, 0); |
|||
tft.endWrite(); |
|||
} |
|||
} |
|||
|
|||
void drawText(String& emotionText, int& i){ |
|||
tft.drawCentreString(emotionText, screen_width / 2, 8, 1); |
|||
|
|||
float sinVal = sin(i * 0.05); // Sine output between -1 and 1
|
|||
float mappedVal = (50 * sinVal) + 50; // Map it to between 1 and 100
|
|||
String ps = String(mappedVal, 2); |
|||
ps.concat("%"); |
|||
tft.drawCentreString(ps, screen_width / 2, screen_height - (screen_height / 6), 1); |
|||
|
|||
int emotionIndex = static_cast<int>(emote); |
|||
tcp.sendData(mappedVal, emotionIndex); |
|||
i++; |
|||
} |
|||
|
|||
void setupEmotion(String& emotionText){ |
|||
switch (emote) { |
|||
case HAPPY: emotionText = "HAPPY"; break; |
|||
case SAD: emotionText = "SAD"; break; |
|||
case ANGRY: emotionText = "ANGRY"; break; |
|||
case FEAR: emotionText = "FEAR"; break; |
|||
case SURPRISED: emotionText = "SURPRISED"; break; |
|||
case DISGUST: emotionText = "DISGUST"; break; |
|||
case NEUTRAL: emotionText = "NEUTRAL"; break; |
|||
} |
|||
|
|||
tft.setTextColor(emotionColors[emote],TFT_BLACK); |
|||
|
|||
tft.setTextSize(2); |
|||
} |
|||
|
|||
|
|||
void setup(void) { |
|||
// Set emotion
|
|||
emote = Emotion::NEUTRAL; |
|||
|
|||
// Setup screen & serial
|
|||
tft.init(); |
|||
tft.setRotation(0); |
|||
tft.fillScreen(TFT_BLACK); |
|||
|
|||
Serial.begin(115200); |
|||
|
|||
screen_height = tft.height(); |
|||
screen_width = tft.width(); |
|||
|
|||
tft.fillScreen(TFT_BLACK); |
|||
|
|||
delay(2000); |
|||
|
|||
setupWiFi(); |
|||
} |
|||
|
|||
void loop() { |
|||
tft.setCursor(0, 0, 2); |
|||
|
|||
// Sin value
|
|||
static int i = 0; |
|||
|
|||
// Display the emotion text
|
|||
String emotionText; |
|||
|
|||
setupEmotion(emotionText); |
|||
drawImage(); |
|||
drawText(emotionText, i); |
|||
|
|||
delay(60); |
|||
} |
|||
|
|||
|
|||
|
@ -0,0 +1,11 @@ |
|||
|
|||
This directory is intended for PlatformIO Test Runner and project tests. |
|||
|
|||
Unit Testing is a software testing method by which individual units of |
|||
source code, sets of one or more MCU program modules together with associated |
|||
control data, usage procedures, and operating procedures, are tested to |
|||
determine whether they are fit for use. Unit testing finds problems early |
|||
in the development cycle. |
|||
|
|||
More information about PlatformIO Unit Testing: |
|||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html |
Loading…
Reference in new issue