From f7b2b9bac77fad2549fb13597c2f94aa6235e8df Mon Sep 17 00:00:00 2001 From: TwinDad Date: Tue, 1 Dec 2015 11:24:13 -0500 Subject: [PATCH] Add support for OUTPUT Sensors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ok, if it’s an output it’s not really a Sensor, but… Allows definition of a Sensor with direction OUTPUT, and adds a Write command to change its state. --- DCCpp_Uno/Sensor.cpp | 55 +++++++++++++++++++++++++++++++++++--------- DCCpp_Uno/Sensor.h | 5 +++- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/DCCpp_Uno/Sensor.cpp b/DCCpp_Uno/Sensor.cpp index c958c94..a667bb4 100644 --- a/DCCpp_Uno/Sensor.cpp +++ b/DCCpp_Uno/Sensor.cpp @@ -24,15 +24,16 @@ for each sensor. You may need to change these parameters through trial and erro To have this sketch monitor one or more Arduino pins for sensor triggers, first define/edit/delete sensor definitions using the following variation of the "S" command: - : creates a new sensor ID, with specified PIN and PULLUP - if sensor ID already exists, it is updated with specificed PIN and PULLUP + : creates a new sensor ID, with specified PIN, DIR and PULLUP + if sensor ID already exists, it is updated with specificed PIN, DIR and PULLUP returns: if successful and if unsuccessful (e.g. out of memory) + DIR is 1 for output, 0 for input. : deletes definition of sensor ID returns: if successful and if unsuccessful (e.g. ID does not exist) : lists all defined sensors - returns: for each defined sensor or if no sensors defined + returns: for each defined sensor or if no sensors defined where @@ -53,6 +54,9 @@ If a Sensor Pin is found to have transitioned from one state to another, one of Depending on whether the physical sensor is acting as an "event-trigger" or a "detection-sensor," you may decide to ignore the return and only react to triggers. +To Write to a Sensor as an Output: + - where VAL is 1 for high or 0 for low. This will be ignored if sensor is defined with DIR=0. + **********************************************************************/ #include "DCCpp_Uno.h" @@ -86,7 +90,12 @@ void Sensor::check(){ /////////////////////////////////////////////////////////////////////////////// -Sensor *Sensor::create(int snum, int pin, int pullUp, int v){ +Sensor *Sensor::create(int snum, int pin, int pullUp, int v) { + // support the prevous input-only definition format: ID PIN PULLUP + return(Sensor::create(snum, pin, 0, pullUp, v)); +} + +Sensor *Sensor::create(int snum, int pin, int dir, int pullUp, int v){ Sensor *tt; if(firstSensor==NULL){ @@ -108,11 +117,16 @@ Sensor *Sensor::create(int snum, int pin, int pullUp, int v){ tt->data.snum=snum; tt->data.pin=pin; + tt->data.dir = (dir == 1 ? true : false); tt->data.pullUp=(pullUp==0?LOW:HIGH); tt->active=false; tt->signal=1; - pinMode(pin,INPUT); // set mode to input - digitalWrite(pin,pullUp); // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor + if (tt->data.dir) { + pinMode(pin, OUTPUT); + } else { + pinMode(pin,INPUT); // set mode to input + digitalWrite(pin,pullUp); // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor + } if(v==1) INTERFACE.print(""); @@ -165,6 +179,8 @@ void Sensor::show(){ INTERFACE.print(" "); INTERFACE.print(tt->data.pin); INTERFACE.print(" "); + INTERFACE.print(tt->data.dir ? "1" : "0"); + INTERFACE.print(" "); INTERFACE.print(tt->data.pullUp); INTERFACE.print(">"); } @@ -190,15 +206,22 @@ void Sensor::status(){ /////////////////////////////////////////////////////////////////////////////// void Sensor::parse(char *c){ - int n,s,m; + int n,s,m,d; Sensor *t; - switch(sscanf(c,"%d %d %d",&n,&s,&m)){ - + switch(sscanf(c,"%d %d %d %d",&n,&s,&d,&m)){ + case 4: + create(n, s, d, m, 1); // argument is string with id number of sensor followed by a pin number, direction and pullUp indicator (O=LOW/1=HIGH) + break; + case 3: // argument is string with id number of sensor followed by a pin number and pullUp indicator (0=LOW/1=HIGH) - create(n,s,m,1); + create(n,s,d,1); break; + case 2: + write(n, s); // argument is a string with id and value + break; + case 1: // argument is a string with id number only remove(n); break; @@ -207,7 +230,7 @@ void Sensor::parse(char *c){ show(); break; - case 2: // invalid number of arguments + default: // invalid number of arguments INTERFACE.print(""); break; } @@ -215,6 +238,16 @@ void Sensor::parse(char *c){ /////////////////////////////////////////////////////////////////////////////// +void Sensor::write(int id, int val) { + if (val != 0 && val != 1) { return; } // sanity check + Sensor *s = Sensor::get(id); + if (s != NULL && s->data.dir == true) { + digitalWrite(s->data.pin, (byte)(val == 1 ? 1 : 0)); + } +} + +/////////////////////////////////////////////////////////////////////////////// + void Sensor::load(){ struct SensorData data; Sensor *tt; diff --git a/DCCpp_Uno/Sensor.h b/DCCpp_Uno/Sensor.h index 9f145f1..019b0dc 100644 --- a/DCCpp_Uno/Sensor.h +++ b/DCCpp_Uno/Sensor.h @@ -17,6 +17,7 @@ Part of DCC++ BASE STATION for the Arduino Uno struct SensorData { int snum; byte pin; + boolean dir; byte pullUp; }; @@ -28,13 +29,15 @@ struct Sensor{ Sensor *nextSensor; static void load(); static void store(); + static Sensor *create(int, int, int, int, int); static Sensor *create(int, int, int, int=0); static Sensor* get(int); static void remove(int); static void show(); static void status(); static void parse(char *c); - static void check(); + static void check(); + static void write(int, int); }; // Sensor #endif