示例#1
0
bool MainWindow::connectRobot (const QString& address) {
  if (m_connectedRobots.end() != m_connectedRobots.find(address)) {
    /* The requested robot is already connected */
    return true;
  }

  auto newrobot = new mobot_t;
  Mobot_init(newrobot);

  /* extract the 8-bit byte array from QString, from which we can then extract
   * the C-string */
  auto baAddress = address.toLocal8Bit();

  if (-1 == Mobot_connectWithAddress(newrobot, baAddress.data(), 1)) {
    delete newrobot;
    qDebug() << "(barobolab) ERROR: Mobot_connectWithTTY failed\n";
    return false;
  }
  Mobot_enableButtonCallback(newrobot, strdup(baAddress.data()), JsInterface::robotButtonCallback);
  auto l = new RobotListener(newrobot, address);
  QObject::connect(l, SIGNAL(scrollUp(QString)), m_interface, SLOT(scrollUpSlot(QString)));
  QObject::connect(l, SIGNAL(scrollDown(QString)), m_interface, SLOT(scrollDownSlot(QString)));
  QThread *thread = new QThread(this);
  l->moveToThread(thread);
  thread->start();
  QMetaObject::invokeMethod(l, "startWork", Qt::QueuedConnection);
  m_connectedRobots.insert(std::make_pair(address, newrobot));
  m_robotListeners.insert(std::make_pair(address, l));
  return true;
}
void on_notebook1_switch_page(GtkNotebook* notebook, gpointer page, guint page_num, gpointer userdata)
{
  static int lastPage = 0;
  static bool buttonCallbackEnabled = false;
  int i;
  int connectPage, controlPage, controlPage2, programPage, posePage;
  GtkWidget *w;

  /* Figure out the page numbers by the number of total pages */
  w = GTK_WIDGET(gtk_builder_get_object(g_builder, "notebook1"));
  int numPages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(w));
  if(numPages == 4) {
    connectPage = 0;
    controlPage = 1;
    controlPage2 = 2;
    posePage = 3;
    programPage = 99;
  } else if (numPages == 5) {
    connectPage = 0;
    controlPage = 1;
    controlPage2 = 2;
    programPage = 3;
    posePage = 4;
  }

  mobot_t* mobot;
  refreshConnectDialog();
  /* If the teaching dialog gets selected, we should initialize all connected
   * Mobots to use our custom button handler. */
  if(page_num == posePage) {
    /* Enable all button handlers */
    for(i = 0; i < g_robotManager->numConnected(); i++) {
      mobot = (mobot_t*)g_robotManager->getMobot(i);
      Mobot_enableButtonCallback(mobot, mobot, on_mobotButtonPress);
      buttonCallbackEnabled = true;
    }
  } else {
    if(buttonCallbackEnabled) {
    /* Disable all button callbacks */
      for(i = 0; i < g_robotManager->numConnected(); i++) {
        mobot = (mobot_t*)g_robotManager->getMobot(i);
        Mobot_disableButtonCallback(mobot);
      }
    }
  }

  /* If the control dialog is selected... */
  if(
      (page_num == controlPage) ||
      (page_num == controlPage2)
      ) 
  {
    w = GTK_WIDGET(gtk_builder_get_object(g_builder, "combobox_connectedRobots"));
    if(g_selectedRobot == -1) {
      g_selectedRobot = 0;
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(w), g_selectedRobot);
    if(page_num == controlPage) {
      g_controlMode = 0;
    } else {
      g_controlMode = 1;
    }
  } else {
    w = GTK_WIDGET(gtk_builder_get_object(g_builder, "combobox_connectedRobots"));
    gtk_combo_box_set_active(GTK_COMBO_BOX(w), -1);
  }

  /* If the connect dialog is selected, refresh the dialog */
  if(page_num == connectPage) {
    refreshConnectDialog();
  }

  lastPage = page_num;
}