LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2004, 07:40 PM   #1
GridX
LQ Newbie
 
Registered: Sep 2003
Location: Canada
Distribution: Fedora & Gentoo-uClinux
Posts: 15

Rep: Reputation: 0
Programming a cylon LED display


I need to write a program that turns one LED on at a time, starting at the right of the display, cycling to the left and
then back to the right. Repeat this three times, at an appropriate speed.


The following code will set pins 1 of the LCD header(GPIO pin C7) and the SPI header (GPIO pin D5) high,
hold for one second, then set them both low. This will alternate 10 times.
what I am trying to do is when the lcd board is connected, Ineed to create the lcd's light up and light off, like the cylon from battlestar galactica.

not sure how to modiy this code, I know I can use the usleep() call to implement shorter delays.


Code:
#include <stdio.h>
#include <stdlib.h>
#include <linux/dimmio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>

#define COUNT 10 /*Number of times to alternate the pin states*/

/* Configures a pin by port and bit number */
void set_config_by_port_bit(struct dimm_io *pin,
			    int handle,
			    short port,
			    short bit,
			    unsigned char mode,
			    unsigned char pull_up_dn) 
{
  int error;
  
  pin->port_number = port;
  pin->bit_number = bit;
  pin->mode = mode;
  pin->pull_up_dn = pull_up_dn;
  
  error = ioctl(handle, DIMM_IO_IOCTL_SET_CONFIG_BY_PORT_BIT, pin);
  if (error <0) 
    {
      fprintf(stderr, "Unable to configure device. Error code: %i\n", errno);
      close(handle);
      exit(1);
    }
}


/* Reads a pin using offset */
void read_by_offset(struct dimm_io *pin, int handle) 
{
  int error;

  lseek(handle, pin->offset, SEEK_SET);
  error = read(handle, &(pin->state), 1);
  if (error <0) 
    {
      fprintf(stderr, "Error in reading from device.  Error code: %i\n", errno);
      close(handle);
      exit(1);
    }
}


/* Writes a value to a pin using offset */
void write_by_offset(struct dimm_io *pin, int handle) 
{
  int error;
  
  lseek(handle, pin->offset, SEEK_SET);
  error = write(handle, &(pin->state), 1);
  if (error <0) 
    {
      fprintf(stderr, "Error in writing to device\n");
      close(handle);
      exit(1);
    }
}

int main() {
  struct dimm_io pinC7,pinD5;
  extern int errno;
  int handle, loop=0;


	handle = open("/dev/io1",O_RDWR);
	if (handle < 0) {
		printf("Unable to open device\n");
		exit(1);
	}

	/* Port C bit 7 (JP6-1), output, pull-up/pull-down disabled */
	set_config_by_port_bit(&pinC7, handle, 2, 7, 1, 0);

	/* Port D bit 5 (JP2-1), output, pull-up/pull-down disabled */
	set_config_by_port_bit(&pinD5, handle, 3, 5, 1, 0);

	
	while (loop <= COUNT){

                pinC7.state = 1;
                pinD5.state = 1;
		write_by_offset(&pinC7, handle);
		write_by_offset(&pinD5, handle);
		sleep(1);
                pinC7.state = 0;
                pinD5.state = 0;
		write_by_offset(&pinC7, handle);
		write_by_offset(&pinD5, handle);
		sleep(1);
		loop++;
	}


	close(handle);
	return 0;
}

thanks

GridX
 
Old 02-12-2004, 11:28 PM   #2
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
This sounds like a school project to me. Here is a hint:

Forget about all the LED stuff and write a simple program that
counts from 0 to N, then N-1 to 0. Sleep for a second
before you output each number. Call it three times.

Each number represents the LED you are going to light up.
First you should turn off the previous LED, then light the new one,
then sleep for another second.

You should be able to make the leap from there.

By the way, the way I've done cylons before is a little different.
I used it to monitor entry into the "idle" loop in the OS. I had
to keep two static int values -- one for position and one for
direction. Then I could call tickCylon() whenever I wanted and
it would move the LED.

Have fun with it. Try making the cylon "eye" three LEDs in
width to see how it changes your program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HDD Led always on!!! ivanvart Mandriva 12 05-31-2006 10:38 AM
CD RW LED keeps flashing arubin Linux - Hardware 1 11-17-2005 03:54 PM
what the 2 led means? cigarstub Linux - Hardware 1 10-21-2005 09:48 PM
HDD Led always on!!! ivanvart Linux - Hardware 2 06-10-2005 06:06 AM
Led settings jijujin Debian 2 05-08-2005 07:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration