You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.3KB

  1. /* TriadexEngine object */
  2. /* triadex0 was back from gdb online */
  3. class TriadexEngine {
  4. public:
  5. void setIntervalAndTheme(int *, int *); /* as slot #s, see slider */
  6. void halfStep(void); /* half step the engine */
  7. void reset(void); /* clear counters and shift register */
  8. unsigned int bitValue(int); /* need for lamp control - add setBitValue one day? */
  9. float getNote(void); /* the note as (octave << 3) + scale[0..7] so /12 is a CV <- NOT! */
  10. void getTriadexState(int *); /* JSON kludge? */
  11. void setTriadexState(int *);
  12. /* everything else is */
  13. private:
  14. unsigned int calculateLFSRBit();
  15. int *interval; int *theme; /* kludge - sorry Mom! */
  16. /* Triadex state variables */
  17. unsigned int count6, count32, count3;
  18. unsigned int lfsrBits;
  19. /* fixed major scale. 'C' appears twice in a run up - Triadex! */
  20. const int scale[8] = {0, 2, 4, 5, 7, 9, 11, 12};
  21. /* slider 0..39 select interval and theme bits, follow
  22. CONST and 0 or 1 OFF, ON
  23. COUNT32 and bit select mask - count32
  24. COUNT6 and bit select mask - count6
  25. LFSR and bit select mask - lfsrBits
  26. */
  27. enum bitType {
  28. CONST, COUNT32, COUNT6, LFSR
  29. };
  30. const struct {
  31. int controlBitType;
  32. unsigned int arg;
  33. } slider[40] = {
  34. /* 0 */ {LFSR, 0x1 << 30},
  35. /* 1 */ {LFSR, 0x1 << 29},
  36. /* 2 */ {LFSR, 0x1 << 28},
  37. /* 3 */ {LFSR, 0x1 << 27},
  38. /* 4 */ {LFSR, 0x1 << 26},
  39. /* 5 */ {LFSR, 0x1 << 25},
  40. /* 6 */ {LFSR, 0x1 << 24},
  41. /* 7 */ {LFSR, 0x1 << 23},
  42. /* 8 */ {LFSR, 0x1 << 22},
  43. /* 9 */ {LFSR, 0x1 << 21},
  44. /* 10 */ {LFSR, 0x1 << 20},
  45. /* 11 */ {LFSR, 0x1 << 19},
  46. /* 12 */ {LFSR, 0x1 << 18},
  47. /* 13 */ {LFSR, 0x1 << 17},
  48. /* 14 */ {LFSR, 0x1 << 16},
  49. /* 15 */ {LFSR, 0x1 << 15},
  50. /* 16 */ {LFSR, 0x1 << 14},
  51. /* 17 */ {LFSR, 0x1 << 13},
  52. /* 18 */ {LFSR, 0x1 << 12},
  53. /* 19 */ {LFSR, 0x1 << 11},
  54. /* 20 */ {LFSR, 0x1 << 10},
  55. /* 21 */ {LFSR, 0x1 << 9},
  56. /* 22 */ {LFSR, 0x1 << 8},
  57. /* 23 */ {LFSR, 0x1 << 7},
  58. /* 24 */ {LFSR, 0x1 << 6},
  59. /* 25 */ {LFSR, 0x1 << 5},
  60. /* 26 */ {LFSR, 0x1 << 4},
  61. /* 27 */ {LFSR, 0x1 << 3},
  62. /* 28 */ {LFSR, 0x1 << 2},
  63. /* 29 */ {LFSR, 0x1 << 1},
  64. /* 30 */ {LFSR, 0x1 },
  65. /* 31 */ {COUNT6, 0x2},
  66. /* 32 */ {COUNT6, 0x1},
  67. /* 33 */ {COUNT32, 0x1 << 4},
  68. /* 34 */ {COUNT32, 0x1 << 3},
  69. /* 35 */ {COUNT32, 0x1 << 2},
  70. /* 36 */ {COUNT32, 0x1 << 1},
  71. /* 37 */ {COUNT32, 0x1 },
  72. /* 38 */ {CONST, 1},
  73. /* 39 */ {CONST, 0},
  74. };
  75. };
  76. void TriadexEngine::getTriadexState(int *vals)
  77. {
  78. vals[0] = count32;
  79. vals[1] = count3;
  80. vals[2] = count6;
  81. vals[3] = lfsrBits;
  82. }
  83. void TriadexEngine::setTriadexState(int *vals)
  84. {
  85. count32 = vals[0];
  86. count3 = vals[1];
  87. count6 = vals[2];
  88. lfsrBits = vals[3];
  89. }
  90. unsigned int TriadexEngine::bitValue(int slot)
  91. {
  92. switch (slider[slot].controlBitType) {
  93. case CONST :
  94. return !!slider[slot].arg;
  95. case COUNT6 :
  96. return !!(count6 & slider[slot].arg);
  97. case COUNT32 :
  98. return !!(count32 & slider[slot].arg);
  99. case LFSR :
  100. return !!(lfsrBits & slider[slot].arg);
  101. }
  102. return (-1); /* shouldn't */
  103. }
  104. void TriadexEngine::setIntervalAndTheme(int *interval, int *theme)
  105. {
  106. this->interval = interval;
  107. this->theme = theme;
  108. }
  109. float TriadexEngine::getNote()
  110. {
  111. int note = 0;
  112. for (int i = 0, j = 1; i < 4; i++, j <<= 1)
  113. note += bitValue(interval[i]) ? j : 0; /* can ya bool << i ? */
  114. return ((scale[note & 0x7] + ((note & 0x8) ? 12 : 0)) / 12.0f);
  115. }
  116. unsigned int TriadexEngine::calculateLFSRBit()
  117. {
  118. unsigned int bit = 1u; /* XNOR, right? :-) */
  119. for (int i = 0; i < 4; i++) //{
  120. bit ^= bitValue(theme[i]);
  121. return (bit);
  122. }
  123. void TriadexEngine::halfStep()
  124. {
  125. unsigned int bit;
  126. count32++; count32 &= 0x1f;
  127. if (count32 & 0x1)
  128. return; /* rising edge, we done */
  129. if (++count3 == 3) {
  130. count3 = 0;
  131. count6++;
  132. count6 &= 0x3;
  133. }
  134. bit = calculateLFSRBit();
  135. lfsrBits <<= 1;
  136. lfsrBits |= bit;
  137. lfsrBits &= 0x7fffffff;
  138. }
  139. /* to see lights before any step Seg faulting... */
  140. void TriadexEngine::reset()
  141. {
  142. count3 = 0;
  143. count6 = 0;
  144. count32 = 0;
  145. lfsrBits = 0x0;
  146. }
  147. /* teset program stripped, see learningcpp/triadex.cpp */