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
Arduino-mbed-APIs/arduino-mbed.h@80:62f0b027efff, 2017-08-03 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Thu Aug 03 13:43:35 2017 +0200
- Revision:
- 80:62f0b027efff
- Parent:
- 78:9d2cc07d9525
- Child:
- 84:3428e25c7157
Added proper wait_ms function which uses our timers
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 | 65:b2d98328fcba | 20 | typedef int PinName; |
Helmut Tschemernjak | 65:b2d98328fcba | 21 | #define NC -1 |
Helmut Tschemernjak | 70:1d496aae2819 | 22 | /* we need to redefine out dprintf because stdio.h uses the same name */ |
Helmut Tschemernjak | 70:1d496aae2819 | 23 | #define dprint dxprintf |
Helmut Tschemernjak | 69:d440a5b04708 | 24 | #if ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10606 |
Helmut Tschemernjak | 69:d440a5b04708 | 25 | #define MYdigitalPinToInterrupt(x) digitalPinToInterrupt(x) |
Helmut Tschemernjak | 69:d440a5b04708 | 26 | #else |
Helmut Tschemernjak | 69:d440a5b04708 | 27 | #define MYdigitalPinToInterrupt(x) (x) |
Helmut Tschemernjak | 69:d440a5b04708 | 28 | #endif |
Helmut Tschemernjak | 69:d440a5b04708 | 29 | |
Helmut Tschemernjak | 75:7330dd86cdea | 30 | void InitSerial(Stream *serial); |
Helmut Tschemernjak | 75:7330dd86cdea | 31 | extern Stream *ser; |
Helmut Tschemernjak | 75:7330dd86cdea | 32 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 33 | /* |
Helmut Tschemernjak | 77:7f227a4dffe6 | 34 | * Arduino_d21.cpp |
Helmut Tschemernjak | 77:7f227a4dffe6 | 35 | */ |
Helmut Tschemernjak | 77:7f227a4dffe6 | 36 | extern void startTimer(Tcc *t, uint64_t delay_ns); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 37 | extern void stopTimer(Tcc *t); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 38 | extern uint64_t ns_getTicker(void); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 39 | extern Tcc *getTimeout_tcc(void); |
Helmut Tschemernjak | 78:9d2cc07d9525 | 40 | extern int CPUID(uint8_t *buf, int maxSize, uint32_t xorval); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 41 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 42 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 43 | extern void sleep(void); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 44 | extern void deepsleep(void); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 45 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 46 | #define MAX_TIMEOUTS 10 |
Helmut Tschemernjak | 77:7f227a4dffe6 | 47 | class Timeout; |
Helmut Tschemernjak | 77:7f227a4dffe6 | 48 | struct TimeoutVector { |
Helmut Tschemernjak | 77:7f227a4dffe6 | 49 | Timeout *timer; |
Helmut Tschemernjak | 77:7f227a4dffe6 | 50 | }; |
Helmut Tschemernjak | 77:7f227a4dffe6 | 51 | extern TimeoutVector TimeOuts[]; |
Helmut Tschemernjak | 77:7f227a4dffe6 | 52 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 53 | |
Helmut Tschemernjak | 77:7f227a4dffe6 | 54 | /* |
Helmut Tschemernjak | 77:7f227a4dffe6 | 55 | * Arduino-mbed.cpp |
Helmut Tschemernjak | 77:7f227a4dffe6 | 56 | */ |
Helmut Tschemernjak | 77:7f227a4dffe6 | 57 | extern uint32_t s_getTicker(void); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 58 | extern uint32_t ms_getTicker(void); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 59 | extern uint32_t us_getTicker(void); |
Helmut Tschemernjak | 80:62f0b027efff | 60 | extern void wait_ms(uint32_t ms); |
Helmut Tschemernjak | 77:7f227a4dffe6 | 61 | |
Helmut Tschemernjak | 69:d440a5b04708 | 62 | |
Helmut Tschemernjak | 68:66251038a897 | 63 | enum PinMode { |
Helmut Tschemernjak | 68:66251038a897 | 64 | PullUp = 1, |
Helmut Tschemernjak | 68:66251038a897 | 65 | PullDown = 2, |
Helmut Tschemernjak | 68:66251038a897 | 66 | }; |
Helmut Tschemernjak | 67:d3afd803f40d | 67 | |
Helmut Tschemernjak | 65:b2d98328fcba | 68 | class DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 69 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 70 | DigitalInOut(PinName pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 71 | _gpioPin = pin; |
Helmut Tschemernjak | 65:b2d98328fcba | 72 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 73 | void write(int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 74 | digitalWrite(_gpioPin, value == 0 ? LOW : HIGH); |
Helmut Tschemernjak | 65:b2d98328fcba | 75 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 76 | |
Helmut Tschemernjak | 65:b2d98328fcba | 77 | void output() { |
Helmut Tschemernjak | 65:b2d98328fcba | 78 | pinMode(_gpioPin, OUTPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 79 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 80 | |
Helmut Tschemernjak | 65:b2d98328fcba | 81 | void input() { |
Helmut Tschemernjak | 65:b2d98328fcba | 82 | pinMode(_gpioPin, INPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 83 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 84 | |
Helmut Tschemernjak | 68:66251038a897 | 85 | void mode(PinMode pull) { |
Helmut Tschemernjak | 68:66251038a897 | 86 | switch(pull) { |
Helmut Tschemernjak | 68:66251038a897 | 87 | case PullUp: |
Helmut Tschemernjak | 68:66251038a897 | 88 | pinMode(_gpioPin, INPUT_PULLUP); |
Helmut Tschemernjak | 68:66251038a897 | 89 | break; |
Helmut Tschemernjak | 68:66251038a897 | 90 | case PullDown: |
Helmut Tschemernjak | 68:66251038a897 | 91 | pinMode(_gpioPin, INPUT_PULLDOWN); |
Helmut Tschemernjak | 68:66251038a897 | 92 | break; |
Helmut Tschemernjak | 68:66251038a897 | 93 | } |
Helmut Tschemernjak | 68:66251038a897 | 94 | } |
Helmut Tschemernjak | 68:66251038a897 | 95 | |
Helmut Tschemernjak | 65:b2d98328fcba | 96 | int read() { |
Helmut Tschemernjak | 65:b2d98328fcba | 97 | if (digitalRead(_gpioPin) == HIGH) |
Helmut Tschemernjak | 65:b2d98328fcba | 98 | return 1; |
Helmut Tschemernjak | 65:b2d98328fcba | 99 | else |
Helmut Tschemernjak | 65:b2d98328fcba | 100 | return 0; |
Helmut Tschemernjak | 65:b2d98328fcba | 101 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 102 | operator int() { |
Helmut Tschemernjak | 65:b2d98328fcba | 103 | return read(); |
Helmut Tschemernjak | 65:b2d98328fcba | 104 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 105 | |
Helmut Tschemernjak | 65:b2d98328fcba | 106 | DigitalInOut& operator= (int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 107 | // Underlying write is thread safe |
Helmut Tschemernjak | 65:b2d98328fcba | 108 | write(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 109 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 110 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 111 | |
Helmut Tschemernjak | 65:b2d98328fcba | 112 | DigitalInOut& operator= (DigitalInOut& rhs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 113 | write(rhs.read()); |
Helmut Tschemernjak | 65:b2d98328fcba | 114 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 115 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 116 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 117 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 118 | int _gpioPin; |
Helmut Tschemernjak | 65:b2d98328fcba | 119 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 120 | |
Helmut Tschemernjak | 65:b2d98328fcba | 121 | class DigitalOut : public DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 122 | public: |
Helmut Tschemernjak | 46:e78a1d0391ac | 123 | |
Helmut Tschemernjak | 65:b2d98328fcba | 124 | DigitalOut(PinName pin) : DigitalInOut(pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 125 | output(); |
Helmut Tschemernjak | 65:b2d98328fcba | 126 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 127 | |
Helmut Tschemernjak | 65:b2d98328fcba | 128 | DigitalOut& operator= (int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 129 | write(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 130 | return *this; |
Helmut Tschemernjak | 65:b2d98328fcba | 131 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 132 | |
Helmut Tschemernjak | 65:b2d98328fcba | 133 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 134 | |
Helmut Tschemernjak | 65:b2d98328fcba | 135 | class DigitalIn : public DigitalInOut { |
Helmut Tschemernjak | 65:b2d98328fcba | 136 | public: |
Helmut Tschemernjak | 46:e78a1d0391ac | 137 | |
Helmut Tschemernjak | 65:b2d98328fcba | 138 | DigitalIn(PinName pin) : DigitalInOut(pin) { |
Helmut Tschemernjak | 65:b2d98328fcba | 139 | input(); |
Helmut Tschemernjak | 65:b2d98328fcba | 140 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 141 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 142 | |
Helmut Tschemernjak | 65:b2d98328fcba | 143 | class XSPI { |
Helmut Tschemernjak | 65:b2d98328fcba | 144 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 145 | XSPI(PinName mosi, PinName miso, PinName sclk) { |
Helmut Tschemernjak | 65:b2d98328fcba | 146 | _mosi = mosi; |
Helmut Tschemernjak | 65:b2d98328fcba | 147 | _miso = miso; |
Helmut Tschemernjak | 65:b2d98328fcba | 148 | _sclk = sclk; |
Helmut Tschemernjak | 65:b2d98328fcba | 149 | if (mosi == PIN_SPI_MOSI && miso == PIN_SPI_MISO && sclk == PIN_SPI_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 150 | _spi = &SPI; |
Helmut Tschemernjak | 65:b2d98328fcba | 151 | #if SPI_INTERFACES_COUNT > 1 |
Helmut Tschemernjak | 65:b2d98328fcba | 152 | else if (mosi == PIN_SPI1_MOSI && miso == PIN_SPI1_MISO && sclk == PIN_SPI1_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 153 | _spi = &SPI1; |
Helmut Tschemernjak | 65:b2d98328fcba | 154 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 155 | #if SPI_INTERFACES_COUNT > 2 |
Helmut Tschemernjak | 65:b2d98328fcba | 156 | else if (mosi == PIN_SPI2_MOSI && miso == PIN_SPI2_MISO && sclk == PIN_SPI2_SCK) |
Helmut Tschemernjak | 65:b2d98328fcba | 157 | _spi = &SPI2; |
Helmut Tschemernjak | 65:b2d98328fcba | 158 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 159 | else { |
Helmut Tschemernjak | 65:b2d98328fcba | 160 | _spi = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 161 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 162 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 163 | _hz = 1000000; |
Helmut Tschemernjak | 65:b2d98328fcba | 164 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 165 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 69:d440a5b04708 | 166 | } |
Helmut Tschemernjak | 69:d440a5b04708 | 167 | ~XSPI() { |
Helmut Tschemernjak | 65:b2d98328fcba | 168 | _spi->endTransaction(); |
Helmut Tschemernjak | 69:d440a5b04708 | 169 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 170 | |
Helmut Tschemernjak | 65:b2d98328fcba | 171 | void format(int bits, int mode = 0) { |
Helmut Tschemernjak | 65:b2d98328fcba | 172 | if (mode == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 173 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 174 | else if (mode == 1) |
Helmut Tschemernjak | 65:b2d98328fcba | 175 | _mode = SPI_MODE1; |
Helmut Tschemernjak | 65:b2d98328fcba | 176 | else if (mode == 2) |
Helmut Tschemernjak | 65:b2d98328fcba | 177 | _mode = SPI_MODE2; |
Helmut Tschemernjak | 65:b2d98328fcba | 178 | else if (mode == 3) |
Helmut Tschemernjak | 65:b2d98328fcba | 179 | _mode = SPI_MODE3; |
Helmut Tschemernjak | 65:b2d98328fcba | 180 | else |
Helmut Tschemernjak | 65:b2d98328fcba | 181 | _mode = SPI_MODE0; |
Helmut Tschemernjak | 65:b2d98328fcba | 182 | _bits = bits; |
Helmut Tschemernjak | 69:d440a5b04708 | 183 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 184 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 185 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 186 | void frequency(int hz) { |
Helmut Tschemernjak | 65:b2d98328fcba | 187 | _hz = hz; |
Helmut Tschemernjak | 69:d440a5b04708 | 188 | _spi->endTransaction(); |
Helmut Tschemernjak | 65:b2d98328fcba | 189 | _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); |
Helmut Tschemernjak | 65:b2d98328fcba | 190 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 191 | |
Helmut Tschemernjak | 65:b2d98328fcba | 192 | int write(int value) { |
Helmut Tschemernjak | 65:b2d98328fcba | 193 | int ret = _spi->transfer(value); |
Helmut Tschemernjak | 65:b2d98328fcba | 194 | return ret; |
Helmut Tschemernjak | 65:b2d98328fcba | 195 | } |
Helmut Tschemernjak | 61:08b50780eb91 | 196 | |
Helmut Tschemernjak | 65:b2d98328fcba | 197 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 198 | SPIClass *_spi; |
Helmut Tschemernjak | 65:b2d98328fcba | 199 | int _hz; |
Helmut Tschemernjak | 65:b2d98328fcba | 200 | int _mode; |
Helmut Tschemernjak | 65:b2d98328fcba | 201 | int _bits; |
Helmut Tschemernjak | 65:b2d98328fcba | 202 | int _mosi, _miso, _sclk; |
Helmut Tschemernjak | 46:e78a1d0391ac | 203 | }; |
Helmut Tschemernjak | 40:0165543c0986 | 204 | |
Helmut Tschemernjak | 65:b2d98328fcba | 205 | class InterruptIn { |
Helmut Tschemernjak | 65:b2d98328fcba | 206 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 207 | static void donothing(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 208 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 209 | |
Helmut Tschemernjak | 65:b2d98328fcba | 210 | InterruptIn(PinName pin) : _func() { |
Helmut Tschemernjak | 65:b2d98328fcba | 211 | _gpioPin = pin; |
Helmut Tschemernjak | 65:b2d98328fcba | 212 | _func = InterruptIn::donothing; |
Helmut Tschemernjak | 65:b2d98328fcba | 213 | pinMode(_gpioPin, INPUT); |
Helmut Tschemernjak | 65:b2d98328fcba | 214 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 215 | |
Helmut Tschemernjak | 65:b2d98328fcba | 216 | ~InterruptIn() { |
Helmut Tschemernjak | 69:d440a5b04708 | 217 | detachInterrupt(MYdigitalPinToInterrupt(_gpioPin)); |
Helmut Tschemernjak | 65:b2d98328fcba | 218 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 219 | |
Helmut Tschemernjak | 65:b2d98328fcba | 220 | static void _irq_handler(InterruptIn *id) { |
Helmut Tschemernjak | 65:b2d98328fcba | 221 | if (id) |
Helmut Tschemernjak | 65:b2d98328fcba | 222 | id->_func(); |
Helmut Tschemernjak | 65:b2d98328fcba | 223 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 224 | |
Helmut Tschemernjak | 65:b2d98328fcba | 225 | void rise(Callback<void()> func); |
Helmut Tschemernjak | 65:b2d98328fcba | 226 | |
Helmut Tschemernjak | 65:b2d98328fcba | 227 | void fall(Callback<void()> func); |
Helmut Tschemernjak | 65:b2d98328fcba | 228 | |
Helmut Tschemernjak | 69:d440a5b04708 | 229 | void mode(PinMode pull) { |
Helmut Tschemernjak | 69:d440a5b04708 | 230 | switch(pull) { |
Helmut Tschemernjak | 69:d440a5b04708 | 231 | case PullUp: |
Helmut Tschemernjak | 69:d440a5b04708 | 232 | pinMode(_gpioPin, INPUT_PULLUP); |
Helmut Tschemernjak | 69:d440a5b04708 | 233 | break; |
Helmut Tschemernjak | 69:d440a5b04708 | 234 | case PullDown: |
Helmut Tschemernjak | 69:d440a5b04708 | 235 | pinMode(_gpioPin, INPUT_PULLDOWN); |
Helmut Tschemernjak | 69:d440a5b04708 | 236 | break; |
Helmut Tschemernjak | 69:d440a5b04708 | 237 | } |
Helmut Tschemernjak | 69:d440a5b04708 | 238 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 239 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 240 | int _gpioPin; |
Helmut Tschemernjak | 65:b2d98328fcba | 241 | Callback<void()> _func; |
Helmut Tschemernjak | 65:b2d98328fcba | 242 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 243 | |
Helmut Tschemernjak | 67:d3afd803f40d | 244 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 245 | |
Helmut Tschemernjak | 65:b2d98328fcba | 246 | class Timer { |
Helmut Tschemernjak | 65:b2d98328fcba | 247 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 248 | void start(void) { |
Helmut Tschemernjak | 67:d3afd803f40d | 249 | _time = ns_getTicker(); |
Helmut Tschemernjak | 67:d3afd803f40d | 250 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 251 | uint32_t read_sec(void) { |
Helmut Tschemernjak | 67:d3afd803f40d | 252 | int64_t n = ns_getTicker() - (uint64_t)_time; |
Helmut Tschemernjak | 67:d3afd803f40d | 253 | n /= (uint64_t)1000000000; |
Helmut Tschemernjak | 67:d3afd803f40d | 254 | return n; |
Helmut Tschemernjak | 65:b2d98328fcba | 255 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 256 | uint32_t read_ms(void) { |
Helmut Tschemernjak | 67:d3afd803f40d | 257 | int64_t n = ns_getTicker() - (uint64_t)_time; |
Helmut Tschemernjak | 67:d3afd803f40d | 258 | n /= (uint64_t)1000000; |
Helmut Tschemernjak | 67:d3afd803f40d | 259 | return n; |
Helmut Tschemernjak | 65:b2d98328fcba | 260 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 261 | uint32_t read_us(void) { |
Helmut Tschemernjak | 67:d3afd803f40d | 262 | int64_t n = ns_getTicker() - (uint64_t)_time; |
Helmut Tschemernjak | 67:d3afd803f40d | 263 | n /= (uint64_t)1000; |
Helmut Tschemernjak | 67:d3afd803f40d | 264 | return n; |
Helmut Tschemernjak | 65:b2d98328fcba | 265 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 266 | private: |
Helmut Tschemernjak | 67:d3afd803f40d | 267 | uint64_t _time; |
Helmut Tschemernjak | 65:b2d98328fcba | 268 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 269 | |
Helmut Tschemernjak | 65:b2d98328fcba | 270 | |
Helmut Tschemernjak | 65:b2d98328fcba | 271 | class Timeout { |
Helmut Tschemernjak | 65:b2d98328fcba | 272 | public: |
Helmut Tschemernjak | 65:b2d98328fcba | 273 | Timeout() : _func() { |
Helmut Tschemernjak | 65:b2d98328fcba | 274 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 275 | ~Timeout() { |
Helmut Tschemernjak | 65:b2d98328fcba | 276 | detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 277 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 278 | |
Helmut Tschemernjak | 67:d3afd803f40d | 279 | void attach_sec(Callback<void()> func, uint32_t secs) { |
Helmut Tschemernjak | 67:d3afd803f40d | 280 | if (secs == 0) |
Helmut Tschemernjak | 67:d3afd803f40d | 281 | return detach(); |
Helmut Tschemernjak | 67:d3afd803f40d | 282 | _func = func; |
Helmut Tschemernjak | 67:d3afd803f40d | 283 | _timeout = ns_getTicker() + (uint64_t)secs * (uint64_t)1000000000; |
Helmut Tschemernjak | 67:d3afd803f40d | 284 | insert(); |
Helmut Tschemernjak | 67:d3afd803f40d | 285 | restart(); |
Helmut Tschemernjak | 67:d3afd803f40d | 286 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 287 | |
Helmut Tschemernjak | 67:d3afd803f40d | 288 | void attach(Callback<void()> func, uint32_t msecs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 289 | if (msecs == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 290 | return detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 291 | _func = func; |
Helmut Tschemernjak | 67:d3afd803f40d | 292 | _timeout = ns_getTicker() + (uint64_t)msecs * (uint64_t)1000000; |
Helmut Tschemernjak | 65:b2d98328fcba | 293 | insert(); |
Helmut Tschemernjak | 65:b2d98328fcba | 294 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 295 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 296 | |
Helmut Tschemernjak | 65:b2d98328fcba | 297 | void attach_us(Callback<void()> func, long usecs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 298 | if (usecs == 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 299 | return detach(); |
Helmut Tschemernjak | 65:b2d98328fcba | 300 | _func = func; |
Helmut Tschemernjak | 67:d3afd803f40d | 301 | _timeout = ns_getTicker() + (uint64_t)usecs * (uint64_t)1000; |
Helmut Tschemernjak | 65:b2d98328fcba | 302 | insert(); |
Helmut Tschemernjak | 65:b2d98328fcba | 303 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 304 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 305 | |
Helmut Tschemernjak | 67:d3afd803f40d | 306 | void detach(void) { |
Helmut Tschemernjak | 65:b2d98328fcba | 307 | _func = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 308 | remove(); |
Helmut Tschemernjak | 65:b2d98328fcba | 309 | restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 310 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 311 | |
Helmut Tschemernjak | 65:b2d98328fcba | 312 | static void _irq_handler(Timeout *tp) { |
Helmut Tschemernjak | 65:b2d98328fcba | 313 | if (tp) { |
Helmut Tschemernjak | 65:b2d98328fcba | 314 | tp->_func(); |
Helmut Tschemernjak | 65:b2d98328fcba | 315 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 316 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 317 | |
Helmut Tschemernjak | 67:d3afd803f40d | 318 | static void restart(void); |
Helmut Tschemernjak | 67:d3afd803f40d | 319 | uint64_t _timeout; // in ns this lasts for 539 years. |
Helmut Tschemernjak | 65:b2d98328fcba | 320 | protected: |
Helmut Tschemernjak | 65:b2d98328fcba | 321 | void insert(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 322 | void remove(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 323 | private: |
Helmut Tschemernjak | 65:b2d98328fcba | 324 | Callback<void()> _func; |
Helmut Tschemernjak | 65:b2d98328fcba | 325 | }; |
Helmut Tschemernjak | 65:b2d98328fcba | 326 | |
Helmut Tschemernjak | 65:b2d98328fcba | 327 | #endif // __ARDUINO_MBED_H__ |
Helmut Tschemernjak | 65:b2d98328fcba | 328 | |
Helmut Tschemernjak | 40:0165543c0986 | 329 | #endif // ARDUINO |