Skip to content

Commit

Permalink
WIP sparkfun rp2040 module
Browse files Browse the repository at this point in the history
  • Loading branch information
zjwhitehead committed Nov 20, 2023
1 parent 3fb3be7 commit 10e2197
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
14 changes: 7 additions & 7 deletions inc/sp140/rp2040-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
#include "shared-config.h"

// Arduino Pins
#define BUTTON_TOP 15 // arm/disarm button_top
#define BUTTON_TOP 6 // arm/disarm button_top
#define BUTTON_SIDE 7 // secondary button_top
#define BUZZER_PIN 10 // output for buzzer speaker
#define LED_SW 12 // output for LED
#define LED_SW LED_BUILTIN // output for LED
#define THROTTLE_PIN A0 // throttle pot input

#define SerialESC Serial1 // ESC UART connection

// SP140
#define POT_PIN A0
#define TFT_RST 5
#define TFT_CS 13
#define TFT_DC 11
#define TFT_LITE 25
#define ESC_PIN 14
#define TFT_RST 2
#define TFT_CS 4
#define TFT_DC 3
#define TFT_LITE 5
#define ESC_PIN 8
#define ENABLE_VIB false // enable vibration

#endif // INC_SP140_RP2040_CONFIG_H_
13 changes: 13 additions & 0 deletions inc/sp140/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@

double mapd(double x, double in_min, double in_max, double out_min, double out_max);

// Definitions for main rainbow colors in WRGB format for NeoPixel.
// The 32-bit color value is WRGB. W (White) is ignored for RGB pixels.
// The next bytes are R (Red), G (Green), and B (Blue).
// For example, YELLOW is 0x00FFFF00, with FF for Red and Green, and 00 for Blue.

#define LED_RED 0x00FF0000
#define LED_ORANGE 0x00FF7F00
#define LED_YELLOW 0x00FFFF00
#define LED_GREEN 0x0000FF00
#define LED_BLUE 0x000000FF
#define LED_INDIGO 0x004B0082
#define LED_VIOLET 0x008000FF

#endif // INC_SP140_UTILITIES_H_
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ lib_deps =
adafruit/Adafruit BMP3XX [email protected]
adafruit/Adafruit DRV2605 [email protected]
adafruit/Adafruit ST7735 and ST7789 [email protected]
adafruit/Adafruit NeoPixel@^1.12.0
https://github.com/rlogiacco/CircularBuffer
Adafruit GFX [email protected]
lib_ignore =
Expand Down
26 changes: 16 additions & 10 deletions src/sp140/sp140.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <extEEPROM.h> // https://github.com/PaoloP74/extEEPROM
#elif RP_PIO
// rp2040 specific libraries here
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
#include "hardware/watchdog.h"
#include "pico/unique_id.h"
Expand Down Expand Up @@ -64,6 +65,8 @@ ButtonConfig* buttonConfig = button_top.getButtonConfig();
CircularBuffer<float, 50> voltageBuffer;
CircularBuffer<int, 8> potBuffer;

Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

bool armed = false;
uint32_t armedAtMillis = 0;
uint32_t cruisedAtMillisMilis = 0;
Expand Down Expand Up @@ -96,7 +99,7 @@ void throttleTask(void *pvParameters) {
(void) pvParameters; // this is a standard idiom to avoid compiler warnings about unused parameters.

for (;;) { // infinite loop
handleThrottle(); //
handleThrottle(); //
delay(22); // wait for 22ms
}
vTaskDelete(NULL); // should never reach this
Expand All @@ -106,7 +109,7 @@ void telemetryTask(void *pvParameters) {
(void) pvParameters; // this is a standard idiom to avoid compiler warnings about unused parameters.

for (;;) { // infinite loop
handleTelemetry();
handleTelemetry();
delay(50); // wait for 50ms
}
vTaskDelete(NULL); // should never reach this
Expand All @@ -116,7 +119,7 @@ void trackPowerTask(void *pvParameters) {
(void) pvParameters; // this is a standard idiom to avoid compiler warnings about unused parameters.

for (;;) { // infinite loop
trackPower();
trackPower();
delay(250); // wait for 250ms
}
vTaskDelete(NULL); // should never reach this
Expand All @@ -126,7 +129,7 @@ void checkButtonTask(void *pvParameters) {
(void) pvParameters; // this is a standard idiom to avoid compiler warnings about unused parameters.

for (;;) { // infinite loop
checkButtons();
checkButtons();
delay(5); // wait for 5ms
}
vTaskDelete(NULL); // should never reach this
Expand All @@ -136,9 +139,9 @@ void updateDisplayTask(void *pvParameters) { //TODO set core affinity to one cor
(void) pvParameters; // this is a standard idiom to avoid compiler warnings about unused parameters.

for (;;) { // infinite loop
// TODO separate alt reading out to its own task. Avoid blocking display updates when alt reading is slow etc
// TODO separate alt reading out to its own task. Avoid blocking display updates when alt reading is slow etc
// TODO use queues to pass data between tasks (xQueueOverwrite)
const float altitude = getAltitude(deviceData);
const float altitude = getAltitude(deviceData);
updateDisplay( deviceData, telemetryData, altitude, armed, cruising, armedAtMillis);
delay(250); // wait for 250ms
}
Expand All @@ -162,6 +165,9 @@ void setup() {
//Serial.print(VERSION_MAJOR + "." + VERSION_MINOR);

pinMode(LED_SW, OUTPUT); // set up the internal LED2 pin
pixels.begin();
pixels.setPixelColor(0, LED_YELLOW);
pixels.show();

analogReadResolution(12); // M0 family chip provides 12bit resolution
pot.setAnalogResolution(4096);
Expand Down Expand Up @@ -205,7 +211,7 @@ void setupTasks() {
NULL, // parameters passed into the task
3, // priority, with 3 being the highest, and 0 being the lowest.
&checkButtonTaskHandle); // used to pass back a handle by which the created task can be referenced.

if (checkButtonTaskHandle != NULL) {
vTaskSuspend(checkButtonTaskHandle); // Suspend the task immediately after creation
}
Expand Down Expand Up @@ -463,7 +469,7 @@ bool armSystem() {
armedAtMillis = millis();
setGroundAltitude(deviceData);

vTaskSuspend(blinkLEDTaskHandle);
vTaskSuspend(blinkLEDTaskHandle);
setLEDs(HIGH); // solid LED while armed
runVibe(arm_vibes, 3);
playMelody(arm_melody, 3);
Expand All @@ -487,8 +493,8 @@ void setCruise() {
// or gradually change color from blue to yellow with time
if (!throttleSafe()) { // using pot/throttle
cruisedPotVal = pot.getValue(); // save current throttle val
cruisedAtMillis = millis(); // start timer
// throttle handle runs fast and a lot. need to set the timer before
cruisedAtMillis = millis(); // start timer
// throttle handle runs fast and a lot. need to set the timer before
// setting cruise so its updated in time
// TODO since these values are accessed in another task make sure memory safe
cruising = true;
Expand Down
19 changes: 15 additions & 4 deletions src/sp140/utilities.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,27 @@ String convertToDigits(byte digits) {
digits_string.concat(digits);
return digits_string;
}
#define ENABLE_NEOPIXEL true


void setLEDs(byte state) {
// digitalWrite(LED_2, state);
// digitalWrite(LED_3, state);
digitalWrite(LED_SW, state);
if (ENABLE_NEOPIXEL) {
if (state == HIGH) {
pixels.setPixelColor(0, LED_YELLOW);
} else {
pixels.clear();
}
pixels.show();
} else {
digitalWrite(LED_SW, state);
}
}

// toggle LEDs
byte ledState = LOW;

void blinkLED() {
byte ledState = !digitalRead(LED_SW);
ledState = !ledState;
setLEDs(ledState);
}

Expand Down

0 comments on commit 10e2197

Please sign in to comment.