|
| 1 | +#include "Wire.h" |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#define Wire Wire1 |
| 5 | + |
| 6 | +class Module { |
| 7 | + public: |
| 8 | + Module(uint8_t address = 0xFF) : address(address) {} |
| 9 | + bool begin() { |
| 10 | + if (address == 0xFF) { |
| 11 | + //find possible matches |
| 12 | + } |
| 13 | + } |
| 14 | + bool read(uint8_t* buf, int howmany) { |
| 15 | + if (address == 0xFF) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + Wire.requestFrom(address, howmany); |
| 19 | + auto start = millis(); |
| 20 | + while ((Wire.available() == 0) && (millis() - start < 100)) { |
| 21 | + delay(1); |
| 22 | + } |
| 23 | + if (Wire.available() < howmany) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + for (int i = 0; i < howmany; i++) { |
| 27 | + buf[i] = Wire.read(); |
| 28 | + } |
| 29 | + return true; |
| 30 | + } |
| 31 | + bool write(uint8_t* buf, int howmany) { |
| 32 | + if (address == 0xFF) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + Wire.beginTransmission(address); |
| 36 | + for (int i = 0; i < howmany; i++) { |
| 37 | + Wire.write(buf[i]); |
| 38 | + } |
| 39 | + Wire.endTransmission(); |
| 40 | + return true; |
| 41 | + } |
| 42 | + private: |
| 43 | + uint8_t address; |
| 44 | +}; |
| 45 | + |
| 46 | +class Buttons : public Module { |
| 47 | + public: |
| 48 | + Buttons(uint8_t address = 0xFF) : Module(address) {} |
| 49 | + bool get(bool& a, bool& b, bool& c) { |
| 50 | + uint8_t buf[3]; |
| 51 | + auto res = read((uint8_t*)buf, 3); |
| 52 | + a = buf[0]; b = buf[1]; c = buf[2]; |
| 53 | + auto ret = res && (a != last_a || b != last_b || c != last_c); |
| 54 | + last_a = a; last_b = b; last_c = c; |
| 55 | + return ret; |
| 56 | + } |
| 57 | + private: |
| 58 | + bool last_a, last_b, last_c; |
| 59 | +}; |
| 60 | + |
| 61 | +class Tone : public Module { |
| 62 | + public: |
| 63 | + Tone(uint8_t address = 0xFF) : Module(address) {} |
| 64 | + void tone(size_t freq, size_t len_ms) { |
| 65 | + uint8_t buf[8]; |
| 66 | + memcpy(&buf[0], &freq, 4); |
| 67 | + memcpy(&buf[4], &len_ms, 4); |
| 68 | + write(buf, 8); |
| 69 | + } |
| 70 | + void noTone() { |
| 71 | + uint8_t buf[8]; |
| 72 | + memset(&buf[0], 0, 8); |
| 73 | + write(buf, 8); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +class Color { |
| 78 | + public: |
| 79 | + Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {} |
| 80 | + operator uint32_t() { |
| 81 | + return (b << 8 | g << 16 | r << 24); |
| 82 | + } |
| 83 | + private: |
| 84 | + uint8_t r, g, b; |
| 85 | +}; |
| 86 | + |
| 87 | +Color RED(255, 0, 0); |
| 88 | +Color BLUE(0, 0, 255); |
| 89 | +Color GREEN(0, 255, 0); |
| 90 | +Color VIOLET(255, 0, 255); |
| 91 | + |
| 92 | +class LEDS : public Module { |
| 93 | + public: |
| 94 | + LEDS(uint8_t address = 0xFF) : Module(address) { |
| 95 | + memset(data, 0xEF, 40); |
| 96 | + } |
| 97 | + bool begin() { |
| 98 | + show(); |
| 99 | + } |
| 100 | + void set(int idx, uint8_t brightness, Color rgb) { |
| 101 | + data[idx] = (uint32_t)rgb | brightness | 0xE0; |
| 102 | + } |
| 103 | + void show() { |
| 104 | + write((uint8_t*)data, 10 * 4); |
| 105 | + } |
| 106 | + private: |
| 107 | + uint32_t data[10]; |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +class Encoder : public Module { |
| 112 | + public: |
| 113 | + Encoder(uint8_t address = 0xFF) : Module(address) {} |
| 114 | + int16_t get() { |
| 115 | + uint8_t buf[3]; |
| 116 | + auto res = read(buf, 3); |
| 117 | + if (res == false) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + _pressed = (buf[2] != 0); |
| 121 | + int16_t ret; |
| 122 | + memcpy(&ret, &buf[0], 2); |
| 123 | + return ret; |
| 124 | + } |
| 125 | + bool pressed() { |
| 126 | + get(); |
| 127 | + return _pressed; |
| 128 | + } |
| 129 | + private: |
| 130 | + bool _pressed = false; |
| 131 | +}; |
| 132 | + |
| 133 | +std::vector<Module*> findModules() { |
| 134 | + |
| 135 | + std::vector<Module*> modules; |
| 136 | + for (int i = 8; i < 127; i++) { |
| 137 | + Wire.beginTransmission(i); |
| 138 | + auto ret = Wire.endTransmission(); |
| 139 | + if (ret != 2) { |
| 140 | + Serial.println(String(i) + ":" + String(ret)); |
| 141 | + } |
| 142 | + } |
| 143 | + return modules; |
| 144 | +} |
| 145 | + |
| 146 | +Buttons button(62); |
| 147 | +Tone _tone(30); |
| 148 | +LEDS leds(54); |
| 149 | +Encoder encoder(58); |
| 150 | + |
| 151 | +void setup() { |
| 152 | + Wire.begin(); |
| 153 | + Wire.setClock(100000); |
| 154 | + Serial.begin(115200); |
| 155 | + findModules(); |
| 156 | + leds.begin(); |
| 157 | +} |
| 158 | + |
| 159 | +int skip = 0; |
| 160 | +void loop() { |
| 161 | + bool a; bool b; bool c; |
| 162 | + |
| 163 | + if (encoder.pressed()) { |
| 164 | + skip = (skip+1)%5; |
| 165 | + } |
| 166 | + |
| 167 | + auto pitch = encoder.get(); |
| 168 | + Serial.println(pitch); |
| 169 | + |
| 170 | + if (button.get(a, b, c)) { |
| 171 | + if (a) { |
| 172 | + leds.set(1+skip, 15, RED); |
| 173 | + _tone.tone(440+pitch, 1000); |
| 174 | + } else { |
| 175 | + leds.set(1+skip, 0, RED); |
| 176 | + } |
| 177 | + if (b) { |
| 178 | + leds.set(2+skip, 15, BLUE); |
| 179 | + _tone.tone(880+pitch, 1000); |
| 180 | + } else { |
| 181 | + leds.set(2+skip, 0, BLUE); |
| 182 | + } |
| 183 | + if (c) { |
| 184 | + leds.set(3+skip, 15, GREEN); |
| 185 | + _tone.tone(1240+pitch, 1000); |
| 186 | + } else { |
| 187 | + leds.set(3+skip, 0, GREEN); |
| 188 | + } |
| 189 | + leds.show(); |
| 190 | + } |
| 191 | +} |
0 commit comments