LoRa node acquiring analog input and sending to LoRa Server - Working ok
Dependents: DISCO-L072CZ-LRWAN1_LoRa_node EIoT_LoRa_node_1 EIoT_LoRa_node_2 EIoT_LoRa_node_3
Fork of SX1276GenericLib by
sx1276/arduino-mbed.h@65:b2d98328fcba, 2017-07-11 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Tue Jul 11 13:32:34 2017 +0200
- Revision:
- 65:b2d98328fcba
- Parent:
- sx1276/sx1276-arduino-hal.h@61:08b50780eb91
- Child:
- 66:fbb2da34bd9a
Added Arduino Support, it still needs further testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut Tschemernjak | 40:0165543c0986 | 1 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 2 | * The file is Licensed under the Apache License, Version 2.0 |
Helmut Tschemernjak | 46:e78a1d0391ac | 3 | * (c) 2017 Helmut Tschemernjak |
Helmut Tschemernjak | 46:e78a1d0391ac | 4 | * 30826 Garbsen (Hannover) Germany |
Helmut Tschemernjak | 46:e78a1d0391ac | 5 | */ |
Helmut Tschemernjak | 40:0165543c0986 | 6 | |
Helmut Tschemernjak | 65:b2d98328fcba | 7 | |
Helmut Tschemernjak | 65:b2d98328fcba | 8 | |
Helmut Tschemernjak | 65:b2d98328fcba | 9 | #ifdef ARDUINO |
Helmut Tschemernjak | 65:b2d98328fcba | 10 | #ifndef __ARDUINO_MBED_H__ |
Helmut Tschemernjak | 65:b2d98328fcba | 11 | #define __ARDUINO_MBED_H__ |
Helmut Tschemernjak | 40:0165543c0986 | 12 | |
Helmut Tschemernjak | 65:b2d98328fcba | 13 | #include <arduino.h> |
Helmut Tschemernjak | 65:b2d98328fcba | 14 | #include "Callback-A.h" |
Helmut Tschemernjak | 65:b2d98328fcba | 15 | #include <SPI.h> |
Helmut Tschemernjak | 65:b2d98328fcba | 16 | #undef min |
Helmut Tschemernjak | 65:b2d98328fcba | 17 | #undef max |
Helmut Tschemernjak | 65:b2d98328fcba | 18 | #undef map |
Helmut Tschemernjak | 40:0165543c0986 | 19 | |
Helmut Tschemernjak | 40:0165543c0986 | 20 | |
Helmut Tschemernjak | 65:b2d98328fcba | 21 | typedef int PinName; |
Helmut Tschemernjak | 65:b2d98328fcba | 22 | #define NC -1 |
Helmut Tschemernjak | 65:b2d98328fcba | 23 | #define wait_ms delay |
Helmut Tschemernjak | 65:b2d98328fcba | 24 | |
Helmut Tschemernjak | 65:b2d98328fcba | 25 | class DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 26 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 27 | DigitalInOut(PinName pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 28 | _gpioPin = pin; |
Helmut Tschemernjak | 65:b2d98328fcba | 29 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 30 | void write(int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 31 | digitalWrite(_gpioPin, value == 0 ? LOW : HIGH); |
Helmut Tschemernjak | 65:b2d98328fcba | 32 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 33 | |
Helmut Tschemernjak | 65:b2d98328fcba | 34 | void output() { |
Helmut Tschemernjak | 65:b2d98328fcba | 35 | pinMode(_gpioPin, OUTPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 36 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 37 | |
Helmut Tschemernjak | 65:b2d98328fcba | 38 | void input() { |
Helmut Tschemernjak | 65:b2d98328fcba | 39 | pinMode(_gpioPin, INPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 40 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 41 | |
Helmut Tschemernjak | 65:b2d98328fcba | 42 | int read() { |
Helmut Tschemernjak | 65:b2d98328fcba | 43 | if (digitalRead(_gpioPin) == HIGH) |
Helmut Tschemernjak | 65:b2d98328fcba | 44 | return 1; |
Helmut Tschemernjak | 65:b2d98328fcba | 45 | else |
Helmut Tschemernjak | 65:b2d98328fcba | 46 | return 0; |
Helmut Tschemernjak | 65:b2d98328fcba | 47 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 48 | operator int() { |
Helmut Tschemernjak | 65:b2d98328fcba | 49 | return read(); |
Helmut Tschemernjak | 65:b2d98328fcba | 50 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 51 | |
Helmut Tschemernjak | 65:b2d98328fcba | 52 | DigitalInOut& operator= (int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 53 | // Underlying write is thread safe |
Helmut Tschemernjak | 65:b2d98328fcba | 54 | write(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 55 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 56 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 57 | |
Helmut Tschemernjak | 65:b2d98328fcba | 58 | DigitalInOut& operator= (DigitalInOut& rhs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 59 | write(rhs.read()); |
Helmut Tschemernjak | 65:b2d98328fcba | 60 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 61 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 62 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 63 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 64 | int _gpioPin; |
Helmut Tschemernjak | 65:b2d98328fcba | 65 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 66 | |
Helmut Tschemernjak | 65:b2d98328fcba | 67 | class DigitalOut : public DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 68 | public: |
Helmut Tschemernjak | 46:e78a1d0391ac | 69 | |
Helmut Tschemernjak | 65:b2d98328fcba | 70 | DigitalOut(PinName pin) : DigitalInOut(pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 71 | output(); |
Helmut Tschemernjak | 65:b2d98328fcba | 72 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 73 | |
Helmut Tschemernjak | 65:b2d98328fcba | 74 | DigitalOut& operator= (int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 75 | write(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 76 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 77 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 78 | |
Helmut Tschemernjak | 65:b2d98328fcba | 79 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 80 | |
Helmut Tschemernjak | 65:b2d98328fcba | 81 | class DigitalIn : public DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 82 | public: |
Helmut Tschemernjak | 46:e78a1d0391ac | 83 | |
Helmut Tschemernjak | 65:b2d98328fcba | 84 | DigitalIn(PinName pin) : DigitalInOut(pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 85 | input(); |
Helmut Tschemernjak | 65:b2d98328fcba | 86 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 87 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 88 | |
Helmut Tschemernjak | 65:b2d98328fcba | 89 | class XSPI { |
Helmut Tschemernjak | 65:b2d98328fcba | 90 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 91 | XSPI(PinName mosi, PinName miso, PinName sclk) { |
Helmut Tschemernjak | 65:b2d98328fcba | 92 | _mosi = mosi; |
Helmut Tschemernjak | 65:b2d98328fcba | 93 | _miso = miso; |
Helmut Tschemernjak | 65:b2d98328fcba | 94 | _sclk = sclk; |
Helmut Tschemernjak | 65:b2d98328fcba | 95 | if (mosi == PIN_SPI_MOSI && miso == PIN_SPI_MISO && sclk == PIN_SPI_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 96 | _spi = &SPI; |
Helmut Tschemernjak | 65:b2d98328fcba | 97 | #if SPI_INTERFACES_COUNT > 1 |
Helmut Tschemernjak | 65:b2d98328fcba | 98 | else if (mosi == PIN_SPI1_MOSI && miso == PIN_SPI1_MISO && sclk == PIN_SPI1_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 99 | _spi = &SPI1; |
Helmut Tschemernjak | 65:b2d98328fcba | 100 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 101 | #if SPI_INTERFACES_COUNT > 2 |
Helmut Tschemernjak | 65:b2d98328fcba | 102 | else if (mosi == PIN_SPI2_MOSI && miso == PIN_SPI2_MISO && sclk == PIN_SPI2_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 103 | _spi = &SPI2; |
Helmut Tschemernjak | 65:b2d98328fcba | 104 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 105 | else { |
Helmut Tschemernjak | 65:b2d98328fcba | 106 | _spi = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 107 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 108 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 109 | _hz = 1000000; |
Helmut Tschemernjak | 65:b2d98328fcba | 110 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 111 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 112 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 113 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 114 | |
Helmut Tschemernjak | 65:b2d98328fcba | 115 | void format(int bits, int mode = 0) { |
Helmut Tschemernjak | 65:b2d98328fcba | 116 | if (mode == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 117 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 118 | else if (mode == 1) |
Helmut Tschemernjak | 65:b2d98328fcba | 119 | _mode = SPI_MODE1; |
Helmut Tschemernjak | 65:b2d98328fcba | 120 | else if (mode == 2) |
Helmut Tschemernjak | 65:b2d98328fcba | 121 | _mode = SPI_MODE2; |
Helmut Tschemernjak | 65:b2d98328fcba | 122 | else if (mode == 3) |
Helmut Tschemernjak | 65:b2d98328fcba | 123 | _mode = SPI_MODE3; |
Helmut Tschemernjak | 65:b2d98328fcba | 124 | else |
Helmut Tschemernjak | 65:b2d98328fcba | 125 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 126 | _bits = bits; |
Helmut Tschemernjak | 65:b2d98328fcba | 127 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 128 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 129 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 130 | void frequency(int hz) { |
Helmut Tschemernjak | 65:b2d98328fcba | 131 | _hz = hz; |
Helmut Tschemernjak | 65:b2d98328fcba | 132 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 133 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 134 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 135 | |
Helmut Tschemernjak | 65:b2d98328fcba | 136 | int write(int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 137 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 138 | int ret = _spi->transfer(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 139 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 140 | return ret; |
Helmut Tschemernjak | 65:b2d98328fcba | 141 | } |
Helmut Tschemernjak | 61:08b50780eb91 | 142 | |
Helmut Tschemernjak | 65:b2d98328fcba | 143 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 144 | SPIClass *_spi; |
Helmut Tschemernjak | 65:b2d98328fcba | 145 | int _hz; |
Helmut Tschemernjak | 65:b2d98328fcba | 146 | int _mode; |
Helmut Tschemernjak | 65:b2d98328fcba | 147 | int _bits; |
Helmut Tschemernjak | 65:b2d98328fcba | 148 | int _mosi, _miso, _sclk; |
Helmut Tschemernjak | 46:e78a1d0391ac | 149 | }; |
Helmut Tschemernjak | 40:0165543c0986 | 150 | |
Helmut Tschemernjak | 65:b2d98328fcba | 151 | class InterruptIn { |
Helmut Tschemernjak | 65:b2d98328fcba | 152 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 153 | static void donothing(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 154 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 155 | |
Helmut Tschemernjak | 65:b2d98328fcba | 156 | InterruptIn(PinName pin) : _func() { |
Helmut Tschemernjak | 65:b2d98328fcba | 157 | _gpioPin = pin; |
Helmut Tschemernjak | 65:b2d98328fcba | 158 | _func = InterruptIn::donothing; |
Helmut Tschemernjak | 65:b2d98328fcba | 159 | pinMode(_gpioPin, INPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 160 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 161 | |
Helmut Tschemernjak | 65:b2d98328fcba | 162 | ~InterruptIn() { |
Helmut Tschemernjak | 65:b2d98328fcba | 163 | detachInterrupt(digitalPinToInterrupt(_gpioPin)); |
Helmut Tschemernjak | 65:b2d98328fcba | 164 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 165 | |
Helmut Tschemernjak | 65:b2d98328fcba | 166 | static void _irq_handler(InterruptIn *id) { |
Helmut Tschemernjak | 65:b2d98328fcba | 167 | if (id) |
Helmut Tschemernjak | 65:b2d98328fcba | 168 | id->_func(); |
Helmut Tschemernjak | 65:b2d98328fcba | 169 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 170 | |
Helmut Tschemernjak | 65:b2d98328fcba | 171 | void rise(Callback<void()> func); |
Helmut Tschemernjak | 65:b2d98328fcba | 172 | |
Helmut Tschemernjak | 65:b2d98328fcba | 173 | void fall(Callback<void()> func); |
Helmut Tschemernjak | 65:b2d98328fcba | 174 | |
Helmut Tschemernjak | 65:b2d98328fcba | 175 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 176 | int _gpioPin; |
Helmut Tschemernjak | 65:b2d98328fcba | 177 | Callback<void()> _func; |
Helmut Tschemernjak | 65:b2d98328fcba | 178 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 179 | |
Helmut Tschemernjak | 65:b2d98328fcba | 180 | class Timer { |
Helmut Tschemernjak | 65:b2d98328fcba | 181 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 182 | void start(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 183 | _time = micros(); |
Helmut Tschemernjak | 65:b2d98328fcba | 184 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 185 | int read_ms(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 186 | return (micros() - _time) / 1000; |
Helmut Tschemernjak | 65:b2d98328fcba | 187 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 188 | int read_us(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 189 | return micros() - _time; |
Helmut Tschemernjak | 65:b2d98328fcba | 190 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 191 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 192 | uint32_t _time; |
Helmut Tschemernjak | 65:b2d98328fcba | 193 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 194 | |
Helmut Tschemernjak | 65:b2d98328fcba | 195 | |
Helmut Tschemernjak | 65:b2d98328fcba | 196 | class Timeout { |
Helmut Tschemernjak | 65:b2d98328fcba | 197 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 198 | Timeout() : _func() { |
Helmut Tschemernjak | 65:b2d98328fcba | 199 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 200 | ~Timeout() { |
Helmut Tschemernjak | 65:b2d98328fcba | 201 | detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 202 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 203 | |
Helmut Tschemernjak | 65:b2d98328fcba | 204 | void attach(Callback<void()> func, int msecs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 205 | if (msecs == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 206 | return detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 207 | _func = func; |
Helmut Tschemernjak | 65:b2d98328fcba | 208 | _timeout = micros() + (uint32_t)msecs * 1000; |
Helmut Tschemernjak | 65:b2d98328fcba | 209 | insert(); |
Helmut Tschemernjak | 65:b2d98328fcba | 210 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 211 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 212 | |
Helmut Tschemernjak | 65:b2d98328fcba | 213 | void attach_us(Callback<void()> func, long usecs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 214 | if (usecs == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 215 | return detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 216 | _func = func; |
Helmut Tschemernjak | 65:b2d98328fcba | 217 | _timeout = micros() + usecs; |
Helmut Tschemernjak | 65:b2d98328fcba | 218 | insert(); |
Helmut Tschemernjak | 65:b2d98328fcba | 219 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 220 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 221 | |
Helmut Tschemernjak | 65:b2d98328fcba | 222 | void detach() { |
Helmut Tschemernjak | 65:b2d98328fcba | 223 | _func = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 224 | remove(); |
Helmut Tschemernjak | 65:b2d98328fcba | 225 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 226 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 227 | |
Helmut Tschemernjak | 65:b2d98328fcba | 228 | static void _irq_handler(Timeout *tp) { |
Helmut Tschemernjak | 65:b2d98328fcba | 229 | if (tp) { |
Helmut Tschemernjak | 65:b2d98328fcba | 230 | tp->_func(); |
Helmut Tschemernjak | 65:b2d98328fcba | 231 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 232 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 233 | |
Helmut Tschemernjak | 65:b2d98328fcba | 234 | static void restart(uint32_t usecs = 0); |
Helmut Tschemernjak | 65:b2d98328fcba | 235 | protected: |
Helmut Tschemernjak | 65:b2d98328fcba | 236 | void insert(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 237 | void remove(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 238 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 239 | Callback<void()> _func; |
Helmut Tschemernjak | 65:b2d98328fcba | 240 | uint32_t _timeout; // in us this lasts form 49 days. |
Helmut Tschemernjak | 65:b2d98328fcba | 241 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 242 | |
Helmut Tschemernjak | 65:b2d98328fcba | 243 | #endif // __ARDUINO_MBED_H__ |
Helmut Tschemernjak | 65:b2d98328fcba | 244 | |
Helmut Tschemernjak | 40:0165543c0986 | 245 | #endif // ARDUINO |