int code_list_proc(int msg, DIALOG *d, int c)
{
   static int curr_num_of_codes = 0;
   int num_of_codes = get_number_of_codes();
   int ret;
   
   if (msg == MSG_READY)
   {
      if ((num_of_codes == 0) || (curr_num_of_codes != num_of_codes))
      {
         d->d1 = 0;
         d->d2 = 0;
         current_code_index = 0;
         curr_num_of_codes = num_of_codes;
      }
      return D_REDRAWME;
   }
     
   ret = d_list_proc(msg, d, c);
   
   if (current_code_index != d->d1)
   {
      current_code_index = d->d1;
      broadcast_dialog_message(MSG_READY, 0);
   }
   
   return ret;
}
void read_codes()
{
   while (comport.status != READY)
   {
      if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "Configure &Port", "&Cancel", 'p', 'c') == 1)
         display_options(); // let the user choose correct settings
      else
         return;
   }

   clear_trouble_codes();
   num_of_codes_reported = 0;
   mil_is_on = FALSE;
   broadcast_dialog_message(MSG_READY, 0);
   broadcast_dialog_message(MSG_READ_CODES, 0);
}
void handle_errors(int error, int operation)
{
   static int retry_attempts = NUM_OF_RETRIES;

   if (error == BUS_ERROR || error == UNABLE_TO_CONNECT || error == BUS_INIT_ERROR)
   {
      display_error_message(error, FALSE);
      retry_attempts = NUM_OF_RETRIES;
      clear_trouble_codes();
      num_of_codes_reported = 0;
      mil_is_on = FALSE;
      broadcast_dialog_message(MSG_READY, 0); // tell everyone that we're done
   }
   else    // if we received "BUS BUSY", "DATA ERROR", "<DATA ERROR", SERIAL_ERROR, or RUBBISH,
   {       // try to re-send the request, do nothing if successful and alert user if failed:
      if(retry_attempts > 0) //
      {
         retry_attempts--;
         switch (operation)
         {
            case READ_CODES:
            case READ_PENDING:
            case NUM_OF_CODES:  // if we are currently reading codes,
               broadcast_dialog_message(MSG_READ_CODES, 0); // try reading again
               break;

            case CLEAR_CODES:   // if we are currently clearing codes,
               broadcast_dialog_message(MSG_CLEAR_CODES, 0);  // try clearing again
               break;
         }
      }
      else
      {
         display_error_message(error, FALSE);
         retry_attempts = NUM_OF_RETRIES; // reset the number of retry attempts
         clear_trouble_codes();
         num_of_codes_reported = 0;
         mil_is_on = FALSE;
         broadcast_dialog_message(MSG_READY, 0); // tell everyone that we're done
      }
   }
}
int clear_codes_proc(int msg, DIALOG *d, int c)
{
   int ret;
   switch (msg)
   {
      case MSG_READ_CODES: case MSG_CLEAR_CODES:
         // if we are currently reading or clearing codes, and the button is enabled,
         if (!(d->flags & D_DISABLED))
         {
            d->flags |= D_DISABLED;     // disable the button
            return D_REDRAWME;
         }
         break;
         
      case MSG_READY:
         // if we're not reading or clearing codes, and the button is disabled,
         if (d->flags & D_DISABLED)
         {
            d->flags &= ~D_DISABLED;   // enable it
            return D_REDRAWME;
         }
         break;
   }
   
   ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE)
   {
      while (!simulate && comport.status != READY)
      {
         if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "Configure &Port", "&Cancel", 'p', 'c') == 1)
            display_options(); // let the user choose correct settings
         else
            return D_REDRAWME;
      }
      
      // if user confirmed that he wants to erase the codes,
      if(popup_dialog(confirm_clear_dialog, 6) == 5)
      {
         if (simulate)
            trouble_codes_simulator(FALSE);
         else
            broadcast_dialog_message(MSG_CLEAR_CODES, 0);  //send everyone MSG_CLEAR_CODES
      }
      
      return D_REDRAWME;
   }

   return ret;
}
void trouble_codes_simulator(int show)
{
   char buf[128];
   
   if (show)
   {
      strcpy(buf, SIM_CODES_STRING);
      process_response(NULL, buf);
      clear_trouble_codes();
      num_of_codes_reported = handle_read_codes(buf, FALSE);
      populate_trouble_codes_list();
      mil_is_on = TRUE;
   }
   else
   {
      clear_trouble_codes();
      num_of_codes_reported = 0;
      mil_is_on = FALSE;
   }

   broadcast_dialog_message(MSG_READY, 0);
}
Example #6
0
void options_menu()
{

   show_mouse(screen);
   DIALOG options_dialog[] =
   {
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key) (flags)  (d1)  (d2)  (dp) */
      {d_bitmap_proc,     0,   0,SCREEN_W,SCREEN_H,0,255,     0,     0,      0,    0, title_back},
      {d_gamespeed_proc, 20,  10,    0,    0,    0,   0,      0,D_SELECTED,  0,    0, const_cast<char *>("gamespeed")},
      {d_slider_proc,   300,  20,   30,  440,    0, 255,      0,     0,    255,    get_config_int("MINER","BPS", 32) - 32, NULL,  (void *)speedslider_callback},
      {d_mapsize_proc,   20,  60,    0,    0,    0,   0,      0,     0,      0,    0, const_cast<char *>("mapsize")},
      {d_slider_proc,   300,  20,   30,  440,    0, 255,      0,D_HIDDEN,   80,    get_config_int("MINER","MAP_SIZE",20) - 20, NULL,  (void *)mapsizeslider_callback},
      {d_sound_proc,     20, 110,    0,    0,    0,   0,      0,     0,      0,    0, const_cast<char *>("sound")},
      {d_slider_proc,   300,  40,   30,  300,    0, 255,      0,D_HIDDEN,    6,  (int)log2(get_config_int("MINER","NR_OF_VOICES",0) + 1), NULL, (void *)soundslider_callback},
      {d_text_proc,     300,  10,  200,   30,    0, 255,      0,D_HIDDEN,    0,    0, const_cast<char *>("Nr of simultaneous sounds")},
      {d_number_proc,   330,  20,    0,    0,    0,   0,      0,     0,      3,   get_config_int("MINER","BPS", 32), NULL},
      {d_number_proc,   330,  20,    0,    0,    0,   0,      0,D_HIDDEN,    3,   get_config_int("MINER","MAP_SIZE",20) , NULL},
      {d_number_proc,   330,  40,    0,    0,    0,   0,      0,D_HIDDEN,    2,   get_config_int("MINER","NR_OF_VOICES",1), NULL},
      {d_back_proc,      20, 160,    0,    0,    0,   0,      0,     0,      0,    0, const_cast<char *>("exit menu")},
      {NULL,              0,   0,    0,    0,    0,   0,      0,     0,      0,    0,    0, NULL}
   };


   the_options_dialog = options_dialog; // ugly, but I found it easier to initialise the dialog
   // within the function, where all BITMAPS's are loaded etc.
   PALETTE options_palette;

   for (int i=0;i < 256;i++)
   {
      options_palette[i].r = game_palette[i].g;
      options_palette[i].g = game_palette[i].b;
      options_palette[i].b = game_palette[i].r;
   }
   set_dialog_color(options_dialog, bestfit_color(options_palette,0,0,0), bestfit_color(options_palette,63,63,63));


   DIALOG_PLAYER *p = init_dialog(options_dialog, 1);
   
   broadcast_dialog_message(MSG_DRAW, 0);
//   update_dialog(p);
   if (fade_speed)
   {
      fade_from(desktop_palette,options_palette,fade_speed);
   }
   else
   {
      set_palette(options_palette);
   }


   while(update_dialog(p));

   shutdown_dialog(p);

   set_config_int("MINER","BPS", options_dialog[SPEED_SLIDER].d2 + 32);
   set_config_int("MINER","MAP_SIZE", options_dialog[MAPSIZE_SLIDER].d2 + 20);
   set_config_int("MINER","NR_OF_VOICES", (1<<options_dialog[SOUND_SLIDER].d2));
   show_mouse(NULL);

   reinit();
   
   return;
}
Example #7
0
int save_options_proc(int msg, DIALOG *d, int c)
{
   int ret;
   int old_baud_rate;
   BITMAP *bmp;
   FILE *file;

   ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE)
   {
      old_baud_rate = comport.baud_rate;
      broadcast_dialog_message(MSG_SAVE_OPTIONS, 0);
      
      if (comport.baud_rate != old_baud_rate)
      {
         if (alert("WARNING!", "This operation may cause scan tool to stop responding.", "Are you sure you want to change the baud rate?", "Yes", "No", 0, 0) != 1)
         {
            comport.baud_rate = old_baud_rate;
            broadcast_dialog_message(MSG_REFRESH, 0);
            return D_O_K;
         }
      }

      close_comport(); // close current comport
      open_comport(); // try reinitializing comport (comport.status will be set)
      
      if ((!(display_mode & WINDOWED_MODE) && (display_mode & WINDOWED_MODE_SET)) || ((display_mode & WINDOWED_MODE) && !(display_mode & WINDOWED_MODE_SET)))
      {
         bmp = create_bitmap(SCREEN_W, SCREEN_H);
         if (bmp)
         {
            scare_mouse();
            blit(screen, bmp, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            unscare_mouse();

            if (display_mode & WINDOWED_MODE)
            {
               if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) == 0)
                  display_mode |= WINDOWED_MODE_SET;
               else
                  display_mode &= ~WINDOWED_MODE_SUPPORTED;
            }
            else
            {
               if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0) == 0)
                  display_mode &= ~WINDOWED_MODE_SET;
               else
                  display_mode &= ~FULLSCREEN_MODE_SUPPORTED;
            }

            set_pallete(datafile[MAIN_PALETTE].dat);
            gui_fg_color = C_BLACK;  // set the foreground color
            gui_bg_color = C_WHITE;  // set the background color
            gui_mg_color = C_GRAY;   // set the disabled color
            set_mouse_sprite(NULL); // make mouse use current palette
            blit(bmp, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            show_mouse(screen);
            destroy_bitmap(bmp);
         }
         else
            alert("Error switching display modes.", "Not enough memory to save screen.", NULL, "OK", NULL, 0, 0);
      }

      file = fopen(options_file_name, "a");

      if (file == NULL)
         alert("Options could not be saved, because file", options_file_name, "could not be open for writing", "OK", NULL, 0, 0);
      else
      {
         fclose(file);
         save_program_options();
      }
   }

   return ret;
}
// heart of the trouble_code_reader module:
int tr_code_proc(int msg, DIALOG *d, int c)
{
   static char vehicle_response[1024];        // character buffer for car response
   static int first_read_occured = FALSE;
   static int receiving_response = FALSE;    // flag, "are we receiving response?"
   static int verifying_connection = FALSE;  // flag, "are we verifying connection?"
   static int current_request = CRQ_NONE;    // NUM_OF_CODES, READ_CODES, CLEAR_CODES
   int response_status = EMPTY;              // EMPTY, DATA, PROMPT
   int response_type;                        // BUS_BUSY, BUS_ERROR, DATA_ERROR, etc.
   int pending_codes_cnt = 0;
   char comport_buffer[256];                  // temporary storage for comport data
   char buf1[64];
   char buf2[64];

   switch (msg)
   {
      case MSG_IDLE:
         if (!first_read_occured)
         {
            if (!simulate)
               read_codes();
            first_read_occured = TRUE;
            return D_O_K;
         }
         
         if (simulate)
            break;
         
         if (comport.status == READY)
         {
            if (!receiving_response)
            {
               if (verifying_connection)
               {
                  send_command("0100"); // send request that requires a response
                  receiving_response = TRUE; // now we're waiting for response
                  vehicle_response[0] = 0; //get buffer ready for the response
                  start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
               }
               else if (current_request == READ_PENDING)
               {
                  send_command("07");   // request "pending" codes
                  receiving_response = TRUE;     // and receiving response
                  vehicle_response[0] = '\0';    // clear the buffer
                  start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer...
               }
            }
            else
            {
               response_status = read_comport(comport_buffer);

               if (response_status == DATA) // if data detected in com port buffer
               {
                  // append contents of comport_buffer to vehicle_response:
                  strcat(vehicle_response, comport_buffer);
                  start_serial_timer(OBD_REQUEST_TIMEOUT);  // we got data, reset the timer
               }
               else if (response_status == PROMPT) // if ">" is detected
               {
                  receiving_response = FALSE; // we're not waiting for response any more
                  stop_serial_timer();        // stop the timer
                  // append contents of comport_buffer to vehicle_response:
                  strcat(vehicle_response, comport_buffer);

                  if (verifying_connection)     // *** if we are verifying connection ***
                  {  // NOTE: we only get here if we got "NO DATA" somewhere else
                     response_type = process_response("0100", vehicle_response);
                     verifying_connection = FALSE; // we're not verifying connection anymore

                     if (response_type == HEX_DATA) // if everything seems to be fine now,
                     {
                        if (current_request == CLEAR_CODES)
                           alert("There may have been a temporary loss of connection.", "Please try clearing codes again.", NULL, "OK", NULL, 0, 0);
                        else if (current_request == NUM_OF_CODES)
                           alert("There may have been a temporary loss of connection.", "Please try reading codes again.", NULL, "OK", NULL, 0, 0);
                        else if (current_request == READ_CODES)
                        {
                           current_request = READ_PENDING;
                           break;
                        }
                     }
                     else if (response_type == ERR_NO_DATA)
                     {
                        if (current_request == CLEAR_CODES) // if we were clearing codes,
                           alert("Communication problem: vehicle did not confirm successful", "deletion of trouble codes.  Please check connection to the vehicle,", "make sure the ignition is ON, and try clearing the codes again.", "OK", NULL, 0, 0);
                        else // if we were reading codes or requesting number or DTCs
                           alert("There may have been a loss of connection.", "Please check connection to the vehicle,", "and make sure the ignition is ON", "OK", NULL, 0, 0);
                     }
                     else
                        display_error_message(response_type, FALSE);
                        
                     broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done
                  }

                  else if (current_request == NUM_OF_CODES) // *** if we are getting number of codes ***
                  {
                     response_type = process_response("0101", vehicle_response);

                     if (response_type == ERR_NO_DATA)   // if we received "NO DATA"
                        verifying_connection = TRUE;  // verify connection
                     else if (response_type != HEX_DATA) // if we got an error,
                        handle_errors(response_type, NUM_OF_CODES);  // handle it
                     else    // if process response returned HEX_DATA (i.e. there are no errors)
                     {  // extract # of codes from vehicle_response
                        num_of_codes_reported = handle_num_of_codes(vehicle_response);
                     
                        send_command("03");   // request "stored" codes
                        current_request = READ_CODES;  // we're reading stored codes now
                        receiving_response = TRUE;     // and receiving response
                        vehicle_response[0] = '\0';    // clear the buffer
                        start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer...
                     }
                  }
                  else if (current_request == READ_CODES) // if we are reading codes,
                  {
                     response_type = process_response("03", vehicle_response);

                     if (response_type == ERR_NO_DATA) // vehicle didn't respond, check connection
                     {
                        if (num_of_codes_reported > 0)
                           verifying_connection = TRUE;
                        else
                           current_request = READ_PENDING;
                     }
                     else if (response_type == HEX_DATA)
                     {
                        handle_read_codes(vehicle_response, FALSE);
                        current_request = READ_PENDING;
                     }
                     else  // if we got an error
                        handle_errors(response_type, READ_CODES);
                  }
                  else if(current_request == READ_PENDING) // if we are reading pending codes,
                  {
                     response_type = process_response("07", vehicle_response);

                     if (response_type == ERR_NO_DATA)
                     {
                        if (get_number_of_codes() == 0 && num_of_codes_reported == 0)
                           alert("No Diagnostic Trouble Codes (DTCs) detected", NULL, NULL, "OK", NULL, 0, 0);
                     }
                     else if(response_type != HEX_DATA) // if we got an error,
                     {
                        handle_errors(response_type, READ_PENDING);
                        break;
                     }
                     else  // if there were *no* errors,
                        pending_codes_cnt = handle_read_codes(vehicle_response, TRUE);

                     // if number of DTCs reported by 0101 request does not equal either number or total DTCs or just stored DTCs
                     if ((get_number_of_codes() != num_of_codes_reported) && (get_number_of_codes() - pending_codes_cnt != num_of_codes_reported))
                     {
                        sprintf(buf1, "Vehicle reported %i Diagnostic Trouble Codes (DTCs).", num_of_codes_reported);
                        sprintf(buf2, "However, %i non-pending DTC(s) have been successfully read.", get_number_of_codes() - pending_codes_cnt);
                        alert(buf1, buf2, "Try reading codes again.", "OK", NULL, 0, 0);
                     }

                     populate_trouble_codes_list();
                     broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done
                  }
                  else if(current_request == CLEAR_CODES)
                  {
                     response_type = process_response("04", vehicle_response);

                     if (response_type == ERR_NO_DATA)// vehicle didn't respond, check connection
                        verifying_connection = TRUE;
                     else if(response_type != HEX_DATA) // if we got an error,
                        handle_errors(response_type, CLEAR_CODES);
                     else // if everything's fine (received confirmation)
                     {
                        clear_trouble_codes();
                        num_of_codes_reported = 0;
                        mil_is_on = FALSE;
                        broadcast_dialog_message(MSG_READY, 0);
                     }
                  }
               }
               else if (serial_time_out)     // if request timed out,
               {
                  stop_serial_timer();
                  receiving_response = FALSE;
                  num_of_codes_reported = 0;
                  mil_is_on = FALSE;
                  clear_trouble_codes();
                  broadcast_dialog_message(MSG_READY, 0);

                  if(alert("Device is not responding.", "Please check that it is connected", "and the port settings are correct", "OK",  "&Configure Port", 27, 'c') == 2)
                     display_options();   // let the user choose correct settings

                  while (comport.status == NOT_OPEN)
                  {
                     if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "&Configure Port", "&Ignore", 'c', 'i') == 1)
                        display_options(); // let the user choose correct settings
                     else
                        comport.status = USER_IGNORED;
                  }
               }
            }
         }
         break;  // end case MSG_IDLE

      case MSG_START:
         first_read_occured = FALSE;
         num_of_codes_reported = 0;
         mil_is_on = FALSE;
         // fall through
         
      case MSG_READY:
         receiving_response = FALSE;
         verifying_connection = FALSE;
         current_request = CRQ_NONE;
         break;

      case MSG_READ_CODES:
         if (comport.status == READY)
         {
            send_command("0101"); // request number of trouble codes
            start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
            current_request = NUM_OF_CODES;
            receiving_response = TRUE; // now we're waiting for response
            vehicle_response[0] = 0;
            clear_trouble_codes();
            num_of_codes_reported = 0;
            mil_is_on = FALSE;
         }
         else
            serial_time_out = TRUE;
         break;

      case MSG_CLEAR_CODES:
         if (comport.status == READY)
         {
            send_command("04"); // "clear codes" request
            current_request = CLEAR_CODES;
            receiving_response = TRUE; // now we're waiting for response
            vehicle_response[0] = 0;
            start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer
         }
         else
            serial_time_out = TRUE;
         break;

      case MSG_END:
         stop_serial_timer();
         break;
   }

   return D_O_K;
}  // end of tr_codes_proc()