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
73 int16_t begin(uint8_t echoPin, driver driver, uint8_t triggerPin,bool autoTrigger = true, bool pullUp = false)
74 {
75 _pin = echoPin;
76 _pinMode = PIN_MODE_ULTRASONIC_DISTANCE;
77
78 uint8_t tx[] = { 200,_pin,_pinMode,(uint8_t)driver, triggerPin, (uint8_t)pullUp,(uint8_t)autoTrigger, 0x55 };
79 return(_sw.sendPacket(tx));
80 }
81
85 uint16_t readPulseCount()
86 {
87
88 uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55, 0x55 };
89 uint8_t rx[8];
90 if(_sw.sendPacket(tx,rx) >= 0)
91 {
92 return (rx[5] + 256 * rx[6]);
93 }
94 else
95 {
96 return 0;
97 }
98
99
100 }
101
106 int16_t manualTrigger()
107 {
108 uint8_t tx[] = { 201,_pin,_pinMode,1,0x55,0x55,0x55, 0x55 };
109
110 return _sw.sendPacket(tx);
111 }
112
114
115 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)
116 {
117 {
118 uint8_t tx[] = { 203, _pin, _pinMode, servoPin, (uint8_t)(memoryIndex & 0xFF), (uint8_t)(memoryIndex >>8),
119 (uint8_t)(servoPositions_ & 0xFF),(uint8_t)(servoPositions_ >>8)};
120 int16_t result = _sw.sendPacket(tx);
121 if (result < 0) { return result; }
122 }
123 {
124 uint8_t tx[] = {204, _pin, _pinMode, (uint8_t)(servoIncrement & 0xFF), (uint8_t)(servoIncrement >> 8),
125 0x55,0x55, (uint8_t)(reverse?1:0)};
126 int16_t result = _sw.sendPacket(tx);
127 if (result < 0) { return result; }
128 }
129 {
130 uint8_t tx[] = {205, _pin, _pinMode, (uint8_t)(servoMoveDelay & 0xFF), (uint8_t)(servoMoveDelay >> 8),
131 (uint8_t)(servoReturnDelay & 0xFF),(uint8_t)(servoReturnDelay >>8), 0x55};
132 int16_t result = _sw.sendPacket(tx);
133 if (result < 0) { return result; }
134 }
135 servoMemoryIndex = memoryIndex;
136 servoPositions = servoPositions_;
137 return 0;
138 }
139 int16_t enableServoSweep(bool enable = true)
140 {
141 uint8_t tx[] = {206, _pin, _pinMode, (uint8_t)(enable ? 1 : 0),0x55,0x55,0x55, 0x55};
142 return _sw.sendPacket(tx);
143 }
144
145 uint16_t readServoSweepEntry(uint16_t servoSweepEntry)
146 {
147 uint8_t b[2];
148 _sw.readUserBuffer((uint16_t)(servoMemoryIndex + 2 * servoSweepEntry), b, 2);
149 return ((uint16_t)(b[0] + 256 * b[1]));
150 }
151
152 int16_t readServoSweepEntries(uint16_t* entries, uint16_t count)
153 {
154 return _sw.readUserBuffer(servoMemoryIndex,(uint8_t*) entries,(uint16_t)( 2 * count));
155
156 }
157
159 uint8_t pin() { return _pin; }
161 uint8_t swPinModeNumber() { return _pinMode; }
162
163 uint16_t servoMemoryIndex = 0;
164 uint16_t servoPositions = 1;
165 private:
166
167};
SerialWombat.h
PIN_MODE_ULTRASONIC_DISTANCE
@ PIN_MODE_ULTRASONIC_DISTANCE
(27)
Definition SerialWombat.h:285
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:313
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215
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:115
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:145
SerialWombatUltrasonicDistanceSensor::pin
uint8_t pin()
Facilitates Inheritance.
Definition SerialWombatUltrasonicDistanceSensor.h:159
SerialWombatUltrasonicDistanceSensor::swPinModeNumber
uint8_t swPinModeNumber()
Facilitates Inheritance.
Definition SerialWombatUltrasonicDistanceSensor.h:161
SerialWombatUltrasonicDistanceSensor::SerialWombatUltrasonicDistanceSensor
SerialWombatUltrasonicDistanceSensor(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseTimer.
Definition SerialWombatUltrasonicDistanceSensor.h:57
SerialWombatUltrasonicDistanceSensor::enableServoSweep
int16_t enableServoSweep(bool enable=true)
Definition SerialWombatUltrasonicDistanceSensor.h:139
SerialWombatUltrasonicDistanceSensor::servoPositions
uint16_t servoPositions
Definition SerialWombatUltrasonicDistanceSensor.h:164
SerialWombatUltrasonicDistanceSensor::readPulseCount
uint16_t readPulseCount()
get the number of pulses that have been sent.
Definition SerialWombatUltrasonicDistanceSensor.h:85
SerialWombatUltrasonicDistanceSensor::manualTrigger
int16_t manualTrigger()
Manually trigger a distance reading.
Definition SerialWombatUltrasonicDistanceSensor.h:106
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:73
SerialWombatUltrasonicDistanceSensor::readServoSweepEntries
int16_t readServoSweepEntries(uint16_t *entries, uint16_t count)
Definition SerialWombatUltrasonicDistanceSensor.h:152
SerialWombatUltrasonicDistanceSensor::servoMemoryIndex
uint16_t servoMemoryIndex
Definition SerialWombatUltrasonicDistanceSensor.h:163