added tasks and some extra helper things
This commit is contained in:
parent
dabe013a4e
commit
d2c73c9fb1
@ -1,3 +1,6 @@
|
|||||||
# Doing serial with c
|
# serial for ip2
|
||||||
|
TODO:
|
||||||
# dont ask
|
- [ ] create read function
|
||||||
|
- [ ] create write function
|
||||||
|
- [ ] how to be aware of data arriving
|
||||||
|
- [ ] other
|
||||||
@ -33,12 +33,18 @@ void comm_end_communication(struct sp_port *port) {
|
|||||||
sp_free_port_list(port_list);
|
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
|
// 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
|
//TODO: IMPLEMENT
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int comm_await() {
|
||||||
|
// Await data from the robot
|
||||||
|
//TODO: IMPLEMENT
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct sp_port **comm_get_port_list() {
|
struct sp_port **comm_get_port_list() {
|
||||||
struct sp_port **port_list;
|
struct sp_port **port_list;
|
||||||
enum sp_return result = sp_list_ports(&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++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
bit_array[i] = (data >> i) & 1;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <libserialport.h>
|
#include <libserialport.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef u_int8_t byte;
|
||||||
|
|
||||||
void comm_init_communication();
|
void comm_init_communication();
|
||||||
|
|
||||||
@ -23,5 +24,6 @@ void comm_open_port(struct sp_port *port);
|
|||||||
|
|
||||||
int check(enum sp_return result);
|
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
|
#endif //COMMS_H
|
||||||
|
|||||||
11
main.c
11
main.c
@ -11,13 +11,13 @@ extern struct sp_port *port;
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
comm_init_communication();
|
comm_init_communication();
|
||||||
|
|
||||||
u_int8_t data = 205;
|
byte data = 205;
|
||||||
int size = 1;
|
int size = 1;
|
||||||
printf("size: %d\n", size);
|
printf("size: %d\n", size);
|
||||||
unsigned int timeout = 50 * size; //50 ms timeout per byte
|
unsigned int timeout = 50 * size; //50 ms timeout per byte
|
||||||
|
|
||||||
check(sp_nonblocking_write(port, &data, size));
|
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));
|
int result = check(sp_blocking_read(port, buf, size, timeout));
|
||||||
|
|
||||||
@ -27,13 +27,14 @@ int main(int argc, char *argv[]) {
|
|||||||
buf[result] = '\0';
|
buf[result] = '\0';
|
||||||
printf("%s\n", buf);
|
printf("%s\n", buf);
|
||||||
|
|
||||||
u_int8_t bits[8];
|
byte bits[8];
|
||||||
uint8_to_bit_array(bits, buf[0]);
|
uint8_to_bit_array(bits, buf);
|
||||||
free(buf);
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
printf("%d ", bits[i]);
|
printf("%d ", bits[i]);
|
||||||
}
|
}
|
||||||
|
byte reconv_data = bit_array_to_uint8(bits);
|
||||||
|
printf("data: %d\n", reconv_data);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
comm_end_communication(port);
|
comm_end_communication(port);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user