diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/MobaBus_Servo.cpp b/MobaBus_Servo.cpp new file mode 100644 index 0000000..35c441f --- /dev/null +++ b/MobaBus_Servo.cpp @@ -0,0 +1,109 @@ +/** + MobaBus Servo + + © 2021, Markus Mair. All rights reserved. + + This file is part of the MobaBus Project + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "MobaBus_Servo.h" + +MobaBus_Servo::MobaBus_Servo(uint8_t pin, uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff){ + this->pin = pin; + this->autoPowerOff = autoPowerOff; + angles[0] = angle0; + angles[1] = angle1; + moveSpeed = (uint8_t)255 - speed; + type = ACCESSORIE; +} + +uint8_t MobaBus_Servo::begin(bool useEEPROM, uint16_t address){ + Serial.println("Module begin"); + Serial.println("Drive Servo at 90 degrees"); + + programmAddress(address); + + servo.attach(pin); + servo.write(90); + actualAngle = 90; + if(autoPowerOff){ + delay(SERVO_POWER_OFF); + servo.detach(); + } + + intitialized = true; + return 1; +} + +void MobaBus_Servo::loop(){ + uint32_t actualTime = millis(); + if(actualTime >= lastMove + moveSpeed){ + lastMove = actualTime; + if(actualAngle != targetAngle){ + if(!active){ + servo.attach(pin); + active = true; + } + actualAngle += targetAngle > actualAngle ? 1 : -1; + servo.write(actualAngle); + powerOffT = actualTime + SERVO_POWER_OFF; + } + else if(autoPowerOff && active && powerOffT <= actualTime){ + active = false; + servo.detach(); + } + } +} + +void MobaBus_Servo::loadConf() { + return; +} + +void MobaBus_Servo::storeConf() { + return; +} + +void MobaBus_Servo::processPkg(MobaBus_Packet *pkg){ + if(pkg->meta.type == type && pkg->meta.address == address){ + if(pkg->meta.cmd == SET){ + setTurnout((bool)pkg->data[0], (bool)pkg->data[1]); + } + else if(pkg->meta.cmd == GET){ + uint8_t data[] = {(actualAngle == angles[1])}; + controller->sendPkg(ACCESSORIE, address, INFO, 1, data); + } + } +} + +uint8_t MobaBus_Servo::programmAddress(uint16_t addr){ + address = addr; + Serial.print("Set Address "); + Serial.println(address); + return 1; +} + +void MobaBus_Servo::setTurnout(bool dir, bool power){ + if(power){ + targetAngle = angles[dir]; + uint8_t data[] = {dir}; + controller->sendPkg(ACCESSORIE, address + pin, INFO, 1, data); + } + else{ + targetAngle = actualAngle; + servo.detach(); + active = false; + } +} \ No newline at end of file diff --git a/modules/MobaBus_Sensor.h b/MobaBus_Servo.h similarity index 62% rename from modules/MobaBus_Sensor.h rename to MobaBus_Servo.h index 7166f97..bb6527a 100644 --- a/modules/MobaBus_Sensor.h +++ b/MobaBus_Servo.h @@ -1,5 +1,5 @@ /** - MobaBus Sensor + MobaBus Servo © 2021, Markus Mair. All rights reserved. @@ -19,30 +19,38 @@ along with this program. If not, see . */ -#ifndef __MOBABUS_SENSOR__ -#define __MOBABUS_SENSOR__ +#ifndef __MOBABUS_SERVO__ +#define __MOBABUS_SERVO__ -#include "MobaBus_Module.h" #include "MobaBus.h" +#include "MobaBus_Module.h" + +#include -class MobaBus_Sensor : public MobaBus_Module{ +#define SERVO_POWER_OFF 100 // ms after last step to power off the servo if autoPowerOff = true + +class MobaBus_Servo : public MobaBus_Module{ private: - uint8_t pin[8]; - bool pullUp; + Servo servo; + + uint8_t pin; - bool state[8]; + uint8_t angles[2]; + uint8_t actualAngle; + uint8_t targetAngle; + bool active; - uint32_t debounce[8]; + uint32_t lastMove; + uint8_t moveSpeed; + + bool autoPowerOff; + uint32_t powerOffT; - void sendStates(); - public: /** - * Constructor to use Arduino pins - * @param pinX number, 0 if not used! - * @param pullup if true pinMode is INPUT_PULLUP else INPUT + * Constructor for Servo */ - MobaBus_Sensor(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6, uint8_t pin7, uint8_t pin8, bool pullup); + MobaBus_Servo(uint8_t pin, uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff); uint8_t begin(bool useEEPROM, uint16_t address); void loop(); @@ -50,8 +58,12 @@ class MobaBus_Sensor : public MobaBus_Module{ void loadConf(); void storeConf(); + void processPkg(MobaBus_Packet *pkg); - + uint8_t programmAddress(uint16_t addr); + + void setTurnout(bool dir, bool power); }; + #endif \ No newline at end of file diff --git a/modules/MobaBus_TurnoutPCA.cpp b/MobaBus_ServoPCA.cpp similarity index 83% rename from modules/MobaBus_TurnoutPCA.cpp rename to MobaBus_ServoPCA.cpp index d1551a0..9f0c4f0 100644 --- a/modules/MobaBus_TurnoutPCA.cpp +++ b/MobaBus_ServoPCA.cpp @@ -1,5 +1,5 @@ /** - MobaBus Turnout PCA9685 + MobaBus Servo PCA9685 © 2021, Markus Mair. All rights reserved. @@ -19,15 +19,15 @@ along with this program. If not, see . */ -#include "MobaBus_TurnoutPCA.h" +#include "MobaBus_ServoPCA.h" -MobaBus_TurnoutPCA::MobaBus_TurnoutPCA(uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff = true){ - MobaBus_TurnoutPCA(0x40, angle0, angle1, speed, autoPowerOff); +MobaBus_ServoPCA::MobaBus_ServoPCA(uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff) { + MobaBus_ServoPCA(0x40, angle0, angle1, speed, autoPowerOff); } -MobaBus_TurnoutPCA::MobaBus_TurnoutPCA(uint16_t addr, uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff = true) : -servoBoard(addr){ +MobaBus_ServoPCA::MobaBus_ServoPCA(uint16_t addr, uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff) { + servoBoard = Adafruit_PWMServoDriver(addr); powerOff = autoPowerOff; angles[0] = angle0; angles[1] = angle1; @@ -36,7 +36,7 @@ servoBoard(addr){ type = ACCESSORIE; } -uint8_t MobaBus_TurnoutPCA::begin(bool useEEPROM, uint16_t address){ +uint8_t MobaBus_ServoPCA::begin(bool useEEPROM, uint16_t address){ Serial.println("Module begin"); servoBoard.begin(); servoBoard.setPWMFreq(60); @@ -54,7 +54,7 @@ uint8_t MobaBus_TurnoutPCA::begin(bool useEEPROM, uint16_t address){ intitialized = true; return channels; } -void MobaBus_TurnoutPCA::loop(){ +void MobaBus_ServoPCA::loop(){ uint32_t actualTime = millis(); if(actualTime >= lastMove + moveSpeed){ lastMove = actualTime; @@ -74,11 +74,9 @@ void MobaBus_TurnoutPCA::loop(){ } } } - - } -void MobaBus_TurnoutPCA::loadConf(){ +void MobaBus_ServoPCA::loadConf(){ if(controller == NULL) return; uint16_t configAddr = controller->getEEPROMAddress(moduleID); @@ -91,7 +89,7 @@ void MobaBus_TurnoutPCA::loadConf(){ configAddr += sizeof(EEPROM.get(configAddr, angles[1])); } -void MobaBus_TurnoutPCA::storeConf(){ +void MobaBus_ServoPCA::storeConf(){ if(controller == NULL) return; uint16_t configAddr = controller->getEEPROMAddress(moduleID); @@ -102,7 +100,7 @@ void MobaBus_TurnoutPCA::storeConf(){ } -void MobaBus_TurnoutPCA::processPkg(MobaBus_Packet *pkg){ +void MobaBus_ServoPCA::processPkg(MobaBus_Packet *pkg){ if(!intitialized) return; Serial.print("First Address: "); Serial.print(address); @@ -119,7 +117,7 @@ void MobaBus_TurnoutPCA::processPkg(MobaBus_Packet *pkg){ } } -uint8_t MobaBus_TurnoutPCA::programmAddress(uint16_t addr){ +uint8_t MobaBus_ServoPCA::programmAddress(uint16_t addr){ address = addr; Serial.print("Set Address "); Serial.print(address); @@ -128,18 +126,18 @@ uint8_t MobaBus_TurnoutPCA::programmAddress(uint16_t addr){ return channels; } -void MobaBus_TurnoutPCA::setTurnout(uint8_t pin, uint8_t dir, bool power){ +void MobaBus_ServoPCA::setTurnout(uint8_t pin, uint8_t dir, bool power){ if(power){ targetAngle[pin] = angles[dir]; uint8_t data[] = {dir}; controller->sendPkg(ACCESSORIE, address + pin, INFO, 1, data); - } - else{ + } + else{ servoBoard.setPWM(pin, 0, 4096); targetAngle[pin] = actualAngle[pin]; active[pin] = false; - } + } } diff --git a/modules/MobaBus_TurnoutPCA.h b/MobaBus_ServoPCA.h similarity index 84% rename from modules/MobaBus_TurnoutPCA.h rename to MobaBus_ServoPCA.h index 67c916f..e6f202f 100644 --- a/modules/MobaBus_TurnoutPCA.h +++ b/MobaBus_ServoPCA.h @@ -1,5 +1,5 @@ /** - MobaBus Turnout PCA9685 + MobaBus Servo PCA9685 © 2021, Markus Mair. All rights reserved. @@ -19,16 +19,16 @@ along with this program. If not, see . */ -#ifndef __MOBA_TURNOUTPCA__ -#define __MOBA_TURNOUTPCA__ +#ifndef __MOBA_SERVO_PCA__ +#define __MOBA_SERVO_PCA__ #include -#include "MobaBus.h" +#include +#include +#include "MobaBus.h" #include "MobaBus_Module.h" -#include -#include #define SERVOMIN 100 // this is the 'minimum' pulse length count (out of 4096) #define SERVOMAX 550 // this is the 'maximum' pulse length count (out of 4096)575 @@ -37,7 +37,7 @@ -class MobaBus_TurnoutPCA : public MobaBus_Module{ +class MobaBus_ServoPCA : public MobaBus_Module{ private: Adafruit_PWMServoDriver servoBoard; @@ -60,13 +60,13 @@ class MobaBus_TurnoutPCA : public MobaBus_Module{ * Constructor for Turnouts with one PCA9685 module * initialized with standard Address 0x40 */ - MobaBus_TurnoutPCA(uint8_t angle0, uint8_t angle1, uint8_t speed = 245, bool autoPowerOff = true); + MobaBus_ServoPCA(uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff); /** * Constructor for Turnouts with one or multiple PCA9685 modules * @param addr of the module (standard = 0x40) */ - MobaBus_TurnoutPCA(uint16_t addr, uint8_t angle0, uint8_t angle1, uint8_t speed = 245, bool autoPowerOff = true); + MobaBus_ServoPCA(uint16_t addr, uint8_t angle0, uint8_t angle1, uint8_t speed, bool autoPowerOff); uint8_t begin(bool useEEPROM, uint16_t address); diff --git a/MobaBus_Standard_IO.cpp b/MobaBus_Standard_IO.cpp new file mode 100644 index 0000000..49a3370 --- /dev/null +++ b/MobaBus_Standard_IO.cpp @@ -0,0 +1,230 @@ +/** + MobaBus Standard Inputs/Outputs + + © 2021, Markus Mair. All rights reserved. + + This file is part of the MobaBus Project + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "MobaBus_Standard_IO.h" + +MobaBus_Sensor::MobaBus_Sensor(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6, uint8_t pin7, uint8_t pin8, bool pullup){ + pullUp = pullup; + pin[0] = pin1; + pin[1] = pin2; + pin[2] = pin3; + pin[3] = pin4; + pin[4] = pin5; + pin[5] = pin6; + pin[6] = pin7; + pin[7] = pin8; + type = FEEDBACK; +} + +uint8_t MobaBus_Sensor::begin(bool useEEPROM, uint16_t address){ + Serial.println("Module begin"); + programmAddress(address); + + for (int i = 0; i < 8; i++){ + pinMode(pin[i], pullUp ? INPUT_PULLUP : INPUT); + } + intitialized = true; + return 8; +} + +void MobaBus_Sensor::loop(){ + bool newState; + for(int i = 0; i < 8; i++){ + if(debounce[i] < millis()){ + newState = digitalRead(pin[i]) ? !pullUp : pullUp; + if(newState != state[i]){ + debounce[i] = millis() + 80; + state[i] = newState; + sendStates(); + } + } + + } +} + +void MobaBus_Sensor::sendStates(){ + uint8_t states; + for (int i = 0; i < 8; i++){ + states = states | (state[i] << i); + } + controller->sendPkg(FEEDBACK, address, SET, 1, &states); +} + +void MobaBus_Sensor::loadConf(){ + if(controller == NULL) return; + + uint16_t configAddr = controller->getEEPROMAddress(moduleID); + uint16_t storedAddress; + configAddr += sizeof(EEPROM.get(configAddr, storedAddress)); + + if(address != storedAddress) return; +} + +void MobaBus_Sensor::storeConf(){ + if(controller == NULL) return; + + uint16_t configAddr = controller->getEEPROMAddress(moduleID); + + configAddr += sizeof(EEPROM.put(configAddr, address)); + +} + +void MobaBus_Sensor::processPkg(MobaBus_Packet *pkg){ + if(!intitialized) return; + if(pkg->meta.type == type && pkg->meta.address == address && pkg->meta.cmd == GET){ + sendStates(); + } +} + +uint8_t MobaBus_Sensor::programmAddress(uint16_t addr){ + address = addr; + Serial.print("Set Module Address "); + Serial.print(address); + return 8; +} + +//---------------------- MobaBus_Turnout ------------------------------------- + +MobaBus_Output::MobaBus_Output(uint8_t pin, bool inverted){ + this->pin = pin; + this->inverted = inverted; + type = ACCESSORIE; +} + +uint8_t MobaBus_Output::begin(bool useEEPROM, uint16_t address){ + Serial.println("Module begin"); + pinMode(this->pin, OUTPUT); + digitalWrite(this->pin, inverted); + programmAddress(address); + intitialized = true; + return 1; +} + +void MobaBus_Output::loop() { + return; +} + +void MobaBus_Output::loadConf() { + return; +} + +void MobaBus_Output::storeConf(){ + return; +} + +void MobaBus_Output::processPkg(MobaBus_Packet *pkg){ + if(pkg->meta.type == type && pkg->meta.address == address){ + if(pkg->meta.cmd == SET){ + setTurnout((bool)pkg->data[0]); + } + else if(pkg->meta.cmd == GET){ + uint8_t data[] = {state}; + controller->sendPkg(ACCESSORIE, address, INFO, 1, data); + } + } +} + +uint8_t MobaBus_Output::programmAddress(uint16_t addr){ + address = addr; + Serial.print("Set Address "); + Serial.println(address); + return 1; +} + +void MobaBus_Output::setTurnout(bool dir){ + digitalWrite(pin, inverted ? !dir : dir); + state = dir; + uint8_t data[] = {dir}; + controller->sendPkg(ACCESSORIE, address, INFO, 1, data); +} + +//---------------------- MobaBus_Turnout_2pin -------------------------------- + +MobaBus_Output_2pin::MobaBus_Output_2pin(uint8_t pin1, uint8_t pin2, bool inverted, bool autoPowerOff){ + pin[0] = pin1; + pin[1] = pin2; + this->inverted = inverted; + this->autoPowerOff = autoPowerOff; + type = ACCESSORIE; +} + +uint8_t MobaBus_Output_2pin::begin(bool useEEPROM, uint16_t address){ + Serial.println("Module begin"); + pinMode(this->pin[0], OUTPUT); + pinMode(this->pin[1], OUTPUT); + digitalWrite(this->pin[0], inverted); + digitalWrite(this->pin[0], inverted); + programmAddress(address); + intitialized = true; + return 1; +} + +void MobaBus_Output_2pin::loop(){ + if(autoPowerOff && active && powerOffT <= millis()){ + setTurnout(state, false); + } +} + +void MobaBus_Output_2pin::loadConf() { + return; +} + +void MobaBus_Output_2pin::storeConf() { + return; +} + + +void MobaBus_Output_2pin::processPkg(MobaBus_Packet *pkg){ + if(pkg->meta.type == type && pkg->meta.address == address){ + if(pkg->meta.cmd == SET){ + setTurnout((bool)pkg->data[0], (bool)pkg->data[1]); + } + else if(pkg->meta.cmd == GET){ + uint8_t data[] = {state}; + controller->sendPkg(ACCESSORIE, address, INFO, 1, data); + } + } +} + +uint8_t MobaBus_Output_2pin::programmAddress(uint16_t addr){ + address = addr; + Serial.print("Set Address "); + Serial.println(address); + return 1; +} + +void MobaBus_Output_2pin::setTurnout(bool dir, bool power){ + if(power){ + bool p = dir ? inverted : !inverted; + digitalWrite(pin[0], p); + digitalWrite(pin[1], !p); + state = dir; + active = true; + powerOffT = millis() + AUTO_POWER_OFF; + uint8_t data[] = {dir}; + controller->sendPkg(ACCESSORIE, address, INFO, 1, data); + } + else{ + digitalWrite(pin[0], inverted); + digitalWrite(pin[1], inverted); + active = false; + } +} diff --git a/MobaBus_Standard_IO.h b/MobaBus_Standard_IO.h new file mode 100644 index 0000000..0f1c8eb --- /dev/null +++ b/MobaBus_Standard_IO.h @@ -0,0 +1,111 @@ +/** + MobaBus Standard Inputs/Outputs + + © 2021, Markus Mair. All rights reserved. + + This file is part of the MobaBus Project + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef __MOBABUS_STANDARD_IO__ +#define __MOBABUS_STANDARD_IO__ + +#include "MobaBus_Module.h" +#include "MobaBus.h" + +class MobaBus_Sensor : public MobaBus_Module{ +private: + uint8_t pin[8]; + bool pullUp; + + bool state[8]; + + uint32_t debounce[8]; + + void sendStates(); + +public: + /** + * Constructor + * @param pinX number, 0 if not used! + * @param pullup if true pinMode is INPUT_PULLUP else INPUT + */ + MobaBus_Sensor(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6, uint8_t pin7, uint8_t pin8, bool pullup); + + uint8_t begin(bool useEEPROM, uint16_t address); + void loop(); + + void loadConf(); + void storeConf(); + + void processPkg(MobaBus_Packet *pkg); + + uint8_t programmAddress(uint16_t addr); +}; + +class MobaBus_Output : public MobaBus_Module{ +private: + uint8_t pin; + bool inverted; + bool state; + +public: + MobaBus_Output(uint8_t pin, bool inverted); + + uint8_t begin(bool useEEPROM, uint16_t address); + void loop(); + + void loadConf(); + void storeConf(); + + + void processPkg(MobaBus_Packet *pkg); + + uint8_t programmAddress(uint16_t addr); + + void setTurnout(bool dir); + +}; + +#define AUTO_POWER_OFF 500 // ms after switching the pins turn off to prevent a burn out of the turnout drive + +class MobaBus_Output_2pin : public MobaBus_Module{ +private: + uint8_t pin[2]; + bool inverted; + bool state; + + bool active; + uint32_t powerOffT; + + bool autoPowerOff; + +public: + MobaBus_Output_2pin(uint8_t pin1, uint8_t pin2, bool inverted, bool autoPowerOff); + + uint8_t begin(bool useEEPROM, uint16_t address); + void loop(); + + void loadConf(); + void storeConf(); + + + void processPkg(MobaBus_Packet *pkg); + + uint8_t programmAddress(uint16_t addr); + + void setTurnout(bool dir, bool power); +}; +#endif \ No newline at end of file diff --git a/examples/servo/servo.ino b/examples/servo/servo.ino new file mode 100644 index 0000000..c5bd2b1 --- /dev/null +++ b/examples/servo/servo.ino @@ -0,0 +1,25 @@ +#include + +#include + +#include + +MobaBus mobaBus(0, 8, 9); //MobaBus-controller instance with EEPROM, progamming Button and statusLED + +MobaBus_CAN can(10, CAN_125KBPS, MCP_8MHZ, 2); //Can Bus interface + +MobaBus_Servo servo(7, 70, 110, 230, true); // Servo attached to pin 7, angle0=70°, angle1=110°, moving speed=230 and autoPowerOff=on + +void setup() { + + mobaBus.begin(); //initialize the controller + + mobaBus.attachInterface(&can); //add the can bus interface to the controller + mobaBus.attachModule(&servo); // add the Servo module to the controller + +} + +void loop() { + + mobaBus.loop(); +} \ No newline at end of file diff --git a/examples/servo16x/servo16x.ino b/examples/servo16x/servo16x.ino index 3947c47..6875cc4 100644 --- a/examples/servo16x/servo16x.ino +++ b/examples/servo16x/servo16x.ino @@ -2,13 +2,13 @@ #include -#include //Require: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library +#include //Require: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library MobaBus mobaBus(0, 8, 9); //MobaBus-controller instance with EEPROM, progamming Button and statusLED MobaBus_CAN can(10, CAN_125KBPS, MCP_8MHZ, 2); //Can Bus interface -MobaBus_TurnoutPCA servos(0x40, 70, 110, 230, true); // PCA9685 board with address=0x40, angle0=70°, angle1=110°, moving speed=230 and autoPowerOff=on +MobaBus_ServoPCA servos(0x40, 70, 110, 230, true); // PCA9685 board with address=0x40, angle0=70°, angle1=110°, moving speed=230 and autoPowerOff=on void setup() { diff --git a/examples/standardIO/standardIO.ino b/examples/standardIO/standardIO.ino new file mode 100644 index 0000000..1badcb3 --- /dev/null +++ b/examples/standardIO/standardIO.ino @@ -0,0 +1,29 @@ +#include + +#include + +#include + +MobaBus mobaBus(0, 8, 9); //MobaBus-controller instance with EEPROM, progamming Button and statusLED + +MobaBus_CAN can(10, CAN_125KBPS, MCP_8MHZ, 2); //Can Bus interface + +MobaBus_Sensor sensor(A0, A1, A2, A3, A4, A5, A6, A7,true); // Sensor attached to pins A0-A7 and INPUT_PULLUP-mode +MobaBus_Output output( 7, false); //Digital output on pin 7, not inverted +MobaBus_Output_2pin output2(5, 6, false, true); // Digital output on 2 pins (dir0 = pin5 high, dir6 = pin2 high), not inverted and auto Power Off to prevent burnout + +void setup() { + + mobaBus.begin(); //initialize the controller + + mobaBus.attachInterface(&can); //add the can bus interface to the controller + mobaBus.attachModule(&sensor); // add the Sensors module to the controller + mobaBus.attachModule(&output); // add the output module to the controller + mobaBus.attachModule(&output2); // add the output2 module to the controller +} + + +void loop() { + + mobaBus.loop(); +} \ No newline at end of file diff --git a/modules/MobaBus_Sensor.cpp b/modules/MobaBus_Sensor.cpp deleted file mode 100644 index 7dd3447..0000000 --- a/modules/MobaBus_Sensor.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - MobaBus Sensor - - © 2021, Markus Mair. All rights reserved. - - This file is part of the MobaBus Project - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "MobaBus_Sensor.h" - - -MobaBus_Sensor::MobaBus_Sensor(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6, uint8_t pin7, uint8_t pin8, bool pullup){ - pullUp = pullup; - pin[0] = pin1; - pin[1] = pin2; - pin[2] = pin3; - pin[3] = pin4; - pin[4] = pin5; - pin[5] = pin6; - pin[6] = pin7; - pin[7] = pin8; - type = FEEDBACK; -} - -uint8_t MobaBus_Sensor::begin(bool useEEPROM, uint16_t address){ - Serial.println("Module begin"); - programmAddress(address); - - for (int i = 0; i < 8; i++){ - pinMode(pin[i], pullUp ? INPUT_PULLUP : INPUT); - } - intitialized = true; - return 8; -} - -void MobaBus_Sensor::loop(){ - bool newState; - for(int i = 0; i < 8; i++){ - if(debounce[i] < millis()){ - newState = digitalRead(pin[i]) ? !pullUp : pullUp; - if(newState != state[i]){ - debounce[i] = millis() + 80; - state[i] = newState; - sendStates(); - } - } - - } -} - -void MobaBus_Sensor::sendStates(){ - uint8_t states; - for (int i = 0; i < 8; i++){ - states = states | (state[i] << i); - } - controller->sendPkg(FEEDBACK, address, SET, 1, &states); -} - -void MobaBus_Sensor::loadConf(){ - if(controller == NULL) return; - - uint16_t configAddr = controller->getEEPROMAddress(moduleID); - uint16_t storedAddress; - configAddr += sizeof(EEPROM.get(configAddr, storedAddress)); - - if(address != storedAddress) return; -} - -void MobaBus_Sensor::storeConf(){ - if(controller == NULL) return; - - uint16_t configAddr = controller->getEEPROMAddress(moduleID); - - configAddr += sizeof(EEPROM.put(configAddr, address)); - -} - -void MobaBus_Sensor::processPkg(MobaBus_Packet *pkg){ - if(!intitialized) return; - if(pkg->meta.type == type && pkg->meta.address == address && pkg->meta.cmd == GET){ - sendStates(); - } -} - -uint8_t MobaBus_Sensor::programmAddress(uint16_t addr){ - address = addr; - Serial.print("Set Module Address "); - Serial.print(address); - return 8; -}