Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zjwhitehead committed Feb 15, 2024
1 parent d3007c8 commit 1f9c73d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 72 deletions.
3 changes: 1 addition & 2 deletions inc/sp140/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <Arduino.h>

#include "sp140/structs.h"
#include "rp2040-config.h"
#include <Adafruit_ST7735.h>
#include "utilities.h"

Expand Down Expand Up @@ -56,7 +55,7 @@ extern float watts;
extern float wattHoursUsed;

// Set up the display and show splash screen
void setupDisplay(const STR_DEVICE_DATA_140_V1& deviceData, const HardwareConfig& hwConfig);
void setupDisplay(const STR_DEVICE_DATA_140_V1& deviceData);

void displayMeta(const STR_DEVICE_DATA_140_V1& deviceData, int duration = 2000);

Expand Down
30 changes: 0 additions & 30 deletions inc/sp140/rp2040-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,4 @@ struct HardwareConfig {
bool enable_vib;
};

// V1 configuration
HardwareConfig v1_config = {
.button_top = 7,
.buzzer_pin = 2,
.led_sw = LED_BUILTIN,
.throttle_pin = A2,
.serial_esc = &Serial1,
.tft_rst = 6,
.tft_cs = 4,
.tft_dc = 5,
.tft_lite = A3,
.esc_pin = 3,
.enable_vib = true
};

// V2 configuration
HardwareConfig v2_config = {
.button_top = 15,
.buzzer_pin = 10,
.led_sw = 12,
.throttle_pin = A0,
.bmp_pin = 9,
.serial_esc = &Serial1,
.tft_rst = 5,
.tft_cs = 13,
.tft_dc = 11,
.tft_lite = 25,
.esc_pin = 14,
.enable_vib = false
};
#endif // INC_SP140_RP2040_CONFIG_H_
14 changes: 10 additions & 4 deletions src/sp140/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ void resetRotation(unsigned int rotation) {
display->setRotation(rotation); // 1=right hand, 3=left hand
}

#define TFT_RST 6
#define TFT_CS 4
#define TFT_DC 5
#define TFT_LITE A3

// Show splash screen
void displayMeta(const STR_DEVICE_DATA_140_V1& deviceData, int duration) {
display->fillScreen(currentTheme->default_bg);
Expand All @@ -98,16 +103,17 @@ void displayMeta(const STR_DEVICE_DATA_140_V1& deviceData, int duration) {
}

// inital screen setup and config with devicedata and boardconfig passed in
void setupDisplay(const STR_DEVICE_DATA_140_V1& deviceData, const HardwareConfig& board_config) {
void setupDisplay(const STR_DEVICE_DATA_140_V1& deviceData) {
#ifdef RP_PIO
watchdogCausedReboot = watchdog_caused_reboot();
watchdogEnableCausedReboot = watchdog_enable_caused_reboot();
#endif
display = new Adafruit_ST7735(board_config.tft_cs, board_config.tft_dc, board_config.tft_rst);

display = new Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

display->initR(INITR_BLACKTAB); // Init ST7735S chip, black tab
pinMode(board_config.tft_lite, OUTPUT);
digitalWrite(board_config.tft_lite, HIGH); // Backlight on
pinMode(TFT_LITE, OUTPUT);
digitalWrite(TFT_LITE, HIGH); // Backlight on
resetRotation(deviceData.screen_rotation);
setTheme(deviceData.theme); // 0=light, 1=dark
displayMeta(deviceData);
Expand Down
117 changes: 82 additions & 35 deletions src/sp140/sp140.ino
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ TaskHandle_t updateDisplayTaskHandle = NULL;
SemaphoreHandle_t eepromSemaphore;
SemaphoreHandle_t tftSemaphore;

// V1 configuration
HardwareConfig v1_config = {
.button_top = 7,
.buzzer_pin = 2,
.led_sw = LED_BUILTIN,
.throttle_pin = A2,
.serial_esc = &Serial1,
.tft_rst = 6,
.tft_cs = 4,
.tft_dc = 5,
.tft_lite = A3,
.esc_pin = 3,
.enable_vib = true
};

// V2 configuration
HardwareConfig v2_config = {
.button_top = 15,
.buzzer_pin = 10,
.led_sw = 12,
.throttle_pin = A0,
.bmp_pin = 9,
.serial_esc = &Serial1,
.tft_rst = 5,
.tft_cs = 13,
.tft_dc = 11,
.tft_lite = 25,
.esc_pin = 14,
.enable_vib = false
};

#pragma message "Warning: OpenPPG software is in beta"

void blinkLEDTask(void *pvParameters) {
Expand Down Expand Up @@ -164,10 +195,7 @@ void loadHardwareConfig() {
button_top = new AceButton(board_config.button_top);
buttonConfig = button_top->getButtonConfig();
}

// the setup function runs once when you press reset or power the board
void setup() {

void commonSetup() {
Serial.begin(115200);
SerialESC.begin(ESC_BAUD_RATE);
SerialESC.setTimeout(ESC_TIMEOUT);
Expand All @@ -178,55 +206,73 @@ void setup() {
usb_web.setLineStateCallback(line_state_callback);
#endif

//Serial.print(F("Booting up (USB) V"));
//Serial.print(VERSION_MAJOR + "." + VERSION_MINOR);

loadHardwareConfig();

if (deviceData.revision == M0) {
pinMode(board_config.bmp_pin, OUTPUT);
digitalWrite(board_config.bmp_pin, HIGH);
} else if (deviceData.revision == V1) {
// no custom setup for v1
} else if (deviceData.revision == MODULE) {
pinMode(board_config.bmp_pin, OUTPUT);
digitalWrite(board_config.bmp_pin, HIGH);

pixels.begin();
pixels.setPixelColor(0, LED_YELLOW);
pixels.show();
}

analogReadResolution(12); // M0 family of chips provide 12bit resolution
pot->setAnalogResolution(4096);
unsigned int startup_vibes[] = { 27, 27, 0 };

initButtons();
setupTasks();

#ifdef M0_PIO
Watchdog.enable(5000);
uint8_t eepStatus = eep.begin(eep.twiClock100kHz);
#elif RP_PIO
watchdog_enable(5000, 1);
EEPROM.begin(512);
#endif
refreshDeviceData();

Serial.print(F("revision "));
Serial.println(deviceData.revision);

loadHardwareConfig();
setup140();
#ifdef M0_PIO
Watchdog.reset();
#endif
setupAltimeter();
setupTelemetry();

setupDisplay(deviceData, board_config);
if (button_top->isPressedRaw()) {
modeSwitch(false);
}
vTaskResume(updateDisplayTaskHandle);
vTaskResume(checkButtonTaskHandle);
}

void setupM0() {
#ifdef M0_PIO
Watchdog.enable(5000);
uint8_t eepStatus = eep.begin(eep.twiClock100kHz);

Serial.println(F("M0"));
pinMode(board_config.bmp_pin, OUTPUT);
digitalWrite(board_config.bmp_pin, HIGH);

Watchdog.reset(); //reset since setup can take a few secs
#endif
}

void setupRP() {
#ifdef RP_PIO
watchdog_enable(5000, 1);
EEPROM.begin(512);

if (deviceData.revision == M0) {
deviceData.revision = V1;
writeDeviceData();
}

if (deviceData.revision == MODULE) {
pinMode(board_config.bmp_pin, OUTPUT);
digitalWrite(board_config.bmp_pin, HIGH);

pixels.begin();
pixels.setPixelColor(0, LED_ORANGE);
pixels.show();
}
#endif
}

void setup() {
commonSetup();

if (deviceData.revision == M0) {
setupM0();
} else if (deviceData.revision == V1 || deviceData.revision == MODULE) {
setupRP();
}
}

void setupTelemetry() {
telemetryData.temperatureC = __FLT_MIN__;
}
Expand Down Expand Up @@ -323,6 +369,7 @@ void loop() {
#endif

delay(100);
Serial.println("loop");
//ps();
//psTop();
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp140/utilities.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ String convertToDigits(byte digits) {
digits_string.concat(digits);
return digits_string;
}
#define ENABLE_NEOPIXEL true

#define ENABLE_NEOPIXEL false // TODO make this a config option

void setLEDs(byte state) {
if (ENABLE_NEOPIXEL) {
Expand Down

0 comments on commit 1f9c73d

Please sign in to comment.