Implements menu and ability to disable groups

This commit is contained in:
Hannes Thalheim 2017-12-01 01:58:15 +01:00
parent 5c74902502
commit 3d5387e8cd
2 changed files with 87 additions and 9 deletions

View File

@ -22,6 +22,9 @@ using MainButton = Button<150, 150>;
Debounce<20> debounce; Debounce<20> debounce;
MainButton button; MainButton button;
bool activeGroups[16] = { false };
uint8_t activeCount = 0;
#define NUM_SLOWDOWN 12 #define NUM_SLOWDOWN 12
const uint16_t PROGMEM SLOWDOWN[NUM_SLOWDOWN] = { const uint16_t PROGMEM SLOWDOWN[NUM_SLOWDOWN] = {
35, 32, 35, 32,
@ -40,6 +43,8 @@ void setup() {
display.setDash(); display.setDash();
display.write(); display.write();
resetGroups();
} }
enum class State : uint8_t { enum class State : uint8_t {
@ -70,14 +75,17 @@ void loop() {
} }
break; break;
case State::Menu: case State::Menu:
// TODO displayMenu();
display.setDash();
display.write();
state = State::Idle; state = State::Idle;
break; break;
case State::Counting: case State::Counting:
if(event == ButtonEvent::LongPress) { if(event == ButtonEvent::LongPress) {
// stay in this state // stay in this state
if((uint16_t) millis() - timeout >= 32) { if((uint16_t) millis() - timeout >= 25) {
displayNextNumber(); display.set(generateNextNumber());
display.write();
timeout = millis(); timeout = millis();
} }
} else { } else {
@ -91,17 +99,28 @@ void loop() {
} }
} }
static uint8_t currentNumber; uint8_t generateNextNumber() {
void displayNextNumber() { uint8_t randomNumber = distribution(rng);
currentNumber = distribution(rng);
display.set(currentNumber); uint8_t i;
display.write(); for(i = 0; i < 16 && randomNumber != 0; i += 1) {
if(activeGroups[i]) {
randomNumber -= 1;
}
}
// already offset by 1 because of the loop postincrement
return i;
} }
void slowdown() { void slowdown() {
uint8_t currentNumber;
for(size_t i = 0; i < NUM_SLOWDOWN; i += 2) { for(size_t i = 0; i < NUM_SLOWDOWN; i += 2) {
for(size_t j = 0; j < pgm_read_word_near(SLOWDOWN + i); ++j) { for(size_t j = 0; j < pgm_read_word_near(SLOWDOWN + i); ++j) {
displayNextNumber(); currentNumber = generateNextNumber();
display.set(currentNumber);
display.write();
delay(pgm_read_word_near(SLOWDOWN + i + 1)); delay(pgm_read_word_near(SLOWDOWN + i + 1));
} }
} }
@ -116,3 +135,52 @@ void slowdown() {
} }
} }
void resetGroups() {
for(uint8_t i = 0; i < 16; i += 1) {
activeGroups[i] = true;
activeCount += 1;
}
}
void displayMenu() {
uint8_t currentGroup = 0;
uint16_t timeout = millis();
constexpr uint16_t longPressDelay = 300;
Button<longPressDelay, 150> menuButton;
for(;;) {
bool buttonPressed = !digitalRead(PIN1);
auto event = menuButton.event(debounce.event(buttonPressed));
display.set(currentGroup + 1);
display.setDP(activeGroups[currentGroup]);
display.write();
switch(event) {
case ButtonEvent::LongPress:
// rotate through groups
if((uint16_t) millis() - timeout >= longPressDelay) {
currentGroup = (currentGroup + 1) % 16;
timeout = millis();
}
break;
case ButtonEvent::Press:
// (de)activate group
activeGroups[currentGroup] = !activeGroups[currentGroup];
break;
case ButtonEvent::DoublePress:
// exit menu
activeCount = 0;
for(uint8_t i = 0; i < 16; i += 1) {
activeCount += (uint8_t) activeGroups[i];
}
if(activeCount == 0) {
resetGroups();
}
distribution = std::uniform_int_distribution<uint8_t>(1,activeCount);
return;
}
}
}

View File

@ -52,5 +52,15 @@ public:
delay(1); delay(1);
digitalWrite(PIN_RCLK, LOW); digitalWrite(PIN_RCLK, LOW);
} }
void setDP(bool on) {
if(on) {
number[0] &= 0x7F;
number[1] &= 0x7F;
} else {
number[0] |= 0x80;
number[1] |= 0x80;
}
}
}; };