Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion embedded-pio
25 changes: 25 additions & 0 deletions include/pointer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef POINTER_H
#define POINTER_H

#include <TFT_eSPI.h>
#include <Arduino.h>

// 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
17 changes: 17 additions & 0 deletions include/speedometer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SPEED_ARC_H
#define SPEED_ARC_H

#include <TFT_eSPI.h>
#include <Arduino.h>

// 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
22 changes: 21 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <Arduino.h>
#include "display.h"
#include "pointer.h"
#include "speedometer.h"

void setup() {
Serial.begin(115200);
Expand All @@ -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);
// }
// }
}
60 changes: 60 additions & 0 deletions src/pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "pointer.h"
#include <Arduino.h>
#include <math.h>

//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);
}
40 changes: 40 additions & 0 deletions src/speedometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "pointer.h"
#include "speedometer.h"
#include <Arduino.h>
#include <math.h>

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);
}
}