Advertisement
Phins

Arduino lib A_0.1

Apr 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #pragma once
  2.  
  3. //#include "Arduino.h"
  4. //#include <SoftwareSerial.h>
  5.  
  6. bool __PORTLIST[32];
  7.  
  8. struct _DEBUG_STRUCT{
  9. public:
  10.     void error(const char*) {
  11.         if(debug_led_eneable) {
  12.             digitalWrite(debug_led_port, HIGH);
  13.         }
  14.        
  15.         while(true) {
  16.             delay(1000);
  17.         }
  18.     }
  19.    
  20.     void eneableLED(int port = 13) {
  21.         pinMode(port, OUTPUT);
  22.         debug_led_port = port;
  23.         debug_led_eneable = true;
  24.     }
  25.    
  26.     void disableLED() {
  27.         debug_led_eneable = false;
  28.     }
  29. private:
  30.     int debug_led_port;
  31.     bool debug_led_eneable = false;
  32. } debug;
  33.  
  34. class arduino_object {
  35. public:
  36.     virtual ~arduino_object() {
  37.         __PORTLIST[port] = false;
  38.     }
  39.  
  40.     bool changePort(int _port) {
  41.         if(checkPort(_port)) {
  42.             __PORTLIST[port] = false;
  43.             __PORTLIST[_port] = true;
  44.             port = _port;
  45.             pinMode(_port, type);
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.    
  51.     int getPort() {
  52.         return port;
  53.     }
  54.    
  55.     virtual void operator=(int _port) {
  56.         changePort(_port);
  57.     }
  58. protected:
  59.     bool setPort(int _port, int _type) {
  60.         if(checkPort(_port)) {
  61.             __PORTLIST[_port] = true;
  62.             port = _port;
  63.             type = _type;
  64.             pinMode(_port, _type);
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69.    
  70.     bool checkPort(int _port) {
  71.         if((_port <= 31 && _port >= 0) && !__PORTLIST[_port]) {
  72.             return true;
  73.         }
  74.         return false;
  75.     }
  76. private:
  77.     int port;
  78.     int type;
  79. };
  80.  
  81. class led : public arduino_object {
  82. public:
  83.     led(led&) = delete;
  84.     led(int port, bool _analog = false) {
  85.         led(port, 0, _analog);
  86.     }
  87.    
  88.     led(int port, bool state, bool _analog = false) {
  89.         if(state) {
  90.             led(port, HIGH, _analog);
  91.         }
  92.         else {
  93.             led(port, LOW, _analog);
  94.         }
  95.     }
  96.    
  97.     led(int _port, int _lux, bool _analog = false)
  98.         : analog {_analog} {
  99.         if(_lux <= HIGH && _lux >= LOW) {
  100.             lux = _lux;
  101.         }
  102.         else {
  103.             debug.error("Expected lux between HIGH(255) and LOW(0)");
  104.         }
  105.         if(!setPort(_port, OUTPUT)) {
  106.             debug.error("Double port");
  107.         }
  108.         doLux();
  109.     }
  110.    
  111.     void setLux(int _lux) {
  112.         if(_lux <= HIGH && _lux >= LOW) {
  113.             lux = _lux;
  114.             doLux();
  115.         }
  116.         else {
  117.             debug.error("Expected lux between HIGH(255) and LOW(0)");
  118.         }
  119.     }
  120.    
  121.     int getLux() {
  122.         return lux;
  123.     }
  124.    
  125.     void setState(bool state) {
  126.         if(state) {
  127.             lux = HIGH;
  128.         }
  129.         else {
  130.             lux = LOW;
  131.         }
  132.         doLux();
  133.     }
  134.    
  135.     void setState(int state) {
  136.         if(state == HIGH || state == LOW) {
  137.             lux = state;
  138.             doLux();
  139.         }
  140.         else {
  141.             debug.error("Expected HIGH or LOW || x.setLux(...)");
  142.         }
  143.     }
  144.    
  145.     void operator=(led& l) {
  146.         setLux( l.getLux() );
  147.     }
  148.     void operator=(int lux) {
  149.         setLux(lux);
  150.     }
  151. private:
  152.     void doLux() {
  153.         digitalWrite(getPort(), lux);
  154.     }
  155.     int lux;
  156.     bool analog;
  157. };
  158.  
  159. class button : public arduino_object {
  160. public:
  161.     //button(&button) = delete;
  162.     button(int port, bool _pullup = true) {
  163.         if(!setPort(port, (_pullup ? INPUT_PULLUP : INPUT ) )) {
  164.             debug.error("Double port");
  165.         }
  166.     }
  167.    
  168.     int getState() {
  169.         return digitalRead(getPort());
  170.     }
  171.    
  172.     bool getBooledState() {
  173.         return getState() == HIGH ? true : false;
  174.     }
  175.    
  176.     int getAnalogState() {
  177.         return analogRead(getPort());
  178.     } // !! Dont use this for normal Buttons !!
  179. };
  180.  
  181. class motor : public arduino_object {
  182. public:
  183.     motor() {
  184.        
  185.     }
  186.    
  187.     void move(signed int t) {
  188.         if(t > 0) {
  189.             move_up(t);
  190.         }
  191.         else if(t < 0) {
  192.             move_down(-t);
  193.         }
  194.         else {
  195.             move_up(0);
  196.             move_down(0);
  197.         }
  198.     }
  199.    
  200.     void move_up(unsigned int t) {
  201.        
  202.     }
  203.    
  204.     void move_down(unsigned int t) {
  205.        
  206.     }
  207. private:
  208.    
  209.     int port_up;
  210.     int port_down;
  211. };
  212.  
  213.  
  214. /*
  215. class easySerial {
  216. public:
  217.     easySerial(easySerial&) = delete;
  218.     easySerial(int);  // serial.begin
  219.     ~easySerial();
  220.    
  221.     void changeChannel();
  222.     int getChannel();
  223.    
  224.     void write(char*); // serial.write
  225.     void print(char*);
  226.    
  227.     easySerial& operator<<(char*);
  228.     easySerial& operator<<(int);
  229.     easySerial& operator<<(char);
  230.     easySerial& operator<<(float);
  231.     easySerial& operator<<(bool);
  232.     easySerial& operator<<(double);
  233. private:
  234.     int channel;
  235. };
  236.  
  237. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement