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.

170 lines
5.1KB

  1. #pragma once
  2. #include "GateTrigger.h"
  3. #include "IComposite.h"
  4. static const uint8_t gtable[256] =
  5. {
  6. 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8,
  7. 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16,
  8. 48, 49, 51, 50, 54, 55, 53, 52, 60, 61, 63, 62, 58, 59, 57, 56,
  9. 40, 41, 43, 42, 46, 47, 45, 44, 36, 37, 39, 38, 34, 35, 33, 32,
  10. 96, 97, 99, 98, 102, 103, 101, 100, 108, 109, 111, 110, 106, 107, 105, 104,
  11. 120, 121, 123, 122, 126, 127, 125, 124, 116, 117, 119, 118, 114, 115, 113, 112,
  12. 80, 81, 83, 82, 86, 87, 85, 84, 92, 93, 95, 94, 90, 91, 89, 88,
  13. 72, 73, 75, 74, 78, 79, 77, 76, 68, 69, 71, 70, 66, 67, 65, 64,
  14. 192, 193, 195, 194, 198, 199, 197, 196, 204, 205, 207, 206, 202, 203, 201, 200,
  15. 216, 217, 219, 218, 222, 223, 221, 220, 212, 213, 215, 214, 210, 211, 209, 208,
  16. 240, 241, 243, 242, 246, 247, 245, 244, 252, 253, 255, 254, 250, 251, 249, 248,
  17. 232, 233, 235, 234, 238, 239, 237, 236, 228, 229, 231, 230, 226, 227, 225, 224,
  18. 160, 161, 163, 162, 166, 167, 165, 164, 172, 173, 175, 174, 170, 171, 169, 168,
  19. 184, 185, 187, 186, 190, 191, 189, 188, 180, 181, 183, 182, 178, 179, 177, 176,
  20. 144, 145, 147, 146, 150, 151, 149, 148, 156, 157, 159, 158, 154, 155, 153, 152,
  21. 136, 137, 139, 138, 142, 143, 141, 140, 132, 133, 135, 134, 130, 131, 129, 128
  22. };
  23. static const uint8_t bgtable[256] =
  24. {
  25. 0x00, 0x01, 0x03, 0x02, 0x06, 0x0E, 0x0A, 0x0B,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0xF5, 0xF4, 0x74, 0x75, 0x77, 0x7F, 0xFF, 0xBF, 0x9F, 0xDF,
  37. 0x5F, 0x57, 0x55, 0x54, 0xD4, 0xD5, 0xD7, 0x97, 0x95, 0x94, 0x9C, 0xDC, 0x5C, 0x58, 0xD8, 0x98,
  38. 0x88, 0xC8, 0x48, 0x4C, 0xCC, 0x8C, 0x84, 0xC4, 0x44, 0x45, 0xC5, 0x85, 0x87, 0xC7, 0x47, 0x4F,
  39. 0xCF, 0x8F, 0x8D, 0xCD, 0x4D, 0x49, 0xC9, 0x89, 0x8B, 0xCB, 0x4B, 0x4A, 0xCA, 0x8A, 0x8E, 0xCE,
  40. 0x4E, 0x46, 0xC6, 0x86, 0x82, 0xC2, 0x42, 0x43, 0xC3, 0x83, 0x81, 0xC1, 0x41, 0x40, 0xC0, 0x80
  41. };
  42. template <class TBase>
  43. class GrayDescription : public IComposite
  44. {
  45. public:
  46. Config getParam(int i) override;
  47. int getNumParams() override;
  48. };
  49. template <class TBase>
  50. class Gray : public TBase
  51. {
  52. public:
  53. Gray(struct Module * module) : TBase(module), gateTrigger(true)
  54. {
  55. init();
  56. }
  57. Gray() : TBase(), gateTrigger(true)
  58. {
  59. init();
  60. }
  61. enum ParamIds
  62. {
  63. PARAM_CODE,
  64. NUM_PARAMS
  65. };
  66. enum InputIds
  67. {
  68. INPUT_CLOCK,
  69. NUM_INPUTS
  70. };
  71. enum OutputIds
  72. {
  73. OUTPUT_MIXED,
  74. OUTPUT_0,
  75. OUTPUT_1,
  76. OUTPUT_2,
  77. OUTPUT_3,
  78. OUTPUT_4,
  79. OUTPUT_5,
  80. OUTPUT_6,
  81. OUTPUT_7,
  82. NUM_OUTPUTS
  83. };
  84. enum LightIds
  85. {
  86. LIGHT_0,
  87. LIGHT_1,
  88. LIGHT_2,
  89. LIGHT_3,
  90. LIGHT_4,
  91. LIGHT_5,
  92. LIGHT_6,
  93. LIGHT_7,
  94. NUM_LIGHTS
  95. };
  96. /** Implement IComposite
  97. */
  98. static std::shared_ptr<IComposite> getDescription()
  99. {
  100. return std::make_shared<GrayDescription<TBase>>();
  101. }
  102. /**
  103. * Main processing entry point. Called every sample
  104. */
  105. void step() override;
  106. private:
  107. uint8_t counterValue = 0;
  108. GateTrigger gateTrigger;
  109. int c = 0;
  110. void init();
  111. };
  112. template <class TBase>
  113. void Gray<TBase>::init()
  114. {
  115. // Init all the outputs to zero,
  116. // since they don't all get update until a clock.
  117. for (int i = 0; i < NUM_OUTPUTS; ++i) {
  118. TBase::outputs[i].value = 0;
  119. }
  120. }
  121. template <class TBase>
  122. void Gray<TBase>::step()
  123. {
  124. gateTrigger.go(TBase::inputs[INPUT_CLOCK].value);
  125. if (!gateTrigger.trigger()) {
  126. return;
  127. }
  128. ++counterValue;
  129. const uint8_t* table = TBase::params[PARAM_CODE].value > .5 ? gtable : bgtable;
  130. const auto g0 = table[counterValue];
  131. auto g = g0;
  132. for (int i = 0; i < 8; ++i) {
  133. bool b = g & 1;
  134. TBase::lights[i + LIGHT_0].value = b ? 10.f : 0.f;
  135. TBase::outputs[i + OUTPUT_0].value = b ? 10.f : 0.f;
  136. g >>= 1;
  137. }
  138. TBase::outputs[OUTPUT_MIXED].value = (float) g0 / 25.f;
  139. }
  140. template <class TBase>
  141. int GrayDescription<TBase>::getNumParams()
  142. {
  143. return Gray<TBase>::NUM_PARAMS;
  144. }
  145. template <class TBase>
  146. IComposite::Config GrayDescription<TBase>::getParam(int i)
  147. {
  148. assert(i == Gray<TBase>::PARAM_CODE);
  149. return {0.0f, 1.0f, 0.0f, "Code type"};
  150. }