diff --git a/embedded-pio b/embedded-pio index 9e8f34c..3640a3e 160000 --- a/embedded-pio +++ b/embedded-pio @@ -1 +1 @@ -Subproject commit 9e8f34cb83b9d1232a50e24863bb080c85eb7a45 +Subproject commit 3640a3e564919c655f7a2507262b2cbed4261e8b diff --git a/include/pointer.h b/include/pointer.h new file mode 100644 index 0000000..90a749b --- /dev/null +++ b/include/pointer.h @@ -0,0 +1,25 @@ +#ifndef POINTER_H +#define POINTER_H + +#include +#include + +// Max/min value for integer input (0 = 0 degrees, 100 = 180 degrees) +#define MAX_SPEED 100 +#define MIN_SPEED 0 + +// Pointer color and length +#define POINTER_COLOR TFT_RED +#define POINTER_LENGTH 60 + +#define BG_COLOR TFT_BLACK // background color + +extern TFT_eSPI tft; + +void begin(); + +void updatePointerAngle(double theta); + +void updatePointer(int speed); + +#endif \ No newline at end of file diff --git a/include/speedometer.h b/include/speedometer.h new file mode 100644 index 0000000..8385495 --- /dev/null +++ b/include/speedometer.h @@ -0,0 +1,17 @@ +#ifndef SPEED_ARC_H +#define SPEED_ARC_H + +#include +#include + +// Speedometer arc color and radius +#define SPEED_ARC_COLOR TFT_WHITE +#define SPEED_ARC_RADIUS 80 + +#define BG_COLOR TFT_BLACK // background color + +extern TFT_eSPI tft; + +void initSpeedometer(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3747802..56453f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include #include "display.h" +#include "pointer.h" +#include "speedometer.h" void setup() { Serial.begin(115200); @@ -9,12 +11,30 @@ void setup() { // drawSdJpeg("/bsr/Jonathan.jpeg", 130, 0); // HeapAnim(); Serial.println("Setup done."); + begin(); + initSpeedometer(); } void loop() { - rotateColors(); + // rotateColors(); // delay(42); // drawSdJpeg("/test.jpg", 0, 0); // HeapAnim(); + + // pointer loop + // if (Serial.available()) { + // String input = Serial.readStringUntil('\n'); + // input.trim(); + + // if (input.startsWith("a")) { // ex: use a10 to set to 10 degrees + // double angle = input.substring(1).toDouble(); + // updatePointerAngle(angle); + // Serial.printf("set to angle: %.2f deg\n", angle); + // } else { + // int value = input.toInt(); // otherwise use integer method + // updatePointer(value); + // Serial.printf("set to value: %d\n", value); + // } + // } } diff --git a/src/pointer.cpp b/src/pointer.cpp new file mode 100644 index 0000000..ce2db0d --- /dev/null +++ b/src/pointer.cpp @@ -0,0 +1,60 @@ +#include "pointer.h" +#include +#include + +//TFT_eSPI tft = TFT_eSPI(); + +static int pivotX = 0; // x coord of pivot point +static int pivotY = 0; // y coord of pivot point +static int length = POINTER_LENGTH; // length of pointer +static double currentTheta = 0.0; // current angle in degrees + +// erases pointer by painting background colored line over old line +static void erasePointer() { + int oldX = pivotX + length * cos(currentTheta * DEG_TO_RAD); + int oldY = pivotY + length * sin(currentTheta * DEG_TO_RAD); + tft.drawLine(pivotX, pivotY, oldX, oldY, BG_COLOR); +} + +// initializes and sets up pointer +void begin() { + tft.init(); + tft.setRotation(1); // needed for some reason (see display.cpp initDisplay) + tft.fillScreen(BG_COLOR); // fills screen with background color + + // pulls screen width and height + int screenW = tft.width(); + int screenH = tft.height(); + + // sets pivot point at bottom left of screen + pivotX = screenW / 4; + pivotY = screenH / 4; + + // sets pointer to 0 degrees + currentTheta = 0.0; + updatePointerAngle(0.0); + + Serial.println("begin"); +} + +// updates pointer given an angle in degrees +void updatePointerAngle(double theta) { + erasePointer(); // erases old point + currentTheta = theta; // sets new degree to input + + int newX = pivotX + length * cos(theta * DEG_TO_RAD); // calculates new x endpoint + int newY = pivotY + length * sin(theta * DEG_TO_RAD); // calculates new y endpoint + + tft.drawLine(pivotX, pivotY, newX, newY, POINTER_COLOR); // draws new line +} + +// updates pointer given a integer value defined in pointer.h +void updatePointer(int speed) { + // checks that value is between min and max values + if (speed < MIN_SPEED) speed = MIN_SPEED; + if (speed > MAX_SPEED) speed = MAX_SPEED; + + // converts to degrees and calls angle method + double theta = 180.0 * speed / MAX_SPEED; + updatePointerAngle(theta); +} \ No newline at end of file diff --git a/src/speedometer.cpp b/src/speedometer.cpp new file mode 100644 index 0000000..db1fe18 --- /dev/null +++ b/src/speedometer.cpp @@ -0,0 +1,40 @@ +#include "pointer.h" +#include "speedometer.h" +#include +#include + +int lowAngle = MIN_SPEED + 270; +int highAngle = MAX_SPEED - 10; +// Starts up the spedometer arc +void initSpeedometer() { + // Initializes the pointer for the speedometer + + //TFT_eSPI tft = TFT_eSPI(); + int sar = SPEED_ARC_RADIUS; + int sx = tft.width() / 2; + int sy = tft.height() / 2; + // Serial.printf("sx: %d\n ", sx); + // Serial.printf("sy: %d\n ", sy); + + tft.drawSmoothArc(sx, sy, sar, sar, lowAngle, highAngle, SPEED_ARC_COLOR, BG_COLOR, false); + + // Adds a tick mark every 20 degrees, 15 pixels long + // Also adds one in between each 20 degree segment, 10 pixels long + for (int i = 0; i <= 180; i += 20) { + float radian = radians(i); + tft.drawLine(sx + sar * cos(radian), + sy + sar * sin(radian), + sx + (sar - 15) * cos(radian), + sy + (sar - 15) * sin(radian), + SPEED_ARC_COLOR); + radian = radians(i + 10); + if (i >= 180){ + break; + } + tft.drawLine(sx + sar * cos(radian), + sy + sar * sin(radian), + sx + (sar - 10) * cos(radian), + sy + (sar - 10) * sin(radian), + SPEED_ARC_COLOR); + } +}