Embedded/Kernel Porting

[Kernel Porting-10] H-Smart4412 응용 실습1

tunanut 2016. 12. 26. 19:45
반응형

작업 환경

-메인 OS : Windows 8.1K(Intel Core i5-4590)

-작업 OS : Ubuntu 14.04 64bit(VirtualBox)

-장 비 명 : H-Smart4412


1.Tack Switch로 LED 작동시키기

#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>

#define tact_d "/dev/tactsw"
#define led_d "/dev/led"

void led_right(int led);
void led_left(int led);
void led_on(int led);
void led_twist(int led);
void led_inside(int led);
void led_off(int led);

unsigned char c;
int tact, led;

int main()
{
    if((tact = open(tact_d,O_RDWR)) < 0)
    {
        perror("tact open");
        exit(1);
    }

    if((led = open(led_d,O_RDWR)) < 0)
    {
        perror("led open");
        exit(1);
    }

    while(1)
    {
        read(tact,&c,sizeof(c));
        if(c)
            break;
    }

    while(1)
    {
        switch(c)
        {
            case 1: led_right(led);
                break;
            case 2: led_left(led);
                break;
            case 3: led_inside(led);
                break;
            case 4: led_twist(led);
                break;
            case 5: led_on(led);
                break;
            case 6: led_off(led);
                break;
            case 12: exit(1);
        }
        sleep(1);
    }

    close(tact);
    close(led);
    return 0;
}

void led_right(int led)
{
    int i;
    unsigned char data=0x00;

    while(c==1||c==0)
    {
        write(led,&data,sizeof(unsigned char));
        data = 1;

        for(i=0;i<8;i++)
        {
            data = data>>1;
            data |= 0x80;
            write(led,&data,sizeof(unsigned char));
            usleep(50000);
        }

        for(i=0;i<8;i++)
        {
            data = data>>1;
            write(led,&data,sizeof(unsigned char));
            usleep(50000);
        }
        data=0x00;
        read(tact,&c,sizeof(c));
    }
}

void led_left(int led)
{
    int i;
    unsigned char data=0x00;

    while(c==2||c==0)
    {
        write(led,&data,sizeof(unsigned char));
        data = 1;

        for(i=0;i<8;i++)
        {
            write(led,&data,sizeof(unsigned char));
            data = data<<1;
            data |= 1;
            usleep(50000);
        }

        for(i=0;i<8;i++)
        {
            write(led,&data,sizeof(unsigned char));
            data = data<<1;
            usleep(50000);
        }
        write(led,&data,sizeof(unsigned char));
        data=0x00;
        read(tact,&c,sizeof(c));
    }
}

void led_on(int led)
{
    int i;
    unsigned char data=0x00;

    
    data = ~data;
    write(led,&data,sizeof(unsigned char));
    while(c==5||c==0)
    {
        read(tact,&c,sizeof(c));
    }
}

void led_twist(int led)
{
    int i;
    unsigned char data=0xAA;

    while(c==4||c==0)
    {
        for(i=0;i<16;i++)
        {
            data = ~data;
            write(led,&data,sizeof(unsigned char));
            usleep(100000);
        }
        data=0x00;
        read(tact,&c,sizeof(c));
    }
}

void led_inside(int led)
{
    int i;
    unsigned char data=0x00;

    while(c==3||c==0)
    {
        for(i=0;i<4;i++)
        {
            data |= 1<<i;
            data |= 0x80>>i;
            write(led,&data,sizeof(unsigned char));
            usleep(100000);
        }

        for(i=0;i<4;i++)
        {
            data &= ~(1<<i);
            data &= ~(0x80>>i);
            write(led,&data,sizeof(unsigned char));
            usleep(100000);
        }
        data=0x00;
        read(tact,&c,sizeof(c));
    }
}

void led_off(int led)
{
    int i;
    unsigned char data=0x00;

    write(led,&data,sizeof(unsigned char));
    while(c==6||c==0)
    {
        read(tact,&c,sizeof(c));
    }
    
}


2. Tact Switch로 LED 녹화하기

#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>

#define tact_d "/dev/tactsw"
#define led_d "/dev/led"

void ChaseAdd(int led, unsigned char *chase);

int tact, led;

int main()
{
    unsigned char c;
    unsigned char buf=0xff, chase=0xff;

    if((tact = open(tact_d,O_RDWR)) < 0)
    {
        perror("tact open");
        exit(1);
    }

    if((led = open(led_d,O_RDWR)) < 0)
    {
        perror("led open");
        exit(1);
    }

    while(1)
    {
        while(1)
        {
            read(tact,&c,sizeof(c));
            if(c)
                break;
        }

        switch(c)
        {
            case 1: buf = ~(0x01);
                write(led,&buf,sizeof(buf));
                break;
            case 2: buf = ~(0x02);
                write(led,&buf,sizeof(buf));
                break;
            case 3: buf = ~(0x04);
                write(led,&buf,sizeof(buf));
                break;
            case 4: buf = ~(0x08);
                write(led,&buf,sizeof(buf));
                break;
            case 5: buf = ~(0x10);
                write(led,&buf,sizeof(buf));
                break;
            case 6: buf = ~(0x20);
                write(led,&buf,sizeof(buf));
                break;
            case 7: buf = ~(0x40);
                write(led,&buf,sizeof(buf));
                break;
            case 8: buf = 0x7f;
                write(led,&buf,sizeof(buf));
                break;
            case 9: buf = 0x00;
                write(led,&buf,sizeof(buf));
                break;
            case 10: ChaseAdd(led,&chase);
                break;
            case 11: write(led,&chase,sizeof(chase));
                break;
            case 12: exit(1);
        }
        sleep(1);
        buf = 0xff;
        write(led,&buf,sizeof(buf));
    }

    close(tact);
    close(led);
    return 0;
}

void ChaseAdd(int led,unsigned char *chase)
{
    sleep(1);
    unsigned char d=0;
    printf("input\n");
    while(1)
    {
        while(1)
        {
            read(tact,&d,sizeof(d));
            if(d)
                break;
        }
        
        printf("%d\n",d);
        switch(d)
        {
            case 1: *chase &= ~(0x01);
                break;
            case 2: *chase &= ~(0x02);
                break;
            case 3: *chase &= ~(0x04);
                break;
            case 4: *chase &= ~(0x08);
                break;
            case 5: *chase &= ~(0x10);
                break;
            case 6: *chase &= ~(0x20);
                break;
            case 7: *chase &= ~(0x40);
                break;
            case 8: *chase &= ~(0x80);
                break;
            case 10: return;
                break;
            case 12: exit(1);
        }
        sleep(1);
        d=0;
    }
    printf("chase end\n");
    
}


3. Tact Switch를 누르면 LCD에 값 출력(10자가 넘으면 왼쪽부터 삭제)

#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>

#define tact_d "/dev/tactsw"
#define lcd_d "/dev/clcd"

int main()
{
    int tact, lcd, flag=0;
    char op;
    unsigned char data[10];
    unsigned char c;

    if((tact = open(tact_d,O_RDWR)) < 0)
    {
        perror("open");
        exit(1);
    }

    if((lcd = open(lcd_d,O_RDWR)) < 0)
    {
        perror("open 2");
        exit(1);
    }

    memset(data,0,sizeof(data));

    while(1)
    {
        while(1)
        {
            read(tact,&c,sizeof(c));
            if(c)
                break;
        }

        if(flag >= 10)
        {
            strcpy(data,data+1);
        }

        switch(c)
        {
            case 1: strcat(data,"1");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 2: strcat(data,"2");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 3: strcat(data,"3");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 4: strcat(data,"4");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 5: strcat(data,"5");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 6: strcat(data,"6");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 7: strcat(data,"7");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 8: strcat(data,"8");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 9: strcat(data,"9");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 10: strcat(data,"0");
                write(lcd,&data,sizeof(data));
                flag++;
                break;
            case 11: exit(1);
            case 12: exit(1);
        }

        sleep(1);
        c=0;
    }

    close(tact);
    return 0;
}


4. Tact Switch와 Dip Switch가 동시에 On일 때만 LED 점등(녹화 기능 포함)

#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>

#define tact_d "/dev/tactsw"
#define dip_d "/dev/dipsw"
#define led_d "/dev/led"

void ChaseAdd(int led, int tact, unsigned char *chase);

int main()
{
    int tact, dip, led, flag=0;
    char op;
    unsigned char data, chase=0xff, chase2=0xff;
    unsigned char c,t;

    if((tact = open(tact_d,O_RDWR)) < 0)
    {
        perror("open");
        exit(1);
    }

    if((dip = open(dip_d,O_RDWR)) < 0)
    {
        perror("open 3");
        exit(1);
    }

    if((led = open(led_d,O_RDWR)) < 0)
    {
        perror("open 2");
        exit(1);
    }

    while(1)
    {
        while(1)
        {
            read(tact,&c,sizeof(c));
            if(c)
                break;
        }
        read(dip,&t,sizeof(t));

        switch(c)
        {
            case 1: if((t & 0x01) == 0x01)
                {
                    data = 0xfe;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 2: if((t & 0x02) == 0x02)
                {
                    data = 0xfd;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 3: if((t & 0x04) == 0x04)
                {
                    data = 0xfb;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 4: if((t & 0x08) == 0x08)
                {
                    data = 0xf7;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 5: if((t & 0x10) == 0x10)
                {
                    data = 0xef;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 6: if((t & 0x20) == 0x20)
                {
                    data = 0xdf;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 7: if((t & 0x40) == 0x40)
                {
                    data = 0xbf;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 8: if((t & 0x80) == 0x80)
                {
                    data = 0x7f;
                    write(led,&data,sizeof(unsigned char));
                }
                break;
            case 9: data = ~t;
                write(led,&data,sizeof(unsigned char));
                break;
            case 10: ChaseAdd(led,tact,&chase);
                break;
            case 11: chase2 = ~(~chase & t);
                 write(led,&chase2,sizeof(unsigned char));
                break;
            case 12: exit(1);
        }

        sleep(1);
        c=0;
    }

    close(tact);
    close(dip);
    close(led);
    return 0;
}

void ChaseAdd(int led, int tact, unsigned char *chase)
{
    sleep(1);
    unsigned char d=0;
    printf("input\n");
    while(1)
    {
        while(1)
        {
            read(tact,&d,sizeof(d));
            if(d)
                break;
        }
        
        printf("%d\n",d);
        switch(d)
        {
            case 1: *chase &= ~(0x01);
                break;
            case 2: *chase &= ~(0x02);
                break;
            case 3: *chase &= ~(0x04);
                break;
            case 4: *chase &= ~(0x08);
                break;
            case 5: *chase &= ~(0x10);
                break;
            case 6: *chase &= ~(0x20);
                break;
            case 7: *chase &= ~(0x40);
                break;
            case 8: *chase &= ~(0x80);
                break;
            case 10: return;
                break;
            case 12: exit(1);
        }
        sleep(1);
        d=0;
    }
    printf("chase end\n");
    
}



반응형