Skip to content

Commit eb9f37d

Browse files
committed
Added PWN pin support and fixed minor bugs
Issue #22
1 parent 92121c4 commit eb9f37d

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

MLC/arduino/Firmware/Firmware.ino

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const uint8_t ADD_INPUT_PIN_CMD = 0x02;
1212
const uint8_t ADD_OUTPUT_PIN_CMD = 0x03;
1313
const uint8_t SET_PIN_MODE_CMD = 0x04;
1414
const uint8_t SET_REPORT_MODE_CMD = 0x05;
15+
const uint8_t ANALOG_WRITE = 0x06;
1516
const uint8_t ACTUATE_CMD = 0xF0;
1617
const uint8_t RESET_PINS = 0xFE;
1718

@@ -40,6 +41,16 @@ uint8_t DIGITAL_PINS_COUNT = 0;
4041

4142
const char* ACK = "\xFF\x00";
4243

44+
/**
45+
* ANALOG_WRITE: 0x06 0x03 [PIN] [H_VALUE][L_VALUE]
46+
*/
47+
int analog_write(const char* data)
48+
{
49+
uint16_t data = (data[3] << 8) + data[4];
50+
analogWrite(data[2], data[3]);
51+
return 5;
52+
}
53+
4354
/**
4455
ANALOG_PRECISION: 0x01 0x01 [BITS]
4556
*/
@@ -208,8 +219,6 @@ int actuate(const char* data)
208219
LOG("=====================================", "");
209220
LOG("Analog pin read: ", INPUT_PORTS[i]);
210221
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);
213222

214223
current_analog++;
215224
} else
@@ -286,25 +295,9 @@ void setup() {
286295
executor[SET_REPORT_MODE_CMD] = &set_report_mode;
287296
executor[ACTUATE_CMD] = &actuate;
288297
executor[RESET_PINS] = &reset;
289-
290-
// executor[SET_PIN_MODE_CMD]("\x04\x02\x3E\x00");
291-
// executor[ANALOG_PRECISION_CMD]("\x01\x01\x0C");
292-
// executor[SET_REPORT_MODE_CMD]("\x05\x03\x00\x09\x00");
293-
// executor[ADD_INPUT_PIN_CMD]("\x02\x01\x3E");
294-
// executor[SET_PIN_MODE_CMD]("\x04\x02\x3E\x00");
295-
// // executor[ADD_INPUT_PIN_CMD]("\x02\x01\x3F");
296-
// // executor[SET_PIN_MODE_CMD]("\x04\x02\x3F\x00");
297-
// executor[ADD_INPUT_PIN_CMD]("\x02\x01\x28");
298-
// executor[SET_PIN_MODE_CMD]("\x04\x02\x28\x00");
299-
// executor[ADD_INPUT_PIN_CMD]("\x02\x01\x29");
300-
// executor[SET_PIN_MODE_CMD]("\x04\x02\x29\x00");
301-
// executor[ADD_INPUT_PIN_CMD]("\x02\x01\x2A");
302-
// executor[SET_PIN_MODE_CMD]("\x04\x02\x2A\x00");
303-
304298
}
305299

306300
void loop() {
307-
//executor[ACTUATE_CMD]("\xF0\x01\x2B\x01");
308301

309302
if (SerialUSB.available() > 0)
310303
{
@@ -327,6 +320,4 @@ void loop() {
327320
LOG("b_pos ", b_pos);
328321
}
329322
}
330-
331-
delay(4000);
332323
}

MLC/arduino/boards.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Due = {"ANALOG_PINS":range(54, 67), "DIGITAL_PINS":range(0,54)}
3-
Uno = {"ANALOG_PINS":range(14, 20), "DIGITAL_PINS":range(0,14)}
4-
Mega = {"ANALOG_PINS":range(54, 70), "DIGITAL_PINS":range(0,54)}
5-
Leonardo = {"ANALOG_PINS":range(14, 20), "DIGITAL_PINS":range(0,14)}
2+
Due = {"ANALOG_PINS":range(54, 67), "DIGITAL_PINS":range(0,54), "PWM_PINS":(2,3,4,5,6,7,8,9,10,11,12,13)}
3+
Uno = {"ANALOG_PINS":range(14, 20), "DIGITAL_PINS":range(0,14), "PWM_PINS":(3,5,6,9,10,11)}
4+
Mega = {"ANALOG_PINS":range(54, 70), "DIGITAL_PINS":range(0,54), "PWM_PINS":range(2,14)}
5+
Leonardo = {"ANALOG_PINS":range(14, 20), "DIGITAL_PINS":range(0,14), "PWM_PINS":(3,5,6,9,10,11,12,13)}

MLC/arduino/protocol.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"SET_OUTPUT" : '\x03\x01%s',
55
"PIN_MODE" : '\x04\x02%s%s',
66
"REPORT_MODE" : '\x05\x03%s%s%s',
7+
"ANALOG_WRITE" : '\x06\x03%s%s%s',
78
"ACK" : '\xFF\x00',
89
"ACTUATE" : '\xF0',
910
"RESET" : '\xFE',
@@ -25,6 +26,12 @@ def __init__(self, connection, board):
2526
self._read_delay = 0
2627
self._board = board
2728

29+
def set_pwm(self, pin, duty_cicle):
30+
if port in self._anlg_inputs or port in self._digital_inputs:
31+
raise Exception("Port %s is configured as input!" % port)
32+
33+
self._connection.send(_PROTOCOL_CMDS["ANALAOG_WRITE"] % (chr(pin), chr((duty_cicle & 0xFF00) >> 8), chr(duty_cicle & 0x00FF)))
34+
2835
def set_precision(self, bits):
2936
if bits > 32 or bits < 1:
3037
raise Exception("Precision bits must be between 1 and 32!")
@@ -92,6 +99,8 @@ def __validate_pin(self, pin):
9299

93100
def reset(self):
94101
self._connection.send(_PROTOCOL_CMDS["RESET"])
102+
self._anlg_outputs = []
103+
self._digital_outputs = []
95104

96105
def actuate(self, data):
97106
"""

0 commit comments

Comments
 (0)