|
29 | 29 | * - GPS longitude |
30 | 30 | * |
31 | 31 | * License: 2-Clause BSD License |
32 | | - * Copyright (c) 2023 codingABI |
| 32 | + * Copyright (c) 2024 codingABI |
33 | 33 | * For details see: License.txt |
34 | 34 | * |
35 | 35 | * created by codingABI https://github.com/codingABI/id3esp32obd2 |
|
67 | 67 | * 20230626, Initial version |
68 | 68 | * 20230712, Add support for "PTC heater current", "Outside temperature", "Inside temperature", "Cruising range", "Charging state", "CO2 content interior" |
69 | 69 | * 20230719, Add support for "GPS time", "GPS height", "GPS latitude" and "GPS longitude", Sync ESP32 time with "GPS Time" |
| 70 | + * 20241205, Update arduino-esp32 from 2.0.17 to 3.0.5 (IDF 5.1.4) |
70 | 71 | */ |
71 | 72 |
|
72 | 73 | #include <driver/gpio.h> |
73 | | -#include <driver/can.h> |
| 74 | +#include <driver/twai.h> |
74 | 75 | #include "secrets.h" |
75 | 76 | #include <BluetoothSerial.h> |
76 | 77 | #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) |
|
83 | 84 | #define BTDEVICENAME "id3esp32obd2" |
84 | 85 |
|
85 | 86 | // Some CAN/UDS/ISOTP definitions |
86 | | -#define CAN_RECEIVE_TIMEOUT 1000 // 10000 |
87 | | -#define CAN_SEND_TIMEOUT 4000 |
| 87 | +#define TWAI_RECEIVE_TIMEOUT 1000 // 10000 |
| 88 | +#define TWAI_SEND_TIMEOUT 4000 |
88 | 89 | #define UDS_ReadDataByIdentifier_0x22 0x22 |
89 | 90 | #define UDS_DiagnosticSessionControl_0x10 0x10 |
90 | 91 | #define UDS_ExtendedDiagnosticSession_0x03 0x03 |
@@ -172,63 +173,48 @@ enum beepTypes { DEFAULTBEEP, SHORTBEEP, LONGBEEP, HIGHSHORTBEEP, LASER }; |
172 | 173 | void beep(int type=DEFAULTBEEP) { |
173 | 174 | // User PWM to improve quality |
174 | 175 | switch(type) { |
175 | | - case DEFAULTBEEP: { // 500 Hz for 200ms |
176 | | - ledcSetup(0, 500, 8); |
177 | | - ledcAttachPin(BUZZER_PIN, 0); |
178 | | - ledcWrite(0, 128); |
| 176 | + case DEFAULTBEEP: // 500 Hz for 200ms |
| 177 | + ledcAttach(BUZZER_PIN,500,8); |
| 178 | + ledcWrite(BUZZER_PIN, 128); |
179 | 179 | delay(200); |
180 | | - ledcWrite(0, 0); |
181 | | - ledcDetachPin(BUZZER_PIN); |
| 180 | + ledcWrite(BUZZER_PIN, 0); |
| 181 | + ledcDetach(BUZZER_PIN); |
182 | 182 | break; |
183 | | - } |
184 | | - case SHORTBEEP: { // 1 kHz for 100ms |
185 | | - ledcSetup(0, 1000, 8); |
186 | | - ledcAttachPin(BUZZER_PIN, 0); |
187 | | - ledcWrite(0, 128); |
| 183 | + case SHORTBEEP: // 1 kHz for 100ms |
| 184 | + ledcAttach(BUZZER_PIN,1000,8); |
| 185 | + ledcWrite(BUZZER_PIN, 128); |
188 | 186 | delay(100); |
189 | | - ledcWrite(0, 0); |
190 | | - ledcDetachPin(BUZZER_PIN); |
| 187 | + ledcWrite(BUZZER_PIN, 0); |
| 188 | + ledcDetach(BUZZER_PIN); |
191 | 189 | break; |
192 | | - } |
193 | | - case LONGBEEP: { // 250 Hz for 400ms |
194 | | - ledcSetup(0, 250, 8); |
195 | | - ledcAttachPin(BUZZER_PIN, 0); |
196 | | - ledcWrite(0, 128); |
| 190 | + case LONGBEEP: // 250 Hz for 400ms |
| 191 | + ledcAttach(BUZZER_PIN,250,8); |
| 192 | + ledcWrite(BUZZER_PIN, 128); |
197 | 193 | delay(400); |
198 | | - ledcWrite(0, 0); |
199 | | - ledcDetachPin(BUZZER_PIN); |
| 194 | + ledcWrite(BUZZER_PIN, 0); |
| 195 | + ledcDetach(BUZZER_PIN); |
200 | 196 | break; |
201 | | - } |
202 | 197 | case HIGHSHORTBEEP: { // High and short beep |
203 | | - ledcSetup(0, 5000, 8); |
204 | | - ledcAttachPin(BUZZER_PIN, 0); |
205 | | - ledcWrite(0, 128); |
| 198 | + ledcAttach(BUZZER_PIN,5000,8); |
| 199 | + ledcWrite(BUZZER_PIN, 128); |
206 | 200 | delay(100); |
207 | | - ledcWrite(0, 0); |
208 | | - ledcDetachPin(BUZZER_PIN); |
| 201 | + ledcWrite(BUZZER_PIN, 0); |
| 202 | + ledcDetach(BUZZER_PIN); |
209 | 203 | break; |
210 | 204 | } |
211 | 205 | case LASER: { // Laser like sound |
212 | 206 | int i = 5000; // Start frequency in Hz (goes down to 300 Hz) |
213 | 207 | int j = 300; // Start duration in microseconds (goes up to 5000 microseconds) |
214 | | - ledcSetup(0, i, 8); |
215 | | - ledcAttachPin(BUZZER_PIN, 0); |
216 | | - ledcWrite(0, 0); |
| 208 | + ledcAttach(BUZZER_PIN,i,8); |
217 | 209 | while (i>300) { |
218 | 210 | i -=50; |
219 | 211 | j +=50; |
220 | | - ledcSetup(0, i, 8); |
221 | | - ledcWrite(0, 128); |
222 | | - delayMicroseconds(j); |
223 | | - ledcWrite(0,0); |
224 | | - delayMicroseconds(1000); |
| 212 | + ledcWriteTone(BUZZER_PIN,i); |
| 213 | + delayMicroseconds(j+1000); |
225 | 214 | } |
226 | | - ledcWrite(0, 0); |
227 | | - ledcDetachPin(BUZZER_PIN); |
| 215 | + ledcDetach(BUZZER_PIN); |
228 | 216 | break; |
229 | 217 | } |
230 | | - default: { |
231 | | - } |
232 | 218 | } |
233 | 219 | } |
234 | 220 |
|
@@ -320,16 +306,16 @@ void setup() { |
320 | 306 | setCpuFrequencyMhz(80); |
321 | 307 |
|
322 | 308 | // Init CAN |
323 | | - can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(CTX_PIN, CRX_PIN, CAN_MODE_NORMAL); |
324 | | - can_timing_config_t t_config = CAN_TIMING_CONFIG_500KBITS(); |
325 | | - can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL(); |
326 | | - if (can_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { |
| 309 | + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CTX_PIN, CRX_PIN, TWAI_MODE_NORMAL); |
| 310 | + twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); |
| 311 | + twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); |
| 312 | + if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { |
327 | 313 | Serial.println("CAN driver installed"); |
328 | 314 | } else { |
329 | 315 | Serial.println("Failed to install CAN driver"); |
330 | 316 | } |
331 | 317 |
|
332 | | - if (can_start() == ESP_OK) { |
| 318 | + if (twai_start() == ESP_OK) { |
333 | 319 | Serial.println("CAN driver started"); |
334 | 320 | } else { |
335 | 321 | Serial.println("Failed to start CAN driver"); |
|
0 commit comments