Chinaunix首页 | 论坛 | 博客
  • 博客访问: 626207
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-04-10 10:21:19

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/hardware.h>
#include <asm-arm/arch-s3c2410/gpio.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>

#define msleep(x) usleep(x*1000)
#define DELAY 100

int gpf_fd;
sem_t sem1, sem2;    //信号量 P、V
unsigned int KEY;
unsigned int bit = 0;
unsigned int key_temp = 0;

int key_scan()
{
    unsigned int a, b, key = 0;
    if(ioctl(gpf_fd, GPIO_SET_MULTI_PIN_IN, 0x0F) != 0)    //
    {
        perror("set out error!");
        exit(1);
    }
    read(gpf_fd, &key, sizeof(key));   //
    a = key;
    msleep(10);   //防抖
    b = key;
    if(a == b)
    {
        if(key == 1)
        {
            key_temp++;
            if(key_temp == 5)
            {
                key_temp = 0;
            }
            return key_temp;
        }
        if(key == 8)
        {
            return key;
        }
    }
    else
    {
        return 0;
    }
}

void right_wing(void)   //
{
    if(bit >= 0 && bit < 8)
    {
        ioctl(gpf_fd, GPIO_SET_PIN, bit);
        msleep(DELAY);
        ioctl(gpf_fd, GPIO_CLR_PIN, bit);
        msleep(DELAY);
        bit++;
    }
    else
    {
        bit = 0;
    }
}

void left_wing(void)   //
{
    if(bit >= 0 && bit < 8)
    {
        ioctl(gpf_fd, GPIO_SET_PIN, bit);
        msleep(DELAY);
        ioctl(gpf_fd, GPIO_CLR_PIN, bit);
        msleep(DELAY);
        bit--;
    }
    else
    {
        bit = 7;
    }
}

void ray(void)   //
{
    ioctl( gpf_fd, GPIO_SET_ALL_PIN );
    msleep( DELAY );
    ioctl( gpf_fd, GPIO_CLR_ALL_PIN );
    msleep( DELAY );
}

void come_and_go(void)  //
{
    for(; bit < 7; bit++)
    {
        ioctl(gpf_fd, GPIO_SET_PIN, bit);
        msleep(DELAY);
        ioctl(gpf_fd, GPIO_CLR_PIN, bit);
        msleep(DELAY);
    }
    for(; bit > 0; bit--)
    {
        ioctl(gpf_fd, GPIO_SET_PIN, bit);
        msleep(DELAY);
        ioctl(gpf_fd, GPIO_CLR_PIN, bit);
        msleep(DELAY);
    }
}


void *thread1(void *arg)   //thread 1 --do key
{
    while(1)
    {
        sem_wait(&sem2);     //wait sem2
        KEY = key_scan();
        sem_post(&sem1);     //
    }
    exit(1);
}

void *thread2(void *arg)   //thread 2 --do tasks
{
    unsigned int i = 0;
    while(1)
    {
        sem_wait(&sem1);        //wait sem1
        if(ioctl(gpf_fd, GPIO_SET_MULTI_PIN_OUT, 0xFF) != 0)
        {
            perror("set out error!");
            exit(1);
        }
        if(KEY != 0)
            i = KEY;
        //printf("%d\n", i);    //
        switch(i)
        {
            case 1:
                right_wing();  //右移
                break;
            case 2:
                left_wing();   //左移
                break;
            case 3:
                ray();         //闪烁
                break;
            case 4:
                come_and_go(); //回环点亮
                break;
            default:
                break;
        }
        sem_post(&sem2);       //
    }
    exit(1);
}

int main(void)     //
{
    pthread_t tid1, tid2;
    void *tret;
    
    if((gpf_fd = open("/dev/gpf/0", O_RDWR)) < 0)
    {
        perror("open error!");
        exit(1);
    }

    sem_init(&sem1, 0, 0);
    sem_init(&sem2, 0, 1);
    pthread_create(&tid1, NULL, thread1, NULL);
    pthread_create(&tid2, NULL, thread2, NULL);

    pthread_join(tid1, &tret);
    pthread_join(tid2, &tret);

    return 0;
}

编译
# arm-linux-gcc key_f.c -o key_f -I /home/skyily/tools/kernel/include/ -
lpthread
导进arm板中,运行前 要装gpio驱动 insmod s3c2410
-gpf.o

:s3c2410 gpio驱动,上面的附件里有。


阅读(2203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~