From 955397da666bcc52b4b8726c99b6eb906b9deb7f Mon Sep 17 00:00:00 2001 From: Michael Andreasen Date: Fri, 21 Jan 2022 16:32:28 +0000 Subject: [PATCH] --- berry/nextion.be | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/berry/nextion.be b/berry/nextion.be index 5ad8e8f..8cf6293 100644 --- a/berry/nextion.be +++ b/berry/nextion.be @@ -1,10 +1,12 @@ -# Sonoff NSPanel Tasmota (Nextion with Flashing) driver v0.02 | code by peepshow-21 +# Sonoff NSPanel Tasmota (Nextion with Flashing) driver v0.03 | code by peepshow-21 # based on; # Sonoff NSPanel Tasmota driver v0.47 | code by blakadder and s-hadinger + class Nextion : Driver static CHUNK_FILE = "nextion" + static header = "PS" var flash_mode var ser @@ -14,6 +16,34 @@ class Nextion : Driver var tot_read var last_per + def crc16(data, poly) + if !poly poly = 0xA001 end + # CRC-16 MODBUS HASHING ALGORITHM + var crc = 0xFFFF + for i:0..size(data)-1 + crc = crc ^ data[i] + for j:0..7 + if crc & 1 + crc = (crc >> 1) ^ poly + else + crc = crc >> 1 + end + end + end + return crc + end + + def encode(payload) + var b = bytes() + b += self.header + b.add(nsp_type) # add a single byte + b.add(size(payload), 2) # add size as 2 bytes, little endian + b += bytes().fromstring(payload) + var msg_crc = self.crc16(b) + b.add(msg_crc, 2) # crc 2 bytes, little endian + return b + end + def encodenx(payload) var b = bytes().fromstring(payload) b += bytes('FFFFFF')