Advertisement
joric

cppgrabber.cpp

Jun 25th, 2016
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. // Arduino Pro Micro vs OV7670, Serial Port Viewer
  2. // Discussion: http://forum.arduino.cc/index.php?topic=159557.msg2815774#msg2815774
  3. // Album: http://imgur.com/a/nurwJ
  4. // Last updated version at http://pastebin.com/p4BZniNV
  5. // this one works exactly as FrameGrabber from http://privateblog.info
  6.  
  7. #include <windows.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10.  
  11. #pragma comment(lib,"gdi32")
  12. #pragma comment(lib,"user32")
  13.  
  14. #define WINDOW_WIDTH 640
  15. #define WINDOW_HEIGHT 480
  16.  
  17. #define BMP_WIDTH 320
  18. #define BMP_HEIGHT 240
  19.  
  20. #define CLASSNAME "Serialplex"
  21.  
  22. #define SERIAL_DEVICE "\\\\.\\COM5"
  23.  
  24. HINSTANCE g_hInstance;
  25. HBITMAP g_hBitmap;
  26. HDC g_hDC;
  27. void *g_pBits = NULL;
  28.  
  29. #define pthread_t DWORD
  30. DWORD thread_handle;
  31.  
  32. void CreateBmp(HWND hWnd)
  33. {
  34.     BITMAPINFO binfo;
  35.     HDC hdc = GetDC(hWnd);
  36.     binfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  37.     binfo.bmiHeader.biWidth = BMP_WIDTH;
  38.     binfo.bmiHeader.biHeight = BMP_HEIGHT;
  39.     binfo.bmiHeader.biPlanes = 1;
  40.     binfo.bmiHeader.biBitCount = 32;
  41.     binfo.bmiHeader.biCompression = 0;
  42.     binfo.bmiHeader.biSizeImage = BMP_WIDTH * BMP_HEIGHT * 4;
  43.     g_hBitmap =
  44.         CreateDIBSection(hdc, &binfo, DIB_RGB_COLORS, &g_pBits, NULL,
  45.                  NULL);
  46.     g_hDC = CreateCompatibleDC(hdc);
  47.     SelectObject(g_hDC, g_hBitmap);
  48.     ReleaseDC(hWnd, hdc);
  49. }
  50.  
  51.  
  52. HANDLE hSerial;
  53.  
  54. int InitSerial() {
  55.  
  56.     DCB dcbSerialParams = {0};
  57.     COMMTIMEOUTS timeouts = {0};
  58.  
  59.     // Open the highest available serial port number
  60.     fprintf(stderr, "Opening serial port...");
  61.     hSerial = CreateFile(
  62.                 SERIAL_DEVICE, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  63.                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  64.     if (hSerial == INVALID_HANDLE_VALUE)
  65.     {
  66.             fprintf(stderr, "Error\n");
  67.             return 1;
  68.     }
  69.     else fprintf(stderr, "OK\n");
  70.  
  71.     // Set device parameters (38400 baud, 1 start bit,
  72.     // 1 stop bit, no parity)
  73.     dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  74.     if (GetCommState(hSerial, &dcbSerialParams) == 0)
  75.     {
  76.         fprintf(stderr, "Error getting device state\n");
  77.         CloseHandle(hSerial);
  78.         return 1;
  79.     }
  80.  
  81.     dcbSerialParams.BaudRate = 1000000;
  82.     dcbSerialParams.ByteSize = 8;
  83.     dcbSerialParams.StopBits = ONESTOPBIT;
  84.     dcbSerialParams.Parity = NOPARITY;
  85.     if(SetCommState(hSerial, &dcbSerialParams) == 0)
  86.     {
  87.         fprintf(stderr, "Error setting device parameters\n");
  88.         CloseHandle(hSerial);
  89.         return 1;
  90.     }
  91.  
  92.     // Set COM port timeout settings
  93.     timeouts.ReadIntervalTimeout = 50;
  94.     timeouts.ReadTotalTimeoutConstant = 50;
  95.     timeouts.ReadTotalTimeoutMultiplier = 10;
  96.     timeouts.WriteTotalTimeoutConstant = 50;
  97.     timeouts.WriteTotalTimeoutMultiplier = 10;
  98.     if(SetCommTimeouts(hSerial, &timeouts) == 0)
  99.     {
  100.         fprintf(stderr, "Error setting timeouts\n");
  101.         CloseHandle(hSerial);
  102.         return 1;
  103.     }
  104.  
  105. }
  106.  
  107. unsigned int GreyscaleRGB(unsigned int c) {
  108.     return (c<<16) | (c<<8) | c;
  109. }
  110.  
  111. int thread_func() {
  112.  
  113.     int iOffset = 0;
  114.     DWORD bytes_total = 0;
  115.     DWORD bytes_read = 0;
  116.     const int bufsize = 4800;
  117.     unsigned char buf[bufsize];
  118.  
  119.     for (;;) {
  120.  
  121.         ReadFile(hSerial, buf, bufsize, &bytes_read, NULL);
  122.  
  123.         bytes_total += bytes_read;
  124.  
  125.         if (bytes_read!=0)
  126.             fprintf(stdout, "bytes read: %d, bytes total: %d\n", bytes_read, bytes_total);
  127.  
  128.         if (bytes_read==5) {
  129.             bytes_total = 0;
  130.             iOffset = 0;
  131.         }
  132.  
  133.         if (bytes_read!=5) for (DWORD i=0; i<bytes_read; i++) {
  134.             iOffset = iOffset % (BMP_WIDTH*BMP_HEIGHT);
  135.             (unsigned long) *((unsigned long *) g_pBits + iOffset) = GreyscaleRGB(buf[i]);
  136.             iOffset++;
  137.         }
  138.  
  139.     }
  140.  
  141.     fprintf(stderr, "exiting thread\n");
  142.  
  143.     return 0;
  144. }
  145.  
  146. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  147. {
  148.     PAINTSTRUCT ps;
  149.     HDC hdc;
  150.     RECT rect;
  151.     switch (message) {
  152.  
  153.     case WM_PAINT:
  154.         hdc = BeginPaint(hWnd, &ps);
  155.         //Function2();
  156.         GetClientRect(hWnd, &rect);
  157.         SetStretchBltMode(hdc, HALFTONE);
  158.         StretchBlt(hdc, 0, 0, rect.right, rect.bottom, g_hDC, 0,
  159.                BMP_HEIGHT - 1, BMP_WIDTH, -BMP_HEIGHT, SRCCOPY);
  160.         EndPaint(hWnd, &ps);
  161.         break;
  162.     case WM_KEYDOWN:
  163.         if (wParam == VK_ESCAPE)
  164.             SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);
  165.         break;
  166.     case WM_CREATE:
  167.         CreateBmp(hWnd);
  168.         break;
  169.     case WM_TIMER:
  170.         InvalidateRect(hWnd, NULL, FALSE);
  171.         break;
  172.     case WM_DESTROY:
  173.         PostQuitMessage(0);
  174.         break;
  175.  
  176.     default:
  177.         return DefWindowProc(hWnd, message, wParam, lParam);
  178.     }
  179.  
  180.     return 0;
  181. }
  182.  
  183. int main() {
  184.     InitSerial();
  185.  
  186.     CreateThread((LPSECURITY_ATTRIBUTES) NULL, 0, (LPTHREAD_START_ROUTINE) thread_func, 0, 0, &thread_handle);
  187.  
  188.     MSG msg;
  189.     HWND hWnd;
  190.     WNDCLASS wc;
  191.  
  192.     g_hInstance = GetModuleHandle(NULL);
  193.  
  194.     wc.style = CS_HREDRAW | CS_VREDRAW;
  195.     wc.lpfnWndProc = (WNDPROC) WndProc;
  196.     wc.hInstance = g_hInstance;
  197.     wc.hbrBackground = NULL;
  198.     wc.lpszClassName = CLASSNAME;
  199.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  200.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  201.     wc.lpszMenuName = NULL;
  202.     wc.cbClsExtra = 0;
  203.     wc.cbWndExtra = 0;
  204.  
  205.     RegisterClass(&wc);
  206.  
  207.     int w = WINDOW_WIDTH;
  208.     int h = WINDOW_HEIGHT;
  209.  
  210.     if (!(hWnd = CreateWindow(wc.lpszClassName, wc.lpszClassName,
  211.         WS_OVERLAPPEDWINDOW, 0, 0, w, h, NULL, NULL,
  212.         wc.hInstance, NULL))) return FALSE;
  213.  
  214.     RECT rc;
  215.  
  216.     SetRect(&rc, 0, 0, w, h);
  217.  
  218.     AdjustWindowRectEx(&rc, GetWindowLong(hWnd, GWL_STYLE),
  219.         GetMenu(hWnd) != NULL, GetWindowLong(hWnd, GWL_EXSTYLE));
  220.  
  221.     w = rc.right - rc.left;
  222.     h = rc.bottom - rc.top;
  223.  
  224.     SetWindowPos(hWnd, NULL, (GetSystemMetrics(SM_CXSCREEN) - w) / 2,
  225.         (GetSystemMetrics(SM_CYSCREEN) - h) / 2, w, h,
  226.         SWP_NOZORDER | SWP_NOACTIVATE);
  227.  
  228.     ShowWindow(hWnd, TRUE);
  229.     UpdateWindow(hWnd);
  230.  
  231.     SetTimer(hWnd, 0, 1, NULL);
  232.     while (GetMessage(&msg, NULL, 0, 0)) {
  233.         TranslateMessage(&msg);
  234.         DispatchMessage(&msg);
  235.     }
  236.     return (int) msg.wParam;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement