https://github.com/ripred/smartpin
Experimenting with the idea of an object-oriented pin class that uses operator overloading to intuitively abbreviate the usage of digitalRead(...), digitalWrite(...), analogRead(...) and analogWrite(...)
https://github.com/ripred/smartpin
arduino arduino-library embedded hardware intuitive object-oriented-programming operator-overloading
Last synced: 7 months ago
JSON representation
Experimenting with the idea of an object-oriented pin class that uses operator overloading to intuitively abbreviate the usage of digitalRead(...), digitalWrite(...), analogRead(...) and analogWrite(...)
- Host: GitHub
- URL: https://github.com/ripred/smartpin
- Owner: ripred
- License: mit
- Created: 2024-05-02T08:18:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-02T11:22:09.000Z (over 1 year ago)
- Last Synced: 2024-05-02T21:48:20.664Z (over 1 year ago)
- Topics: arduino, arduino-library, embedded, hardware, intuitive, object-oriented-programming, operator-overloading
- Language: C++
- Homepage: https://github.com/ripred/SmartPin
- Size: 60.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/marketplace/actions/arduino_ci)
[](https://github.com/ripred/SmartPin/actions/workflows/arduino-lint.yml)

[](https://github.com/ripred/SmartPin/releases)
[](https://github.com/ripred/SmartPin/blob/master/LICENSE)
[](https://github.com/ripred/SmartPin)
[](https://github.com/ripred/SmartPin)
# Arduino SmartPin Library
### Example
```cpp
/*
* SmartPin.ino
*
* Experimenting with the idea of an object-oriented pin class
* that uses operator overloading to intuitively abbreviate the
* usage of digitalRead(...), digitalWrite(...), analogRead(...)
* and analogWrite(...)
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* example 1
*
* SmartPin button(2, INPUT_PULLUP);
* SmartPin led(3, OUTPUT);
*
* void loop() {
* led = !button;
* ...
* }
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
* example 2
*
* SmartPin potentiometer(A0, INPUT, analogWrite, analogRead);
* SmartPin led(3, OUTPUT, analogWrite);
*
* void loop() {
* led = potentiometer / 4;
* ...
* }
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
*/
#include
enum MagicNumbers {
// project-specific pin usage; Change as needed
BUTTON_PIN = 2, // a digital input pin wth a push button
POT_PIN = A0, // an analog input pin with a potentiometer
LED1_PIN = 3, // a digital output pin to follow the button
LED2_PIN = 5, // a pwm output pin to follow the potentiometer value
}; // enum MagicNumbers
// a push button that drives an LED
SmartPin button_pin(BUTTON_PIN, INPUT_PULLUP);
SmartPin led1_pin(LED1_PIN, OUTPUT);
// a potentiometer that drives the PWM brightness of an LED
SmartPin pot_pin(POT_PIN, INPUT, analogWrite, analogRead);
SmartPin led2_pin(LED2_PIN, OUTPUT, analogWrite);
void setup()
{
// example LED fade in/out using simple integer assignment
for (int i=0; i < 4; i++) {
for (int pwm=0; pwm < 256; pwm += 4) {
led2_pin = pwm;
delay(4);
}
for (int pwm=255; pwm >= 0; pwm -= 4) {
led2_pin = pwm;
delay(4);
}
}
}
void loop()
{
// using the pins is ridiculously easy 😎:
led1_pin = !button_pin; // we invert the HIGH/LOW value since the button is active-low
led2_pin = pot_pin / 4; // set the led brightness relative to the potentiometer value
}
```