Skip to content

Commit 3064f98

Browse files
committed
Use the new status LED helper methods as they're much nicer
1 parent 28ea145 commit 3064f98

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
lines changed

buttons.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
#include "buttons.h"
6+
#include "ledpwm.h"
67

78
uint8_t was_button_pressed() {
89

@@ -56,7 +57,7 @@ uint8_t was_button_pressed() {
5657
if(F.is_down && !pins) {
5758
// released
5859
if(F.clicks && now - F.last_push > BUTTON_LONG_PUSH_SPEED) {
59-
portb_val = 0;
60+
clear_status_leds();
6061
}
6162
F.is_down = false;
6263
}
@@ -66,10 +67,10 @@ uint8_t was_button_pressed() {
6667
if(F.clicks) {
6768
if(now - F.last_push > BUTTON_REALLY_LONG_PUSH_SPEED) {
6869
// full bright as really long press (usually reset)
69-
portb_val = seven_seg(12);
70+
set_status_leds(seven_seg(12));
7071
} else if(now - F.last_push > BUTTON_LONG_PUSH_SPEED) {
7172
// half bright at long press
72-
portb_val = (F.frame_counter & 1) ? 0 : seven_seg(12);
73+
set_status_leds((F.frame_counter & 1) ? 0 : seven_seg(12));
7374
}
7475
}
7576
}

demo.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ static void _demo_loop(const uint8_t start_sober)
106106
uint8_t sober_mode_divider = FPS; // once per second
107107
uint8_t sober_mode_counter = sober_mode_divider;
108108

109-
portb_val = 0;
110-
portb_mask = 0;
109+
clear_status_leds();
111110

112111
uint8_t portb_mask_in = 0;
113112
uint8_t portb_val_in = 0;
@@ -253,9 +252,7 @@ static void _demo_loop(const uint8_t start_sober)
253252
break;
254253
}
255254
}
256-
portb_mask = portb_mask_in;
257-
portb_val = portb_val_in;
258-
255+
set_status_leds_and_mask(portb_val_in, portb_mask_in);
259256
frame_epilogue();
260257
}
261258
}

gpio0.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#ifndef _GPIO0_H
66
#define _GPIO0_H
77

8+
// - bit 1 is used as an overflow flag for the fps_count() to tick the FPS (see fps.h, fps_count.h)
9+
// - bit 0 is used to trigger the sampler at half the PWM rate (e.g. 5kHz sampler for 10kHz PWM) (see ledpwm.h, sampler.h)
10+
811
#define LEDPWM_ROTATE_BACK_BUFFER_FLAG_BIT (2)
912
#define END_OF_FRAME_FLAG_BIT (1)
1013
#define EVERY_OTHER_FRAME_FLAG_BIT (0)
1114

12-
1315
#define LEDPWM_ROTATE_BACK_BUFFER_FLAG (1<<LEDPWM_ROTATE_BACK_BUFFER_FLAG_BIT)
1416
#define END_OF_FRAME_FLAG (1<<END_OF_FRAME_FLAG_BIT)
1517
#define EVERY_OTHER_FRAME_FLAG (1<<EVERY_OTHER_FRAME_FLAG_BIT)

ledpwm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setup_ledpwm() {
3939
// TIMER2_COMPA_vect clears the LEDs, TIMER2_COMPB_vect lights them.
4040
TIMSK2 |= (1 << OCIE2A) | (1 << OCIE2B);
4141

42-
portb_mask = MASK_RESET_VAL;
42+
clear_status_leds_within_interrupt();
4343

4444
// this clears the timer and sets the right pre-scaler, starting the timer.
4545
enable_ledpwm();
@@ -55,7 +55,7 @@ void setup_ledpwm() {
5555
void disable_ledpwm() {
5656
cli();
5757
TCCR2B = 0;
58-
PORTB = 0;
58+
clear_status_leds_within_interrupt();
5959
sei();
6060
}
6161

ledpwm.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
*/
44

55
// LED PWM using interrupts.
6+
67
// Makes a huge assumption that you're using the entirety of PORTB for LEDs.
8+
79
// Uses all three GPIOR registers for speed.
8-
// GPIOR0 is used as a flags register
9-
// - bit 0 is used to trigger the sampler at half the PWM rate (e.g. 5kHz sampler for 10kHz PWM)
10-
// - but 1 is used as an overflow flag for the fps_count() to tick the FPS
10+
11+
// GPIOR0 is used as a flags register. See gpio0.h for bit meanings
1112
// GPIOR1 contains the PORTB value to write
1213
// GPIOR2 contains the a rotating mask used to swap nibbles of GPIOR1 before writing
1314

1415
#ifndef _LEDPWM_H
1516
#define _LEDPWM_H
1617

18+
#include <Arduino.h>
1719
#include <DigitalIO.h>
1820

1921
#include "render.h"
@@ -33,4 +35,42 @@ extern DigitalPin<BEAT_PIN_1> beat_pin;
3335
extern DigitalPin<BEAT_PIN_2> tempo_pin;
3436

3537

38+
static void inline __attribute__((always_inline)) set_status_leds_and_mask_within_interrupt(uint8_t new_portb_val, uint8_t new_portb_mask)
39+
{
40+
portb_mask = new_portb_mask;
41+
portb_val = new_portb_val;
42+
}
43+
44+
static void inline __attribute__((always_inline)) set_status_leds_within_interrupt(uint8_t new_portb_val)
45+
{
46+
set_status_leds_and_mask_within_interrupt(new_portb_val, MASK_RESET_VAL);
47+
}
48+
49+
static void inline __attribute__((always_inline)) clear_status_leds_within_interrupt()
50+
{
51+
set_status_leds_and_mask_within_interrupt(0, MASK_RESET_VAL);
52+
}
53+
54+
static void inline __attribute__((always_inline)) set_status_leds_and_mask(uint8_t new_portb_val, uint8_t new_portb_mask)
55+
{
56+
cli();
57+
set_status_leds_and_mask_within_interrupt(new_portb_val, new_portb_mask);
58+
sei();
59+
}
60+
61+
static void inline __attribute__((always_inline)) set_status_leds(uint8_t new_portb_val)
62+
{
63+
cli();
64+
set_status_leds_within_interrupt(new_portb_val);
65+
sei();
66+
}
67+
68+
static void inline __attribute__((always_inline)) clear_status_leds()
69+
{
70+
cli();
71+
clear_status_leds_within_interrupt();
72+
sei();
73+
}
74+
75+
3676
#endif /* _LEDPWM_H */

vu3.ino

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,9 @@ static bool auto_mode_change(bool is_beat) {
108108
return false;
109109
}
110110

111-
static void ledpwm_reset() {
112-
cli();
113-
portb_mask = MASK_RESET_VAL; // min brightness mask
114-
portb_val = seven_seg(F.mode);
115-
sei();
111+
static void ledpwm_reset()
112+
{
113+
set_status_leds(seven_seg(F.mode));
116114
}
117115

118116
/*
@@ -121,10 +119,10 @@ static void ledpwm_reset() {
121119
*/
122120
static void ledpwm_vu_1() {
123121

124-
static const PROGMEM uint8_t masks[16] = { 0b11111110, 0b11101110, 0b11101010, 0b10101010,
125-
0b10101000, 0b10001000, 0b10000000, 0b00000000,
126-
0b10101010, 0b10101010, 0b10101010, 0b10101010,
127-
0b10101010, 0b10101010, 0b10101010, 0b10101010 };
122+
static const PROGMEM uint8_t masks[16] = { 0b01111111, 0b01110111, 0b01110101, 0b01010101,
123+
0b01010100, 0b01000100, 0b01000000, 0b00000000,
124+
0b01010101, 0b01010101, 0b01010101, 0b01010101,
125+
0b01010101, 0b01010101, 0b01010101, 0b01010101 };
128126

129127
uint8_t four_bit_level = (F.vu_width & 0xF0) >> 4;
130128
uint8_t new_portb_mask = masks[four_bit_level];
@@ -140,10 +138,7 @@ static void ledpwm_vu_1() {
140138
}
141139
new_portb_val |= seven_seg(F.mode);
142140

143-
cli();
144-
portb_mask = new_portb_mask;
145-
portb_val = new_portb_val;
146-
sei();
141+
set_status_leds_and_mask(new_portb_val, new_portb_mask);
147142
}
148143

149144
void loop() {

0 commit comments

Comments
 (0)