The Blinky

As it turns out we found it hard to try to resist the Blinky! Here we go. As usual we wanted to blink an LED but not the one on the board. Maybe the on the breadboard is what we wanted. So before we started wiring up we decided to take a look around and found lots of interesting stuff on the internet, we also tried a couple of on board blinky programs available on the BBB web server, tried a few other programs on the Cloud9 IDE using node.js as explained in the BBB web server page, the blinky on the board worked. Cloud9 IDE worked like a charm, we were also able to connect to the BBB Linux shell using SST and terminal programs like PUTTY.  OK so far so good, somehow we still left scratching; maybe we didnt like to do it in node.js or Phyton, maybe we wanted to do it in C and run it as a start-up thread. With this achieved we could pretty much program the BBB to work as an independent unit, may be a networked, cloud connected LED to switch On/Off  :0

So it was time we figured how to connect the LED to the BBB safely, not drawing too much current. SRM has all the details of the connectors P8 & P9 . We chose PIN10 on P8 (GPIO_68 Refer BBB PINOUT) for our experiment because not many peripheral dependencies were there on that pin and it had an output drive required to power an LED.

Beaglebone Black Breadboard LED

Beaglebone Black Breadboard LED

We found a few parts laying around like the LED, berg sticks, hook-up wires, breadboard and a 180 Ohms resistor. That’s all we wanted to really see if we could try the blinky. First we disconnected all wires and cables from the BBB, then we hooked it up as shown below, connect the USB to the computer and it was time to try the code.

Basically we needed to write a C program to get the LED to blink and later make it a part of start-up sequence.

We wrote the C code on the computer using Notepad++, copied the .C file into the BBB directory, compiled the code and ran-it.

Yes the blinky worked.

Blinky Connections – Schematoons

Beagle Bone LED blink, C language

Beagle Bone LED blink, C language

 

 

 

 

 

 

 

 

 

 

C-Laungauage Code

 

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>

#ifndef FALSE
#define FALSE (0)
#endif

#ifndef TRUE
#define TRUE (1)
#endif

#ifndef SUCCESS
#define SUCCESS (0)
#endif

#ifndef FAILURE
#define FAILURE (1)
#endif

#define LED_GPIO_NUM (68) /* Port (2,4) => ((2*32)+4 = 68) */
#define SWITCH_GPIO_NUM (69)

typedef enum gpio_dir_types
{
E_INPUT = 0x00,
E_OUTPUT
}gpio_dir_t;

typedef enum gpio_level_types
{
E_LOW = 0x00,
E_HIGH
}gpio_level_t;

uint8_t
init_gpio (uint8_t gpio_num,gpio_dir_t e_dir)
{
FILE *gpio_dir_fd = NULL;
uint8_t ret_val = FAILURE;
uint8_t set_dir_flag = TRUE;
int8_t dir_file_name[60];

(void)memset (dir_file_name,”,sizeof (dir_file_name));

sprintf (dir_file_name,
“/sys/class/gpio/gpio%d/direction”,
gpio_num);

gpio_dir_fd = fopen (dir_file_name,”r+”);

if( NULL == gpio_dir_fd )
{
/* GPIO files need to be exported. */

int8_t err = -1;
int8_t command_buf[60];

printf (“GPIO Files need to be exported\n”);

(void)memset (command_buf,”,sizeof (command_buf));

sprintf (command_buf,
“echo %d > /sys/class/gpio/export”,
gpio_num);

err = system (command_buf);

if( -1 == err )
{
printf (“Error in execution of following shell command\n”);
printf (“%s\n”,command_buf);
ret_val = FAILURE;
set_dir_flag = FALSE;
}

}
else
{
/* GPIO files has already been exported. */
printf (“GPIO files has already been exported.\n”);
}

if( TRUE == set_dir_flag )
{
if( NULL == gpio_dir_fd )
{
gpio_dir_fd = fopen (dir_file_name,”r+”);
}

if( NULL == gpio_dir_fd )
{
printf (“Error opening direction file\n”);
ret_val = FAILURE;
}
else
{

if( E_INPUT == e_dir )
{
fwrite (“in”,sizeof (“in”),1,gpio_dir_fd);
}
else if( E_OUTPUT == e_dir )
{
fwrite (“out”,sizeof (“out”),1,gpio_dir_fd);
}

fclose (gpio_dir_fd);
ret_val = SUCCESS;
}
}
return (ret_val);
}

uint8_t
de_init_gpio (uint8_t gpio_num)
{
uint8_t ret_val = FAILURE;
int8_t err = -1;
int8_t command_buf[60];

(void)memset (command_buf,”,sizeof (command_buf));

sprintf (command_buf,”echo %d > /sys/class/gpio/unexport”,gpio_num);

err = system (command_buf);

if( -1 == err )
{
printf (“Error in execution of following shell command\n”);
printf (“%s\n”,command_buf);
ret_val = FAILURE;
}
else
{
ret_val = SUCCESS;
}

return (ret_val);
}

uint8_t
set_gpio_output_value (uint8_t gpio_num,gpio_level_t e_out_val)
{
FILE *fd = NULL;
uint8_t ret_val = FAILURE;
int8_t file_name[60];

(void)memset (file_name,”,sizeof (file_name));

sprintf (file_name,”/sys/class/gpio/gpio%d/value”,gpio_num);

fd = fopen (file_name,”r+”);

if( NULL == fd )
{
printf (“Error opening data file\n”);
ret_val = FAILURE;
}
else
{
if( E_LOW == e_out_val )
{
fwrite (“0”,sizeof (“0”),1,fd);
}
else if( E_HIGH == e_out_val )
{
fwrite (“1”,sizeof (“1″),1,fd);
}

fclose (fd);
ret_val = SUCCESS;
}

return (ret_val);
}

uint8_t
get_gpio_input_value (uint8_t gpio_num,gpio_level_t *e_i_val)
{
FILE *fd = NULL;
uint8_t ret_val = FAILURE;
int8_t file_name[60];

(void)memset (file_name,”,sizeof (file_name));

sprintf (file_name,”/sys/class/gpio/gpio%d/value”,gpio_num);

fd = fopen (file_name,”r”);

if( NULL == fd )
{
printf (“Error opening data file\n”);
ret_val = FAILURE;
}
else
{
int8_t read_value = 0;
int8_t cmp_val = -1;

read_value = (int8_t)fgetc (fd);
fclose (fd);

cmp_val = memcmp (“0”,&read_value,sizeof (read_value));

if( 0 == cmp_val )
{
*e_i_val = E_LOW;
ret_val = SUCCESS;
}
else
{

cmp_val = memcmp (“1”,&read_value,sizeof (read_value));

if( 0 == cmp_val )
{
*e_i_val = E_HIGH;
ret_val = SUCCESS;
}
else
{
ret_val = FAILURE;
}
}
}

return (ret_val);
}

void
main (void)
{
gpio_level_t in_value = E_LOW;
uint16_t i = 0;
uint8_t err = FAILURE;

init_gpio (LED_GPIO_NUM,E_OUTPUT);
init_gpio (SWITCH_GPIO_NUM,E_INPUT);

set_gpio_output_value (LED_GPIO_NUM,E_HIGH);

while (1)
{
static uint8_t flag = FALSE;

err = FAILURE;

err = get_gpio_input_value (SWITCH_GPIO_NUM,&in_value);

if( SUCCESS == err )
{
if( E_LOW == in_value )
{
/* Delay to avoid repeated read of the same single
press. */
usleep (300*1000);

printf (“Switch Pressed\n”);

if( TRUE == flag )
{
set_gpio_output_value (LED_GPIO_NUM,E_HIGH);
flag = FALSE;
}
else
{
set_gpio_output_value (LED_GPIO_NUM,E_LOW);
flag = TRUE;
}
}
}
else
{
printf (“Error, while reading GPIO value\n”);
break;
}

}

set_gpio_output_value (LED_GPIO_NUM,E_LOW);

de_init_gpio (LED_GPIO_NUM);
de_init_gpio (SWITCH_GPIO_NUM);

}

 

 

Advertisements
Tagged with: , , , , ,
Posted in Projects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: