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.

105 lines
2.4 KiB

  1. /*
  2. MicroMDAEPiano
  3. MicroMDAEPiano is a port of the MDA-EPiano sound engine
  4. (https://sourceforge.net/projects/mda-vst/) for the Teensy-3.5/3.6 with audio shield.
  5. (c)2019 H. Wirtz <wirtz@parasitstudio.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. //#define ENCODER_DO_NOT_USE_INTERRUPTS
  19. #include <Encoder.h>
  20. #include "limits.h"
  21. #ifndef ENCODER4_H_INCLUDED
  22. #define ENCODER4_H_INCLUDED
  23. class Encoder4 : public Encoder
  24. {
  25. public:
  26. //using Encoder::Encoder;
  27. Encoder4(uint8_t pin1, uint8_t pin2) : Encoder::Encoder(pin1, pin2)
  28. {
  29. _min = INT_MIN / 4;
  30. _max = INT_MAX / 4;
  31. _wrap_around = false;
  32. }
  33. int32_t read()
  34. {
  35. int32_t p = Encoder::read();
  36. if (p / 4 < _min && _wrap_around == false)
  37. Encoder::write(_min * 4);
  38. else if (p / 4 < _min && _wrap_around == true)
  39. Encoder::write(_max * 4);
  40. else if (p / 4 > _max && _wrap_around == false)
  41. Encoder::write(_max * 4);
  42. else if (p / 4 > _max && _wrap_around == true)
  43. Encoder::write(_min * 4);
  44. return (Encoder::read() / 4);
  45. }
  46. void write(int32_t p)
  47. {
  48. Encoder::write(p * 4);
  49. }
  50. void write(int32_t p, int32_t min, int32_t max, bool wrap_around = false)
  51. {
  52. _wrap_around = wrap_around;
  53. if (max < min)
  54. {
  55. _min = max;
  56. _max = min;
  57. }
  58. else
  59. {
  60. _min = min;
  61. _max = max;
  62. }
  63. if (p > _max)
  64. p = _max;
  65. else if (p < _min)
  66. p = _min;
  67. Encoder::write(p * 4);
  68. }
  69. int32_t read_min(void)
  70. {
  71. return (_min);
  72. }
  73. int32_t read_max(void)
  74. {
  75. return (_max);
  76. }
  77. private:
  78. int32_t _min;
  79. int32_t _max;
  80. bool _wrap_around;
  81. };
  82. #endif