Tags:
create new tag
, view all tags

Controlling The eBug Via the Multiplexer

Ok, so we're finally here! Take a breath... We have the Uart ready for us to go, everythings nice and installed. Now there's one final bit to go, we have to use some C++ code to write to our uart, (uart2_tx specifically). So lets break this down into three parts:

  1. Setting up the uart
  2. Accessing the command library
  3. Running the code
So lets start...

Setting up the uart2_tx

To do this, we will use a piece of code by Alexandre Proust to help us with this. You'll need both "serial_config.c" and "serial_config.h" for later on, so download them both now. For now however, we will concern ourselves with "serial_config.c"

// $Id: ControllingTheeBugWithMultiplexer.txt,v 1.3 2013/02/05 04:29:54 AlYoung Exp $
/**
 * Library for Microsoft kinect device
 * Autor : Alexandre PROUST
 * Date : 16/06/2011
 *
 * configure serial port for future communication 
 */
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
 
#define BAUDRATE    B115200
#define MODEMDEVICE "/dev/ttyO1"


        ///*************///
        ///Serial config///
        ///*************///
        
int serialconfig()
{
    int srl_handle;
    struct termios options;
    
    srl_handle = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
    if(srl_handle < 0) {
    perror("serial port open");
    exit(-1);
    }
    //~ else {    
    //~ fcntl(srl_handle,F_SETFL,0);
    //~ }
    
    tcgetattr(srl_handle, &options);
    cfsetospeed(&options, BAUDRATE);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(srl_handle, TCSANOW, &options);
    return srl_handle;
}

Ok, if you're not too concerned with the inner workings of this code, skip this section and jump right ahead to Accessing the Command Library, but for those who are interested, let's break this code down so we can understand this.

So firstly these few lines:

#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
 
#define BAUDRATE   B115200
#define MODEMDEVICE "/dev/ttyO1"

Ok, so we can ignore the #include functions, these are command libraries that we will be using in the code and examining them in great detail will consume too much time, so for now let's focus on our #define commands.

Our first, BAUDRATE, sets the amount of bits sent per second. So 115200 baud = 115200 bits/sec.

Our second, MODEMDEVICE, refers to our uart2_tx. This device lives on "/dev/ttyO1", so this command makes MODEMDEVICE=/dev/ttyO1

Next we look inside the int function itself...

srl_handle = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY);

This command opens our uart2_tx device, and places it on the handle "srl_handle", allowing us to call it when we want without the need to run the "open" command each time.

    tcgetattr(srl_handle, &options);
    cfsetospeed(&options, BAUDRATE);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(srl_handle, TCSANOW, &options);
    return srl_handle;

These final commands are configuring the uart to our desired bit rate and for transmitting our packets. Finally, the function should output "srl_handle", our integer from before which opens the uart, which has now been configured to our desired specifications.

Accessing the Command Library

Ok, so we have our uart2_tx open, set-up and ready to go, but what do we send to our eBug to make it move? Nick D'Ademo has written us a large command library which will generate the necessary packets of data without us having to do anything except call the functions. The command libraries can be found here, so to access the commands simply include the header file in your own code and call the functions and they will return the packets we require.

For example aquire the packet to set all the LED's to Red, run the following code:

std::vector&lt;char&gt; command_packet;
command_packet = TLC5947_SetAllRed(4095, false, 0x00);

Command_packet will now contain the packet to set all the LED's to Red.

Running the Code

Ok, so now we have our packets and our uart2_tx is ready to go, all we have to do is put it all together.As mentioned earlier, this code is written in C++, but you may notice that our serial_config is in C! This means that given our configuration, we may need to include some C commands in our file. But not to worry, if you use the skeleton code below, the header files will contain all the necessary commands...

// $Id: ControllingTheeBugWithMultiplexer.txt,v 1.3 2013/02/05 04:29:54 AlYoung Exp $
// Code by Alastair Young, Alexandre Proust and Nick D'Ademo  
// ADYOU1 19874650
// 31/1/2013

#include <stdio.h>        //Normal header stuff
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <stdbool.h>

#include "serial_config.h"     //Alex's setup stuff
#include "eBugAPI.h"        //Nick's command library 

int main()
{
  std::vector<char> command_packet;
    
  int srl_handle = serialconfig();
  
  command_packet = TLC5947_SetAllRed(4095, false, 0x00);
  
  // Send the command packet to the UART.
  write(srl_handle, &command_packet[0], command_packet.size());
  
  printf("Turned on all LEDs red.\n");
  return(0);
}

As you can see, there are a couple of things to watch out for. Firstly, our "command_packet" has to match the variable type being used in the command library, so it must be a std::vector<char>. In order to make this mesh with the write command, we can't call the variable itself, but rather use pointers to tell the code to look for it there.

Now to make this file, as we are using unix the simplest way it to create a make file for this.

  1. Move all the files you wish to use into a seperate folder. (so the folder should contain "control_eyebug.cpp, serial_config.c, serial_config.h, eBugAPI.cpp, eBugAPI.h")
  2. create a new file called Makefile in your custom directory
  3. place the following code into your makefile
 
control_eyebug : control_eyebug.cpp \
       eBugAPI.cpp \
       eBugAPI.h \
       serial_config.h \
       serial_config.c
   g++ -o control_eyebug control_eyebug.cpp eBugAPI.cpp serial_config.c

.PHONY: clean
clean :
   - rm control_eyebug 
  1. Open up the command terminal and switch to the folder containing the makefile
  2. run the command
    make
  3. Our program control_eyebug should now appear in this folder, ready for use!
Congratulations! It was a long road, but we're finally in control of our eyeBugs, lets celebrate with a LED Dance Party...

-- AlYoung - 2013-02-04<

Topic attachments
I Attachment Action Size Date Who Comment
C source code filecpp LED_DANCE_PARTY.cpp manage 3.9 K 2013-02-05 - 04:28 AlYoung LED dance party code
C source code filecpp control_eyebug.cpp manage 0.8 K 2013-02-05 - 04:27 AlYoung skeleton code for eyebug_control
C source code filecpp eBugAPI.cpp manage 19.4 K 2013-02-05 - 03:44 AlYoung command library
Header fileh eBugAPI.h manage 4.9 K 2013-02-05 - 03:44 AlYoung command library headers
C source code filec serial_config.c manage 1.0 K 2013-02-05 - 00:19 AlYoung uart port open
Header fileh serial_config.h manage 0.2 K 2013-02-05 - 00:19 AlYoung uart port open header file
Topic revision: r3 - 2013-02-05 - AlYoung
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback