├── README.rst ├── keywords.txt ├── LICENSE ├── examples ├── Simple │ └── Simple.ino └── Advanced │ └── Advanced.ino ├── Maxbotix.h ├── RxSoftwareSerial.h.disabled ├── Maxbotix.cpp └── RxSoftwareSerial.cpp.disabled /README.rst: -------------------------------------------------------------------------------- 1 | Maxbotix 2 | ======== 3 | An Arduino library to read Maxbotix ultrasonic sensors. https://github.com/Diaoul/arduino-Maxbotix 4 | 5 | History 6 | ======= 7 | 0.1 8 | --- 9 | 10 | * Initial version 11 | 12 | About 13 | ===== 14 | Author: Antoine Bertin 15 | 16 | License: MIT 17 | 18 | Code formated with: ``astyle --break-blocks --remove-brackets`` 19 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | # Class 2 | Maxbotix KEYWORD1 3 | 4 | # Functions 5 | getRange KEYWORD2 6 | getSampleSize KEYWORD2 7 | readSample KEYWORD2 8 | getSample KEYWORD2 9 | getSampleMedian KEYWORD2 10 | getSampleMode KEYWORD2 11 | getSampleBest KEYWORD2 12 | getModel KEYWORD2 13 | getInput KEYWORD2 14 | getFilter KEYWORD2 15 | setADSampleDelay KEYWORD2 16 | toCentimeters KEYWORD2 17 | toInches KEYWORD2 18 | 19 | # Enums 20 | LV LITERAL1 21 | XL LITERAL1 22 | HRLV LITERAL1 23 | PW LITERAL1 24 | AN LITERAL1 25 | TX LITERAL1 26 | NONE LITERAL1 27 | MEDIAN LITERAL1 28 | HIGHEST_MODE LITERAL1 29 | LOWEST_MODE LITERAL1 30 | BEST LITERAL1 31 | SIMPLE LITERAL1 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Antoine Bertin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /examples/Simple/Simple.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Maxbotix simple test 3 | 4 | Instructions: 5 | - At least one of: (comment the appropriate code below) 6 | * PW is digital pin 8 7 | * TX is digital pin 6 8 | * AN is analog pin A0 9 | - Change code below according to your model (LV, XL and 10 | HRLV supported) 11 | 12 | Note: 13 | For convenience, the getRange method will always return centimeters. 14 | You can use convert fuctions to convert to another unit (toInches and 15 | toCentimeters are available) 16 | 17 | */ 18 | #include "Maxbotix.h" 19 | 20 | Maxbotix rangeSensorPW(8, Maxbotix::PW, Maxbotix::LV); 21 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 22 | Maxbotix rangeSensorTX(6, Maxbotix::TX, Maxbotix::LV); 23 | #endif 24 | Maxbotix rangeSensorAD(A0, Maxbotix::AN, Maxbotix::LV); 25 | 26 | void setup() 27 | { 28 | Serial.begin(9600); 29 | } 30 | 31 | void loop() 32 | { 33 | unsigned long start; 34 | 35 | Serial.println("Reading..."); 36 | 37 | // PW 38 | start = millis(); 39 | Serial.print("PW: "); 40 | Serial.print(rangeSensorPW.getRange()); 41 | Serial.print("cm - "); 42 | Serial.print(millis() - start); 43 | Serial.println("ms"); 44 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 45 | // TX 46 | start = millis(); 47 | Serial.print("TX: "); 48 | Serial.print(rangeSensorTX.getRange()); 49 | Serial.print("cm - "); 50 | Serial.print(millis() - start); 51 | Serial.println("ms"); 52 | #endif 53 | // AD 54 | start = millis(); 55 | Serial.print("AD: "); 56 | Serial.print(rangeSensorAD.getRange()); 57 | Serial.print("cm - "); 58 | Serial.print(millis() - start); 59 | Serial.println("ms"); 60 | 61 | Serial.println(); 62 | delay(5000); 63 | } 64 | -------------------------------------------------------------------------------- /Maxbotix.h: -------------------------------------------------------------------------------- 1 | #ifndef Maxbotix_h 2 | #define Maxbotix_h 3 | 4 | // Configuration 5 | //#define MAXBOTIX_WITH_SOFTWARE_SERIAL 1 6 | 7 | #include 8 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 9 | #include "RxSoftwareSerial.h" 10 | #endif 11 | 12 | class Maxbotix 13 | { 14 | public: 15 | // models 16 | typedef enum { 17 | LV, 18 | XL, 19 | HRLV 20 | } 21 | MAXBOTIX_MODEL_t; 22 | 23 | // inputs 24 | typedef enum { 25 | PW, 26 | AN, 27 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 28 | TX 29 | #endif 30 | } 31 | MAXBOTIX_INPUT_t; 32 | 33 | // filters 34 | typedef enum { 35 | NONE, 36 | MEDIAN, 37 | HIGHEST_MODE, 38 | LOWEST_MODE, 39 | BEST, 40 | SIMPLE 41 | } 42 | MAXBOTIX_FILTER_t; 43 | 44 | // init/delete 45 | Maxbotix(uint8_t pin, MAXBOTIX_INPUT_t input, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter = NONE, 46 | uint8_t sample_size = 0); 47 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 48 | Maxbotix(Stream* serial, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter = NONE, uint8_t sample_size = 0); 49 | #endif 50 | ~Maxbotix(); 51 | 52 | // simple api 53 | float getRange(); 54 | uint8_t getSampleSize() 55 | { 56 | return sample_size; 57 | }; 58 | 59 | // advanced api 60 | void readSample(); 61 | float* getSample() 62 | { 63 | return sample; 64 | }; 65 | float getSampleMedian(); 66 | float getSampleMode(bool highest = true); 67 | float getSampleBest(); 68 | 69 | // getters 70 | MAXBOTIX_MODEL_t getModel() 71 | { 72 | return model; 73 | } 74 | MAXBOTIX_INPUT_t getInput() 75 | { 76 | return input; 77 | } 78 | MAXBOTIX_FILTER_t getFilter() 79 | { 80 | return filter; 81 | } 82 | 83 | // setters 84 | void setADSampleDelay(uint8_t delay) 85 | { 86 | ad_sample_delay = delay; 87 | } 88 | 89 | // utilities 90 | static float toCentimeters(float inches) 91 | { 92 | return 2.54 * inches; 93 | }; 94 | static float toInches(float centimeters) 95 | { 96 | return centimeters / 2.54; 97 | }; 98 | 99 | private: 100 | // config variables 101 | uint8_t pin; 102 | MAXBOTIX_INPUT_t input; 103 | MAXBOTIX_MODEL_t model; 104 | MAXBOTIX_FILTER_t filter; 105 | uint8_t sample_size; 106 | uint8_t ad_sample_delay; 107 | Stream* serial; 108 | 109 | // core 110 | float* sample; 111 | void init(); 112 | float readSensor(); 113 | unsigned short readSensorSerial(uint8_t length); 114 | void pushToSample(float value); 115 | void sortSample(); 116 | }; 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /RxSoftwareSerial.h.disabled: -------------------------------------------------------------------------------- 1 | /* 2 | RxSoftwareSerial.h (Rx-only SoftwareSerial.h by Antoine Bertin) 3 | Multi-instance software serial library for Arduino/Wiring 4 | -- Interrupt-driven receive and other improvements by ladyada 5 | (http://ladyada.net) 6 | -- Tuning, circular buffer, derivation from class Print/Stream, 7 | multi-instance support, porting to 8MHz processors, 8 | various optimizations, PROGMEM delay tables, inverse logic and 9 | direct port writing by Mikal Hart (http://www.arduiniana.org) 10 | -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com) 11 | -- 20MHz processor support by Garrett Mace (http://www.macetech.com) 12 | -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/) 13 | 14 | This library is free software; you can redistribute it and/or 15 | modify it under the terms of the GNU Lesser General Public 16 | License as published by the Free Software Foundation; either 17 | version 2.1 of the License, or (at your option) any later version. 18 | 19 | This library is distributed in the hope that it will be useful, 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 | Lesser General Public License for more details. 23 | 24 | You should have received a copy of the GNU Lesser General Public 25 | License along with this library; if not, write to the Free Software 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 27 | 28 | */ 29 | 30 | #ifndef RxSoftwareSerial_h 31 | #define RxSoftwareSerial_h 32 | 33 | #include 34 | #include 35 | 36 | /****************************************************************************** 37 | * Definitions 38 | ******************************************************************************/ 39 | 40 | #define _SS_MAX_RX_BUFF 64 // RX buffer size 41 | #ifndef GCC_VERSION 42 | #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 43 | #endif 44 | 45 | class RxSoftwareSerial : public Stream 46 | { 47 | private: 48 | // per object data 49 | uint8_t _receivePin; 50 | uint8_t _receiveBitMask; 51 | volatile uint8_t *_receivePortRegister; 52 | 53 | uint16_t _rx_delay_centering; 54 | uint16_t _rx_delay_intrabit; 55 | uint16_t _rx_delay_stopbit; 56 | uint16_t _tx_delay; 57 | 58 | uint16_t _buffer_overflow:1; 59 | uint16_t _inverse_logic:1; 60 | 61 | // static data 62 | static char _receive_buffer[_SS_MAX_RX_BUFF]; 63 | static volatile uint8_t _receive_buffer_tail; 64 | static volatile uint8_t _receive_buffer_head; 65 | static RxSoftwareSerial *active_object; 66 | 67 | // private methods 68 | void recv(); 69 | uint8_t rx_pin_read(); 70 | void setRX(uint8_t receivePin); 71 | 72 | // private static method for timing 73 | static inline void tunedDelay(uint16_t delay); 74 | 75 | public: 76 | // public methods 77 | RxSoftwareSerial(uint8_t receivePin, bool inverse_logic = false); 78 | ~RxSoftwareSerial(); 79 | void begin(long speed); 80 | bool listen(); 81 | void end(); 82 | bool isListening() { return this == active_object; } 83 | bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; } 84 | int peek(); 85 | 86 | virtual size_t write(uint8_t byte); 87 | virtual int read(); 88 | virtual int available(); 89 | virtual void flush(); 90 | 91 | using Print::write; 92 | 93 | // public only for easy access by interrupt handlers 94 | static inline void handle_interrupt(); 95 | }; 96 | 97 | // Arduino 0012 workaround 98 | #undef int 99 | #undef char 100 | #undef long 101 | #undef byte 102 | #undef float 103 | #undef abs 104 | #undef round 105 | 106 | #endif -------------------------------------------------------------------------------- /examples/Advanced/Advanced.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Maxbotix advanced test 3 | 4 | Instructions: 5 | - Same as simple test 6 | 7 | Filters: 8 | * NONE (default): No filtering 9 | * MEDIAN: Take the median of a sample 10 | * HIGHEST_MODE: Take the mode of a sample. In case more than 11 | one mode, the highest one is returned 12 | * LOWEST_MODE: Take the mode of a sample. In case more than 13 | one mode, the lowest one is returned 14 | * BEST: Take the mode of a sample. In case more than one 15 | mode is found, the median is returned 16 | * SIMPLE: Continue reading until sample_size consecutive readings 17 | are issued 18 | 19 | */ 20 | #include "Maxbotix.h" 21 | 22 | Maxbotix rangeSensorPW(8, Maxbotix::PW, Maxbotix::LV, Maxbotix::BEST); 23 | 24 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 25 | Maxbotix rangeSensorTX(6, Maxbotix::TX, Maxbotix::LV, Maxbotix::MEDIAN); 26 | #endif 27 | 28 | Maxbotix rangeSensorAD(A0, Maxbotix::AN, Maxbotix::LV, Maxbotix::BEST, 9); 29 | 30 | void setup() 31 | { 32 | Serial.begin(9600); 33 | // Set the delay between AD readings to 10ms 34 | rangeSensorAD.setADSampleDelay(10); 35 | } 36 | 37 | void loop() 38 | { 39 | unsigned long start; 40 | 41 | Serial.println("Reading..."); 42 | 43 | // PW 44 | start = millis(); 45 | Serial.print("PW (BEST): "); 46 | Serial.print(rangeSensorPW.getRange()); 47 | Serial.print("cm - "); 48 | Serial.print(millis() - start); 49 | Serial.print("ms - "); 50 | printArray(rangeSensorPW.getSample(), rangeSensorPW.getSampleSize()); 51 | Serial.print(" - Highest Mode: "); 52 | Serial.print(rangeSensorPW.getSampleMode(true)); 53 | Serial.print(" - Lowest Mode: "); 54 | Serial.print(rangeSensorPW.getSampleMode(false)); 55 | Serial.print(" - Median: "); 56 | Serial.print(rangeSensorPW.getSampleMedian()); 57 | Serial.print(" - Best: "); 58 | Serial.print(rangeSensorPW.getSampleBest()); 59 | Serial.println(); 60 | 61 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 62 | // TX 63 | start = millis(); 64 | Serial.print("TX (MEDIAN): "); 65 | Serial.print(rangeSensorTX.getRange()); 66 | Serial.print("cm - "); 67 | Serial.print(millis() - start); 68 | Serial.print("ms - "); 69 | printArray(rangeSensorTX.getSample(), rangeSensorTX.getSampleSize()); 70 | Serial.print(" - Highest Mode: "); 71 | Serial.print(rangeSensorTX.getSampleMode(true)); 72 | Serial.print(" - Lowest Mode: "); 73 | Serial.print(rangeSensorTX.getSampleMode(false)); 74 | Serial.print(" - Median: "); 75 | Serial.print(rangeSensorTX.getSampleMedian()); 76 | Serial.print(" - Best: "); 77 | Serial.print(rangeSensorTX.getSampleBest()); 78 | Serial.println(); 79 | #endif 80 | 81 | // AD 82 | start = millis(); 83 | Serial.print("AD (BEST): "); 84 | Serial.print(rangeSensorAD.getRange()); 85 | Serial.print("cm - "); 86 | Serial.print(millis() - start); 87 | Serial.print("ms - "); 88 | printArray(rangeSensorAD.getSample(), rangeSensorAD.getSampleSize()); 89 | Serial.print(" - Highest Mode: "); 90 | Serial.print(rangeSensorAD.getSampleMode(true)); 91 | Serial.print(" - Lowest Mode: "); 92 | Serial.print(rangeSensorAD.getSampleMode(false)); 93 | Serial.print(" - Median: "); 94 | Serial.print(rangeSensorAD.getSampleMedian()); 95 | Serial.print(" - Best: "); 96 | Serial.print(rangeSensorAD.getSampleBest()); 97 | Serial.println(); 98 | 99 | Serial.println(); 100 | delay(5000); 101 | } 102 | 103 | void printArray(float* array, uint8_t array_size) { 104 | Serial.print("["); 105 | for (int i = 0; i < array_size; i++) { 106 | Serial.print(array[i]); 107 | if (i != array_size - 1) { 108 | Serial.print(", "); 109 | } 110 | } 111 | Serial.print("]"); 112 | } 113 | -------------------------------------------------------------------------------- /Maxbotix.cpp: -------------------------------------------------------------------------------- 1 | #include "Maxbotix.h" 2 | 3 | Maxbotix::Maxbotix(uint8_t pin, MAXBOTIX_INPUT_t input, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter, 4 | uint8_t sample_size) : 5 | pin(pin), input(input), model(model), filter(filter), sample_size(sample_size) 6 | { 7 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 8 | 9 | if (input == TX) { 10 | serial = new RxSoftwareSerial(pin, true); 11 | ((RxSoftwareSerial*)serial)->begin(9600); 12 | } else { 13 | #endif 14 | pinMode(pin, INPUT); 15 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 16 | } 17 | 18 | #endif 19 | init(); 20 | } 21 | 22 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 23 | Maxbotix::Maxbotix(Stream* serial, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter, uint8_t sample_size) : 24 | input(TX), model(model), serial(serial), filter(filter), sample_size(sample_size) 25 | { 26 | init(); 27 | } 28 | #endif 29 | 30 | 31 | void Maxbotix::init() 32 | { 33 | switch (filter) { 34 | case MEDIAN: 35 | case HIGHEST_MODE: 36 | case LOWEST_MODE: 37 | case BEST: 38 | if (sample_size == 0) 39 | sample_size = 5; 40 | else if (sample_size % 2) 41 | sample_size++; 42 | 43 | break; 44 | 45 | case SIMPLE: 46 | if (sample_size == 0) 47 | sample_size = 2; 48 | 49 | break; 50 | 51 | case NONE: 52 | default: 53 | sample_size = 1; 54 | break; 55 | } 56 | 57 | sample = new float[sample_size]; 58 | } 59 | 60 | Maxbotix::~Maxbotix() 61 | { 62 | delete[] sample; 63 | delete serial; 64 | } 65 | 66 | float Maxbotix::getRange() 67 | { 68 | float range; 69 | 70 | readSample(); 71 | 72 | switch (filter) { 73 | case MEDIAN: 74 | range = getSampleMedian(); 75 | break; 76 | 77 | case HIGHEST_MODE: 78 | range = getSampleMode(true); 79 | break; 80 | 81 | case LOWEST_MODE: 82 | range = getSampleMode(false); 83 | break; 84 | 85 | case SIMPLE: 86 | while (sample[0] != sample[sample_size - 1]) 87 | pushToSample(readSensor()); 88 | 89 | range = sample[0]; 90 | break; 91 | 92 | case BEST: 93 | range = getSampleBest(); 94 | break; 95 | 96 | case NONE: 97 | default: 98 | range = sample[0]; 99 | break; 100 | } 101 | 102 | return range; 103 | } 104 | 105 | float Maxbotix::getSampleMedian() 106 | { 107 | return sample[sample_size / 2]; 108 | } 109 | 110 | float Maxbotix::getSampleMode(bool highest) 111 | { 112 | float mode = sample[0]; 113 | uint8_t mode_count = 1; 114 | uint8_t count = 1; 115 | 116 | for (int i = 1; i < sample_size; i++) { 117 | if (sample[i] == sample[i - 1]) 118 | count++; 119 | else 120 | count = 1; 121 | 122 | if (sample[i] == mode) 123 | mode_count++; 124 | else if (!highest && count > mode_count || highest && count == mode_count) { 125 | mode_count = count; 126 | mode = sample[i]; 127 | } 128 | } 129 | 130 | return mode; 131 | } 132 | 133 | float Maxbotix::getSampleBest() 134 | { 135 | float range; 136 | 137 | if ((range = getSampleMode(true)) != getSampleMode(false)) 138 | range = getSampleMedian(); 139 | 140 | return range; 141 | } 142 | 143 | float Maxbotix::readSensor() 144 | { 145 | float result; 146 | 147 | switch (input) { 148 | case PW: 149 | switch (model) { 150 | case LV: 151 | result = toCentimeters(pulseIn(pin, HIGH) / 147.0); 152 | break; 153 | 154 | case XL: 155 | result = pulseIn(pin, HIGH) / 58.0; 156 | break; 157 | 158 | case HRLV: 159 | result = pulseIn(pin, HIGH) / 10.0; 160 | break; 161 | 162 | default: 163 | break; 164 | } 165 | 166 | break; 167 | 168 | case AN: 169 | switch (model) { 170 | case LV: 171 | result = toCentimeters(analogRead(pin) / 2.0); 172 | break; 173 | 174 | case XL: 175 | result = analogRead(pin); 176 | break; 177 | 178 | case HRLV: 179 | result = analogRead(pin) * 5.0 / 10.0; 180 | break; 181 | 182 | default: 183 | break; 184 | } 185 | 186 | break; 187 | #ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL 188 | 189 | case TX: 190 | switch (model) { 191 | case LV: 192 | result = toCentimeters(readSensorSerial(3)); 193 | break; 194 | 195 | case XL: 196 | result = readSensorSerial(3); 197 | break; 198 | 199 | case HRLV: 200 | result = readSensorSerial(4) / 10.0; 201 | break; 202 | 203 | default: 204 | break; 205 | } 206 | 207 | break; 208 | #endif 209 | 210 | default: 211 | break; 212 | } 213 | 214 | return result; 215 | } 216 | 217 | unsigned short Maxbotix::readSensorSerial(uint8_t length) 218 | { 219 | char buffer[length]; 220 | 221 | // flush and wait for a range reading 222 | serial->flush(); 223 | 224 | while (!serial->available() || serial->read() != 'R'); 225 | 226 | // read the range 227 | for (int i = 0; i < length; i++) { 228 | while (!serial->available()); 229 | 230 | buffer[i] = serial->read(); 231 | } 232 | 233 | return atoi(buffer); 234 | } 235 | 236 | void Maxbotix::readSample() 237 | { 238 | // read 239 | for (int i = 0; i < sample_size; i++) { 240 | sample[i] = readSensor(); 241 | 242 | if (input == AN && i != sample_size - 1) 243 | delay(ad_sample_delay); 244 | } 245 | 246 | // sort 247 | sortSample(); 248 | } 249 | 250 | void Maxbotix::pushToSample(float value) 251 | { 252 | for (int i = 0; i < sample_size - 1; i++) 253 | sample[i] = sample[i + 1]; 254 | 255 | sample[sample_size - 1] = value; 256 | } 257 | 258 | 259 | void Maxbotix::sortSample() 260 | { 261 | for (int i = 1; i < sample_size; i++) { 262 | float j = sample[i]; 263 | int k; 264 | 265 | for (k = i - 1; (k >= 0) && (j < sample[k]); k--) 266 | sample[k + 1] = sample[k]; 267 | 268 | sample[k + 1] = j; 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /RxSoftwareSerial.cpp.disabled: -------------------------------------------------------------------------------- 1 | /* 2 | RxSoftwareSerial.cpp (Rx-only SoftwareSerial.cpp by Antoine Bertin) 3 | Multi-instance software serial library for Arduino/Wiring 4 | -- Interrupt-driven receive and other improvements by ladyada 5 | (http://ladyada.net) 6 | -- Tuning, circular buffer, derivation from class Print/Stream, 7 | multi-instance support, porting to 8MHz processors, 8 | various optimizations, PROGMEM delay tables, inverse logic and 9 | direct port writing by Mikal Hart (http://www.arduiniana.org) 10 | -- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com) 11 | -- 20MHz processor support by Garrett Mace (http://www.macetech.com) 12 | -- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/) 13 | 14 | This library is free software; you can redistribute it and/or 15 | modify it under the terms of the GNU Lesser General Public 16 | License as published by the Free Software Foundation; either 17 | version 2.1 of the License, or (at your option) any later version. 18 | 19 | This library is distributed in the hope that it will be useful, 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 | Lesser General Public License for more details. 23 | 24 | You should have received a copy of the GNU Lesser General Public 25 | License along with this library; if not, write to the Free Software 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 27 | 28 | */ 29 | 30 | // When set, _DEBUG co-opts pins 11 and 13 for debugging with an 31 | // oscilloscope or logic analyzer. Beware: it also slightly modifies 32 | // the bit times, so don't rely on it too much at high baud rates 33 | #define _DEBUG 0 34 | #define _DEBUG_PIN1 11 35 | #define _DEBUG_PIN2 13 36 | // 37 | // Includes 38 | // 39 | #include 40 | #include 41 | #include 42 | #include 43 | // 44 | // Lookup table 45 | // 46 | typedef struct _DELAY_TABLE 47 | { 48 | long baud; 49 | unsigned short rx_delay_centering; 50 | unsigned short rx_delay_intrabit; 51 | unsigned short rx_delay_stopbit; 52 | unsigned short tx_delay; 53 | } DELAY_TABLE; 54 | 55 | #if F_CPU == 16000000 56 | 57 | static const DELAY_TABLE table[] PROGMEM = 58 | { 59 | // baud rxcenter rxintra rxstop tx 60 | { 115200, 1, 17, 17, 12, }, 61 | { 57600, 10, 37, 37, 33, }, 62 | { 38400, 25, 57, 57, 54, }, 63 | { 31250, 31, 70, 70, 68, }, 64 | { 28800, 34, 77, 77, 74, }, 65 | { 19200, 54, 117, 117, 114, }, 66 | { 14400, 74, 156, 156, 153, }, 67 | { 9600, 114, 236, 236, 233, }, 68 | { 4800, 233, 474, 474, 471, }, 69 | { 2400, 471, 950, 950, 947, }, 70 | { 1200, 947, 1902, 1902, 1899, }, 71 | { 600, 1902, 3804, 3804, 3800, }, 72 | { 300, 3804, 7617, 7617, 7614, }, 73 | }; 74 | 75 | const int XMIT_START_ADJUSTMENT = 5; 76 | 77 | #elif F_CPU == 8000000 78 | 79 | static const DELAY_TABLE table[] PROGMEM = 80 | { 81 | // baud rxcenter rxintra rxstop tx 82 | { 115200, 1, 5, 5, 3, }, 83 | { 57600, 1, 15, 15, 13, }, 84 | { 38400, 2, 25, 26, 23, }, 85 | { 31250, 7, 32, 33, 29, }, 86 | { 28800, 11, 35, 35, 32, }, 87 | { 19200, 20, 55, 55, 52, }, 88 | { 14400, 30, 75, 75, 72, }, 89 | { 9600, 50, 114, 114, 112, }, 90 | { 4800, 110, 233, 233, 230, }, 91 | { 2400, 229, 472, 472, 469, }, 92 | { 1200, 467, 948, 948, 945, }, 93 | { 600, 948, 1895, 1895, 1890, }, 94 | { 300, 1895, 3805, 3805, 3802, }, 95 | }; 96 | 97 | const int XMIT_START_ADJUSTMENT = 4; 98 | 99 | #elif F_CPU == 20000000 100 | 101 | // 20MHz support courtesy of the good people at macegr.com. 102 | // Thanks, Garrett! 103 | 104 | static const DELAY_TABLE table[] PROGMEM = 105 | { 106 | // baud rxcenter rxintra rxstop tx 107 | { 115200, 3, 21, 21, 18, }, 108 | { 57600, 20, 43, 43, 41, }, 109 | { 38400, 37, 73, 73, 70, }, 110 | { 31250, 45, 89, 89, 88, }, 111 | { 28800, 46, 98, 98, 95, }, 112 | { 19200, 71, 148, 148, 145, }, 113 | { 14400, 96, 197, 197, 194, }, 114 | { 9600, 146, 297, 297, 294, }, 115 | { 4800, 296, 595, 595, 592, }, 116 | { 2400, 592, 1189, 1189, 1186, }, 117 | { 1200, 1187, 2379, 2379, 2376, }, 118 | { 600, 2379, 4759, 4759, 4755, }, 119 | { 300, 4759, 9523, 9523, 9520, }, 120 | }; 121 | 122 | const int XMIT_START_ADJUSTMENT = 6; 123 | 124 | #else 125 | 126 | #error This version of RxSoftwareSerial supports only 20, 16 and 8MHz processors 127 | 128 | #endif 129 | 130 | // 131 | // Statics 132 | // 133 | RxSoftwareSerial *RxSoftwareSerial::active_object = 0; 134 | char RxSoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; 135 | volatile uint8_t RxSoftwareSerial::_receive_buffer_tail = 0; 136 | volatile uint8_t RxSoftwareSerial::_receive_buffer_head = 0; 137 | 138 | // 139 | // Debugging 140 | // 141 | // This function generates a brief pulse 142 | // for debugging or measuring on an oscilloscope. 143 | inline void DebugPulse(uint8_t pin, uint8_t count) 144 | { 145 | #if _DEBUG 146 | volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin)); 147 | 148 | uint8_t val = *pport; 149 | while (count--) 150 | { 151 | *pport = val | digitalPinToBitMask(pin); 152 | *pport = val; 153 | } 154 | #endif 155 | } 156 | 157 | // 158 | // Private methods 159 | // 160 | 161 | /* static */ 162 | inline void RxSoftwareSerial::tunedDelay(uint16_t delay) { 163 | uint8_t tmp=0; 164 | 165 | asm volatile("sbiw %0, 0x01 \n\t" 166 | "ldi %1, 0xFF \n\t" 167 | "cpi %A0, 0xFF \n\t" 168 | "cpc %B0, %1 \n\t" 169 | "brne .-10 \n\t" 170 | : "+r" (delay), "+a" (tmp) 171 | : "0" (delay) 172 | ); 173 | } 174 | 175 | // This function sets the current object as the "listening" 176 | // one and returns true if it replaces another 177 | bool RxSoftwareSerial::listen() 178 | { 179 | if (active_object != this) 180 | { 181 | _buffer_overflow = false; 182 | uint8_t oldSREG = SREG; 183 | cli(); 184 | _receive_buffer_head = _receive_buffer_tail = 0; 185 | active_object = this; 186 | SREG = oldSREG; 187 | return true; 188 | } 189 | 190 | return false; 191 | } 192 | 193 | // 194 | // The receive routine called by the interrupt handler 195 | // 196 | void RxSoftwareSerial::recv() 197 | { 198 | 199 | #if GCC_VERSION < 40302 200 | // Work-around for avr-gcc 4.3.0 OSX version bug 201 | // Preserve the registers that the compiler misses 202 | // (courtesy of Arduino forum user *etracer*) 203 | asm volatile( 204 | "push r18 \n\t" 205 | "push r19 \n\t" 206 | "push r20 \n\t" 207 | "push r21 \n\t" 208 | "push r22 \n\t" 209 | "push r23 \n\t" 210 | "push r26 \n\t" 211 | "push r27 \n\t" 212 | ::); 213 | #endif 214 | 215 | uint8_t d = 0; 216 | 217 | // If RX line is high, then we don't see any start bit 218 | // so interrupt is probably not for us 219 | if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) 220 | { 221 | // Wait approximately 1/2 of a bit width to "center" the sample 222 | tunedDelay(_rx_delay_centering); 223 | DebugPulse(_DEBUG_PIN2, 1); 224 | 225 | // Read each of the 8 bits 226 | for (uint8_t i=0x1; i; i <<= 1) 227 | { 228 | tunedDelay(_rx_delay_intrabit); 229 | DebugPulse(_DEBUG_PIN2, 1); 230 | uint8_t noti = ~i; 231 | if (rx_pin_read()) 232 | d |= i; 233 | else // else clause added to ensure function timing is ~balanced 234 | d &= noti; 235 | } 236 | 237 | // skip the stop bit 238 | tunedDelay(_rx_delay_stopbit); 239 | DebugPulse(_DEBUG_PIN2, 1); 240 | 241 | if (_inverse_logic) 242 | d = ~d; 243 | 244 | // if buffer full, set the overflow flag and return 245 | if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head) 246 | { 247 | // save new data in buffer: tail points to where byte goes 248 | _receive_buffer[_receive_buffer_tail] = d; // save new byte 249 | _receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; 250 | } 251 | else 252 | { 253 | #if _DEBUG // for scope: pulse pin as overflow indictator 254 | DebugPulse(_DEBUG_PIN1, 1); 255 | #endif 256 | _buffer_overflow = true; 257 | } 258 | } 259 | 260 | #if GCC_VERSION < 40302 261 | // Work-around for avr-gcc 4.3.0 OSX version bug 262 | // Restore the registers that the compiler misses 263 | asm volatile( 264 | "pop r27 \n\t" 265 | "pop r26 \n\t" 266 | "pop r23 \n\t" 267 | "pop r22 \n\t" 268 | "pop r21 \n\t" 269 | "pop r20 \n\t" 270 | "pop r19 \n\t" 271 | "pop r18 \n\t" 272 | ::); 273 | #endif 274 | } 275 | 276 | uint8_t RxSoftwareSerial::rx_pin_read() 277 | { 278 | return *_receivePortRegister & _receiveBitMask; 279 | } 280 | 281 | // 282 | // Interrupt handling 283 | // 284 | 285 | /* static */ 286 | inline void RxSoftwareSerial::handle_interrupt() 287 | { 288 | if (active_object) 289 | { 290 | active_object->recv(); 291 | } 292 | } 293 | 294 | #if defined(PCINT0_vect) 295 | ISR(PCINT0_vect) 296 | { 297 | RxSoftwareSerial::handle_interrupt(); 298 | } 299 | #endif 300 | 301 | #if defined(PCINT1_vect) 302 | ISR(PCINT1_vect) 303 | { 304 | RxSoftwareSerial::handle_interrupt(); 305 | } 306 | #endif 307 | 308 | #if defined(PCINT2_vect) 309 | ISR(PCINT2_vect) 310 | { 311 | RxSoftwareSerial::handle_interrupt(); 312 | } 313 | #endif 314 | 315 | #if defined(PCINT3_vect) 316 | ISR(PCINT3_vect) 317 | { 318 | RxSoftwareSerial::handle_interrupt(); 319 | } 320 | #endif 321 | 322 | // 323 | // Constructor 324 | // 325 | RxSoftwareSerial::RxSoftwareSerial(uint8_t receivePin, bool inverse_logic /* = false */) : 326 | _rx_delay_centering(0), 327 | _rx_delay_intrabit(0), 328 | _rx_delay_stopbit(0), 329 | _tx_delay(0), 330 | _buffer_overflow(false), 331 | _inverse_logic(inverse_logic) 332 | { 333 | setRX(receivePin); 334 | } 335 | 336 | // 337 | // Destructor 338 | // 339 | RxSoftwareSerial::~RxSoftwareSerial() 340 | { 341 | end(); 342 | } 343 | 344 | void RxSoftwareSerial::setRX(uint8_t rx) 345 | { 346 | pinMode(rx, INPUT); 347 | if (!_inverse_logic) 348 | digitalWrite(rx, HIGH); // pullup for normal logic! 349 | _receivePin = rx; 350 | _receiveBitMask = digitalPinToBitMask(rx); 351 | uint8_t port = digitalPinToPort(rx); 352 | _receivePortRegister = portInputRegister(port); 353 | } 354 | 355 | // 356 | // Public methods 357 | // 358 | 359 | void RxSoftwareSerial::begin(long speed) 360 | { 361 | _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; 362 | 363 | for (unsigned i=0; i