Code Monkey home page Code Monkey logo

ripred / smartpin Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 62 KB

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(...)

Home Page: https://github.com/ripred/SmartPin

License: MIT License

C 40.87% C++ 59.13%
arduino arduino-library embedded hardware intuitive object-oriented-programming operator-overloading

smartpin's Introduction

Arduino CI Arduino-lint code size: GitHub release License: MIT Stars Forks

Arduino SmartPin Library

Example

/*
 * 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 <SmartPin.h>

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
}

smartpin's People

Contributors

ripred avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.