cfag12864b-example.c 5.2 KB

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