Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active November 25, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricImpSampleCode/116af6a0ba225fd57b92 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/116af6a0ba225fd57b92 to your computer and use it in GitHub Desktop.
A sample class available for logging via UART while an imp is disconnected from WiFi.
// UART Logger
// Copyright (c) 2014-19, Electric Imp, Inc.
// Licence: MIT
class Logger {
// Code version
static VERSION = "1.2.0";
// Instance properties
_uart = null;
_txsize = 80;
_enabled = false;
_configured = false;
constructor(uart = null, baudrate = 115200, txsize = 80, 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)
_configure(uart, baudrate, txsize, enable);
}
function enable() {
if (!_configured) _configure();
_enabled = true;
}
function disable() {
_enabled = false;
}
function log(message) {
if (_enabled) _logtouart("[LOG]", message);
server.log(message);
}
function error(message) {
if (_enabled) _logtouart("[ERR]", message);
server.error(message);
}
// Private Methods **DO NOT CALL DIRECTLY**
function _logtouart(prefix, message) {
if (_enabled) {
if (!_configured) _configure();
local s = prefix + " (" + _settimestring() + ") " + message;
// Break the message into lines of 'txsize' characters (last may be less)
local done = false;
do {
local t = "";
if (s.len() > _txsize) {
t = s.slice(0, _txsize);
s = s.slice(s.len() - _txsize);
} else {
t = s;
done = true;
}
_uart.write(t + "\r\n");
} while (!done);
}
}
function _settimestring(time = null) {
// If 'time' is supplied, it must be a table formatted as per the output of 'date()'
local now = time != null ? time : date();
return format("%04d-%02d-%02d %02d:%02d:%02d %s", now.year, now.month + 1, now.day, now.hour, now.min, now.sec, "UTC");
}
function _configure(uart = null, baudrate = 115200, txsize = 80, enable = true) {
if (uart == null) {
// No passed in UART object, so pick a default one according to imp type
switch(imp.info().type) {
case "imp001":
case "imp002":
uart = hardware.uart12;
break;
case "imp003":
uart = hardware.uartDM;
break;
case "imp004m":
uart = hardware.uartHJ;
break;
case "imp005":
uart = hardware.uart0;
case "impC001":
uart = hardware.uartNU;
}
}
if (typeof enable != "bool") enable = false;
_enabled = enable;
if (typeof txsize != "integer" || txsize < 80 || txsize > 256) txsize = 80;
_txsize = txsize;
// Configure UART and make sure we free any RX, CTS and RTS pins
// as they are not required by Logger
_uart = uart;
_uart.settxfifosize(txsize);
_uart.configure(baudrate, 8, PARITY_NONE, 1, NO_RX | NO_CTSRTS);
_configured = true;
}
}
// Paste in the Logger class code here
// Code up for imp004m
globalDebug <- Logger(hardware.uartHJ, 19200);
// This will log to UART and the online log
globalDebug.log("Testing, Testing, 123...");
// This will log to the online log only
globalDebug.disable();
globalDebug.log("Testing, Testing, 456...");
// This will log to UART and the online log
globalDebug.enable();
globalDebug.error("Oops! An error occurred");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment