Baseline: Migrated from local arduino IDE project, compiles but contains dead code and needs refactoring
This commit is contained in:
119
src/Programs/Calculator/Calculator.cpp
Normal file
119
src/Programs/Calculator/Calculator.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include <Arduino.h>
|
||||
#include "Calculator.h"
|
||||
|
||||
namespace Calculator{
|
||||
void Calculator::setup(){
|
||||
// Code for setup
|
||||
}
|
||||
void Calculator::loop(Adafruit_SSD1351 *tft, inputData *userInput){
|
||||
|
||||
static int previousJoystickCommand = COMMAND_NO;
|
||||
int currentJoystickCommand = userInput->joystickCommand;
|
||||
|
||||
if ((currentJoystickCommand & COMMAND_UP) && !(previousJoystickCommand & COMMAND_UP)) {
|
||||
cursorY--;
|
||||
if (cursorY < 0) cursorY = 3;
|
||||
}
|
||||
if ((currentJoystickCommand & COMMAND_DOWN) && !(previousJoystickCommand & COMMAND_DOWN)) {
|
||||
cursorY++;
|
||||
if (cursorY > 3) cursorY = 0;
|
||||
}
|
||||
if ((currentJoystickCommand & COMMAND_LEFT) && !(previousJoystickCommand & COMMAND_LEFT)) {
|
||||
cursorX--;
|
||||
if (cursorX < 0) cursorX = 3;
|
||||
}
|
||||
if ((currentJoystickCommand & COMMAND_RIGHT) && !(previousJoystickCommand & COMMAND_RIGHT)) {
|
||||
cursorX++;
|
||||
if (cursorX > 3) cursorX = 0;
|
||||
}
|
||||
|
||||
if (userInput->joyStickButton.isReleased()) {
|
||||
const char* selectedButton = buttonGrid[cursorY][cursorX];
|
||||
handleButtonPress(selectedButton);
|
||||
}
|
||||
|
||||
previousJoystickCommand = currentJoystickCommand;
|
||||
|
||||
drawCalculator(tft);
|
||||
}
|
||||
|
||||
void Calculator::handleButtonPress(const char* button) {
|
||||
char buttonChar = button[0];
|
||||
|
||||
if (buttonChar >= '0' && buttonChar <= '9') {
|
||||
if (strcmp(display, "0") == 0 || newNumber) {
|
||||
strcpy(display, button);
|
||||
newNumber = false;
|
||||
} else if (strlen(display) < 15) {
|
||||
strcat(display, button);
|
||||
}
|
||||
} else if (buttonChar == 'C') {
|
||||
strcpy(display, "0");
|
||||
storedValue = 0;
|
||||
operation = ' ';
|
||||
newNumber = true;
|
||||
} else if (buttonChar == '+' || buttonChar == '-' || buttonChar == '*' || buttonChar == '/') {
|
||||
storedValue = atof(display);
|
||||
operation = buttonChar;
|
||||
newNumber = true;
|
||||
} else if (buttonChar == '=') {
|
||||
if (operation != ' ') {
|
||||
double secondValue = atof(display);
|
||||
double result = 0;
|
||||
switch (operation) {
|
||||
case '+': result = storedValue + secondValue; break;
|
||||
case '-': result = storedValue - secondValue; break;
|
||||
case '*': result = storedValue * secondValue; break;
|
||||
case '/': result = storedValue / secondValue; break;
|
||||
}
|
||||
dtostrf(result, 0, 2, display);
|
||||
operation = ' ';
|
||||
newNumber = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Calculator::onEnter(Adafruit_SSD1351 *tft){
|
||||
// Code to run when program is entered
|
||||
Calculator::disableInfoBar = true;
|
||||
// clear the screen
|
||||
tft->fillRect(0, 0, 128, 128, BLACK);
|
||||
}
|
||||
|
||||
void Calculator::onExit(){
|
||||
// Code to run when program is exited
|
||||
}
|
||||
void Calculator::drawCalculator(Adafruit_SSD1351 *tft) {
|
||||
// Clear only the top display area to reduce flicker
|
||||
tft->fillRect(0, 0, 128, 30, BLACK);
|
||||
tft->setTextSize(2);
|
||||
tft->setCursor(10, 10);
|
||||
tft->println(display);
|
||||
|
||||
int buttonWidth = 32;
|
||||
int buttonHeight = 24;
|
||||
int startX = 0;
|
||||
int startY = 30;
|
||||
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 4; x++) {
|
||||
int buttonX = startX + x * buttonWidth;
|
||||
int buttonY = startY + y * buttonHeight;
|
||||
|
||||
if (x == cursorX && y == cursorY) {
|
||||
// draw selected Item
|
||||
tft->fillRect(buttonX, buttonY, buttonWidth, buttonHeight, WHITE);
|
||||
tft->setTextColor(BLACK);
|
||||
} else {
|
||||
// Fill the button background black to "un-highlight" it
|
||||
tft->fillRect(buttonX, buttonY, buttonWidth, buttonHeight, BLACK);
|
||||
//tft->drawRect(buttonX, buttonY, buttonWidth, buttonHeight, WHITE);
|
||||
tft->setTextColor(WHITE);
|
||||
}
|
||||
|
||||
tft->setCursor(buttonX + 10, buttonY + 5);
|
||||
tft->print(buttonGrid[y][x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user