From 3f96783eba8768109b449e0aebed67052e832661 Mon Sep 17 00:00:00 2001 From: Lenczu Vex Date: Sun, 13 Oct 2024 00:37:05 +0200 Subject: Fixes and some redesign --- ibus.c | 45 +++++++++++++++++---------------------------- ibus.h | 12 +++++++++--- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/ibus.c b/ibus.c index 217e6ca..3d7a8d0 100644 --- a/ibus.c +++ b/ibus.c @@ -1,37 +1,26 @@ #include "ibus.h" +uint16_t CRC16Gen(*IBusFrame frame){ + uint16_t checksum = 0xFFFF; + for (int i = 2; i < frame[0]-2; i++) { + checksum -= ibusPacket[i]; + } + + return checksum; +} + int encodeIBus(*IBusFrame frame, *channelValues vals){ - *frame=startByteAlpha; - *(frame+1)=startByteAlpha; + *frame=IBusSize; + *(frame+1)=startByteBeta; for (int i=0; i>sizeof(char); + *(frame+i+2)=(uint8_t) (*(vals+(i/2)))>>sizeof(uint8_t); else - *(frame+i+2)=(char) *(vals+(i/2)); - + *(frame+i+2)=(uint8_t) (*(vals+(i/2)) & 0xFF); + } + uint16_t checksum=CRC16Gen(frame); + *(frame+frame[0]-2)=(uint8_t)(checksum & 0xFF) + *(frame+frame[0]-1)=(uint8_t)(checksum>>8) } -int CRC16Gen(*IBusFrame frame){ - unsigned char i; - unsigned int data; - unsigned int crc = 0xffff; - - if (length == 0) - return (~crc); - - do{ - for (i=0, data=(unsigned int)0xff & *data_p++;i < 8; i++, data >>= 1){ - if ((crc & 0x0001) ^ (data & 0x0001)) - crc = (crc >> 1) ^ POLY; - else - crc >>= 1; - } - } while (--length); - - crc = ~crc; - data = crc; - crc = (crc << 8) | (data >> 8 & 0xff); - - return (crc); -} diff --git a/ibus.h b/ibus.h index 3be61c8..fb1d7fb 100644 --- a/ibus.h +++ b/ibus.h @@ -1,7 +1,13 @@ +#ifndef IBUS_H +#define IBUS_H + +#include + #define IBusSize 34 #define channelC 15 -#define startByteAlpha 0x20 +#define IBusSize 0x20 #define startByteBeta 0x40 -typedef char[IBusSize] IBusFrame -typedef unsigned short int[15] channelValues +typedef uint8_t[IBusSize] IBusFrame +typedef uint16_t[15] channelValues +#endif -- cgit v1.2.3-70-g09d2