Andy's CarPlayer

This is the main.c source code. It handles all the inital setup and runs the main menu. The selection of tracks, albums etc is handled in PlayTracks.c


/***************************************************************************
                          main.c  -  description
                             -------------------
    begin                : Mon Feb 28 2005
    copyright            : (C) 2005 by Andy
    email                : 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

  #include 
 #include 
 #include 
 #include 


 #include "display.h"
 #include "main.h"
 #include "log.h"
 #include "tools.h"
 #include "mpg_interface.h"

// MYSQL mysql;
 
 int Connect_DB()
 {
   char *host = "localhost";
   char *user = "mp3";
   char *passwd = "mp3";
   char *database = "mp3";
   mysql_init(&mysql);
   if (!mysql_real_connect(&mysql, host, user, passwd, database, 0, NULL, 0))
   {
     printf("Error connecting to database.\n");
     exit(-1);
   }
   else
     printf("Connected...\n");
 }

 MYSQL_RES *db_query(char *query)
 {
   MYSQL_RES *res;

   res = (MYSQL_RES *)mysql_real_query(&mysql, query, (unsigned int)strlen(query));
   if (res)
   {
     log_printf(LOG_NORMAL, "db_query : Error making query (%s) : %s\n", query, mysql_error(&mysql));
     return ((MYSQL_RES *) -1);
   }
   res = mysql_store_result(&mysql);
   return (res);
 }

 main()
 {
   MYSQL_RES *res;
   MYSQL_ROW row;

   char option, LCD_buffer[17];
   int terminate = FALSE, status=SONG_STOP;
   char date_buffer[11], time_buffer[11];

   debug_level = LOG_DEBUG;

   log_open("./logfile", debug_level);
   Connect_DB();
   setup_initial_arrays();
   res = db_query("SELECT COUNT(*) FROM all_songs WHERE available = 1");
   row = mysql_fetch_row(res);
   MP3_system.number_of_tracks = atoi(row[0]);
   mysql_free_result(res);
/*   res = db_query("SELECT COUNT(distinct artist) FROM all_songs WHERE available = 1");
   row = mysql_fetch_row(res);
   MP3_system.number_of_artists = atoi(row[0]);
   mysql_free_result(res);
   res = db_query("SELECT COUNT(distinct album) FROM all_songs WHERE available = 1");
   row = mysql_fetch_row(res);
   MP3_system.number_of_albums = atoi(row[0]);
   mysql_free_result(res);
*/
   CRT_setup();

//   mvwprintw(player_win[INFO_SCREEN], 1, 2, "Number of Tracks %i", MP3_system.number_of_tracks);
 //  mvwprintw(player_win[INFO_SCREEN], 2, 2, "Number of Artists %i", MP3_system.number_of_artists);
  // mvwprintw(player_win[INFO_SCREEN], 3, 2, "Number of Albums %i", MP3_system.number_of_albums);

   FifoSetup();
   tty_setup();
   tty_write("4", "   Starting up...");
  
   mixer_set_volume("vol", 80, 0);  // This is the Master volume mixer channel.  This will probably never change.
   mixer_set_volume("pcm", 75, 0);  // This is the mixer channel for the mpg123.  Use the PCM channel for internal volume changes.
   MP3_system.PCM_volume = get_volume("pcm");
   mpg123_start();
   mpg123_load("./startup.mp3");
   do
   {
     status = mpg123_read();
     if (status != -2) log_printf(LOG_NOISY_DEBUG, "main : status = %i\n", status);
   }
   while (status != SONG_END);
   
//   wtimeout(player_win[MAIN_SCREEN], 1000); 
   main_menu();
   
   while (!terminate)
   {
//     option = FifoRead();
     option = wgetch(player_win[MAIN_SCREEN]);
     switch (option)
     {
       case '.':
         endwin();
         LCD_clear();
         tty_write("4", "   Bye Bye..");
         terminate = TRUE;
         mpg123_quit();
         break;
       case '1':
         play_single_track();
         main_menu();
         break;
       case '2':
         random_play();
         main_menu();
         break;
       case '3':
         play_artist();
         main_menu();
         break;
       case '4':
         play_album();
         main_menu();
         break;
       case '9':
         print_database();
         break;
       case '/':
         new_random_play_init();
         main_menu();
         break;
       default:
         get_time(time_buffer);
         get_date(date_buffer);
         MP3_system.PC_temp = temperature();
         mvwprintw(player_win[MAIN_SCREEN], 12, 2, "Temperature : %iC", temperature());
         mvwprintw(player_win[MAIN_SCREEN], 13, 2, "%s %s", date_buffer, time_buffer);
         sprintf(LCD_buffer, "Vol : %-3i T: %-2iC", MP3_system.PCM_volume, MP3_system.PC_temp);
         tty_write("7", LCD_buffer);
         get_short_date(date_buffer);
         sprintf(LCD_buffer, "%s %s", date_buffer, time_buffer);
         tty_write("8", LCD_buffer);
         if (MP3_system.play_status == RECONFIGURE)
         {
//           main_menu();
           MP3_system.play_status = STOPPED;
           if (MP3_system.playing_type == SELECT_ARTIST)
           {
             log_printf(LOG_NORMAL, "Reconfigure to Artist : %s\n", current_song.artist);
             do
               play_artist_number(-1);
             while(MP3_system.play_status != TERMINATE);
           }
           if (MP3_system.playing_type == SELECT_ALBUM)
           {
             log_printf(LOG_NORMAL, "Reconfigure to Album : %s\n", current_song.album);
             do
               play_album_number(-1);
             while (MP3_system.play_status != TERMINATE);
           }
    //       random_play();
    //       MP3_system.play_status = STOPPED;
           main_menu();
         }
         break;
     }
   }
 }

 void read_config()
 {
   char *defaults = "\
## This is the main configuration file for the carputer media player.\n\n\
## logging settings\n\
##\n\
## The logging settings are from 0 to 5.  LOG_NORMAL is 2.\n\
##\n\
## LOG_QUIET, LOG_ERROR, LOG_NORMAL, LOG_VERBOSE, LOG_DEBUG, LOG_NOISY_DEBUG\n\n\
debug=2\n\
## file locations\n\
mp3root=/mp3\n\
##\n\
## SETTINGS\n\n\
## These settings are to be adjusted for tailoring the system defaults\n\
volume=75\n\
startup=/startup.mp3\
";
}