Repository navigation
Expand file tree
/
Copy pathicsim.c
More file actions
3567 lines (3209 loc) · 105 KB
/
Copy pathicsim.c
File metadata and controls
3567 lines (3209 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ICSim++
*
* Instrument Control Simulator
*
* University of Adelaide
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "lib.h"
#include <errno.h>
//////////////////////////////////
////DEFAULT CONSTANT VARIABLES////
//////////////////////////////////
#ifndef DATA_DIR
#define DATA_DIR "./data/" // Needs trailing slash
#endif
#ifndef LOG_DIR
#define LOG_DIR "./logs/" // Needs trailing slash
#endif
#define SCREEN_WIDTH 720
#define SCREEN_HEIGHT 329
#define DOOR_LOCKED 0
#define DOOR_UNLOCKED 1
#define WINDOW_LOCKED 0
#define WINDOW_UNLOCKED 1
#define OFF 0
#define ON 1
#define CAN_DEFROSTER_INIT 0
#define CAN_DEFROSTER_FRONT_ON 1
#define CAN_DEFROSTER_FRONT_OFF 2
#define CAN_DEFROSTER_REAR_ON 3
#define CAN_DEFROSTER_REAR_OFF 4
#define CAN_DEFROSTER_BOTH_ON 5
#define CAN_DEFROSTER_BOTH_OFF 6
#define CAN_RADIO_ON 1
#define CAN_RADIO_OFF 0
#define CAN_RADIO_NextStation 2
#define CAN_RADIO_PreviousStation 3
#define CAN_RADIO_VolumeUp 4
#define CAN_RADIO_VolumeDown 5
#define CAN_DOOR1_LOCK 1
#define CAN_DOOR2_LOCK 2
#define CAN_DOOR3_LOCK 4
#define CAN_DOOR4_LOCK 8
#define CAN_WINDOW1_LOCK 1
#define CAN_WINDOW2_LOCK 2
#define CAN_WINDOW3_LOCK 4
#define CAN_WINDOW4_LOCK 8
#define DEFAULT_WARNING_POS 2
#define CAN_LEFT_SIGNAL 1
#define CAN_RIGHT_SIGNAL 2
#define CAN_WARNING_SIGNAL 3
#define CAN_LIGHT_ON 0x0B
#define CAN_LIGHT_OFF 0x0A
#define CAN_HAZARD_ON 0x05
#define CAN_HAZARD_OFF 0x06
#define CAN_BLUETOOTH_ON 0x0B
#define CAN_BLUETOOTH_OFF 0x0A
#define CAN_AIRCONDITION_ON 0x08
#define CAN_AIRCONDITION_OFF 0x09
#define CAN_AIRCONDITION_Up 0x0A
#define CAN_AIRCONDITION_Down 0x0B
#define CAN_HANDBRAKE_ON 0x0F
#define CAN_HANDBRAKE_OFF 0x0E
#define CAN_BRAKECONTROL_ON 0x0D
#define CAN_BRAKECONTROL_OFF 0x0C
#define CAN_ABS_ON 0x03
#define CAN_ABS_OFF 0x04
#define CAN_WIPER_ON 0x09
#define CAN_WIPER_OFF 0x08
#define CAN_AIRBAG_ON 0x06
#define CAN_AIRBAG_OFF 0x07
#define CAN_SIDEMIRROR_ON 0x0E
#define CAN_SIDEMIRROR_OFF 0x0F
#define CAN_SUNROOF_ON 0x01
#define CAN_SUNROOF_OFF 0x02
#define CAN_TRUNK_ON 0x0C
#define CAN_TRUNK_OFF 0x0D
#define CAN_STEERING_ON 0x07
#define CAN_STEERING_OFF 0x08
#define CAN_CRUISECONTROL_ON 0x05
#define CAN_CRUISECONTROL_OFF 0x06
#define MAX_SEGMENTS 50
//Powertrain IDs
#define DEFAULT_THROTTLE_ID 16 //0x010
#define DEFAULT_THROTTLE_POS 4
#define DEFAULT_RPM_ID 32 //0x020
#define DEFAULT_RPM_POS 1
#define DEFAULT_SPEED_ID 48 //0x030
#define DEFAULT_SPEED_POS 3
#define DEFAULT_ENGINE_LOAD_ID 64 //0x040
#define DEFAULT_ENGINE_LOAD_POS 2
#define DEFAULT_FUEL_TANK_ID 80 //0x050
#define DEFAULT_FUEL_TANK_POS 4
#define DEFAULT_AIR_FLOW_ID 96 //0x060
#define DEFAULT_AIR_FLOW_POS 1
#define DEFAULT_OXYGEN_SENSOR_ID 112 //0x070
#define DEFAULT_OXYGEN_SENSOR_POS 3
#define DEFAULT_BATTERY_ID 128 //0x080
#define DEFAULT_BATTERY_POS 0
#define DEFAULT_VVT_ID 144 //0x090
#define DEFAULT_VVT_POS 2
#define DEFAULT_OIL_TEMPERATURE_ID 160 //0x0A0
#define DEFAULT_OIL_TEMPERATURE_POS 4
#define DEFAULT_RADIATOR_TEMPERATURE_ID 176 //0x0B0
#define DEFAULT_RADIATOR_TEMPERATURE_POS 0
#define DEFAULT_GEAR_ID 192 //0x0C0
#define DEFAULT_GEAR_POS 3
//Chassis
#define DEFAULT_BRAKECONTROL_ID 512 //0x200
#define DEFAULT_BRAKECONTROL_POS 2
#define DEFAULT_HANDBRAKE_ID 528 //0x210
#define DEFAULT_HAND_BRAKE_POS 0
#define DEFAULT_STEERING_ID 544 //0x220
#define DEFAULT_STEERING_POS 5
#define DEFAULT_ABS_ID 560 //0x230
#define DEFAULT_ABS_POS 3
#define DEFAULT_AIRBAG_ID 576 //0x240
#define DEFAULT_AIRBAG_POS 1
#define DEFAULT_CRUISECONTROL_ID 592 //0x250
#define DEFAULT_CRUISECONTROL_POS 0
//Body
#define DEFAULT_DOOR_ID 1024 // 0x400
#define DEFAULT_DOOR_POS 5
#define DEFAULT_WINDOW_ID 1040 // 0x410
#define DEFAULT_WINDOW_POS 2
#define DEFAULT_LIGHT_ID 1056 //0x420
#define DEFAULT_LIGHT_POS 0
#define DEFAULT_SIGNAL_ID 1072 // 0x430
#define DEFAULT_SIGNAL_POS 3
#define DEFAULT_HAZARD_ID 1077 // 0x435
#define DEFAULT_HAZARD_POS 1
#define DEFAULT_WIPER_ID 1088 //0x440
#define DEFAULT_WIPER_POS 4
#define DEFAULT_SUNROOF_ID 1104 //0x450
#define DEFAULT_SUNROOF_POS 3
#define DEFAULT_SIDEMIRROR_ID 1120 //0x460
#define DEFAULT_SIDEMIRROR_POS 2
#define DEFAULT_TRUNK_ID 1136 //0x470
#define DEFAULT_TRUNK_POS 1
#define DEFAULT_HORN_ID 1152 //0x480
#define DEFAULT_HORN_POS 5
//Infotainment
#define DEFAULT_AUDIOSYSTEM_ID 1536 //0x600
#define DEFAULT_AUDIOSYSTEM_POS 3
#define DEFAULT_AIRCONDITION_ID 1552 //0x610
#define DEFAULT_AIRCONDITION_POS 1
#define DEFAULT_BLUETOOTH_ID 1568 //0x620
#define DEFAULT_BLUETOOTH_POS 5
#define DEFAULT_SATELITENAVIGATION_ID 1584 //0x630
#define DEFAULT_SATELITENAVIGATION_POS 0
#define DEFAULT_DEFROSTER_ID 1600 //0x640
#define DEFAULT_DEFROSTER_POS 4
#define DEFAULT_LOG_ALL_FRAMES 0
#define Frame_Log_Level_All 1
#define Frame_Log_Level_Both_User_System 2
#define Frame_Log_Level_UserOnly 3
#define Frame_Log_Level_SegmentOnly 4
//////////////////////////////////
////VARIABLES INITIALIZATION//////
//////////////////////////////////
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
const int canfd_on = 1;
char vcanName[50];
struct timeval *tv;
int debug = 0;
int randomize = 0;
int seed = 0;
int door_pos = DEFAULT_DOOR_POS;
int window_pos = DEFAULT_WINDOW_POS;
int signal_pos = DEFAULT_SIGNAL_POS;
int speed_pos = DEFAULT_SPEED_POS;
int steering_pos = DEFAULT_STEERING_POS;
int rpm_pos = DEFAULT_RPM_POS;
int oil_temp_pos = DEFAULT_OIL_TEMPERATURE_POS;
int radiator_temp_pos = DEFAULT_RADIATOR_TEMPERATURE_POS;
int air_flow_pos = DEFAULT_AIR_FLOW_POS;
int fuel_pos = DEFAULT_FUEL_TANK_POS;
int engine_load_pos = DEFAULT_ENGINE_LOAD_POS;
int gear_pos = DEFAULT_GEAR_POS;
int vvt_pos = DEFAULT_VVT_POS;
int oxygen_sensor_pos = DEFAULT_OXYGEN_SENSOR_POS;
int battery_pos = DEFAULT_BATTERY_POS;
int throttle_pos = DEFAULT_THROTTLE_POS;
int horn_pos = DEFAULT_HORN_POS;
int lightPos = DEFAULT_LIGHT_POS;
int bluetooth_pos = DEFAULT_BLUETOOTH_POS;
int aircondition_pos = DEFAULT_AIRCONDITION_POS;
int satelitenavigation_pos = DEFAULT_SATELITENAVIGATION_POS;
int defroster_pos = DEFAULT_DEFROSTER_POS;
int audiosys_pos = DEFAULT_AUDIOSYSTEM_POS;
int handbrake_pos = DEFAULT_HAND_BRAKE_POS;
int brakecontrol_pos = DEFAULT_BRAKECONTROL_POS;
int abs_pos = DEFAULT_ABS_POS;
int wiper_pos = DEFAULT_WIPER_POS;
int airbag_pos = DEFAULT_AIRBAG_POS;
int hazard_pos = DEFAULT_HAZARD_POS;
int sunroof_pos = DEFAULT_SUNROOF_POS;
int sidemirror_pos = DEFAULT_SIDEMIRROR_POS;
int trunk_pos = DEFAULT_TRUNK_POS;
int cruisecontrol_pos = DEFAULT_CRUISECONTROL_POS;
long current_speed = 0;
int16_t current_steering_angle = 0;
long current_rpm = 0;
float current_oil_temp = 30;
float current_radiator_temp = 30;
float current_engine_load = 0;
int current_gear = 0;
float current_vvt = 0;
float current_oxygen_sensor = 0.9;
float current_air_flow = 0;
float current_battery = 1000;
int current_throttle_status = OFF;
int current_horn_status = OFF;
int last_right_signal_status = OFF;
int last_left_signal_status = OFF;
float current_fuel = 0;
int current_temp = 25;
struct timeval t1, t2;
double elapsedTime;
int door_status[4];
int last_door_status[4];
int window_status[4];
int last_window_status[4];
int turn_status[2];
int aircondition_value = 20;
char lightStatus = CAN_LIGHT_OFF;
char bluetooth_status = CAN_BLUETOOTH_OFF;
char aircondition_status = CAN_AIRCONDITION_OFF;
char satelitenavigation_status = OFF;
char defroster_status = CAN_DEFROSTER_INIT;
char defroster_front_status = CAN_DEFROSTER_FRONT_OFF;
char defroster_rear_status = CAN_DEFROSTER_REAR_OFF;
char defroster_both_status = CAN_DEFROSTER_BOTH_OFF;
char audiosys_status = CAN_RADIO_OFF;
char handbrake_status = CAN_HANDBRAKE_OFF;
char brakecontrol_status = CAN_BRAKECONTROL_OFF;
char abs_status = CAN_ABS_OFF;
char wiper_status = CAN_WIPER_OFF;
char airbag_status = CAN_AIRBAG_OFF;
char hazard_status = CAN_HAZARD_OFF;
char sunroof_status = CAN_SUNROOF_OFF;
char sidemirror_status = CAN_SIDEMIRROR_OFF;
char trunk_status = CAN_TRUNK_OFF;
char cruisecontrol_status = CAN_CRUISECONTROL_OFF;
char *logfilter = NULL;
char *commentsfilter = NULL;
int log_all_comments_enabled = 0;
int log_user_comments_enabled = 0;
int log_system_comments_enabled = 0;
int log_segment_comments_enabled = 0;
int log_all_enabled = 0;
int log_user_enabled = 0;
int log_system_enabled = 0;
int log_segment_enabled = 0;
int currentTime = 0;
int lastTimeCheck = 0;
char data_file[256];
SDL_Renderer *renderer = NULL;
SDL_Texture *base_texture = NULL;
SDL_Texture *needle_tex = NULL;
SDL_Texture *steering_tex = NULL;
SDL_Texture *rpm_needle_tex = NULL;
SDL_Texture *oil_temp_needle_tex = NULL;
SDL_Texture *radiator_temp_needle_tex = NULL;
SDL_Texture *fuel_needle_tex = NULL;
SDL_Texture *battery_needle_tex = NULL;
SDL_Texture *sprite_tex = NULL;
SDL_Rect speed_rect;
SDL_Rect rpm_rect;
SDL_Rect oil_temp_rect;
SDL_Rect radiator_temp_rect;
SDL_Rect battery_rect;
SDL_Rect fuel_rect;
SDL_Rect steering_rect;
//Network Segment Structure
typedef struct {
char name[20];
char interface[10];
unsigned int start_id;
unsigned int end_id;
int socket;
} NetworkSegment;
NetworkSegment segments[MAX_SEGMENTS];
int num_segments = -1;
///////////////////////////////////////////////
////Function prototypes for read functions////
///////////////////////////////////////////////
void read_speed_status(struct canfd_frame *cf, int maxdlen);
void read_rpm_status(struct canfd_frame *cf, int maxdlen);
void read_oil_temp_status(struct canfd_frame *cf, int maxdlen);
void read_fuel_status(struct canfd_frame *cf, int maxdlen);
void read_battery_status(struct canfd_frame *cf, int maxdlen);
void read_throttle_status(struct canfd_frame *cf, int maxdlen);
void read_horn_status(struct canfd_frame *cf, int maxdlen);
void read_light_status(struct canfd_frame *cf, int maxdlen);
void read_bluetooth_status(struct canfd_frame *cf, int maxdlen);
void read_aircondition_status(struct canfd_frame *cf, int maxdlen);
void read_satelitenavigation_status(struct canfd_frame *cf, int maxdlen);
void read_defroster_status(struct canfd_frame *cf, int maxdlen);
void read_audiosys_status(struct canfd_frame *cf, int maxdlen);
void read_handbrake_status(struct canfd_frame *cf, int maxdlen);
void read_brakecontrol_status(struct canfd_frame *cf, int maxdlen);
void read_abs_status(struct canfd_frame *cf, int maxdlen);
void read_wiper_status(struct canfd_frame *cf, int maxdlen);
void read_airbag_status(struct canfd_frame *cf, int maxdlen);
void read_hazard_status(struct canfd_frame *cf, int maxdlen);
void read_sunroof_status(struct canfd_frame *cf, int maxdlen);
void read_sidemirror_status(struct canfd_frame *cf, int maxdlen);
void read_trunk_status(struct canfd_frame *cf, int maxdlen);
void read_cruisecontrol_status(struct canfd_frame *cf, int maxdlen);
void read_steering_status(struct canfd_frame *cf, int maxdlen);
NetworkSegment get_segment_name(struct canfd_frame *cf);
int read_config();
//////////////////////////////////
////ANGLE MAPPING AS PER COORDINATES
//////////////////////////////////
// Simple map function
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// Adds data dir to file name
// Uses a single pointer so not to have a memory leak
// returns point to data_files or NULL if append is too large
char *get_data(char *fname) {
if(strlen(DATA_DIR) + strlen(fname) > 255) return NULL;
strncpy(data_file, DATA_DIR, 255);
strncat(data_file, fname, 255-strlen(data_file));
return data_file;
}
//////////////////////////////////
////Default vehicle state/////////
//////////////////////////////////
void init_car_state() {
door_status[0] = DOOR_LOCKED;
door_status[1] = DOOR_LOCKED;
door_status[2] = DOOR_LOCKED;
door_status[3] = DOOR_LOCKED;
last_door_status[0] = DOOR_LOCKED;
last_door_status[1] = DOOR_LOCKED;
last_door_status[2] = DOOR_LOCKED;
last_door_status[3] = DOOR_LOCKED;
window_status[0] = WINDOW_LOCKED;
window_status[1] = WINDOW_LOCKED;
window_status[2] = WINDOW_LOCKED;
window_status[3] = WINDOW_LOCKED;
last_window_status[0] = WINDOW_LOCKED;
last_window_status[1] = WINDOW_LOCKED;
last_window_status[2] = WINDOW_LOCKED;
last_window_status[3] = WINDOW_LOCKED;
turn_status[0] = OFF;
turn_status[1] = OFF;
handbrake_status = CAN_HANDBRAKE_OFF;
brakecontrol_status = CAN_BRAKECONTROL_OFF;
abs_status = CAN_ABS_OFF;
wiper_status = CAN_WIPER_OFF;
airbag_status = CAN_AIRBAG_OFF;
hazard_status = CAN_HAZARD_OFF;
sunroof_status = CAN_SUNROOF_OFF;
sidemirror_status = CAN_SIDEMIRROR_OFF;
trunk_status = CAN_TRUNK_OFF;
cruisecontrol_status = CAN_CRUISECONTROL_OFF;
lightStatus = CAN_LIGHT_OFF;
bluetooth_status = CAN_BLUETOOTH_OFF;
aircondition_status = CAN_AIRCONDITION_OFF;
satelitenavigation_status = OFF;
defroster_front_status = CAN_DEFROSTER_FRONT_OFF;
defroster_rear_status = CAN_DEFROSTER_FRONT_OFF;
defroster_both_status = CAN_DEFROSTER_BOTH_OFF;
audiosys_status = CAN_RADIO_OFF;
}
//////////////////////////////////
////Empty IC//////////////////////
//////////////////////////////////
void blank_ic() {
SDL_RenderCopy(renderer, base_texture, NULL, NULL);
}
//////////////////////////////////
////Updates speedo////////////////
//////////////////////////////////
void update_speed() {
SDL_Rect dial_rect;
SDL_Point center;
double angle = -100;
dial_rect.x = 200;
dial_rect.y = 80;
dial_rect.h = 135;
dial_rect.w = 300;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
/* Because it's a curve we do a smaller rect for the top */
dial_rect.x = 250;
dial_rect.y = 30;
dial_rect.h = 65;
dial_rect.w = 200;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
// And one more smaller box for the pivot point of the needle
dial_rect.x = 253;
dial_rect.y = 171;
dial_rect.h = 52;
dial_rect.w = 47;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
center.x = 77;
center.y = 10;
angle = map(current_speed*1, -270, 280, -270, 180);
//printf("speed angle%f\r\n",angle);
if(angle < -150) angle = -150;
if(angle > 180) angle = 180;
SDL_RenderCopyEx(renderer, needle_tex, NULL, &speed_rect, angle, ¢er, SDL_FLIP_NONE);
}
//////////////////////////////////
////Updates steering////////////////
//////////////////////////////////
void update_steering() {
SDL_Rect dial_rect;
SDL_Point center;
int16_t angle = 0;
dial_rect.x = 450;
dial_rect.y = 220;
dial_rect.h = 120;
dial_rect.w = 120;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
/* Because it's a curve we do a smaller rect for the top */
dial_rect.x = 450;
dial_rect.y = 220;
dial_rect.h = 120;
dial_rect.w = 120;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
// And one more smaller box for the pivot point of the needle
dial_rect.x = 450;
dial_rect.y = 220;
dial_rect.h = 120;
dial_rect.w = 120;
SDL_RenderCopy(renderer, base_texture, &dial_rect, &dial_rect);
center.x = 50;
center.y = 50;
angle = current_steering_angle;//map(current_steering_angle, 100, 90, 100, 90);
if(angle < -90) angle = -90;
if(angle > 90) angle = 90;
SDL_RenderCopyEx(renderer, steering_tex, NULL, &steering_rect, angle, ¢er, SDL_FLIP_NONE);
}
//////////////////////////////////
////Updates rpm //////////////////
//////////////////////////////////
void update_rpm() {
SDL_Rect rpm_dial_rect;
SDL_Point rpm_center;
double angle = 0;
rpm_dial_rect.x = 40;
rpm_dial_rect.y = 80;
rpm_dial_rect.h = 130;
rpm_dial_rect.w = 80;
SDL_RenderCopy(renderer, base_texture, &rpm_dial_rect, &rpm_dial_rect);
/* Because it's a curve we do a smaller rect for the top */
rpm_dial_rect.x = 50;
rpm_dial_rect.y = 30;
rpm_dial_rect.h = 65;
rpm_dial_rect.w = 80;
SDL_RenderCopy(renderer, base_texture, &rpm_dial_rect, &rpm_dial_rect);
// And one more smaller box for the pivot point of the rpm needle
rpm_dial_rect.x = 40;
rpm_dial_rect.y = 171;
rpm_dial_rect.h = 52;
rpm_dial_rect.w = 47;
SDL_RenderCopy(renderer, base_texture, &rpm_dial_rect, &rpm_dial_rect);
rpm_center.x = 59.5;
rpm_center.y = 7.5;
angle = map(current_rpm/30, -620, 280, -620, 180);
if(angle < -480) angle = -480;
if(angle > 180) angle = 180;
SDL_RenderCopyEx(renderer, rpm_needle_tex, NULL, &rpm_rect, angle, &rpm_center, SDL_FLIP_NONE);
}
//////////////////////////////////
////Updates oil temp /////////////
//////////////////////////////////
void update_oil_temp() {
SDL_Rect oil_temp_dial_rect;
SDL_Point oil_temp_center;
double angle = 0;
oil_temp_dial_rect.x = 120;
oil_temp_dial_rect.y = 80;
oil_temp_dial_rect.h = 130;
oil_temp_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &oil_temp_dial_rect, &oil_temp_dial_rect);
/* Because it's a curve we do a smaller rect for the top */
oil_temp_dial_rect.x = 110;
oil_temp_dial_rect.y = 30;
oil_temp_dial_rect.h = 65;
oil_temp_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &oil_temp_dial_rect, &oil_temp_dial_rect);
// And one more smaller box for the pivot point of the oil_temp needle
oil_temp_dial_rect.x = 120;
oil_temp_dial_rect.y = 171;
oil_temp_dial_rect.h = 52;
oil_temp_dial_rect.w = 47;
SDL_RenderCopy(renderer, base_texture, &oil_temp_dial_rect, &oil_temp_dial_rect);
oil_temp_center.x = 6;
oil_temp_center.y = 6;
angle = map(current_oil_temp*-1, 100, 280, 100, 180);
if(angle <-150) angle = -150;
if(angle > 480) angle = 480;
SDL_RenderCopyEx(renderer, oil_temp_needle_tex, NULL, &oil_temp_rect, angle, &oil_temp_center, SDL_FLIP_HORIZONTAL);
}
//////////////////////////////////
////Updates radiator temp ////////
//////////////////////////////////
void update_radiator_temp() {
SDL_Rect radiator_temp_dial_rect;
SDL_Point radiator_temp_center;
double angle = 0;
radiator_temp_dial_rect.x = 570;
radiator_temp_dial_rect.y = 80;
radiator_temp_dial_rect.h = 130;
radiator_temp_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &radiator_temp_dial_rect, &radiator_temp_dial_rect);
/* Because it's a curve we do a smaller rect for the top */
radiator_temp_dial_rect.x = 560;
radiator_temp_dial_rect.y = 30;
radiator_temp_dial_rect.h = 65;
radiator_temp_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &radiator_temp_dial_rect, &radiator_temp_dial_rect);
// And one more smaller box for the pivot point of the radiator_temp needle
radiator_temp_dial_rect.x = 570;
radiator_temp_dial_rect.y = 171;
radiator_temp_dial_rect.h = 52;
radiator_temp_dial_rect.w = 47;
SDL_RenderCopy(renderer, base_texture, &radiator_temp_dial_rect, &radiator_temp_dial_rect);
radiator_temp_center.x = 6;
radiator_temp_center.y = 6;
angle = map(current_radiator_temp*-1.3, 60, 280, 60, 180);
if(angle < -150) angle = -150;
if(angle > 480) angle = 480;
SDL_RenderCopyEx(renderer, radiator_temp_needle_tex, NULL, &radiator_temp_rect, angle, &radiator_temp_center, SDL_FLIP_HORIZONTAL);
}
//////////////////////////////////
////Updates battery //////////////
//////////////////////////////////
void update_battery() {
SDL_Rect battery_dial_rect;
SDL_Point battery_center;
double angle = 0;
battery_dial_rect.x = 470;
battery_dial_rect.y = 80;
battery_dial_rect.h = 130;
battery_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &battery_dial_rect, &battery_dial_rect);
/* Because it's a curve we do a smaller rect for the top */
battery_dial_rect.x = 470;
battery_dial_rect.y = 30;
battery_dial_rect.h = 65;
battery_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &battery_dial_rect, &battery_dial_rect);
// And one more smaller box for the pivot point of the battery needle
battery_dial_rect.x = 470;
battery_dial_rect.y = 171;
battery_dial_rect.h = 52;
battery_dial_rect.w = 47;
SDL_RenderCopy(renderer, base_texture, &battery_dial_rect, &battery_dial_rect);
battery_center.x = 34;
battery_center.y = 6;
angle = map(current_battery*0.1, -220, 280, -220, 180);
if(angle < -220) angle = -220;
if(angle > 480) angle = 480;
SDL_RenderCopyEx(renderer, battery_needle_tex, NULL, &battery_rect, angle, &battery_center, SDL_FLIP_NONE);
}
//////////////////////////////////
////Updates Horn Status //////////
//////////////////////////////////
void update_horn() {
//printf("current_throttle_status=%d\r\n",current_throttle_status);
SDL_Rect horn_area, pos;
horn_area.x = 676;
horn_area.y = 235;
horn_area.w = 50;
horn_area.h = 40;
memcpy(&pos, &horn_area, sizeof(SDL_Rect));
if(current_horn_status == OFF){
SDL_RenderCopy(renderer, base_texture, &horn_area, &horn_area);
}
else {
SDL_RenderCopy(renderer, sprite_tex, &horn_area, &pos);
}
}
//////////////////////////////////
////Updates fuel /////////////////
//////////////////////////////////
void update_fuel() {
SDL_Rect fuel_dial_rect;
SDL_Point fuel_center;
double angle = 0;
fuel_dial_rect.x = 570;
fuel_dial_rect.y = 160;
fuel_dial_rect.h = 85;
fuel_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &fuel_dial_rect, &fuel_dial_rect);
/* Because it's a curve we do a smaller rect for the top */
fuel_dial_rect.x = 570;
fuel_dial_rect.y = 160;
fuel_dial_rect.h = 85;
fuel_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &fuel_dial_rect, &fuel_dial_rect);
// And one more smaller box for the pivot point of the fuel needle
fuel_dial_rect.x = 570;
fuel_dial_rect.y = 160;
fuel_dial_rect.h = 85;
fuel_dial_rect.w = 100;
SDL_RenderCopy(renderer, base_texture, &fuel_dial_rect, &fuel_dial_rect);
fuel_center.x = 6;
fuel_center.y = 6;
angle = map(current_fuel*-3*2, 135, 280, 135, 180);
if(angle < -150) angle = -150;
if(angle > 480) angle = 480;
SDL_RenderCopyEx(renderer, fuel_needle_tex, NULL, &fuel_rect, angle, &fuel_center, SDL_FLIP_HORIZONTAL);
}
//////////////////////////////////
////Updates window unlocks simulated by open icons
//////////////////////////////////
void update_doors() {
SDL_Rect door_area, update, pos;
door_area.x = 310;
door_area.y = 195;
door_area.w = 100;
door_area.h = 125;
SDL_RenderCopy(renderer, base_texture, &door_area, &door_area);
// No update if all doors are locked
if(door_status[0] == DOOR_LOCKED && door_status[1] == DOOR_LOCKED &&
door_status[2] == DOOR_LOCKED && door_status[3] == DOOR_LOCKED) return;
if(door_status[0] == DOOR_UNLOCKED) {
update.x = 374;
update.y = 185;
update.w = 35;
update.h = 65;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(door_status[1] == DOOR_UNLOCKED) {
update.x = 310;
update.y = 185;
update.w = 35;
update.h = 65;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(door_status[2] == DOOR_UNLOCKED) {
update.x = 374;
update.y = 250;
update.w = 35;
update.h = 45;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(door_status[3] == DOOR_UNLOCKED) {
update.x = 310;
update.y = 250;
update.w = 35;
update.h = 45;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
}
//////////////////////////////////
////Updates door unlocks simulated by open icons
//////////////////////////////////
void update_windows() {
SDL_Rect window_area, update, pos;
window_area.x = 280;
window_area.y = 240;
window_area.w = 35;
window_area.h = 35;
SDL_RenderCopy(renderer, base_texture, &window_area, &window_area);
window_area.x = 410;
window_area.y = 240;
window_area.w = 35;
window_area.h = 35;
SDL_RenderCopy(renderer, base_texture, &window_area, &window_area);
window_area.x = 280;
window_area.y = 280;
window_area.w = 35;
window_area.h = 35;
SDL_RenderCopy(renderer, base_texture, &window_area, &window_area);
window_area.x = 410;
window_area.y = 280;
window_area.w = 35;
window_area.h = 35;
SDL_RenderCopy(renderer, base_texture, &window_area, &window_area);
// No update if all windows are locked
if(window_status[0] == WINDOW_LOCKED && window_status[1] == WINDOW_LOCKED &&
window_status[2] == WINDOW_LOCKED && window_status[3] == WINDOW_LOCKED) return;
if(window_status[0] == WINDOW_UNLOCKED) {
update.x = 410;
update.y = 240;
update.w = 35;
update.h = 35;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(window_status[1] == WINDOW_UNLOCKED) {
update.x = 280;
update.y = 240;
update.w = 35;
update.h = 35;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(window_status[2] == WINDOW_UNLOCKED) {
update.x = 410;
update.y = 280;
update.w = 35;
update.h = 35;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
if(window_status[3] == WINDOW_UNLOCKED) {
update.x = 280;
update.y = 280;
update.w = 35;
update.h = 35;
memcpy(&pos, &update, sizeof(SDL_Rect));
SDL_RenderCopy(renderer, sprite_tex, &update, &pos);
}
}
//////////////////////////////////
////Updates turn signals /////////
//////////////////////////////////
void update_turn_signals() {
SDL_Rect left, right, lpos, rpos;
left.x = 232;
left.y = 21;
left.w = 75;
left.h = 75;
memcpy(&right, &left, sizeof(SDL_Rect));
right.x = 402;
memcpy(&lpos, &left, sizeof(SDL_Rect));
memcpy(&rpos, &right, sizeof(SDL_Rect));
lpos.x -= 22;
lpos.y -= 22;
rpos.x += 22;
rpos.y -= 22;
if(turn_status[0] == OFF) {
SDL_RenderCopy(renderer, base_texture, &lpos, &lpos);
} else {
SDL_RenderCopy(renderer, sprite_tex, &left, &lpos);
}
if(turn_status[1] == OFF) {
SDL_RenderCopy(renderer, base_texture, &rpos, &rpos);
} else {
SDL_RenderCopy(renderer, sprite_tex, &right, &rpos);
}
}
//////////////////////////////////
////Updates light status /////////
//////////////////////////////////
void update_light() {
SDL_Rect light_area, pos;
light_area.x = 566;
light_area.y = 230;
light_area.w = 50;
light_area.h = 40;
memcpy(&pos, &light_area, sizeof(SDL_Rect));
if(lightStatus == CAN_LIGHT_OFF){
SDL_RenderCopy(renderer, base_texture, &light_area, &light_area);
}
else {
SDL_RenderCopy(renderer, sprite_tex, &light_area, &pos);
}
}
//////////////////////////////////
////Updates BLUETOOTH status /////////
//////////////////////////////////
void update_bluetooth() {
SDL_Rect bluetooth_area, pos;
bluetooth_area.x = 50;
bluetooth_area.y = 285;
bluetooth_area.w = 30;
bluetooth_area.h = 35;
memcpy(&pos, &bluetooth_area, sizeof(SDL_Rect));
if(bluetooth_status == CAN_BLUETOOTH_OFF){
SDL_RenderCopy(renderer, base_texture, &bluetooth_area, &bluetooth_area);
}
else {
SDL_RenderCopy(renderer, sprite_tex, &bluetooth_area, &pos);
}
}
void update_bluetooth_status(struct canfd_frame *cf, int maxdlen) {
read_bluetooth_status(cf, maxdlen);
update_bluetooth();
SDL_RenderPresent(renderer);
}
void read_bluetooth_status(struct canfd_frame *cf, int maxdlen) {
if(maxdlen == CANFD_MAX_DLEN)
{
bluetooth_pos = DEFAULT_BLUETOOTH_POS + 16;
}
else
{
bluetooth_pos = DEFAULT_BLUETOOTH_POS;
}
int len = (cf->len > maxdlen) ? maxdlen : cf->len;
if (len < bluetooth_pos) {
return;
}
bluetooth_status = cf->data[bluetooth_pos] ;
}
//////////////////////////////////
////Updates AUDIOSYSTEM status /////////
//////////////////////////////////
void update_audiosys() {
SDL_Rect audiosys_area, pos;
audiosys_area.x = 30;
audiosys_area.y = 215;
audiosys_area.w = 50;
audiosys_area.h = 70;
memcpy(&pos, &audiosys_area, sizeof(SDL_Rect));
SDL_Rect audiosys_next_area, audiosys_next_pos;
audiosys_next_area.x = 80;
audiosys_next_area.y = 215;
audiosys_next_area.w = 30;
audiosys_next_area.h = 70;
memcpy(&audiosys_next_pos, &audiosys_next_area, sizeof(SDL_Rect));
SDL_Rect audiosys_prev_area, audiosys_prev_pos;
audiosys_prev_area.x = 8;
audiosys_prev_area.y = 215;
audiosys_prev_area.w = 25;
audiosys_prev_area.h = 70;
memcpy(&audiosys_prev_pos, &audiosys_prev_area, sizeof(SDL_Rect));
SDL_Rect audiosys_volumeUP_area, audiosys_volumeUP_pos;
audiosys_volumeUP_area.x = 8;
audiosys_volumeUP_area.y = 290;
audiosys_volumeUP_area.w = 25;
audiosys_volumeUP_area.h = 30;
memcpy(&audiosys_volumeUP_pos, &audiosys_volumeUP_area, sizeof(SDL_Rect));
SDL_Rect audiosys_volumeDown_area, audiosys_volumeDown_pos;
audiosys_volumeDown_area.x = 65;
audiosys_volumeDown_area.y = 290;
audiosys_volumeDown_area.w = 35;
audiosys_volumeDown_area.h = 30;
memcpy(&audiosys_volumeDown_pos, &audiosys_volumeDown_area, sizeof(SDL_Rect));
if(audiosys_status == CAN_RADIO_OFF){
SDL_RenderCopy(renderer, base_texture, &audiosys_area, &audiosys_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
}
else if(audiosys_status == CAN_RADIO_NextStation){
SDL_RenderCopy(renderer, sprite_tex, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
lastTimeCheck = currentTime;
}
else if(audiosys_status == CAN_RADIO_PreviousStation){
SDL_RenderCopy(renderer, base_texture, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, sprite_tex, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
lastTimeCheck = currentTime;
}
else if(audiosys_status == CAN_RADIO_VolumeUp){
SDL_RenderCopy(renderer, base_texture, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, sprite_tex, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
lastTimeCheck = currentTime;
}
else if(audiosys_status == CAN_RADIO_VolumeDown){
SDL_RenderCopy(renderer, base_texture, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, sprite_tex, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
lastTimeCheck = currentTime;
}
else {
SDL_RenderCopy(renderer, sprite_tex, &audiosys_area, &pos);
SDL_RenderCopy(renderer, base_texture, &audiosys_next_area, &audiosys_next_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_prev_area, &audiosys_prev_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeUP_area, &audiosys_volumeUP_area);
SDL_RenderCopy(renderer, base_texture, &audiosys_volumeDown_area, &audiosys_volumeDown_area);
}
}
void update_audiosys_status(struct canfd_frame *cf, int maxdlen) {
read_audiosys_status(cf, maxdlen);
update_audiosys();
SDL_RenderPresent(renderer);
}
void read_audiosys_status(struct canfd_frame *cf, int maxdlen) {
if(maxdlen == CANFD_MAX_DLEN)
{
audiosys_pos = DEFAULT_AUDIOSYSTEM_POS + 16;
}
else
{
audiosys_pos = DEFAULT_AUDIOSYSTEM_POS;
}
int len = (cf->len > maxdlen) ? maxdlen : cf->len;
if (len < audiosys_pos) {
return;
}
audiosys_status = cf->data[audiosys_pos] ;
}
//////////////////////////////////
////Updates AIRCONDITION status /////////
//////////////////////////////////
void update_aircondition() {
SDL_Rect aircondition_area, pos;