cfag12864b-example.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Filename: cfag12864b-example.c
  3. * Version: 0.1.0
  4. * Description: cfag12864b LCD userspace example program
  5. * License: GPLv2
  6. *
  7. * Author: Copyright (C) Miguel Ojeda Sandonis
  8. * Date: 2006-10-31
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * ------------------------
  26. * start of cfag12864b code
  27. * ------------------------
  28. */
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/mman.h>
  35. #define CFAG12864B_WIDTH (128)
  36. #define CFAG12864B_HEIGHT (64)
  37. #define CFAG12864B_SIZE (128 * 64 / 8)
  38. #define CFAG12864B_BPB (8)
  39. #define CFAG12864B_ADDRESS(x, y) ((y) * CFAG12864B_WIDTH / \
  40. CFAG12864B_BPB + (x) / CFAG12864B_BPB)
  41. #define CFAG12864B_BIT(n) (((unsigned char) 1) << (n))
  42. #undef CFAG12864B_DOCHECK
  43. #ifdef CFAG12864B_DOCHECK
  44. #define CFAG12864B_CHECK(x, y) ((x) < CFAG12864B_WIDTH && \
  45. (y) < CFAG12864B_HEIGHT)
  46. #else
  47. #define CFAG12864B_CHECK(x, y) (1)
  48. #endif
  49. int cfag12864b_fd;
  50. unsigned char * cfag12864b_mem;
  51. unsigned char cfag12864b_buffer[CFAG12864B_SIZE];
  52. /*
  53. * init a cfag12864b framebuffer device
  54. *
  55. * No error: return = 0
  56. * Unable to open: return = -1
  57. * Unable to mmap: return = -2
  58. */
  59. static int cfag12864b_init(char *path)
  60. {
  61. cfag12864b_fd = open(path, O_RDWR);
  62. if (cfag12864b_fd == -1)
  63. return -1;
  64. cfag12864b_mem = mmap(0, CFAG12864B_SIZE, PROT_READ | PROT_WRITE,
  65. MAP_SHARED, cfag12864b_fd, 0);
  66. if (cfag12864b_mem == MAP_FAILED) {
  67. close(cfag12864b_fd);
  68. return -2;
  69. }
  70. return 0;
  71. }
  72. /*
  73. * exit a cfag12864b framebuffer device
  74. */
  75. static void cfag12864b_exit(void)
  76. {
  77. munmap(cfag12864b_mem, CFAG12864B_SIZE);
  78. close(cfag12864b_fd);
  79. }
  80. /*
  81. * set (x, y) pixel
  82. */
  83. static void cfag12864b_set(unsigned char x, unsigned char y)
  84. {
  85. if (CFAG12864B_CHECK(x, y))
  86. cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] |=
  87. CFAG12864B_BIT(x % CFAG12864B_BPB);
  88. }
  89. /*
  90. * unset (x, y) pixel
  91. */
  92. static void cfag12864b_unset(unsigned char x, unsigned char y)
  93. {
  94. if (CFAG12864B_CHECK(x, y))
  95. cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &=
  96. ~CFAG12864B_BIT(x % CFAG12864B_BPB);
  97. }
  98. /*
  99. * is set (x, y) pixel?
  100. *
  101. * Pixel off: return = 0
  102. * Pixel on: return = 1
  103. */
  104. static unsigned char cfag12864b_isset(unsigned char x, unsigned char y)
  105. {
  106. if (CFAG12864B_CHECK(x, y))
  107. if (cfag12864b_buffer[CFAG12864B_ADDRESS(x, y)] &
  108. CFAG12864B_BIT(x % CFAG12864B_BPB))
  109. return 1;
  110. return 0;
  111. }
  112. /*
  113. * not (x, y) pixel
  114. */
  115. static void cfag12864b_not(unsigned char x, unsigned char y)
  116. {
  117. if (cfag12864b_isset(x, y))
  118. cfag12864b_unset(x, y);
  119. else
  120. cfag12864b_set(x, y);
  121. }
  122. /*
  123. * fill (set all pixels)
  124. */
  125. static void cfag12864b_fill(void)
  126. {
  127. unsigned short i;
  128. for (i = 0; i < CFAG12864B_SIZE; i++)
  129. cfag12864b_buffer[i] = 0xFF;
  130. }
  131. /*
  132. * clear (unset all pixels)
  133. */
  134. static void cfag12864b_clear(void)
  135. {
  136. unsigned short i;
  137. for (i = 0; i < CFAG12864B_SIZE; i++)
  138. cfag12864b_buffer[i] = 0;
  139. }
  140. /*
  141. * format a [128*64] matrix
  142. *
  143. * Pixel off: src[i] = 0
  144. * Pixel on: src[i] > 0
  145. */
  146. static void cfag12864b_format(unsigned char * matrix)
  147. {
  148. unsigned char i, j, n;
  149. for (i = 0; i < CFAG12864B_HEIGHT; i++)
  150. for (j = 0; j < CFAG12864B_WIDTH / CFAG12864B_BPB; j++) {
  151. cfag12864b_buffer[i * CFAG12864B_WIDTH / CFAG12864B_BPB +
  152. j] = 0;
  153. for (n = 0; n < CFAG12864B_BPB; n++)
  154. if (matrix[i * CFAG12864B_WIDTH +
  155. j * CFAG12864B_BPB + n])
  156. cfag12864b_buffer[i * CFAG12864B_WIDTH /
  157. CFAG12864B_BPB + j] |=
  158. CFAG12864B_BIT(n);
  159. }
  160. }
  161. /*
  162. * blit buffer to lcd
  163. */
  164. static void cfag12864b_blit(void)
  165. {
  166. memcpy(cfag12864b_mem, cfag12864b_buffer, CFAG12864B_SIZE);
  167. }
  168. /*
  169. * ----------------------
  170. * end of cfag12864b code
  171. * ----------------------
  172. */
  173. #include <stdio.h>
  174. #define EXAMPLES 6
  175. static void example(unsigned char n)
  176. {
  177. unsigned short i, j;
  178. unsigned char matrix[CFAG12864B_WIDTH * CFAG12864B_HEIGHT];
  179. if (n > EXAMPLES)
  180. return;
  181. printf("Example %i/%i - ", n, EXAMPLES);
  182. switch (n) {
  183. case 1:
  184. printf("Draw points setting bits");
  185. cfag12864b_clear();
  186. for (i = 0; i < CFAG12864B_WIDTH; i += 2)
  187. for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
  188. cfag12864b_set(i, j);
  189. break;
  190. case 2:
  191. printf("Clear the LCD");
  192. cfag12864b_clear();
  193. break;
  194. case 3:
  195. printf("Draw rows formatting a [128*64] matrix");
  196. memset(matrix, 0, CFAG12864B_WIDTH * CFAG12864B_HEIGHT);
  197. for (i = 0; i < CFAG12864B_WIDTH; i++)
  198. for (j = 0; j < CFAG12864B_HEIGHT; j += 2)
  199. matrix[j * CFAG12864B_WIDTH + i] = 1;
  200. cfag12864b_format(matrix);
  201. break;
  202. case 4:
  203. printf("Fill the lcd");
  204. cfag12864b_fill();
  205. break;
  206. case 5:
  207. printf("Draw columns unsetting bits");
  208. for (i = 0; i < CFAG12864B_WIDTH; i += 2)
  209. for (j = 0; j < CFAG12864B_HEIGHT; j++)
  210. cfag12864b_unset(i, j);
  211. break;
  212. case 6:
  213. printf("Do negative not-ing all bits");
  214. for (i = 0; i < CFAG12864B_WIDTH; i++)
  215. for (j = 0; j < CFAG12864B_HEIGHT; j ++)
  216. cfag12864b_not(i, j);
  217. break;
  218. }
  219. puts(" - [Press Enter]");
  220. }
  221. int main(int argc, char *argv[])
  222. {
  223. unsigned char n;
  224. if (argc != 2) {
  225. printf(
  226. "Sintax: %s fbdev\n"
  227. "Usually: /dev/fb0, /dev/fb1...\n", argv[0]);
  228. return -1;
  229. }
  230. if (cfag12864b_init(argv[1])) {
  231. printf("Can't init %s fbdev\n", argv[1]);
  232. return -2;
  233. }
  234. for (n = 1; n <= EXAMPLES; n++) {
  235. example(n);
  236. cfag12864b_blit();
  237. while (getchar() != '\n');
  238. }
  239. cfag12864b_exit();
  240. return 0;
  241. }