add can support
This commit is contained in:
parent
6cd0fbcf3f
commit
673cdb16a0
53
src/main.c
53
src/main.c
@ -6,6 +6,10 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <linux/can.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
|
||||
const int digit_bitmaps[10][5][3] = {
|
||||
{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}, // 0
|
||||
@ -99,6 +103,26 @@ void get_fake_data(Tel *t) {
|
||||
t->mot_temp.data = t->mot_temp.ldata + ((float)rand() / (float)RAND_MAX) * (float)(t->mot_temp.hdata - t->mot_temp.ldata);
|
||||
}
|
||||
|
||||
void read_can(Tel *t, int soc) {
|
||||
struct can_frame frame;
|
||||
int nbytes = read(soc, &frame, sizeof(struct can_frame));
|
||||
|
||||
if (nbytes > 0) {
|
||||
|
||||
switch (frame.can_id) {
|
||||
|
||||
case 0x382:
|
||||
if (frame.can_dlc >= 2) {
|
||||
t->rpm.data = frame.data[0] | (frame.data[1] << 8);
|
||||
t->speed.data = t->rpm.data * 0.017;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long now_ms(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
@ -338,6 +362,11 @@ int main(int argc, char **argv) {
|
||||
int option;
|
||||
int delay = 1;
|
||||
int fake_data = 0;
|
||||
int use_can = 0;
|
||||
int soc = 0;
|
||||
struct sockaddr_can addr;
|
||||
struct ifreq ifr;
|
||||
|
||||
|
||||
while ((option = getopt(argc, argv, "d:hf")) !=-1) {
|
||||
switch (option) {
|
||||
@ -350,12 +379,32 @@ int main(int argc, char **argv) {
|
||||
case 'f' :
|
||||
fake_data = 1;
|
||||
break;
|
||||
case 'c' :
|
||||
use_can = 1;
|
||||
break;
|
||||
case 'h' :
|
||||
printf("Usage: ./cocomobile-tui [-d int DELAY by how much to slow down] [-h HELP] [-f generate fake data]\n");
|
||||
printf("Usage: ./cocomobile-tui [-d int DELAY by how much to slow down] [-h HELP] [-f generate fake data] [-c enable can]\n");
|
||||
return(0);
|
||||
}
|
||||
}
|
||||
|
||||
if (use_can) {
|
||||
soc = socket(PF_CAN, SOCK_RAW, CAN_RAW);
|
||||
if (soc < 0) {
|
||||
perror("socket error");
|
||||
return 1;
|
||||
}
|
||||
strcpy(ifr.ifr_name, "can0");
|
||||
ioctl(soc, SIOCGIFINDEX, &ifr);
|
||||
addr.can_family = AF_CAN;
|
||||
addr.can_ifindex = ifr.ifr_ifindex;
|
||||
if (bind(soc, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
perror("bind error");
|
||||
close(soc);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
initscr();
|
||||
noecho();
|
||||
cbreak();
|
||||
@ -429,7 +478,7 @@ int main(int argc, char **argv) {
|
||||
if (fake_data) {
|
||||
get_fake_data(&tel);
|
||||
} else {
|
||||
//real_data()
|
||||
if (use_can) read_can(&tel, soc);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user