buttoncontrol.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of eScooterControl project https://github.com/semlanik/eScooterControl
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "buttoncontrol.h"
  26. #include "pinconfig.h"
  27. #include "display.h"
  28. const unsigned long ReactionTime = 700;
  29. const unsigned long LongReactionTime = 500;
  30. /*
  31. * Do not use interruption here because film buttons have false positives in case of bad contact.
  32. * It's better to dispatch them frequently and count positives manually by digitalRead
  33. */
  34. ButtonControl::ButtonControl() : m_triggers(0)
  35. ,m_triggerTime(0)
  36. ,m_previousState(HIGH)
  37. ,m_ledState(LOW)
  38. {
  39. pinMode(LedPin, OUTPUT);
  40. pinMode(PowerPin, OUTPUT);
  41. pinMode(ButtonPin, INPUT_PULLUP);
  42. digitalWrite(PowerPin, HIGH);
  43. }
  44. void ButtonControl::dispatch() {
  45. unsigned long currentTime = millis();
  46. bool state = digitalRead(ButtonPin);
  47. if (m_previousState == HIGH && state == LOW) {
  48. if (m_triggers == 0) {
  49. m_triggerTime = currentTime;
  50. }
  51. ++m_triggers;
  52. }
  53. unsigned long timeDiff = currentTime - m_triggerTime;
  54. m_previousState = state;
  55. if (state == LOW && timeDiff >= LongReactionTime && m_triggers == 1) {
  56. // Serial.println("Power off");
  57. //TODO: Prohibit switch off if speed > 0 || throttle is no at 0
  58. digitalWrite(PowerPin, LOW);
  59. } else if (timeDiff >= ReactionTime && state == HIGH) {
  60. if (m_triggers == 1) {
  61. // Serial.println("Toggle led");
  62. toggleLedState();
  63. } else if (m_triggers == 2) {
  64. // Serial.println("Toggle eco");
  65. //TODO: Enable eco mode
  66. }
  67. //Action finished reset number of triggers
  68. m_triggers = 0;
  69. }
  70. }
  71. void ButtonControl::toggleLedState()
  72. {
  73. if (m_ledState == HIGH) {
  74. m_ledState = LOW;
  75. } else {
  76. m_ledState = HIGH;
  77. }
  78. Display::instance()->setLightOn(m_ledState);
  79. digitalWrite(LedPin, m_ledState);
  80. }