Controls the central heating system and the lights.
Dependencies: 1-wire clock crypto fram log lpc1768 net web wiz mbed
heating/hall-pb.c@0:22b158d3c76f, 2021-05-10 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon May 10 10:23:48 2021 +0000
- Revision:
- 0:22b158d3c76f
- Child:
- 8:8ac076ce51af
New version as old one would not commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:22b158d3c76f | 1 | #include <stdbool.h> |
andrewboyson | 0:22b158d3c76f | 2 | #include <stdint.h> |
andrewboyson | 0:22b158d3c76f | 3 | |
andrewboyson | 0:22b158d3c76f | 4 | #include "gpio.h" |
andrewboyson | 0:22b158d3c76f | 5 | #include "radiator.h" |
andrewboyson | 0:22b158d3c76f | 6 | #include "program.h" |
andrewboyson | 0:22b158d3c76f | 7 | #include "mstimer.h" |
andrewboyson | 0:22b158d3c76f | 8 | #include "rit.h" |
andrewboyson | 0:22b158d3c76f | 9 | |
andrewboyson | 0:22b158d3c76f | 10 | #define HALL_PB_PIN FIO0PIN(05) // P0.05 == p29; |
andrewboyson | 0:22b158d3c76f | 11 | |
andrewboyson | 0:22b158d3c76f | 12 | #define DEBOUNCE_MS 20 |
andrewboyson | 0:22b158d3c76f | 13 | #define LONG_PRESS_MS 3000 |
andrewboyson | 0:22b158d3c76f | 14 | #define INACTIVE_REVERT_MS 30000 |
andrewboyson | 0:22b158d3c76f | 15 | |
andrewboyson | 0:22b158d3c76f | 16 | static int ms = 0; |
andrewboyson | 0:22b158d3c76f | 17 | |
andrewboyson | 0:22b158d3c76f | 18 | static void (*ritHook)(); |
andrewboyson | 0:22b158d3c76f | 19 | |
andrewboyson | 0:22b158d3c76f | 20 | static void ritHandler() |
andrewboyson | 0:22b158d3c76f | 21 | { |
andrewboyson | 0:22b158d3c76f | 22 | if (ritHook) ritHook(); //Call the RIT function chain before this |
andrewboyson | 0:22b158d3c76f | 23 | |
andrewboyson | 0:22b158d3c76f | 24 | if (HALL_PB_PIN) { if (ms < DEBOUNCE_MS) ms++; } |
andrewboyson | 0:22b158d3c76f | 25 | else { if (ms > 0 ) ms--; } |
andrewboyson | 0:22b158d3c76f | 26 | } |
andrewboyson | 0:22b158d3c76f | 27 | |
andrewboyson | 0:22b158d3c76f | 28 | static bool getHallPbPressed() |
andrewboyson | 0:22b158d3c76f | 29 | { |
andrewboyson | 0:22b158d3c76f | 30 | static bool pressed = false; |
andrewboyson | 0:22b158d3c76f | 31 | if (ms >= DEBOUNCE_MS) pressed = false; |
andrewboyson | 0:22b158d3c76f | 32 | if (ms <= 0) pressed = true; |
andrewboyson | 0:22b158d3c76f | 33 | return pressed; |
andrewboyson | 0:22b158d3c76f | 34 | } |
andrewboyson | 0:22b158d3c76f | 35 | |
andrewboyson | 0:22b158d3c76f | 36 | bool HallPbOverrideMode = true; |
andrewboyson | 0:22b158d3c76f | 37 | |
andrewboyson | 0:22b158d3c76f | 38 | void HallPbInit() |
andrewboyson | 0:22b158d3c76f | 39 | { |
andrewboyson | 0:22b158d3c76f | 40 | ritHook = RitHook; |
andrewboyson | 0:22b158d3c76f | 41 | RitHook = ritHandler; |
andrewboyson | 0:22b158d3c76f | 42 | } |
andrewboyson | 0:22b158d3c76f | 43 | void HallPbMain() |
andrewboyson | 0:22b158d3c76f | 44 | { |
andrewboyson | 0:22b158d3c76f | 45 | static uint32_t hallButtonPushedMsTimer = 0; |
andrewboyson | 0:22b158d3c76f | 46 | static uint32_t hallButtonNotPushedMsTimer = 0; |
andrewboyson | 0:22b158d3c76f | 47 | |
andrewboyson | 0:22b158d3c76f | 48 | static bool buttonWasPressed = false; |
andrewboyson | 0:22b158d3c76f | 49 | static bool buttonWasLongPressed = false; |
andrewboyson | 0:22b158d3c76f | 50 | |
andrewboyson | 0:22b158d3c76f | 51 | bool buttonIsPressed = getHallPbPressed(); |
andrewboyson | 0:22b158d3c76f | 52 | bool buttonIsLongPressed; |
andrewboyson | 0:22b158d3c76f | 53 | if (buttonIsPressed) |
andrewboyson | 0:22b158d3c76f | 54 | { |
andrewboyson | 0:22b158d3c76f | 55 | buttonIsLongPressed = MsTimerRelative(hallButtonPushedMsTimer, LONG_PRESS_MS); |
andrewboyson | 0:22b158d3c76f | 56 | hallButtonNotPushedMsTimer = MsTimerCount; |
andrewboyson | 0:22b158d3c76f | 57 | } |
andrewboyson | 0:22b158d3c76f | 58 | else |
andrewboyson | 0:22b158d3c76f | 59 | { |
andrewboyson | 0:22b158d3c76f | 60 | buttonIsLongPressed = false; |
andrewboyson | 0:22b158d3c76f | 61 | hallButtonPushedMsTimer = MsTimerCount; |
andrewboyson | 0:22b158d3c76f | 62 | } |
andrewboyson | 0:22b158d3c76f | 63 | |
andrewboyson | 0:22b158d3c76f | 64 | if (buttonIsLongPressed && !buttonWasLongPressed) HallPbOverrideMode = !HallPbOverrideMode; |
andrewboyson | 0:22b158d3c76f | 65 | if (MsTimerRelative(hallButtonNotPushedMsTimer, INACTIVE_REVERT_MS)) HallPbOverrideMode = true; |
andrewboyson | 0:22b158d3c76f | 66 | |
andrewboyson | 0:22b158d3c76f | 67 | if (!buttonIsPressed && buttonWasPressed && !buttonWasLongPressed) |
andrewboyson | 0:22b158d3c76f | 68 | { |
andrewboyson | 0:22b158d3c76f | 69 | if (HallPbOverrideMode) RadiatorChgOverride(); |
andrewboyson | 0:22b158d3c76f | 70 | else RadiatorChgWinter(); |
andrewboyson | 0:22b158d3c76f | 71 | } |
andrewboyson | 0:22b158d3c76f | 72 | buttonWasPressed = buttonIsPressed; |
andrewboyson | 0:22b158d3c76f | 73 | buttonWasLongPressed = buttonIsLongPressed; |
andrewboyson | 0:22b158d3c76f | 74 | } |