1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* Octave arduino ultrasonic interface
* Copyright (C) 2019 John Donoghue <john.donoghue@ieee.org>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "settings.h"
#include "OctaveUltrasonicLibrary.h"
#define ARDUINO_CONFIG_ULTRASONIC 1
#define ARDUINO_READ_ULTRASONIC 2
static const char ERRORMSG_CANT_READ[] PROGMEM = "Max ultrasonics reached";
#ifdef USE_ULTRASONIC
#define MAX_ULTRASONICS 4
class Ultrasonic
{
#define USED 1
public:
uint8_t flags;
uint8_t pins[2];
uint8_t state;
Ultrasonic ();
uint8_t init (uint8_t p1, uint8_t p2);
uint8_t free () { flags = 0; return 0;}
uint32_t read ();
};
Ultrasonic::Ultrasonic ()
{
flags = 0;
}
uint8_t
Ultrasonic::init (uint8_t p1, uint8_t p2=0xff)
{
flags = USED;
pins[0] = p1;
pins[1] = p2;
if(p1 == p2)
pins[1] = 0xff;
pinMode (pins[0], OUTPUT);
digitalWrite (pins[0], LOW);
if (pins[1] != 0xff)
{
pinMode (pins[1], INPUT);
digitalWrite (pins[1], LOW);
}
return 0;
}
uint32_t
Ultrasonic::read ()
{
uint32_t duration;
digitalWrite (pins[0], LOW);
delayMicroseconds (5);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite (pins[0], HIGH);
delayMicroseconds (10);
digitalWrite (pins[0], LOW);
if (pins[1] == 0xff)
{
pinMode (pins[0], INPUT);
duration = pulseIn(pins[0], HIGH, 250);
pinMode (pins[0], OUTPUT);
}
else
{
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn (pins[1], HIGH); //, 250);
}
return duration;
}
static Ultrasonic ultrasonics[MAX_ULTRASONICS];
Ultrasonic *
getUltrasonic (uint8_t id)
{
uint8_t i;
Ultrasonic * unused = 0;
for (i=0; i<MAX_ULTRASONICS; i++)
{
if (ultrasonics[i].flags)
{
if (ultrasonics[i].pins[0] == id)
return &ultrasonics[i];
}
else if (!unused)
{
unused = &ultrasonics[i];
}
}
return unused;
}
#endif
OctaveUltrasonicLibrary::OctaveUltrasonicLibrary (OctaveArduinoClass &oc)
{
libName = "Ultrasonic";
oc.registerLibrary (this);
}
void
OctaveUltrasonicLibrary::commandHandler (uint8_t cmdID, uint8_t* data, uint8_t datasz)
{
switch (cmdID)
{
#ifdef USE_ULTRASONIC
case ARDUINO_CONFIG_ULTRASONIC:
{
// 0 = id/triggerpin
// 1 - enable/alloc
// 2 = echo pin (optional)
Ultrasonic * reg = getUltrasonic (data[0]);
if (reg)
{
// alloc
if (data[1] == 1 && datasz == 2)
{
reg->init (data[0]);
sendResponseMsg (cmdID,data, 2);
}
else if(data[1] == 1 && datasz == 3)
{
reg->init (data[0], data[2]);
sendResponseMsg (cmdID,data, 2);
}
// free
else if (data[1] == 0 && reg->flags && datasz == 2)
{
reg->free ();
sendResponseMsg(cmdID,data, 2);
}
else
{
sendErrorMsg_P (ERRORMSG_INVALID_ARGS);
}
}
else
{
sendErrorMsg_P (ERRORMSG_INVALID_ARGS);
}
break;
}
case ARDUINO_READ_ULTRASONIC:
{
// 0 = id
Ultrasonic * reg = getUltrasonic (data[0]);
if (reg && datasz == 1)
{
uint32_t v = reg->read ();
data[1] = (v>>24)&0xff;
data[2] = (v>>16)&0xff;
data[3] = (v>>8)&0xff;
data[4] = (v)&0xff;
datasz = 5;
sendResponseMsg (cmdID, data, datasz);
}
else
{
sendErrorMsg_P (ERRORMSG_INVALID_ARGS);
}
break;
}
#endif
default:
sendUnknownCmdIDMsg ();
break;
}
}
|