This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
serial_debugger/serial_debugger.ino

100 lines
2.5 KiB
C++

// Varous system includes
#include <Arduino.h>
// Various library includes
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// Debugging via serial monitor (don't turn this on unless you're hacking on the firmware code)
#define DEBUG true
// Battery level measurement
#define VBATPIN A6
float measuredVBat;
float batteryPercent;
// TFT Setup
#define TFT_CS 9
#define TFT_DC 10
Adafruit_ILI9341 tft(TFT_CS, TFT_DC);
#include <Fonts/FreeMono9pt7b.h>
#define CHARS_HORIZONTAL 28
#define CHARS_ROWS 13
// NeoPixels
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
// UI screens
void screenClear();
// Color conversion (RGB888 -> RGB565 used by Adafruit GFX)
uint16_t RGB565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0b11111000) << 8) | ((g & 0b11111100) << 3) | (b >> 3);
}
void setup() {
// Setup red LED to indicate device is on (in case we disable NeoPixel battery level later)
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
// Setup NeoPixels
pixels.begin();
pixels.clear();
pixels.setBrightness(10);
// Green : pixels.Color(0, 255, 0)
// Yellow : pixels.Color(255, 255, 0)
// Orange : pixels.Color(255, 128, 0)
// Red : pixels.Color(255, 0, 0)
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
// Setup TFT
tft.begin();
tft.setRotation(1);
screenClear();
tft.println("");
tft.setFont(&FreeMono9pt7b);
}
// Measure battery level and change NeoPixel accordingly
void batteryLevel() {
measuredVBat = analogRead(VBATPIN);
measuredVBat *= 2; // we divided by 2, so multiply back
measuredVBat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredVBat /= 1024; // convert to voltage
batteryPercent = measuredVBat / 3.3;
if (batteryPercent >= 0.75) {
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
}
else if (batteryPercent >= 0.50) {
pixels.setPixelColor(0, pixels.Color(255, 255, 0));
}
else if (batteryPercent >= 0.25) {
pixels.setPixelColor(0, pixels.Color(255, 128, 0));
}
else {
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
}
pixels.show();
if (DEBUG) {
Serial.print("VBat: " ); Serial.println(measuredVBat);
Serial.print("Bat %: "); Serial.println(batteryPercent);
}
}
void loop() {
// Update battery level as appropriate
batteryLevel();
delay(250);
}
// Clear the screen
void screenClear() {
tft.setCursor(0, 0);
tft.fillScreen(RGB565(0, 0, 0));
}