lcd_app.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <stdint.h>
  8. #define LCD_DEVICE "/dev/lcd2002"
  9. /* IOCTL command definition - must be consistent with the driver */
  10. #define LCD_IOCTL_MAGIC 'L'
  11. #define LCD_IOCTL_CLEAR _IO(LCD_IOCTL_MAGIC, 0)
  12. #define LCD_IOCTL_HOME _IO(LCD_IOCTL_MAGIC, 1)
  13. #define LCD_IOCTL_SET_CURSOR _IOW(LCD_IOCTL_MAGIC, 2, struct lcd_pos)
  14. #define LCD_IOCTL_DISPLAY_CTRL _IOW(LCD_IOCTL_MAGIC, 3, unsigned char[3])
  15. struct lcd_pos {
  16. unsigned char col;
  17. unsigned char row;
  18. };
  19. /* Display control parameters */
  20. #define DISPLAY_ON 0x04
  21. #define DISPLAY_OFF 0x00
  22. #define CURSOR_ON 0x02
  23. #define CURSOR_OFF 0x00
  24. #define BLINK_ON 0x01
  25. #define BLINK_OFF 0x00
  26. int fd;
  27. void test_basic_display(void)
  28. {
  29. printf("Test 1: Basic Display\n");
  30. ioctl(fd, LCD_IOCTL_CLEAR);
  31. usleep(100000);
  32. write(fd, "Hello, Intel ADL!", 17);
  33. sleep(2);
  34. /* write to the second line */
  35. struct lcd_pos pos = {0, 1};
  36. ioctl(fd, LCD_IOCTL_SET_CURSOR, &pos);
  37. write(fd, "N97 Platform", 12);
  38. sleep(2);
  39. }
  40. void test_cursor_and_blink(void)
  41. {
  42. printf("Test 2: Cursor and Blink\n");
  43. ioctl(fd, LCD_IOCTL_CLEAR);
  44. write(fd, "Cursor Test:", 12);
  45. struct lcd_pos pos = {0, 1};
  46. ioctl(fd, LCD_IOCTL_SET_CURSOR, &pos);
  47. write(fd, "See me?", 7);
  48. /* Turn on the cursor */
  49. unsigned char ctrl[3] = {DISPLAY_ON, CURSOR_ON, BLINK_OFF};
  50. ioctl(fd, LCD_IOCTL_DISPLAY_CTRL, ctrl);
  51. sleep(2);
  52. /* Turn on the flashing */
  53. ctrl[2] = BLINK_ON;
  54. ioctl(fd, LCD_IOCTL_DISPLAY_CTRL, ctrl);
  55. sleep(2);
  56. /* return to normal*/
  57. ctrl[1] = CURSOR_OFF;
  58. ctrl[2] = BLINK_OFF;
  59. ioctl(fd, LCD_IOCTL_DISPLAY_CTRL, ctrl);
  60. }
  61. void test_long_text_wrap(void)
  62. {
  63. printf("Test 3: Text Wrapping\n");
  64. ioctl(fd, LCD_IOCTL_CLEAR);
  65. /* Write more than 20 characters to test automatic line wrapping */
  66. write(fd, "This is a long text that should wrap to next line automatically", 61);
  67. sleep(3);
  68. }
  69. void test_special_chars(void)
  70. {
  71. printf("Test 4: Special Characters\n");
  72. ioctl(fd, LCD_IOCTL_CLEAR);
  73. write(fd, "Line1\nLine2", 11); /* \nShould wrap */
  74. sleep(2);
  75. ioctl(fd, LCD_IOCTL_CLEAR);
  76. write(fd, "Test\rOverwrite", 14); /* \rshould press Enter */
  77. sleep(2);
  78. }
  79. void test_full_display(void)
  80. {
  81. printf("Test 5: Full 20x2 Display\n");
  82. ioctl(fd, LCD_IOCTL_CLEAR);
  83. char line1[21], line2[21];
  84. int i;
  85. /* the first line of numbers */
  86. for (i = 0; i < 20; i++)
  87. line1[i] = '0' + (i % 10);
  88. line1[20] = '\0';
  89. /* The second letter */
  90. for (i = 0; i < 20; i++)
  91. line2[i] = 'A' + (i % 26);
  92. line2[20] = '\0';
  93. write(fd, line1, 20);
  94. struct lcd_pos pos = {0, 1};
  95. ioctl(fd, LCD_IOCTL_SET_CURSOR, &pos);
  96. write(fd, line2, 20);
  97. sleep(3);
  98. }
  99. void test_clear_and_home(void)
  100. {
  101. printf("Test 6: Clear and Home\n");
  102. ioctl(fd, LCD_IOCTL_CLEAR);
  103. write(fd, "Clear in 2 sec...", 17);
  104. sleep(2);
  105. ioctl(fd, LCD_IOCTL_CLEAR);
  106. write(fd, "Cleared!", 8);
  107. sleep(1);
  108. ioctl(fd, LCD_IOCTL_HOME);
  109. sleep(1);
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. printf("LCD2002 Test Program\n");
  114. printf("Opening %s...\n", LCD_DEVICE);
  115. fd = open(LCD_DEVICE, O_WRONLY);
  116. if (fd < 0) {
  117. perror("Failed to open LCD device");
  118. printf("Please check: sudo modprobe lcd2002_drv\n");
  119. printf("Or: sudo mknod /dev/lcd2002 c <major> <minor>\n");
  120. return 1;
  121. }
  122. printf("LCD opened successfully\n\n");
  123. /* Run Test */
  124. test_basic_display();
  125. printf("\n");
  126. test_cursor_and_blink();
  127. printf("\n");
  128. test_long_text_wrap();
  129. printf("\n");
  130. test_special_chars();
  131. printf("\n");
  132. test_full_display();
  133. printf("\n");
  134. test_clear_and_home();
  135. printf("\n");
  136. /* End Display */
  137. ioctl(fd, LCD_IOCTL_CLEAR);
  138. write(fd, "Test Complete!", 14);
  139. struct lcd_pos pos = {4, 1};
  140. ioctl(fd, LCD_IOCTL_SET_CURSOR, &pos);
  141. write(fd, "Goodbye!", 8);
  142. sleep(2);
  143. ioctl(fd, LCD_IOCTL_CLEAR);
  144. close(fd);
  145. printf("All tests completed.\n");
  146. return 0;
  147. }