Browse Source

control light

main
Hendrik Langer 7 years ago
parent
commit
46dbc5b7b0
  1. 5
      src/led.cpp
  2. 1
      src/led.h
  3. 1
      src/main.cpp
  4. 72
      src/screen.cpp
  5. 15
      src/screen.h

5
src/led.cpp

@ -83,6 +83,11 @@ void Led::changeAnimation(uint8_t num, uint16_t duration) {
else if (num == 255) refresh = &Led::loop_off;
}
bool Led::isOn() {
if (refresh && refresh != &Led::loop_off && FastLED.getBrightness() != 0) return true;
else return false;
}
void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) {
color = CRGB(r, g, b);
}

1
src/led.h

@ -17,6 +17,7 @@ class Led {
void setBrightness(uint8_t);
void stop(void);
void wakeUpLight(uint8_t);
bool isOn();
private:
CRGB leds[NUM_LEDS];
void animationTask(void*);

1
src/main.cpp

@ -405,6 +405,7 @@ void loop()
else if (menuChange == eStationMenu) screen = new StationMenu();
else if (menuChange == eAlarmClockScreen) screen = new AlarmClockScreen();
else if (menuChange == eSuspendScreen) screen = new SuspendScreen();
else if (menuChange == eLightScreen) screen = new LightScreen();
else screen = new MainScreen();
menuChange = eNone;
}

72
src/screen.cpp

@ -60,9 +60,9 @@ MainMenu::MainMenu() {
string_list = {
"PLAY",
"Radio Stations",
"Toggle Light",
"Test2",
"AlarmClock",
"Toggle Light",
"Wohnzimmer L",
"SLEEP",
"\xAB Return"};
if (mp3.playing) {
@ -71,7 +71,6 @@ MainMenu::MainMenu() {
}
uint8_t MainMenu::select() {
String topic = "esp32-node/cmd/" + iot.hostname + "/button";
switch (current_pos) {
case 0:
if (mp3.playing) {
@ -87,13 +86,15 @@ uint8_t MainMenu::select() {
return current_pos;
break;
case 2:
iot.mqtt.publish(topic.c_str(), 0, false, "light" );
menuChange = eAlarmClockScreen;
return current_pos;
break;
case 3:
iot.mqtt.publish(topic.c_str(), 0, false, "test2" );
if (!led.isOn()) led.changeAnimation(3, 0);
else led.changeAnimation(255, 0);
break;
case 4:
menuChange = eAlarmClockScreen;
menuChange = eLightScreen;
return current_pos;
break;
case 5:
@ -371,3 +372,62 @@ uint8_t AlarmClockScreen::select() {
void SuspendScreen::draw() {
suspend(30);
}
LightScreen::LightScreen() {
brightness = 50;
}
void LightScreen::draw() {
uint8_t header_height = 12;
u8g2.clearBuffer();
/* draw Title */
u8g2.setFont(u8g2_font_inb19_mf);
u8g2.drawStr(15, 20, "Brightness");
u8g2.drawRBox(10, 28, 108, 22, 5);
u8g2.setDrawColor(0);
uint32_t percent = (uint32_t)brightness*100 / 255;
u8g2.drawBox(14+percent, 31, 100-percent, 16);
char valueStr[6];
sprintf(valueStr, "%3d", brightness);
u8g2.setFont(u8g2_font_profont17_mf);
u8g2.setFontMode(1);
u8g2.setDrawColor(2);
u8g2.drawStr(40, 45, valueStr);
u8g2.setFontMode(0);
u8g2.setDrawColor(1);
u8g2.sendBuffer(); // transfer internal memory to the display
}
void LightScreen::next() {
if (millis() - lastRotary <= 40) brightness+=20;
else if (millis() - lastRotary <= 80) brightness+=5;
else brightness++;
if (brightness > 255) brightness = 255;
lastRotary = millis();
}
void LightScreen::previous() {
if (millis() - lastRotary <= 40) brightness-=20;
else if (millis() - lastRotary <= 80) brightness-=5;
else brightness--;
if (brightness < 0) brightness = 0;
lastRotary = millis();
}
uint8_t LightScreen::select() {
String topic = "esp32-node/cmd/" + iot.hostname + "/button";
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["brightness"] = brightness;
root["light"] = brightness > 0 ? "ON" : "OFF";
char publishBuf[root.measureLength()+1];
root.printTo(publishBuf, sizeof(publishBuf));
iot.mqtt.publish(topic.c_str(), 0, false, publishBuf );
menuChange = eMainScreen;
}

15
src/screen.h

@ -22,7 +22,7 @@
#include "image.h"
#include "led.h"
enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen };
enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen, eLightScreen };
extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2;
extern char timeStr[];
@ -128,4 +128,17 @@ class SuspendScreen : public Screen {
int progress = 0;
};
class LightScreen : public Screen {
public:
LightScreen();
void draw(void) override;
void next(void) override;
void previous(void) override;
uint8_t select(void) override;
const char* title = "Wohnzimmer L";
private:
uint32_t lastRotary = 0;
int brightness;
};
#endif /* _SCREEN_H */

Loading…
Cancel
Save