|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include "buttons.h"
- #include "lcd.h"
- #include "scale.h"
-
- void Buttons::init() {
- pinMode(tare_button, INPUT);
- pinMode(controller_button, INPUT);
- pinMode(plus_button, INPUT);
- pinMode(minus_button, INPUT);
- }
-
- bool Buttons::is_any_pressed() {
- return digitalRead(tare_button) || digitalRead(controller_button);
- }
-
- void Buttons::on_button_pressed(Scale& scale, WeightControllerPool& pool, const LCD& lcd) {
- Action buttonAction = get_button_action();
- switch (buttonAction) {
- case Action::TAR:
- scale.tare();
- break;
- case Action::CALIBRATE:
- scale.calibrate();
- break;
- case Action::NEXT_CONTROLLER:
- lcd.clear();
- pool.next();
- break;
- default:
- break;
- }
- }
-
- Buttons::Action Buttons::get_button_action() {
- int button_pressed = 0;
- for (int i = 0; i < buttons_count; i++) {
- button_pressed |= digitalRead(buttons[i]) < i;
- }
-
- if (last_pressed == button_pressed) {
-
- }
-
- Buttons::Action action;
- if (button_pressed & 0x0001) {
- action = Action::TAR;
- }
- else if (tare_pressed && !controller_pressed) {
- action = Action::NEXT_CONTROLLER;
- }
- else {
- action = Action::NONE;
- }
-
- if (action == Action::TAR && (now - then) > 2000) {
- action = Action::CALIBRATE;
- }
-
- return action;
- }
|