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