5 #include "crsf_protocol.h"
8 extern bool InBindingMode
;
10 extern bool connectionHasModelMatch
;
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
;
32 digitalWrite(_pin
, LOW
^ _pin_inverted
);
34 digitalWrite(_pin
, HIGH
^ _pin_inverted
);
35 if (_counter
>= _count
)
39 return _durations
[_counter
++] * 10;
42 static uint16_t flashLED(uint8_t pin
, uint8_t pin_inverted
, const uint8_t durations
[], uint8_t count
)
46 _pin_inverted
= pin_inverted
;
47 _durations
= durations
;
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
);
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
);
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 */
101 static void setPowerLEDs()
103 #if defined(TARGET_TX_BETAFPV_2400_V1) || defined(TARGET_TX_BETAFPV_900_V1)
104 switch (POWERMGNT::currPower())
107 digitalWrite(GPIO_PIN_LED_BLUE
, HIGH
);
108 digitalWrite(GPIO_PIN_LED_GREEN
, HIGH
);
111 digitalWrite(GPIO_PIN_LED_BLUE
, LOW
);
112 digitalWrite(GPIO_PIN_LED_GREEN
, HIGH
);
119 digitalWrite(GPIO_PIN_LED_BLUE
, HIGH
);
120 digitalWrite(GPIO_PIN_LED_GREEN
, LOW
);
128 #if defined(TARGET_RX) && defined(GPIO_PIN_LED)
131 return flashLED(GPIO_PIN_LED
, GPIO_LED_RED_INVERTED
, LEDSEQ_BINDING
, sizeof(LEDSEQ_BINDING
));
135 switch (connectionState
)
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
);
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
);
152 #ifdef GPIO_PIN_LED_RED
153 digitalWrite(GPIO_PIN_LED_RED
, HIGH
^ GPIO_LED_RED_INVERTED
);
157 if (connectionHasModelMatch
)
159 digitalWrite(GPIO_PIN_LED
, HIGH
^ GPIO_LED_RED_INVERTED
); // turn on led
163 return flashLED(GPIO_PIN_LED
, GPIO_LED_RED_INVERTED
, LEDSEQ_MODEL_MISMATCH
, sizeof(LEDSEQ_MODEL_MISMATCH
));
167 return DURATION_NEVER
;
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
);
178 #if defined(TARGET_RX)
179 #ifdef GPIO_PIN_LED_GREEN
180 digitalWrite(GPIO_PIN_LED_GREEN
, LOW
^ GPIO_LED_GREEN_INVERTED
);
182 #ifdef GPIO_PIN_LED_RED
183 digitalWrite(GPIO_PIN_LED_RED
, LOW
^ GPIO_LED_RED_INVERTED
);
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
));
191 return DURATION_NEVER
;
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
));
199 return DURATION_NEVER
;
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
));
204 return DURATION_NEVER
;
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
));
219 return DURATION_NEVER
;
222 return DURATION_NEVER
;
226 device_t LED_device
= {
227 .initialize
= initialize
,