您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

3 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "buttons.h"
  2. #include "lcd.h"
  3. #include "scale.h"
  4. void Buttons::init() {
  5. pinMode(tare_button, INPUT);
  6. pinMode(controller_button, INPUT);
  7. pinMode(plus_button, INPUT);
  8. pinMode(minus_button, INPUT);
  9. }
  10. bool Buttons::is_any_pressed() {
  11. return digitalRead(tare_button) || digitalRead(controller_button);
  12. }
  13. void Buttons::on_button_pressed(Scale& scale, WeightControllerPool& pool, const LCD& lcd) {
  14. Action buttonAction = get_button_action();
  15. switch (buttonAction) {
  16. case Action::TAR:
  17. scale.tare();
  18. break;
  19. case Action::CALIBRATE:
  20. scale.calibrate();
  21. break;
  22. case Action::NEXT_CONTROLLER:
  23. lcd.clear();
  24. pool.next();
  25. break;
  26. default:
  27. break;
  28. }
  29. }
  30. Buttons::Action Buttons::get_button_action() {
  31. int button_pressed = 0;
  32. for (int i = 0; i < buttons_count; i++) {
  33. button_pressed |= digitalRead(buttons[i]) < i;
  34. }
  35. if (last_pressed == button_pressed) {
  36. }
  37. Buttons::Action action;
  38. if (button_pressed & 0x0001) {
  39. action = Action::TAR;
  40. }
  41. else if (tare_pressed && !controller_pressed) {
  42. action = Action::NEXT_CONTROLLER;
  43. }
  44. else {
  45. action = Action::NONE;
  46. }
  47. if (action == Action::TAR && (now - then) > 2000) {
  48. action = Action::CALIBRATE;
  49. }
  50. return action;
  51. }