Skip to content

Instantly share code, notes, and snippets.

@pineoc
Last active December 31, 2015 11:39
Show Gist options
  • Save pineoc/7981344 to your computer and use it in GitHub Desktop.
Save pineoc/7981344 to your computer and use it in GitHub Desktop.
project for embedded system
#include <stdio.h>
#include <stdlib.h> /* for exit */
#include <unistd.h> /* for open/close .. */
#include <fcntl.h> /* for O_RDONLY */
#include <sys/ioctl.h> /* for ioctl */
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <sys/mman.h> /* for mmap */
#include <linux/fb.h> /* for fb_var_screeninfo, FBIOGET_VSCREENINFO */
#include <pthread.h>
#include <signal.h>
typedef unsigned char ubyte;
/* EMPOSII의 그래픽 LCD 전체를 다 선택하기위해 XPOS1,YPOS1,
XPOS2, YPOS2 를 선언함 */
#define XPOS1 0
#define YPOS1 0
#define XPOS2 640
#define YPOS2 480
//----------------------------------------------------------
//for textlcdport//
#define TEXTLCDPORT_BASE 0xbc
#define TEXTLCD_COMMAND_SET _IOW(TEXTLCDPORT_BASE,0,32)
#define TEXTLCD_FUNCTION_SET _IOW(TEXTLCDPORT_BASE,1,32)
#define TEXTLCD_DISPLAY_CONTROL _IOW(TEXTLCDPORT_BASE,2,32)
#define TEXTLCD_CURSOR_SHIFT _IOW(TEXTLCDPORT_BASE,3,32)
#define TEXTLCD_ENTRY_MODE_SET _IOW(TEXTLCDPORT_BASE,4,32)
#define TEXTLCD_RETURN_HOME _IOW(TEXTLCDPORT_BASE,5,32)
#define TEXTLCD_CLEAR _IOW(TEXTLCDPORT_BASE,6,32)
#define TEXTLCD_DD_ADDRESS _IOW(TEXTLCDPORT_BASE,7,32)
#define TEXTLCD_WRITE_BYTE _IOW(TEXTLCDPORT_BASE,8,32)
//----------------------------------------------------------
//for 7segment
#define SEGPORT_BASE 0xbb
#define ioctl_bit_low _IOW(SEGPORT_BASE,0,2)
#define ioctl_bit_high _IOW(SEGPORT_BASE,1,2)
//----------------------------------------------------------
//strcommand------------------------------------------------
struct strcommand_varible {
char rows;
char nfonts;
char display_enable;
char cursor_enable;
char nblink;
char set_screen;
char set_rightshit;
char increase;
char nshift;
char pos;
char command;
char strlength;
char buf[20];
};
//----------------------------------------------------------
//mouse event정보를 읽어오기위한 구조체
typedef struct ts_event {
unsigned short x;
unsigned short y;
unsigned short pressure;
} TS_MSG_EVENT;
static TS_MSG_EVENT tsMsgEvent;
//device
static int push;
static int gpio;
static int touch;
static int txtlcd;
static int led;
static int gpio_led;
static int sevenS;
//val
static unsigned char vkey;
static unsigned short pixel;
static unsigned short *pfbdata;
static int offset;
static unsigned char r,g,b;
static char buff[40];
static int brush_size; // brush size for 7 button
static int brush_type; // brush type for 8 button
//thrad id
pthread_t clear;
pthread_t draw;
pthread_t textlcd_tid;
pthread_t led_tid;
//function
void pushfunc(int sig);
unsigned short makepixel(ubyte r,ubyte g, ubyte b);
void cleardisplay();
void drawfunc();
void textlcd_welcome();
void textlcd_rgb();
void led_bitlighting();
void sevenSegment();
int main(int argc, char *argv[])
{
int fb;
pid_t id;
short t,tt;
brush_type=0;
brush_size=0;
/* flame buffer 디바이스를 open 함 */
fb = open("/dev/fb", O_RDWR);
if(fb < 0)
{
printf("ERROR fbdev open\n");
exit(1);
}
touch = open("/dev/touch0", O_RDWR);
if(touch<0)
{
printf("Error on Touch open\n");
exit(1);
}
push = open("/dev/pushbuttonport",O_RDWR);
if(push<0)
{
printf("error on pushbutton open\n");
exit(1);
}
gpio = open("/dev/gpio",O_RDWR);
if(gpio <0)
{
printf("Error on gpio\n");
exit(1);
}
gpio_led = open("/dev/gpio_led",O_RDWR);
if(gpio_led<0)
{
printf("error on gpio_led\n");
exit(1);
}
txtlcd = open("/dev/textlcdport",O_RDWR);
if(txtlcd<0)
{
printf("error on textlcdport\n");
exit(1);
}
led = open("/dev/ledport",O_RDWR);
if(led<0)
{
printf("error on Led\n");
exit(1);
}
sevenS = open("/dev/7segmentport",O_RDWR);
if(sevenS<0)
{
printf("error on 7Segment\n");
exit(1);
}
(void)signal(SIGUSR1,pushfunc);
id=getpid();
write(push,&id,4);
r=0;g=0;b=0;
pfbdata = (unsigned short *) mmap(0,XPOS2*YPOS2*2,
PROT_READ|PROT_WRITE, MAP_SHARED, fb, 0);
if((unsigned)pfbdata == (unsigned)-1)
{
printf("Error fbdev mmap\n");
exit(1);
}
pthread_create(&clear,NULL,(void*)cleardisplay,NULL);
pthread_create(&draw,NULL,(void*)drawfunc,NULL);
pthread_create(&textlcd_tid,NULL,(void*)textlcd_rgb,NULL);
pthread_create(&led_tid,NULL,(void*)led_bitlighting,NULL);
pthread_join(draw,NULL);
pthread_join(clear,NULL);
pthread_join(textlcd_tid,NULL);
pthread_join(led_tid,NULL);
munmap(pfbdata, XPOS2*YPOS2);
close(fb);
close(push);
close(gpio);
close(textlcd);
close(led);
return 0;
}
void pushfunc(int sig)
{
read(push,&vkey,1);
}
/* r,g,b의 3byte값을 2byte 값으로 표시하기 위해서 */
unsigned short makepixel(ubyte r, ubyte g, ubyte b)
{
return (unsigned short)(((r>>3)<<11)|((g>>2)<<5)|(b>>3));
}
void cleardisplay()
{// use gpio
while(1)
{
int t,tt;
read(gpio,buff,40);
pixel = makepixel(255, 255, 255);
for(t = YPOS1; t < YPOS2; t++)
{
offset = t * XPOS2;
for(tt = XPOS1; tt < XPOS2; tt++)
{
*(pfbdata + offset + tt) = pixel;
}
}
}
}
void drawfunc()
{
char buf;
while(read(touch,&tsMsgEvent, sizeof(TS_MSG_EVENT))>0)
{
/*
switch(vkey)
{
case 0x0: break;
case 0x1: r=(r+25)%255;break;
case 0x2: r=(r-25);break;
case 0x4: g=(g+25)%255;break;
case 0x8: g-=25;break;
case 0x10: b=(b+25)%255;break;
case 0x20: b-=25;break;
case 0x40: r=0;g=0;b=0;break;
case 0x80: r=255;g=255;b=255;break;
}
*/
buf=1;
write(gpio_led,&buf,1);
pixel = makepixel(r, g, b);
//draw pixel rectangle( 9 pixel)
switch(brush_type)
{
case 0:
offset = (tsMsgEvent.x) + ((tsMsgEvent.y)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x) + ((tsMsgEvent.y+1)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x) + ((tsMsgEvent.y-1)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x+1) + ((tsMsgEvent.y)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x+1) + ((tsMsgEvent.y+1)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x+1) + ((tsMsgEvent.y-1)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x-1) + ((tsMsgEvent.y)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x-1) + ((tsMsgEvent.y+1)*XPOS2);
*(pfbdata + offset)=pixel;
offset = (tsMsgEvent.x-1) + ((tsMsgEvent.y-1)*XPOS2);
*(pfbdata + offset)=pixel;
break;
case 1: //square
for(i=-brush_size;i<brush_size;i++)
{
for(j=-brush_size;j<brush_size;j++)
{
offset = (tsMsgEvent.x+i) + ((tsMsgEvent.y)*XPOS2+j);
*(pfbdata + offset)=pixel;
}
}
break;
case 2: //Diamond
i=0;
j=0;
int k=0;
while(k<brush_size)
{
offset = (tsMsgEvent.x+i) + ((tsMsgEvent.y)*XPOS2+j);
*(pfbdata + offset)=pixel;
if(k<brush_size/2)
{
i=i+1;
j=j-1;
}
else
{
i=i-1;
j=j+1;
}
k++;
}
break;
case 3:
i=-brush_size;
while(i<brush_size)
{
offset=(tsMsgEvent.x+i) + ((tsMsgEvent.y)*XPOS2);
*(pfbdata + offset)=pixel;
i++;
}
break;
case 4:
i=-brush_size;
while(i<brush_size)
{
offset=(tsMsgEvent.x) + ((tsMsgEvent.y)*XPOS2)+i;
*(pfbdata + offset)=pixel;
i++;
}
break;
}
}
buf=0;
write(gpio_led,&buf,1);
}
void textlcd_welcome()
{
int i;
char welcome[32]="Welcome to painter";
struct strcommand_varible strcommand;
strcommand.rows = 0;
strcommand.nfonts = 0;
strcommand.display_enable = 1;
strcommand.cursor_enable = 0;
strcommand.nblink = 0;
strcommand.set_screen = 0;
strcommand.set_rightshit= 1;
strcommand.increase = 1;
strcommand.nshift = 0;
strcommand.pos = 0;
strcommand.command = 1;
strcommand.strlength = 18;
ioctl(textlcd,TEXTLCD_CLEAR,&strcommand,32); // clear
strcommand.pos = 0; // pointing pos to start
ioctl(textlcd,TEXTLCD_DD_ADDRESS,&strcommand,32); // address
for(i=0;i<18;i++)
{
memcpy(&strcommand.buf[0],&welcome[i],1);
ioctl(textlcdport,TEXTLCD_WRITE_BYTE,&strcommand,32);
}
}
void textlcd_rgb()
{
int i;
char rgbstr[40];
struct strcommand_varible strcommand;
strcommand.rows = 0;
strcommand.nfonts = 0;
strcommand.display_enable = 1;
strcommand.cursor_enable = 0;
strcommand.nblink = 0;
strcommand.set_screen = 0;
strcommand.set_rightshit= 1;
strcommand.increase = 1;
strcommand.nshift = 0;
strcommand.pos = 0;
strcommand.command = 1;
strcommand.strlength = 18;
while(1)
{
sprintf(rgbstr,"r: %d, g: %d, b: %d",r,g,b);
ioctl(textlcd,TEXTLCD_CLEAR,&strcommand,32); // clear
strcommand.pos = 40; // pointing pos to start
ioctl(textlcd,TEXTLCD_DD_ADDRESS,&strcommand,32); // address
for(i=0;i<20;i++)
{
memcpy(&strcommand.buf[0],&rgbstr[i],1); //each rgbstr[i] memory copy to strcommand.buf[0]
ioctl(textlcdport,TEXTLCD_WRITE_BYTE,&strcommand,32); //write lcd rgbstr
}
usleep(100);
}
}
void led_bitlighting()
{
char buf[1];
int quit = 1;
while(quit)
{
switch(vkey)
{
case 0x0: break;
case 0x1:
buf[0]=0b00000001;vkey = 0;
r=100;
break;
case 0x2:
buf[0]=0b00000010;vkey = 0;
g=100;b=100;
break;
case 0x4:
buf[0]=0b00000100;vkey = 0;
g=100;
break;
case 0x8:
buf[0]=0b00001000;vkey = 0;
b=100;
break;
case 0x10:
buf[0]=0b00010000;vkey = 0;
r-=50; g-=50; b-=50;
break;
case 0x20:
buf[0]=0b00100000;vkey = 0;
r+=50; g+=50; b+=50;
break;
case 0x40:
buf[0]=0b01000000;vkey = 0;
// brush size up
brush_size = (brush_size++)%5;
break;
case 0x80:
buf[0]=0b10000000;vkey = 0;
//brush square, diamond, line
brush_type = (brush_type++)%5;
break;
default :
quit = 0; break;
}
write(led,buf,1);
}
}
void sevenSegment()
{
while(1)
{
ioctl(sevenS, ioctl_bit_low, &brush_type, 2);
ioctl(sevenS, ioctl_bit_high, &brush_size, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment