Created
June 4, 2022 03:34
-
-
Save matpratta/7a98c4c59d7490f97b998427edb8bdaf to your computer and use it in GitHub Desktop.
Small Espruino thingy that allows for bidirectional serial communication while also detecting Espruino IDE for development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start init | |
console.log('Preparing JS environment...'); | |
console.log('Booting I2c...'); | |
I2C1.setup({scl:NodeMCU.D1, sda:NodeMCU.D2}); | |
// Command object | |
var COMMANDS = {}; | |
// Response | |
function response (data) { | |
Serial1.print(JSON.stringify(data) + '\n'); | |
} | |
// Error handling | |
function error (code, data) { | |
response({ | |
error: code, | |
data: data, | |
}); | |
} | |
// Creates parser | |
function parseCommand (data) { | |
// Detects IDE, enables console | |
if (data.indexOf('print("<","<<",JSON.stringify(process.env),">>",">")') >= 0) { | |
Serial1.print('Serial 1 connected to Espruino IDE\n'); | |
Serial1.setConsole(); | |
return; | |
} | |
// Handles serial communication | |
Serial1.print('Serial1 > ' + data + '\n'); | |
// Ignores invalid commands | |
if (data.indexOf('{') < 0 || data.indexOf('}') < 0) { | |
return error('E_INVALID', data); | |
} | |
// Tries converting into JSON | |
var json = data.substring(data.indexOf('{'), data.indexOf('}') + 1); | |
var jsonData = null; | |
try { | |
jsonData = JSON.parse(json); | |
} catch (e) { | |
return error('E_PARSE', json); | |
} | |
// No command provided | |
if (!jsonData.cmd) return error('E_NOCMD', jsonData); | |
// Handles commands | |
(COMMANDS[jsonData.cmd] || () => { | |
error('E_INVALID_CMD', jsonData); | |
})(jsonData); | |
} | |
// Creates I2c scanner | |
function scanI2c( i2c, first, last ) { | |
if (typeof first === "undefined") { | |
first = 0x03; | |
} | |
if (typeof (last) === "undefined") { | |
last = 0x77; | |
} | |
print( " 0 1 2 3 4 5 6 7 8 9 a b c d e f" ); | |
for (var upper = 0; upper < 8; ++upper) { | |
var line = upper + "0: "; | |
for (var lower = 0; lower < 16; ++lower) { | |
var address = (upper << 4) + lower; | |
// Skip unwanted addresses | |
if ((address < first) || (address > last)) { | |
line += " "; | |
continue; | |
} | |
try { | |
i2c.readFrom( address, 1 ); | |
line += (address + 0x100).toString( 16 ).substr( -2 ); | |
line += " "; | |
} catch (err) { | |
line += "-- "; | |
} | |
} | |
print( line ); | |
} | |
} | |
// Boot logic to initialize Serial | |
function init () { | |
// Disconnects IDE from Serial | |
console.log('Done! Disconnecting Serial1...'); | |
Serial2.setConsole(); | |
// Setup Serial buffer | |
var serialBuffer = ''; | |
Serial1.setup(115200); | |
Serial1.on('data', (data) => { | |
data = data.replace('\r', '\n'); | |
var parseData = data.split('\n'); | |
if (data == '\n') { | |
parseCommand(serialBuffer); | |
serialBuffer = ''; | |
} else if (parseData.length > 1) { | |
serialBuffer += parseData[0]; | |
parseCommand(serialBuffer); | |
serialBuffer = parseData[1]; | |
} else { | |
serialBuffer += data; | |
} | |
}); | |
// Calls main function | |
main(); | |
} | |
function main () { | |
// Do stuff. | |
} | |
// Starts preparation | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment