Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed
main.cpp
00001 /* 00002 mbed Dashboard trip computer 00003 December 2014 00004 ******************************************************************************** 00005 WARNING: Use at your own risk, sadly this software comes with no guarantees. 00006 This software is provided 'free' and in good faith, but the author does not 00007 accept liability for any damage arising from its use. 00008 ******************************************************************************** 00009 */ 00010 00011 #include "mbed.h" 00012 #include "ecu_reader.h" 00013 #include "globals.h" 00014 #include "rtos.h" 00015 #include "SDFileSystem.h" 00016 00017 Mutex stdio_mutex; 00018 ecu_reader obdii1(CANSPEED_125); //supports different CAN speeds 00019 ecu_reader obdii2(CANSPEED_250); 00020 ecu_reader obdii3(CANSPEED_500); 00021 InterruptIn switch_reading(p8); 00022 SDFileSystem sd(p5, p6, p7, p12, "sd"); 00023 DigitalOut myled4(LED4); 00024 DigitalOut myled3(LED3); 00025 00026 int flag; //indicates which reading to get from vehicle 00027 int change; //signifies that the system is switching readings 00028 char temp_buffer[20]; //buffers for readings from vehicle 00029 char rpm_buffer[20]; 00030 char speed_buffer[20]; 00031 char time_buff[32]; 00032 char buffer[20]; 00033 float dial_len; 00034 int ui_temp, ui_rpm; 00035 float ui_speed; 00036 int dist_init; 00037 float dist; 00038 time_t seconds; 00039 int testing_temp; //not connected to car 00040 int testing_rpm; //not connected to car 00041 int testing_speed; 00042 00043 void display_coolant_temp(); 00044 void display_rpm(); 00045 void draw_tachometer(); 00046 void draw_thermometer(); 00047 void display_speed(); 00048 void draw_speedometer(); 00049 void display_trip(); 00050 00051 void pb_hit_interrupt (void) { 00052 myled4 = 1; 00053 flag = flag+1; 00054 if (flag>3) { 00055 flag = 0; 00056 ui_temp = -1; 00057 } 00058 else if (flag == 1) 00059 ui_rpm = -1; 00060 else if (flag == 2) 00061 ui_speed = -1; 00062 wait(0.5); 00063 myled4 = 0; 00064 change = 1; 00065 } 00066 void testingThread(void const *args) { // run this thread and set main OBD request conditions to 0 to test UI 00067 ui_temp = -1; 00068 ui_rpm = -1; 00069 testing_temp = 100; 00070 testing_rpm = 0; 00071 dist_init = 0; 00072 testing_speed = 0; 00073 while(1){ 00074 // --- TESTING ONLY - WITHOUT CAR --- 00075 sprintf(temp_buffer,"%d",testing_temp); 00076 sprintf(rpm_buffer,"%d",testing_rpm); 00077 sprintf(speed_buffer,"%d", testing_speed); 00078 dist += 0.1; 00079 testing_temp -= 1; 00080 testing_rpm += 100; 00081 testing_speed +=5; 00082 if (testing_temp<5) { 00083 testing_temp = 100; 00084 } 00085 if (testing_rpm>5000) { 00086 testing_rpm = 900; 00087 } 00088 // --- TESTING ONLY - WITHOUT CAR --- 00089 Thread::wait(1000); 00090 } 00091 } 00092 00093 void sdThread(void const *args) { 00094 dist = -1; 00095 dist_init = -1; 00096 set_time(0); 00097 char file[35]; 00098 char buffer[32]; 00099 char hours[5]; 00100 char minutes[5]; 00101 float hrs = 0; 00102 FILE *fp; 00103 00104 mkdir("/sd/tripStats",0777); 00105 sprintf(file, "/sd/tripStats/log.txt"); 00106 while (dist_init == -1){ // 00107 stdio_mutex.lock(); 00108 if(obdii3.request(DIST_SINCE_CLR, buffer) == 1){ 00109 dist_init = atoi(buffer); 00110 } 00111 stdio_mutex.unlock(); 00112 } 00113 00114 while (1){ 00115 fp = fopen(file, "w"); 00116 myled3 = 1; 00117 seconds = time(NULL); 00118 strftime(time_buff, 32, "%R:%S", localtime(&seconds)); 00119 strftime(hours, 5, "%H", localtime(&seconds)); 00120 strftime(minutes, 5, "%M", localtime(&seconds)); 00121 hrs = atoi(hours) + ((float)atoi(minutes))/60; 00122 if(fp == NULL) { 00123 error("Could not open file for write\n"); 00124 } 00125 stdio_mutex.lock(); 00126 if(obdii3.request(DIST_SINCE_CLR, buffer) == 1){ 00127 dist = ((float)atoi(buffer)-dist_init)*0.62; 00128 } 00129 stdio_mutex.unlock(); 00130 fprintf(fp, "Time since mbed start: %s\n\n", time_buff); 00131 fprintf(fp, "Distance Traveled: %2.2f miles\n", dist); 00132 fprintf(fp, "Average Speed: %2.2f mph\n", dist/hrs); 00133 fclose(fp); 00134 myled3 = 0; 00135 Thread::wait(30000); //write to SD card every 30 seconds 00136 } 00137 } 00138 00139 int main() { 00140 ui_temp = -1; //initialize default values 00141 ui_rpm = -1; 00142 ui_speed = -1; 00143 change = 0; 00144 flag = 0; 00145 dial_len = 60.0; 00146 lcd.cls(); 00147 lcd.baudrate(3000000); 00148 switch_reading.mode(PullUp); 00149 wait(0.2); 00150 switch_reading.fall(&pb_hit_interrupt); 00151 Thread t2(sdThread); 00152 //Thread test(testingThread); //run this thread and change OBD request conditions to 0 to test UI 00153 00154 while (1) { //main thread 00155 if (change == 0) { //update LCD only when not in process of switching screens 00156 switch (flag){ 00157 case 0: //engine coolant temperature 00158 if(obdii3.request(ENGINE_COOLANT_TEMP, temp_buffer) == 1){ //set to 1 for car, 0 for testing UI 00159 stdio_mutex.lock(); 00160 display_coolant_temp(); 00161 stdio_mutex.unlock(); 00162 } 00163 else { 00164 can2.reset(); 00165 wait(0.5); 00166 } 00167 break; 00168 case 1: //engine rpm 00169 if (obdii3.request(ENGINE_RPM, rpm_buffer) == 1){ //set to 1 for car, 0 for testing UI 00170 stdio_mutex.lock(); 00171 display_rpm(); 00172 stdio_mutex.unlock(); 00173 } 00174 else { 00175 can2.reset(); 00176 wait(0.5); 00177 } 00178 break; 00179 case 2: //vehicle speed 00180 if (obdii3.request(VEHICLE_SPEED, speed_buffer) == 1){ //set to 1 for car, 0 for testing UI 00181 stdio_mutex.lock(); 00182 display_speed(); 00183 stdio_mutex.unlock(); 00184 } 00185 else { 00186 can2.reset(); 00187 wait(0.5); 00188 } 00189 break; 00190 case 3: //distance since codes last cleared 00191 if(obdii3.request(DIST_SINCE_CLR, buffer) == 1){ 00192 dist = ((float)atoi(buffer)-(float)dist_init)*0.62; 00193 stdio_mutex.lock(); 00194 display_trip(); 00195 stdio_mutex.unlock(); 00196 } 00197 else { 00198 stdio_mutex.lock(); 00199 display_trip(); 00200 stdio_mutex.unlock(); 00201 can2.reset(); 00202 wait(0.5); 00203 } 00204 break; 00205 } 00206 } 00207 else { 00208 lcd.cls(); 00209 change = 0; 00210 } 00211 } 00212 } 00213 00214 void display_coolant_temp(){ //displays temperature 00215 float farenheit = atoi(temp_buffer) * 9/5 +32; 00216 draw_thermometer(); 00217 lcd.color(0x909090); 00218 lcd.text_width(2); 00219 lcd.text_height(2); 00220 lcd.locate(1,0); 00221 lcd.printf("%s\n\r", "COOLANT\n TEMP."); 00222 lcd.color(0x66CCFF); 00223 lcd.locate(0,3); 00224 lcd.printf("%3.0f \n\r", farenheit); 00225 lcd.locate(0,4); 00226 lcd.printf("oF\n\r"); 00227 } 00228 00229 void draw_thermometer(){ //draws temperature animation 00230 lcd.rectangle(73,47,119,128,0xFFFFFF); 00231 int new_temp = atoi(temp_buffer); 00232 int height = 128-ceil((float)new_temp*.8); 00233 00234 if (new_temp > 100 && ui_temp!=new_temp) { //red 00235 lcd.filled_rectangle(74,48,118,67,0xFF0000); 00236 } 00237 else if (new_temp > 75 && ui_temp!=new_temp) { 00238 if (ui_temp>new_temp) lcd.filled_rectangle(74,48,118,height,0x000000); 00239 lcd.filled_rectangle(74,height,118,67,0xFF0000); 00240 } 00241 else lcd.filled_rectangle(74,48,118,67,0x000000); 00242 if (new_temp > 75 && ui_temp!=new_temp) { //dark orange 00243 lcd.filled_rectangle(74,68,118,87,0xFF4719); 00244 } 00245 else if (new_temp > 50 && ui_temp!=new_temp) { 00246 if (ui_temp>new_temp) lcd.filled_rectangle(74,68,118,height,0x000000); 00247 lcd.filled_rectangle(74,height,118,87,0xFF4719); 00248 } 00249 else lcd.filled_rectangle(74,68,118,87,0x000000); 00250 if (new_temp > 50 && ui_temp!=new_temp) { //orange 00251 lcd.filled_rectangle(74,88,118,107,0xFF9933); 00252 } 00253 else if (new_temp > 25 && ui_temp!=new_temp) { 00254 if (ui_temp>new_temp) lcd.filled_rectangle(74,88,118,height,0x000000); 00255 lcd.filled_rectangle(74,height,118,107,0xFF9933); 00256 } 00257 else lcd.filled_rectangle(74,88,118,107,0x000000); 00258 if (new_temp > 25 && ui_temp!=new_temp) { //yellow 00259 lcd.filled_rectangle(74,108,118,128,0xFFFF99); 00260 } 00261 else if (new_temp > 0 && ui_temp!=new_temp) { 00262 if (ui_temp>new_temp) lcd.filled_rectangle(74,108,118,height,0x000000); 00263 lcd.filled_rectangle(74,height,118,128,0xFFFF99); 00264 } 00265 else lcd.filled_rectangle(74,108,118,128,0x000000); 00266 ui_temp = new_temp; 00267 } 00268 00269 void display_rpm(){ //displays rpm 00270 draw_tachometer(); 00271 lcd.color(0x909090); 00272 lcd.text_width(2); 00273 lcd.text_height(2); 00274 lcd.locate(1,0); 00275 lcd.printf("%s\n\r", "ENG RPM"); 00276 lcd.color(0x66CCFF); 00277 lcd.text_width(3); 00278 lcd.text_height(3); 00279 lcd.locate(1,1); 00280 lcd.printf("%s \n\r", rpm_buffer); 00281 } 00282 00283 void draw_tachometer(){ //draws rpm gauge 00284 lcd.circle(64,120,60,0xFFFFFF); 00285 int new_rpm = atoi(rpm_buffer); 00286 int x = int(cos(float(new_rpm)*0.00041887902)*dial_len); 00287 int y = int(sin(float(new_rpm)*0.00041887902)*dial_len); 00288 if (ui_rpm!=new_rpm && ui_rpm!=-1) { 00289 int old_x = int(cos(float(ui_rpm)*0.00041887902)*dial_len); 00290 int old_y = int(sin(float(ui_rpm)*0.00041887902)*dial_len); 00291 lcd.line(64-old_x,120-old_y,64,120,0x000000); 00292 } 00293 lcd.line(64-x,120-y,64,120,0xFF8C00); 00294 ui_rpm = new_rpm; 00295 } 00296 00297 void display_speed() { //displays speed 00298 draw_speedometer(); 00299 lcd.color(0x909090); 00300 lcd.text_width(2); 00301 lcd.text_height(2); 00302 lcd.locate(1,0); 00303 lcd.printf("%s\n\r", "SPEED"); 00304 lcd.color(0x66CCFF); 00305 lcd.text_width(3); 00306 lcd.text_height(3); 00307 lcd.locate(1,1); 00308 lcd.printf("%3.0f \n\r", (((float)atoi(speed_buffer))*0.62)); 00309 } 00310 00311 void draw_speedometer(){ //draws gauge for speed 00312 lcd.circle(64,120,60,0xFFFFFF); 00313 float new_speed = atoi(speed_buffer) * 0.62; 00314 int x = int(cos(new_speed*0.0314159265)*dial_len); 00315 int y = int(sin(new_speed*0.0314159265)*dial_len); 00316 if (ui_speed!=new_speed && ui_speed!=-1) { 00317 int old_x = int(cos(ui_speed*0.0314159265)*dial_len); 00318 int old_y = int(sin(ui_speed*0.0314159265)*dial_len); 00319 lcd.line(64-old_x,120-old_y,64,120,0x000000); 00320 } 00321 lcd.line(64-x,120-y,64,120,0xFF8C00); 00322 ui_speed = new_speed; 00323 } 00324 00325 void display_trip() { //displays data being output to SD card 00326 lcd.color(0x909090); 00327 lcd.text_width(2); 00328 lcd.text_height(2); 00329 lcd.locate(0,0); 00330 lcd.printf("%s\n\r", "TOTALTIME"); 00331 lcd.color(0x66CCFF); 00332 lcd.text_width(3); 00333 lcd.text_height(3); 00334 lcd.locate(0,1); 00335 strftime(time_buff, 32, "%R", localtime(&seconds)); 00336 lcd.printf("%s \n\r", time_buff); 00337 00338 lcd.color(0x909090); 00339 lcd.text_width(2); 00340 lcd.text_height(2); 00341 lcd.locate(0,4); 00342 lcd.printf("%s\n\r", "DISTANCE"); 00343 lcd.color(0x66CCFF); 00344 lcd.text_width(3); 00345 lcd.text_height(3); 00346 lcd.locate(0,4); 00347 lcd.printf("%3.1f \n\r", dist); 00348 }
Generated on Wed Jul 13 2022 21:41:21 by
1.7.2