-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsa_esp32.ino
1633 lines (1327 loc) · 44.7 KB
/
psa_esp32.ino
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
//#define PSA_SIMULATE
//#define PSA_SPIFFS_LOG
#define RASPBERRY_POWER_CONTROL // Define to control the Raspberry power on status
#define ESP_USE_WIFI // Define to enable the use of WiFi on the ESP
#define ESP_POWER_CONTROL // Define to enable deep sleep
#include "config.h"
#ifndef PSA_SIMULATE
#ifdef USE_SOFTWARE_VAN
#include <VanBus.h>
#include <VanBusRx.h>
#include <VanBusTx.h>
#else
// Everything TSS463C Related -> Used for sending CD changer packets, and maybe receving VAN packets
#include <itss46x.h>
#include <tss46x_van.h>
#include <tss463.h>
// Everything related to receiving VAN packets
#include <esp32_arduino_rmt_van_rx.h>
#include <esp32_rmt_van_rx.h>
#define VAN_824_CHANNEL 0
#define VAN_564_CHANNEL 1
#define VAN_8A4_CHANNEL 2
#define VAN_4D4_CHANNEL 3
#define VAN_554_CHANNEL 4
#define VAN_E24_CHANNEL 5
#endif // USE SOFTWARE_VAN
#include "header/van_packet_parser.h" // Parsing received packets
#endif // PSA_SIMULATE
#ifdef PSA_SPIFFS_LOG
#include <FS.h>
#include <SPIFFS.h>
#endif
#include "header/psa_packet_defs.h" // MSP packet definitons, for RPi
#include "header/psa_common.h" // psa_status and msp buffers passed to the VAN parser
#include "header/msp_common.h" // MSP defines
#include "header/van_helper.h" // get_iden_from_bytes
#include "header/esp_helper.h" // read_esp32_temperature(), ntp_config(), set_time_zone()
#include "header/van_cdc_emulator_structs.h" // CD changer MSP packet definiton, and initializer function
#include "header/version.h"
#include <WiFi.h>
RTC_DATA_ATTR uint8_t rtc_time_valid = 0; // Is the time set in the RTC
RTC_DATA_ATTR uint8_t car_state = PSA_STATE_CAR_SLEEP; // The car state, used for RPi power management
uint8_t raspberry_power_status = 0; // Target RPi power status, 0 - Off, 1 - On
// HardwareSerial serial_port = Serial; // Serial port used, Serial - PC, Serial2 - RPi
// I haven't been able to run both together reliably
unsigned int wifi_retries = 0;
unsigned long last_cdc_message = 0; // last cd changer packet timestamp, used to send it every second
unsigned long current_cdc_message = 0; // Current cd changer packet timestamp, used for overflow safety... I hope
esp_timer_handle_t timer_handle;
/*
The following unions are for MSP packets.
They are unions so I can easily manipulate the buffers underneath
*/
union engine_packet_union
{
struct psa_engine_packet packet;
uint8_t buffer[sizeof(packet)];
};
union radio_packet_union
{
struct psa_radio_packet packet;
uint8_t buffer[sizeof(packet)];
};
union radio_presets_union
{
struct psa_preset_packet packet;
uint8_t buffer[sizeof(packet)];
};
union headunit_packet_union
{
struct psa_headunit_packet packet;
uint8_t buffer[sizeof(packet)];
};
union cd_player_packet_union
{
struct psa_cd_player_packet packet;
uint8_t buffer[sizeof(packet)];
};
union vin_packet_union
{
struct psa_vin_packet packet;
uint8_t buffer[sizeof(packet)];
};
union dash_packet_union
{
struct psa_dash_packet packet;
uint8_t buffer[sizeof(packet)];
};
union door_packet_union
{
struct psa_door_packet packet;
uint8_t buffer[sizeof(packet)];
};
union trip_packet_union
{
struct psa_trip_packet packet;
uint8_t buffer[sizeof(packet)];
};
union status_packet_union
{
struct psa_status_packet packet;
uint8_t buffer[sizeof(packet)];
};
union time_packet_union
{
struct psa_time_packet packet;
uint8_t buffer[sizeof(packet)];
};
union esp32_temp_packet_union
{
struct psa_esp32_temp_packet packet;
uint8_t buffer[sizeof(packet)];
};
union version_packet_union
{
struct psa_version_packet packet;
uint8_t buffer[sizeof(packet)];
};
uint8_t msp_input_buffer[MSP_MAX_SIZE]; // Buffer for incoming MSP messages
uint8_t msp_response_buffer[MSP_MAX_SIZE]; // Buffer for MSP responses for SET request messages
#ifdef PSA_SPIFFS_LOG
uint16_t log_file_no = 0;
File log_file;
uint8_t do_log = 0;
char log_buffer[255];
uint8_t log_error = 0;
#endif
#ifdef PSA_SIMULATE
const char vin[] = "VF32AKFWF4XXXXXXX";
const char *radio_station_names[] = {
"HRT-HR 1",
"HRT-HR 2",
"Laganini",
"Otvoreni",
"Narodni",
"Sl.Radio"};
const uint16_t radio_station_freqs[] = {
9180,
10170,
9910,
9120,
9530,
10620};
#endif
// const int TX_PIN = D1;
// const int RX_PIN = D2;
#ifdef USE_SOFTWARE_VAN
const int TX_PIN = 22;
const int RX_PIN = 23;
#endif // USE_SOFTWARE_VAN
#ifndef PSA_SIMULATE
SemaphoreHandle_t mutex = xSemaphoreCreateMutex(); // A mutex just in case, probably not even doing anything
#endif
RTC_DATA_ATTR uint8_t presets_data_valid = 0;
RTC_DATA_ATTR union radio_presets_union store_presets_packet[4];
union engine_packet_union engine_packet;
union radio_packet_union radio_packet;
union radio_presets_union presets_packet[4];
union headunit_packet_union headunit_packet;
union cd_player_packet_union cd_packet;
union vin_packet_union vin_packet;
union dash_packet_union dash_packet;
union door_packet_union door_packet;
union trip_packet_union trip_packet;
union status_packet_union status_packet;
union time_packet_union time_packet;
union esp32_temp_packet_union esp32_packet;
union version_packet_union version_packet;
union van_cd_changer_union cd_changer_emu_packet; // In van_cdc_emulator_structs.h
#ifdef PSA_SIMULATE
volatile uint16_t rpm = 700 * 8;
volatile uint16_t speed = 0;
volatile uint8_t fuel = 35;
volatile uint8_t temp = 90;
volatile uint8_t dash_data_ready = 0;
volatile uint32_t engine_time_passed = 0;
volatile uint32_t radio_time_passed = 0;
volatile uint32_t door_time_passed = 0;
volatile uint16_t engine_sequence = 0;
volatile uint8_t curr_band = 1;
volatile uint8_t curr_preset = 1;
volatile uint16_t curr_fuel = 0;
volatile uint16_t curr_range = 1000;
volatile unsigned long last_trip = 0;
#endif
// struct psa_incoming_msp_packet msp_message;
// struct psa_vin_packet vin_packet;
struct psa_output_data_buffers data_buffers; // Pointers to MSP buffers that will be filled out by the VAN parser
#ifndef PSA_SIMULATE
#ifdef USE_SOFTWARE_VAN
TVanPacketRxDesc van_rx_pkt;
#else
SPIClass *spi; // Spi instance, used for comms with the TSS463C
ITss46x *vanSender; // Creating a TSS instance
TSS46X_VAN *VANInterface; // Ditto
uint8_t van_message[34]; // Buffer for incoming VAN message, the entire VAN packet has a max size of 32
uint8_t van_message_len = 0; // Length of the message received
ESP32_RMT_VAN_RX VAN_RX; // ESP32 VAN receiver instance
// ESP32 Receiver pin setup
const uint8_t VAN_DATA_RX_RMT_CHANNEL = 0;
const uint8_t VAN_DATA_RX_PIN = 21;
const uint8_t VAN_DATA_RX_LED_INDICATOR_PIN = 2;
// Handles for tasks, they are all on core 0
TaskHandle_t van_task = NULL;
TaskHandle_t msp_task = NULL;
TaskHandle_t ntp_task = NULL;
TaskHandle_t car_state_task = NULL;
/**
* @brief Initialize SPI communication with the TSS463 chip.
*
*/
void InitTss463()
{
// initialize SPI
uint8_t VAN_PIN = 5; // Set pin 5 as the CS pin
spi = new SPIClass(VSPI); // Start spi communication on the VSPI SPI bus
spi->begin();
// instantiate the VAN message sender for a TSS463
vanSender = new Tss463(VAN_PIN, spi);
}
/**
* @brief A Task that periodically checks for new VAN packets
*
* @param params NULL
*/
void van_receive_task(void *params)
{
// serial_port.print("Van_receive_task on core ");
// serial_port.println(xPortGetCoreID());
uint8_t *van_message_ptr = (uint8_t *)van_message; // Unneccesary cast, but to be safe
// All tasks must have a loop like this
while (1)
{
xSemaphoreTake(mutex, portMAX_DELAY); // Mutex stuff, hopefully does something
VAN_RX.Receive(&van_message_len, van_message_ptr); // Receives a new VAN frame
// The first byte contains the SOF byte, 0x0E
// The second and third bytes are the IDEN (12 bits) and COMMAND (4 bits)
// The rest is data
// The last two bytes are the CRC
if (van_message_len > 0)
{
if (VAN_RX.IsCrcOk(van_message_ptr, van_message_len))
{
psa_parse_van_packet_esp32(get_iden_from_bytes(van_message[1], van_message[2]), van_message_len - 5, van_message_ptr + 3, &data_buffers);
// uint8_t data_len = van_message_len - 5;
// uint8_t *data = van_message_ptr + 3;
// Currently looking for packet 8C4 with data type 8A
// if ((get_iden_from_bytes(van_message[1], van_message[2]) == 0x8C4))
// {
// // Only display the packet if it's different
// // if (memcmp(vanMessage, vanMessageOld, vanMessageLength) != 0)
// {
// // memcpy(vanMessageOld, vanMessage, vanMessageLength);
// for (size_t i = 0; i < data_len; i++)
// {
// if (i != data_len - 1)
// {
// serial_port.printf("%02X ", data[i]);
// }
// else
// {
// serial_port.printf("%02X", data[i]);
// }
// }
// serial_port.println();
// }
// }
}
}
xSemaphoreGive(mutex);
vTaskDelay(5 / portTICK_PERIOD_MS); // A delay, so it doesn't take up all processing time
// delay(7);
}
}
#endif // USE_SOFTWARE_VAN
void update_rasbperry_power_state(int power)
{
#ifdef RASPBERRY_POWER_CONTROL
if (power)
{
pinMode(rpi_power_pin, INPUT);
}
else
{
pinMode(rpi_power_pin, OUTPUT);
digitalWrite(rpi_power_pin, LOW);
}
#endif // RASPBERRY_POWER_CONTROL
}
void update_car_state(uint8_t new_car_state)
{
if (new_car_state == car_state)
{
return;
}
else if (new_car_state > PSA_STATE_CAR_OFF)
{
// if (esp_timer_is_active(timer_handle))
// {
// esp_timer_stop(timer_handle);
// }
setCpuFrequencyMhz(160);
}
else
{
// if (raspberry_power_status == 1 && !esp_timer_is_active(timer_handle))
// {
// esp_timer_start_once(timer_handle, (60 * 1000 * 1000));
// }
// else if (raspberry_power_status == 0 && esp_timer_is_active(timer_handle))
// {
// esp_timer_stop(timer_handle);
// }
setCpuFrequencyMhz(80);
}
car_state = new_car_state;
}
void rpi_off_timer_timeout(void *args)
{
raspberry_power_status = 0;
update_rasbperry_power_state(raspberry_power_status);
}
/**
* @brief A task that calculates the car state as defined in the table.
*
* @param params
*/
void psa_calculate_state(void *params)
{
while (1)
{
// PSA_STATE_ENGINE_ON
if (dash_packet.packet.data.engine_running)
{
update_car_state(PSA_STATE_ENGINE_ON);
raspberry_power_status = 1;
}
// PSA_STATE_ECONOMY_MODE
// else if (dash_packet.packet.data.economy_mode)
// {
// update_car_state(PSA_STATE_ECONOMY_MODE);
// raspberry_power_status = 0;
// }
// PSA_STATE_CRANKING
else if (dash_packet.packet.data.accesories_on == 0 &&
dash_packet.packet.data.ignition_on == 1)
{
update_car_state(PSA_STATE_CRANKING);
raspberry_power_status = 1;
}
// PSA_STATE_IGNITION
else if (dash_packet.packet.data.ignition_on == 1 &&
dash_packet.packet.data.accesories_on == 1)
{
update_car_state(PSA_STATE_IGNITION);
raspberry_power_status = 1;
}
// PSA_STATE_ACCESSORY
else if (dash_packet.packet.data.accesories_on == 1 &&
dash_packet.packet.data.ignition_on == 0)
{
update_car_state(PSA_STATE_ACCESSORY);
raspberry_power_status = 1;
}
// PSA_STATE_RADIO_ON
// PSA_STATE_CAR_LOCKED
// PSA_STATE_CAR_OFF
else if (dash_packet.packet.data.accesories_on == 0 &&
dash_packet.packet.data.ignition_on == 0)
{
if (headunit_packet.packet.data.unit_powered_on)
{
update_car_state(PSA_STATE_RADIO_ON);
raspberry_power_status = 1;
}
else if (status_packet.packet.data.doors_locked)
{
update_car_state(PSA_STATE_CAR_LOCKED);
raspberry_power_status = 0;
}
else
{
update_car_state(PSA_STATE_CAR_OFF);
}
}
update_rasbperry_power_state(raspberry_power_status);
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
#endif // PSA_SIMULATE
/**
* @brief Calculate crc of an msp packet. Returns the crc.
* Calculated by XOR-ing every byte except the first two (start magic and direction) and the last one (the crc itself)
*
* @param packet The packet buffer
* @param size Packet size
* @return uint8_t crc
*/
uint8_t calculate_crc(uint8_t const *const packet, const uint8_t size)
{
if (packet == NULL)
{
return PSA_NULL_PTR;
}
if (size <= sizeof(struct psa_header))
{
return PSA_INVALID_MSP_PACKET_SIZE;
}
uint8_t crc = 0;
for (int i = 2; i < size - 1; ++i)
{
crc ^= packet[i];
}
return crc;
}
/*
MSP Packet initialization functions.
They populate the header with static information for each packet.
*/
void prepare_vin_packet()
{
memset(vin_packet.buffer, 0, sizeof(vin_packet));
vin_packet.packet.header.start = MSP_START_MAGIC;
vin_packet.packet.header.ident = PSA_IDENT_VIN;
vin_packet.packet.header.direction = '>';
vin_packet.packet.header.size = sizeof(vin_packet.packet.vin);
#ifdef PSA_SIMULATE
memcpy(vin_packet.packet.vin, vin, vin_packet.packet.header.size);
#endif
}
void init_version_packet()
{
memset(version_packet.buffer, 0, sizeof(version_packet));
version_packet.packet.header.start = MSP_START_MAGIC;
version_packet.packet.header.ident = PSA_IDENT_VERSIONS;
version_packet.packet.header.size = sizeof(version_packet.packet.data);
version_packet.packet.header.direction = '>';
version_packet.packet.data.api_version = PSA_ESP_API_VERSION;
version_packet.packet.data.firmware_version_major = PSA_ESP_FIRMWARE_VERSION_MAJOR;
version_packet.packet.data.firmware_version_minor = PSA_ESP_FIRMWARE_VERSION_MINOR;
version_packet.packet.crc = calculate_crc(version_packet.buffer, sizeof(version_packet));
}
void init_engine_packet()
{
// engine_data_size = sizeof(engine_packet);
engine_packet.packet.header.start = MSP_START_MAGIC;
engine_packet.packet.header.direction = '>';
engine_packet.packet.header.ident = (uint16_t)PSA_IDENT_ENGINE;
engine_packet.packet.header.size = sizeof(struct psa_engine_data);
engine_packet.packet.crc = 0;
}
void init_radio_packet()
{
radio_packet.packet.header.start = MSP_START_MAGIC;
radio_packet.packet.header.direction = '>';
radio_packet.packet.header.ident = (uint16_t)PSA_IDENT_RADIO;
radio_packet.packet.header.size = sizeof(struct psa_radio_data);
radio_packet.packet.crc = 0;
}
void init_presets_packet()
{
for (int i = 0; i < PSA_PRESET_MAX; ++i)
{
presets_packet[i].packet.header.start = MSP_START_MAGIC;
presets_packet[i].packet.header.direction = '>';
presets_packet[i].packet.header.ident = PSA_IDENT_RADIO_PRESETS;
presets_packet[i].packet.header.size = sizeof(struct psa_preset_data);
presets_packet[i].packet.crc = 0;
}
}
void init_headunit_packet()
{
headunit_packet.packet.header.start = MSP_START_MAGIC;
headunit_packet.packet.header.direction = '>';
headunit_packet.packet.header.ident = (uint16_t)PSA_IDENT_HEADUNIT;
headunit_packet.packet.header.size = sizeof(struct psa_headunit_data);
headunit_packet.packet.crc = 0;
}
void init_cd_packet()
{
cd_packet.packet.header.start = MSP_START_MAGIC;
cd_packet.packet.header.direction = '>';
cd_packet.packet.header.ident = (uint16_t)PSA_IDENT_CD_PLAYER;
cd_packet.packet.header.size = sizeof(struct psa_cd_player_data);
cd_packet.packet.crc = 0;
}
void init_dash_packet()
{
dash_packet.packet.header.start = MSP_START_MAGIC;
dash_packet.packet.header.direction = '>';
dash_packet.packet.header.ident = (uint16_t)PSA_IDENT_DASHBOARD;
dash_packet.packet.header.size = sizeof(struct psa_dash_data);
dash_packet.packet.crc = 0;
#ifdef PSA_SIMULATE
dash_packet.packet.data.engine_running = 1;
#endif // PSA_SIMULATE
}
void init_door_packet()
{
door_packet.packet.header.start = MSP_START_MAGIC;
door_packet.packet.header.direction = '>';
door_packet.packet.header.ident = (uint16_t)PSA_IDENT_DOORS;
door_packet.packet.header.size = sizeof(struct psa_door_data);
door_packet.packet.crc = 0;
}
void init_trip_packet()
{
trip_packet.packet.header.start = MSP_START_MAGIC;
trip_packet.packet.header.direction = '>';
trip_packet.packet.header.ident = (uint16_t)PSA_IDENT_TRIP;
trip_packet.packet.header.size = sizeof(struct psa_trip_data);
trip_packet.packet.data.current_fuel_consumption = 0xffff;
trip_packet.packet.crc = 0;
}
void init_status_packet()
{
status_packet.packet.header.start = MSP_START_MAGIC;
status_packet.packet.header.direction = '>';
status_packet.packet.header.ident = (uint16_t)PSA_IDENT_CAR_STATUS;
status_packet.packet.header.size = sizeof(struct psa_status_data);
status_packet.packet.crc = 0;
}
void init_time_packet()
{
time_packet.packet.header.start = MSP_START_MAGIC;
time_packet.packet.header.direction = '>';
time_packet.packet.header.ident = (uint16_t)PSA_IDENT_TIME;
time_packet.packet.header.size = sizeof(struct psa_time_data);
time_packet.packet.crc = 0;
}
void init_esp32_temp_packet()
{
memset(esp32_packet.buffer, 0, sizeof(esp32_packet));
esp32_packet.packet.header.start = MSP_START_MAGIC;
esp32_packet.packet.header.direction = '>';
esp32_packet.packet.header.ident = (uint16_t)PSA_IDENT_ESP32_TEMP;
esp32_packet.packet.header.size = sizeof(struct psa_esp32_temp_data);
esp32_packet.packet.crc = 0;
}
/**
* @brief Initializes all MSP packets.
* Fills out header information.
*
*/
void init_packets()
{
init_version_packet();
prepare_vin_packet();
init_engine_packet();
init_radio_packet();
init_presets_packet();
init_dash_packet();
init_door_packet();
init_trip_packet();
init_headunit_packet();
init_cd_packet();
init_status_packet();
init_time_packet();
init_esp32_temp_packet();
init_cdc_packet((struct van_cd_changer_packet *)&cd_changer_emu_packet.packet); // in van_cdc_emulator_structs.h
}
#ifdef PSA_SPIFFS_LOG
void psa_start_log()
{
if (do_log == 0)
{
log_file = SPIFFS.open("/logno.txt", "r", true);
char file_no[10];
memset(file_no, 0, sizeof(file_no));
log_file_no = log_file.parseInt();
serial_port.printf("Read file number %d\n", log_file_no);
log_file_no++;
log_file.close();
log_file = SPIFFS.open("/logno.txt", "w", true);
log_file.println(log_file_no);
log_file.close();
char path[50];
sprintf(path, "/log_%d.csv", log_file_no);
log_file = SPIFFS.open(path, "w", true);
serial_port.printf("Opened file %s\n", path);
sprintf(log_buffer, "RPM,SPEED,TEMP,FUEL,FUEL_CONS");
log_file.println(log_buffer);
do_log = 1;
}
}
void psa_log_data()
{
if (do_log)
{
uint16_t fuel_cons = (trip_packet.packet.data.current_fuel_consumption == 0xffff ? 0 : trip_packet.packet.data.current_fuel_consumption);
if (!log_error)
{
sprintf(log_buffer, "%d,%.2f,%d,%d,%.1f",
engine_packet.packet.data.rpm / 8,
(float)engine_packet.packet.data.speed / 100,
engine_packet.packet.data.engine_temperature - 39,
engine_packet.packet.data.fuel_level,
(float)fuel_cons / 10);
log_file.println(log_buffer);
}
}
}
void psa_end_log()
{
if (do_log)
{
if (!log_error)
{
log_file.flush();
log_file.close();
do_log = 0;
}
}
}
#endif
/**
* @brief WiFi Event callback.
* Executed when the ESP gets an IP address.
* Sets the time using ntp, and shuts down the wifi.
*
* @param event
* @param info
*/
void on_wifi_ip_callback(WiFiEvent_t event, WiFiEventInfo_t info)
{
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
#ifdef ESP_USE_WIFI
wifi_retries = 0;
ntp_config(ntp_server1, ntp_server2, ntp_server3);
struct tm tm_test;
if (getLocalTime(&tm_test))
{
rtc_time_valid = 1;
}
delay(4000);
// WiFi.disconnect();
// WiFi.mode(WIFI_OFF);
#endif // ESP_USE_WIFI
}
void on_wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info)
{
#ifdef ESP_USE_WIFI
if (rtc_time_valid == 1 && wifi_retries > 10)
{
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
}
else
{
WiFi.reconnect();
wifi_retries++;
}
#endif // ESP_USE_WIFI
}
/**
* @brief A seperate task for receiving MSP messages.
* Currently unused.
*
* @param params
*/
// void msp_receive_task(void *params)
// {
// while (1)
// {
// receive_msp_message();
// }
// }
/**
* @brief Periodic retry if clock is not set.
*
* @param params
*/
void wifi_retry_task(void *params)
{
while (1)
{
vTaskDelay(30000 / portTICK_PERIOD_MS);
#ifdef ESP_USE_WIFI
if (rtc_time_valid == 0 && WiFi.status() != WL_CONNECTED)
{
WiFi.reconnect();
}
#endif // ESP_USE_WIFI
}
}
// void serialFlush(int bytes, HardwareSerial serial_port)
// {
// while (bytes > 0)
// {
// char t = serial_port.read();
// bytes--;
// }
// }
/**
* @brief Send a response to MSP SET request
*
* @param ident Ident to respond to
* @param response Byte to send as response. From enum psa_msp_response
*/
void send_set_response(uint16_t ident, uint8_t response, HardwareSerial *serial)
{
int response_size = sizeof(struct psa_header) + 2;
struct psa_header *header = (struct psa_header *)msp_response_buffer;
header->start = MSP_START_MAGIC;
header->size = 1;
header->direction = '>';
header->ident = ident;
msp_response_buffer[sizeof(*header)] = response;
msp_response_buffer[sizeof(*header) + 1] = calculate_crc((uint8_t *)msp_response_buffer, response_size);
serial->write(msp_response_buffer, response_size);
}
void parse_msp_data(uint16_t ident, uint8_t *packet, uint8_t packet_size, HardwareSerial *serial)
{
struct psa_header *header = (struct psa_header *)packet;
uint8_t *data = (uint8_t *)(packet + sizeof(struct psa_header));
uint8_t data_size = header->size;
uint8_t packet_crc = data[data_size];
uint8_t calculated_crc = calculate_crc(packet, packet_size);
if (packet_crc != calculated_crc)
{
send_set_response(ident, PSA_MSP_CRC_ERROR, serial);
}
switch (ident)
{
case PSA_IDENT_SET_CD_CHANGER_DATA:
if (data_size != sizeof(struct psa_cd_changer_data))
{
send_set_response(ident, PSA_MSP_INVALID_DATA_SIZE, serial);
}
else
{
struct psa_cd_changer_data *cdc_data = (struct psa_cd_changer_data *)data;
if (cdc_data->cds_present > 0b111111 || cdc_data->cd_number > 6 || cdc_data->cd_number < 0 || cdc_data->cds_present < 0)
{
send_set_response(ident, PSA_MSP_INVALID_DATA, serial);
}
else
{
// cd_changer_emu_packet.packet.cd_flag = cdc_data->cds_present;
if (cdc_data->track_time_minutes == 0xff ||
cdc_data->track_time_seconds == 0xff ||
cdc_data->total_tracks == 0xff ||
cdc_data->track_number == 0xff)
{
cd_changer_emu_packet.packet.minutes = 0xff;
cd_changer_emu_packet.packet.seconds = 0xff;
cd_changer_emu_packet.packet.total_tracks = 0xff;
cd_changer_emu_packet.packet.track_num = 0xff;
}
else
{
cd_changer_emu_packet.packet.minutes = dec_to_bcd(cdc_data->track_time_minutes);
cd_changer_emu_packet.packet.seconds = dec_to_bcd(cdc_data->track_time_seconds);
cd_changer_emu_packet.packet.total_tracks = dec_to_bcd(cdc_data->total_tracks);
cd_changer_emu_packet.packet.track_num = dec_to_bcd(cdc_data->track_number);
}
cd_changer_emu_packet.packet.cd_num = cdc_data->cd_number;
cd_changer_emu_packet.packet.status = cdc_data->status;
}
send_set_response(ident, PSA_MSP_OK, serial);
AnswerToCDC();
}
break;
case PSA_IDENT_SET_TRIP_RESET:
if (data_size != sizeof(struct psa_trip_reset_data))
{
send_set_response(ident, PSA_MSP_INVALID_DATA_SIZE, serial);
}
else
{
struct psa_trip_reset_data *trip_data = (struct psa_trip_reset_data *)data;
if (send_VAN_trip_reset((enum psa_trip_meter)trip_data->trip_meter))
{
send_set_response(ident, PSA_MSP_INVALID_DATA, serial);
}
send_set_response(ident, PSA_MSP_OK, serial);
}
break;
default:
send_set_response(ident, PSA_MSP_UNKNOWN_IDENT, serial);
break;
}
}
/**
* @brief Send an MSP packet back.
*
* @param ident Ident to send back
*/
void send_packet(uint16_t ident, HardwareSerial *serial)
{
// Most packets are updated in van_packet_parser.ino
// Here, they are just sent.
// Serial1.printf("Getting ready to send ident %d\r\n", ident);
switch (ident)
{
case PSA_IDENT_VERSIONS:
serial->write(version_packet.buffer, sizeof(version_packet));
break;
case PSA_IDENT_ENGINE:
engine_packet.packet.crc = calculate_crc(engine_packet.buffer, sizeof(engine_packet));
// Serial1.printf("Sent ident %d\r\n", ident);
serial->write(engine_packet.buffer, sizeof(engine_packet));
break;
case PSA_IDENT_RADIO:
radio_packet.packet.crc = calculate_crc(radio_packet.buffer, sizeof(radio_packet));
// Serial1.printf("Sent ident %d\r\n", ident);
serial->write(radio_packet.buffer, sizeof(radio_packet));