1
1

drumduino_firmware.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 = 6,
  9. CHAN_PER_PORT_CNT = 8,
  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 1
  96. struct SysexFrame {
  97. byte begin = 0xf0;
  98. byte manufacturer = 42;
  99. byte msgType = 0;
  100. unsigned long time1 = 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. midi::SysexFrame sysexFrame;
  268. uint64_t& frameCounter = g_runtime.frameCounter;
  269. size_t curFrameIdx = frameCounter % FRAME_BUFFER_SIZE;
  270. size_t lastFrameIdx = (frameCounter - 1) % FRAME_BUFFER_SIZE;
  271. for(uint8_t chan = 0; chan < CHAN_PER_PORT_CNT; ++chan) {
  272. multiplexSelectChan(chan);
  273. for(uint8_t port = 0; port < PORT_CNT; ++port) {
  274. int pad = port * CHAN_PER_PORT_CNT + chan;
  275. // shortcuts!
  276. uint8_t& currentValue = g_runtime.value[pad][curFrameIdx];
  277. const uint8_t& lastValue = g_runtime.value[pad][lastFrameIdx];
  278. Runtime::State& state = g_runtime.state[pad];
  279. uint8_t& triggerFrame = g_runtime.trigger[pad];
  280. uint8_t& maxValue = g_runtime.max[pad];
  281. //const uint8_t& sumValue = g_runtime.sum[pad];
  282. const Configuration::Type& type = g_configuration.type[pad];
  283. const uint8_t& threshold = g_configuration.threshold[pad];
  284. const uint8_t& scanTime = g_configuration.scanTime[pad];
  285. const uint8_t& maskTime = g_configuration.maskTime[pad];
  286. // real processing
  287. currentValue = uint8_t(analogRead(port) >> 3);
  288. switch(type) {
  289. case Configuration::TypePiezo: {
  290. switch(state) {
  291. // In this state we wait for a signal to trigger
  292. default:
  293. case Runtime::StateAwait: {
  294. STATE_AGAIN:
  295. if(currentValue < lastValue + threshold) {
  296. break;
  297. }
  298. state = Runtime::StateScan;
  299. triggerFrame = frameCounter;
  300. maxValue = currentValue;
  301. //sumValue = currentValue;
  302. //### fallthrough
  303. }
  304. // In this state we measure the value for the given time period to get the max value
  305. case Runtime::StateScan: {
  306. if(frameCounter < triggerFrame + scanTime) {
  307. maxValue = max(currentValue, maxValue);
  308. //sumValue += currentValue;
  309. break;
  310. }
  311. const Configuration::CurveSettings& curve = g_configuration.curve[pad];
  312. uint8_t velocity = calcCurve(curve, maxValue);
  313. const uint8_t& note = g_configuration.note[pad];
  314. midi::noteOn(g_runtime.softSerial, note, velocity);
  315. state = Runtime::StateMask;
  316. #if USE_DISPLAY
  317. g_runtime.displayTriggerEvent[pad] = true;
  318. #endif
  319. //### fallthrough
  320. }
  321. // In this state we do nothing to prevent retriggering
  322. case Runtime::StateMask: {
  323. if(frameCounter < triggerFrame + scanTime + maskTime) {
  324. break;
  325. }
  326. state = Runtime::StateAwait;
  327. //goto STATE_AGAIN;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. #if USE_DISPLAY
  335. if(frameCounter % 1000 == 0) {
  336. lcd.clear();
  337. lcd.setCursor(0, 0);
  338. switch(g_configuration.type[0]) {
  339. default:
  340. case Configuration::TypeDisabled: {
  341. lcd.print("Off");
  342. break;
  343. }
  344. case Configuration::TypePiezo: {
  345. lcd.print("Piez");
  346. break;
  347. }
  348. }
  349. lcd.setCursor(5, 0);
  350. lcd.print(g_configuration.note[0]);
  351. lcd.setCursor(9, 0);
  352. lcd.print(g_configuration.scanTime[0]);
  353. lcd.setCursor(13, 0);
  354. lcd.print(g_configuration.threshold[0]);
  355. lcd.setCursor(0, 1);
  356. lcd.print("CurV:");
  357. lcd.setCursor(6, 1);
  358. lcd.print(g_runtime.value[0][curFrameIdx]);
  359. lcd.setCursor(11, 1);
  360. switch(g_runtime.state[0]) {
  361. default:
  362. case Runtime::StateAwait: {
  363. lcd.print("A");
  364. break;
  365. }
  366. case Runtime::StateScan: {
  367. lcd.print("S");
  368. break;
  369. }
  370. case Runtime::StateMask: {
  371. lcd.print("M");
  372. break;
  373. }
  374. }
  375. lcd.setCursor(13, 1);
  376. if(g_runtime.displayTriggerEvent[0]) {
  377. lcd.print("[x]");
  378. }
  379. else {
  380. lcd.print("[ ]");
  381. }
  382. }
  383. if(frameCounter % 1000 == 0) {
  384. memset(g_runtime.displayTriggerEvent, 0, sizeof(g_runtime.displayTriggerEvent));
  385. }
  386. #endif
  387. sysexFrame.time1 = millis();
  388. for(int pad = 0; pad < PAD_CNT; ++pad) {
  389. sysexFrame.values[pad] = g_runtime.value[pad][curFrameIdx];
  390. }
  391. Serial.write((const char*)&sysexFrame, sizeof(sysexFrame));
  392. ++frameCounter;
  393. }