1
1

drumduino_firmware.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #include <SoftwareSerial.h>
  2. #define USE_DISPLAY 1
  3. #if USE_DISPLAY
  4. #include <LiquidCrystal.h>
  5. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  6. #endif
  7. enum DrumduinoFirmwareSettings {
  8. PORT_CNT = 1,
  9. CHAN_PER_PORT_CNT = 1,
  10. PAD_CNT = PORT_CNT * CHAN_PER_PORT_CNT,
  11. FRAME_BUFFER_SIZE = 3,
  12. };
  13. enum Pins {
  14. PIN_MULTIPLEX_A = 2,
  15. PIN_MULTIPLEX_B = 3 ,
  16. PIN_MULTIPLEX_C = 4 ,
  17. PIN_SOFTSERIAL_RX = 5,
  18. PIN_SOFTSERIAL_TX = 6,
  19. };
  20. //=================================================================================
  21. // Set Bit and Clwear Bit Helpers
  22. //=================================================================================
  23. #ifndef cbi
  24. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  25. #endif
  26. #ifndef sbi
  27. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  28. #endif
  29. //=================================================================================
  30. // Prescaler
  31. //=================================================================================
  32. // Maximum sampling frequency // Resolution
  33. enum Prescaler {
  34. Prescaler_2 = B00000000, // 16 MHz / 2 = 8 MHz //
  35. Prescaler_4 = B00000010, // 16 MHz / 4 = 4 MHz // ~5.9
  36. Prescaler_8 = B00000011, // 16 MHz / 8 = 2 MHz // ~7.4
  37. Prescaler_16 = B00000100, // 16 MHz / 16 = 1 MHz // ~8.6
  38. Prescaler_32 = B00000101, // 16 MHz / 32 = 500 kHz // ~8.9
  39. Prescaler_64 = B00000110, // 16 MHz / 64 = 250 kHz // ~9.0
  40. Prescaler_128 = B00000111, // 16 MHz / 128 = 125 kHz // ~9.1
  41. };
  42. inline void setPrescaler(int prescaler) {
  43. ADCSRA &= B11111000;
  44. ADCSRA |= prescaler;
  45. }
  46. //=================================================================================
  47. // Ad Pin
  48. //=================================================================================
  49. enum AdPin {
  50. AdPin_0 = B00000000,
  51. AdPin_1 = B00000001,
  52. AdPin_2 = B00000010,
  53. AdPin_3 = B00000011,
  54. AdPin_4 = B00000100,
  55. AdPin_5 = B00000101,
  56. AdPin_6 = B00000110, // Bei Atmega8 nur in der Gehäusebauform TQFP und MLF verfügbar, nicht in PDIP
  57. AdPin_7 = B00000111, // Bei Atmega8 nur in der Gehäusebauform TQFP und MLF verfügbar, nicht in PDIP
  58. AdPin_Vbg = B00001110, // 1.23V
  59. AdPin_GND = B00001111, // 0V
  60. };
  61. inline void setAdPin(int adPin) {
  62. ADMUX &= B11110000;
  63. ADMUX |= adPin;
  64. }
  65. //=================================================================================
  66. // ADC Alignment
  67. //=================================================================================
  68. // Das Ergebnis wird in den Registern ADCH/ADCL linksbündig ausgerichtet.
  69. // Die 8 höchstwertigen Bits des Ergebnisses werden in ADCH abgelegt.
  70. // Die verbleibenden 2 niederwertigen Bits werden im Register ADCL in den Bits 6 und 7 abgelegt.
  71. enum AdcAlignment {
  72. ADAlignmentLeft = B00100000,
  73. ADAlignmentRight = B00000000,
  74. };
  75. inline void setADAlignment(int align) {
  76. ADMUX &= ~B00100000;
  77. ADMUX |= align;
  78. }
  79. //=================================================================================
  80. inline void startADCConversion() {
  81. ADCSRA |= B01000000;
  82. }
  83. //=================================================================================
  84. inline void disableAnalogComparator() {
  85. ACSR = B10000000;
  86. }
  87. inline void multiplexSelectChan(uint8_t chan) {
  88. PORTD = B00011100 & (chan << 2) | (B11100011 & PORTD);
  89. }
  90. //=================================================================================
  91. // MIDI
  92. //=================================================================================
  93. namespace midi {
  94. /// http://www.midi.org/techspecs/midimessages.php
  95. #if 0
  96. struct SysexFrame {
  97. byte begin = 0xf0;
  98. byte manufacturer = 42;
  99. unsigned long time1 = 0;
  100. unsigned long time2 = 0;
  101. byte values[PAD_CNT] = { 0 };
  102. byte end = 0xF7;
  103. };
  104. #endif
  105. /**
  106. Note Off event.
  107. This message is sent when a note is released (ended).
  108. */
  109. template<typename SERIAL_IF>
  110. void noteOn(SERIAL_IF& serial, uint8_t note, uint8_t velocity) {
  111. serial.write((uint8_t)0x90);
  112. serial.write((uint8_t)note);
  113. serial.write((uint8_t)velocity);
  114. }
  115. /**
  116. Note On event.
  117. This message is sent when a note is depressed (start).
  118. */
  119. template<typename SERIAL_IF>
  120. void noteOff(SERIAL_IF& serial, uint8_t note, uint8_t velocity) {
  121. serial.write((uint8_t)0x80);
  122. serial.write((uint8_t)note);
  123. serial.write((uint8_t)velocity);
  124. }
  125. /**
  126. Polyphonic Key Pressure (Aftertouch).
  127. This message is most often sent by pressing down on the key after it "bottoms out".
  128. */
  129. template<typename SERIAL_IF>
  130. void polyphonicKeyPressure(SERIAL_IF& serial, uint8_t note, uint8_t pressure) {
  131. serial.write((uint8_t)0xA0);
  132. serial.write((uint8_t)note);
  133. serial.write((uint8_t)pressure);
  134. }
  135. /**
  136. Control Change.
  137. This message is sent when a controller value changes.
  138. Controllers include devices such as pedals and levers.
  139. Controller numbers 120-127 are reserved as "Channel Mode Messages" (below).
  140. */
  141. template<typename SERIAL_IF>
  142. void controlChange(SERIAL_IF& serial, uint8_t controllerNumber, uint8_t controllerValue) {
  143. serial.write((uint8_t)0xB0);
  144. serial.write((uint8_t)controllerNumber);
  145. serial.write((uint8_t)controllerValue);
  146. }
  147. }
  148. //=================================================================================
  149. //
  150. //=================================================================================
  151. struct Configuration {
  152. enum Type {
  153. TypeDisabled,
  154. TypePiezo,
  155. } type[PAD_CNT] = { TypePiezo };
  156. struct CurveSettings {
  157. enum CurveType {
  158. CurveNormal,
  159. CurveExp,
  160. CurveLog,
  161. CurveSigma,
  162. CurveFlat,
  163. CurveExtra,
  164. };
  165. CurveType type = CurveNormal;
  166. uint8_t value = 127;
  167. int8_t offset = 0;
  168. uint8_t factor = 127;
  169. } curve[PAD_CNT];
  170. uint8_t note[PAD_CNT] = { 0 };
  171. uint8_t threshold[PAD_CNT] = { 25 };
  172. uint8_t scanTime[PAD_CNT] = { 25 };
  173. uint8_t maskTime[PAD_CNT] = { 35 };
  174. } g_configuration;
  175. //=================================================================================
  176. //
  177. //=================================================================================
  178. struct Runtime {
  179. SoftwareSerial softSerial{ PIN_SOFTSERIAL_RX, PIN_SOFTSERIAL_TX }; // RX, TX
  180. uint8_t value[PAD_CNT][FRAME_BUFFER_SIZE];
  181. enum State {
  182. StateAwait,
  183. StateScan,
  184. StateMask,
  185. } state[PAD_CNT] = { StateAwait };
  186. uint8_t trigger[PAD_CNT] = { 0 };
  187. uint8_t max[PAD_CNT] = { 0 };
  188. //uint64_t sum[PAD_CNT] = { 0 };
  189. uint64_t frameCounter = 0;
  190. #if USE_DISPLAY
  191. bool displayTriggerEvent[PAD_CNT] = { 0 };
  192. #endif
  193. } g_runtime;
  194. //=================================================================================
  195. //
  196. //=================================================================================
  197. inline uint8_t calcCurve(const Configuration::CurveSettings& curveSettings, uint8_t value) {
  198. uint8_t ret = 0;
  199. float x = value * 8.0;
  200. float f = ((float)curveSettings.value) / 64.0; //[1;127]->[0.;2.0]
  201. switch(curveSettings.type) {
  202. //[0-1023]x[0-127]
  203. case Configuration::CurveSettings::CurveNormal:
  204. ret = x * f / 16.0;
  205. break;
  206. case Configuration::CurveSettings::CurveExp:
  207. ret = (127.0 / (exp(2.0 * f) - 1)) * (exp(f * x / 512.0) - 1.0);
  208. break; //Exp 4*(exp(x/256)-1)
  209. case Configuration::CurveSettings::CurveLog:
  210. ret = log(1.0 + (f * x / 128.0)) * (127.0 / log((8 * f) + 1));
  211. break; //Log 64*log(1+x/128)
  212. case Configuration::CurveSettings::CurveSigma:
  213. ret = (127.0 / (1.0 + exp(f * (512.0 - x) / 64.0)));
  214. break; //Sigma
  215. case Configuration::CurveSettings::CurveFlat:
  216. ret = (64.0 - ((8.0 / f) * log((1024 / (1 + x)) - 1)));
  217. break; //Flat
  218. case Configuration::CurveSettings::CurveExtra:
  219. ret = (x + 0x20) * f / 16.0;
  220. }
  221. ret = ret * (curveSettings.factor / 127.0) + curveSettings.offset;
  222. if(ret <= 0) {
  223. return 0;
  224. }
  225. if(ret >= 127) {
  226. return 127; //127
  227. }
  228. return ret;
  229. }
  230. //=================================================================================
  231. //
  232. //=================================================================================
  233. void setup() {
  234. #if USE_DISPLAY
  235. lcd.begin(16, 2);
  236. delay(500);
  237. lcd.print("Drumduino 0.0.1");
  238. delay(1000);
  239. #endif
  240. memset(g_runtime.value, 0, sizeof(g_runtime.value));
  241. // generate note mapping
  242. for(uint8_t pad = 0; pad < PAD_CNT; ++pad) {
  243. uint8_t note = 0x1E + pad;
  244. g_configuration.note[pad] = note;
  245. }
  246. // Setup MultiplexSelection Pins
  247. pinMode(PIN_MULTIPLEX_A, OUTPUT);
  248. pinMode(PIN_MULTIPLEX_B, OUTPUT);
  249. pinMode(PIN_MULTIPLEX_C, OUTPUT);
  250. // Setup ADCs
  251. analogReference(DEFAULT);
  252. disableAnalogComparator();
  253. setPrescaler(Prescaler_8);
  254. //setADAlignment(ADAlignmentLeft);
  255. // Disable digital input buffers on all analog input pins
  256. DIDR0 = DIDR0 | B00111111;
  257. // Setup Serial
  258. Serial.begin(2000000);
  259. Serial.flush();
  260. g_runtime.softSerial.begin(31250);
  261. //g_runtime.softSerial.flush();
  262. }
  263. //=================================================================================
  264. //
  265. //=================================================================================
  266. void loop() {
  267. uint64_t& frameCounter = g_runtime.frameCounter;
  268. size_t curFrameIdx = frameCounter % FRAME_BUFFER_SIZE;
  269. size_t lastFrameIdx = (frameCounter - 1) % FRAME_BUFFER_SIZE;
  270. for(uint8_t chan = 0; chan < CHAN_PER_PORT_CNT; ++chan) {
  271. multiplexSelectChan(chan);
  272. for(uint8_t port = 0; port < PORT_CNT; ++port) {
  273. int pad = port * CHAN_PER_PORT_CNT + chan;
  274. // shortcuts!
  275. uint8_t& currentValue = g_runtime.value[pad][curFrameIdx];
  276. const uint8_t& lastValue = g_runtime.value[pad][lastFrameIdx];
  277. Runtime::State& state = g_runtime.state[pad];
  278. uint8_t& triggerFrame = g_runtime.trigger[pad];
  279. uint8_t& maxValue = g_runtime.max[pad];
  280. //const uint8_t& sumValue = g_runtime.sum[pad];
  281. const Configuration::Type& type = g_configuration.type[pad];
  282. const uint8_t& threshold = g_configuration.threshold[pad];
  283. const uint8_t& scanTime = g_configuration.scanTime[pad];
  284. const uint8_t& maskTime = g_configuration.maskTime[pad];
  285. // real processing
  286. currentValue = uint8_t(analogRead(port) >> 3);
  287. switch(type) {
  288. case Configuration::TypePiezo: {
  289. switch(state) {
  290. // In this state we wait for a signal to trigger
  291. default:
  292. case Runtime::StateAwait: {
  293. STATE_AGAIN:
  294. if(currentValue < lastValue + threshold) {
  295. break;
  296. }
  297. state = Runtime::StateScan;
  298. triggerFrame = frameCounter;
  299. maxValue = currentValue;
  300. //sumValue = currentValue;
  301. //### fallthrough
  302. }
  303. // In this state we measure the value for the given time period to get the max value
  304. case Runtime::StateScan: {
  305. if(frameCounter < triggerFrame + scanTime) {
  306. maxValue = max(currentValue, maxValue);
  307. //sumValue += currentValue;
  308. break;
  309. }
  310. const Configuration::CurveSettings& curve = g_configuration.curve[pad];
  311. uint8_t velocity = calcCurve(curve, maxValue);
  312. const uint8_t& note = g_configuration.note[pad];
  313. midi::noteOn(g_runtime.softSerial, note, velocity);
  314. state = Runtime::StateMask;
  315. #if USE_DISPLAY
  316. g_runtime.displayTriggerEvent[pad] = true;
  317. #endif
  318. //### fallthrough
  319. }
  320. // In this state we do nothing to prevent retriggering
  321. case Runtime::StateMask: {
  322. if(frameCounter < triggerFrame + scanTime + maskTime) {
  323. break;
  324. }
  325. state = Runtime::StateAwait;
  326. //goto STATE_AGAIN;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. #if USE_DISPLAY
  334. if(frameCounter % 1000 == 0) {
  335. lcd.clear();
  336. lcd.setCursor(0, 0);
  337. switch(g_configuration.type[0]) {
  338. default:
  339. case Configuration::TypeDisabled: {
  340. lcd.print("Off");
  341. break;
  342. }
  343. case Configuration::TypePiezo: {
  344. lcd.print("Piez");
  345. break;
  346. }
  347. }
  348. lcd.setCursor(5, 0);
  349. lcd.print(g_configuration.note[0]);
  350. lcd.setCursor(9, 0);
  351. lcd.print(g_configuration.scanTime[0]);
  352. lcd.setCursor(13, 0);
  353. lcd.print(g_configuration.threshold[0]);
  354. lcd.setCursor(0, 1);
  355. lcd.print("CurV:");
  356. lcd.setCursor(6, 1);
  357. lcd.print(g_runtime.value[0][curFrameIdx]);
  358. lcd.setCursor(11, 1);
  359. switch(g_runtime.state[0]) {
  360. default:
  361. case Runtime::StateAwait: {
  362. lcd.print("A");
  363. break;
  364. }
  365. case Runtime::StateScan: {
  366. lcd.print("S");
  367. break;
  368. }
  369. case Runtime::StateMask: {
  370. lcd.print("M");
  371. break;
  372. }
  373. }
  374. lcd.setCursor(13, 1);
  375. if(g_runtime.displayTriggerEvent[0]) {
  376. lcd.print("[x]");
  377. }
  378. else {
  379. lcd.print("[ ]");
  380. }
  381. }
  382. if(frameCounter % 1000 == 0) {
  383. memset(g_runtime.displayTriggerEvent, 0, sizeof(g_runtime.displayTriggerEvent));
  384. }
  385. #endif
  386. ++frameCounter;
  387. }