![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork for LoDev S76S
Dependencies: mbed BufferedSerial SX1276GenericLib USBDeviceHT
utils.cpp@21:6379a5650867, 2019-02-18 (annotated)
- Committer:
- steve918
- Date:
- Mon Feb 18 22:57:14 2019 +0000
- Revision:
- 21:6379a5650867
- Parent:
- 20:a1029437adca
First working rev for LoDev S76S
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 17:98f2528e8399 | 1 | /* |
Helmut64 | 17:98f2528e8399 | 2 | * Copyright (c) 2018 Helmut Tschemernjak |
Helmut64 | 17:98f2528e8399 | 3 | * 30826 Garbsen (Hannover) Germany |
Helmut64 | 17:98f2528e8399 | 4 | */ |
Helmut64 | 17:98f2528e8399 | 5 | #include "main.h" |
Helmut64 | 17:98f2528e8399 | 6 | |
Helmut64 | 17:98f2528e8399 | 7 | time_t cvt_date(char const *date, char const *time); |
Helmut64 | 17:98f2528e8399 | 8 | |
Helmut64 | 17:98f2528e8399 | 9 | BufferedSerial *ser; |
Helmut64 | 17:98f2528e8399 | 10 | bool _useDprintf; |
Helmut64 | 17:98f2528e8399 | 11 | |
Helmut64 | 20:a1029437adca | 12 | /* |
Helmut64 | 20:a1029437adca | 13 | * keep a timer running to avoid USB hangup in sleep, |
Helmut64 | 20:a1029437adca | 14 | * in newer mbed versions sleep calls deepsleep which may |
Helmut64 | 20:a1029437adca | 15 | * hangup the USBSerial. |
Helmut64 | 20:a1029437adca | 16 | * For this reason we keep a timer pending which avoids deepsleep. |
Helmut64 | 20:a1029437adca | 17 | */ |
Helmut64 | 20:a1029437adca | 18 | static Timeout busyTimer; |
Helmut64 | 20:a1029437adca | 19 | |
Helmut64 | 20:a1029437adca | 20 | void busyTimerFunc(void) |
Helmut64 | 20:a1029437adca | 21 | { |
Helmut64 | 20:a1029437adca | 22 | busyTimer.attach(callback(&busyTimerFunc), 300); |
Helmut64 | 20:a1029437adca | 23 | } |
Helmut64 | 20:a1029437adca | 24 | |
Helmut64 | 17:98f2528e8399 | 25 | void InitSerial(int timeout, DigitalOut *led) |
Helmut64 | 17:98f2528e8399 | 26 | { |
Helmut64 | 17:98f2528e8399 | 27 | _useDprintf = true; |
Helmut64 | 17:98f2528e8399 | 28 | { |
steve918 | 21:6379a5650867 | 29 | // need to turn rx low to avoid floating signal |
steve918 | 21:6379a5650867 | 30 | DigitalOut rx(USBRX); |
steve918 | 21:6379a5650867 | 31 | rx = 0; |
Helmut64 | 17:98f2528e8399 | 32 | } |
steve918 | 21:6379a5650867 | 33 | DigitalIn uartRX(USBRX); |
Helmut64 | 17:98f2528e8399 | 34 | { |
Helmut64 | 17:98f2528e8399 | 35 | ser = new BufferedSerial(USBTX, USBRX); |
steve918 | 21:6379a5650867 | 36 | ser->baud(115200); |
Helmut64 | 17:98f2528e8399 | 37 | ser->format(8); |
Helmut64 | 17:98f2528e8399 | 38 | } |
Helmut64 | 17:98f2528e8399 | 39 | time_t t = cvt_date(__DATE__, __TIME__); |
Helmut64 | 17:98f2528e8399 | 40 | if (t > time(NULL)) { |
Helmut64 | 17:98f2528e8399 | 41 | set_time(t); |
Helmut64 | 17:98f2528e8399 | 42 | } |
Helmut64 | 17:98f2528e8399 | 43 | |
Helmut64 | 17:98f2528e8399 | 44 | } |
Helmut64 | 17:98f2528e8399 | 45 | |
Helmut64 | 17:98f2528e8399 | 46 | void printTimeStamp() |
Helmut64 | 17:98f2528e8399 | 47 | { |
Helmut64 | 17:98f2528e8399 | 48 | static LowPowerTimer *timer; |
Helmut64 | 17:98f2528e8399 | 49 | if (!timer) { |
Helmut64 | 17:98f2528e8399 | 50 | timer = new LowPowerTimer(); |
Helmut64 | 17:98f2528e8399 | 51 | timer->start(); |
Helmut64 | 17:98f2528e8399 | 52 | } |
Helmut64 | 17:98f2528e8399 | 53 | time_t seconds = time(NULL); |
Helmut64 | 17:98f2528e8399 | 54 | struct tm *tm = localtime(&seconds); |
Helmut64 | 17:98f2528e8399 | 55 | int usecs = timer->read_us(); |
Helmut64 | 17:98f2528e8399 | 56 | if (usecs < 0) { |
Helmut64 | 17:98f2528e8399 | 57 | usecs = 0; |
Helmut64 | 17:98f2528e8399 | 58 | timer->stop(); |
Helmut64 | 17:98f2528e8399 | 59 | timer->reset(); |
Helmut64 | 17:98f2528e8399 | 60 | timer->start(); |
Helmut64 | 17:98f2528e8399 | 61 | } |
Helmut64 | 17:98f2528e8399 | 62 | int msecs = usecs % 1000000; |
Helmut64 | 17:98f2528e8399 | 63 | |
Helmut64 | 17:98f2528e8399 | 64 | rprintf("%02d:%02d:%02d.%06d ", tm->tm_hour, tm->tm_min, tm->tm_sec, msecs); |
Helmut64 | 17:98f2528e8399 | 65 | } |
Helmut64 | 17:98f2528e8399 | 66 | |
Helmut64 | 17:98f2528e8399 | 67 | void dprintf(const char *format, ...) |
Helmut64 | 17:98f2528e8399 | 68 | { |
Helmut64 | 17:98f2528e8399 | 69 | std::va_list arg; |
Helmut64 | 17:98f2528e8399 | 70 | |
Helmut64 | 17:98f2528e8399 | 71 | va_start(arg, format); |
Helmut64 | 17:98f2528e8399 | 72 | VAprintf(true, true, _useDprintf, format, arg); |
Helmut64 | 17:98f2528e8399 | 73 | va_end(arg); |
Helmut64 | 17:98f2528e8399 | 74 | } |
Helmut64 | 17:98f2528e8399 | 75 | |
Helmut64 | 17:98f2528e8399 | 76 | void rprintf(const char *format, ...) |
Helmut64 | 17:98f2528e8399 | 77 | { |
Helmut64 | 17:98f2528e8399 | 78 | std::va_list arg; |
Helmut64 | 17:98f2528e8399 | 79 | |
Helmut64 | 17:98f2528e8399 | 80 | va_start(arg, format); |
Helmut64 | 17:98f2528e8399 | 81 | VAprintf(false, false, _useDprintf, format, arg); |
Helmut64 | 17:98f2528e8399 | 82 | va_end(arg); |
Helmut64 | 17:98f2528e8399 | 83 | } |
Helmut64 | 17:98f2528e8399 | 84 | |
Helmut64 | 17:98f2528e8399 | 85 | void VAprintf(bool timstamp, bool newline, bool printEnabled, const char *format, va_list arg) |
Helmut64 | 17:98f2528e8399 | 86 | { |
Helmut64 | 17:98f2528e8399 | 87 | if (!printEnabled) |
Helmut64 | 17:98f2528e8399 | 88 | return; |
Helmut64 | 17:98f2528e8399 | 89 | |
Helmut64 | 17:98f2528e8399 | 90 | if (timstamp) |
Helmut64 | 17:98f2528e8399 | 91 | printTimeStamp(); |
Helmut64 | 17:98f2528e8399 | 92 | if (0) { |
Helmut64 | 17:98f2528e8399 | 93 | } else if (ser) { |
Helmut64 | 17:98f2528e8399 | 94 | // serial jas |
Helmut64 | 17:98f2528e8399 | 95 | int r = 0; |
Helmut64 | 17:98f2528e8399 | 96 | r = vsnprintf(NULL, 0, format, arg); |
Helmut64 | 17:98f2528e8399 | 97 | if (r < 82) { |
Helmut64 | 17:98f2528e8399 | 98 | char buffer[82+1]; |
Helmut64 | 17:98f2528e8399 | 99 | |
Helmut64 | 17:98f2528e8399 | 100 | vsnprintf(buffer, sizeof(buffer), format, arg); |
Helmut64 | 17:98f2528e8399 | 101 | r = ser->write(buffer, r); |
Helmut64 | 17:98f2528e8399 | 102 | } else { |
Helmut64 | 17:98f2528e8399 | 103 | char *buffer = new char[r+1]; |
Helmut64 | 17:98f2528e8399 | 104 | if (buffer) { |
Helmut64 | 17:98f2528e8399 | 105 | vsnprintf(buffer, r+1, format, arg); |
Helmut64 | 17:98f2528e8399 | 106 | r = ser->write(buffer, r); |
Helmut64 | 17:98f2528e8399 | 107 | delete[] buffer; |
Helmut64 | 17:98f2528e8399 | 108 | } else { |
Helmut64 | 17:98f2528e8399 | 109 | error("%s %d cannot alloc memory (%d bytes)!\r\n", __FILE__, __LINE__, r+1); |
Helmut64 | 17:98f2528e8399 | 110 | r = 0; |
Helmut64 | 17:98f2528e8399 | 111 | } |
Helmut64 | 17:98f2528e8399 | 112 | } |
Helmut64 | 17:98f2528e8399 | 113 | if (newline) |
Helmut64 | 17:98f2528e8399 | 114 | ser->write("\r\n", 2); |
Helmut64 | 17:98f2528e8399 | 115 | } |
Helmut64 | 17:98f2528e8399 | 116 | } |
Helmut64 | 17:98f2528e8399 | 117 | |
Helmut64 | 17:98f2528e8399 | 118 | |
Helmut64 | 17:98f2528e8399 | 119 | void dump(const char *title, const void *data, int len, bool dwords) |
Helmut64 | 17:98f2528e8399 | 120 | { |
Helmut64 | 17:98f2528e8399 | 121 | dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len); |
Helmut64 | 17:98f2528e8399 | 122 | |
Helmut64 | 17:98f2528e8399 | 123 | int i, j, cnt; |
Helmut64 | 17:98f2528e8399 | 124 | unsigned char *u; |
Helmut64 | 17:98f2528e8399 | 125 | const int width = 16; |
Helmut64 | 17:98f2528e8399 | 126 | const int seppos = 7; |
Helmut64 | 17:98f2528e8399 | 127 | |
Helmut64 | 17:98f2528e8399 | 128 | cnt = 0; |
Helmut64 | 17:98f2528e8399 | 129 | u = (unsigned char *)data; |
Helmut64 | 17:98f2528e8399 | 130 | while (len > 0) { |
Helmut64 | 17:98f2528e8399 | 131 | rprintf("%08x: ", (unsigned int)data + cnt); |
Helmut64 | 17:98f2528e8399 | 132 | if (dwords) { |
Helmut64 | 17:98f2528e8399 | 133 | unsigned int *ip = ( unsigned int *)u; |
Helmut64 | 17:98f2528e8399 | 134 | rprintf(" 0x%08x\r\n", *ip); |
Helmut64 | 17:98f2528e8399 | 135 | u+= 4; |
Helmut64 | 17:98f2528e8399 | 136 | len -= 4; |
Helmut64 | 17:98f2528e8399 | 137 | cnt += 4; |
Helmut64 | 17:98f2528e8399 | 138 | continue; |
Helmut64 | 17:98f2528e8399 | 139 | } |
Helmut64 | 17:98f2528e8399 | 140 | cnt += width; |
Helmut64 | 17:98f2528e8399 | 141 | j = len < width ? len : width; |
Helmut64 | 17:98f2528e8399 | 142 | for (i = 0; i < j; i++) { |
Helmut64 | 17:98f2528e8399 | 143 | rprintf("%2.2x ", *(u + i)); |
Helmut64 | 17:98f2528e8399 | 144 | if (i == seppos) |
Helmut64 | 17:98f2528e8399 | 145 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 146 | } |
Helmut64 | 17:98f2528e8399 | 147 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 148 | if (j < width) { |
Helmut64 | 17:98f2528e8399 | 149 | i = width - j; |
Helmut64 | 17:98f2528e8399 | 150 | if (i > seppos + 1) |
Helmut64 | 17:98f2528e8399 | 151 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 152 | while (i--) { |
Helmut64 | 17:98f2528e8399 | 153 | rprintf("%s", " "); |
Helmut64 | 17:98f2528e8399 | 154 | } |
Helmut64 | 17:98f2528e8399 | 155 | } |
Helmut64 | 17:98f2528e8399 | 156 | for (i = 0; i < j; i++) { |
Helmut64 | 17:98f2528e8399 | 157 | int c = *(u + i); |
Helmut64 | 17:98f2528e8399 | 158 | if (c >= ' ' && c <= '~') |
Helmut64 | 17:98f2528e8399 | 159 | rprintf("%c", c); |
Helmut64 | 17:98f2528e8399 | 160 | else |
Helmut64 | 17:98f2528e8399 | 161 | rprintf("."); |
Helmut64 | 17:98f2528e8399 | 162 | if (i == seppos) |
Helmut64 | 17:98f2528e8399 | 163 | rprintf(" "); |
Helmut64 | 17:98f2528e8399 | 164 | } |
Helmut64 | 17:98f2528e8399 | 165 | len -= width; |
Helmut64 | 17:98f2528e8399 | 166 | u += width; |
Helmut64 | 17:98f2528e8399 | 167 | rprintf("\r\n"); |
Helmut64 | 17:98f2528e8399 | 168 | } |
Helmut64 | 17:98f2528e8399 | 169 | rprintf("--\r\n"); |
Helmut64 | 17:98f2528e8399 | 170 | } |
Helmut64 | 17:98f2528e8399 | 171 | |
Helmut64 | 17:98f2528e8399 | 172 | /* |
Helmut64 | 17:98f2528e8399 | 173 | * Convert compile time to system time |
Helmut64 | 17:98f2528e8399 | 174 | */ |
Helmut64 | 17:98f2528e8399 | 175 | time_t |
Helmut64 | 17:98f2528e8399 | 176 | cvt_date(char const *date, char const *time) |
Helmut64 | 17:98f2528e8399 | 177 | { |
Helmut64 | 17:98f2528e8399 | 178 | char s_month[5]; |
Helmut64 | 17:98f2528e8399 | 179 | int year; |
Helmut64 | 17:98f2528e8399 | 180 | struct tm t; |
Helmut64 | 17:98f2528e8399 | 181 | static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
Helmut64 | 17:98f2528e8399 | 182 | sscanf(date, "%s %d %d", s_month, &t.tm_mday, &year); |
Helmut64 | 17:98f2528e8399 | 183 | sscanf(time, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec); |
Helmut64 | 17:98f2528e8399 | 184 | // Find where is s_month in month_names. Deduce month value. |
Helmut64 | 17:98f2528e8399 | 185 | t.tm_mon = (strstr(month_names, s_month) - month_names) / 3; |
Helmut64 | 17:98f2528e8399 | 186 | t.tm_year = year - 1900; |
Helmut64 | 17:98f2528e8399 | 187 | return (int)mktime(&t); |
Helmut64 | 17:98f2528e8399 | 188 | } |