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.

143 lines
3.0KB

  1. #include "DS.hpp"
  2. float DS_Module::midpoint() {
  3. return (voltage0 * 0.5f + voltage1 * 0.5f);
  4. }
  5. json_t *DS_Module::toJson() {
  6. json_t *rootJ = json_object();
  7. json_object_set_new(rootJ, "voltage0", json_real(voltage0));
  8. json_object_set_new(rootJ, "voltage1", json_real(voltage1));
  9. return rootJ;
  10. }
  11. void DS_Module::fromJson(json_t *rootJ) {
  12. json_t *j0 = json_object_get(rootJ, "voltage0");
  13. if (j0)
  14. voltage0 = json_number_value(j0);
  15. json_t *j1 = json_object_get(rootJ, "voltage1");
  16. if (j1)
  17. voltage1 = json_number_value(j1);
  18. }
  19. void DS_Module::onReset() {
  20. voltage0 = 0.0f;
  21. voltage1 = 5.0f;
  22. }
  23. float DS_Module::output(int state) {
  24. return state?voltage1:voltage0;
  25. }
  26. void DS_Module::appendContextMenu(Menu *menu) {
  27. menu->addChild(MenuEntry::create());
  28. DS_MenuItem *m = MenuItem::create<DS_MenuItem>("Range 0V - 1V");
  29. m->module = this;
  30. m->vl = 0.0f;
  31. m->vh = 1.0f;
  32. menu->addChild(m);
  33. m = MenuItem::create<DS_MenuItem>("Range 0V - 5V");
  34. m->module = this;
  35. m->vl = 0.0f;
  36. m->vh = 5.0f;
  37. menu->addChild(m);
  38. m = MenuItem::create<DS_MenuItem>("Range 0V - 10V");
  39. m->module = this;
  40. m->vl = 0.0f;
  41. m->vh = 10.0f;
  42. menu->addChild(m);
  43. m = MenuItem::create<DS_MenuItem>("Range -5V - 5V");
  44. m->module = this;
  45. m->vl = -5.0f;
  46. m->vh = 5.0f;
  47. menu->addChild(m);
  48. m = MenuItem::create<DS_MenuItem>("Range -10V - 10V");
  49. m->module = this;
  50. m->vl = -10.0f;
  51. m->vh = 10.0f;
  52. menu->addChild(m);
  53. }
  54. void DS_MenuItem::onAction(EventAction &e) {
  55. module->voltage0 = vl;
  56. module->voltage1 = vh;
  57. }
  58. void DS_MenuItem::step() {
  59. rightText = CHECKMARK((module->voltage0 == vl) && (module->voltage1 == vh));
  60. }
  61. float DS_Schmitt::high(float v0, float v1) {
  62. return (v0 * 0.4f + v1 * 0.6f);
  63. }
  64. float DS_Schmitt::low(float v0, float v1) {
  65. return (v0 * 0.6f + v1 * 0.4f);
  66. }
  67. void DS_Schmitt::reset() {
  68. _state = 0;
  69. }
  70. void DS_Schmitt::set() {
  71. _state = 1;
  72. }
  73. void DS_Schmitt::set(int state) {
  74. _state = state;
  75. }
  76. int DS_Schmitt::state(float vl, float vh, float v) {
  77. if (_state) {
  78. if (v < vl)
  79. _state = 0;
  80. }
  81. else {
  82. if (v > vh)
  83. _state = 1;
  84. }
  85. return _state;
  86. }
  87. int DS_Schmitt::state(DS_Module *module, float v) {
  88. return state(low(module->voltage0, module->voltage1), high(module->voltage0, module->voltage1), v);
  89. }
  90. int DS_Schmitt::edge(float vl, float vh, float v) {
  91. int old = _state;
  92. return (state(vl, vh, v) != old);
  93. }
  94. int DS_Schmitt::edge(DS_Module *module, float v) {
  95. int old = _state;
  96. return (state(module, v) != old);
  97. }
  98. int DS_Schmitt::edge(float vl, float vh, float v, int falling) {
  99. return falling?fedge(vl, vh, v):redge(vl, vh, v);
  100. }
  101. int DS_Schmitt::edge(DS_Module *module, float v, int falling) {
  102. return falling?fedge(module, v):redge(module, v);
  103. }
  104. int DS_Schmitt::redge(float vl, float vh, float v) {
  105. int old = _state;
  106. return (state(vl, vh, v) && !old);
  107. }
  108. int DS_Schmitt::redge(DS_Module *module, float v) {
  109. int old = _state;
  110. return (state(module, v) && !old);
  111. }
  112. int DS_Schmitt::fedge(float vl, float vh, float v) {
  113. int old = _state;
  114. return (!state(vl, vh, v) && old);
  115. }
  116. int DS_Schmitt::fedge(DS_Module *module, float v) {
  117. int old = _state;
  118. return (!state(module, v) && old);
  119. }