summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLenczu Vex <kuba.lenczowski03@gmail.com>2024-10-13 00:37:05 +0200
committerLenczu Vex <kuba.lenczowski03@gmail.com>2024-10-13 00:37:05 +0200
commit3f96783eba8768109b449e0aebed67052e832661 (patch)
tree7882655767a70d2c3a9bd2269e3873e34de443d5
parentd278e9e866802734fff1c11a38771028db36b085 (diff)
Fixes and some redesign
-rw-r--r--ibus.c45
-rw-r--r--ibus.h12
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<channelC*2; i++){
if (i%2)
- *(frame+i+2)=(char) (*(vals+(i/2)))>>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 <stdint.h>
+
#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