openEmbroider  0.1
an open source embroidery software
comm.hpp
1 #pragma once
2 
3 #include "uart.hpp"
4 
5 // TODO master mutex 1 by 1 command
6 
7 // Master messages
8 // 1. Add points, n<=16+data
9 // 2. Delete points
10 // 3. Stop move
11 // 4. Start move
12 // 5. Set param, n<=16+data
13 // 6. Goto xy
14 // 7. Move dxdy
15 // 8. Goto nextpoint
16 // 9. Clear Npoint
17 // A. Set pos
18 
19 // Slave message
20 // Ack last message crc. Position. Npoint. Moving. StoredPoints. MemTotalPoints. 1+4+2+1+2+2 = 12
21 
22 class Comm : public UartBlock<37>
23 {
24 public:
25  typedef int16_t PosType;
26 protected:
27  enum class Command : uint8_t
28  {
29  kAddPoints = 0,
30  kDeletePoints,
31  kStopMove,
32  kStartMove,
33  kSetParam,
34  kGotoXY,
35  kMovedXdY,
36  kGotoNextPoint,
37  kSetNPoint,
38  kSetPos,
39  };
40  PosType readPosType(const uint8_t* buffer, unsigned index=0)
41  {
42  PosType pos;
43  const uint8_t* posBuffer = buffer+index*sizeof(PosType);
44  for (unsigned i=0; i<sizeof(PosType); i++)
45  ((uint8_t*)&pos)[i] = posBuffer[i];
46  return pos;
47  }
48  void writePosType(uint8_t* buffer, PosType pos, unsigned index=0)
49  {
50  uint8_t* posBuffer = buffer+index*sizeof(PosType);
51  for (unsigned i=0; i<sizeof(PosType); i++)
52  posBuffer[i] = ((uint8_t*)&pos)[i];
53  }
54  virtual void addPoints(const uint8_t* xys, unsigned points) = 0;
55  virtual void deletePoints() = 0;
56  virtual void stopMove() = 0;
57  virtual void startMove() = 0;
58  virtual void setParam() = 0;
59  virtual void gotoXY(PosType x, PosType y) = 0;
60  virtual void movedXdY(PosType dx, PosType dy) = 0;
61  virtual void gotoNextPoint() = 0;
62  virtual void setNPoint(uint16_t nPoint) = 0;
63  virtual void setPos(PosType x, PosType y) = 0;
64 };
65 
66 #pragma pack(push, 1)
67 struct SlaveInfo
68 {
69  uint8_t sequence;
70  uint16_t status;
71  uint16_t spaceSize;
72  uint16_t freeSpace;
73  int16_t posX;
74  int16_t posY;
75  uint16_t nPoint;
76 };
77 #pragma pack(pop)
78 
79 class CommSlave : public Comm
80 {
81 public:
82  static const uint16_t maskProtocol = 0xF000;
83  static const uint16_t maskButton = 0x0010;
84  static const uint16_t maskMoving = 0x0008;
85  static const uint16_t maskWaitingNeedle = 0x0004;
86  static const uint16_t maskRunning = 0x0002;
87  static const uint16_t maskAlive = 0x0001;
88 
89  static uint8_t getProtocol(uint16_t status)
90  {
91  return (status&maskProtocol)>>12;
92  }
93 
94  CommSlave()
95  {
96  sequence = 255;
97  }
98  virtual void onMsg(uint8_t* buffer)
99  {
100  sequence = buffer[0];
101  switch((Command)(buffer[1]&0xf))
102  {
103  case Command::kAddPoints:
104  addPoints(buffer+2, (buffer[1]>>4)+1);
105  break;
106  case Command::kDeletePoints:
107  deletePoints();
108  break;
109  case Command::kStopMove:
110  stopMove();
111  break;
112  case Command::kStartMove:
113  startMove();
114  break;
115  case Command::kSetParam:
116  setParam();
117  break;
118  case Command::kGotoXY:
119  gotoXY(readPosType(buffer+2,0), readPosType(buffer+2,1));
120  break;
121  case Command::kMovedXdY:
122  movedXdY(readPosType(buffer+2,0), readPosType(buffer+2,1));
123  break;
124  case Command::kGotoNextPoint:
125  gotoNextPoint();
126  break;
127  case Command::kSetNPoint:
128  setNPoint((buffer[2+0]<<8) | buffer[2+1]);
129  break;
130  case Command::kSetPos:
131  setPos(readPosType(buffer+2,0), readPosType(buffer+2,1));
132  break;
133  }
134  }
135  uint8_t sequence;
136 };
Definition: comm.hpp:22
Definition: comm.hpp:67
Definition: comm.hpp:79
Definition: uart.hpp:7