Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino Pro Micro vs OV7670, Serial Port Viewer
- // Discussion: http://forum.arduino.cc/index.php?topic=159557.msg2815774#msg2815774
- // Album: http://imgur.com/a/nurwJ
- // Last updated version at http://pastebin.com/p4BZniNV
- // this one works exactly as FrameGrabber from http://privateblog.info
- #include <windows.h>
- #include <math.h>
- #include <stdio.h>
- #pragma comment(lib,"gdi32")
- #pragma comment(lib,"user32")
- #define WINDOW_WIDTH 640
- #define WINDOW_HEIGHT 480
- #define BMP_WIDTH 320
- #define BMP_HEIGHT 240
- #define CLASSNAME "Serialplex"
- #define SERIAL_DEVICE "\\\\.\\COM5"
- HINSTANCE g_hInstance;
- HBITMAP g_hBitmap;
- HDC g_hDC;
- void *g_pBits = NULL;
- #define pthread_t DWORD
- DWORD thread_handle;
- void CreateBmp(HWND hWnd)
- {
- BITMAPINFO binfo;
- HDC hdc = GetDC(hWnd);
- binfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- binfo.bmiHeader.biWidth = BMP_WIDTH;
- binfo.bmiHeader.biHeight = BMP_HEIGHT;
- binfo.bmiHeader.biPlanes = 1;
- binfo.bmiHeader.biBitCount = 32;
- binfo.bmiHeader.biCompression = 0;
- binfo.bmiHeader.biSizeImage = BMP_WIDTH * BMP_HEIGHT * 4;
- g_hBitmap =
- CreateDIBSection(hdc, &binfo, DIB_RGB_COLORS, &g_pBits, NULL,
- NULL);
- g_hDC = CreateCompatibleDC(hdc);
- SelectObject(g_hDC, g_hBitmap);
- ReleaseDC(hWnd, hdc);
- }
- HANDLE hSerial;
- int InitSerial() {
- DCB dcbSerialParams = {0};
- COMMTIMEOUTS timeouts = {0};
- // Open the highest available serial port number
- fprintf(stderr, "Opening serial port...");
- hSerial = CreateFile(
- SERIAL_DEVICE, GENERIC_READ|GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
- if (hSerial == INVALID_HANDLE_VALUE)
- {
- fprintf(stderr, "Error\n");
- return 1;
- }
- else fprintf(stderr, "OK\n");
- // Set device parameters (38400 baud, 1 start bit,
- // 1 stop bit, no parity)
- dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
- if (GetCommState(hSerial, &dcbSerialParams) == 0)
- {
- fprintf(stderr, "Error getting device state\n");
- CloseHandle(hSerial);
- return 1;
- }
- dcbSerialParams.BaudRate = 1000000;
- dcbSerialParams.ByteSize = 8;
- dcbSerialParams.StopBits = ONESTOPBIT;
- dcbSerialParams.Parity = NOPARITY;
- if(SetCommState(hSerial, &dcbSerialParams) == 0)
- {
- fprintf(stderr, "Error setting device parameters\n");
- CloseHandle(hSerial);
- return 1;
- }
- // Set COM port timeout settings
- timeouts.ReadIntervalTimeout = 50;
- timeouts.ReadTotalTimeoutConstant = 50;
- timeouts.ReadTotalTimeoutMultiplier = 10;
- timeouts.WriteTotalTimeoutConstant = 50;
- timeouts.WriteTotalTimeoutMultiplier = 10;
- if(SetCommTimeouts(hSerial, &timeouts) == 0)
- {
- fprintf(stderr, "Error setting timeouts\n");
- CloseHandle(hSerial);
- return 1;
- }
- }
- unsigned int GreyscaleRGB(unsigned int c) {
- return (c<<16) | (c<<8) | c;
- }
- int thread_func() {
- int iOffset = 0;
- DWORD bytes_total = 0;
- DWORD bytes_read = 0;
- const int bufsize = 4800;
- unsigned char buf[bufsize];
- for (;;) {
- ReadFile(hSerial, buf, bufsize, &bytes_read, NULL);
- bytes_total += bytes_read;
- if (bytes_read!=0)
- fprintf(stdout, "bytes read: %d, bytes total: %d\n", bytes_read, bytes_total);
- if (bytes_read==5) {
- bytes_total = 0;
- iOffset = 0;
- }
- if (bytes_read!=5) for (DWORD i=0; i<bytes_read; i++) {
- iOffset = iOffset % (BMP_WIDTH*BMP_HEIGHT);
- (unsigned long) *((unsigned long *) g_pBits + iOffset) = GreyscaleRGB(buf[i]);
- iOffset++;
- }
- }
- fprintf(stderr, "exiting thread\n");
- return 0;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- RECT rect;
- switch (message) {
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- //Function2();
- GetClientRect(hWnd, &rect);
- SetStretchBltMode(hdc, HALFTONE);
- StretchBlt(hdc, 0, 0, rect.right, rect.bottom, g_hDC, 0,
- BMP_HEIGHT - 1, BMP_WIDTH, -BMP_HEIGHT, SRCCOPY);
- EndPaint(hWnd, &ps);
- break;
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);
- break;
- case WM_CREATE:
- CreateBmp(hWnd);
- break;
- case WM_TIMER:
- InvalidateRect(hWnd, NULL, FALSE);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- int main() {
- InitSerial();
- CreateThread((LPSECURITY_ATTRIBUTES) NULL, 0, (LPTHREAD_START_ROUTINE) thread_func, 0, 0, &thread_handle);
- MSG msg;
- HWND hWnd;
- WNDCLASS wc;
- g_hInstance = GetModuleHandle(NULL);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC) WndProc;
- wc.hInstance = g_hInstance;
- wc.hbrBackground = NULL;
- wc.lpszClassName = CLASSNAME;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.lpszMenuName = NULL;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- RegisterClass(&wc);
- int w = WINDOW_WIDTH;
- int h = WINDOW_HEIGHT;
- if (!(hWnd = CreateWindow(wc.lpszClassName, wc.lpszClassName,
- WS_OVERLAPPEDWINDOW, 0, 0, w, h, NULL, NULL,
- wc.hInstance, NULL))) return FALSE;
- RECT rc;
- SetRect(&rc, 0, 0, w, h);
- AdjustWindowRectEx(&rc, GetWindowLong(hWnd, GWL_STYLE),
- GetMenu(hWnd) != NULL, GetWindowLong(hWnd, GWL_EXSTYLE));
- w = rc.right - rc.left;
- h = rc.bottom - rc.top;
- SetWindowPos(hWnd, NULL, (GetSystemMetrics(SM_CXSCREEN) - w) / 2,
- (GetSystemMetrics(SM_CYSCREEN) - h) / 2, w, h,
- SWP_NOZORDER | SWP_NOACTIVATE);
- ShowWindow(hWnd, TRUE);
- UpdateWindow(hWnd);
- SetTimer(hWnd, 0, 1, NULL);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement