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