Skip to content

Commit 92121c4

Browse files
committed
Bug fixing
Removed bugs in data response when more than one pin is configured as output Issue #22
1 parent 2596f74 commit 92121c4

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

MLC/arduino/Firmware/Firmware.ino

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int actuate(const char* data)
139139
int byte_count = byte(data[1]); // Un byte puede llegar a limitar la cantidad de salidas... creo
140140

141141
uint8_t digital_input_buffer[DIGITAL_PINS_COUNT][REPORT_READ_COUNT / 8 + 2]; // 255 lectures of 1 bit for every digital pin -- 1 extra byte for the port address
142-
uint8_t analog_input_buffer[ANALOG_PINS_COUNT][(2 * REPORT_READ_COUNT) + 2]; // 255 lectures of 2 bytes for every analog pin -- 1 extra byte for the port address
142+
uint8_t analog_input_buffer[ANALOG_PINS_COUNT][(2 * REPORT_READ_COUNT) + 3]; // 255 lectures of 2 bytes for every analog pin -- 1 extra byte for the port address
143143

144144
LOG("Actutating over payload of size: ", byte_count);
145145

@@ -201,15 +201,17 @@ int actuate(const char* data)
201201
//response[len + 2] = byte((data & 0xFF00) >> 8); // Se guarda el msb en el buffer
202202
//response[len + 3] = byte(data & 0xFF); // Se guarda el lsb en el buffer
203203
analog_input_buffer[current_analog][0] = INPUT_PORTS[i];
204-
analog_input_buffer[current_analog][lecture + 1] = byte((data & 0xFF00) >> 8);
205-
analog_input_buffer[current_analog][lecture + 2] = byte(data & 0xFF);
206-
207-
current_analog++;
204+
analog_input_buffer[current_analog][(lecture * 2) + 1] = byte((data & 0xFF00) >> 8);
205+
analog_input_buffer[current_analog][(lecture * 2) + 2] = byte(data & 0xFF);
208206

209207
//len += 3; // Cada lectura de un recurso analógico ocupa dos bytes. FIXME: se puede optimizar con bajas resoluciones
210208
LOG("=====================================", "");
211209
LOG("Analog pin read: ", INPUT_PORTS[i]);
212210
LOG("Analog read value: ", data);
211+
Serial.println(analog_input_buffer[current_analog][(lecture * 2) + 1], HEX);
212+
Serial.println(analog_input_buffer[current_analog][(lecture * 2) + 2], HEX);
213+
214+
current_analog++;
213215
} else
214216
{
215217
int data = digitalRead(INPUT_PORTS[i]);
@@ -233,12 +235,12 @@ int actuate(const char* data)
233235
// Every analog output will be in the buffer as
234236
if (ANALOG_PINS_COUNT > 0)
235237
{
236-
len += ANALOG_PINS_COUNT + ( (REPORT_READ_COUNT + 1) * 2);
238+
len += ANALOG_PINS_COUNT + ((REPORT_READ_COUNT + 1) * 2 * ANALOG_PINS_COUNT);
237239
}
238240

239241
if (DIGITAL_PINS_COUNT > 0)
240242
{
241-
len += DIGITAL_PINS_COUNT + (((REPORT_READ_COUNT + 1) / 8 ) + 1);
243+
len += DIGITAL_PINS_COUNT + (((REPORT_READ_COUNT + 1) / 8 ) + 1) * DIGITAL_PINS_COUNT;
242244
}
243245

244246
LOG("Reporting actuate results ", len - 1);
@@ -251,7 +253,7 @@ int actuate(const char* data)
251253
SerialUSB.write(analog_input_buffer[0], ANALOG_PINS_COUNT + ((REPORT_READ_COUNT + 1) * 2 * ANALOG_PINS_COUNT));
252254
//SerialUSB.write(INPUT_PORTS[i]);
253255
//SerialUSB.write(analog_input_buffer[INPUT_PORTS[i] - A0], (REPORT_READ_COUNT + 1) * 2);
254-
LOG("Reported an analog port with a read count len of: ", (REPORT_READ_COUNT + 1) * 2 + 1);
256+
LOG("Reported an analog port with a read count len of: ", ANALOG_PINS_COUNT + (REPORT_READ_COUNT + 1) * 2 * ANALOG_PINS_COUNT);
255257
}
256258

257259
if ( DIGITAL_PINS_COUNT > 0) {

MLC/arduino/protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ def actuate(self, data):
144144
else:
145145
if pin in self._digital_inputs:
146146
for i in range(0, self._read_count + 1):
147-
results[pin].append(bool(ord(data[pos + 1 + i/8]) & (0x80 >> (i % 8))))
147+
results[pin].append(bool(ord(data[pos + 1 + i/8]) & (0x01 << (i % 8))))
148148
pos = pos + 1 + self._read_count/8 + 1
149149

150150
if self._report_mode == "AVERAGE":
151151
results[pin] = [ (sum(results[pin]) * 2) > (self._read_count + 1) ]
152152

153153
else:
154-
raise Exception("Unknown port in response. Restart Arduino board, your software and pray")
154+
raise Exception("Unknown port \"%d\" in response. Restart Arduino board, your software and pray" % pin)
155155

156156
return results
157157

tests/mlc/arduino_protocol/test_protocol.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
ACK = "\xFF\x00"
77
NACK = "\xFF\x01"
8-
REPORT = "\xF1\x05\x10\x80\x3D\x05\x20"
9-
REPORT_B = "\xF1\x1A\x10\xF1\xA0\x3D\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00"
8+
REPORT = "\xF1\x05\x10\x01\x3D\x05\x20"
9+
REPORT_B = "\xF1\x1A\x10\xF1\x0A\x3D\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00"
10+
REPORT_C = "\xF1\x16\x3D\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x3E\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00"
1011

1112

1213
class TestArduinoInterface(unittest.TestCase):
@@ -89,14 +90,27 @@ def test_actuate_with_many_readings(self):
8990
response = self._interface.actuate([(60, 128)])
9091
self.assertEqual(2, len(response))
9192
self.assertTrue(response[0x10][0])
92-
self.assertFalse(response[0x10][6])
93+
self.assertTrue(response[0x10][6])
9394
self.assertTrue(response[0x10][7])
94-
self.assertTrue(response[0x10][8])
95-
self.assertFalse(response[0x10][9])
96-
self.assertTrue(response[0x10][10])
95+
self.assertFalse(response[0x10][8])
96+
self.assertTrue(response[0x10][9])
97+
self.assertFalse(response[0x10][10])
9798
for i in range(0, 11):
9899
self.assertEqual(0x0100, response[0x3D][i])
99100

101+
def test_actuate_with_many_pins_readings(self):
102+
self._connection = MockConnection(REPORT_C)
103+
self._interface = ArduinoInterface(self._connection, boards.Due)
104+
self._interface.set_report_mode("BULK", read_count=5, read_delay=5)
105+
self._interface.add_output(60)
106+
self._interface.add_input(61)
107+
self._interface.add_input(62)
108+
response = self._interface.actuate([(60, 128)])
109+
for i in range(0, 5):
110+
self.assertEqual(0x0100, response[0x3D][i])
111+
for i in range(0, 5):
112+
self.assertEqual(0x0100, response[0x3E][i])
113+
100114

101115
def test_average(self):
102116
self._connection = MockConnection(REPORT_B)

tests/pocs/test_connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ def actuate(terminal):
1010
arduinoDue = ArduinoInterface(connection, boards.Due)
1111

1212
arduinoDue.reset()
13-
arduinoDue.set_report_mode("BULK")
13+
arduinoDue.set_report_mode("BULK", read_count=5, read_delay=200)
1414
arduinoDue.add_output(40)
1515
arduinoDue.add_input(64)
16+
arduinoDue.add_input(63)
1617
arduinoDue.add_input(30)
1718

1819
output = arduinoDue.actuate([(40,1)])
19-
for i in output:
20-
print "Pin %d input: %s" % (ord(i[0]), i[1])
20+
for i in output.keys():
21+
print i
22+
print output[i]
23+
#print "Pin %d input: %s" % (output[i][0])
2124

2225

2326
if __name__ == "__main__":

0 commit comments

Comments
 (0)