|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Alexander Serebryakov |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +// This example demonstrates how to work with Button object. |
| 24 | +// |
| 25 | +// Hardware: |
| 26 | +// - Arduino |
| 27 | +// - Button |
| 28 | +// |
| 29 | +// Setup: |
| 30 | +// - Button is connected to D2 |
| 31 | + |
| 32 | +#include <Arduino.h> |
| 33 | +#include <CppComponentsLibrary.h> |
| 34 | + |
| 35 | +// Instantiate a hardware api object. |
| 36 | +HwApiImpl hw_api{}; |
| 37 | + |
| 38 | +constexpr int BUTTON_PIN{2}; |
| 39 | + |
| 40 | +// Class is used to implement callbacks and store the internal state. |
| 41 | +// |
| 42 | +// Inheritance from Device class isn't necessary, but it provides default functions |
| 43 | +// to be implemented to have a consistent API |
| 44 | +class ButtonLogic : public Device { |
| 45 | +public: |
| 46 | + ButtonLogic() : button{ |
| 47 | + {BUTTON_PIN, true}, // Setting DT pin to PULL_UP mode |
| 48 | + hw_api, // Providing to button instance of HwApi to be used to setup pin and read pin value |
| 49 | + {onPushButton, this}, // Connecting button push event to callback function and this object |
| 50 | + {onReleaseButton, this}, // Connecting button release event to callback function and this object |
| 51 | + } |
| 52 | + { |
| 53 | + } |
| 54 | + |
| 55 | + virtual ~ButtonLogic() override = default; |
| 56 | + |
| 57 | + // Function to be called from setup() |
| 58 | + virtual void begin() override { |
| 59 | + button.begin(); // Initializes pin |
| 60 | + } |
| 61 | + |
| 62 | + // Function to be called from loop() |
| 63 | + virtual void loop() override { |
| 64 | + button.loop(); // Reads pin value and calls callback funcitons |
| 65 | + } |
| 66 | + |
| 67 | + // Function that is called when button is pushed |
| 68 | + static void onPushButton(void* self) { |
| 69 | + // Callback function is unaware of the pointer type so we cast it to current object type. |
| 70 | + auto& logic{*static_cast<ButtonLogic*>(self)}; |
| 71 | + logic.counter++; |
| 72 | + Serial.print("Button pushed : "); |
| 73 | + Serial.println(logic.counter); |
| 74 | + } |
| 75 | + |
| 76 | + // Function that is called when when button is released |
| 77 | + static void onReleaseButton(void* self) { |
| 78 | + auto& logic{*static_cast<ButtonLogic*>(self)}; |
| 79 | + logic.counter--; |
| 80 | + Serial.print("Button released : "); |
| 81 | + Serial.println(logic.counter); |
| 82 | + } |
| 83 | + |
| 84 | +private: |
| 85 | + Button button; |
| 86 | + int counter{0}; |
| 87 | +}; |
| 88 | + |
| 89 | +// Instantiating the object |
| 90 | +ButtonLogic logic{}; |
| 91 | + |
| 92 | +void setup() { |
| 93 | + Serial.begin(57600); |
| 94 | + logic.begin(); |
| 95 | +} |
| 96 | + |
| 97 | +void loop() { |
| 98 | + logic.loop(); |
| 99 | + delay(10); |
| 100 | +} |
0 commit comments