Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Linux and the iPAQ, Arm in Arm


May 2003/Linux and the iPAQ, Arm in Arm/Listing 1

Listing 1: The main program creates the window, sets up the user interface, and handles events

#include <gtk/gtk.h>
#include <termios.h>
#include <fcntl.h>
#include "dial.h"
#include "numberpad.h"

int serialPort = -1; // Handle to serial port
float oldValue = -1; // To avoid sending redundant commands

void value_changed(GtkAdjustment* adjustment, GtkWidget* label)
{
  unsigned char buffer[16];
  int value = (int) adjustment->value;
 
  if (oldValue != value)
  {
    oldValue = value;

    sprintf(buffer, "%i degrees", value);
    gtk_label_set(GTK_LABEL(label), buffer);

    buffer[0] = 1; // First servo motor has ID# of 1
    buffer[1] = 25 + adjustment->value; // offset 25 = 0 degrees

    write(serialPort, buffer, 2);
  }
}

int openSerialPort(unsigned char* portName)
{
  int port;
  struct termios options;

  port = open("/dev/ttySA0", O_WRONLY | O_NOCTTY | O_NDELAY);
  if (port == -1)
    return -1;

  tcgetattr(port, &options);
  cfsetospeed(&options, B2400);
  tcsetattr(port, TCSANOW, &options);

  return port; 
}

int main(int argc, char* argv[])
{
  GtkWidget *window;
  GtkWidget *label;
  GtkWidget *alignment;
  GtkWidget *vbox;
  GtkWidget *dial, *numberpad;
  GtkWidget *notebook;
  GtkAdjustment *adjustment;
  
  serialPort = openSerialPort("/dev/ttySA0");
  if (serialPort == -1)
  {
    printf("Error: Could not open serial port\n");
    return -1; // Comment this out to run the prototype on a PC
  }

  gtk_init(&argc, &argv);
   
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Servo Controller");
  gtk_signal_connect(GTK_OBJECT(window), "destroy",
          GTK_SIGNAL_FUNC(gtk_exit), NULL);
  
  vbox = gtk_vbox_new(FALSE, 5);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  gtk_widget_show(vbox);

  adjustment = GTK_ADJUSTMENT(
                 gtk_adjustment_new(0, 0, 180, 1, 1, 0));

  dial = gtk_dial_new(adjustment);
  numberpad = numberpad_new(adjustment);

  notebook = gtk_notebook_new();
 
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), dial,
                             gtk_label_new("Analog"));
  gtk_widget_show(dial);

  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), numberpad,
                             gtk_label_new("Digital"));
  gtk_container_set_border_width(GTK_CONTAINER(numberpad), 5);
  gtk_widget_show(numberpad);

  gtk_container_add(GTK_CONTAINER(vbox), notebook);
  gtk_widget_show(notebook);
 
  label = gtk_label_new("0 degrees");
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
  gtk_widget_show(label);

  gtk_signal_connect(GTK_OBJECT(adjustment), "value_changed",
                     GTK_SIGNAL_FUNC(value_changed), label);

  gtk_widget_show(window);
  gtk_main();
    
  close(serialPort);

  return 0;
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.