-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathGwLedTask.cpp
89 lines (86 loc) · 2.06 KB
/
GwLedTask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "GwLedTask.h"
#include "GwHardware.h"
#include "GwApi.h"
void handleLeds(GwApi *api);
void initLeds(GwApi *param)
{
#ifdef GWLED_FASTLED
param->addUserTask(handleLeds, "handleLeds");
#endif
}
#ifdef GWLED_FASTLED
#include "FastLED.h"
typedef enum
{
LED_OFF,
LED_GREEN,
LED_BLUE,
LED_RED,
LED_WHITE
} GwLedMode;
static CRGB::HTMLColorCode colorFromMode(GwLedMode cmode)
{
switch (cmode)
{
case LED_BLUE:
return CRGB::Blue;
case LED_GREEN:
return CRGB::Green;
case LED_RED:
return CRGB::Red;
case LED_WHITE:
return CRGB::White;
default:
return CRGB::Black;
}
}
void handleLeds(GwApi *api)
{
GwLog *logger = api->getLogger();
CRGB leds[1];
#ifdef GWLED_SCHEMA
FastLED.addLeds<GWLED_TYPE, GWLED_PIN, (EOrder)GWLED_SCHEMA>(leds, 1);
#else
FastLED.addLeds<GWLED_TYPE, GWLED_PIN>(leds, 1);
#endif
uint8_t brightness = api->getConfig()->getInt(GwConfigDefinitions::ledBrightness, 128);
GwLedMode currentMode = LED_GREEN;
leds[0] = colorFromMode(currentMode);
FastLED.setBrightness(brightness);
FastLED.show();
LOG_DEBUG(GwLog::LOG, "led task started with mode %d, brightness=%d", (int)currentMode, (int)brightness);
int apiResult = 0;
while (true)
{
delay(50);
GwLedMode newMode = currentMode;
IButtonTask buttonState = api->taskInterfaces()->get<IButtonTask>(apiResult);
if (apiResult >= 0)
{
switch (buttonState.state)
{
case IButtonTask::PRESSED_5:
newMode = LED_BLUE;
break;
case IButtonTask::PRESSED_10:
newMode = LED_RED;
break;
default:
newMode = LED_GREEN;
break;
}
}
else
{
newMode = LED_WHITE;
}
if (newMode != currentMode)
{
leds[0] = colorFromMode(newMode);
FastLED.show();
currentMode = newMode;
}
}
vTaskDelete(NULL);
}
#endif