Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- //#include "Arduino.h"
- //#include <SoftwareSerial.h>
- bool __PORTLIST[32];
- struct _DEBUG_STRUCT{
- public:
- void error(const char*) {
- if(debug_led_eneable) {
- digitalWrite(debug_led_port, HIGH);
- }
- while(true) {
- delay(1000);
- }
- }
- void eneableLED(int port = 13) {
- pinMode(port, OUTPUT);
- debug_led_port = port;
- debug_led_eneable = true;
- }
- void disableLED() {
- debug_led_eneable = false;
- }
- private:
- int debug_led_port;
- bool debug_led_eneable = false;
- } debug;
- class arduino_object {
- public:
- virtual ~arduino_object() {
- __PORTLIST[port] = false;
- }
- bool changePort(int _port) {
- if(checkPort(_port)) {
- __PORTLIST[port] = false;
- __PORTLIST[_port] = true;
- port = _port;
- pinMode(_port, type);
- return true;
- }
- return false;
- }
- int getPort() {
- return port;
- }
- virtual void operator=(int _port) {
- changePort(_port);
- }
- protected:
- bool setPort(int _port, int _type) {
- if(checkPort(_port)) {
- __PORTLIST[_port] = true;
- port = _port;
- type = _type;
- pinMode(_port, _type);
- return true;
- }
- return false;
- }
- bool checkPort(int _port) {
- if((_port <= 31 && _port >= 0) && !__PORTLIST[_port]) {
- return true;
- }
- return false;
- }
- private:
- int port;
- int type;
- };
- class led : public arduino_object {
- public:
- led(led&) = delete;
- led(int port, bool _analog = false) {
- led(port, 0, _analog);
- }
- led(int port, bool state, bool _analog = false) {
- if(state) {
- led(port, HIGH, _analog);
- }
- else {
- led(port, LOW, _analog);
- }
- }
- led(int _port, int _lux, bool _analog = false)
- : analog {_analog} {
- if(_lux <= HIGH && _lux >= LOW) {
- lux = _lux;
- }
- else {
- debug.error("Expected lux between HIGH(255) and LOW(0)");
- }
- if(!setPort(_port, OUTPUT)) {
- debug.error("Double port");
- }
- doLux();
- }
- void setLux(int _lux) {
- if(_lux <= HIGH && _lux >= LOW) {
- lux = _lux;
- doLux();
- }
- else {
- debug.error("Expected lux between HIGH(255) and LOW(0)");
- }
- }
- int getLux() {
- return lux;
- }
- void setState(bool state) {
- if(state) {
- lux = HIGH;
- }
- else {
- lux = LOW;
- }
- doLux();
- }
- void setState(int state) {
- if(state == HIGH || state == LOW) {
- lux = state;
- doLux();
- }
- else {
- debug.error("Expected HIGH or LOW || x.setLux(...)");
- }
- }
- void operator=(led& l) {
- setLux( l.getLux() );
- }
- void operator=(int lux) {
- setLux(lux);
- }
- private:
- void doLux() {
- digitalWrite(getPort(), lux);
- }
- int lux;
- bool analog;
- };
- class button : public arduino_object {
- public:
- //button(&button) = delete;
- button(int port, bool _pullup = true) {
- if(!setPort(port, (_pullup ? INPUT_PULLUP : INPUT ) )) {
- debug.error("Double port");
- }
- }
- int getState() {
- return digitalRead(getPort());
- }
- bool getBooledState() {
- return getState() == HIGH ? true : false;
- }
- int getAnalogState() {
- return analogRead(getPort());
- } // !! Dont use this for normal Buttons !!
- };
- class motor : public arduino_object {
- public:
- motor() {
- }
- void move(signed int t) {
- if(t > 0) {
- move_up(t);
- }
- else if(t < 0) {
- move_down(-t);
- }
- else {
- move_up(0);
- move_down(0);
- }
- }
- void move_up(unsigned int t) {
- }
- void move_down(unsigned int t) {
- }
- private:
- int port_up;
- int port_down;
- };
- /*
- class easySerial {
- public:
- easySerial(easySerial&) = delete;
- easySerial(int); // serial.begin
- ~easySerial();
- void changeChannel();
- int getChannel();
- void write(char*); // serial.write
- void print(char*);
- easySerial& operator<<(char*);
- easySerial& operator<<(int);
- easySerial& operator<<(char);
- easySerial& operator<<(float);
- easySerial& operator<<(bool);
- easySerial& operator<<(double);
- private:
- int channel;
- };
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement