From d2c73c9fb1bd4118123692c0ec33c08385d60f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicholas=20St=C4=83nescu?= Date: Sat, 26 Apr 2025 18:05:52 +0200 Subject: [PATCH] added tasks and some extra helper things --- README.MD | 9 ++++++--- communication.c | 20 ++++++++++++++++++-- communication.h | 4 +++- main.c | 11 ++++++----- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.MD b/README.MD index a1908f1..6e55aaf 100644 --- a/README.MD +++ b/README.MD @@ -1,3 +1,6 @@ -# Doing serial with c - -# dont ask \ No newline at end of file +# serial for ip2 +TODO: +- [ ] create read function +- [ ] create write function +- [ ] how to be aware of data arriving +- [ ] other \ No newline at end of file diff --git a/communication.c b/communication.c index e7ba785..602d7da 100644 --- a/communication.c +++ b/communication.c @@ -33,12 +33,18 @@ void comm_end_communication(struct sp_port *port) { sp_free_port_list(port_list); } -int comm_write(u_int8_t data) { +int comm_write(byte data) { // we are writing 8 bit bytes to the fpga anyways and pc -> fpga only needs to tell what do to next, can be fit in 8 bits //TODO: IMPLEMENT return 0; } +int comm_await() { + // Await data from the robot + //TODO: IMPLEMENT + return 0; +} + struct sp_port **comm_get_port_list() { struct sp_port **port_list; enum sp_return result = sp_list_ports(&port_list); @@ -95,8 +101,18 @@ int check(enum sp_return result) } } -void uint8_to_bit_array(u_int8_t bit_array[], u_int8_t data) { +void uint8_to_bit_array(byte bit_array[], byte data) { for (int i = 0; i < 8; i++) { bit_array[i] = (data >> i) & 1; } } + +byte bit_array_to_uint8(byte bit_array[]) { + byte data=0; + byte currentpow=1; + for (int i = 0; i < 8; i++) { + data+=bit_array[i]*currentpow; + currentpow*=2; + } + return data; +} diff --git a/communication.h b/communication.h index c20dd67..7f617b4 100644 --- a/communication.h +++ b/communication.h @@ -7,6 +7,7 @@ #include #include +typedef u_int8_t byte; void comm_init_communication(); @@ -23,5 +24,6 @@ void comm_open_port(struct sp_port *port); int check(enum sp_return result); -void uint8_to_bit_array(u_int8_t *bit_array, u_int8_t data); +void uint8_to_bit_array(byte *bit_array, byte data); +byte bit_array_to_uint8(const byte bit_array[]); #endif //COMMS_H diff --git a/main.c b/main.c index ace6a69..b047b6c 100644 --- a/main.c +++ b/main.c @@ -11,13 +11,13 @@ extern struct sp_port *port; int main(int argc, char *argv[]) { comm_init_communication(); - u_int8_t data = 205; + byte data = 205; int size = 1; printf("size: %d\n", size); unsigned int timeout = 50 * size; //50 ms timeout per byte check(sp_nonblocking_write(port, &data, size)); - char *buf = malloc(size + 1); + byte *buf = malloc(size + 1); int result = check(sp_blocking_read(port, buf, size, timeout)); @@ -27,13 +27,14 @@ int main(int argc, char *argv[]) { buf[result] = '\0'; printf("%s\n", buf); - u_int8_t bits[8]; - uint8_to_bit_array(bits, buf[0]); - free(buf); + byte bits[8]; + uint8_to_bit_array(bits, buf); for (int i = 0; i < 8; i++) { printf("%d ", bits[i]); } + byte reconv_data = bit_array_to_uint8(bits); + printf("data: %d\n", reconv_data); printf("\n"); comm_end_communication(port);