Skip to content

Commit 0e0054e

Browse files
authored
Cleanup proposal
1 parent 366ca54 commit 0e0054e

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

microcontroller-src/kv4p_ht_esp32_wroom_32/kv4p_ht_esp32_wroom_32.ino

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void iir_lowpass_reset();
172172
hw_ver_t get_hardware_version();
173173
void reportPhysPttState();
174174
void show_LEDs();
175-
void neopixelColor(RGBColor color, float bright = 1.0);
175+
void neopixelColor(RGBColor C, uint8_t bright);
176176

177177
void setup() {
178178
// Used for setup, need to know early.
@@ -229,6 +229,7 @@ void setup() {
229229
// Begin in STOPPED mode
230230
lastSquelched = (digitalRead(SQ_PIN) == HIGH);
231231
setMode(MODE_STOPPED);
232+
show_LEDs();
232233
}
233234

234235
void initI2SRx() {
@@ -633,7 +634,6 @@ void loop() {
633634
// Check for squelch status change
634635
if (squelched != lastSquelched) {
635636
lastSquelched = squelched;
636-
show_LEDs();
637637
if (squelched) {
638638
// Start fade-out
639639
fadeCounter = FADE_SAMPLES;
@@ -811,7 +811,6 @@ void setMode(Mode newMode) {
811811
isTxCacheSatisfied = false;
812812
break;
813813
}
814-
show_LEDs();
815814
}
816815

817816
void processTxAudio(uint8_t tempBuffer[], int bytesRead) {
@@ -853,52 +852,41 @@ hw_ver_t get_hardware_version() {
853852
return ver;
854853
}
855854

856-
void neopixelColor(RGBColor C, float bright) {
857-
uint8_t red = uint8_t(C.red*bright);
858-
uint8_t green = uint8_t(C.green*bright);
859-
uint8_t blue = uint8_t(C.blue*bright);
855+
void neopixelColor(RGBColor C, uint8_t bright = 255) {
856+
uint8_t red = (C.red * bright) >> 8;
857+
uint8_t green = (C.green * bright) >> 8;
858+
uint8_t blue = (C.blue * bright) >> 8;
860859
neopixelWrite(PIXELS_PIN, red, green, blue);
861860
}
862861

863862
// Calculate a float between min and max, that ramps from min to max in half of breath_every,
864863
// and from max to min the second half of breath_every.
865864
// now and breath_every are arbitrary units, but usually are milliseconds from millis().
866-
float calc_breath(uint32_t now, uint32_t breath_every, float min, float max) {
867-
float amplitude = max - min;
868-
float bright = (float(now%breath_every)/breath_every)*2.0; // 0.0 to 2.0, how far through breath_every are we
869-
if (bright > 1.0) bright = 2.0-bright; // Fold >1.0 back down to between 1.0 and 0.0.
870-
bright = bright*amplitude + min;
871-
return bright;
865+
uint8_t calc_breath(uint32_t now, uint32_t breath_every, uint8_t min, uint8_t max) {
866+
uint16_t amplitude = max - min; // Ensure enough range for calculations
867+
uint16_t bright = ((now % breath_every) * 512) / breath_every; // Scale to 0-512
868+
if (bright > 255) bright = 512 - bright; // Fold >255 back down to 255-0
869+
return ((bright * amplitude) / 255) + min;
872870
}
873871

874872
void show_LEDs() {
875873
// When to actually act?
876-
static Mode last_mode = MODE_STOPPED;
877-
static bool last_squelched = true; // Eww. This is too closely named to the variable we're tracking.
878874
static uint32_t next_time = 0;
879875
const static uint32_t update_every = 50; // in milliseconds
880-
881876
uint32_t now = millis();
882-
883877
// Only change LEDs if:
884878
// * it's been more than update_every ms since we've last set the LEDs.
885-
// * either mode or lastSquelched have changed.
886-
// This allows us to call show_LEDs() as often as we want without risking slamming the micro with IO.
887-
if (now >= next_time || last_mode != mode || last_squelched != lastSquelched) {
888-
last_mode = mode;
889-
last_squelched = lastSquelched;
879+
if (now >= next_time) {
890880
next_time = now + update_every;
891-
892881
switch (mode) {
893882
case MODE_STOPPED:
894883
digitalWrite(LED_PIN, LOW);
895-
//neopixelColor(COLOR_STOPPED);
896884
neopixelColor(COLOR_HW_VER);
897885
break;
898886
case MODE_RX:
899887
digitalWrite(LED_PIN, LOW);
900888
if (lastSquelched) {
901-
neopixelColor(COLOR_RX_SQL_CLOSED, calc_breath(now, 2000, 0.5, 1.5));
889+
neopixelColor(COLOR_RX_SQL_CLOSED, calc_breath(now, 2000, 127, 255));
902890
} else {
903891
neopixelColor(COLOR_RX_SQL_OPEN);
904892
}

0 commit comments

Comments
 (0)