-
Notifications
You must be signed in to change notification settings - Fork 71
/
DigitalOutputFirmata.cpp
106 lines (91 loc) · 3.29 KB
/
DigitalOutputFirmata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
DigitalOutputFirmata.cpp - Firmata library
Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved.
Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
Last updated by Jeff Hoefs: February 16th, 2016
*/
#include <ConfigurableFirmata.h>
#include "DigitalOutputFirmata.h"
DigitalOutputFirmata *DigitalOutputFirmataInstance;
void digitalOutputWriteCallback(byte port, int value)
{
DigitalOutputFirmataInstance->digitalWritePort(port, value);
}
/*
* Sets the value of an individual pin. Useful if you want to set a pin value but
* are not tracking the digital port state.
* Can only be used on pins configured as OUTPUT.
* Cannot be used to enable pull-ups on Digital INPUT pins.
*/
void handleSetPinValueCallback(byte pin, int value)
{
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
if (Firmata.getPinMode(pin) == PIN_MODE_OUTPUT) {
digitalWrite(pin, value);
Firmata.setPinState(pin, value);
}
}
}
DigitalOutputFirmata::DigitalOutputFirmata()
{
DigitalOutputFirmataInstance = this;
Firmata.attach(DIGITAL_MESSAGE, digitalOutputWriteCallback);
Firmata.attach(SET_DIGITAL_PIN_VALUE, handleSetPinValueCallback);
}
boolean DigitalOutputFirmata::handleSysex(byte command, byte argc, byte* argv)
{
return false;
}
void DigitalOutputFirmata::reset()
{
}
void DigitalOutputFirmata::digitalWritePort(byte port, int value)
{
byte pin, lastPin, pinValue, mask = 1, pinWriteMask = 0;
if (port < TOTAL_PORTS) {
// create a mask of the pins on this port that are writable.
lastPin = port * 8 + 8;
if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS;
for (pin = port * 8; pin < lastPin; pin++) {
// do not disturb non-digital pins (eg, Rx & Tx)
if (IS_PIN_DIGITAL(pin)) {
// do not touch pins in PWM, ANALOG, SERVO or other modes
if (Firmata.getPinMode(pin) == PIN_MODE_OUTPUT || Firmata.getPinMode(pin) == PIN_MODE_INPUT) {
pinValue = ((byte)value & mask) ? 1 : 0;
if (Firmata.getPinMode(pin) == PIN_MODE_OUTPUT) {
pinWriteMask |= mask;
} else if (Firmata.getPinMode(pin) == PIN_MODE_INPUT && pinValue == 1 && Firmata.getPinState(pin) != 1) {
pinMode(pin, INPUT_PULLUP);
}
Firmata.setPinState(pin, pinValue);
}
}
mask = mask << 1;
}
writePort(port, (byte)value, pinWriteMask);
}
}
boolean DigitalOutputFirmata::handlePinMode(byte pin, int mode)
{
if (IS_PIN_DIGITAL(pin) && mode == PIN_MODE_OUTPUT && Firmata.getPinMode(pin) != PIN_MODE_IGNORE) {
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM
pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
return true;
}
return false;
}
void DigitalOutputFirmata::handleCapability(byte pin)
{
if (IS_PIN_DIGITAL(pin)) {
Firmata.write((byte)PIN_MODE_OUTPUT);
Firmata.write(1);
}
}