fix a bug in ini write

master
Ondřej Hruška 7 years ago
parent cf4314f1d8
commit c6f386d886
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 15
      gex/gex_settings.c
  2. 17
      main.c

@ -3,6 +3,8 @@
// //
#define GEX_H // to allow including other headers #define GEX_H // to allow including other headers
#include <utils/payload_builder.h>
#include "gex_settings.h" #include "gex_settings.h"
#include "gex_defines.h" #include "gex_defines.h"
#include "gex_internal.h" #include "gex_internal.h"
@ -18,15 +20,26 @@ uint32_t GEX_IniRead(GexClient *gex, char *buffer, uint32_t maxlen)
uint32_t actuallyRead = GEX_BulkRead(GEX_SysUnit(gex), &br); uint32_t actuallyRead = GEX_BulkRead(GEX_SysUnit(gex), &br);
if (actuallyRead == maxlen) {
actuallyRead--; // lose last char in favor of terminator
}
buffer[actuallyRead] = 0; // terminate
return actuallyRead; return actuallyRead;
} }
bool GEX_IniWrite(GexClient *gex, const char *buffer) bool GEX_IniWrite(GexClient *gex, const char *buffer)
{ {
uint8_t buf[8];
PayloadBuilder pb = pb_start(buf, 8, NULL);
pb_u32(&pb, (uint32_t) strlen(buffer));
GexBulk bw = (GexBulk){ GexBulk bw = (GexBulk){
.buffer = (uint8_t *) buffer, .buffer = (uint8_t *) buffer,
.len = (uint32_t) strlen(buffer), .len = (uint32_t) strlen(buffer),
.req_cmd = MSG_INI_WRITE .req_cmd = MSG_INI_WRITE,
.req_data = buf,
.req_len = (uint32_t) pb_length(&pb),
}; };
return GEX_BulkWrite(GEX_SysUnit(gex), &bw); return GEX_BulkWrite(GEX_SysUnit(gex), &bw);

@ -27,12 +27,18 @@ int main(void)
gex = GEX_Init("/dev/ttyACM0", 200); gex = GEX_Init("/dev/ttyACM0", 200);
if (!gex) exit(1); if (!gex) exit(1);
#if 0
// INI read example // INI read example
// char buffer[2000]; char buffer[2000];
// uint32_t len = GEX_IniRead(gex, buffer, 2000); uint32_t len = GEX_IniRead(gex, buffer, 2000);
// printf("%.*s", len, buffer); printf("%s", buffer);
// DO example GEX_IniWrite(gex, buffer);
printf("Written.\r\n");
#endif
#if 0
// DO example - there's a 10-LED bargraph connected to this unit
GexUnit *bg = GEX_GetUnit(gex, "bargraph", "DO"); GexUnit *bg = GEX_GetUnit(gex, "bargraph", "DO");
struct DO_Set { struct DO_Set {
@ -40,6 +46,8 @@ int main(void)
} __attribute__((packed)); } __attribute__((packed));
struct DO_Set cmd_set; struct DO_Set cmd_set;
// animation on the bargraph
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
int n = i%20; int n = i%20;
if(n>=10) n=10-(n-10); if(n>=10) n=10-(n-10);
@ -51,6 +59,7 @@ int main(void)
// all dark // all dark
cmd_set.bits = 0x3FF; cmd_set.bits = 0x3FF;
GEX_Send(bg, 0x00, (uint8_t *)&cmd_set, sizeof(cmd_set)); GEX_Send(bg, 0x00, (uint8_t *)&cmd_set, sizeof(cmd_set));
#endif
GEX_DeInit(gex); GEX_DeInit(gex);
return 0; return 0;

Loading…
Cancel
Save