Last active
March 29, 2025 16:34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5StickCPlus2.h> | |
#include <M5Unified.h> | |
// DISPLAY | |
// SIZE: 135x240 | |
// -- TEXT SIZE | |
#define BIG_TEXT 4 | |
#define MEDIUM_TEXT 3 | |
#define SMALL_TEXT 2 | |
#define TINY_TEXT 1 | |
// -- TEXT COLOR | |
#define TEXT_COLOR_BACK 0x0000 | |
#define TEXT_COLOR_BLACK 0x0000 | |
#define TEXT_COLOR_NAVY 0x000F | |
#define TEXT_COLOR_DARKGREEN 0x03E0 | |
#define TEXT_COLOR_DARKCYAN 0x03EF | |
#define TEXT_COLOR_MAROON 0x7800 | |
#define TEXT_COLOR_PURPLE 0x780F | |
#define TEXT_COLOR_OLIVE 0x7BE0 | |
#define TEXT_COLOR_LIGHTGREY 0xC618 | |
#define TEXT_COLOR_DARKGREY 0x7BEF | |
#define TEXT_COLOR_BLUE 0x001F | |
#define TEXT_COLOR_GREEN 0x07E0 | |
#define TEXT_COLOR_CYAN 0x07FF | |
#define TEXT_COLOR_RED 0xF800 | |
#define TEXT_COLOR_MAGENTA 0xF81F | |
#define TEXT_COLOR_YELLOW 0xFFE0 | |
#define TEXT_COLOR_WHITE 0xFFFF | |
#define TEXT_COLOR_ORANGE 0xFDA0 | |
#define TEXT_COLOR_GREENYELLOW 0xB7E0 | |
#define TEXT_COLOR_PINK 0xFC9F | |
// -- ROTATION | |
#define DISPLAY_ROTATION_HORIZONTAL_FLIPPED 3 | |
#define DISPLAY_ROTATION_VERTICAL_FLIPPED 2 | |
#define DISPLAY_ROTATION_HORIZONTAL 1 | |
#define DISPLAY_ROTATION_VERTICAL 0 | |
// FEATURES | |
#define ACTIVE_LOW_IR | |
#define M5LED 19 | |
#define ROTATION | |
#define USE_EEPROM | |
#define RTC // plus2 has a BM8563 RTC but the class isn't the same, needs work. | |
#define SDCARD // Requires a custom-built adapter, but can have one. | |
#define PWRMGMT | |
#define SPEAKER M5.Speaker | |
// BUTTONS | |
#define BTN_A StickCP2.BtnA | |
#define BTN_B StickCP2.BtnB | |
#define BTN_PWR StickCP2.BtnPWR | |
const uint16_t colors[] = { | |
TEXT_COLOR_NAVY, TEXT_COLOR_DARKGREEN, TEXT_COLOR_PINK, | |
TEXT_COLOR_DARKCYAN, TEXT_COLOR_MAROON, TEXT_COLOR_PURPLE, | |
TEXT_COLOR_OLIVE, TEXT_COLOR_LIGHTGREY, TEXT_COLOR_DARKGREY, | |
TEXT_COLOR_BLUE, TEXT_COLOR_GREEN, TEXT_COLOR_CYAN, | |
TEXT_COLOR_RED, TEXT_COLOR_MAGENTA, TEXT_COLOR_YELLOW, | |
TEXT_COLOR_WHITE, TEXT_COLOR_ORANGE, TEXT_COLOR_GREENYELLOW | |
}; | |
void initDeviceConfig() { | |
auto cfg = M5.config(); | |
StickCP2.begin(cfg); | |
} | |
void initDisplayConfig() { | |
StickCP2.Display.setRotation(DISPLAY_ROTATION_HORIZONTAL_FLIPPED); | |
StickCP2.Display.setTextColor(TEXT_COLOR_GREEN); | |
StickCP2.Display.setTextSize(SMALL_TEXT); | |
} | |
void setRandomTextColor() { | |
int index = random(0, sizeof(colors) / sizeof(colors[0])); | |
uint16_t randomColor = colors[index]; | |
StickCP2.Display.setTextColor(randomColor); | |
StickCP2.Display.clear(); | |
} | |
void clockText() { | |
// This method is to set the RTC module inital date | |
// StickCP2.Rtc.setDateTime({{2025, 3, 29}, {12, 48, 0}}); | |
auto dt = StickCP2.Rtc.getDateTime(); | |
static constexpr const char* const wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"}; | |
StickCP2.Display.setCursor(0, 20); | |
StickCP2.Display.printf("%s, %02d/%02d/%04d\n", wd[dt.date.weekDay], dt.date.date, dt.date.month, dt.date.year); | |
StickCP2.Display.setCursor(M5.Display.width() / 4, M5.Display.height() / 2); | |
StickCP2.Display.printf("%02d:%02d:%02d", dt.time.hours, dt.time.minutes, dt.time.seconds); | |
} | |
void batteryText() { | |
StickCP2.Display.setCursor(M5.Display.width() / 1.7, 115); | |
StickCP2.Display.printf("B%%: %d", M5.Power.getBatteryLevel()); | |
} | |
void setup() { | |
initDeviceConfig(); | |
initDisplayConfig(); | |
} | |
void loop(void) { | |
StickCP2.Display.clear(); | |
StickCP2.update(); | |
clockText(); | |
batteryText(); | |
if(BTN_A.wasPressed()) { | |
setRandomTextColor(); | |
} | |
M5.delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment