Advertisement
Guest User

abtest.c

a guest
Feb 3rd, 2017
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.61 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <fcntl.h>
  7. #include <linux/fb.h>
  8. #include <linux/kd.h>
  9. #include <sys/mman.h>
  10.  
  11. #include <errno.h>
  12. #include <wiringPi.h>
  13. #include <wiringSerial.h>
  14.  
  15. //------------------
  16. #include <stdbool.h>
  17. #include <termio.h>
  18.  
  19. #include "key.h"
  20.  
  21. static int stdin_fd = -1;
  22. static struct termios original;
  23. //------------------
  24.  
  25. int fd;
  26.  
  27. int paletteSelected = 0;
  28. unsigned long paletteArray[11][4] = { // {4294967295u, 0},{0, 4294967295u}
  29.  {0xE7DAE7DA, 0x8E0E8E0E, 0x334B334B, 0x08C408C4} // 1 BGB (lcd green)
  30. ,{0xEF5DEF5D, 0xA514A514, 0x5ACB5ACB, 0x10821082} // 2 grey
  31. ,{0xEFF9EFF9, 0xAEB2AEB2, 0x546E546E, 0x11671167} // 3 BGB 0.3
  32. ,{0xFF11FF11, 0xDD8BDD8B, 0x9BC79BC7, 0x49C349C3} // 4 no$gmb (brown)
  33. ,{0x05B005B0, 0x04CE04CE, 0x03490349, 0x02870287} // 5 gameboy light
  34. ,{0xEF76EF76, 0xBDE3BDE3, 0x6B606B60, 0x11C011C0} // 6 gameboy kiosk
  35. ,{0x568A568A, 0x45084508, 0x33863386, 0x22042204} // 7 KiGB (green)
  36. ,{0xFF8FFF8F, 0xB549B549, 0x6B466B46, 0x21022102} // 8 yellow
  37. ,{0xFE18FE18, 0xFB0CFB0C, 0xC000C000, 0x60006000} // 9 red
  38. ,{0xC61FC61F, 0x631F631F, 0x00180018, 0x000C000C} // 0 blue
  39. ,{0xFFFFFFFF, 0xB596B596, 0x6B4D6B4D, 0x00000000} // - greys high contrast
  40. };
  41.  
  42. // char bitMask[] = {128, 64, 32, 16, 8, 4, 2, 1};
  43. char bitMask[] = {1, 2, 4, 8, 16, 32, 64, 128};
  44.  
  45. // 'global' variables to store screen info
  46. char *fbp = 0;
  47. struct fb_var_screeninfo vinfo;
  48. struct fb_fix_screeninfo finfo;
  49.  
  50. //------------------
  51. bool keyPressed(int *character)
  52. {
  53.     // If this is the first time the function is called, change the stdin
  54.     // stream so that we get each character when the keys are pressed and
  55.     // and so that character aren't echoed to the screen when the keys are
  56.     // pressed.
  57.     if (stdin_fd == -1)
  58.     {
  59.         // Get the file descriptor associated with stdin stream.
  60.         stdin_fd = fileno(stdin);
  61.  
  62.         // Get the terminal (termios) attritubets for stdin so we can
  63.         // modify them and reset them before exiting the program.
  64.         tcgetattr(stdin_fd, &original);
  65.  
  66.         // Copy the termios attributes so we can modify them.
  67.         struct termios term;
  68.         memcpy(&term, &original, sizeof(term));
  69.  
  70.         // Unset ICANON and ECHO for stdin. When ICANON is not set the
  71.         // input is in noncanonical mode. In noncanonical mode input is
  72.         // available as each key is pressed. In canonical mode input is
  73.         // only available after the enter key is pressed. Unset ECHO so that
  74.         // the characters aren't echoed to the screen when keys are pressed.
  75.         // See the termios(3) man page for more information.
  76.         term.c_lflag &= ~(ICANON|ECHO);
  77.         tcsetattr(stdin_fd, TCSANOW, &term);
  78.  
  79.         // Turn off buffering for stdin. We want to get the characters
  80.         // immediately. We don't want the characters to be buffered.
  81.         setbuf(stdin, NULL);
  82.     }
  83.  
  84.     // Get the number of characters that are waiting to be read.
  85.     int characters_buffered = 0;
  86.     ioctl(stdin_fd, FIONREAD, &characters_buffered);
  87.  
  88.     // Set the return value to true if there are any characters to be read.
  89.     bool pressed = (characters_buffered != 0);
  90.  
  91.     if (characters_buffered == 1)
  92.     {
  93.         // There is only one character to be read. Read it in.
  94.         int c = fgetc(stdin);
  95.  
  96.         // Check if the caller wants the value of character read.
  97.         if (character != NULL)
  98.         {
  99.             *character = c;
  100.         }
  101.     }
  102.     else if (characters_buffered > 1)
  103.     {
  104.         // There is more than one character to be read. This can be key such
  105.         // as the arrow keys or function keys. This code just reads them in
  106.         // and ignores them. The caller will be informed that a key was
  107.         // pressed, but won't get a value for the key.
  108.         while (characters_buffered)
  109.         {
  110.             fgetc(stdin);
  111.             --characters_buffered;
  112.         }
  113.     }
  114.  
  115.     return pressed;
  116. }
  117.  
  118. void keyboardReset(void)
  119. {
  120.     if (stdin_fd != -1)
  121.     {
  122.         // If keyPressed() has been called the terminal input has been
  123.         // changed for the stdin stream. Put the attributes back the way
  124.         // we found them.
  125.         tcsetattr(stdin_fd, TCSANOW, &original);
  126.     }
  127. }
  128. //------------------
  129.  
  130. void draw()
  131. {
  132.   int b, p;
  133.   int counter = 0;
  134.   char tempByte;
  135.   unsigned int pix_offset = 0;
  136.  
  137.   for (b = 0; b < 1024; b++) // 128 x 64 / 8 = 1024
  138.   {
  139.     tempByte = serialGetchar(fd);
  140.  
  141.     for (p = 0; p < 8; p++) // 8 pixels per byte
  142.     {
  143.       if (tempByte & bitMask[p])
  144.       {
  145.         *((unsigned long*)(fbp + pix_offset)) = paletteArray[paletteSelected][0];
  146.         *((unsigned long*)(fbp + (pix_offset + 512))) = paletteArray[paletteSelected][0];
  147.       }
  148.       else
  149.       {
  150.         *((unsigned long*)(fbp + pix_offset)) = paletteArray[paletteSelected][3];
  151.         *((unsigned long*)(fbp + (pix_offset + 512))) = paletteArray[paletteSelected][3];
  152.       }
  153.  
  154.       pix_offset = pix_offset + 1024;
  155.  
  156.     }
  157.  
  158.     counter++;
  159.  
  160.     if (counter == 128)
  161.     {
  162.       pix_offset = pix_offset - 508;
  163.       counter = 0;
  164.     }
  165.     else
  166.     {
  167.       pix_offset = pix_offset - 8188;
  168.     }
  169.   }
  170. }
  171.  
  172. // application entry point
  173. int main(int argc, char* argv[])
  174. {
  175.  
  176.     int fbfd = 0;
  177.     struct fb_var_screeninfo orig_vinfo;
  178.     long int screensize = 0;
  179.  
  180.  
  181.     if ((fd = serialOpen ("/dev/ttyACM0", 115200)) < 0)
  182.     {
  183.       fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
  184.       return 1 ;
  185.     }
  186.     if (wiringPiSetup () == -1)
  187.     {
  188.       fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
  189.       return 1 ;
  190.     }
  191.  
  192.  
  193.     // Open the file for reading and writing
  194.     fbfd = open("/dev/fb0", O_RDWR);
  195.     if (fbfd == -1) {
  196.         printf("Error: cannot open framebuffer device.\n");
  197.         return(1);
  198.     }
  199.     printf("The framebuffer device was opened successfully.\n");
  200.  
  201.     // Get variable screen information
  202.     if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
  203.         printf("Error reading variable information.\n");
  204.     }
  205.     printf("Original %dx%d, %dbpp\n", vinfo.xres, vinfo.yres,
  206.        vinfo.bits_per_pixel );
  207.  
  208.     // Store for reset (copy vinfo to vinfo_orig)
  209.     memcpy(&orig_vinfo, &vinfo, sizeof(struct fb_var_screeninfo));
  210.  
  211.     // Change variable info - force 16 bit and 256 x 128 resolution
  212.     vinfo.bits_per_pixel = 16;
  213.     vinfo.xres = 256;
  214.     vinfo.yres = 128;
  215.     vinfo.xres_virtual = 256;
  216.     vinfo.yres_virtual = 128;
  217.     if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo)) {
  218.         printf("Error setting variable information.\n");
  219.     }
  220.    
  221.     // Get fixed screen information
  222.     if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
  223.         printf("Error reading fixed information.\n");
  224.     }
  225.  
  226.     // hide cursor
  227.     char *kbfds = "/dev/tty";
  228.     int kbfd = open(kbfds, O_WRONLY);
  229.     if (kbfd >= 0) {
  230.         ioctl(kbfd, KDSETMODE, KD_GRAPHICS);
  231.     }
  232.     else {
  233.         printf("Could not open %s.\n", kbfds);
  234.     }
  235.  
  236.     // map fb to user mem
  237.     screensize = finfo.smem_len;
  238.     fbp = (char*)mmap(0,
  239.               screensize,
  240.               PROT_READ | PROT_WRITE,
  241.               MAP_SHARED,
  242.               fbfd,
  243.               0);
  244.  
  245. //------------
  246.     int c = 0;
  247. //------------
  248.  
  249.     if ((int)fbp == -1) {
  250.         printf("Failed to mmap.\n");
  251.     }
  252.     else
  253.     {
  254.       // draw...
  255.       while (c != 27)
  256.       {
  257.         if (keyPressed(&c))
  258.         {
  259.           if      (c == 49) {paletteSelected = 0;}  // KEY_1
  260.           else if (c == 50) {paletteSelected = 1;}  // KEY_2
  261.           else if (c == 51) {paletteSelected = 2;}  // KEY_3
  262.           else if (c == 52) {paletteSelected = 3;}  // KEY_4
  263.           else if (c == 53) {paletteSelected = 4;}  // KEY_5
  264.           else if (c == 54) {paletteSelected = 5;}  // KEY_6
  265.           else if (c == 55) {paletteSelected = 6;}  // KEY_7
  266.           else if (c == 56) {paletteSelected = 7;}  // KEY_8
  267.           else if (c == 57) {paletteSelected = 8;}  // KEY_9
  268.           else if (c == 48) {paletteSelected = 9;}  // KEY_0
  269.           else if (c == 45) {paletteSelected = 10;} // KEY_MINUS
  270.         }
  271.  
  272.         if (serialDataAvail(fd)) {draw();}
  273.       }
  274.     }
  275.  
  276. //------------------
  277.     keyboardReset();
  278. //------------------
  279.  
  280.     // cleanup
  281.     // unmap fb file from memory
  282.     munmap(fbp, screensize);
  283.     // reset cursor
  284.     if (kbfd >= 0) {
  285.         ioctl(kbfd, KDSETMODE, KD_TEXT);
  286.     }
  287.     // reset the display mode
  288.     if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &orig_vinfo)) {
  289.         printf("Error re-setting variable information.\n");
  290.     }
  291.     // close fb file    
  292.     close(fbfd);
  293.  
  294.     return 0;
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement