Skip to content

Commit 457ce6a

Browse files
committed
Sandbox Refactor - AgFirmware.ino
1 parent 4c5682e commit 457ce6a

17 files changed

+971
-1456
lines changed

PumpController/Firmware/PumpController/PumpController.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <PeerIOSerialControl.h> //See https://github.com/tgit23/PeerIOSerialControl
1111
#include <SoftwareSerial.h>
1212

13-
#define TRANSCEIVER_ID 10 // Unique Transceiver ID for this Device (1-15)
13+
#define TRANSCEIVER_ID 11 // Unique Transceiver ID for this Device (1-15)
1414
#define XBEECONFIG 0 // 1 to enter XBEE Configuration Mode, 0 Normal Operation
1515
#define DEBUG 0 // 1 for DEBUG
1616

@@ -19,7 +19,7 @@
1919
#define SS_RX_PIN 3 // XBee DOUT
2020
#define PUMP_POWER_PIN 7 // Pump Power Pin ( Blue twisted pair )
2121
#define PUMP_AUX_CONTACT 0 // Pump Power Aux Contact ( Green twisted pair ) 0-to deactivate
22-
#define ULTRASONIC_PRESENT 1 // 1=UltraSonic Level Monitor Attached, 0=No UltraSonic
22+
#define ULTRASONIC_PRESENT 0 // 1=UltraSonic Level Monitor Attached, 0=No UltraSonic
2323
#define ULTRASONIC_TRIG_PIN 4 // UltraSonic Trigger Pin
2424
#define ULTRASONIC_ECHO_PIN 5 // UltraSonic Echo Pin
2525
#define ULTRASONIC_MAX_DIST 400 // Longest Distance to Measure

Sandbox/AgFirmware/AgFirmware.ino

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/******************************************************************************************************************//**
2+
* @file AgFirmware.ino
3+
* @brief Arduino Sketch Firmware for the Devices of the AgIrrigationRemoteControl project
4+
* @see https://github.com/tgit23/AgIrrigationRemoteControl
5+
* @authors
6+
* tgit23 8/2017 Original
7+
**********************************************************************************************************************/
8+
#include <LiquidCrystal.h>
9+
#include "RemoteMenu.h"
10+
11+
//vvvvvvvvvvvvvvvvvv[[[ DEVICES ]]]vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
12+
// Hand-Held Remote Units ( 1-5 )
13+
// Smart-Controller Units with LCD ( 6-10 )
14+
// Dumb-Controller Units without LCD ( 11-15 )
15+
#define HAND_REMOTE 1
16+
#define PUMP_CONTROLLER 6
17+
#define GATE_CONTROLLER 11
18+
#define CHEM_CONTROLLER 12
19+
#define PIVOT_CONTROLLER 8
20+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
//------------------------------------------------------------------------------------------
22+
#define THISDEVICE HAND_REMOTE // The device this Firmware is for
23+
#define XBEECONFIG 0 // Set to 1 to configure RF Radio through XCTU
24+
#define RELEASE 20170706 // Release Version used to Build the Units
25+
//------------------------------------------------------------------------------------------
26+
27+
#if THISDEVICE<11
28+
//vvvvvvvvvvvvvvvvvv[[[ USER PIN DEFINITIONS ]]]vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
29+
// PinTypes { INPIN, INPINHIGH, OUTPIN, BUZZPIN, SONICPIN, SETTABLE };
30+
// |---- DEVICE-----|-PIN-||-PinType---|----NAME---|ID|-------------------
31+
//------------------------------------------------------------------------------------------
32+
PinPoint PumpPower ( PUMP_CONTROLLER, 7, SETTABLE, "Power", 'P' );
33+
PinPoint WaterLevel ( PUMP_CONTROLLER, 64, SONICPIN, "Water", 'L' );
34+
PinPoint GateLevel ( GATE_CONTROLLER, 6, SETTABLE, "Gate", 'G' );
35+
PinPoint ChemPump ( CHEM_CONTROLLER, 7, OUTPIN, "Chem", 'C' );
36+
PinPoint PivotOnOff ( PIVOT_CONTROLLER, 7, INPINHIGH, "Pivot", 'P' );
37+
38+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
// Hardware Pin Definitions
41+
#if THISDEVICE<6 //----------------- Hand-Held Remote Units -------
42+
LiquidCrystal LCD(8, 9, 4, 5, 6, 7);
43+
PinPoint Battery ( HAND_REMOTE, A1, INPIN, "Battery", 'B' );
44+
#if RELEASE<20170525
45+
PinPoint Buzzer ( HAND_REMOTE, 2, BUZZPIN );
46+
RemoteMenu Menu (12, 11, THISDEVICE, &LCD);
47+
#else
48+
PinPoint Buzzer ( HAND_REMOTE, 11, BUZZPIN );
49+
RemoteMenu Menu (3, 2, THISDEVICE, &LCD);
50+
#endif
51+
#else //----------------- Smart-Controller with LCD -----
52+
LiquidCrystal LCD(12, 13, 8, 9, 10, 11);
53+
RemoteMenu Menu(3, 2, THISDEVICE, &LCD);
54+
#endif
55+
56+
void setup() {
57+
Serial.begin(9600);LCD.begin(16, 2);
58+
59+
//vvvvvvvvvvvvvvvvvv[[[ DEVICE NAMES ]]]vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
60+
// Assign Display Names to the Devices
61+
Menu.DeviceDisplayName(HAND_REMOTE, "Remote");
62+
Menu.DeviceDisplayName(PUMP_CONTROLLER, "Pump");
63+
Menu.DeviceDisplayName(GATE_CONTROLLER, "Gate");
64+
Menu.DeviceDisplayName(CHEM_CONTROLLER, "Chem");
65+
Menu.DeviceDisplayName(PIVOT_CONTROLLER, "Pivot");
66+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
68+
//vvvvvvvvvvvvvvvvvv[[[ PIN CONTROLS ]]]vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
69+
//
70+
// ControlTypes { SET_PIN, PID_SET, DIRECTLY, LESS_THAN, GREATER_THAN, EQUAL_TO, NOT_EQUAL_TO };
71+
//----------------------- PIN --------- ControlType-------ID----|StorePin|-
72+
#if THISDEVICE<6
73+
Battery.Controls ( &Buzzer, LESS_THAN, 'b' );
74+
Menu.AddPin(&Battery);
75+
#endif
76+
WaterLevel.Controls ( &GateLevel, PID_SET, 'g' );
77+
PivotOnOff.Controls ( &ChemPump, DIRECTLY, 'e' );
78+
79+
//--------- Add Pins to the Display Menu --------------------
80+
Menu.AddPin(&PumpPower);
81+
Menu.AddPin(&WaterLevel);
82+
Menu.AddPin(&GateLevel);
83+
Menu.AddPin(&ChemPump);
84+
Menu.StartDisplay(&PumpPower);
85+
}
86+
87+
void loop() {
88+
Menu.loop();
89+
}
90+
91+
92+
#else // THISDEVIC is (11-15)
93+
//---------------------------------------------------------------------------------------------------------------------
94+
// DUMB-CONTROLLER without LCD support or any local controls
95+
//---------------------------------------------------------------------------------------------------------------------
96+
SoftwareSerial IOSerial(3,2); // SoftSerial for XBEE ( rxPin, txPin )
97+
PeerIOSerialControl XBee(THISDEVICEID,IOSerial,Serial);
98+
99+
void setup() {
100+
pinMode(3, INPUT); // XBee DOUT Pin
101+
pinMode(2, OUTPUT); // XBee DIN Pin
102+
Serial.begin(9600);
103+
IOSerial.begin(9600); // Start UART Communications with the XBee->Module
104+
}
105+
106+
void loop(){
107+
108+
#if XBEECONFIG!=0
109+
if ( IOSerial.available()>0 ) Serial.write(IOSerial.read()); // Forward Serial to XBEE for XBEE Config
110+
if ( Serial.available()>0 ) IOSerial.write(Serial.read());
111+
#else
112+
XBee.Available(); // Check Communications
113+
#if UTRASONIC_METER_INSTALLED==1 // Read UltraSonic water level if installed
114+
int ulCurrentTime = millis();
115+
if ( ulCurrentTime > ulLastPing + 1000 ) {
116+
XBee.VirtualPin(64, sonar.ping_in() ); // Assign UltraSonic reading to Virt.Pin(64)
117+
ulLastPing = ulCurrentTime;
118+
}
119+
#endif
120+
121+
#endif
122+
#endif

Sandbox/AgIrrigationFirmwareWithClasses/PeerIOSerialControl.cpp renamed to Sandbox/AgFirmware/PeerIOSerialControl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* tgit23 1/2017 Original
66
******************************************************************************/
77
#include "PeerIOSerialControl.h"
8+
89
#define ID_MASK 0x0F // Bytes[0] [0000 1111] ArduinoID ( 0-15 )
910
#define REPLY_BIT 4 // Bytes[0] [0001 0000] Reply-1, Send-0
1011
#define RW_BIT 5 // Bytes[0] [0010 0000] Read-1, Write-0
@@ -82,9 +83,9 @@ void PeerIOSerialControl::Timeout(int milliseconds) {
8283
int PeerIOSerialControl::Timeout() {
8384
return BlockingTimeoutMS;
8485
}
85-
void PeerIOSerialControl::VirtualPin(int Pin, int Value, PinStatus _PinStatus = IsOff) {
86+
void PeerIOSerialControl::VirtualPin(int Pin, int Value, PinStatus _PinStatus = OKAY) {
8687
if ( Pin > 63 && Pin < 128 ) {
87-
iVirtualPin[Pin-64] = Value = ((Value << 0) & 0x0FFF) + ((_PinStatus << 12) & 0x3000);
88+
iVirtualPin[Pin-64] = ((Value << 0) & 0x0FFF) + ((_PinStatus << 12) & 0x3000);
8889
}
8990
}
9091
int PeerIOSerialControl::VirtualPin(int Pin) {

Sandbox/AgIrrigationFirmwareWithClasses/PeerIOSerialControl.h renamed to Sandbox/AgFirmware/PeerIOSerialControl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#define READ 1
3636
#define WRITE 0
3737
#define VPIN_STATUS_ON
38-
typedef enum PinStatus { IsOff = 0, IsOn = 1, IsWait = 2, IsErr = 3 };
38+
typedef enum PinStatus { ISOFF = 0, ISON = 1, WAIT = 2, ERR = 3, OKAY = 4 };
3939

4040
/**********************************************************************************************//**
4141
* @class PeerIOSerialControl
@@ -275,7 +275,7 @@ class PeerIOSerialControl {
275275
* XBee.VirtualPin(70, 15200);
276276
* @endcode
277277
**************************************************************************************/
278-
void VirtualPin(int Pin, int Value, PinStatus _PinStatus = IsOff);
278+
void VirtualPin(int Pin, int Value, PinStatus _PinStatus = OKAY);
279279

280280
/***********************************************************************************//**
281281
* @brief Gets the Value for one of the Analog Virtual Pins (i.e. 64-127)

Sandbox/AgFirmware/PinPoint.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/******************************************************************************************************************//**
2+
* @file PinPoint.cpp
3+
* @brief Class definition for PinPoint used for I/O Control of a Pin on a Device
4+
* @authors
5+
* tgit23 8/2017 Original
6+
**********************************************************************************************************************/
7+
#include "PinPoint.h"
8+
#include "UserControl.h"
9+
10+
PeerIOSerialControl *PinPoint::XBee = NULL;
11+
uint8_t PinPoint::ThisDeviceID = 0;
12+
unsigned int PinPoint::LastEpromOffset = 0;
13+
14+
/******************************************************************************************************************//**
15+
* @brief Constructor
16+
* @remarks
17+
* - Raw values are still used behind the scene's only the displayed value is modified
18+
* @code
19+
* exmaple code
20+
* @endcode
21+
**********************************************************************************************************************/
22+
PinPoint::PinPoint(uint8_t _Device, uint8_t _Pin, ePinType _PinType, char *_Name = NULL, char _ID = NULL ) {
23+
Device = _Device; Pin = _Pin; PinType = _PinType; Name = _Name; ID = _ID;
24+
if ( Pin <= A0 ) IsOnOff = true;
25+
if ( PinType == SETTABLE ) FirstControl = new UserControl( this, SET_PIN, this, ID );
26+
switch ( PinType ) {
27+
case INPIN: pinMode(Pin,INPUT);break;
28+
case INPINHIGH: pinMode(Pin,INPUT_PULLUP);break;
29+
case OUTPIN: pinMode(Pin,OUTPUT);break;
30+
case BUZZPIN: pinMode(Pin,OUTPUT);break;
31+
case SONICPIN: break;
32+
case SETTABLE: pinMode(Pin,OUTPUT);break;
33+
}
34+
}
35+
36+
/******************************************************************************************************************//**
37+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
38+
* @remarks
39+
* - Raw values are still used behind the scene's only the displayed value is modified
40+
* @code
41+
* APin.ReadValue();
42+
* while ( APin.Status() == WAIT ) { // Loop Till Value is Retreived }
43+
* GottenValue = APin.GetValue();
44+
* @endcode
45+
**********************************************************************************************************************/
46+
void PinPoint::ReadValue(bool _ForceBlocking = false) {
47+
DB(("PinPoint::ReadValue(Blocking="));DB((_ForceBlocking));DBL((")"));
48+
49+
mStatus = ERR; mPacketID = -1;
50+
if ( Device > 16 || Pin > 127 ) return; // Value Check
51+
52+
if ( Device == ThisDeviceID ) { //--- Local Pin Read ---
53+
DB(("LOCAL Get: Device="));DB((Device));DB((" Pin="));DBL((Pin));
54+
if ( Pin >= A0 ) { mValue = analogRead(Pin); }
55+
else { mValue = digitalRead(Pin); }
56+
if ( mValue != -1 ) mStatus = OKAY;
57+
}
58+
else if ( _ForceBlocking ) {
59+
DB(("REMOTE Get: Device="));DB((Device));DB((" Pin="));DBL((Pin));
60+
if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device ); //Set TransceiverID
61+
if ( Pin >= A0 ) { mValue = XBee->analogReadB(Pin); }
62+
else { mValue = XBee->digitalReadB(Pin); }
63+
if ( mValue != -1 ) mStatus = OKAY;
64+
}
65+
else { //--- Remote Pin Read ---
66+
DB(("REMOTE Get: Device="));DB((Device));DB((" Pin="));DBL((Pin));
67+
if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device ); //Set TransceiverID
68+
mWaitStart = millis();
69+
if ( Pin >= A0 ) { mPacketID = XBee->analogReadNB(Pin); }
70+
else { mPacketID = XBee->digitalReadNB(Pin); }
71+
mStatus = WAIT;
72+
}
73+
}
74+
75+
/******************************************************************************************************************//**
76+
* @brief Checks XBee communications and returns 'true' if the pin Status has changed
77+
* @remarks
78+
* @code
79+
* exmaple code
80+
* @endcode
81+
**********************************************************************************************************************/
82+
bool PinPoint::UpdateAvailable() {
83+
XBee->Available();
84+
if ( mStatus == WAIT ) {
85+
if ( mPacketID != -1 ) {
86+
int Ret = XBee->GetReply(mPacketID);
87+
if ( Ret != -1 ) {
88+
mValue = Ret;
89+
mStatus = OKAY;
90+
return true;
91+
}
92+
}
93+
if ( millis() - mWaitStart > XBee->Timeout() ) {
94+
mPacketID = -1;
95+
mStatus = ERR;
96+
return true;
97+
}
98+
}
99+
return false;
100+
}
101+
102+
/******************************************************************************************************************//**
103+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
104+
* @remarks
105+
* - Raw values are still used behind the scene's only the displayed value is modified
106+
* @code
107+
* exmaple code
108+
* @endcode
109+
**********************************************************************************************************************/
110+
PinStatus PinPoint::GetStatus() {
111+
return mStatus;
112+
}
113+
114+
/******************************************************************************************************************//**
115+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
116+
* @remarks
117+
* - Raw values are still used behind the scene's only the displayed value is modified
118+
* @code
119+
* exmaple code
120+
* @endcode
121+
**********************************************************************************************************************/
122+
int PinPoint::GetRawValue() {
123+
return mValue;
124+
}
125+
126+
/******************************************************************************************************************//**
127+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
128+
* @remarks
129+
* - Raw values are still used behind the scene's only the displayed value is modified
130+
* @code
131+
* exmaple code
132+
* @endcode
133+
**********************************************************************************************************************/
134+
int PinPoint::GetModifiedValue() {
135+
if ( ValueModifierCallback == NULL ) return mValue;
136+
return (*ValueModifierCallback)(mValue);
137+
}
138+
139+
int PinPoint::ModifyValue(int _Value) {
140+
if ( ValueModifierCallback == NULL ) return _Value;
141+
return (*ValueModifierCallback)(_Value);
142+
}
143+
/******************************************************************************************************************//**
144+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
145+
* @remarks
146+
* - Raw values are still used behind the scene's only the displayed value is modified
147+
* @code
148+
* exmaple code
149+
* @endcode
150+
**********************************************************************************************************************/
151+
void PinPoint::SetTo(unsigned int _Value, PinStatus _Status = OKAY) {
152+
DB(("PinPoint::SetTo("));DB((Device));DBC;DB((Pin));DBC;DB((_Value));DBL((")"));
153+
154+
if ( Device > 16 || Pin > 127 ) return; // Value Check
155+
156+
// Drive Buzzer
157+
if ( PinType == BUZZPIN ) {
158+
if (_Value < 1) { noTone(Pin); }
159+
else { tone(Pin, _Value); }
160+
}
161+
162+
// Drive Local Pin
163+
else if ( Device == ThisDeviceID ) { // Drive a LOCAL Pin
164+
if ( Pin > 63 ) { XBee->VirtualPin(Pin, _Value, _Status); } // Drive a Virtual Pin
165+
else {
166+
if ( Pin >= A0 ) { analogWrite(Pin, _Value); }
167+
else { digitalWrite(Pin, _Value); }
168+
}
169+
}
170+
171+
// Drive Remote Pin
172+
else {
173+
if ( XBee == NULL ) return;
174+
if ( Device != XBee->TargetArduinoID() ) XBee->TargetArduinoID( Device );
175+
if ( Pin >= A0 ) { XBee->analogWriteB(Pin, _Value); } // Set the Remote Arduino Analog Pin
176+
else { XBee->digitalWriteB(Pin, _Value); } // Set the Remote Arduino Digital Pin
177+
}
178+
}
179+
180+
/******************************************************************************************************************//**
181+
* @brief Use a user-defined function in the main sketch to modify the raw value read from the arduino
182+
* @remarks
183+
* - Raw values are still used behind the scene's only the displayed value is modified
184+
* @code
185+
* exmaple code
186+
* @endcode
187+
**********************************************************************************************************************/
188+
void PinPoint::AttachValueModifier(int (*_ValueModifierCallback)(int)) {
189+
ValueModifierCallback = _ValueModifierCallback;
190+
}
191+
192+
/******************************************************************************************************************//**
193+
* @brief Adds a 'UserControl' to the Controls Link-List
194+
* @remarks
195+
* @code
196+
* exmaple code
197+
* @endcode
198+
**********************************************************************************************************************/
199+
void PinPoint::Controls(PinPoint* _OutPin, eControlType _ControlType, char _ID, PinPoint* _StorePin = NULL) {
200+
if ( FirstControl == NULL ) {
201+
FirstControl = new UserControl( this, _ControlType, _OutPin, _ID, _StorePin );
202+
LastEpromOffset = FirstControl->SetEpromOffset(LastEpromOffset);
203+
} else {
204+
UserControl *thisControl = FirstControl;
205+
while ( thisControl->Next != NULL ) { thisControl = thisControl->Next; }
206+
thisControl->Next = new UserControl( this, _ControlType, _OutPin, _ID, _StorePin );
207+
LastEpromOffset = thisControl->Next->SetEpromOffset(LastEpromOffset);
208+
thisControl->Next->Prev = thisControl;
209+
}
210+
}
211+
212+

0 commit comments

Comments
 (0)