This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seriallog.nut
179 lines (167 loc) · 5.68 KB
/
seriallog.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Code version for Squinter
#version "2.2.0"
/**
* Serial logger
*
* Incorporates code which sends log and error messages to UART as well as to the imp API methods server.log()*
* and server.error(). To use this code as-is, replace all of your application's server.log() and server.error()
* calls with seriallog.log() and *seriallog.error()
*
* @Author Tony Smith (@smittytone)
* @licence MIT
* @version 2.2.0
*
* @table
*
*/
seriallog <- {
// Public Properties
"uart" : null,
"enabled" : false,
"configured": false,
"txsize": 120,
/**
* Configures the serial logger
*
* @param {imp::uart} [uart] - The imp UART object. Default: imp-specific UART (see code)
* @param {integer} [baudrate] - The speed of the UART in bits per second. Default: 115200
* @param {integer} [txsize] - The size of the UART transmit buffer in bytes. Default: 80
* @param {bool} [enable] - Whether to enable the serial logger immediately. Default: true
*
*/
"configure" : function(uart = null, baudrate = 115200, txsize = 120, enable = true) {
// Pass a UART object, eg. hardware.uart6E; your preferred baud rate; and initial state
// NOTE UART is enabled by default and UART will be chosen for you if you pass in null
// If you don't call configure, serial logging is disabled by default but
// server logging will continue (provided the imp is connected)
if (uart == null) {
// No passed in UART object, so pick one and log it
local u = "";
switch(imp.info().type) {
case "imp001":
case "imp002":
uart = hardware.uart12;
u = "12";
break;
case "imp003":
uart = hardware.uartDM;
u = "DM";
break;
case "imp004m":
uart = hardware.uartHJ;
u = "HJ";
break;
case "imp005":
uart = hardware.uart0;
u = "0";
case "imp006":
uart = hardware.uartABCD;
u = "ABCD";
case "impC001":
uart = hardware.uartNU;
u = "NU";
}
server.log("Read the serial log via hardware.uart" + u);
} else {
server.log("Read the serial log via the programmed UART");
}
seriallog.uart = uart;
if (typeof txsize != "integer" || txsize < 80) txsize = 80;
seriallog.txsize = txsize;
seriallog.uart.settxfifosize(txsize);
local speed = seriallog.uart.configure(baudrate, 8, PARITY_NONE, 1, NO_RX | NO_CTSRTS);
server.log("UART speed: " + speed);
seriallog.configured = true;
if (typeof enable == "bool") seriallog.enabled = enable;
},
/**
* Enable logging via serial
*
*/
"enable" : function() {
if (!seriallog.configured) seriallog.configure();
seriallog.enabled = true;
},
/**
* Disable logging via serial
*
*/
"disable" : function() {
seriallog.enabled = false;
},
/**
* Log a message. This also (always) logs to the server
*
* @param {string} message - The message to be logged
*
*/
"log": function(message) {
seriallog._logtouart(message);
server.log(message);
},
/**
* Log an error message. This also (always) logs to the server
*
* @param {string} message - The error message to be logged
*
*/
"error": function(message) {
seriallog._logtouart(message);
server.error(message);
},
// ********** Private Methods DO NOT CALL DIRECTLY **********
/**
* Writes the supplied text string ('message') to UART
*
* @private
*
* @param {string} message - The error message to be logged
*
*/
"_logtouart": function(message) {
if (seriallog.enabled) {
if (!seriallog.configured) seriallog.configure();
local s = "[IMP LOG] (" + seriallog._formatTimeString() + ") " + message;
// Break the message into lines of 'txsize' characters (last may be less)
local done = false;
do {
local t = "";
if (s.len() > seriallog.txsize) {
t = s.slice(0, seriallog.txsize);
s = s.slice(seriallog.txsize, s.len());
} else {
t = s;
done = true;
}
seriallog.uart.write(t + "\r\n");
} while (!done);
}
},
/**
* Format a timestamp string, either the current time (default; pass null as the argument),
* or a specific time (pass a timestamp as the argument). Includes the timezone
* NOTE It is able to make use of the 'utilities' BST checker, if also included in your application
*
* @private
*
* @param {table} [n] - A Squirrel date/time description table (see date()). Default: current date
*
* @returns {string} The timestamp string, eg. "2019-01-31 12:45:0 +1:00"
*
*/
"_formatTimeString": function(time = null) {
local bst = false;
if ("utilities" in getroottable()) bst = utilities.isBST();
if (time == null) time = date();
time.hour += (bst ? 1 : 0);
if (time.hour > 23) time.hour -= 24;
local z = bst ? "+01:00" : "UTC";
return format("%04d-%02d-%02d %02d:%02d:%02d %s", time.year, time.month + 1, time.day, time.hour, time.min, time.sec, z);
}
}
log <- function(msg) {
seriallog.log(msg);
}
err <- function(msg) {
seriallog.error(msg);
}