Skip to content

Commit

Permalink
Add more modules and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Maggge committed Feb 6, 2022
1 parent 14afae7 commit 7e0ef53
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 148 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
109 changes: 109 additions & 0 deletions MobaBus_Servo.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#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;
}
}
44 changes: 28 additions & 16 deletions modules/MobaBus_Sensor.h → MobaBus_Servo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
MobaBus Sensor
MobaBus Servo
© 2021, Markus Mair. All rights reserved.
Expand All @@ -19,39 +19,51 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __MOBABUS_SENSOR__
#define __MOBABUS_SENSOR__
#ifndef __MOBABUS_SERVO__
#define __MOBABUS_SERVO__

#include "MobaBus_Module.h"
#include "MobaBus.h"
#include "MobaBus_Module.h"

#include <Servo.h>

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

void loadConf();
void storeConf();


void processPkg(MobaBus_Packet *pkg);

uint8_t programmAddress(uint16_t addr);

void setTurnout(bool dir, bool power);
};

#endif
34 changes: 16 additions & 18 deletions modules/MobaBus_TurnoutPCA.cpp → MobaBus_ServoPCA.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
MobaBus Turnout PCA9685
MobaBus Servo PCA9685
© 2021, Markus Mair. All rights reserved.
Expand All @@ -19,15 +19,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
}
}


Expand Down
18 changes: 9 additions & 9 deletions modules/MobaBus_TurnoutPCA.h → MobaBus_ServoPCA.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
MobaBus Turnout PCA9685
MobaBus Servo PCA9685
© 2021, Markus Mair. All rights reserved.
Expand All @@ -19,16 +19,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __MOBA_TURNOUTPCA__
#define __MOBA_TURNOUTPCA__
#ifndef __MOBA_SERVO_PCA__
#define __MOBA_SERVO_PCA__

#include <Arduino.h>
#include "MobaBus.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

#include "MobaBus.h"
#include "MobaBus_Module.h"

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

#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
Expand All @@ -37,7 +37,7 @@



class MobaBus_TurnoutPCA : public MobaBus_Module{
class MobaBus_ServoPCA : public MobaBus_Module{
private:
Adafruit_PWMServoDriver servoBoard;

Expand All @@ -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);
Expand Down
Loading

0 comments on commit 7e0ef53

Please sign in to comment.