diff --git a/clock/README.md b/clock/README.md index 2d75286..f4ef647 100644 --- a/clock/README.md +++ b/clock/README.md @@ -36,8 +36,8 @@ States: - Rate CV input - Less jittery timer - -- Faster speeds for sequencers? Negative subdivisions? +- Get working on bare ATMega328p +- Faster speeds with negative subdivisions https://en.wikipedia.org/wiki/Swing_(jazz_performance_style) https://github.com/adafruit/TinyWireM/blob/master/USI_TWI_Master.h diff --git a/clock/src/Button.h b/clock/src/Button.h index 5d1c3b2..7b6d37c 100644 --- a/clock/src/Button.h +++ b/clock/src/Button.h @@ -1,4 +1,5 @@ #pragma once + #include // A push-button toggle switch. Supports debouncing. diff --git a/clock/src/Display.h b/clock/src/Display.h index e6df629..b53e2f6 100644 --- a/clock/src/Display.h +++ b/clock/src/Display.h @@ -1,4 +1,5 @@ #pragma once + #include "SevenSegment.h" // given a number, get the character at index `digit` @@ -23,71 +24,92 @@ char getDigit(int16_t value, size_t digit) return '0' + value; } -// A set of 7-segment displays. -// Expects +// An array of 7-segment displays. +// Uses the same digit control port but only addresses one at a time, +// round robbin. This means the more segments there are the worse +// the brightness will be. Must call `tick` on a tight loop to +// switch the digit displayed or else the display will clearly flicker. +// Number of segments must be a compile time constant through `DIGITS` +// template. +// Displays signed 16-bit numbers. template class Display { private: - SevenSegment _digits[DIGITS]; - size_t _index = 0; + SevenSegment _digits[DIGITS]; // LSB first + size_t _index = 0; // which segment is currently addressed char _contents[DIGITS]; public: - void begin(volatile uint8_t* segmentPort, uint8_t* ctrlPins) + void begin(uint8_t portNumber, uint8_t *ctrlPins) { - for(size_t i = 0; i < DIGITS; i++) { - _digits[i].begin(ctrlPins[i], segmentPort); - _contents[i] = ' '; + for (size_t i = 0; i < DIGITS; i++) + { + _digits[i].begin(portNumber, ctrlPins[i]); + setChar(i, ' '); } _index = 0; } + // set all the displays to blank void clear() { - for (size_t i = 0; i < DIGITS; i++) { - _contents[i] = ' '; + for (size_t i = 0; i < DIGITS; i++) + { + setChar(i, ' '); } } - void tick() { + // update which display is showing + void tick() + { _digits[_index].turnOff(); _index = (_index + 1) % DIGITS; _digits[_index].display(_contents[_index]); _digits[_index].turnOn(); } - void setChar(size_t i, char c) { - if (i < DIGITS) { + void setChar(size_t i, char c) + { + if (i < DIGITS) + { _contents[i] = c; } } + // display a signed number on the void displayNumber(int16_t number) { - for (size_t i = 0; i < DIGITS; i++) { - _contents[i] = getDigit(number, i); + for (size_t i = 0; i < DIGITS; i++) + { + setChar(i, getDigit(number, i)); } } + // light up all segments (all 8's) void displayReset() { - for (size_t i = 0; i < DIGITS; i++) { - _contents[i] = '0'; + for (size_t i = 0; i < DIGITS; i++) + { + setChar(i, '8'); } } - void blinkReset() + // blink the reset a few times. + // NOTE: blocking, will return after `blinkCount * 2 * blinkTime` milliseconds. + void blinkReset(size_t blinkCount = 3, uint8_t blinkTime = 50) { - for (size_t i = 0; i < 3; i++) + for (size_t i = 0; i < blinkCount; i++) { clear(); - for (size_t i = 0; i < 50; i++) { + for (size_t i = 0; i < blinkTime; i++) + { tick(); delay(1); } displayReset(); - for (size_t i = 0; i < 50; i++) { + for (size_t i = 0; i < blinkTime; i++) + { tick(); delay(1); } diff --git a/clock/src/Knob.h b/clock/src/Knob.h index decf4e7..db7c945 100644 --- a/clock/src/Knob.h +++ b/clock/src/Knob.h @@ -1,4 +1,5 @@ #pragma once + #include // A continuous rotary switch with evenly-valued resistors between each diff --git a/clock/src/SevenSegment.h b/clock/src/SevenSegment.h index 79667eb..178263a 100644 --- a/clock/src/SevenSegment.h +++ b/clock/src/SevenSegment.h @@ -1,60 +1,83 @@ #pragma once + #include #define CHAR_BLANK 0b00000000 #define CHAR_MINUS 0b00000010 +#define CHAR_UNDERSCORE 0b00001000 +// TODO: would switch be faster than +// array index? const uint8_t LETTERS[] = { - // ABCDEGF - 0b01111101, - 0b00110000, - 0b01101110, - 0b01111010, - 0b00110011, - 0b01011011, - 0b01011111, - 0b01110000, - 0b01111111, - 0b01111011, + //_ABCDEGF + 0b01111101, + 0b00110000, + 0b01101110, + 0b01111010, + 0b00110011, + 0b01011011, + 0b01011111, + 0b01110000, + 0b01111111, + 0b01111011, }; -class SevenSegment { - private: - uint8_t _controlPin; - volatile uint8_t* _segmentPort; - - public: - // NOTE: its up to the caller to set the segmentPort to an - // output since we don't know which registers to use - void begin(uint8_t controlPin, volatile uint8_t* segmentPort) { - _controlPin = controlPin; - _segmentPort = segmentPort; - pinMode(controlPin, OUTPUT); - turnOff(); - } +// Addresses a common-cathode 7-segment display without a +// decimal. +// Assumes ABCDEGF segments are connected to pins 0-6 of +// a AVR port for faster addressing. +// The control pin should be connected to the common cathode. +// It is pulled low to turn on the LEDs. +class SevenSegment +{ +private: + uint8_t _controlPin; + volatile uint8_t *_segmentPort; - void turnOff() { - digitalWrite(_controlPin, HIGH); - } +public: + void begin(uint8_t portNumber, uint8_t controlPin) + { + _controlPin = controlPin; + _segmentPort = portOutputRegister(portNumber); + pinMode(controlPin, OUTPUT); + volatile uint8_t *portModeRegister = portModeRegister(portNumber); + *portModeRegister = 0x7F; + turnOff(); + } - void turnOn() { - digitalWrite(_controlPin, LOW); - } + void turnOff() + { + digitalWrite(_controlPin, HIGH); + } - bool display(char c) { - if (c >= '0' && c <= '9') { - *_segmentPort = LETTERS[c - '0']; - return true; - } - if (c == ' ') { - *_segmentPort = CHAR_BLANK; - return true; - } - if (c == '-') { - *_segmentPort = CHAR_MINUS; - return true; - } - // unsupported char - return false; + void turnOn() + { + digitalWrite(_controlPin, LOW); + } + + bool display(char c) + { + if (c >= '0' && c <= '9') + { + *_segmentPort = LETTERS[c - '0']; + return true; + } + if (c == ' ') + { + *_segmentPort = CHAR_BLANK; + return true; + } + if (c == '-') + { + *_segmentPort = CHAR_MINUS; + return true; + } + if (c == '_') + { + *_segmentPort = CHAR_UNDERSCORE; + return true; } + // unsupported char + return false; + } }; diff --git a/clock/src/TapTempo.h b/clock/src/TapTempo.h index 30041cf..7458635 100644 --- a/clock/src/TapTempo.h +++ b/clock/src/TapTempo.h @@ -1,4 +1,5 @@ #pragma once + #include #define NO_TEMPO 0 diff --git a/clock/src/Timer.h b/clock/src/Timer.h index 336a7f6..1525663 100644 --- a/clock/src/Timer.h +++ b/clock/src/Timer.h @@ -2,130 +2,134 @@ #include +#define DEFAULT_BPM 120 +#define DEFAULT_SUBDIVISIONS 2 +#define DEFAULT_SWING 0 + // Lookup table for OCR1A register values for BPMs from 15 to 500. // This is to avoid floating point math. // Calculations: // https://docs.google.com/spreadsheets/d/1pTg9IQDEw8LUGN85Lwp80kAzATN1I9yxx0m5xDrEWBg/edit#gid=0 const PROGMEM uint16_t BPM_LOOKUP[] = { - 62499, 58592, 55146, 52082, 49341, 46874, 44641, 42612, 40759, 39061, - 37499, 36056, 34721, 33481, 32326, 31249, 30240, 29295, 28408, 27572, - 26784, 26040, 25336, 24670, 24037, 23436, 22864, 22320, 21801, 21305, - 20832, 20379, 19945, 19530, 19131, 18749, 18381, 18027, 17687, 17360, - 17044, 16740, 16446, 16162, 15888, 15624, 15367, 15119, 14879, 14647, - 14422, 14203, 13991, 13785, 13585, 13391, 13203, 13019, 12841, 12667, - 12499, 12334, 12174, 12018, 11866, 11717, 11573, 11431, 11294, 11159, - 11028, 10900, 10774, 10652, 10532, 10415, 10301, 10189, 10079, 9972, - 9867, 9764, 9663, 9565, 9468, 9374, 9281, 9190, 9100, 9013, - 8927, 8843, 8760, 8679, 8599, 8521, 8444, 8369, 8295, 8222, - 8151, 8080, 8011, 7943, 7877, 7811, 7746, 7683, 7620, 7559, - 7499, 7439, 7380, 7323, 7266, 7210, 7155, 7101, 7047, 6995, - 6943, 6892, 6842, 6792, 6743, 6695, 6647, 6601, 6554, 6509, - 6464, 6420, 6376, 6333, 6290, 6249, 6207, 6166, 6126, 6086, - 6047, 6008, 5970, 5932, 5895, 5858, 5821, 5786, 5750, 5715, - 5680, 5646, 5612, 5579, 5546, 5513, 5481, 5449, 5418, 5386, - 5356, 5325, 5295, 5265, 5236, 5207, 5178, 5150, 5121, 5094, - 5066, 5039, 5012, 4985, 4959, 4933, 4907, 4881, 4856, 4831, - 4806, 4782, 4757, 4733, 4710, 4686, 4663, 4640, 4617, 4594, - 4572, 4549, 4527, 4506, 4484, 4463, 4442, 4421, 4400, 4379, - 4359, 4339, 4319, 4299, 4279, 4260, 4241, 4221, 4203, 4184, - 4165, 4147, 4128, 4110, 4092, 4075, 4057, 4039, 4022, 4005, - 3988, 3971, 3954, 3938, 3921, 3905, 3889, 3872, 3857, 3841, - 3825, 3809, 3794, 3779, 3764, 3749, 3734, 3719, 3704, 3689, - 3675, 3661, 3646, 3632, 3618, 3604, 3590, 3577, 3563, 3550, - 3536, 3523, 3510, 3497, 3484, 3471, 3458, 3445, 3433, 3420, - 3408, 3395, 3383, 3371, 3359, 3347, 3335, 3323, 3311, 3300, - 3288, 3276, 3265, 3254, 3242, 3231, 3220, 3209, 3198, 3187, - 3176, 3166, 3155, 3144, 3134, 3124, 3113, 3103, 3093, 3082, - 3072, 3062, 3052, 3042, 3032, 3023, 3013, 3003, 2994, 2984, - 2975, 2965, 2956, 2947, 2937, 2928, 2919, 2910, 2901, 2892, - 2883, 2874, 2865, 2857, 2848, 2839, 2831, 2822, 2814, 2805, - 2797, 2789, 2780, 2772, 2764, 2756, 2748, 2740, 2732, 2724, - 2716, 2708, 2700, 2692, 2685, 2677, 2669, 2662, 2654, 2647, - 2639, 2632, 2625, 2617, 2610, 2603, 2595, 2588, 2581, 2574, - 2567, 2560, 2553, 2546, 2539, 2532, 2525, 2519, 2512, 2505, - 2499, 2492, 2485, 2479, 2472, 2466, 2459, 2453, 2446, 2440, - 2434, 2427, 2421, 2415, 2409, 2402, 2396, 2390, 2384, 2378, - 2372, 2366, 2360, 2354, 2348, 2342, 2336, 2331, 2325, 2319, - 2313, 2308, 2302, 2296, 2291, 2285, 2280, 2274, 2268, 2263, - 2258, 2252, 2247, 2241, 2236, 2231, 2225, 2220, 2215, 2210, - 2204, 2199, 2194, 2189, 2184, 2179, 2174, 2169, 2164, 2159, - 2154, 2149, 2144, 2139, 2134, 2129, 2124, 2120, 2115, 2110, - 2105, 2101, 2096, 2091, 2086, 2082, 2077, 2073, 2068, 2063, - 2059, 2054, 2050, 2045, 2041, 2037, 2032, 2028, 2023, 2019, - 2015, 2010, 2006, 2002, 1997, 1993, 1989, 1985, 1981, 1976, - 1972, 1968, 1964, 1960, 1956, 1952, 1948, 1944, 1939, 1935, - 1931, 1928, 1924, 1920, 1916, 1912, 1908, 1904, 1900, 1896, - 1892, 1889, 1885, 1881, 1877, 1874, 1870, 1866, 1862, 1859, - 1855, 1851, 1848, 1844, 1840, 1837, 1833, 1830, 1826, 1822, - 1819, 1815, 1812, 1808, 1805, 1801, 1798, 1794, 1791, 1788, - 1784, 1781, 1777, 1774, 1771, 1767, 1764, 1761, 1757, 1754, - 1751, 1748, 1744, 1741, 1738, 1735, 1731, 1728, 1725, 1722, - 1719, 1716, 1712, 1709, 1706, 1703, 1700, 1697, 1694, 1691, - 1688, 1685, 1682, 1679, 1676, 1673, 1670, 1667, 1664, 1661, - 1658, 1655, 1652, 1649, 1646, 1643, 1640, 1637, 1635, 1632, - 1629, 1626, 1623, 1620, 1618, 1615, 1612, 1609, 1607, 1604, - 1601, 1598, 1596, 1593, 1590, 1587, 1585, 1582, 1579, 1577, - 1574, 1571, 1569, 1566, 1564, 1561, 1558, 1556, 1553, 1551, - 1548, 1546, 1543, 1540, 1538, 1535, 1533, 1530, 1528, 1525, - 1523, 1520, 1518, 1515, 1513, 1511, 1508, 1506, 1503, 1501, - 1499, 1496, 1494, 1491, 1489, 1487, 1484, 1482, 1480, 1477, - 1475, 1473, 1470, 1468, 1466, 1463, 1461, 1459, 1457, 1454, - 1452, 1450, 1447, 1445, 1443, 1441, 1439, 1436, 1434, 1432, - 1430, 1428, 1425, 1423, 1421, 1419, 1417, 1415, 1413, 1410, - 1408, 1406, 1404, 1402, 1400, 1398, 1396, 1394, 1392, 1389, - 1387, 1385, 1383, 1381, 1379, 1377, 1375, 1373, 1371, 1369, - 1367, 1365, 1363, 1361, 1359, 1357, 1355, 1353, 1351, 1349, - 1347, 1345, 1344, 1342, 1340, 1338, 1336, 1334, 1332, 1330, - 1328, 1326, 1325, 1323, 1321, 1319, 1317, 1315, 1313, 1312, - 1310, 1308, 1306, 1304, 1302, 1301, 1299, 1297, 1295, 1293, - 1292, 1290, 1288, 1286, 1285, 1283, 1281, 1279, 1277, 1276, - 1274, 1272, 1271, 1269, 1267, 1265, 1264, 1262, 1260, 1259, - 1257, 1255, 1254, 1252, 1250, 1249, 1247, 1245, 1244, 1242, - 1240, 1239, 1237, 1235, 1234, 1232, 1230, 1229, 1227, 1226, - 1224, 1222, 1221, 1219, 1218, 1216, 1214, 1213, 1211, 1210, - 1208, 1207, 1205, 1204, 1202, 1200, 1199, 1197, 1196, 1194, - 1193, 1191, 1190, 1188, 1187, 1185, 1184, 1182, 1181, 1179, - 1178, 1176, 1175, 1173, 1172, 1170, 1169, 1167, 1166, 1165, - 1163, 1162, 1160, 1159, 1157, 1156, 1154, 1153, 1152, 1150, - 1149, 1147, 1146, 1145, 1143, 1142, 1140, 1139, 1138, 1136, - 1135, 1133, 1132, 1131, 1129, 1128, 1127, 1125, 1124, 1123, - 1121, 1120, 1119, 1117, 1116, 1115, 1113, 1112, 1111, 1109, - 1108, 1107, 1105, 1104, 1103, 1101, 1100, 1099, 1098, 1096, - 1095, 1094, 1092, 1091, 1090, 1089, 1087, 1086, 1085, 1084, - 1082, 1081, 1080, 1079, 1077, 1076, 1075, 1074, 1072, 1071, - // 1070, 1069, 1067, 1066, 1065, 1064, 1063, 1061, 1060, 1059, - // 1058, 1057, 1055, 1054, 1053, 1052, 1051, 1050, 1048, 1047, - // 1046, 1045, 1044, 1042, 1041, 1040, 1039, 1038, 1037, 1036, - // 1034, 1033, 1032, 1031, 1030, 1029, 1028, 1026, 1025, 1024, - // 1023, 1022, 1021, 1020, 1019, 1018, 1016, 1015, 1014, 1013, - // 1012, 1011, 1010, 1009, 1008, 1007, 1005, 1004, 1003, 1002, - // 1001, 1000, 999, 998, 997, 996, 995, 994, 993, 992, - // 991, 990, 988, 987, 986, 985, 984, 983, 982, 981, - // 980, 979, 978, 977, 976, 975, 974, 973, 972, 971, - // 970, 969, 968, 967, 966, 965, 964, 963, 962, 961, - // 960, 959, 958, 957, 956, 955, 954, 953, 952, 951, - // 950, 949, 948, 947, 946, 945, 945, 944, 943, 942, - // 941, 940, 939, 938, 937, 936, 935, 934, 933, 932, - // 931, 930, 929, 929, 928, 927, 926, 925, 924, 923, - // 922, 921, 920, 919, 919, 918, 917, 916, 915, 914, - // 913, 912, 911, 910, 910, 909, 908, 907, 906, 905, - // 904, 903, 903, 902, 901, 900, 899, 898, 897, 896, - // 896, 895, 894, 893, 892, 891, 891, 890, 889, 888, - // 887, 886, 885, 885, 884, 883, 882, 881, 880, 880, - // 879, 878, 877, 876, 875, 875, 874, 873, 872, 871, - // 871, 870, 869, 868, 867, 867, 866, 865, 864, 863, - // 863, 862, 861, 860, 859, 859, 858, 857, 856, 855, - // 855, 854, 853, 852, 852, 851, 850, 849, 848, 848, - // 847, 846, 845, 845, 844, 843, 842, 842, 841, 840, - // 839, 839, 838, 837, 836, 836, 835, 834, 833, 833, - // 832, 831, 830, 830, 829, 828, 827, 827, 826, 825, - // 824, 824, 823, 822, 822, 821, 820, 819, 819, 818, - // 817, 817, 816, 815, 814, 814, 813, 812, 812, 811, - // 810, 809, 809, 808, 807, 807, 806, 805, 805, 804, - // 803, 803, 802, 801, 800, 800, 799, 798, 798, 797, - // 796, 796, 795, 794, 794, 793, 792, 792, 791, 790, - // 790, 789, 788, 788, 787, 786, 786, 785, 784, 784, - // 783, 782, 782, 781, 780, 780, + 62499, 58592, 55146, 52082, 49341, 46874, 44641, 42612, 40759, 39061, + 37499, 36056, 34721, 33481, 32326, 31249, 30240, 29295, 28408, 27572, + 26784, 26040, 25336, 24670, 24037, 23436, 22864, 22320, 21801, 21305, + 20832, 20379, 19945, 19530, 19131, 18749, 18381, 18027, 17687, 17360, + 17044, 16740, 16446, 16162, 15888, 15624, 15367, 15119, 14879, 14647, + 14422, 14203, 13991, 13785, 13585, 13391, 13203, 13019, 12841, 12667, + 12499, 12334, 12174, 12018, 11866, 11717, 11573, 11431, 11294, 11159, + 11028, 10900, 10774, 10652, 10532, 10415, 10301, 10189, 10079, 9972, + 9867, 9764, 9663, 9565, 9468, 9374, 9281, 9190, 9100, 9013, + 8927, 8843, 8760, 8679, 8599, 8521, 8444, 8369, 8295, 8222, + 8151, 8080, 8011, 7943, 7877, 7811, 7746, 7683, 7620, 7559, + 7499, 7439, 7380, 7323, 7266, 7210, 7155, 7101, 7047, 6995, + 6943, 6892, 6842, 6792, 6743, 6695, 6647, 6601, 6554, 6509, + 6464, 6420, 6376, 6333, 6290, 6249, 6207, 6166, 6126, 6086, + 6047, 6008, 5970, 5932, 5895, 5858, 5821, 5786, 5750, 5715, + 5680, 5646, 5612, 5579, 5546, 5513, 5481, 5449, 5418, 5386, + 5356, 5325, 5295, 5265, 5236, 5207, 5178, 5150, 5121, 5094, + 5066, 5039, 5012, 4985, 4959, 4933, 4907, 4881, 4856, 4831, + 4806, 4782, 4757, 4733, 4710, 4686, 4663, 4640, 4617, 4594, + 4572, 4549, 4527, 4506, 4484, 4463, 4442, 4421, 4400, 4379, + 4359, 4339, 4319, 4299, 4279, 4260, 4241, 4221, 4203, 4184, + 4165, 4147, 4128, 4110, 4092, 4075, 4057, 4039, 4022, 4005, + 3988, 3971, 3954, 3938, 3921, 3905, 3889, 3872, 3857, 3841, + 3825, 3809, 3794, 3779, 3764, 3749, 3734, 3719, 3704, 3689, + 3675, 3661, 3646, 3632, 3618, 3604, 3590, 3577, 3563, 3550, + 3536, 3523, 3510, 3497, 3484, 3471, 3458, 3445, 3433, 3420, + 3408, 3395, 3383, 3371, 3359, 3347, 3335, 3323, 3311, 3300, + 3288, 3276, 3265, 3254, 3242, 3231, 3220, 3209, 3198, 3187, + 3176, 3166, 3155, 3144, 3134, 3124, 3113, 3103, 3093, 3082, + 3072, 3062, 3052, 3042, 3032, 3023, 3013, 3003, 2994, 2984, + 2975, 2965, 2956, 2947, 2937, 2928, 2919, 2910, 2901, 2892, + 2883, 2874, 2865, 2857, 2848, 2839, 2831, 2822, 2814, 2805, + 2797, 2789, 2780, 2772, 2764, 2756, 2748, 2740, 2732, 2724, + 2716, 2708, 2700, 2692, 2685, 2677, 2669, 2662, 2654, 2647, + 2639, 2632, 2625, 2617, 2610, 2603, 2595, 2588, 2581, 2574, + 2567, 2560, 2553, 2546, 2539, 2532, 2525, 2519, 2512, 2505, + 2499, 2492, 2485, 2479, 2472, 2466, 2459, 2453, 2446, 2440, + 2434, 2427, 2421, 2415, 2409, 2402, 2396, 2390, 2384, 2378, + 2372, 2366, 2360, 2354, 2348, 2342, 2336, 2331, 2325, 2319, + 2313, 2308, 2302, 2296, 2291, 2285, 2280, 2274, 2268, 2263, + 2258, 2252, 2247, 2241, 2236, 2231, 2225, 2220, 2215, 2210, + 2204, 2199, 2194, 2189, 2184, 2179, 2174, 2169, 2164, 2159, + 2154, 2149, 2144, 2139, 2134, 2129, 2124, 2120, 2115, 2110, + 2105, 2101, 2096, 2091, 2086, 2082, 2077, 2073, 2068, 2063, + 2059, 2054, 2050, 2045, 2041, 2037, 2032, 2028, 2023, 2019, + 2015, 2010, 2006, 2002, 1997, 1993, 1989, 1985, 1981, 1976, + 1972, 1968, 1964, 1960, 1956, 1952, 1948, 1944, 1939, 1935, + 1931, 1928, 1924, 1920, 1916, 1912, 1908, 1904, 1900, 1896, + 1892, 1889, 1885, 1881, 1877, 1874, 1870, 1866, 1862, 1859, + 1855, 1851, 1848, 1844, 1840, 1837, 1833, 1830, 1826, 1822, + 1819, 1815, 1812, 1808, 1805, 1801, 1798, 1794, 1791, 1788, + 1784, 1781, 1777, 1774, 1771, 1767, 1764, 1761, 1757, 1754, + 1751, 1748, 1744, 1741, 1738, 1735, 1731, 1728, 1725, 1722, + 1719, 1716, 1712, 1709, 1706, 1703, 1700, 1697, 1694, 1691, + 1688, 1685, 1682, 1679, 1676, 1673, 1670, 1667, 1664, 1661, + 1658, 1655, 1652, 1649, 1646, 1643, 1640, 1637, 1635, 1632, + 1629, 1626, 1623, 1620, 1618, 1615, 1612, 1609, 1607, 1604, + 1601, 1598, 1596, 1593, 1590, 1587, 1585, 1582, 1579, 1577, + 1574, 1571, 1569, 1566, 1564, 1561, 1558, 1556, 1553, 1551, + 1548, 1546, 1543, 1540, 1538, 1535, 1533, 1530, 1528, 1525, + 1523, 1520, 1518, 1515, 1513, 1511, 1508, 1506, 1503, 1501, + 1499, 1496, 1494, 1491, 1489, 1487, 1484, 1482, 1480, 1477, + 1475, 1473, 1470, 1468, 1466, 1463, 1461, 1459, 1457, 1454, + 1452, 1450, 1447, 1445, 1443, 1441, 1439, 1436, 1434, 1432, + 1430, 1428, 1425, 1423, 1421, 1419, 1417, 1415, 1413, 1410, + 1408, 1406, 1404, 1402, 1400, 1398, 1396, 1394, 1392, 1389, + 1387, 1385, 1383, 1381, 1379, 1377, 1375, 1373, 1371, 1369, + 1367, 1365, 1363, 1361, 1359, 1357, 1355, 1353, 1351, 1349, + 1347, 1345, 1344, 1342, 1340, 1338, 1336, 1334, 1332, 1330, + 1328, 1326, 1325, 1323, 1321, 1319, 1317, 1315, 1313, 1312, + 1310, 1308, 1306, 1304, 1302, 1301, 1299, 1297, 1295, 1293, + 1292, 1290, 1288, 1286, 1285, 1283, 1281, 1279, 1277, 1276, + 1274, 1272, 1271, 1269, 1267, 1265, 1264, 1262, 1260, 1259, + 1257, 1255, 1254, 1252, 1250, 1249, 1247, 1245, 1244, 1242, + 1240, 1239, 1237, 1235, 1234, 1232, 1230, 1229, 1227, 1226, + 1224, 1222, 1221, 1219, 1218, 1216, 1214, 1213, 1211, 1210, + 1208, 1207, 1205, 1204, 1202, 1200, 1199, 1197, 1196, 1194, + 1193, 1191, 1190, 1188, 1187, 1185, 1184, 1182, 1181, 1179, + 1178, 1176, 1175, 1173, 1172, 1170, 1169, 1167, 1166, 1165, + 1163, 1162, 1160, 1159, 1157, 1156, 1154, 1153, 1152, 1150, + 1149, 1147, 1146, 1145, 1143, 1142, 1140, 1139, 1138, 1136, + 1135, 1133, 1132, 1131, 1129, 1128, 1127, 1125, 1124, 1123, + 1121, 1120, 1119, 1117, 1116, 1115, 1113, 1112, 1111, 1109, + 1108, 1107, 1105, 1104, 1103, 1101, 1100, 1099, 1098, 1096, + 1095, 1094, 1092, 1091, 1090, 1089, 1087, 1086, 1085, 1084, + 1082, 1081, 1080, 1079, 1077, 1076, 1075, 1074, 1072, 1071, + // 1070, 1069, 1067, 1066, 1065, 1064, 1063, 1061, 1060, 1059, + // 1058, 1057, 1055, 1054, 1053, 1052, 1051, 1050, 1048, 1047, + // 1046, 1045, 1044, 1042, 1041, 1040, 1039, 1038, 1037, 1036, + // 1034, 1033, 1032, 1031, 1030, 1029, 1028, 1026, 1025, 1024, + // 1023, 1022, 1021, 1020, 1019, 1018, 1016, 1015, 1014, 1013, + // 1012, 1011, 1010, 1009, 1008, 1007, 1005, 1004, 1003, 1002, + // 1001, 1000, 999, 998, 997, 996, 995, 994, 993, 992, + // 991, 990, 988, 987, 986, 985, 984, 983, 982, 981, + // 980, 979, 978, 977, 976, 975, 974, 973, 972, 971, + // 970, 969, 968, 967, 966, 965, 964, 963, 962, 961, + // 960, 959, 958, 957, 956, 955, 954, 953, 952, 951, + // 950, 949, 948, 947, 946, 945, 945, 944, 943, 942, + // 941, 940, 939, 938, 937, 936, 935, 934, 933, 932, + // 931, 930, 929, 929, 928, 927, 926, 925, 924, 923, + // 922, 921, 920, 919, 919, 918, 917, 916, 915, 914, + // 913, 912, 911, 910, 910, 909, 908, 907, 906, 905, + // 904, 903, 903, 902, 901, 900, 899, 898, 897, 896, + // 896, 895, 894, 893, 892, 891, 891, 890, 889, 888, + // 887, 886, 885, 885, 884, 883, 882, 881, 880, 880, + // 879, 878, 877, 876, 875, 875, 874, 873, 872, 871, + // 871, 870, 869, 868, 867, 867, 866, 865, 864, 863, + // 863, 862, 861, 860, 859, 859, 858, 857, 856, 855, + // 855, 854, 853, 852, 852, 851, 850, 849, 848, 848, + // 847, 846, 845, 845, 844, 843, 842, 842, 841, 840, + // 839, 839, 838, 837, 836, 836, 835, 834, 833, 833, + // 832, 831, 830, 830, 829, 828, 827, 827, 826, 825, + // 824, 824, 823, 822, 822, 821, 820, 819, 819, 818, + // 817, 817, 816, 815, 814, 814, 813, 812, 812, 811, + // 810, 809, 809, 808, 807, 807, 806, 805, 805, 804, + // 803, 803, 802, 801, 800, 800, 799, 798, 798, 797, + // 796, 796, 795, 794, 794, 793, 792, 792, 791, 790, + // 790, 789, 788, 788, 787, 786, 786, 785, 784, 784, + // 783, 782, 782, 781, 780, 780, }; uint16_t counterValue(uint16_t bpm) @@ -143,7 +147,7 @@ uint16_t counterValue(uint16_t bpm) class Timer1 { private: - uint16_t _bpm = 120; + uint16_t _bpm = DEFAULT_BPM; uint8_t _clockPin; uint8_t _subdivisionPin; uint8_t _subdivisions = 4; @@ -245,6 +249,13 @@ class Timer1 TCNT1 = 0; // reset timer } + void restoreDefaults() + { + setSubdivisions(DEFAULT_SUBDIVISIONS); + setBPM(DEFAULT_BPM); + setSwing(DEFAULT_SWING); + } + bool clockOn() { return _clockHigh; diff --git a/clock/src/main.cpp b/clock/src/main.cpp index cc1d17f..843cd2c 100644 --- a/clock/src/main.cpp +++ b/clock/src/main.cpp @@ -11,9 +11,6 @@ #define A_BUTTON_PIN 8 #define B_BUTTON_PIN 9 #define FULL_RESET_TIME_MS 2000 -#define DEFAULT_BPM 120 -#define DEFAULT_SUBDIVISIONS 2 -#define DEFAULT_SWING 0 Display<3> display; Knob<12> knob; @@ -21,17 +18,18 @@ Button aButton; Button bButton; TapTempo tapTempo; -uint8_t DISPLAY_CTRL_PINS[] = { 7, 6, 5 }; +uint8_t DISPLAY_CTRL_PINS[] = {7, 6, 5}; void setup() { // Serial.begin(9600); - DDRF = 0x7F; // pinMode OUTPUT for all of PORTF display.begin(&PORTF, DISPLAY_CTRL_PINS); - // indicate we're doing setup - display.setChar(0, '-'); - display.setChar(1, '-'); - display.setChar(2, '-'); + // indicate we're doing setup by writing a dash + // to each display and stepping through them + // at each step + display.setChar(0, '_'); + display.setChar(1, '_'); + display.setChar(2, '_'); display.tick(); Timer.begin(CLOCK_PIN, SUBDIV_PIN); display.tick(); @@ -53,7 +51,7 @@ void setup() } void loop() -{ +{ uint16_t tapBpm = tapTempo.tick(aButton.isPressed()); int8_t knobMotion = knob.readChanges(); @@ -65,10 +63,8 @@ void loop() display.displayReset(); if (aButton.holdTime() >= FULL_RESET_TIME_MS && bButton.holdTime() >= FULL_RESET_TIME_MS) { - // Full reset, restore all defaults - Timer.setSubdivisions(DEFAULT_SUBDIVISIONS); - Timer.setBPM(DEFAULT_BPM); - Timer.setSwing(DEFAULT_SWING); + // Full reset + Timer.restoreDefaults(); display.blinkReset(); } } @@ -103,7 +99,8 @@ void loop() display.displayNumber(bpm); } - for (size_t t = 0; t < 10; t++) { + for (size_t t = 0; t < 10; t++) + { display.tick(); delay(1); }