1
1

drumduino_firmware.ino 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #define PORT_CNT 6
  2. #define CHAN_CNT 8
  3. #define MULTIPLEX_PIN_A 2
  4. #define MULTIPLEX_PIN_B 3
  5. #define MULTIPLEX_PIN_C 4
  6. inline void setPrescalers(byte i)
  7. {
  8. i = i % 7;
  9. const byte prescalers[] = {
  10. B00000000, // PS_2
  11. B00000010, // PS_4
  12. B00000011, // PS_8
  13. B00000100, // PS_16
  14. B00000101, // PS_32
  15. B00000110, // PS_64
  16. B00000111, // PS_128
  17. };
  18. ADCSRA &= ~prescalers[6];
  19. ADCSRA |= prescalers[2];
  20. }
  21. inline void multiplexSelectChan(uint8_t chan)
  22. {
  23. PORTD = B00011100 & (chan << 2);
  24. }
  25. struct SysexFrame {
  26. byte begin;
  27. byte manufacturer;
  28. unsigned long time;
  29. byte values[CHAN_CNT* PORT_CNT];
  30. byte end;
  31. SysexFrame()
  32. : begin(0xf0)
  33. , manufacturer(42)
  34. , end(0xF7)
  35. {
  36. memset(values, 0, sizeof(values));
  37. }
  38. };
  39. SysexFrame _frame;
  40. void setup()
  41. {
  42. // Setup MultiplexSelection Pins
  43. pinMode(MULTIPLEX_PIN_A, OUTPUT);
  44. pinMode(MULTIPLEX_PIN_B, OUTPUT);
  45. pinMode(MULTIPLEX_PIN_C, OUTPUT);
  46. // Setup AD Pins
  47. analogReference(DEFAULT);
  48. // Setup Serial
  49. Serial.begin(115200);
  50. Serial.flush();
  51. //Configure Prescaler
  52. setPrescalers(2);
  53. }
  54. //inline void handleMessage(byte* msg, byte length)
  55. //{
  56. // switch(msg[0] && length == 3) {
  57. // case 0xff: {
  58. // setPrescalers(msg[1]);
  59. // _throttle = msg[2] != 0 ? msg[2] : 1;
  60. // }
  61. // }
  62. //}
  63. //
  64. //inline void input()
  65. //{
  66. // //read until we receive a sysex
  67. // while(Serial.peek() >= 0 && Serial.peek() != 0xF0) {
  68. // Serial.read();
  69. // }
  70. //
  71. // if(Serial.available() >= 6) {
  72. // byte start = Serial.read();
  73. // byte manufacturerId = Serial.read();
  74. // byte deviceId = Serial.read();
  75. // byte length = Serial.read();
  76. // byte value[128];
  77. //
  78. // if(length > 0) {
  79. // Serial.readBytes(value, length);
  80. // handleMessage(value, length);
  81. // }
  82. // }
  83. //}
  84. #define BURST_CNT 5
  85. inline void output()
  86. {
  87. for(uint8_t chan = 0; chan < CHAN_CNT; ++chan) {
  88. multiplexSelectChan(chan);
  89. for(uint8_t port = 0; port < PORT_CNT; ++port) {
  90. int channelNumber = port * CHAN_CNT + chan;
  91. byte& value = *(_frame.values + channelNumber);
  92. value = 0;
  93. for(uint8_t burst = 0; burst < BURST_CNT; ++burst) {
  94. byte v = byte(analogRead(port) >> 3);
  95. if(v > value) {
  96. value = v;
  97. }
  98. }
  99. }
  100. }
  101. _frame.time = millis();
  102. Serial.write((byte*)&_frame, sizeof(_frame));
  103. }
  104. void loop()
  105. {
  106. //input();
  107. output();
  108. }