Serial Wombat Arduino Library
SerialWombatPulseOnChange.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 Copyright 2022-2023 Broadwell Consulting Inc.
4 
5 "Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25 */
26 
27 
28 #include <stdint.h>
29 #include "SerialWombat.h"
30 
31 class SerialWombat;
32 
83 {
84 public:
90  {
92  }
93 
94 
105  int16_t begin(uint8_t pin, SerialWombatPinState_t activeMode = SW_HIGH, SerialWombatPinState_t inactiveMode = SW_LOW, uint16_t pulseOnTime = 50, uint16_t pulseOffTime = 50, uint8_t orNotAnd = 1, uint16_t PWMperiod = 0, uint16_t PWMdutyCycle = 0x8000)
106 {
107  int16_t result;
108  _pin = pin;
109 
110  {
111  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,
112  _pin,
113  _pinMode,
114  (uint8_t)activeMode,
115  (uint8_t)inactiveMode,
116  orNotAnd,
117  0x55,
118  0x55
119  };
120  result = _sw.sendPacket(tx);
121  if (result < 0) { return result; }
122  }
123  {
124  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE1,
125  _pin,
126  _pinMode,
127  SW_LE16(pulseOnTime),
128  SW_LE16(pulseOffTime),
129  0x55
130  };
131  result = _sw.sendPacket(tx);
132  if (result < 0) { return result; }
133  }
134  {
135  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE2,
136  _pin,
137  _pinMode,
138  SW_LE16(PWMperiod),
139  SW_LE16(PWMdutyCycle),
140  0x55
141  };
142  result = _sw.sendPacket(tx);
143  if (result < 0) { return result; }
144  }
145  return(0);
146 }
155  int16_t setEntryOnChange(uint8_t entryID, uint8_t sourcePin)
156 {
157  return setEntryMode(entryID, sourcePin, 0);
158 }
167  int16_t setEntryOnIncrease(uint8_t entryID, uint8_t sourcePin)
168 {
169  return setEntryMode(entryID, sourcePin, 1);
170 }
179  int16_t setEntryOnDecrease(uint8_t entryID, uint8_t sourcePin)
180 {
181  return setEntryMode(entryID, sourcePin, 2);
182 }
192  int16_t setEntryOnEqualValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
193 {
194  int16_t result = setEntryParams(entryID, value, 0); if (result < 0) return result;
195  return setEntryMode(entryID, sourcePin, 3);
196 }
206  int16_t setEntryOnLessThanValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
207  {
208  int16_t result = setEntryParams(entryID, value, 0); if (result < 0) return result;
209  return setEntryMode(entryID, sourcePin, 4);
210  }
220  int16_t setEntryOnGreaterThanValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
221  {
222  int16_t result = setEntryParams(entryID, value, 0); if (result < 0) return result;
223  return setEntryMode(entryID, sourcePin, 5);
224  }
225 
226 
236  int16_t setEntryOnNotEqualValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
237  {
238  int16_t result = setEntryParams(entryID, value, 0); if (result < 0) return result;
239  return setEntryMode(entryID, sourcePin, 6);
240  }
250  int16_t setEntryOnPinsEqual(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin )
251  {
252  int16_t result = setEntryParams(entryID, secondPin, 0); if (result < 0) return result;
253  return setEntryMode(entryID, sourcePin, 7);
254  }
255 
265  int16_t setEntryOnPinsNotEqual(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin )
266  {
267  int16_t result = setEntryParams(entryID, secondPin, 0); if (result < 0) return result;
268  return setEntryMode(entryID, sourcePin, 10);
269  }
270 
280  int16_t setEntryOnPinGTPin(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin );
281 
291  int16_t setEntryOnPinLTPin(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin );
301  int16_t setEntryOnPinCrossValue(uint8_t entryID, uint8_t sourcePin, uint16_t value);
302 
312  int16_t setEntryOnPinCrossAscending(uint8_t entryID, uint8_t sourcePin, uint16_t value);
313 
323  int16_t setEntryOnPinCrossDescending(uint8_t entryID, uint8_t sourcePin, uint16_t value);
324 
335  int16_t setEntryOnPinWithinRange(uint8_t entryID, uint8_t sourcePin, uint16_t lowValue, uint16_t highValue);
336 
347  int16_t setEntryOnPinOutsideRange(uint8_t entryID, uint8_t sourcePin, uint16_t lowValue, uint16_t highValue);
348 private:
349  int16_t setEntryParams(uint8_t entryID, uint16_t firstParam, uint16_t secondParam)
350 {
351  {
352  int16_t result;
353  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE3,
354  _pin,
355  _pinMode,
356  entryID,
357  SW_LE16(firstParam),
358  SW_LE16(secondParam),
359  };
360  result = _sw.sendPacket(tx);
361  if (result < 0) { return result; }
362  }
363  return 0;
364 }
365  int16_t setEntryMode(uint8_t entryID, uint8_t pin, uint8_t mode)
366 {
367  {
368  int16_t result;
369  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE4,
370  _pin,
371  _pinMode,
372  entryID,
373  mode,
374  pin,
375 
376  0x55,
377  0x55
378  };
379  result = _sw.sendPacket(tx);
380  if (result < 0) { return result; }
381  }
382  return 0;
383 }
384 };
385 
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:286
PIN_MODE_PULSE_ON_CHANGE
@ PIN_MODE_PULSE_ON_CHANGE
(25)
Definition: SerialWombat.h:264
SerialWombatCommands::CONFIGURE_PIN_MODE0
@ CONFIGURE_PIN_MODE0
(200)
SerialWombatPulseOnChange::setEntryOnPinsNotEqual
int16_t setEntryOnPinsNotEqual(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin)
Configure a change entry to pulse when a pin or public data is not equal to a second pin or public da...
Definition: SerialWombatPulseOnChange.h:265
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombatPin.h:134
SerialWombatCommands::CONFIGURE_PIN_MODE3
@ CONFIGURE_PIN_MODE3
(203)
SerialWombatPulseOnChange::setEntryOnPinCrossDescending
int16_t setEntryOnPinCrossDescending(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data falls below a specified value.
SerialWombatPulseOnChange::setEntryOnPinWithinRange
int16_t setEntryOnPinWithinRange(uint8_t entryID, uint8_t sourcePin, uint16_t lowValue, uint16_t highValue)
Configure a change entry to pulse when a pin or public data is within a specified range.
SerialWombatPulseOnChange::setEntryOnPinOutsideRange
int16_t setEntryOnPinOutsideRange(uint8_t entryID, uint8_t sourcePin, uint16_t lowValue, uint16_t highValue)
Configure a change entry to pulse when a pin or public data is within a specified range.
SerialWombatPulseOnChange::setEntryOnLessThanValue
int16_t setEntryOnLessThanValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data is below a specified value.
Definition: SerialWombatPulseOnChange.h:206
SerialWombatPulseOnChange::setEntryOnNotEqualValue
int16_t setEntryOnNotEqualValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data is not equal to a specified value.
Definition: SerialWombatPulseOnChange.h:236
SerialWombatCommands::CONFIGURE_PIN_MODE4
@ CONFIGURE_PIN_MODE4
(204)
SerialWombat.h
SW_HIGH
@ SW_HIGH
Definition: SerialWombat.h:50
SerialWombatPulseOnChange::setEntryOnDecrease
int16_t setEntryOnDecrease(uint8_t entryID, uint8_t sourcePin)
Configure a change entry to pulse when a pin or public data Decreases.
Definition: SerialWombatPulseOnChange.h:179
SerialWombatPulseOnChange::setEntryOnPinGTPin
int16_t setEntryOnPinGTPin(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin)
Configure a change entry to pulse when a pin or public data is greater than a second pin or public da...
SerialWombatPulseOnChange::setEntryOnEqualValue
int16_t setEntryOnEqualValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data equals a specified value.
Definition: SerialWombatPulseOnChange.h:192
SerialWombatPulseOnChange::SerialWombatPulseOnChange
SerialWombatPulseOnChange(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseOnChange.
Definition: SerialWombatPulseOnChange.h:89
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombatPin.h:38
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition: SerialWombatPin.h:135
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:115
SerialWombatPulseOnChange::setEntryOnChange
int16_t setEntryOnChange(uint8_t entryID, uint8_t sourcePin)
Configure a change entry to pulse when a pin or public data changes.
Definition: SerialWombatPulseOnChange.h:155
SerialWombatPulseOnChange::setEntryOnGreaterThanValue
int16_t setEntryOnGreaterThanValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data is above a specified value.
Definition: SerialWombatPulseOnChange.h:220
SerialWombatPinState_t
SerialWombatPinState_t
Definition: SerialWombat.h:47
SerialWombatPulseOnChange::begin
int16_t begin(uint8_t pin, SerialWombatPinState_t activeMode=SW_HIGH, SerialWombatPinState_t inactiveMode=SW_LOW, uint16_t pulseOnTime=50, uint16_t pulseOffTime=50, uint8_t orNotAnd=1, uint16_t PWMperiod=0, uint16_t PWMdutyCycle=0x8000)
Initialization routine for SerialWombatPulseOnChange.
Definition: SerialWombatPulseOnChange.h:105
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition: SerialWombatPin.h:121
SerialWombat
This class name is depricated. Do not use for new development. Use SerialWombatChip instead.
Definition: SerialWombat.h:1522
SW_LOW
@ SW_LOW
Definition: SerialWombat.h:49
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombatPin.h:133
SerialWombatPulseOnChange
Monitors other pin(s) or public data in the Serial Wombat chip and generates a pin pulse on change.
Definition: SerialWombatPulseOnChange.h:82
SerialWombatCommands::CONFIGURE_PIN_MODE2
@ CONFIGURE_PIN_MODE2
(202)
SerialWombatPulseOnChange::setEntryOnPinsEqual
int16_t setEntryOnPinsEqual(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin)
Configure a change entry to pulse when a pin or public data equals a second pin or public data's valu...
Definition: SerialWombatPulseOnChange.h:250
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition: SerialWombat.h:41
SerialWombatPulseOnChange::setEntryOnPinCrossAscending
int16_t setEntryOnPinCrossAscending(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data rises above a specified value.
SerialWombatPulseOnChange::setEntryOnPinCrossValue
int16_t setEntryOnPinCrossValue(uint8_t entryID, uint8_t sourcePin, uint16_t value)
Configure a change entry to pulse when a pin or public data crosses a specified value.
SerialWombatCommands::CONFIGURE_PIN_MODE1
@ CONFIGURE_PIN_MODE1
(201)
SerialWombatPulseOnChange::setEntryOnPinLTPin
int16_t setEntryOnPinLTPin(uint8_t entryID, uint8_t sourcePin, uint8_t secondPin)
Configure a change entry to pulse when a pin or public data is less than a second pin or public data'...
SerialWombatPulseOnChange::setEntryOnIncrease
int16_t setEntryOnIncrease(uint8_t entryID, uint8_t sourcePin)
Configure a change entry to pulse when a pin or public data increases.
Definition: SerialWombatPulseOnChange.h:167