source: rtos_arduino/trunk/arduino_lib/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.8 KB
Line 
1/*
2 * Firmata is a generic protocol for communicating with microcontrollers
3 * from software on a host computer. It is intended to work with
4 * any host computer software package.
5 *
6 * To download a host software package, please clink on the following link
7 * to open the download page in your default browser.
8 *
9 * http://firmata.org/wiki/Download
10 */
11
12/* Supports as many digital inputs and outputs as possible.
13 *
14 * This example code is in the public domain.
15 */
16#include <Firmata.h>
17
18byte previousPIN[TOTAL_PORTS]; // PIN means PORT for input
19byte previousPORT[TOTAL_PORTS];
20
21void outputPort(byte portNumber, byte portValue)
22{
23 // only send the data when it changes, otherwise you get too many messages!
24 if (previousPIN[portNumber] != portValue) {
25 Firmata.sendDigitalPort(portNumber, portValue);
26 previousPIN[portNumber] = portValue;
27 }
28}
29
30void setPinModeCallback(byte pin, int mode) {
31 if (IS_PIN_DIGITAL(pin)) {
32 pinMode(PIN_TO_DIGITAL(pin), mode);
33 }
34}
35
36void digitalWriteCallback(byte port, int value)
37{
38 byte i;
39 byte currentPinValue, previousPinValue;
40
41 if (port < TOTAL_PORTS && value != previousPORT[port]) {
42 for (i = 0; i < 8; i++) {
43 currentPinValue = (byte) value & (1 << i);
44 previousPinValue = previousPORT[port] & (1 << i);
45 if (currentPinValue != previousPinValue) {
46 digitalWrite(i + (port * 8), currentPinValue);
47 }
48 }
49 previousPORT[port] = value;
50 }
51}
52
53void setup()
54{
55 Firmata.setFirmwareVersion(0, 1);
56 Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
57 Firmata.attach(SET_PIN_MODE, setPinModeCallback);
58 Firmata.begin(57600);
59}
60
61void loop()
62{
63 byte i;
64
65 for (i = 0; i < TOTAL_PORTS; i++) {
66 outputPort(i, readPort(i, 0xff));
67 }
68
69 while (Firmata.available()) {
70 Firmata.processInput();
71 }
72}
Note: See TracBrowser for help on using the repository browser.