-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
113 lines (108 loc) · 3.26 KB
/
main.cpp
File metadata and controls
113 lines (108 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "main.hpp"
#include "board_pins.hpp"
#include "display.h"
#include "pitches.hpp"
#include "serial_port.hpp"
#include <Arduino.h>
#include <cstdint>
#include <string>
/**
* Events are the beginning of a pressed button.
*
* @returns 1-8 or 0 in case of no event
*/
static std::uint8_t getEvent()
{
constexpr PinType inputPins[] = {
board::button::pin::task1,
board::button::pin::task2,
board::button::pin::task3,
board::button::pin::task4,
board::button::pin::up,
board::button::pin::down,
board::button::pin::enter,
board::button::pin::back,
};
static PinType oldValue = 0U;
PinType newValue = 0U;
std::uint8_t result = 0U;
std::uint8_t candidateEvent = 1;
for (const auto pin : inputPins)
{
if (digitalRead(pin) == LOW) // buttons are active low
{
newValue = candidateEvent;
}
candidateEvent++;
}
if (newValue != 0 && oldValue == 0)
{
result = newValue;
}
oldValue = newValue;
return result;
}
namespace main
{
void setup(char const *programIdentificationString)
{
serial_port::initialize();
pinMode(board::button::pin::up, INPUT_PULLUP);
pinMode(board::button::pin::down, INPUT_PULLUP);
pinMode(board::button::pin::enter, INPUT_PULLUP);
pinMode(board::button::pin::back, INPUT_PULLUP);
pinMode(board::button::pin::task1, INPUT_PULLUP);
pinMode(board::button::pin::task2, INPUT_PULLUP);
pinMode(board::button::pin::task3, INPUT_PULLUP);
pinMode(board::button::pin::task4, INPUT_PULLUP);
pinMode(board::led::pin::task1, OUTPUT);
pinMode(board::led::pin::task2, OUTPUT);
pinMode(board::led::pin::task3, OUTPUT);
pinMode(board::led::pin::task4, OUTPUT);
pinMode(board::buzzer::pin::on_off, OUTPUT);
setup_display();
serial_port::cout << std::endl
<< " begin program '" << programIdentificationString << std::endl;
std::string foo;
Serial.setTimeout(3000);
for (unsigned int i = 0; i < 3;)
{
serial_port::cout << "Waiting for user input:" << std::endl;
serial_port::cin >> foo;
serial_port::cout << "Did read: '" << foo << "'" << std::endl;
}
}
void loop()
{
constexpr unsigned long loopDurationMs = 250;
const auto event = getEvent();
constexpr PinType outputPins[] = {
board::led::pin::task1,
board::led::pin::task2,
board::led::pin::task3,
board::led::pin::task4,
};
if (event)
{
Serial.printf("Process event '%u'.\n", event);
constexpr std::uint16_t notes[] = {note::c3, note::d3, note::e3, note::f3, note::g3, note::a3, note::b3, note::c4};
tone(board::buzzer::pin::on_off, notes[event - 1], loopDurationMs);
if (event < sizeof(outputPins) / sizeof(outputPins[0]) + 1)
{
constexpr int minBrightness = 0;
analogWrite(outputPins[event - 1], minBrightness);
}
}
else
{
for (const auto pin : outputPins)
{
constexpr int maxBrightness = 255;
constexpr int brightness = maxBrightness * 25 / 100.0;
analogWrite(pin, brightness);
}
}
delay(loopDurationMs);
refresh_display(); // Animate bitmaps
}
} // namespace main