Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatUltrasonicDistanceSensor.h
Go to the documentation of this file.
1#pragma once
2/*
3Copyright 2020-2023 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6the United States. See SerialWombat.com for usage guidance.
7
8Permission 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
15The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18THE 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
53{
54public:
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;
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
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;
165private:
166
167};
@ PIN_MODE_ULTRASONIC_DISTANCE
(27)
SerialWombatAbstractProcessedInput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
SerialWombatChip & _sw
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
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.
@ HC_SR04
Standard buffered mode. Colors are uploaded by the host.
SerialWombatUltrasonicDistanceSensor(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseTimer.
uint16_t readPulseCount()
get the number of pulses that have been sent.
int16_t manualTrigger()
Manually trigger a distance reading.
int16_t begin(uint8_t echoPin, driver driver, uint8_t triggerPin, bool autoTrigger=true, bool pullUp=false)
Initialization routine for SerialWombatUltrasonicDistanceSensor.
int16_t readServoSweepEntries(uint16_t *entries, uint16_t count)