Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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_VALUE 100
#define MIN_VALUE 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 value);

#endif
17 changes: 17 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Arduino.h>
#include "display.h"
#include "pointer.h"

void setup() {
Serial.begin(115200);
Expand All @@ -17,4 +18,20 @@ void loop() {
// 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 middle of screen
pivotX = screenW / 2;
pivotY = screenH / 2;

// 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 value) {
// checks that value is between min and max values
if (value < MIN_VALUE) value = MIN_VALUE;
if (value > MAX_VALUE) value = MAX_VALUE;

// converts to degrees and calls angle method
double theta = 180.0 * value / MAX_VALUE;
updatePointerAngle(theta);
}