3 #include "uartconnect.hpp"
16 #include <sys/socket.h>
17 #include <bluetooth/bluetooth.h>
18 #include <bluetooth/rfcomm.h>
25 if (!strncmp(name,
"/dev/tty", 8))
27 fd = open (name, O_RDWR | O_NOCTTY | O_SYNC);
30 printf (
"error %d opening %s: %s", errno, name, strerror (errno));
34 memset (&tty, 0,
sizeof tty);
35 if (tcgetattr (fd, &tty) != 0)
37 printf (
"error %d from tcgetattr", errno);
43 cfsetospeed (&tty, 9600);
44 cfsetispeed (&tty, 9600);
46 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
49 tty.c_iflag &= ~IGNBRK;
56 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
58 tty.c_cflag |= (CLOCAL | CREAD);
60 tty.c_cflag &= ~(PARENB | PARODD);
62 tty.c_cflag &= ~CSTOPB;
63 tty.c_cflag &= ~CRTSCTS;
65 if (tcsetattr (fd, TCSANOW, &tty) != 0)
67 printf (
"error %d from tcsetattr", errno);
70 memset (&tty, 0,
sizeof tty);
71 if (tcgetattr (fd, &tty) != 0)
73 printf (
"error %d from tggetattr", errno);
80 if (tcsetattr (fd, TCSANOW, &tty) != 0)
81 printf (
"error %d setting term attributes", errno);
85 struct sockaddr_rc addr = { 0 };
86 addr.rc_family = AF_BLUETOOTH;
87 addr.rc_channel = (uint8_t) 1;
88 str2ba(name, &addr.rc_bdaddr);
89 fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
90 connect(fd, (
struct sockaddr *)&addr,
sizeof(addr));
93 bool uartwrite(
const void* buf,
size_t count)
95 return write (fd, buf, count) == (ssize_t)count;
99 std::thread th(&PcUart::reader,
this);
106 if (read(fd, (
void*)&c, 1) == 1)
109 virtual void uartOnChar(uint8_t c) = 0;
121 CommMaster(
const char* portname,
unsigned msTimeout,
unsigned retries) :
122 PcUart(portname), sequence(0), sequenceAck(0), sndBuffer(msgBuffer+2), msTimeout(msTimeout), retries(retries)
128 return sequence == sequenceAck;
131 virtual void addPoints(
const uint8_t* xys,
unsigned points)
135 points = points<=16 ? points : 16;
136 for (
unsigned i=0; i<2*points; i++) {
137 sndBuffer[i] = xys[i];
139 sendCommand(Command::kAddPoints, points);
141 virtual void deletePoints() { sendCommand(Command::kDeletePoints); }
142 virtual void stopMove() { sendCommand(Command::kStopMove); }
143 virtual void startMove() { sendCommand(Command::kStartMove); }
144 virtual void setParam() { sendCommand(Command::kSetParam); }
145 virtual void gotoXY(PosType x, PosType y)
147 writePosType(sndBuffer, x, 0);
148 writePosType(sndBuffer, y, 1);
149 sendCommand(Command::kGotoXY);
151 virtual void movedXdY(PosType dx, PosType dy)
153 writePosType(sndBuffer, dx, 0);
154 writePosType(sndBuffer, dy, 1);
155 sendCommand(Command::kMovedXdY);
157 virtual void gotoNextPoint() { sendCommand(Command::kGotoNextPoint); }
158 virtual void setNPoint(uint16_t nPoint)
160 sndBuffer[0] = nPoint>>8;
161 sndBuffer[1] = nPoint&0xff;
162 sendCommand(Command::kSetNPoint);
164 virtual void setPos(PosType x, PosType y)
166 writePosType(sndBuffer, x, 0);
167 writePosType(sndBuffer, y, 1);
168 sendCommand(Command::kSetPos);
171 void sendCommand(Command cmd, uint8_t len=1)
173 msgBuffer[0] = ++sequence;
174 len = len ? (len<=16 ? len-1 : 15) : 0;
175 msgBuffer[1] = (len<<4) | (uint8_t)cmd;
181 for (tries=0; (tries<retries) && (sequence != sequenceAck); tries++)
183 for (ms=0; (ms<msTimeout) && (sequence != sequenceAck); ms++)
186 dt.tv_nsec = 1000000;
187 while(nanosleep(&dt,&dt)<0 && errno==EINTR);
195 virtual void onMsg(uint8_t* buffer)
198 sequenceAck = pInfo->sequence;
199 memcpy(&info, pInfo,
sizeof(info));
200 printf(
"Seq %u, status %u, spaceSize %u, freeSpace %u, pos (%d,%d), nPoint %u\n",
201 info.sequence, info.status, info.spaceSize, info.freeSpace, info.posX, info.posY, info.nPoint);
203 virtual void send(
const uint8_t* buffer,
unsigned len)
205 uartwrite(buffer, len);
207 virtual void uartOnChar(uint8_t c)
216 uint8_t msgBuffer[kPacketSize-3];
Definition: master.hpp:20
Definition: master.hpp:118
Definition: uartconnect.hpp:6