muse.c 2.2 KB

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