00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdlib.h>
00011 #include <fcntl.h>
00012 #include <sys/stat.h>
00013 #include <termio.h>
00014 #include <string.h>
00015 #include <stdarg.h>
00016
00017 #include "crystalfontz634.h"
00018
00019 int c634_open(char const *path)
00020 {
00021 int fd;
00022 struct termios termios_p;
00023
00024 fd = open(path, O_WRONLY);
00025 if (fd == -1)
00026 return -1;
00027
00028 tcgetattr(fd, &termios_p);
00029 termios_p.c_cflag &= ~CSTOPB & ~CRTSCTS;
00030 termios_p.c_cflag |= CLOCAL | CREAD;
00031 termios_p.c_oflag &= ~OPOST;
00032 tcgetattr(fd, &termios_p);
00033 termios_p.c_cflag = CS8 | CLOCAL | CREAD | B19200;
00034 termios_p.c_iflag = IGNPAR;
00035 termios_p.c_oflag = 0;
00036 termios_p.c_lflag = 0;
00037 termios_p.c_cc[VMIN] = 0;
00038 termios_p.c_cc[VTIME] = 0;
00039 (void)tcflush(fd, TCIFLUSH);
00040 tcsetattr(fd, TCSANOW, &termios_p);
00041
00042 return fd;
00043 }
00044
00045 void c634_close(int fd)
00046 {
00047 close(fd);
00048 }
00049
00050 void c634_home(int fd)
00051 {
00052 char b = 1;
00053 write(fd, &b, 1);
00054 }
00055
00056 void c634_hide(int fd)
00057 {
00058 char b = 2;
00059 write(fd, &b, 1);
00060 }
00061
00062 void c634_restore(int fd)
00063 {
00064 char b = 3;
00065 write(fd, &b, 1);
00066 }
00067
00068 void c634_cursor(int fd, int type)
00069 {
00070 char b = type == k_c634_cursorHide ? 4 : type == k_c634_cursorUnderline ? 5
00071 : type == k_c634_cursorInverting ? 7 : 6;
00072 write(fd, &b, 1);
00073 }
00074
00075 void c634_backspace(int fd)
00076 {
00077 char b = 8;
00078 write(fd, &b, 1);
00079 }
00080
00081 void c634_clear(int fd)
00082 {
00083 char b = 12;
00084 write(fd, &b, 1);
00085 }
00086
00087 void c634_cr(int fd)
00088 {
00089 char b = 13;
00090 write(fd, &b, 1);
00091 }
00092
00093 void c634_lf(int fd)
00094 {
00095 char b = 10;
00096 write(fd, &b, 1);
00097 }
00098
00099 void c634_backlight(int fd, int v)
00100 {
00101 char b[2] = {14};
00102 b[1] = v;
00103 write(fd, b, 2);
00104 }
00105
00106 void c634_contrast(int fd, int v)
00107 {
00108 char b[2] = {15};
00109 b[1] = v;
00110 write(fd, b, 2);
00111 }
00112
00113 void c634_gotoxy(int fd, int x, int y)
00114 {
00115 char b[3] = {17};
00116 b[1] = x;
00117 b[2] = y;
00118 write(fd, b, 3);
00119 }
00120
00121 void c634_scrollMode(int fd, int m)
00122 {
00123 char b = m ? 19 : 20;
00124 write(fd, &b, 1);
00125 }
00126
00127 void c634_wrapMode(int fd, int m)
00128 {
00129 char b = m ? 23 : 24;
00130 write(fd, &b, 1);
00131 }
00132
00133 void c634_defineChar(int fd, char ch, char bm[8])
00134 {
00135 char b[2] = {25};
00136 b[1] = ch - 128;
00137 write(fd, b, 2);
00138 write(fd, bm, 8);
00139 }
00140
00141 void c634_reboot(int fd)
00142 {
00143 char b = 26;
00144 write(fd, &b, 1);
00145 }
00146
00147 void c634_printf(int fd, char const *format, ...)
00148 {
00149 char *str, *str1;
00150 int i, j, len;
00151 va_list ap;
00152
00153 str = malloc(1024);
00154 if (str)
00155 {
00156 va_start(ap, format);
00157 vsnprintf(str, 1024, format, ap);
00158 va_end(ap);
00159 len = strlen(str);
00160 for (i = 0; i < len; i += j + 1)
00161 {
00162 str1 = strchr(str + i, '\n');
00163 j = str1 ? str1 - str : len - i;
00164 write(fd, str + i, j);
00165 if (i + j < len)
00166 write(fd, "\r\n", 2);
00167 }
00168 free(str);
00169 }
00170 }