makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / LED / devLED.cpp
blob0e88ed8d1faba697b68f13f87c5a2ac17c9032e0
1 #include "targets.h"
2 #include "common.h"
3 #include "device.h"
5 #include "crsf_protocol.h"
6 #include "POWERMGNT.h"
8 extern bool InBindingMode;
9 #if defined(TARGET_RX)
10 extern bool connectionHasModelMatch;
11 #endif
13 constexpr uint8_t LEDSEQ_RADIO_FAILED[] = { 20, 100 }; // 200ms on, 1000ms off
14 constexpr uint8_t LEDSEQ_DISCONNECTED[] = { 50, 50 }; // 500ms on, 500ms off
15 constexpr uint8_t LEDSEQ_WIFI_UPDATE[] = { 2, 3 }; // 20ms on, 30ms off
16 constexpr uint8_t LEDSEQ_BINDING[] = { 10, 10, 10, 100 }; // 2x 100ms blink, 1s pause
17 constexpr uint8_t LEDSEQ_MODEL_MISMATCH[] = { 10, 10, 10, 10, 10, 100 }; // 3x 100ms blink, 1s pause
19 static uint8_t _pin = -1;
20 static uint8_t _pin_inverted;
21 static const uint8_t *_durations;
22 static uint8_t _count;
23 static uint8_t _counter = 0;
25 static uint16_t updateLED()
27 if (_pin == UNDEF_PIN)
29 return DURATION_NEVER;
31 if(_counter % 2 == 1)
32 digitalWrite(_pin, LOW ^ _pin_inverted);
33 else
34 digitalWrite(_pin, HIGH ^ _pin_inverted);
35 if (_counter >= _count)
37 _counter = 0;
39 return _durations[_counter++] * 10;
42 static uint16_t flashLED(uint8_t pin, uint8_t pin_inverted, const uint8_t durations[], uint8_t count)
44 _counter = 0;
45 _pin = pin;
46 _pin_inverted = pin_inverted;
47 _durations = durations;
48 _count = count;
49 return updateLED();
52 static void initialize()
54 // TODO for future PR, remove TARGET_TX, TARGET_RX, and TARGET_TX_FM30 defines.
55 #if defined(TARGET_TX)
56 #if defined(GPIO_PIN_LED_BLUE) && (GPIO_PIN_LED_BLUE != UNDEF_PIN)
57 pinMode(GPIO_PIN_LED_BLUE, OUTPUT);
58 digitalWrite(GPIO_PIN_LED_BLUE, LOW ^ GPIO_LED_BLUE_INVERTED);
59 #endif // GPIO_PIN_BLUE_GREEN
60 #if defined(GPIO_PIN_LED_GREEN) && (GPIO_PIN_LED_GREEN != UNDEF_PIN)
61 pinMode(GPIO_PIN_LED_GREEN, OUTPUT);
62 digitalWrite(GPIO_PIN_LED_GREEN, HIGH ^ GPIO_LED_GREEN_INVERTED);
63 #endif // GPIO_PIN_LED_GREEN
64 #if defined(GPIO_PIN_LED_RED) && (GPIO_PIN_LED_RED != UNDEF_PIN)
65 pinMode(GPIO_PIN_LED_RED, OUTPUT);
66 digitalWrite(GPIO_PIN_LED_RED, LOW ^ GPIO_LED_RED_INVERTED);
67 #endif // GPIO_PIN_LED_RED
68 #if defined(TARGET_TX_IFLIGHT)
69 digitalWrite(GPIO_PIN_LED_GREEN, LOW);
70 digitalWrite(GPIO_PIN_LED_RED, HIGH);
71 digitalWrite(GPIO_PIN_LED_BLUE, LOW);
72 #endif
73 #if defined(TARGET_TX_FM30)
74 pinMode(GPIO_PIN_LED_RED_GREEN, OUTPUT); // Green LED on "Red" LED (off)
75 digitalWrite(GPIO_PIN_LED_RED_GREEN, HIGH);
76 pinMode(GPIO_PIN_LED_GREEN_RED, OUTPUT); // Red LED on "Green" LED (off)
77 digitalWrite(GPIO_PIN_LED_GREEN_RED, HIGH);
78 #endif
79 #endif
80 #if defined(TARGET_RX)
81 #ifdef GPIO_PIN_LED_GREEN
82 pinMode(GPIO_PIN_LED_GREEN, OUTPUT);
83 digitalWrite(GPIO_PIN_LED_GREEN, LOW ^ GPIO_LED_GREEN_INVERTED);
84 #endif /* GPIO_PIN_LED_GREEN */
85 #ifdef GPIO_PIN_LED_RED
86 pinMode(GPIO_PIN_LED_RED, OUTPUT);
87 digitalWrite(GPIO_PIN_LED_RED, LOW ^ GPIO_LED_RED_INVERTED);
88 #endif /* GPIO_PIN_LED_RED */
89 #if defined(GPIO_PIN_LED)
90 pinMode(GPIO_PIN_LED, OUTPUT);
91 digitalWrite(GPIO_PIN_LED, LOW ^ GPIO_LED_RED_INVERTED);
92 #endif /* GPIO_PIN_LED */
93 #endif
96 static int timeout()
98 return updateLED();
101 static void setPowerLEDs()
103 #if defined(TARGET_TX_BETAFPV_2400_V1) || defined(TARGET_TX_BETAFPV_900_V1)
104 switch (POWERMGNT::currPower())
106 case PWR_250mW:
107 digitalWrite(GPIO_PIN_LED_BLUE, HIGH);
108 digitalWrite(GPIO_PIN_LED_GREEN, HIGH);
109 break;
110 case PWR_500mW:
111 digitalWrite(GPIO_PIN_LED_BLUE, LOW);
112 digitalWrite(GPIO_PIN_LED_GREEN, HIGH);
113 break;
114 case PWR_10mW:
115 case PWR_25mW:
116 case PWR_50mW:
117 case PWR_100mW:
118 default:
119 digitalWrite(GPIO_PIN_LED_BLUE, HIGH);
120 digitalWrite(GPIO_PIN_LED_GREEN, LOW);
121 break;
123 #endif
126 static int event()
128 #if defined(TARGET_RX) && defined(GPIO_PIN_LED)
129 if (InBindingMode)
131 return flashLED(GPIO_PIN_LED, GPIO_LED_RED_INVERTED, LEDSEQ_BINDING, sizeof(LEDSEQ_BINDING));
133 #endif
134 setPowerLEDs();
135 switch (connectionState)
137 case connected:
138 #if defined(TARGET_TX)
139 #if defined(TARGET_TX_IFLIGHT)
140 digitalWrite(GPIO_PIN_LED_GREEN, HIGH);
141 digitalWrite(GPIO_PIN_LED_RED, LOW);
142 digitalWrite(GPIO_PIN_LED_BLUE, LOW);
143 #elif defined(GPIO_PIN_LED_RED) && (GPIO_PIN_LED_RED != UNDEF_PIN)
144 digitalWrite(GPIO_PIN_LED_RED, HIGH ^ GPIO_LED_RED_INVERTED);
145 #endif
146 #endif // GPIO_PIN_LED_RED
147 #if defined(TARGET_RX)
148 #ifdef GPIO_PIN_LED_GREEN
149 digitalWrite(GPIO_PIN_LED_GREEN, HIGH ^ GPIO_LED_GREEN_INVERTED);
150 #endif
152 #ifdef GPIO_PIN_LED_RED
153 digitalWrite(GPIO_PIN_LED_RED, HIGH ^ GPIO_LED_RED_INVERTED);
154 #endif
156 #ifdef GPIO_PIN_LED
157 if (connectionHasModelMatch)
159 digitalWrite(GPIO_PIN_LED, HIGH ^ GPIO_LED_RED_INVERTED); // turn on led
161 else
163 return flashLED(GPIO_PIN_LED, GPIO_LED_RED_INVERTED, LEDSEQ_MODEL_MISMATCH, sizeof(LEDSEQ_MODEL_MISMATCH));
165 #endif
166 #endif
167 return DURATION_NEVER;
168 case disconnected:
169 #if defined(TARGET_TX)
170 #if defined(TARGET_TX_IFLIGHT)
171 digitalWrite(GPIO_PIN_LED_GREEN, LOW);
172 digitalWrite(GPIO_PIN_LED_BLUE, LOW);
173 return flashLED(GPIO_PIN_LED_RED, GPIO_LED_RED_INVERTED, LEDSEQ_DISCONNECTED, sizeof(LEDSEQ_DISCONNECTED));
174 #elif defined(GPIO_PIN_LED_RED) && (GPIO_PIN_LED_RED != UNDEF_PIN)
175 digitalWrite(GPIO_PIN_LED_RED, LOW ^ GPIO_LED_RED_INVERTED);
176 #endif
177 #endif
178 #if defined(TARGET_RX)
179 #ifdef GPIO_PIN_LED_GREEN
180 digitalWrite(GPIO_PIN_LED_GREEN, LOW ^ GPIO_LED_GREEN_INVERTED);
181 #endif
182 #ifdef GPIO_PIN_LED_RED
183 digitalWrite(GPIO_PIN_LED_RED, LOW ^ GPIO_LED_RED_INVERTED);
184 #endif
185 #ifdef GPIO_PIN_LED
186 return flashLED(GPIO_PIN_LED, GPIO_LED_RED_INVERTED, LEDSEQ_DISCONNECTED, sizeof(LEDSEQ_DISCONNECTED));
187 #elif GPIO_PIN_LED_GREEN
188 return flashLED(GPIO_PIN_LED_GREEN, GPIO_LED_GREEN_INVERTED, LEDSEQ_DISCONNECTED, sizeof(LEDSEQ_DISCONNECTED));
189 #endif
190 #endif
191 return DURATION_NEVER;
192 case wifiUpdate:
193 #if defined(TARGET_TX)
194 #if defined(TARGET_TX_IFLIGHT)
195 digitalWrite(GPIO_PIN_LED_GREEN, LOW);
196 digitalWrite(GPIO_PIN_LED_RED, LOW);
197 return flashLED(GPIO_PIN_LED_BLUE, GPIO_LED_BLUE_INVERTED, LEDSEQ_WIFI_UPDATE, sizeof(LEDSEQ_WIFI_UPDATE));
198 #endif
199 return DURATION_NEVER;
200 #endif
201 #if defined(TARGET_RX) && defined(GPIO_PIN_LED)
202 return flashLED(GPIO_PIN_LED, GPIO_LED_RED_INVERTED, LEDSEQ_WIFI_UPDATE, sizeof(LEDSEQ_WIFI_UPDATE));
203 #else
204 return DURATION_NEVER;
205 #endif
206 case radioFailed:
207 #if defined(TARGET_TX_IFLIGHT)
208 digitalWrite(GPIO_PIN_LED_GREEN, LOW);
209 digitalWrite(GPIO_PIN_LED_BLUE, LOW);
210 return flashLED(GPIO_PIN_LED_RED, GPIO_LED_RED_INVERTED, LEDSEQ_RADIO_FAILED, sizeof(LEDSEQ_RADIO_FAILED));
211 #elif defined(GPIO_PIN_LED_GREEN) && (GPIO_PIN_LED_GREEN != UNDEF_PIN)
212 digitalWrite(GPIO_PIN_LED_GREEN, LOW ^ GPIO_LED_GREEN_INVERTED);
213 #endif // GPIO_PIN_LED_GREEN
214 #if defined(GPIO_PIN_LED_RED) && (GPIO_PIN_LED_RED != UNDEF_PIN)
215 return flashLED(GPIO_PIN_LED_RED, GPIO_LED_RED_INVERTED, LEDSEQ_RADIO_FAILED, sizeof(LEDSEQ_RADIO_FAILED));
216 #elif defined(GPIO_PIN_LED) && (GPIO_PIN_LED != UNDEF_PIN)
217 return flashLED(GPIO_PIN_LED, GPIO_LED_RED_INVERTED, LEDSEQ_RADIO_FAILED, sizeof(LEDSEQ_RADIO_FAILED));
218 #else
219 return DURATION_NEVER;
220 #endif
221 default:
222 return DURATION_NEVER;
226 device_t LED_device = {
227 .initialize = initialize,
228 .start = event,
229 .event = event,
230 .timeout = timeout