/*The Main Program*/ int main(int argc, char** argv) { /* defines the tray_icon, as well as init gtk*/ g_set_application_name(PACKAGE_NAME); parse_command_line_options(argc,argv); register_thread("Main Thread"); if (!queue_init()) print("queue_init FAILED",NULL,ERROR); print("Glade File",glade_file,DEBUG); g_thread_init(NULL); gtk_init(NULL,NULL); gtk_init(&argc, &argv); Hosts_lock = g_mutex_new(); Userpath_lock = g_mutex_new(); g_mutex_lock(Userpath_lock); Userpath = g_strdup(getenv("HOME")); g_mutex_unlock(Userpath_lock); settings_init(); rest_init(); if (!xml_init()) print("xml_init FAILED",NULL,ERROR); init_hostname(); /*sets the tray icon from the create_tray_icon*/ create_tray_icon(); playing_info_music pInfo = {"Artist","Album","Song",0,0,0}; /* declares the playing info struct, and print if, if _DEBUG is definded at the top of msdaemon.c*/ /*inits the dbus and get the first set of info*/ dbus_init(); pInfo = dbus_get_playing_info_music(); print_playing_info_music(pInfo); get_active_devices(NULL); update_song_info(); GError *error; if ( (network_thread = g_thread_create((GThreadFunc)rest_thread_handler, NULL, FALSE, &error)) == NULL){ print("Error Creating Network Thread",error->message,ERROR); g_error_free(error); } if ( (file_thread = g_thread_create((GThreadFunc)file_thread_handler, NULL, FALSE, &error)) == NULL){ print("Error Creating Network Thread",error->message,ERROR); g_error_free(error); } if ( (gui_thread = g_thread_create((GThreadFunc)gui_thread_handler, NULL, FALSE, &error)) == NULL){ print("Error Creating Network Thread",error->message,ERROR); g_error_free(error); } g_timeout_add (1000,(GSourceFunc) get_next_command,NULL); g_timeout_add (300000,(GSourceFunc) update_active_devices,NULL); init_status_window(FALSE,glade_file); start_tray(); g_free(Userpath); deauthenticate(); return 0; }
int main(int argc, const char *argv[]) { uint8_t ret; uint8_t init = 1; cfg_t *cfg; int32_t adc_channel = -1; uint32_t val_cur[MCP32XX_MAX_CHANNEL], val_old[MCP32XX_MAX_CHANNEL]; int32_t diff; struct mcp32xx_dev mcp_dev; struct volume_handle v_handle = {0}; struct mpd_handle m_handle = {0}; struct lcd_handle l_handle = {0}; useconds_t sleep_usec; uint8_t song_update_count_max; uint8_t song_update_count = 0; cfg_opt_t opts[] = { CFG_INT("UpdateInterval", 300000, CFGF_NONE), CFG_INT("SongUpdateInterval", 2000000, CFGF_NONE), CFG_INT("ChannelUsed", 2, CFGF_NONE), CFG_INT("VolumeChannel", 0, CFGF_NONE), CFG_INT("TunerChannel", 1, CFGF_NONE), CFG_INT("MaxLineChar", 16, CFGF_NONE), CFG_INT("MaxLineBuf", 80, CFGF_NONE), CFG_STR("HostName", "localhost", CFGF_NONE), CFG_INT("Port", 6600, CFGF_NONE), CFG_INT("MaxTunerPos", 12, CFGF_NONE), CFG_INT("MaxVolSteps", 50, CFGF_NONE), CFG_STR("RadioPlaylistName", "stations", CFGF_NONE), CFG_INT("LCD-Pin_RS", 25, CFGF_NONE), CFG_INT("LCD-Pin_E", 24, CFGF_NONE), CFG_INT("LCD-Pin_DB4", 23, CFGF_NONE), CFG_INT("LCD-Pin_DB5", 17, CFGF_NONE), CFG_INT("LCD-Pin_DB6", 27, CFGF_NONE), CFG_INT("LCD-Pin_DB7", 22, CFGF_NONE), CFG_STR("FillPatternFirstLine", " ++ ", CFGF_NONE), CFG_STR("FillPatternSecondLine", " ** ", CFGF_NONE), CFG_INT("MaxLineLength", 16, CFGF_NONE), CFG_INT("MaxLineBufferLength", 80, CFGF_NONE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); ret = cfg_parse(cfg, "/etc/radioberry.conf"); if(ret == CFG_FILE_ERROR) { perror("/etc/radioberry.conf"); printf("WARN: could not read /etc/radioberry.conf," " process with default values\n"); } else if (ret == CFG_PARSE_ERROR) { printf("configuration parse error\n"); goto out; } /* get configuration values */ channel_used = (uint8_t)cfg_getint(cfg, "ChannelUsed"); volume_chan = (uint8_t)cfg_getint(cfg, "VolumeChannel"); tuner_chan = (uint8_t)cfg_getint(cfg, "TunerChannel"); sleep_usec = cfg_getint(cfg, "UpdateInterval") / channel_used; printf("sleep_usec %d\n", sleep_usec); song_update_count_max = cfg_getint(cfg, "SongUpdateInterval") / sleep_usec; /* pass over pointer to handles of mpd, lcd and volume */ m_handle.lh = &l_handle; v_handle.mh = &m_handle; m_handle.cfg = cfg; l_handle.cfg = cfg; signal(SIGINT, sigintterm_handler); signal(SIGTERM, sigintterm_handler); ret = lcd_init(&l_handle); if (ret) { printf("failed to initialize lcd, lcd control disabled \n"); lcd_control_enabled = 0; } if (lcd_control_enabled) { lcd_print_string("Radioberry"); lcd_move_cursor_down(); lcd_print_string("=== Welcome ==="); } ret = mcp32xx_init(&mcp_dev); if (ret) { printf("failed to initialize spi device, analog control" " devices disabled\n"); tuner_control_enabled = 0; vol_control_enabled = 0; } ret = init_mpd_handle(&m_handle); if (ret) { printf("could not initialize mpd client," " tuner control disabled\n"); tuner_control_enabled = 0; } /* disable tuner control if we cannot get any station at all. If one * station fails, let's process and wait for tuner changes */ ret = load_stations_playlist(&m_handle); if (ret == 1) { printf("could not load radio stations list," " tuner control disabled\n"); tuner_control_enabled = 0; } ret = init_vol_control(&v_handle); if (ret) { printf("could not initialize volume control," " volumen control disabled\n"); vol_control_enabled = 0; } running = 1; while ( running ) { usleep(sleep_usec); if (adc_channel + 1 == channel_used) { adc_channel = 0; mpd_status_update(m_handle.mpd_obj); /* detect if initial loop is done. Start update lcd * after initial loop */ if (init) init = 0; else { if (lcd_control_enabled) lcd_update_screen(&l_handle); } } else adc_channel++; if (song_update_count == song_update_count_max) { update_song_info(&m_handle); song_update_count = 0; } else song_update_count++; /* loop over all channels in singel ended mode * and reset counter in the last loop. */ val_cur[adc_channel] = mcp32xx_get_val(&mcp_dev, adc_channel); /* fill the old value buffer and intialize the player with * current settings*/ if (init) { val_old[adc_channel] = val_cur[adc_channel]; ret = process_value(&v_handle, &m_handle, val_cur[adc_channel], adc_channel); /* skip comparing old/new values while intializing */ if (ret) { printf("unrecoverable error, quit now\n"); break; } else continue; } diff = val_old[adc_channel] - val_cur[adc_channel]; #ifdef DEBUG printf("adc_channel: %u, old %u, new %u, diff %d\n", adc_channel, val_cur[adc_channel], val_old[adc_channel], diff); #endif if (abs(diff) < TOLERANT_THRESHOLD) continue; #ifdef DEBUG if (diff > 0) printf("Poti %u was turned left, current val: %d\n", adc_channel + 1, val_cur[adc_channel]); else printf("Poti %u was turned right, current Pos: %d\n", adc_channel + 1, val_cur[adc_channel]); #endif process_value(&v_handle, &m_handle, val_cur[adc_channel], adc_channel); val_old[adc_channel] = val_cur[adc_channel]; } out: lcd_clear_screen(); lcd_close(&l_handle); mcp32xx_close(&mcp_dev); close_vol_ctl(&v_handle); close_mpd_handle(&m_handle); exit(ret); }