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