Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "cube.h"
- #include <termios.h>
- #include <fcntl.h>
- #include <strings.h>
- #include <windows.h> // windows
- void cube_push (unsigned char data[8][8])
- {
- int x,y,i;
- i= 0;
- unsigned char buffer[200];
- buffer[i++] = 0xff; // escape
- buffer[i++] = 0x00; // reset to 0,0
- for (x=0;x<8;x++)
- {
- for (y=0;y<8;y++)
- {
- buffer[i++] = data[x][y];
- if (data[x][y] == 0xff)
- {
- buffer[i++] = data[x][y];
- }
- }
- }
- // windows
- HANDLE hSerial;
- PDWORD dwBytesWrite = 0;
- if(!WriteFile(hSerial, &buffer, i, dwBytesWrite, NULL)){
- //error occurred. Report to user.
- }
- //write(tty,&buffer,i); // linux
- }
- int cube_init (void)
- {
- // windows
- // opening port 3
- HANDLE hSerial;
- hSerial = CreateFile("COM3",
- GENERIC_READ | GENERIC_WRITE,
- 0,
- 0,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- 0);
- if(hSerial==INVALID_HANDLE_VALUE){
- if(GetLastError()==ERROR_FILE_NOT_FOUND){
- //serial port does not exist. Inform user.
- }
- //some other error occurred. Inform user.
- }
- // setting parameters
- DCB dcbSerialParams = {0};
- dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
- if (!GetCommState(hSerial, &dcbSerialParams)) {
- //error getting state
- }
- dcbSerialParams.BaudRate=CBR_38400;
- dcbSerialParams.ByteSize=8;
- dcbSerialParams.StopBits=ONESTOPBIT;
- dcbSerialParams.Parity=NOPARITY;
- if(!SetCommState(hSerial, &dcbSerialParams)){
- //error setting serial port state
- }
- /* linux
- //FILE *ftty;
- //ftty = fopen("/dev/ttyUSB0","a");
- struct termios io;
- char *tty_path = "/dev/ttyUSB0";
- //tty = open(tty_path, O_RDWR | O_NOCTTY | O_NDELAY); // <- ORIGINAL
- tty = open(tty_path, O_RDWR);
- if (tty <0) {perror(tty_path); exit(-1); }
- bzero(&io, sizeof(io));
- //io.c_cflag = B2400 | CRTSCTS | CS8 | CLOCAL | CREAD;
- //io.c_cflag = B2400 | CRTSCTS | PARENB | CS8 | CLOCAL | CREAD;
- io.c_cflag = B38400 | PARENB | CS8 | CLOCAL | CREAD;
- io.c_iflag = IGNPAR;
- io.c_oflag = 0;
- // set input mode (non-canonical, no echo,...)
- io.c_lflag &= ~OPOST;
- //io.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- io.c_cc[VTIME] = 0; // inter-character timer unused
- io.c_cc[VMIN] = 0; // blocking read until 5 chars received
- // Flush buffer
- tcflush(tty, TCIFLUSH);
- // write config to tty
- tcsetattr(tty,TCSANOW,&io);
- //fcntl(tty, F_SETFL, 0);
- */
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement