-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutomateFirmata.ino
executable file
·1511 lines (1354 loc) · 46.1 KB
/
AutomateFirmata.ino
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define LOWPOWER
//#define VIRTUALWIRE
//#define LIQUIDCRYSTAL
#include <Firmata.h>
#include <EEPROM.h>
#include <Wire.h>
#ifdef LOWPOWER
#include <LowPower.h>
#endif
#ifdef VIRTUALWIRE
#include <VirtualWire.h>
#endif
#ifdef LIQUIDCRYSTAL
#include <LiquidCrystal_I2C.h>
#endif
/*
AutomateFirmata. Firmata with some additional features for Automate
(https://github.com/tuomas2/automate)
Repository URL: https://github.com/tuomas2/AutomateFirmata
Based on StandardFirmata.
Firmata is a generic protocol for communicating with microcontrollers
from software on a host computer. It is intended to work with
any host computer software package.
To download a host software package, please click on the following link
to open the list of Firmata client libraries in your default browser.
https://github.com/firmata/arduino#firmata-client-libraries
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) 2009-2016 Jeff Hoefs. All rights reserved.
Copyright (C) 2017 Tuomas Airaksinen. 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.
TODO
- Test high frequency PWM
- Test power saving (measurements)
- Power saving by disabling AD converter if ports not used
- Make power-saving configurable
*/
#define DEBUG 0
#if DEBUG
char tmpBuf[64];
#define dbgf(f_, ...) snprintf_P(tmpBuf, sizeof(tmpBuf), PSTR(f_), __VA_ARGS__); Firmata.sendString(tmpBuf);
#define dbg(f_) snprintf_P(tmpBuf, sizeof(tmpBuf), PSTR(f_)); Firmata.sendString(tmpBuf);
#else
#define dbgf(...);
#define dbg(f_);
#endif
#define I2C_WRITE B00000000
#define I2C_READ B00001000
#define I2C_READ_CONTINUOUSLY B00010000
#define I2C_STOP_READING B00011000
#define I2C_READ_WRITE_MODE_MASK B00011000
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
#define I2C_END_TX_MASK B01000000
#define I2C_STOP_TX 1
#define I2C_RESTART_TX 0
#define I2C_MAX_QUERIES 8
#define I2C_REGISTER_NOT_SPECIFIED -1
// the minimum interval for sampling analog input
#define MINIMUM_SAMPLING_INTERVAL 1
#define DEFAULT_SAMPLING_INTERVAL 500
#define DEFAULT_VIRTUALWIRE_SPEED 2
#define BLINK_INTERVAL 0
#define BLINK_PIN 13
#define SERIAL_SHUTDOWN_TIME 120000 // 2 minutes
// VirtualWire.h does not export this but we need it.
extern "C"
{
extern void vw_tx_stop();
}
/*==============================================================================
* GLOBAL VARIABLES
*============================================================================*/
#ifdef LIQUIDCRYSTAL
LiquidCrystal_I2C *lcd = NULL;
LiquidCrystal_I2C lcd0(0x27, 16, 2);
#else
int lcd = 0;
#endif
char lcdBuf[20];
byte lcdPort = 0x27;
byte lcdColumns = 16;
byte lcdRows = 2;
byte lcdReporting = false;
unsigned short int reportPin = 0; // currently being reported
unsigned long previousLCDMillis = 0;
uint8_t homeId = 0x01; // Set this different to your neighbors
uint8_t deviceId = 0x01; // Set this individual within your home
uint8_t vwPttPin = 0;
uint8_t vwRxPin = 0; // 0 disabled, otherwise pin number
uint8_t vwTxPin = 0;
uint8_t wakeUpPin = 0; // 2 or 3
uint8_t virtualWireSpeed = DEFAULT_VIRTUALWIRE_SPEED; // * 1000 bits per second
boolean serialEnabled = true;
boolean instantDigitalReporting = true;
unsigned long digitalOutputMillis = 0;
#ifdef LOWPOWER
period_t sleepMode = SLEEP_1S;
#endif
int sleepTime = 1000;
static const int BROADCAST_RECIPIENT = 0xFF;
static const uint8_t HEADER_LENGTH = 4;
// Incoming sysexs (0x00-0x0F are user defined according to FirmataConstants.h, let's use those)
static const byte SYSEX_VIRTUALWIRE_MESSAGE = 0x00; // This is used both incoming and outgoing
static const byte SYSEX_KEEP_ALIVE = 0x01;
static const byte SYSEX_SETUP_VIRTUALWIRE = 0x02;
static const byte SYSEX_SETUP_LCD = 0x03;
static const byte SYSEX_LCD_COMMAND = 0x04;
static const byte SYSEX_SET_ANALOG_REFERENCE = 0x05;
static const byte SYSEX_SET_INSTANT_DIGITAL_REPORTING = 0x06;
// LCD command bytes
static const byte LCD_SET_BACKLIGHT = 0x01;
static const byte LCD_PRINT = 0x02;
static const byte LCD_CLEAR = 0x03;
static const byte LCD_SET_CURSOR = 0x04;
static const byte LCD_SET_REPORTING = 0x05;
// Virtualwire command bytes
static const byte VIRTUALWIRE_SET_PIN_MODE = 0x01;
static const byte VIRTUALWIRE_ANALOG_MESSAGE = 0x02;
static const byte VIRTUALWIRE_DIGITAL_MESSAGE = 0x03;
static const byte VIRTUALWIRE_START_SYSEX = 0x04;
static const byte VIRTUALWIRE_SET_DIGITAL_PIN_VALUE = 0x05;
static const byte VIRTUALWIRE_DIGITAL_BROADCAST = 0x06;
static const byte VIRTUALWIRE_ANALOG_BROADCAST = 0x07;
// EEPROM addressses
static const int EEPROM_HOME_ID = 0;
static const int EEPROM_DEVICE_ID = 1;
static const int EEPROM_VIRTUALWIRE_RX_PIN = 2;
static const int EEPROM_VIRTUALWIRE_TX_PIN = 3;
static const int EEPROM_VIRTUALWIRE_PTT_PIN = 4;
static const int EEPROM_VIRTUALWIRE_SPEED = 5;
static const int EEPROM_WAKEUP_PIN = 6;
static const int EEPROM_SAMPLING_INTERVAL = 7; // 2 bytes
static const int EEPROM_ANALOG_INPUTS_TO_REPORT = 9; // 2 byte
static const int EEPROM_LCD_PORT = 11;
static const int EEPROM_LCD_COLUMNS = 12;
static const int EEPROM_LCD_ROWS = 13;
static const int EEPROM_CONFIGURED = 14;
static const int EEPROM_CONFIG_VERSION = 15;
static const int EEPROM_LCD_REPORTING = 16;
static const int EEPROM_ANALOG_REFERENCE = 17;
static const int EEPROM_INSTANT_DIGITAL_REPORTING = 18;
static const int EEPROM_DIGITAL_INPUTS_TO_REPORT = 50; // size required: TOTAL_PORTS x 1 byte
static const int EEPROM_PORT_CONFIG_INPUTS = 100; // size required: TOTAL_PORTS x 1 byte
static const int EEPROM_PIN_MODES = 150; // TOTAL_PINS x 1 byte
static const int EEPROM_IS_I2C_ENABLED = 200;
static const int EEPROM_I2C_QUERY_INDEX = 201;
static const int EEPROM_I2C_QUERY = 202; // sizeof(i2c_device_info)*queryIndex
static const byte IS_CONFIGURED = 0b10101010;
static const byte CONFIG_VERSION = 7;
#ifdef FIRMATA_SERIAL_FEATURE
SerialFirmata serialFeature;
#endif
/* analog inputs */
int analogInputsToReport = 0; // bitwise array to store pin reporting
#define ANALOG_PINS 6
static const int ANALOG_SAMPLING = 50;
unsigned long analogPinData[ANALOG_PINS];
unsigned long analogDataCount = 0;
byte analogReferenceVar = DEFAULT;
/* digital input ports */
byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence. 1 port == 8 pins.
byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent
/* pins configuration */
byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in (any) INPUT, 0 = anything else
/* timer variables */
unsigned long currentMillis = 0; // store the current value from millis()
unsigned long previousMillis = 0; // for comparison with currentMillis
unsigned long previousAnalogMillis = 0;
unsigned long lastSerialMillis = 0; // for comparison with currentMillis
int lcdInterval = 4000; // TODO: make configurable
unsigned int samplingInterval = DEFAULT_SAMPLING_INTERVAL; // how often to run the main loop (in ms)
unsigned long blinkMillis=0; // for comparison with currentMillis
/* i2c data */
struct i2c_device_info {
byte addr;
int reg;
byte bytes;
byte stopTX;
};
/* for i2c read continuous more */
i2c_device_info query[I2C_MAX_QUERIES];
byte i2cRxData[64];
boolean isI2CEnabled = false;
signed char queryIndex = -1;
// default delay time between i2c read request and Wire.requestFrom()
unsigned int i2cReadDelayTime = 0;
boolean isResetting = false;
// Forward declare a few functions to avoid compiler errors with older versions
// of the Arduino IDE.
void setPinModeCallback(byte, int);
void reportAnalogCallback(byte analogPin, int value);
void sysexCallback(byte, byte, byte*);
/* utility functions */
void wireWrite(byte data)
{
Wire.write((byte)data);
}
byte wireRead(void)
{
return Wire.read();
}
/*==============================================================================
* FUNCTIONS
*============================================================================*/
/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024. [default:64]
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024. [default:64]
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0
* - Pins 9 and 10 are paired on timer1
* - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 5, 6 cause the delay() and millis() functions to stop working.
* Other timing-related functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*/
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
void enableI2CPins()
{
byte i;
// is there a faster way to do this? would probaby require importing
// Arduino.h to get SCL and SDA pins
for (i = 0; i < TOTAL_PINS; i++) {
if (IS_PIN_I2C(i)) {
// mark pins as i2c so they are ignore in non i2c data requests
setPinModeCallback(i, PIN_MODE_I2C);
}
}
isI2CEnabled = true;
EEPROM.update(EEPROM_IS_I2C_ENABLED, isI2CEnabled);
Wire.begin();
}
/* disable the i2c pins so they can be used for other functions */
void disableI2CPins() {
isI2CEnabled = false;
// disable read continuous mode for all devices
queryIndex = -1;
EEPROM.update(EEPROM_IS_I2C_ENABLED, isI2CEnabled);
EEPROM.update(EEPROM_I2C_QUERY_INDEX, queryIndex);
}
void readAndReportData(byte address, int theRegister, byte numBytes, byte stopTX) {
// allow I2C requests that don't require a register read
// for example, some devices using an interrupt pin to signify new data available
// do not always require the register read so upon interrupt you call Wire.requestFrom()
if (theRegister != I2C_REGISTER_NOT_SPECIFIED) {
Wire.beginTransmission(address);
wireWrite((byte)theRegister);
Wire.endTransmission(stopTX); // default = true
// do not set a value of 0
if (i2cReadDelayTime > 0) {
// delay is necessary for some devices such as WiiNunchuck
delayMicroseconds(i2cReadDelayTime);
}
} else {
theRegister = 0; // fill the register with a dummy value
}
Wire.requestFrom(address, numBytes); // all bytes are returned in requestFrom
// check to be sure correct number of bytes were returned by slave
if (numBytes < Wire.available()) {
dbg("I2C: Too many bytes received");
} else if (numBytes > Wire.available()) {
dbg("I2C: Too few bytes received");
}
i2cRxData[0] = address;
i2cRxData[1] = theRegister;
for (int i = 0; i < numBytes && Wire.available(); i++) {
i2cRxData[2 + i] = wireRead();
}
// send slave address, register and received bytes
Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData);
}
void sendVirtualWireDigitalOutput(byte portNumber, byte portValue)
{
#ifdef VIRTUALWIRE
if(!vwTxPin)
return;
byte data[6];
data[0] = homeId;
data[1] = deviceId; // sender
data[2] = BROADCAST_RECIPIENT;
data[3] = VIRTUALWIRE_DIGITAL_BROADCAST;
data[4] = portNumber;
data[5] = portValue;
blink();
vw_send(data, sizeof(data));
#endif
}
void sendVirtualWireAnalogOutput(byte pinNumber, int analogData)
{
#ifdef VIRTUALWIRE
if(!vwTxPin)
return;
byte data[7];
data[0] = homeId;
data[1] = deviceId; // sender
data[2] = BROADCAST_RECIPIENT;
data[3] = VIRTUALWIRE_ANALOG_BROADCAST;
data[4] = pinNumber;
data[5] = analogData >> 8; // msb
data[6] = analogData; // lsb
blink();
vw_send(data, sizeof(data));
#endif
}
void outputPort(byte portNumber, byte portValue, byte forceSend)
{
// pins not configured as INPUT are cleared to zeros
portValue = portValue & portConfigInputs[portNumber];
// only send if the value is different than previously sent
if (forceSend || previousPINs[portNumber] != portValue) {
if(serialEnabled)
Firmata.sendDigitalPort(portNumber, portValue);
previousPINs[portNumber] = portValue;
#ifdef VIRTUALWIRE
sendVirtualWireDigitalOutput(portNumber, portValue);
#endif
digitalOutputMillis = currentMillis;
}
}
/* -----------------------------------------------------------------------------
* check all the active digital inputs for change of state, then add any events
* to the Serial output queue using Serial.print() */
void checkDigitalInputs(bool force)
{
/* Using non-looping code allows constants to be given to readPort().
* The compiler will apply substantial optimizations if the inputs
* to readPort() are compile-time constants. */
if (TOTAL_PORTS > 0 && reportPINs[0]) outputPort(0, readPort(0, portConfigInputs[0]), force);
if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), force);
if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), force);
if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), force);
if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), force);
if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), force);
if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), force);
if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), force);
if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), force);
if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), force);
if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), force);
if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), force);
if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), force);
if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), force);
if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), force);
if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), force);
}
// -----------------------------------------------------------------------------
/* sets the pin mode to the correct state and sets the relevant bits in the
* two bit-arrays that track Digital I/O and PWM status
*/
void setPinModeCallback(byte pin, int mode)
{
if (Firmata.getPinMode(pin) == PIN_MODE_IGNORE)
return;
if (Firmata.getPinMode(pin) == PIN_MODE_I2C && isI2CEnabled && mode != PIN_MODE_I2C) {
// disable i2c so pins can be used for other functions
// the following if statements should reconfigure the pins properly
disableI2CPins();
}
if (IS_PIN_ANALOG(pin)) {
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); // turn on/off reporting
}
if (IS_PIN_DIGITAL(pin)) {
if (mode == INPUT || mode == PIN_MODE_PULLUP) {
portConfigInputs[pin / 8] |= (1 << (pin & 7));
} else {
portConfigInputs[pin / 8] &= ~(1 << (pin & 7));
}
if(!isResetting)
EEPROM.update(EEPROM_PORT_CONFIG_INPUTS + pin/8, portConfigInputs[pin/8]);
}
Firmata.setPinState(pin, 0);
switch (mode) {
case PIN_MODE_ANALOG:
if (IS_PIN_ANALOG(pin)) {
if (IS_PIN_DIGITAL(pin)) {
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
}
Firmata.setPinMode(pin, PIN_MODE_ANALOG);
}
break;
case INPUT:
if (IS_PIN_DIGITAL(pin)) {
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
Firmata.setPinMode(pin, INPUT);
}
break;
case PIN_MODE_PULLUP:
if (IS_PIN_DIGITAL(pin)) {
pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP);
Firmata.setPinMode(pin, PIN_MODE_PULLUP);
Firmata.setPinState(pin, 1);
}
break;
case OUTPUT:
if (IS_PIN_DIGITAL(pin)) {
if (Firmata.getPinMode(pin) == PIN_MODE_PWM) {
// Disable PWM if pin mode was previously set to PWM.
digitalWrite(PIN_TO_DIGITAL(pin), LOW);
}
pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
Firmata.setPinMode(pin, OUTPUT);
}
break;
case PIN_MODE_PWM:
if (IS_PIN_PWM(pin)) {
if(PIN_TO_DIGITAL(pin)==3 || PIN_TO_DIGITAL(pin)==11)
setPwmFrequency(pin, 1);
pinMode(PIN_TO_PWM(pin), OUTPUT);
analogWrite(PIN_TO_PWM(pin), 0);
Firmata.setPinMode(pin, PIN_MODE_PWM);
}
break;
case PIN_MODE_I2C:
if (IS_PIN_I2C(pin)) {
// mark the pin as i2c
// the user must call I2C_CONFIG to enable I2C for a device
Firmata.setPinMode(pin, PIN_MODE_I2C);
}
break;
case PIN_MODE_SERIAL:
#ifdef FIRMATA_SERIAL_FEATURE
serialFeature.handlePinMode(pin, PIN_MODE_SERIAL);
#endif
break;
default:
dbg("Unknown pin mode");
return;
}
dbgf("W: Pin %d mode changed to %d", pin, mode);
EEPROM.update(EEPROM_PIN_MODES + pin, mode);
}
void configureLcd()
{
#ifdef LIQUIDCRYSTAL
if(lcd)
{
lcd = NULL;
}
if(lcdPort)
{
dbgf("LCD %d %d %d", lcdPort, lcdColumns, lcdRows);
lcd0 = LiquidCrystal_I2C(lcdPort, lcdColumns, lcdRows);
lcd = &lcd0;
lcd->begin();
lcd->print("AutomateFirmata");
lcd->setCursor(0,1);
lcd->print(" ready!");
lcd->setCursor(0,0);
lcd->setBacklight(false);
}
#endif
}
void configureVirtualWire()
{
#ifdef VIRTUALWIRE
dbgf("VW: %d %d %d %d", vwRxPin, vwTxPin, vwPttPin, virtualWireSpeed);
if(virtualWireSpeed < 1 || virtualWireSpeed > 9)
virtualWireSpeed = DEFAULT_VIRTUALWIRE_SPEED;
vw_rx_stop();
vw_tx_stop();
if(vwRxPin)
vw_set_rx_pin(vwRxPin);
if(vwTxPin)
vw_set_tx_pin(vwTxPin);
if(vwPttPin)
vw_set_ptt_pin(vwPttPin);
if(wakeUpPin && !(portConfigInputs[wakeUpPin/8] && (1<<wakeUpPin))) // if not already configured as input
pinMode(wakeUpPin, INPUT);
if(virtualWireSpeed && (vwTxPin || vwRxPin))
{
vw_setup(1000*virtualWireSpeed);
if(vwRxPin)
vw_rx_start();
}
#endif
}
/*
* 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 setPinValueCallback(byte pin, int value)
{
if (pin < TOTAL_PINS && IS_PIN_DIGITAL(pin)) {
if (Firmata.getPinMode(pin) == OUTPUT) {
Firmata.setPinState(pin, value);
digitalWrite(PIN_TO_DIGITAL(pin), value);
}
}
}
void analogWriteCallback(byte pin, int value)
{
if (pin < TOTAL_PINS) {
switch (Firmata.getPinMode(pin)) {
case PIN_MODE_PWM:
if (IS_PIN_PWM(pin))
analogWrite(PIN_TO_PWM(pin), value);
Firmata.setPinState(pin, value);
break;
}
}
}
void digitalWriteCallback(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) == OUTPUT || Firmata.getPinMode(pin) == INPUT) {
pinValue = ((byte)value & mask) ? 1 : 0;
if (Firmata.getPinMode(pin) == OUTPUT) {
pinWriteMask |= mask;
} else if (Firmata.getPinMode(pin) == INPUT && pinValue == 1 && Firmata.getPinState(pin) != 1) {
// only handle INPUT here for backwards compatibility
pinMode(pin, INPUT_PULLUP);
}
Firmata.setPinState(pin, pinValue);
}
}
mask = mask << 1;
}
writePort(port, (byte)value, pinWriteMask);
}
}
// -----------------------------------------------------------------------------
/* sets bits in a bit array (int) to toggle the reporting of the analogIns
*/
//void FirmataClass::setAnalogPinReporting(byte pin, byte state) {
//}
void reportAnalogCallback(byte analogPin, int value)
{
int analogData;
if (analogPin < TOTAL_ANALOG_PINS) {
if (value == 0) {
analogInputsToReport = analogInputsToReport & ~ (1 << analogPin);
} else {
analogInputsToReport = analogInputsToReport | (1 << analogPin);
// prevent during system reset or all analog pin values will be reported
// which may report noise for unconnected analog pins
if (!isResetting) {
// Send pin value immediately. This is helpful when connected via
// ethernet, wi-fi or bluetooth so pin states can be known upon
// reconnecting.
analogData = analogRead(analogPin);
if(serialEnabled)
Firmata.sendAnalog(analogPin, analogData);
sendVirtualWireAnalogOutput(analogPin, analogData);
}
}
if(!isResetting)
EEPROM.put(EEPROM_ANALOG_INPUTS_TO_REPORT, analogInputsToReport);
}
}
void reportDigitalCallback(byte port, int value)
{
if (port < TOTAL_PORTS) {
reportPINs[port] = value;
if(!isResetting)
EEPROM.update(EEPROM_DIGITAL_INPUTS_TO_REPORT + port, value);
// Send port value immediately. This is helpful when connected via
// ethernet, wi-fi or bluetooth so pin states can be known upon
// reconnecting.
if (value) outputPort(port, readPort(port, portConfigInputs[port]), true);
}
// do not disable analog reporting on these 8 pins, to allow some
// pins used for digital, others analog. Instead, allow both types
// of reporting to be enabled, but check if the pin is configured
// as analog when sampling the analog inputs. Likewise, while
// scanning digital pins, portConfigInputs will mask off values from any
// pins configured as analog
}
/*==============================================================================
* SYSEX-BASED commands
*============================================================================*/
void sysexCallback(byte command, byte argc, byte *argv)
{
byte mode;
byte stopTX;
byte slaveAddress;
byte data;
int slaveRegister;
unsigned int delayTime;
byte lcdCommand;
switch (command) {
case SYSEX_SETUP_LCD:
lcdPort = argv[0];
lcdColumns = argv[1];
lcdRows = argv[2];
lcdReporting = argv[3];
EEPROM.update(EEPROM_LCD_PORT, lcdPort);
EEPROM.update(EEPROM_LCD_COLUMNS, lcdColumns);
EEPROM.update(EEPROM_LCD_ROWS, lcdRows);
EEPROM.update(EEPROM_LCD_REPORTING, lcdReporting);
configureLcd();
break;
case SYSEX_LCD_COMMAND:
#ifdef LIQUIDCRYSTAL
if(!lcd)
return;
lcdCommand = argv[0];
switch (lcdCommand) {
case LCD_SET_BACKLIGHT:
if(argv[1])
lcd->backlight();
else
lcd->noBacklight();
break;
case LCD_SET_CURSOR:
lcd->setCursor(argv[1], argv[2]);
break;
case LCD_CLEAR:
lcd->clear();
break;
case LCD_PRINT:
lcd->print((char*)&argv[1]);
break;
case LCD_SET_REPORTING:
lcdReporting = argv[1];
EEPROM.update(EEPROM_LCD_REPORTING, lcdReporting);
break;
}
#endif
break;
case SYSEX_KEEP_ALIVE:
dbg("I'm alive!");
break;
case SYSEX_SETUP_VIRTUALWIRE:
vwRxPin = argv[0];
vwTxPin = argv[1];
vwPttPin = argv[2];
wakeUpPin = argv[3],
virtualWireSpeed = argv[4];
homeId = argv[5];
deviceId = argv[6];
EEPROM.update(EEPROM_VIRTUALWIRE_RX_PIN, vwRxPin);
EEPROM.update(EEPROM_VIRTUALWIRE_TX_PIN, vwTxPin);
EEPROM.update(EEPROM_VIRTUALWIRE_PTT_PIN, vwPttPin);
EEPROM.update(EEPROM_WAKEUP_PIN, wakeUpPin);
EEPROM.update(EEPROM_VIRTUALWIRE_SPEED, virtualWireSpeed);
EEPROM.update(EEPROM_HOME_ID, homeId);
EEPROM.update(EEPROM_DEVICE_ID, deviceId);
configureVirtualWire();
break;
case SYSEX_SET_ANALOG_REFERENCE:
analogReferenceVar = argv[0];
EEPROM.update(EEPROM_ANALOG_REFERENCE, analogReferenceVar);
analogReference(analogReferenceVar);
break;
case SYSEX_SET_INSTANT_DIGITAL_REPORTING:
instantDigitalReporting = argv[0];
EEPROM.update(EEPROM_INSTANT_DIGITAL_REPORTING, instantDigitalReporting);
break;
case SYSEX_VIRTUALWIRE_MESSAGE:
#ifdef VIRTUALWIRE
blink();
if(vwTxPin)
vw_send(argv, argc);
#endif
break;
case I2C_REQUEST:
mode = argv[1] & I2C_READ_WRITE_MODE_MASK;
if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) {
dbg("10-bit addressing not supported");
return;
}
else {
slaveAddress = argv[0];
}
// need to invert the logic here since 0 will be default for client
// libraries that have not updated to add support for restart tx
if (argv[1] & I2C_END_TX_MASK) {
stopTX = I2C_RESTART_TX;
}
else {
stopTX = I2C_STOP_TX; // default
}
switch (mode) {
case I2C_WRITE:
Wire.beginTransmission(slaveAddress);
for (byte i = 2; i < argc; i += 2) {
data = argv[i] + (argv[i + 1] << 7);
wireWrite(data);
}
Wire.endTransmission();
delayMicroseconds(70);
break;
case I2C_READ:
if (argc == 6) {
// a slave register is specified
slaveRegister = argv[2] + (argv[3] << 7);
data = argv[4] + (argv[5] << 7); // bytes to read
}
else {
// a slave register is NOT specified
slaveRegister = I2C_REGISTER_NOT_SPECIFIED;
data = argv[2] + (argv[3] << 7); // bytes to read
}
readAndReportData(slaveAddress, (int)slaveRegister, data, stopTX);
break;
case I2C_READ_CONTINUOUSLY:
if ((queryIndex + 1) >= I2C_MAX_QUERIES) {
// too many queries, just ignore
dbg("too many queries");
break;
}
if (argc == 6) {
// a slave register is specified
slaveRegister = argv[2] + (argv[3] << 7);
data = argv[4] + (argv[5] << 7); // bytes to read
}
else {
// a slave register is NOT specified
slaveRegister = (int)I2C_REGISTER_NOT_SPECIFIED;
data = argv[2] + (argv[3] << 7); // bytes to read
}
queryIndex++;
query[queryIndex].addr = slaveAddress;
query[queryIndex].reg = slaveRegister;
query[queryIndex].bytes = data;
query[queryIndex].stopTX = stopTX;
EEPROM.update(EEPROM_I2C_QUERY_INDEX, queryIndex);
EEPROM.put(EEPROM_I2C_QUERY + queryIndex*sizeof(i2c_device_info), query[queryIndex]);
break;
case I2C_STOP_READING:
byte queryIndexToSkip;
// if read continuous mode is enabled for only 1 i2c device, disable
// read continuous reporting for that device
if (queryIndex <= 0) {
queryIndex = -1;
} else {
queryIndexToSkip = 0;
// if read continuous mode is enabled for multiple devices,
// determine which device to stop reading and remove it's data from
// the array, shifiting other array data to fill the space
for (byte i = 0; i < queryIndex + 1; i++) {
if (query[i].addr == slaveAddress) {
queryIndexToSkip = i;
break;
}
}
for (byte i = queryIndexToSkip; i < queryIndex + 1; i++) {
if (i < I2C_MAX_QUERIES) {
query[i].addr = query[i + 1].addr;
query[i].reg = query[i + 1].reg;
query[i].bytes = query[i + 1].bytes;
query[i].stopTX = query[i + 1].stopTX;
EEPROM.put(EEPROM_I2C_QUERY + i*sizeof(i2c_device_info), query[i]);
}
}
queryIndex--;
}
EEPROM.update(EEPROM_I2C_QUERY_INDEX, queryIndex);
break;
default:
break;
}
break;
case I2C_CONFIG:
delayTime = (argv[0] + (argv[1] << 7));
if (delayTime > 0) {
i2cReadDelayTime = delayTime;
}
if (!isI2CEnabled) {
enableI2CPins();
}
break;
case SAMPLING_INTERVAL:
if (argc > 1) {
samplingInterval = argv[0] + (argv[1] << 7);
if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) {
samplingInterval = MINIMUM_SAMPLING_INTERVAL;
}
} else {
//Firmata.sendString("Not enough data");
}
setSleepMode();
EEPROM.put(EEPROM_SAMPLING_INTERVAL, samplingInterval);
break;
case EXTENDED_ANALOG:
if (argc > 1) {
int val = argv[1];
if (argc > 2) val |= (argv[2] << 7);
if (argc > 3) val |= (argv[3] << 14);
analogWriteCallback(argv[0], val);
}
break;
case CAPABILITY_QUERY:
Firmata.write(START_SYSEX);
Firmata.write(CAPABILITY_RESPONSE);
for (byte pin = 0; pin < TOTAL_PINS; pin++) {
if (IS_PIN_DIGITAL(pin)) {
Firmata.write((byte)INPUT);
Firmata.write(1);
Firmata.write((byte)PIN_MODE_PULLUP);
Firmata.write(1);
Firmata.write((byte)OUTPUT);
Firmata.write(1);
}
if (IS_PIN_ANALOG(pin)) {
Firmata.write(PIN_MODE_ANALOG);
Firmata.write(10); // 10 = 10-bit resolution
}
if (IS_PIN_PWM(pin)) {
Firmata.write(PIN_MODE_PWM);
Firmata.write(DEFAULT_PWM_RESOLUTION);
}
if (IS_PIN_I2C(pin)) {
Firmata.write(PIN_MODE_I2C);
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
}
#ifdef FIRMATA_SERIAL_FEATURE
serialFeature.handleCapability(pin);
#endif
Firmata.write(127);
}
Firmata.write(END_SYSEX);
break;
case PIN_STATE_QUERY:
if (argc > 0) {
byte pin = argv[0];
Firmata.write(START_SYSEX);
Firmata.write(PIN_STATE_RESPONSE);
Firmata.write(pin);
if (pin < TOTAL_PINS) {
Firmata.write(Firmata.getPinMode(pin));
Firmata.write((byte)Firmata.getPinState(pin) & 0x7F);
if (Firmata.getPinState(pin) & 0xFF80) Firmata.write((byte)(Firmata.getPinState(pin) >> 7) & 0x7F);
if (Firmata.getPinState(pin) & 0xC000) Firmata.write((byte)(Firmata.getPinState(pin) >> 14) & 0x7F);
}
Firmata.write(END_SYSEX);
}
break;
case ANALOG_MAPPING_QUERY:
Firmata.write(START_SYSEX);
Firmata.write(ANALOG_MAPPING_RESPONSE);
for (byte pin = 0; pin < TOTAL_PINS; pin++) {