src/hp34comm.cpp

changeset 49
c146d19101a3
parent 47
11c57010e4f9
child 50
279868684eb3
equal deleted inserted replaced
48:fc55f6eaa8bc 49:c146d19101a3
30 cur_gstate(GSTATE_IDLE) 30 cur_gstate(GSTATE_IDLE)
31 { 31 {
32 serial.baud(187500); 32 serial.baud(187500);
33 serial.format(8, BufferedSerial::Even, 1); 33 serial.format(8, BufferedSerial::Even, 1);
34 cur_state = STATE_IDLE; 34 cur_state = STATE_IDLE;
35 send_thread.start(callback(this, &HPSerial::send_pending_key));
35 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq); 36 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
36 } 37 }
37 38
38 void HPSerial::startup(uint8_t keycode) { 39 void HPSerial::startup(uint8_t keycode) {
39 cur_gstate = GSTATE_STARTING; 40 cur_gstate = GSTATE_STARTING;
65 66
66 void HPSerial::sendkey(uint8_t keycode) 67 void HPSerial::sendkey(uint8_t keycode)
67 { 68 {
68 if (!sendbuf.full()) 69 if (!sendbuf.full())
69 sendbuf.push(keycode); 70 sendbuf.push(keycode);
71 }
72
73
74 bool HPSerial::wait_for(uint8_t value)
75 {
76 char c;
77
78 for(uint8_t i=0; i<2; i++)
79 {
80 while(!serial.readable())
81 wait_us(10);
82 //ThisThread::sleep_for(0.1ms);
83 serial.read(&c, 1);
84 if (value == c)
85 return true;
86 }
87 return false;
88 }
89
90 void HPSerial::send_pending_key() {
91 uint8_t c;
92
93 while(true)
94 {
95 if (!sendbuf.empty())
96 {
97 if (cur_gstate == GSTATE_IDLE)
98 {
99 serial.attach(0, SerialBase::RxIrq);
100 cur_gstate = GSTATE_TX;
101
102 c = 0x66;
103 serial.write(&c, 1);
104 if (!wait_for(0x99)) {}
105 // break; // XXX what to do?
106
107 sendbuf.pop(c);
108 serial.write(&c, 1);
109 if (!wait_for(0x00)) {}
110
111 c = 0x55;
112 serial.write(&c, 1);
113 cur_gstate = GSTATE_IDLE;
114 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
115 }
116 }
117 //else // prevent from flooding the main unit
118 ThisThread::sleep_for(5ms);
119 }
120 }
121
122 void HPSerial::send_startup_seq(uint8_t keycode) {
123 uint8_t c;
124
125 while (cur_gstate != GSTATE_IDLE) {
126 ThisThread::sleep_for(1ms);
127 }
128
129 serial.attach(0, SerialBase::RxIrq);
130 cur_gstate = GSTATE_TX;
131
132 // Send the init seq 0x33 0x02 0xFF <keycode> 0x55
133 c = 0x33;
134 serial.write(&c, 1);
135 if (!wait_for(0xCC)) {}
136
137 c = 0x02;
138 serial.write(&c, 1);
139 if (!wait_for(0x00)) {}
140
141 c = 0xFF;
142 serial.write(&c, 1);
143 if (!wait_for(0x00)) {}
144
145 c = keycode;
146 serial.write(&c, 1);
147 if (!wait_for(0x00)) {}
148
149 c = 0x55;
150 serial.write(&c, 1);
151 cur_gstate = GSTATE_IDLE;
152 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
153
154 }
155
156 void HPSerial::send_startup_seq() {
157 uint8_t c;
158
159 while (cur_gstate != GSTATE_IDLE) {
160 ThisThread::sleep_for(1ms);
161 }
162
163 serial.attach(0, SerialBase::RxIrq);
164 cur_gstate = GSTATE_TX;
165
166 // Send the init seq 0x33 0x02 0x00 0x55
167 c = 0x33;
168 serial.write(&c, 1);
169 if (!wait_for(0xCC)) {}
170
171 c = 0x02;
172 serial.write(&c, 1);
173 if (!wait_for(0x00)) {}
174
175 c = 0x00;
176 serial.write(&c, 1);
177 if (!wait_for(0x00)) {}
178
179 c = 0x55;
180 serial.write(&c, 1);
181 cur_gstate = GSTATE_IDLE;
182 serial.attach(callback(this, &HPSerial::rx_irq), SerialBase::RxIrq);
70 } 183 }
71 184
72 void HPSerial::send_key_when_idle() { 185 void HPSerial::send_key_when_idle() {
73 if (!sendbuf.empty() && cur_gstate == GSTATE_IDLE) 186 if (!sendbuf.empty() && cur_gstate == GSTATE_IDLE)
74 { 187 {

mercurial