added tasks and some extra helper things

This commit is contained in:
Nicholas Stănescu 2025-04-26 18:05:52 +02:00 committed by Nanokloon
parent dabe013a4e
commit d2c73c9fb1
4 changed files with 33 additions and 11 deletions

View File

@ -1,3 +1,6 @@
# Doing serial with c
# dont ask
# serial for ip2
TODO:
- [ ] create read function
- [ ] create write function
- [ ] how to be aware of data arriving
- [ ] other

View File

@ -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;
}

View File

@ -7,6 +7,7 @@
#include <libserialport.h>
#include <stdlib.h>
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

11
main.c
View File

@ -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);