Example #1
0
void MainWindow::disconnectRobot (const QString& address) {
  auto it2 = m_robotListeners.find(address);
  if (m_robotListeners.end() != it2) {
    delete it2->second;
    m_robotListeners.erase(it2);
  }

  auto it = m_connectedRobots.find(address);
  if (m_connectedRobots.end() == it) {
    return;
  }
  Mobot_disconnect(it->second);
  delete it->second;
  m_connectedRobots.erase(it);
}
void* findDongleWorkerThread(void* arg)
{
  /* The argument is a pointer to an int */
  int num = *(int*)arg;
  char buf[80];
  mobot_t* mobot;
  int rc;
#if defined (_WIN32) or defined (_MSYS)
  sprintf(buf, "\\\\.\\COM%d", num);
#else
  sprintf(buf, "/dev/ttyACM%d", num);
#endif
  /* Try to connect to it */
  mobot = (mobot_t*)malloc(sizeof(mobot_t));
  Mobot_init(mobot);
  rc = Mobot_connectWithTTY(mobot, buf);
  if(rc != 0) {
    rc = Mobot_connectWithTTY_500kbaud(mobot, buf);
  }
  if(rc == 0) {
    /* We found the Mobot */
    MUTEX_LOCK(&g_giant_lock);
    /* Only update g_mobot pointer if no one else has updated it already */
    if(g_mobot == NULL) {
      g_mobot = mobot;
      g_dongleSearchStatus = DONGLE_FOUND;
      strcpy(g_comport, buf);
      COND_SIGNAL(&g_giant_cond);
      MUTEX_UNLOCK(&g_giant_lock);
    } else {
      MUTEX_UNLOCK(&g_giant_lock);
      Mobot_disconnect(mobot);
      free(mobot);
    }
    Mobot_getStatus(mobot);
  } else {
    free(mobot);
  }
  MUTEX_LOCK(&g_giant_lock);
  g_numThreads--;
  COND_SIGNAL(&g_giant_cond);
  MUTEX_UNLOCK(&g_giant_lock);
  return NULL;
}
void on_button_p2_yes_clicked(GtkWidget* widget, gpointer data)
{
  int i;
  gtk_widget_set_sensitive(widget, false);
  /* First, reprogram the serial ID */
  const char* text;
  char buf[5];
  text = gtk_entry_get_text(
      GTK_ENTRY(gtk_builder_get_object(g_builder, "entry_serialID")));
  if(strlen(text)==4) {
    for(i = 0; i < 5; i++) {
      buf[i] = toupper(text[i]);
    }
    if(strcmp(g_mobot->serialID, buf)) {
      Mobot_setID(g_mobot, buf);
    }
  } else {
    /* Pop up warning dialog */
    GtkWidget* d = gtk_message_dialog_new(
        GTK_WINDOW(gtk_builder_get_object(g_builder, "window1")),
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_MESSAGE_WARNING,
        GTK_BUTTONS_OK,
        "The Serial ID does not have a valid format. The serial ID should consist of four alpha-numeric characters.");
    gtk_dialog_run(GTK_DIALOG(d));
    gtk_widget_hide(d);
    gtk_widget_set_sensitive(widget, true);
    return;
  }

#ifndef _WIN32
  g_hexfilename = strdup("hexfiles/linkbot_latest.hex");
#else
  /* Get the install path of BaroboLink from the registry */
  DWORD size;
  char path[1024];
  HKEY key;
  RegOpenKeyEx(
      HKEY_LOCAL_MACHINE,
      "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\BaroboLink.exe",
      0,
      KEY_QUERY_VALUE,
      &key);

  RegQueryValueEx(
      key,
      "PATH",
      NULL,
      NULL,
      (LPBYTE)path,
      &size);
  path[size] = '\0';

  strcat(path, "\\hexfiles\\linkbot_latest.hex");
  g_hexfilename = strdup(path);
#endif
 
#if 0 
  /* Make sure a file is selected and exists */
  g_hexfilename = gtk_file_chooser_get_filename(
      GTK_FILE_CHOOSER(gtk_builder_get_object(g_builder, "filechooserbutton_hexfile")));
#endif
  if(!fileExists(g_hexfilename)) {
    /* Pop up a warning dialog and abort */
    GtkWidget* d = gtk_message_dialog_new(
        GTK_WINDOW(gtk_builder_get_object(g_builder, "window1")),
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_MESSAGE_ERROR,
        GTK_BUTTONS_OK,
        "File \"%s\" does not exist. \nPlease select a valid hex file to flash to the Linkbot.",
        g_hexfilename);
    int rc = gtk_dialog_run(GTK_DIALOG(d));
    return;
  }

  /* Next, reboot the module and go on to the next page */
  Mobot_reboot(g_mobot);
  Mobot_disconnect(g_mobot);
  free(g_mobot);
  g_mobot = NULL;
  g_timeout_add(3000, switch_to_p3_timeout, NULL);
}