| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdarg.h>
 | 
					
						
							|  |  |  | #include <inttypes.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <ncurses.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <hexagram/can.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int usage(int argc, char **argv, const char *message, ...) { | 
					
						
							|  |  |  |     if (message) { | 
					
						
							|  |  |  |         va_list args; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         va_start(args, message); | 
					
						
							|  |  |  |         vfprintf(stderr, message, args); | 
					
						
							|  |  |  |         va_end(args); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fprintf(stderr, "usage: %s canif\n", argv[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char **argv) { | 
					
						
							|  |  |  |     WINDOW *win; | 
					
						
							|  |  |  |     hexagram_can_if *can_if; | 
					
						
							|  |  |  |     struct can_frame frame; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |     struct timeval now, last; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |     if (argc != 2) { | 
					
						
							|  |  |  |         return usage(argc, argv, NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((can_if = hexagram_can_if_open(argv[1])) == NULL) { | 
					
						
							|  |  |  |         perror("hexagram_can_if_open()"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         goto error_can_if_open; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |     gettimeofday(&now, NULL); | 
					
						
							|  |  |  |     memcpy(&last, &now, sizeof(now)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |     initscr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     win = newwin(LINES, COLS, 0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (hexagram_can_if_read(can_if, &frame) >= 0) { | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |         gettimeofday(&now, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |         if (frame.can_id == 0x280) { | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |             double rpm = 0.25 * (double)(frame.data[2] | (frame.data[3] << 8)); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |             wmove(win, 1, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |             wprintw(win, "%6.1lf RPM", rpm); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |         } else if (frame.can_id == 0x540) { | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |             wmove(win, 6, 0); | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ((frame.data[7] & 0xc) == 0xc) { | 
					
						
							|  |  |  |                 uint8_t gear = ((frame.data[7] & 0xf0) >> 4) - 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 wprintw(win, "1 2 3 4 5 6"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |                 wmove(win, 7, 0); | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |                 wprintw(win, "                   "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |                 wmove(win, 7, gear * 2); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |                 wprintw(win, "_"); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 uint8_t pos = ((frame.data[7] & 0xf0) >> 4) - 2; | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |                 wprintw(win, "P R N D S  "); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |                 wmove(win, 7, 0); | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |                 wprintw(win, "                   "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |                 wmove(win, 7, pos * 2); | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 wprintw(win, "_"); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |         } else if (frame.can_id == 0x5a0) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							| 
									
										
										
										
											2019-02-20 22:15:30 -06:00
										 |  |  |              * Speed is expressed in units of 0.001 wheel cycles per second | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |              */ | 
					
						
							| 
									
										
										
										
											2019-02-20 22:15:30 -06:00
										 |  |  |             double rps = 0.001 * (double)((frame.data[1] >> 1) | 
					
						
							|  |  |  |                                         | (frame.data[2] << 8)); | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             double kph = (2.032 * rps * 3600) / 1000.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |             double g = 0.01 * (((double)frame.data[0]) - 127.0); | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |             wmove(win, 2, 0); | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |             wprintw(win, "%6.1lf kph", kph); | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             wmove(win, 4, 0); | 
					
						
							|  |  |  |             wprintw(win, "%6.1lf  G lateral", g); | 
					
						
							| 
									
										
										
										
											2019-02-21 00:17:53 -06:00
										 |  |  |         } else if (frame.can_id == 0x420) { | 
					
						
							|  |  |  |             double temp = ((double)frame.data[1] - 100) / 2.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             wmove(win, 3, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |             wprintw(win, "%6.1lf °C ambient", temp); | 
					
						
							|  |  |  |         } else if (frame.can_id == 0x4a0) { | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 21:03:41 -05:00
										 |  |  |         if (((now.tv_sec * 1000000) + now.tv_usec) - ((last.tv_sec * 1000000) + last.tv_usec) > 100000) { | 
					
						
							| 
									
										
										
										
											2019-02-20 21:44:40 -06:00
										 |  |  |             memcpy(&last, &now, sizeof(now)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             wrefresh(win); | 
					
						
							| 
									
										
										
										
											2019-02-19 19:26:33 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hexagram_can_if_close(can_if); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error_can_if_open: | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |