Skip to content

Commit

Permalink
Merge pull request #85 from eadmaster/serialcmds
Browse files Browse the repository at this point in the history
added power serial cmds, wakeup cardputer by pressing any key (#64)
  • Loading branch information
pr3y authored Jul 26, 2024
2 parents 8acf3da + 51a2e5e commit b728503
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
90 changes: 74 additions & 16 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#include "cJSON.h"
#include <inttypes.h> // for PRIu64

#include <ESP8266Audio.h>
#include <ESP8266SAM.h>
#ifndef STICK_C_PLUS
#include <ESP8266Audio.h>
#include <ESP8266SAM.h>
#endif

#include "sd_functions.h"
#include "settings.h"
#include "display.h"
#include "powerSave.h"
#include "modules/rf/rf.h"


Expand Down Expand Up @@ -54,6 +58,10 @@ void handleSerialCommands() {

// TODO: more commands https://docs.flipper.net/development/cli#0Z9fs

if(cmd_str == "" ) { // empty
return;
}

if(cmd_str.startsWith("ir") ) {

// ir tx <protocol> <address> <command>
Expand Down Expand Up @@ -194,11 +202,12 @@ void handleSerialCommands() {
}
} // endof rf

if(cmd_str.startsWith("music_player " ) || cmd_str.startsWith("ttf" ) ) {
#ifndef STICK_C_PLUS
if(cmd_str.startsWith("music_player " ) || cmd_str.startsWith("tts" ) || cmd_str.startsWith("say" ) ) {
// TODO: move in audio.cpp module
AudioOutputI2S *audioout = new AudioOutputI2S(); // https://github.com/earlephilhower/ESP8266Audio/blob/master/src/AudioOutputI2S.cpp#L32
#ifdef CARDPUTER
audioout->SetPinout(41, 43, 42);
audioout->SetPinout(41, 43, 42); // bclk, wclk, dout
// TODO: other pinouts
#endif
AudioGenerator* generator = NULL;
Expand Down Expand Up @@ -235,8 +244,8 @@ void handleSerialCommands() {
if(song.endsWith(".mid")) {
// need to load a soundfont
AudioFileSource* sf2 = NULL;
if(setupSdCard()) sf2 = new AudioFileSourceSD("audio/1mgm.sf2"); // TODO: make configurable
if(!sf2) sf2 = new AudioFileSourceLittleFS("audio/1mgm.sf2"); // TODO: make configurable
if(setupSdCard()) sf2 = new AudioFileSourceSD("1mgm.sf2"); // TODO: make configurable
if(!sf2) sf2 = new AudioFileSourceLittleFS("1mgm.sf2"); // TODO: make configurable
if(sf2) {
// a soundfount was found
AudioGeneratorMIDI* midi = new AudioGeneratorMIDI();
Expand Down Expand Up @@ -281,30 +290,79 @@ void handleSerialCommands() {
return;
}
} // end of music_player
#endif

// WIP: record | mic
// https://github.com/earlephilhower/ESP8266Audio/issues/70
// https://github.com/earlephilhower/ESP8266Audio/pull/118

/* TODO: rewrite using the new screen dimmer feat.
if(cmd_str.startsWith("lcd " ) || cmd_str.startsWith("tft" ) ) {
String new_status = cmd_str.substring(strlen("lcd "), cmd_str.length());
if(new_status=="off") {
analogWrite(BACKLIGHT, 0);
esp_timer_stop(screensaver_timer);
} else if(new_status=="on") {
getBrightness(); // reinit brightness
reset_screensaver_timer();
// backlight brightness adjust (range 0-255) https://docs.flipper.net/development/cli/#XQQAI
// e.g. "led br 127"
if(cmd_str.startsWith("led br ")) {
const char* valueStr = cmd_str.c_str() + strlen("led br ");
int value = (atoi(valueStr) * 100) / 255; // convert to 0-100 range
//Serial.print("value: ");
//Serial.println(value);
if(value<=0) value=1;
if(value>100) value=100;
setBrightness(value, false); // false -> do not save
return;
}
else if(cmd_str.startsWith("led ")) {
// change UI color
// e.g. "led 255 255 255"
const char* rgbString = cmd_str.c_str() + 4;
int r, g, b;
if (sscanf(rgbString, "%d %d %d", &r, &g, &b) != 3) {
Serial.println("invalid color: " + String(rgbString));
return;
}
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
Serial.println("invalid color: " + String(rgbString));
return;
}
uint16_t hexColor = tft.color565(r, g, b); // Use the TFT_eSPI function to convert RGB to 16-bit color
//Serial.print("converted color:");
//SerialPrintHexString(hexColor);
FGCOLOR = hexColor; // change global var, dont save in settings
return;
}
*/

// power cmds: off, reboot, sleep
if(cmd_str == "power off" ) {
// closest thing https://github.com/esp8266/Arduino/issues/929
#if defined(STICK_C_PLUS)
axp192.PowerOff();
#elif defined(STICK_C_PLUS2)
digitalWrite(4,LOW);
#else
//ESP.deepSleep(0);
esp_deep_sleep_start(); // only wake up via hardware reset
#endif
return;
}
if(cmd_str == "power reboot" ) {
ESP.restart();
return;
}
if(cmd_str == "power sleep" ) {
// NOTE: cmd not supported on flipper0
setSleepMode();
//turnOffDisplay();
//esp_timer_stop(screensaver_timer);
return;
}

if(cmd_str == "clock" ) {
//esp_timer_stop(screensaver_timer); // disable screensaver while the clock is running
runClockLoop();
return;
}

// TODO: "storage" cmd to manage files https://docs.flipper.net/development/cli/#Xgais

// TODO: "gpio" cmds https://docs.flipper.net/development/cli/#aqA4b


Serial.println("unsupported serial command: " + cmd_str);

Expand Down
2 changes: 1 addition & 1 deletion src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void setSleepMode() {
sleepModeOn();
while (1) {
#if defined(CARDPUTER)
if (checkEscPress() || checkSelPress())
if (checkAnyKeyPress())
#else
if (checkSelPress())
#endif
Expand Down

0 comments on commit b728503

Please sign in to comment.