Skip to content

Instantly share code, notes, and snippets.

@SeedyROM
Created October 17, 2019 04:18
Show Gist options
  • Save SeedyROM/346d00009bd144230e7afb98f34a9ff6 to your computer and use it in GitHub Desktop.
Save SeedyROM/346d00009bd144230e7afb98f34a9ff6 to your computer and use it in GitHub Desktop.
byte incomingBytes[32];
byte powerOn[] = {0x6, 0x01, 0x00, 0x19, 0x02, 0x1C};
byte powerOff[] = {0x6, 0x01, 0x00, 0x19, 0x01, 0x1F};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
inline boolean isMessageGet(byte* message) {
return message[0] == 0x05 &&
message[1] == 0x01 &&
message[2] == 0x00 &&
message[3] == 0x19 &&
message[4] == 0x1D;
}
byte counter = 0;
void loop() {
if(Serial.available() > 0) {
Serial.readBytes(incomingBytes, 5);
if(isMessageGet(incomingBytes)) {
if(counter++ % 2) {
Serial.write(powerOff, sizeof(powerOff));
digitalWrite(LED_BUILTIN, LOW);
} else {
Serial.write(powerOn, sizeof(powerOn));
digitalWrite(LED_BUILTIN, HIGH);
}
} else {
digitalWrite(LED_BUILTIN, HIGH);
}
}
}
const SerialPort = require('serialport');
const ByteLength = require('@serialport/parser-byte-length')
const comName = "/dev/ttyACM0"
const { Transform } = require('stream')
class VariableLengthParser extends Transform {
constructor(options = {}) {
super(options)
if(options.bufferSize) {
if (typeof options.bufferSize !== 'number') {
throw new TypeError('"bufferSize" is not a number')
}
if (options.bufferSize < 1) {
throw new TypeError('"bufferSize" is not greater than 0')
}
}
this.bufferSize = options.bufferSize || 32
this.position = 0
this.buffer = Buffer.alloc(this.bufferSize)
this.commandLength = 0;
}
_transform(chunk, encoding, cb) {
let cursor = 0
if(this.commandLength === 0) this.commandLength = chunk[0];
while (cursor < chunk.length) {
this.buffer[this.position] = chunk[cursor]
cursor++
this.position++
if (this.position === this.commandLength) {
this.push(this.buffer)
this.buffer = Buffer.alloc(this.bufferSize)
this.position = 0
this.commandLength = 0
}
}
cb()
}
_flush(cb) {
this.push(this.buffer.slice(0, this.position))
this.buffer = Buffer.alloc(this.bufferSize)
cb()
}
}
const port = new SerialPort(comName, {
baudRate: 9600,
});
const parser = port.pipe(new VariableLengthParser({bufferSize: 8}))
const calcPhilipsCheckSum = (bytes) => {
let sum = bytes[0];
for(let i = 1; i < bytes.length; i++) {
sum ^= bytes[i];
}
return sum;
}
const command = [0x05, 0x01, 0x00, 0x19];
const messageGet = Buffer.from([...command, calcPhilipsCheckSum(command)]);
const isScreenOn = (buffer) => {
if(buffer[0] === 0x06 &&
buffer[1] === 0x01 &&
buffer[2] === 0x00 &&
buffer[3] === 0x19)
{
if(buffer[4] === 0x01) {
return false;
}
if(buffer[4] === 0x02) {
return true;
}
throw new Error("Invalid power status");
}
}
port.open((error) => {
console.log("Found serial connection at " + comName);
// Send the fucking message
setInterval(() => {
console.log("Sending MESSAGE_GET command...");
port.write(messageGet, (error) => {
if(error) console.log("Failed to write: " + error);
});
}, 1000);
// Get the data sent back
parser.on("data", (data) => {
try {
const screenOn = isScreenOn(data);
console.log("Is screen powered? " + screenOn);
} catch(error) {
console.log("Invalid response receieved: ");1
console.log(error);
}
console.log(`[${Date.now()}] recv: ${data.toString("hex")}`);
});
// Log errors
port.on("error", (error) => {
console.error("Something went wrong: ")
console.error(error);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment