muse.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. enum {
  2. MUSE_OFF,
  3. MUSE_ON,
  4. MUSE_C_1_2,
  5. MUSE_C1,
  6. MUSE_C2,
  7. MUSE_C4,
  8. MUSE_C8,
  9. MUSE_C3,
  10. MUSE_C6,
  11. MUSE_B1,
  12. MUSE_B2,
  13. MUSE_B3,
  14. MUSE_B4,
  15. MUSE_B5,
  16. MUSE_B6,
  17. MUSE_B7,
  18. MUSE_B8,
  19. MUSE_B9,
  20. MUSE_B10,
  21. MUSE_B11,
  22. MUSE_B12,
  23. MUSE_B13,
  24. MUSE_B14,
  25. MUSE_B15,
  26. MUSE_B16,
  27. MUSE_B17,
  28. MUSE_B18,
  29. MUSE_B19,
  30. MUSE_B20,
  31. MUSE_B21,
  32. MUSE_B22,
  33. MUSE_B23,
  34. MUSE_B24,
  35. MUSE_B25,
  36. MUSE_B26,
  37. MUSE_B27,
  38. MUSE_B28,
  39. MUSE_B29,
  40. MUSE_B30,
  41. MUSE_B31
  42. };
  43. bool number_of_ones_to_bool[16] = {
  44. 1, 0, 0, 1, 0, 1, 1, 0,
  45. 0, 1, 1, 0, 1, 0, 0, 1
  46. };
  47. uint8_t muse_interval[4] = {0};
  48. uint8_t muse_theme[4] = {0};
  49. bool timer_1bit = 0;
  50. uint8_t timer_2bit = 0;
  51. uint8_t timer_2bit_counter = 0;
  52. uint8_t timer_4bit = 0;
  53. uint32_t timer_31bit = 0;
  54. bool bit_for_value(uint8_t value) {
  55. switch (value) {
  56. case MUSE_OFF:
  57. return 0;
  58. case MUSE_ON:
  59. return 1;
  60. case MUSE_C_1_2:
  61. return timer_1bit;
  62. case MUSE_C1:
  63. return (timer_4bit & 1);
  64. case MUSE_C2:
  65. return (timer_4bit & 2);
  66. case MUSE_C4:
  67. return (timer_4bit & 4);
  68. case MUSE_C8:
  69. return (timer_4bit & 8);
  70. case MUSE_C3:
  71. return (timer_2bit & 1);
  72. case MUSE_C6:
  73. return (timer_2bit & 2);
  74. default:
  75. return timer_31bit & (1UL << (value - MUSE_B1));
  76. }
  77. }
  78. uint8_t clock_pulse() {
  79. bool top = number_of_ones_to_bool[
  80. bit_for_value(muse_theme[0]) +
  81. bit_for_value(muse_theme[1]) << 1 +
  82. bit_for_value(muse_theme[2]) << 2 +
  83. bit_for_value(muse_theme[3]) << 3
  84. ];
  85. if (timer_1bit == 0) {
  86. if (timer_2bit_counter == 0) {
  87. timer_2bit = (timer_2bit + 1) % 4;
  88. timer_2bit_counter = (timer_2bit_counter + 1) % 3;
  89. }
  90. timer_4bit = (timer_4bit + 1) % 16;
  91. timer_31bit = (timer_31bit << 1) + top;
  92. }
  93. timer_1bit = (timer_1bit + 1) % 2;
  94. return
  95. bit_for_value(muse_interval[0]) +
  96. bit_for_value(muse_interval[1]) << 1 +
  97. bit_for_value(muse_interval[2]) << 2 +
  98. bit_for_value(muse_interval[3]) << 3;
  99. }