본문 바로가기
Embedded/Kernel Porting

[Kernel Porting-11] H-Smart4412 응용 실습2

by tunanut 2016. 12. 26.
반응형

작업 환경

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

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

-장 비 명 : H-Smart4412


1.Tack Switch와 Dot Matrix 를 이용한 계산기


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

//driver location
#define dot "/dev/dot"
#define tact "/dev/tactsw"

int Opent(void);
int Opend(unsigned char c);

int flag = 0;
unsigned char op, result;
unsigned char c2[3]={0};

int main()
{
    int dot_d, tact_d;
    unsigned char c;
    
    while(1)
    {
        c = Opent();
        Opend(c);

    }

    return 0;
}

int Opent(void)
{
    int d, c=0;
    if((d = open(tact, O_RDWR)) < 0)
    {
        printf("Can't Open2\n");
        exit(0);
    }
    
    while(1)
    {
        read(d,&c,sizeof(c));
        if(c)
            break;
    }
    close(d);
    return c;
}

int Opend(unsigned char c)
{
    int dot_d;
    unsigned char buf[][8] ={
    {0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x1C},   //1
    {0x1C, 0x22, 0x02, 0x02, 0x1C, 0x20, 0x20, 0x3E},   //2
    {0x1C, 0x22, 0x02, 0x1C, 0x02, 0x02, 0x22, 0x1C},   //3
    {0x04, 0x0C, 0x14, 0x24, 0x44, 0x3E, 0x04, 0x04},   //4
    {0x1E, 0x20, 0x20, 0x1C, 0x02, 0x02, 0x22, 0x1C},   //5
    {0x3C, 0x40, 0x40, 0x7C, 0x42, 0x42, 0x42, 0x3C},   //6
    {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81},   //7
    {0x18, 0x18, 0x00, 0xff, 0xff, 0x00, 0x18, 0x18},   //8
    {0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00},   //9
    {0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18},   //+
    {0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00},   //-
    {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81},   //*
    {0x18, 0x18, 0x00, 0xff, 0xff, 0x00, 0x18, 0x18},   ///
    {0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00},   //=
    };

    if((dot_d = open(dot, O_RDWR)) < 0)
    {
        printf("Can't Open\n");
        exit(0);
    }

    if(flag == 1)
    {
        op = c+3;
        printf("op\n");
        write(dot_d, &buf[op-1], sizeof(buf[op-1]));
    }
    if(flag%2 == 0)
    {
        c2[flag] = c;
        printf("np\n");
        write(dot_d, &buf[c-1], sizeof(buf[c-1]));
    }
    sleep(1);
    if(flag == 3)
    {
        write(dot_d, &buf[13],8);
        sleep(1);
        switch(op)
        {
            case 10: result = c2[0] + c2[2];
                printf("asd\n");
                break;
            case 11: result = c2[0] - c2[2];
                break;
            case 12: result = c2[0] * c2[2];
                break;
            case 13: result = c2[0] / c2[2];
                break;
        }

        printf("%d %d  = %d \n", c2[0], c2[2], result);
        write(dot_d, &buf[result-1],sizeof(buf[result-1]));
        sleep(1);
        exit(1);
    }
    flag++;

    close(dot_d);
}


2. Dip Switch로 Dot Matrix 켜기

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#define dot "/dev/dot"
#define dip "/dev/dipsw"

void dott(unsigned char t);
int dipp();

unsigned char c[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
int cnt=0;

int main()
{
    unsigned char t;

    while(1)
    {    
        t = dipp();
        dott(t);
    }
    return 0;
}

int dipp()
{
    unsigned char t;
    int dip_d;

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

    while(1)
    {
        read(dip_d,&t,sizeof(t));
        if(t)
            break;
    }
    close(dip_d);
    return t;
}

void dott(unsigned char t)
{
    unsigned char d,e;
    int dot_d;

    printf("%d\n",t);
    if((dot_d = open(dot, O_RDWR)) < 0)
    {
        printf("Can't Open\n");
        exit(0);
    }

    if((t & 0x01) == 0x01)
    {
        printf("1:%d\n",c[0]);
        c[0] = c[0] << 1;
        if((c[0] == 0x100) || (c[0] == 0))
            c[0] = 0x01;
    }
    else
        c[0] = 0x00;
    if((t & 0x02) == 0x02)
    {
        printf("2:%d\n",c[1]);
        c[1] = c[1] >> 1;
        if(c[1] == 0)
            c[1] = 0x80;
    }
    else
        c[1] = 0x00;
    if((t & 0x08) == 0x08)
    {
        printf("3:%d\n",c[2]);
        d = (c[2]&0xf0)>>1;
        e = (c[2]&0x0f)<<1;
        c[2] = e|d;
        if((c[2] == 0) ||(c[2] ==0x18))
        {
            cnt++;
        }
        if(cnt == 2)
        {
            c[2] = 0x81;
            cnt = 0;
        }
    }
    else
        c[2] = 0x00;
    if((t & 0x10) == 0x10)
    {
        printf("4:%d\n",c[3]);
        d = (c[3]&0xf0)<<1;
        e = (c[3]&0x0f)>>1;
        c[3] = e|d;
        if((c[3] == 0)||(c[3] == 0x100))
            c[3] = 0x18;
    }
    else
        c[3] = 0x00;
    write(dot_d,&c,sizeof(c));
    sleep(1);

    close(dot_d);
}


3. Dot Matrix 전광판 만들기

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#define dot_path "/dev/dot"

int main()
{
    int dot, i, j, k=0, cnt=0, flag=0;
    unsigned char c[9][8] =
                {{0x42, 0x42, 0x42, 0x7E, 0x42, 0x42, 0x42, 0x42},  //H
                {0x3E, 0x20, 0x20, 0x3E, 0x20, 0x20, 0x20, 0x3E},   //E
                {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3E},   //L
                {0x3C, 0x42, 0x81, 0x81, 0x81, 0x81, 0x42, 0x3C},   //O
                {0x81, 0x99, 0x99, 0x99, 0x99, 0x99, 0x5A, 0x24},   //W
                {0x7C, 0x42, 0x42, 0x42, 0x7C, 0x48, 0x44, 0x42},   //R
                {0x38, 0x44, 0x42, 0x42, 0x42, 0x42, 0x44, 0x38},   //D
                {0x00, 0x00, 0x00, 0x33, 0xcc, 0x00, 0x00, 0x00},   //~
                {0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18}    //!
                };
    unsigned char p[8];
    int idx[12] = {0,1,2,2,3,4,3,5,2,6,7,8};

    if((dot = open(dot_path, O_RDWR)) < 0)
    {
        printf("Can't Open\n");
        exit(0);
    }

    while(k<12)
    {
        cnt = idx[k];
        for(i=0;i<16;i++)
        {
            for(j=0;j<8;j++)
            {
                if(flag == 0)
                    p[j] = (c[cnt][j] >> (7-i));
                if(flag == 1)
                    p[j] = (c[cnt][j] << (i-7));
            }
            write(dot,&p,sizeof(p));
            usleep(50000);
            if(i == 7)
                flag = 1;
        }
        flag=0;
        k++;
    }

    close(dot);

    return 0;
}


4. Dot Matrix 사방에서부터 점등

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#define dot_path "/dev/dot"

int main()
{
    int dot, i, j;
    unsigned char init[4]={0x81,0x42,0x24,0x18};
    unsigned char c[8];

    memset(c,0,sizeof(c));

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

    for(i=0;i<4;i++)
    {
        c[i] |=init[i];
        c[7-i] |=init[i];
        write(dot,&c,sizeof(c));
        usleep(300000);
        
        for(j=0;j<6-i;j++)
        {
            c[j+i+1]|=0x80>>i;
            c[7-j-i-1]|=0x01<<i;
            c[i]|=(0x02<<i)<<j;
            c[7-i]|=(0x40>>i)>>j;
            write(dot,&c,sizeof(c));
            usleep(300000);
        }
    }
    
    close(dot);
    return 0;
}


5. Dot Matrix 숫자 회전

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#define dot_path "/dev/dot"

int main()
{
    int dot,i,j,flag=0,num=0,flag2=0;
    unsigned char c[10][8] = {{0x3c,0x42,0x42,0x42,0x42,0x42,0x3c,0x00},
                {0x18,0x28,0x08,0x08,0x08,0x08,0x3c,0x00},
                {0x18,0x24,0x24,0x04,0x08,0x10,0x3c,0x00},
                {0x18,0x24,0x04,0x18,0x04,0x24,0x18,0x00},
                {0x08,0x18,0x28,0x48,0xfc,0x08,0x08,0x00},
                {0x3c,0x20,0x20,0x18,0x04,0x24,0x18,0x00},
                {0x18,0x24,0x20,0x38,0x24,0x24,0x18,0x00},
                {0x3c,0x04,0x04,0x08,0x10,0x10,0x10,0x00},
                {0x18,0x24,0x24,0x18,0x24,0x24,0x18,0x00},
                {0x18,0x24,0x24,0x1c,0x04,0x04,0x18,0x00},
                };
    unsigned char c2[10][8] = {{0x30,0x48,0x84,0x82,0x42,0x24,0x18,0x00},
                {0x00,0x40,0x60,0x50,0x0a,0x04,0x08,0x00},
                {0x00,0x70,0x48,0x48,0x0a,0x0c,0x08,0x00},
                {0x00,0x30,0x48,0x4c,0x12,0x02,0x0c,0x00},
                {0x40,0x62,0x54,0x48,0x54,0x62,0x40,0x00},
                {0x20,0x40,0x8c,0x52,0x22,0x04,0x08,0x00},
                {0x20,0x40,0x88,0x54,0x22,0x14,0x08,0x00},
                {0x20,0x50,0x90,0x10,0x08,0x04,0x02,0x00},
                {0x60,0x90,0x90,0x7c,0x12,0x12,0x0c,0x00},
                {0x20,0x50,0x88,0x54,0x22,0x04,0x08,0x00},
                };
    unsigned char p[8];
    unsigned char a[8];
    unsigned char a2[8];
    unsigned char t[8];
    unsigned char b=0x01;
    unsigned char cnt=0x80;
    

    if((dot = open(dot_path, O_RDWR)) < 0)
    {
        printf("Can't Open\n");
        exit(0);
    }

    while(num<10)
    {
        memset(a,0,sizeof(a));
        memset(a2,0,sizeof(a2));
        memcpy(a,c[num],sizeof(a));
        memcpy(a2,c2[num],sizeof(a2));

        write(dot,a,sizeof(a));
        usleep(200000);

        write(dot,a2,sizeof(a2));
        usleep(200000);

        while(flag<7)
        {
            if(flag2==0)
            {
                memcpy(p,a,sizeof(a));
                printf("1\n");
            }
            else
            {
                memcpy(p,a2,sizeof(a2));
                printf("2\n");
            }

            memset(t,0,sizeof(t));

            for(i=0;i<8;i++)
            {
                for(j=0;j<8;j++)
                {
                    if((p[i]&(b<<j))==(b<<j))
                    {
                        t[j]|=cnt;
                    }
                }
                cnt=cnt>>1;
            }
            memcpy(p,t,sizeof(p));
            write(dot,p,sizeof(p));
            usleep(200000);
            cnt=0x80;
            flag++;
            
            if(flag2==0)
                memcpy(a,p,sizeof(a));
            else
                memcpy(a2,p,sizeof(a2));

            flag2=!flag2;
            //printf("%d\n",flag);
        }
        flag=0;
        flag2=0;
        num++;
    }
    close(dot);
    return 0;
}



반응형