bobbycar/controller_teensy/include/led.h

178 lines
5.5 KiB
C
Raw Normal View History

2023-05-19 14:45:56 +00:00
#ifndef _LED_H_
#define _LED_H_
//LED Ring Positions
/*
3 2 1
4 0
5 15
6 14
7 13
8 12
9 10 11
*/
2023-05-19 14:45:56 +00:00
#include <Adafruit_NeoPixel.h>
2024-07-12 17:57:43 +00:00
#define LED_PIN 20
2023-05-19 14:45:56 +00:00
#define LED_COUNT 16
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50 // Set BRIGHTNESS to about 1/5 (max = 255)
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
unsigned long last_ledupdate=0;
#define LEDUPDATEINTERVAL 100
2023-05-23 19:46:34 +00:00
uint8_t led_errorcount=0; //count led progress errors. used for delay at end if any errors occured
void led_dotcircle(unsigned long loopmillis);
void led_gauge(unsigned long loopmillis,float value,float value_min,float value_max,uint32_t colorGauge,uint32_t colorBG);
2023-05-23 19:46:34 +00:00
2023-05-19 14:45:56 +00:00
void init_led() {
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS);
}
2023-05-23 19:46:34 +00:00
void led_testLEDSBlocking(){
strip.clear();
strip.show();
delay(10);
uint32_t color=strip.Color(0, 0, 0, 0);
for(int i=0; i<strip.numPixels()*4; i++) { // For each pixel in strip...
switch(i/strip.numPixels()) {
case 0:
color=strip.Color(255, 0, 0, 0);
break;
case 1:
color=strip.Color(0, 255, 0, 0);
break;
case 2:
color=strip.Color(0, 0, 255, 0);
break;
case 3:
color=strip.Color(0, 0, 0, 255);
break;
}
strip.setPixelColor(i%strip.numPixels(), color);
strip.show();
delay(25);
}
strip.clear();
strip.show();
}
void led_simpeProgress(uint8_t progressID,uint8_t result){
if (result!=1) {
led_errorcount++;
}
uint32_t color=strip.Color(0, 0, 0, 0);
switch(result) {
case 0: //fail
color=strip.Color(255, 0, 0, 0);
break;
case 1: //success
color=strip.Color(0, 255, 0, 0);
break;
case 2: //warning
color=strip.Color(127, 127, 0, 0);
break;
}
strip.setPixelColor(progressID, color);
strip.show();
}
void led_simpleProgressWait() {
if (led_errorcount>0) {
delay(5000);
}else{
delay(100);
}
}
void led_update(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) {
2023-05-19 14:45:56 +00:00
if (loopmillis>last_ledupdate+LEDUPDATEINTERVAL) {
last_ledupdate=loopmillis;
2023-05-23 19:46:34 +00:00
if (error_brake_outofrange || error_throttle_outofrange || error_throttle_difftoohigh || error_ads_max_read_interval) {
//Error
}else{
if (armed) {
if (loopmillis-last_notidle>5000) {
//Standing
float vbat=min(escRear.getFeedback_batVoltage(),escFront.getFeedback_batVoltage());
2024-07-16 21:49:45 +00:00
led_gauge(loopmillis,vbat,3.3*12,4.2*12,strip.Color(0, 255, 0, 0),strip.Color(100, 0, 0, 0));
}else{
//Driving
float currentMean=escRear.getFiltered_curL()+escRear.getFiltered_curR()+escFront.getFiltered_curL()+escFront.getFiltered_curR();
2023-06-03 10:59:26 +00:00
uint32_t _bgColor=strip.Color(0, 0, 20, 0);
if (reverse_enabled) {
_bgColor=strip.Color(20, 0, 20, 0);
}
led_gauge(loopmillis,currentMean,0,32.0,strip.Color(0, 0, 0, 255),_bgColor);
}
}else{
//Disarmed
led_dotcircle(loopmillis); //fill background with animation
uint32_t colorConnected=strip.Color(0, 255, 0, 0);
if (escFront.getControllerConnected()) {
for(int i=1; i<=3; i++) {
strip.setPixelColor(i, colorConnected);
}
}
if (escRear.getControllerConnected()) {
for(int i=1+8; i<=3+8; i++) {
strip.setPixelColor(i, colorConnected);
}
}
}
}
2023-05-23 19:46:34 +00:00
2023-05-19 14:45:56 +00:00
strip.show(); // Update strip to match
}
}
void led_gauge(unsigned long loopmillis,float value,float value_min,float value_max,uint32_t colorGauge,uint32_t colorBG) {
uint8_t position=map( max(min(value,value_max),value_min) ,value_min,value_max, 0,strip.numPixels()+1);
2023-05-23 19:46:34 +00:00
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
2023-05-23 20:56:39 +00:00
uint8_t pp=(strip.numPixels()-i-1 + 10 )%strip.numPixels(); //Offset and invert
if (i<position) {
strip.setPixelColor(pp, colorGauge); // Set pixel's color (in RAM)
2023-05-23 19:46:34 +00:00
}else{
strip.setPixelColor(pp, colorBG); // Set pixel's color (in RAM)
2023-05-23 19:46:34 +00:00
}
}
}
void led_dotcircle(unsigned long loopmillis) {
float position=(loopmillis/1000.0*strip.numPixels()/10.0);
while (position>strip.numPixels()){ //modulo for float
position-=strip.numPixels();
}
2023-05-23 19:46:34 +00:00
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
float _bright=min(abs(i-position),strip.numPixels()-abs(i-position));
_bright/=strip.numPixels()/2.0; //1 if position is at i, 0 if position is oposite of i in circle
_bright=pow(_bright,2);
uint32_t color=strip.Color(0, 0, 0, _bright*255);
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
2023-05-23 19:46:34 +00:00
}
}
2023-05-19 14:45:56 +00:00
#endif