Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatUltrasonicDistanceSensor.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-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
34
51
52 class SerialWombatUltrasonicDistanceSensor : public SerialWombatPin, public SerialWombatAbstractProcessedInput
53{
54 public:
57 SerialWombatUltrasonicDistanceSensor(SerialWombatChip& serialWombat): SerialWombatPin(serialWombat),SerialWombatAbstractProcessedInput(serialWombat)
58 {}
59
60 enum driver {
61 HC_SR04 = 0,
62 };
63
64
71 int16_t begin(uint8_t echoPin, driver driver, uint8_t triggerPin,bool autoTrigger = true, bool pullUp = false)
72 {
73 _pin = echoPin;
74 _pinMode = PIN_MODE_ULTRASONIC_DISTANCE;
75
76 uint8_t tx[] = { 200,_pin,_pinMode,(uint8_t)driver, triggerPin, (uint8_t)pullUp,(uint8_t)autoTrigger, 0x55 };
77 return(_sw.sendPacket(tx));
78 }
79
83 uint16_t readPulseCount()
84 {
85
86 uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55, 0x55 };
87 uint8_t rx[8];
88 if(_sw.sendPacket(tx,rx) >= 0)
89 {
90 return (rx[5] + 256 * rx[6]);
91 }
92 else
93 {
94 return 0;
95 }
96
97
98 }
99
104 int16_t manualTrigger()
105 {
106 uint8_t tx[] = { 201,_pin,_pinMode,1,0x55,0x55,0x55, 0x55 };
107
108 return _sw.sendPacket(tx);
109 }
110
112
113 int16_t configureServoSweep(uint8_t servoPin,uint16_t memoryIndex,uint16_t servoPositions_,uint16_t servoIncrement, bool reverse = false, uint16_t servoMoveDelay = 100, uint16_t servoReturnDelay = 1000)
114 {
115 {
116 uint8_t tx[] = { 203, _pin, _pinMode, servoPin, (uint8_t)(memoryIndex & 0xFF), (uint8_t)(memoryIndex >>8),
117 (uint8_t)(servoPositions_ & 0xFF),(uint8_t)(servoPositions_ >>8)};
118 int16_t result = _sw.sendPacket(tx);
119 if (result < 0) { return result; }
120 }
121 {
122 uint8_t tx[] = {204, _pin, _pinMode, (uint8_t)(servoIncrement & 0xFF), (uint8_t)(servoIncrement >> 8),
123 0x55,0x55, (uint8_t)(reverse?1:0)};
124 int16_t result = _sw.sendPacket(tx);
125 if (result < 0) { return result; }
126 }
127 {
128 uint8_t tx[] = {205, _pin, _pinMode, (uint8_t)(servoMoveDelay & 0xFF), (uint8_t)(servoMoveDelay >> 8),
129 (uint8_t)(servoReturnDelay & 0xFF),(uint8_t)(servoReturnDelay >>8), 0x55};
130 int16_t result = _sw.sendPacket(tx);
131 if (result < 0) { return result; }
132 }
133 servoMemoryIndex = memoryIndex;
134 servoPositions = servoPositions_;
135 return 0;
136 }
137 int16_t enableServoSweep(bool enable = true)
138 {
139 uint8_t tx[] = {206, _pin, _pinMode, (uint8_t)(enable ? 1 : 0),0x55,0x55,0x55, 0x55};
140 return _sw.sendPacket(tx);
141 }
142
143 uint16_t readServoSweepEntry(uint16_t servoSweepEntry)
144 {
145 uint8_t b[2];
146 _sw.readUserBuffer((uint16_t)(servoMemoryIndex + 2 * servoSweepEntry), b, 2);
147 return ((uint16_t)(b[0] + 256 * b[1]));
148 }
149
150 int16_t readServoSweepEntries(uint16_t* entries, uint16_t count)
151 {
152 return _sw.readUserBuffer(servoMemoryIndex,(uint8_t*) entries,(uint16_t)( 2 * count));
153
154 }
155
157 uint8_t pin() { return _pin; }
159 uint8_t swPinModeNumber() { return _pinMode; }
160
161 uint16_t servoMemoryIndex = 0;
162 uint16_t servoPositions = 1;
163 private:
164
165};
SerialWombat.h
PIN_MODE_ULTRASONIC_DISTANCE
@ PIN_MODE_ULTRASONIC_DISTANCE
(27)
Definition SerialWombat.h:277
SerialWombatAbstractProcessedInput::SerialWombatAbstractProcessedInput
SerialWombatAbstractProcessedInput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractProcessedInput.h:87
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:300
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:206
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:205
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:207
SerialWombatUltrasonicDistanceSensor::configureServoSweep
int16_t configureServoSweep(uint8_t servoPin, uint16_t memoryIndex, uint16_t servoPositions_, uint16_t servoIncrement, bool reverse=false, uint16_t servoMoveDelay=100, uint16_t servoReturnDelay=1000)
Configure a Servo sweep of Ultrasonic Distance Sensor.
Definition SerialWombatUltrasonicDistanceSensor.h:113
SerialWombatUltrasonicDistanceSensor::driver
driver
Definition SerialWombatUltrasonicDistanceSensor.h:60
SerialWombatUltrasonicDistanceSensor::HC_SR04
@ HC_SR04
Standard buffered mode. Colors are uploaded by the host.
Definition SerialWombatUltrasonicDistanceSensor.h:61
SerialWombatUltrasonicDistanceSensor::readServoSweepEntry
uint16_t readServoSweepEntry(uint16_t servoSweepEntry)
Definition SerialWombatUltrasonicDistanceSensor.h:143
SerialWombatUltrasonicDistanceSensor::pin
uint8_t pin()
Facilitates Inheritance.
Definition SerialWombatUltrasonicDistanceSensor.h:157
SerialWombatUltrasonicDistanceSensor::swPinModeNumber
uint8_t swPinModeNumber()
Facilitates Inheritance.
Definition SerialWombatUltrasonicDistanceSensor.h:159
SerialWombatUltrasonicDistanceSensor::SerialWombatUltrasonicDistanceSensor
SerialWombatUltrasonicDistanceSensor(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseTimer.
Definition SerialWombatUltrasonicDistanceSensor.h:57
SerialWombatUltrasonicDistanceSensor::enableServoSweep
int16_t enableServoSweep(bool enable=true)
Definition SerialWombatUltrasonicDistanceSensor.h:137
SerialWombatUltrasonicDistanceSensor::servoPositions
uint16_t servoPositions
Definition SerialWombatUltrasonicDistanceSensor.h:162
SerialWombatUltrasonicDistanceSensor::readPulseCount
uint16_t readPulseCount()
get the number of pulses that have been sent.
Definition SerialWombatUltrasonicDistanceSensor.h:83
SerialWombatUltrasonicDistanceSensor::manualTrigger
int16_t manualTrigger()
Manually trigger a distance reading.
Definition SerialWombatUltrasonicDistanceSensor.h:104
SerialWombatUltrasonicDistanceSensor::begin
int16_t begin(uint8_t echoPin, driver driver, uint8_t triggerPin, bool autoTrigger=true, bool pullUp=false)
Initialization routine for SerialWombatUltrasonicDistanceSensor.
Definition SerialWombatUltrasonicDistanceSensor.h:71
SerialWombatUltrasonicDistanceSensor::readServoSweepEntries
int16_t readServoSweepEntries(uint16_t *entries, uint16_t count)
Definition SerialWombatUltrasonicDistanceSensor.h:150
SerialWombatUltrasonicDistanceSensor::servoMemoryIndex
uint16_t servoMemoryIndex
Definition SerialWombatUltrasonicDistanceSensor.h:161