From ada8fdbdf99f2b1aeec3c8f32a6d47b280933647 Mon Sep 17 00:00:00 2001 From: simisimis Date: Wed, 8 Aug 2018 08:33:26 +0200 Subject: [PATCH] initial commit --- electricity/measure/measure.ino | 33 + .../Adafruit_BLE_Firmata.cpp | 509 ++++ .../Adafruit_BLE_Firmata.h | 219 ++ .../Adafruit_BLE_Firmata_Boards.h | 135 + libraries/Adafruit_BLEFirmata/LICENSE.txt | 458 ++++ libraries/Adafruit_BLEFirmata/README.md | 125 + .../BluefruitLE_nrf51822/BluefruitConfig.h | 56 + .../BluefruitLE_nrf51822.ino | 859 +++++++ .../BluefruitConfig.h | 54 + .../CircuitPlayground_nrf51822.ino | 851 +++++++ .../Firmata_nRF8001/BluefruitConfig.h | 9 + .../Firmata_nRF8001/Firmata_nRF8001.ino | 811 ++++++ .../examples/StandardFirmata/LICENSE.txt | 458 ++++ .../examples/StandardFirmata/Makefile | 273 ++ .../StandardFirmata/StandardFirmata.ino | 738 ++++++ libraries/Adafruit_BLEFirmata/keywords.txt | 62 + .../Adafruit_BLEFirmata/library.properties | 9 + .../Adafruit_ESP8266/Adafruit_ESP8266.cpp | 270 ++ libraries/Adafruit_ESP8266/Adafruit_ESP8266.h | 65 + libraries/Adafruit_ESP8266/README.md | 4 + .../examples/webclient/webclient.ino | 117 + libraries/Adafruit_ESP8266/library.properties | 9 + .../previous/Adafruit_ESP8266.ino | 367 +++ .../Adafruit_NeoPixel/Adafruit_NeoPixel.cpp | 2238 +++++++++++++++++ .../Adafruit_NeoPixel/Adafruit_NeoPixel.h | 182 ++ libraries/Adafruit_NeoPixel/COPYING | 165 ++ libraries/Adafruit_NeoPixel/README.md | 51 + libraries/Adafruit_NeoPixel/esp8266.c | 82 + .../RGBWstrandtest/.esp8266.test.skip | 0 .../RGBWstrandtest/.trinket.test.skip | 0 .../RGBWstrandtest/RGBWstrandtest.ino | 261 ++ .../examples/StrandtestBLE/.test.skip | 0 .../examples/StrandtestBLE/BLESerial.cpp | 133 + .../examples/StrandtestBLE/BLESerial.h | 46 + .../examples/StrandtestBLE/StrandtestBLE.ino | 208 ++ .../examples/buttoncycler/.esp8266.test.skip | 0 .../examples/buttoncycler/buttoncycler.ino | 165 ++ .../examples/simple/.esp8266.test.skip | 0 .../examples/simple/simple.ino | 47 + .../examples/strandtest/.esp8266.test.skip | 0 .../examples/strandtest/strandtest.ino | 134 + libraries/Adafruit_NeoPixel/keywords.txt | 40 + .../Adafruit_NeoPixel/library.properties | 9 + .../Adafruit_PWMServoDriver.cpp | 194 ++ .../Adafruit_PWMServoDriver.h | 69 + .../README.md | 50 + .../examples/gpiotest/gpiotest.ino | 49 + .../examples/pwmtest/pwmtest.ino | 52 + .../examples/servo/servo.ino | 83 + .../library.properties | 9 + .../license.txt | 26 + .../ConfigurableFirmata.ino | 441 ++++ .../ConfigurableFirmataBLE.ino | 382 +++ .../ConfigurableFirmataWiFi.ino | 690 +++++ .../ConfigurableFirmata/extras/LICENSE.txt | 458 ++++ libraries/ConfigurableFirmata/keywords.txt | 96 + libraries/ConfigurableFirmata/library.json | 16 + .../ConfigurableFirmata/library.properties | 9 + libraries/ConfigurableFirmata/readme.md | 73 + .../src/AccelStepperFirmata.cpp | 420 ++++ .../src/AccelStepperFirmata.h | 69 + .../ConfigurableFirmata/src/AnalogFirmata.cpp | 32 + .../ConfigurableFirmata/src/AnalogFirmata.h | 24 + .../src/AnalogInputFirmata.cpp | 109 + .../src/AnalogInputFirmata.h | 42 + .../src/AnalogOutputFirmata.cpp | 65 + .../src/AnalogOutputFirmata.h | 38 + .../ConfigurableFirmata/src/AnalogWrite.h | 51 + .../src/ConfigurableFirmata.cpp | 710 ++++++ .../src/ConfigurableFirmata.h | 241 ++ .../src/DigitalInputFirmata.cpp | 127 + .../src/DigitalInputFirmata.h | 46 + .../src/DigitalOutputFirmata.cpp | 106 + .../src/DigitalOutputFirmata.h | 40 + .../ConfigurableFirmata/src/Encoder7Bit.cpp | 64 + .../ConfigurableFirmata/src/Encoder7Bit.h | 35 + .../src/EthernetClientStream.cpp | 3 + .../src/EthernetClientStream.h | 141 ++ .../ConfigurableFirmata/src/FirmataExt.cpp | 117 + .../ConfigurableFirmata/src/FirmataExt.h | 44 + .../ConfigurableFirmata/src/FirmataFeature.h | 31 + .../src/FirmataReporting.cpp | 66 + .../src/FirmataReporting.h | 43 + .../src/FirmataScheduler.cpp | 300 +++ .../src/FirmataScheduler.h | 73 + .../ConfigurableFirmata/src/I2CFirmata.cpp | 4 + .../ConfigurableFirmata/src/I2CFirmata.h | 333 +++ .../src/OneWireFirmata.cpp | 178 ++ .../ConfigurableFirmata/src/OneWireFirmata.h | 63 + .../ConfigurableFirmata/src/SerialFirmata.cpp | 360 +++ .../ConfigurableFirmata/src/SerialFirmata.h | 166 ++ .../ConfigurableFirmata/src/ServoFirmata.cpp | 4 + .../ConfigurableFirmata/src/ServoFirmata.h | 151 ++ .../src/StepperFirmata.cpp | 151 ++ .../ConfigurableFirmata/src/StepperFirmata.h | 40 + .../src/utility/AccelStepper.cpp | 651 +++++ .../src/utility/AccelStepper.h | 728 ++++++ .../src/utility/BLEStream.cpp | 3 + .../src/utility/BLEStream.h | 243 ++ .../ConfigurableFirmata/src/utility/Boards.h | 697 +++++ .../src/utility/FirmataStepper.cpp | 403 +++ .../src/utility/FirmataStepper.h | 145 ++ .../src/utility/MultiStepper.cpp | 73 + .../src/utility/MultiStepper.h | 78 + .../src/utility/OneWire.cpp | 567 +++++ .../ConfigurableFirmata/src/utility/OneWire.h | 367 +++ .../src/utility/WiFiClientStream.h | 105 + .../src/utility/WiFiServerStream.h | 107 + .../src/utility/WiFiStream.cpp | 4 + .../src/utility/WiFiStream.h | 226 ++ .../src/utility/firmataDebug.h | 14 + .../test/firmata_test/firmata_test.ino | 136 + libraries/ConfigurableFirmata/test/readme.md | 13 + .../test/stepper_test/stepper_test.ino | 91 + libraries/Firmata/Boards.h | 953 +++++++ libraries/Firmata/Firmata.cpp | 554 ++++ libraries/Firmata/Firmata.h | 180 ++ libraries/Firmata/FirmataConstants.h | 97 + libraries/Firmata/FirmataDefines.h | 283 +++ libraries/Firmata/FirmataMarshaller.cpp | 431 ++++ libraries/Firmata/FirmataMarshaller.h | 75 + libraries/Firmata/FirmataParser.cpp | 480 ++++ libraries/Firmata/FirmataParser.h | 105 + libraries/Firmata/LICENSE.txt | 458 ++++ .../AllInputsFirmata/AllInputsFirmata.ino | 90 + .../examples/AnalogFirmata/AnalogFirmata.ino | 94 + .../examples/EchoString/EchoString.ino | 44 + .../examples/OldStandardFirmata/LICENSE.txt | 458 ++++ .../OldStandardFirmata/OldStandardFirmata.ino | 239 ++ .../examples/ServoFirmata/ServoFirmata.ino | 65 + .../SimpleAnalogFirmata.ino | 46 + .../SimpleDigitalFirmata.ino | 72 + .../examples/StandardFirmata/LICENSE.txt | 458 ++++ .../StandardFirmata/StandardFirmata.ino | 823 ++++++ .../examples/StandardFirmataBLE/LICENSE.txt | 458 ++++ .../StandardFirmataBLE/StandardFirmataBLE.ino | 835 ++++++ .../examples/StandardFirmataBLE/bleConfig.h | 168 ++ .../StandardFirmataChipKIT/LICENSE.txt | 458 ++++ .../StandardFirmataChipKIT.ino | 802 ++++++ .../StandardFirmataEthernet/LICENSE.txt | 458 ++++ .../StandardFirmataEthernet.ino | 969 +++++++ .../StandardFirmataEthernet/ethernetConfig.h | 94 + .../examples/StandardFirmataPlus/LICENSE.txt | 458 ++++ .../StandardFirmataPlus.ino | 851 +++++++ .../examples/StandardFirmataWiFi/LICENSE.txt | 458 ++++ .../StandardFirmataWiFi.ino | 1048 ++++++++ .../examples/StandardFirmataWiFi/wifiConfig.h | 240 ++ libraries/Firmata/extras/revisions.txt | 265 ++ libraries/Firmata/keywords.txt | 90 + libraries/Firmata/library.properties | 9 + libraries/Firmata/readme.md | 194 ++ libraries/Firmata/release.sh | 33 + .../test/firmata_test/firmata_test.ino | 172 ++ libraries/Firmata/test/readme.md | 13 + libraries/Firmata/utility/BLEStream.cpp | 3 + libraries/Firmata/utility/BLEStream.h | 243 ++ .../utility/BluefruitLE_SPI_Stream.cpp | 3 + .../Firmata/utility/BluefruitLE_SPI_Stream.h | 157 ++ .../Firmata/utility/EthernetClientStream.cpp | 3 + .../Firmata/utility/EthernetClientStream.h | 141 ++ .../Firmata/utility/EthernetServerStream.cpp | 3 + .../Firmata/utility/EthernetServerStream.h | 147 ++ libraries/Firmata/utility/FirmataFeature.h | 38 + libraries/Firmata/utility/SerialFirmata.cpp | 342 +++ libraries/Firmata/utility/SerialFirmata.h | 208 ++ libraries/Firmata/utility/WiFiClientStream.h | 105 + libraries/Firmata/utility/WiFiServerStream.h | 107 + libraries/Firmata/utility/WiFiStream.cpp | 4 + libraries/Firmata/utility/WiFiStream.h | 226 ++ libraries/Firmata/utility/firmataDebug.h | 14 + .../ConfigurableFirmata.ino | 430 ++++ .../ConfigurableFirmataDeviceDriver.ino | 445 ++++ .../SelectedDeviceDrivers.h | 22 + .../ConfigurableFirmataWiFi.ino | 609 +++++ .../extras/LICENSE.txt | 458 ++++ .../extras/v0.9-release-notes.md | 48 + .../FirmataWithDeviceFeature/keywords.txt | 96 + .../library.properties | 9 + libraries/FirmataWithDeviceFeature/readme.md | 74 + .../src-features/AnalogFirmata.cpp | 32 + .../src-features/AnalogFirmata.h | 24 + .../src-features/AnalogInputFirmata.cpp | 109 + .../src-features/AnalogInputFirmata.h | 42 + .../src-features/AnalogOutputFirmata.cpp | 65 + .../src-features/AnalogOutputFirmata.h | 38 + .../src-features/AnalogWrite.h | 51 + .../src-features/DigitalInputFirmata.cpp | 127 + .../src-features/DigitalInputFirmata.h | 46 + .../src-features/DigitalOutputFirmata.cpp | 106 + .../src-features/DigitalOutputFirmata.h | 40 + .../src-features/Encoder7Bit.cpp | 64 + .../src-features/Encoder7Bit.h | 35 + .../src-features/EthernetClientStream.cpp | 103 + .../src-features/EthernetClientStream.h | 50 + .../src-features/FirmataReporting.cpp | 66 + .../src-features/FirmataReporting.h | 43 + .../src-features/FirmataScheduler.cpp | 300 +++ .../src-features/FirmataScheduler.h | 73 + .../src-features/I2CFirmata.cpp | 4 + .../src-features/I2CFirmata.h | 333 +++ .../src-features/OneWireFirmata.cpp | 178 ++ .../src-features/OneWireFirmata.h | 63 + .../src-features/SerialFirmata.cpp | 358 +++ .../src-features/SerialFirmata.h | 163 ++ .../src-features/ServoFirmata.cpp | 4 + .../src-features/ServoFirmata.h | 151 ++ .../src-features/StepperFirmata.cpp | 151 ++ .../src-features/StepperFirmata.h | 40 + .../src-features/utility/FirmataStepper.cpp | 448 ++++ .../src-features/utility/FirmataStepper.h | 145 ++ .../src-features/utility/OneWire.cpp | 567 +++++ .../src-features/utility/OneWire.h | 367 +++ .../src-features/utility/WiFi101Stream.cpp | 4 + .../src-features/utility/WiFi101Stream.h | 259 ++ .../src-features/utility/WiFiStream.cpp | 4 + .../src-features/utility/WiFiStream.h | 258 ++ .../src-features/utility/firmataDebug.h | 14 + .../src/ConfigurableFirmata.cpp | 709 ++++++ .../src/ConfigurableFirmata.h | 245 ++ .../src/DeviceFirmata.cpp | 264 ++ .../src/DeviceFirmata.h | 41 + .../src/FirmataExt.cpp | 117 + .../FirmataWithDeviceFeature/src/FirmataExt.h | 44 + .../src/FirmataFeature.h | 31 + .../src/utility/Boards.h | 610 +++++ .../test/firmata_test/firmata_test.ino | 136 + .../FirmataWithDeviceFeature/test/readme.md | 13 + libraries/NeoPixelBus_by_Makuna/COPYING | 165 ++ libraries/NeoPixelBus_by_Makuna/ReadMe.md | 34 + .../examples/DotStarTest/DotStarTest.ino | 88 + .../NeoPixelBrightness/NeoPixelBrightness.ino | 83 + .../examples/NeoPixelGamma/NeoPixelGamma.ino | 95 + .../examples/NeoPixelTest/NeoPixelTest.ino | 140 ++ .../NeoPixelAnimation/NeoPixelAnimation.ino | 225 ++ .../NeoPixelCylon/NeoPixelCylon.ino | 123 + .../NeoPixelFunFadeInOut.ino | 132 + .../NeoPixelFunLoop/NeoPixelFunLoop.ino | 141 ++ .../NeoPixelFunRandomChange.ino | 116 + .../NeoPixelRotateLoop/NeoPixelRotateLoop.ino | 97 + .../bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino | 98 + .../bitmaps/NeoPixelBitmap/Strings.bmp | Bin 0 -> 62262 bytes .../bitmaps/NeoPixelBitmap/StringsW.bmp | Bin 0 -> 82998 bytes .../bitmaps/NeoPixelBufferCylon/Cylon.pdn | Bin 0 -> 3953 bytes .../bitmaps/NeoPixelBufferCylon/CylonGrb.h | 30 + .../bitmaps/NeoPixelBufferCylon/CylonGrbw.h | 30 + .../NeoPixelBufferCylon.ino | 74 + .../NeoPixelBufferShader.ino | 167 ++ .../NeoPixelDibTest/NeoPixelDibTest.ino | 158 ++ .../NeoPixelMosaicDump/NeoPixelMosaicDump.ino | 98 + .../NeoPixelMosaicTest/NeoPixelMosaicTest.ino | 97 + .../NeoPixelRingTopologyTest.ino | 100 + .../NeoPixelTilesDump/NeoPixelTilesDump.ino | 102 + .../NeoPixelTilesTest/NeoPixelTilesTest.ino | 103 + .../NeoPixelTopologyDump.ino | 78 + .../NeoPixelTopologyTest.ino | 92 + libraries/NeoPixelBus_by_Makuna/keywords.txt | 184 ++ libraries/NeoPixelBus_by_Makuna/library.json | 15 + .../NeoPixelBus_by_Makuna/library.properties | 9 + .../src/NeoPixelAnimator.h | 174 ++ .../src/NeoPixelBrightnessBus.h | 161 ++ .../NeoPixelBus_by_Makuna/src/NeoPixelBus.h | 401 +++ .../src/internal/DotStarAvrMethod.h | 153 ++ .../src/internal/DotStarColorFeatures.h | 326 +++ .../src/internal/DotStarGenericMethod.h | 128 + .../src/internal/DotStarSpiMethod.h | 125 + .../src/internal/Esp32_i2s.c | 522 ++++ .../src/internal/Esp32_i2s.h | 38 + .../src/internal/HsbColor.cpp | 68 + .../src/internal/HsbColor.h | 113 + .../src/internal/HslColor.cpp | 72 + .../src/internal/HslColor.h | 113 + .../src/internal/HtmlColor.cpp | 58 + .../src/internal/HtmlColor.h | 340 +++ .../src/internal/HtmlColorNameStrings.cpp | 178 ++ .../src/internal/HtmlColorNameStrings.h | 180 ++ .../src/internal/HtmlColorNames.cpp | 188 ++ .../src/internal/HtmlColorShortNames.cpp | 59 + .../src/internal/Layouts.h | 426 ++++ .../src/internal/NeoArmMethod.h | 697 +++++ .../src/internal/NeoAvrMethod.h | 200 ++ .../src/internal/NeoBitmapFile.h | 351 +++ .../src/internal/NeoBuffer.h | 154 ++ .../src/internal/NeoBufferContext.h | 48 + .../src/internal/NeoBufferMethods.h | 263 ++ .../src/internal/NeoColorFeatures.h | 389 +++ .../src/internal/NeoDib.h | 161 ++ .../src/internal/NeoEase.h | 224 ++ .../src/internal/NeoEsp32I2sMethod.h | 189 ++ .../src/internal/NeoEsp8266DmaMethod.h | 382 +++ .../src/internal/NeoEsp8266UartMethod.cpp | 216 ++ .../src/internal/NeoEsp8266UartMethod.h | 178 ++ .../src/internal/NeoEspBitBangMethod.h | 173 ++ .../src/internal/NeoGamma.cpp | 50 + .../src/internal/NeoGamma.h | 73 + .../src/internal/NeoHueBlend.h | 118 + .../src/internal/NeoMosaic.h | 191 ++ .../src/internal/NeoPixelAnimator.cpp | 166 ++ .../src/internal/NeoPixelAvr.c | 648 +++++ .../src/internal/NeoPixelEsp.c | 151 ++ .../src/internal/NeoRingTopology.h | 89 + .../src/internal/NeoSpriteSheet.h | 163 ++ .../src/internal/NeoTiles.h | 158 ++ .../src/internal/NeoTopology.h | 91 + .../src/internal/RgbColor.cpp | 249 ++ .../src/internal/RgbColor.h | 148 ++ .../src/internal/RgbwColor.cpp | 178 ++ .../src/internal/RgbwColor.h | 178 ++ libraries/PubSubClient/CHANGES.txt | 68 + libraries/PubSubClient/LICENSE.txt | 20 + libraries/PubSubClient/README.md | 47 + .../examples/mqtt_auth/mqtt_auth.ino | 43 + .../examples/mqtt_basic/mqtt_basic.ino | 77 + .../examples/mqtt_esp8266/mqtt_esp8266.ino | 126 + .../mqtt_publish_in_callback.ino | 60 + .../mqtt_reconnect_nonblocking.ino | 67 + .../examples/mqtt_stream/mqtt_stream.ino | 57 + libraries/PubSubClient/keywords.txt | 30 + libraries/PubSubClient/library.json | 17 + libraries/PubSubClient/library.properties | 9 + libraries/PubSubClient/src/PubSubClient.cpp | 590 +++++ libraries/PubSubClient/src/PubSubClient.h | 144 ++ libraries/PubSubClient/tests/.gitignore | 4 + libraries/PubSubClient/tests/Makefile | 25 + libraries/PubSubClient/tests/README.md | 93 + .../PubSubClient/tests/src/connect_spec.cpp | 256 ++ .../PubSubClient/tests/src/keepalive_spec.cpp | 185 ++ .../PubSubClient/tests/src/lib/Arduino.h | 23 + .../PubSubClient/tests/src/lib/BDDTest.cpp | 50 + .../PubSubClient/tests/src/lib/BDDTest.h | 23 + .../PubSubClient/tests/src/lib/Buffer.cpp | 30 + libraries/PubSubClient/tests/src/lib/Buffer.h | 23 + libraries/PubSubClient/tests/src/lib/Client.h | 21 + .../PubSubClient/tests/src/lib/IPAddress.cpp | 44 + .../PubSubClient/tests/src/lib/IPAddress.h | 72 + .../PubSubClient/tests/src/lib/ShimClient.cpp | 153 ++ .../PubSubClient/tests/src/lib/ShimClient.h | 51 + .../PubSubClient/tests/src/lib/Stream.cpp | 39 + libraries/PubSubClient/tests/src/lib/Stream.h | 22 + libraries/PubSubClient/tests/src/lib/trace.h | 10 + .../PubSubClient/tests/src/publish_spec.cpp | 190 ++ .../PubSubClient/tests/src/receive_spec.cpp | 249 ++ .../PubSubClient/tests/src/subscribe_spec.cpp | 177 ++ .../PubSubClient/tests/testcases/__init__.py | 0 .../tests/testcases/mqtt_basic.py | 43 + .../testcases/mqtt_publish_in_callback.py | 64 + .../PubSubClient/tests/testcases/settings.py | 2 + libraries/PubSubClient/tests/testsuite.py | 179 ++ libraries/Servo/README.adoc | 25 + libraries/Servo/examples/Knob/Knob.ino | 27 + libraries/Servo/examples/Sweep/Sweep.ino | 32 + libraries/Servo/keywords.txt | 24 + libraries/Servo/library.properties | 9 + libraries/Servo/src/Servo.h | 119 + libraries/Servo/src/avr/Servo.cpp | 318 +++ libraries/Servo/src/avr/ServoTimers.h | 59 + libraries/Servo/src/nrf52/Servo.cpp | 134 + libraries/Servo/src/nrf52/ServoTimers.h | 38 + libraries/Servo/src/sam/Servo.cpp | 283 +++ libraries/Servo/src/sam/ServoTimers.h | 88 + libraries/Servo/src/samd/Servo.cpp | 297 +++ libraries/Servo/src/samd/ServoTimers.h | 71 + libraries/Servo/src/stm32f4/Servo.cpp | 194 ++ libraries/Servo/src/stm32f4/ServoTimers.h | 207 ++ libraries/readme.txt | 1 + mqqt-neopixel/mqqt-neo/mqqt-neo.ino | 167 ++ mqqt-test/mqqtTest/mqqtTest.ino | 68 + servo/servo.ino | 85 + wifi/sketch_aug02a/sketch_aug02a.ino | 41 + 368 files changed, 65878 insertions(+) create mode 100644 electricity/measure/measure.ino create mode 100644 libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata.cpp create mode 100644 libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata.h create mode 100644 libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata_Boards.h create mode 100644 libraries/Adafruit_BLEFirmata/LICENSE.txt create mode 100644 libraries/Adafruit_BLEFirmata/README.md create mode 100644 libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitConfig.h create mode 100644 libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitLE_nrf51822.ino create mode 100644 libraries/Adafruit_BLEFirmata/examples/CircuitPlayground_nrf51822/BluefruitConfig.h create mode 100644 libraries/Adafruit_BLEFirmata/examples/CircuitPlayground_nrf51822/CircuitPlayground_nrf51822.ino create mode 100644 libraries/Adafruit_BLEFirmata/examples/Firmata_nRF8001/BluefruitConfig.h create mode 100644 libraries/Adafruit_BLEFirmata/examples/Firmata_nRF8001/Firmata_nRF8001.ino create mode 100644 libraries/Adafruit_BLEFirmata/examples/StandardFirmata/LICENSE.txt create mode 100644 libraries/Adafruit_BLEFirmata/examples/StandardFirmata/Makefile create mode 100644 libraries/Adafruit_BLEFirmata/examples/StandardFirmata/StandardFirmata.ino create mode 100644 libraries/Adafruit_BLEFirmata/keywords.txt create mode 100644 libraries/Adafruit_BLEFirmata/library.properties create mode 100644 libraries/Adafruit_ESP8266/Adafruit_ESP8266.cpp create mode 100644 libraries/Adafruit_ESP8266/Adafruit_ESP8266.h create mode 100644 libraries/Adafruit_ESP8266/README.md create mode 100644 libraries/Adafruit_ESP8266/examples/webclient/webclient.ino create mode 100644 libraries/Adafruit_ESP8266/library.properties create mode 100644 libraries/Adafruit_ESP8266/previous/Adafruit_ESP8266.ino create mode 100644 libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp create mode 100644 libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h create mode 100644 libraries/Adafruit_NeoPixel/COPYING create mode 100644 libraries/Adafruit_NeoPixel/README.md create mode 100644 libraries/Adafruit_NeoPixel/esp8266.c create mode 100644 libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.esp8266.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.trinket.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/RGBWstrandtest.ino create mode 100644 libraries/Adafruit_NeoPixel/examples/StrandtestBLE/.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp create mode 100644 libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.h create mode 100644 libraries/Adafruit_NeoPixel/examples/StrandtestBLE/StrandtestBLE.ino create mode 100644 libraries/Adafruit_NeoPixel/examples/buttoncycler/.esp8266.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/buttoncycler/buttoncycler.ino create mode 100644 libraries/Adafruit_NeoPixel/examples/simple/.esp8266.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/simple/simple.ino create mode 100644 libraries/Adafruit_NeoPixel/examples/strandtest/.esp8266.test.skip create mode 100644 libraries/Adafruit_NeoPixel/examples/strandtest/strandtest.ino create mode 100644 libraries/Adafruit_NeoPixel/keywords.txt create mode 100644 libraries/Adafruit_NeoPixel/library.properties create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.cpp create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.h create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/README.md create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/examples/gpiotest/gpiotest.ino create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/examples/pwmtest/pwmtest.ino create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/examples/servo/servo.ino create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/library.properties create mode 100644 libraries/Adafruit_PWM_Servo_Driver_Library/license.txt create mode 100644 libraries/ConfigurableFirmata/examples/ConfigurableFirmata/ConfigurableFirmata.ino create mode 100644 libraries/ConfigurableFirmata/examples/ConfigurableFirmataBLE/ConfigurableFirmataBLE.ino create mode 100644 libraries/ConfigurableFirmata/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino create mode 100644 libraries/ConfigurableFirmata/extras/LICENSE.txt create mode 100644 libraries/ConfigurableFirmata/keywords.txt create mode 100644 libraries/ConfigurableFirmata/library.json create mode 100644 libraries/ConfigurableFirmata/library.properties create mode 100644 libraries/ConfigurableFirmata/readme.md create mode 100644 libraries/ConfigurableFirmata/src/AccelStepperFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/AccelStepperFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/AnalogFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/AnalogFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/AnalogInputFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/AnalogInputFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/AnalogOutputFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/AnalogOutputFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/AnalogWrite.h create mode 100644 libraries/ConfigurableFirmata/src/ConfigurableFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/ConfigurableFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/DigitalInputFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/DigitalInputFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/DigitalOutputFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/DigitalOutputFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/Encoder7Bit.cpp create mode 100644 libraries/ConfigurableFirmata/src/Encoder7Bit.h create mode 100644 libraries/ConfigurableFirmata/src/EthernetClientStream.cpp create mode 100644 libraries/ConfigurableFirmata/src/EthernetClientStream.h create mode 100644 libraries/ConfigurableFirmata/src/FirmataExt.cpp create mode 100644 libraries/ConfigurableFirmata/src/FirmataExt.h create mode 100644 libraries/ConfigurableFirmata/src/FirmataFeature.h create mode 100644 libraries/ConfigurableFirmata/src/FirmataReporting.cpp create mode 100644 libraries/ConfigurableFirmata/src/FirmataReporting.h create mode 100644 libraries/ConfigurableFirmata/src/FirmataScheduler.cpp create mode 100644 libraries/ConfigurableFirmata/src/FirmataScheduler.h create mode 100644 libraries/ConfigurableFirmata/src/I2CFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/I2CFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/OneWireFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/OneWireFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/SerialFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/SerialFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/ServoFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/ServoFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/StepperFirmata.cpp create mode 100644 libraries/ConfigurableFirmata/src/StepperFirmata.h create mode 100644 libraries/ConfigurableFirmata/src/utility/AccelStepper.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/AccelStepper.h create mode 100644 libraries/ConfigurableFirmata/src/utility/BLEStream.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/BLEStream.h create mode 100644 libraries/ConfigurableFirmata/src/utility/Boards.h create mode 100644 libraries/ConfigurableFirmata/src/utility/FirmataStepper.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/FirmataStepper.h create mode 100644 libraries/ConfigurableFirmata/src/utility/MultiStepper.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/MultiStepper.h create mode 100644 libraries/ConfigurableFirmata/src/utility/OneWire.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/OneWire.h create mode 100644 libraries/ConfigurableFirmata/src/utility/WiFiClientStream.h create mode 100644 libraries/ConfigurableFirmata/src/utility/WiFiServerStream.h create mode 100644 libraries/ConfigurableFirmata/src/utility/WiFiStream.cpp create mode 100644 libraries/ConfigurableFirmata/src/utility/WiFiStream.h create mode 100644 libraries/ConfigurableFirmata/src/utility/firmataDebug.h create mode 100644 libraries/ConfigurableFirmata/test/firmata_test/firmata_test.ino create mode 100644 libraries/ConfigurableFirmata/test/readme.md create mode 100644 libraries/ConfigurableFirmata/test/stepper_test/stepper_test.ino create mode 100644 libraries/Firmata/Boards.h create mode 100644 libraries/Firmata/Firmata.cpp create mode 100644 libraries/Firmata/Firmata.h create mode 100644 libraries/Firmata/FirmataConstants.h create mode 100644 libraries/Firmata/FirmataDefines.h create mode 100644 libraries/Firmata/FirmataMarshaller.cpp create mode 100644 libraries/Firmata/FirmataMarshaller.h create mode 100644 libraries/Firmata/FirmataParser.cpp create mode 100644 libraries/Firmata/FirmataParser.h create mode 100644 libraries/Firmata/LICENSE.txt create mode 100644 libraries/Firmata/examples/AllInputsFirmata/AllInputsFirmata.ino create mode 100644 libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.ino create mode 100644 libraries/Firmata/examples/EchoString/EchoString.ino create mode 100644 libraries/Firmata/examples/OldStandardFirmata/LICENSE.txt create mode 100644 libraries/Firmata/examples/OldStandardFirmata/OldStandardFirmata.ino create mode 100644 libraries/Firmata/examples/ServoFirmata/ServoFirmata.ino create mode 100644 libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.ino create mode 100644 libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.ino create mode 100644 libraries/Firmata/examples/StandardFirmata/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino create mode 100644 libraries/Firmata/examples/StandardFirmataBLE/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmataBLE/StandardFirmataBLE.ino create mode 100644 libraries/Firmata/examples/StandardFirmataBLE/bleConfig.h create mode 100644 libraries/Firmata/examples/StandardFirmataChipKIT/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino create mode 100644 libraries/Firmata/examples/StandardFirmataEthernet/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino create mode 100644 libraries/Firmata/examples/StandardFirmataEthernet/ethernetConfig.h create mode 100644 libraries/Firmata/examples/StandardFirmataPlus/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmataPlus/StandardFirmataPlus.ino create mode 100644 libraries/Firmata/examples/StandardFirmataWiFi/LICENSE.txt create mode 100644 libraries/Firmata/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino create mode 100644 libraries/Firmata/examples/StandardFirmataWiFi/wifiConfig.h create mode 100644 libraries/Firmata/extras/revisions.txt create mode 100644 libraries/Firmata/keywords.txt create mode 100644 libraries/Firmata/library.properties create mode 100644 libraries/Firmata/readme.md create mode 100644 libraries/Firmata/release.sh create mode 100644 libraries/Firmata/test/firmata_test/firmata_test.ino create mode 100644 libraries/Firmata/test/readme.md create mode 100644 libraries/Firmata/utility/BLEStream.cpp create mode 100644 libraries/Firmata/utility/BLEStream.h create mode 100644 libraries/Firmata/utility/BluefruitLE_SPI_Stream.cpp create mode 100644 libraries/Firmata/utility/BluefruitLE_SPI_Stream.h create mode 100644 libraries/Firmata/utility/EthernetClientStream.cpp create mode 100644 libraries/Firmata/utility/EthernetClientStream.h create mode 100644 libraries/Firmata/utility/EthernetServerStream.cpp create mode 100644 libraries/Firmata/utility/EthernetServerStream.h create mode 100644 libraries/Firmata/utility/FirmataFeature.h create mode 100644 libraries/Firmata/utility/SerialFirmata.cpp create mode 100644 libraries/Firmata/utility/SerialFirmata.h create mode 100644 libraries/Firmata/utility/WiFiClientStream.h create mode 100644 libraries/Firmata/utility/WiFiServerStream.h create mode 100644 libraries/Firmata/utility/WiFiStream.cpp create mode 100644 libraries/Firmata/utility/WiFiStream.h create mode 100644 libraries/Firmata/utility/firmataDebug.h create mode 100644 libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmata/ConfigurableFirmata.ino create mode 100644 libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/ConfigurableFirmataDeviceDriver.ino create mode 100644 libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/SelectedDeviceDrivers.h create mode 100644 libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino create mode 100644 libraries/FirmataWithDeviceFeature/extras/LICENSE.txt create mode 100644 libraries/FirmataWithDeviceFeature/extras/v0.9-release-notes.md create mode 100644 libraries/FirmataWithDeviceFeature/keywords.txt create mode 100644 libraries/FirmataWithDeviceFeature/library.properties create mode 100644 libraries/FirmataWithDeviceFeature/readme.md create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/AnalogWrite.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.h create mode 100644 libraries/FirmataWithDeviceFeature/src-features/utility/firmataDebug.h create mode 100644 libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src/DeviceFirmata.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src/DeviceFirmata.h create mode 100644 libraries/FirmataWithDeviceFeature/src/FirmataExt.cpp create mode 100644 libraries/FirmataWithDeviceFeature/src/FirmataExt.h create mode 100644 libraries/FirmataWithDeviceFeature/src/FirmataFeature.h create mode 100644 libraries/FirmataWithDeviceFeature/src/utility/Boards.h create mode 100644 libraries/FirmataWithDeviceFeature/test/firmata_test/firmata_test.ino create mode 100644 libraries/FirmataWithDeviceFeature/test/readme.md create mode 100644 libraries/NeoPixelBus_by_Makuna/COPYING create mode 100644 libraries/NeoPixelBus_by_Makuna/ReadMe.md create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/DotStarTest/DotStarTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/NeoPixelBrightness/NeoPixelBrightness.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/NeoPixelGamma/NeoPixelGamma.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/NeoPixelTest/NeoPixelTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelCylon/NeoPixelCylon.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/Strings.bmp create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/StringsW.bmp create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/Cylon.pdn create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino create mode 100644 libraries/NeoPixelBus_by_Makuna/keywords.txt create mode 100644 libraries/NeoPixelBus_by_Makuna/library.json create mode 100644 libraries/NeoPixelBus_by_Makuna/library.properties create mode 100644 libraries/NeoPixelBus_by_Makuna/src/NeoPixelAnimator.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/NeoPixelBrightnessBus.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/NeoPixelBus.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/DotStarAvrMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/DotStarColorFeatures.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/DotStarGenericMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/DotStarSpiMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.c create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNames.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorShortNames.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/Layouts.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoArmMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoAvrMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoBitmapFile.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoBuffer.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferContext.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferMethods.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoColorFeatures.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoDib.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEase.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp32I2sMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266DmaMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoEspBitBangMethod.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoHueBlend.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoMosaic.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAnimator.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAvr.c create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelEsp.c create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoRingTopology.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoSpriteSheet.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoTiles.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/NeoTopology.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.h create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.cpp create mode 100644 libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.h create mode 100644 libraries/PubSubClient/CHANGES.txt create mode 100644 libraries/PubSubClient/LICENSE.txt create mode 100644 libraries/PubSubClient/README.md create mode 100644 libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino create mode 100644 libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino create mode 100644 libraries/PubSubClient/examples/mqtt_esp8266/mqtt_esp8266.ino create mode 100644 libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino create mode 100644 libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino create mode 100644 libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino create mode 100644 libraries/PubSubClient/keywords.txt create mode 100644 libraries/PubSubClient/library.json create mode 100644 libraries/PubSubClient/library.properties create mode 100644 libraries/PubSubClient/src/PubSubClient.cpp create mode 100644 libraries/PubSubClient/src/PubSubClient.h create mode 100644 libraries/PubSubClient/tests/.gitignore create mode 100644 libraries/PubSubClient/tests/Makefile create mode 100644 libraries/PubSubClient/tests/README.md create mode 100644 libraries/PubSubClient/tests/src/connect_spec.cpp create mode 100644 libraries/PubSubClient/tests/src/keepalive_spec.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/Arduino.h create mode 100644 libraries/PubSubClient/tests/src/lib/BDDTest.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/BDDTest.h create mode 100644 libraries/PubSubClient/tests/src/lib/Buffer.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/Buffer.h create mode 100644 libraries/PubSubClient/tests/src/lib/Client.h create mode 100644 libraries/PubSubClient/tests/src/lib/IPAddress.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/IPAddress.h create mode 100644 libraries/PubSubClient/tests/src/lib/ShimClient.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/ShimClient.h create mode 100644 libraries/PubSubClient/tests/src/lib/Stream.cpp create mode 100644 libraries/PubSubClient/tests/src/lib/Stream.h create mode 100644 libraries/PubSubClient/tests/src/lib/trace.h create mode 100644 libraries/PubSubClient/tests/src/publish_spec.cpp create mode 100644 libraries/PubSubClient/tests/src/receive_spec.cpp create mode 100644 libraries/PubSubClient/tests/src/subscribe_spec.cpp create mode 100644 libraries/PubSubClient/tests/testcases/__init__.py create mode 100644 libraries/PubSubClient/tests/testcases/mqtt_basic.py create mode 100644 libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py create mode 100644 libraries/PubSubClient/tests/testcases/settings.py create mode 100644 libraries/PubSubClient/tests/testsuite.py create mode 100644 libraries/Servo/README.adoc create mode 100644 libraries/Servo/examples/Knob/Knob.ino create mode 100644 libraries/Servo/examples/Sweep/Sweep.ino create mode 100644 libraries/Servo/keywords.txt create mode 100644 libraries/Servo/library.properties create mode 100644 libraries/Servo/src/Servo.h create mode 100644 libraries/Servo/src/avr/Servo.cpp create mode 100644 libraries/Servo/src/avr/ServoTimers.h create mode 100644 libraries/Servo/src/nrf52/Servo.cpp create mode 100644 libraries/Servo/src/nrf52/ServoTimers.h create mode 100644 libraries/Servo/src/sam/Servo.cpp create mode 100644 libraries/Servo/src/sam/ServoTimers.h create mode 100644 libraries/Servo/src/samd/Servo.cpp create mode 100644 libraries/Servo/src/samd/ServoTimers.h create mode 100644 libraries/Servo/src/stm32f4/Servo.cpp create mode 100644 libraries/Servo/src/stm32f4/ServoTimers.h create mode 100644 libraries/readme.txt create mode 100644 mqqt-neopixel/mqqt-neo/mqqt-neo.ino create mode 100644 mqqt-test/mqqtTest/mqqtTest.ino create mode 100644 servo/servo.ino create mode 100644 wifi/sketch_aug02a/sketch_aug02a.ino diff --git a/electricity/measure/measure.ino b/electricity/measure/measure.ino new file mode 100644 index 0000000..b181907 --- /dev/null +++ b/electricity/measure/measure.ino @@ -0,0 +1,33 @@ +/* +Into Robotics +*/ + +const int lowerThreshold = 250; +const int upperThreshold = 450; + +const uint8_t analogPin = A0; + +void setup() +{ + Serial.begin(9600); + pinMode(analogPin, INPUT); +} + +void loop() { + static bool state = LOW; + static unsigned int counter = 0; + int analogValue = analogRead(analogPin); + if (state == HIGH) { + if (analogValue < lowerThreshold) { + state = LOW; + counter++; + Serial.println(counter); + Serial.println("blykst"); + } + } else { // state == LOW + if (analogValue > upperThreshold) { + state = HIGH; + } + } +} + diff --git a/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata.cpp b/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata.cpp new file mode 100644 index 0000000..373c4d9 --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata.cpp @@ -0,0 +1,509 @@ +/* + Firmata.cpp - Firmata library + Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved. + + Modified for Adafruit_BLE_Uart by Limor Fried/Kevin Townsend for + Adafruit Industries, 2014 + + 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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "Adafruit_BLE_Firmata.h" + +extern "C" { +#include +#include +} + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +void Adafruit_BLE_FirmataClass::sendValueAsTwo7bitBytes(int value) +{ + FirmataSerial.write(value & B01111111); // LSB + FirmataSerial.write(value >> 7 & B01111111); // MSB +} + +void Adafruit_BLE_FirmataClass::startSysex(void) +{ + FirmataSerial.write(START_SYSEX); +} + +void Adafruit_BLE_FirmataClass::endSysex(void) +{ + FirmataSerial.write(END_SYSEX); +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +Adafruit_BLE_FirmataClass::Adafruit_BLE_FirmataClass(Stream &s) : FirmataSerial(s) +{ + firmwareVersionCount = 0; + systemReset(); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/* begin method for overriding default serial bitrate */ +void Adafruit_BLE_FirmataClass::begin(void) +{ + printVersion(); + printFirmwareVersion(); +} + +void Adafruit_BLE_FirmataClass::begin(Stream &s) +{ + FirmataSerial = s; + systemReset(); + printVersion(); + printFirmwareVersion(); +} + +// output the protocol version message to the serial port +void Adafruit_BLE_FirmataClass::printVersion(void) { + FirmataSerial.write(REPORT_VERSION); + FirmataSerial.write(FIRMATA_MAJOR_VERSION); + FirmataSerial.write(FIRMATA_MINOR_VERSION); +} + +void Adafruit_BLE_FirmataClass::printFirmwareVersion(void) +{ + byte i; + + if(firmwareVersionCount) { // make sure that the name has been set before reporting + startSysex(); + FirmataSerial.write(REPORT_FIRMWARE); + FirmataSerial.write(firmwareVersionVector[0]); // major version number + FirmataSerial.write(firmwareVersionVector[1]); // minor version number + for(i=2; i 0) && (inputData < 128) ) { + waitForData--; + storedInputData[waitForData] = inputData; +#ifdef BLE_DEBUG + Serial.print(F(" 0x")); Serial.print(inputData, HEX); +#endif + + if( (waitForData==0) && executeMultiByteCommand ) { // got the whole message + +#ifdef BLE_DEBUG + Serial.println(); +#endif + + + switch(executeMultiByteCommand) { + case ANALOG_MESSAGE: + if(currentAnalogCallback) { + (*currentAnalogCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case DIGITAL_MESSAGE: + if(currentDigitalCallback) { + (*currentDigitalCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case SET_PIN_MODE: + if(currentPinModeCallback) + (*currentPinModeCallback)(storedInputData[1], storedInputData[0]); + break; + case REPORT_ANALOG: + if(currentReportAnalogCallback) + (*currentReportAnalogCallback)(multiByteChannel,storedInputData[0]); + break; + case REPORT_DIGITAL: + if(currentReportDigitalCallback) + (*currentReportDigitalCallback)(multiByteChannel,storedInputData[0]); + break; + } + executeMultiByteCommand = 0; + } + } else { +#ifdef BLE_DEBUG + Serial.print(F("\tReceived 0x")); Serial.print(inputData, HEX); +#endif + // remove channel info from command byte if less than 0xF0 + if(inputData < 0xF0) { + command = inputData & 0xF0; + multiByteChannel = inputData & 0x0F; + } else { + command = inputData; + // commands in the 0xF* range don't use channel data + } + switch (command) { + case ANALOG_MESSAGE: + case DIGITAL_MESSAGE: + case SET_PIN_MODE: + waitForData = 2; // two data bytes needed + executeMultiByteCommand = command; + break; + case REPORT_ANALOG: + case REPORT_DIGITAL: + waitForData = 1; // two data bytes needed + executeMultiByteCommand = command; + break; + case START_SYSEX: + parsingSysex = true; + sysexBytesRead = 0; + break; + case SYSTEM_RESET: + systemReset(); + break; + case REPORT_VERSION: + printVersion(); + break; + } + } + + return inputData; +} + +//------------------------------------------------------------------------------ +// Serial Send Handling + +// send an analog message +void Adafruit_BLE_FirmataClass::sendAnalog(byte pin, int value) +{ + // create a three byte buffer + uint8_t sendbuffer[3]; + + // pin can only be 0-15, so chop higher bits + //FirmataSerial.write(ANALOG_MESSAGE | (pin & 0xF)); + sendbuffer[0] = ANALOG_MESSAGE | (pin & 0xF); + + //sendValueAsTwo7bitBytes(value); + sendbuffer[1] = value % 128; // Tx bits 0-6 + sendbuffer[2] = (value >> 7) &0x7F; // Tx bits 7-13 + + FirmataSerial.write(sendbuffer, 3); +} + +// send a single digital pin in a digital message +void Adafruit_BLE_FirmataClass::sendDigital(byte pin, int value) +{ + /* TODO add single pin digital messages to the protocol, this needs to + * track the last digital data sent so that it can be sure to change just + * one bit in the packet. This is complicated by the fact that the + * numbering of the pins will probably differ on Arduino, Wiring, and + * other boards. The DIGITAL_MESSAGE sends 14 bits at a time, but it is + * probably easier to send 8 bit ports for any board with more than 14 + * digital pins. + */ + + // TODO: the digital message should not be sent on the serial port every + // time sendDigital() is called. Instead, it should add it to an int + // which will be sent on a schedule. If a pin changes more than once + // before the digital message is sent on the serial port, it should send a + // digital message for each change. + + // if(value == 0) + // sendDigitalPortPair(); +} + + +// send 14-bits in a single digital message (protocol v1) +// send an 8-bit port in a single digital message (protocol v2) +void Adafruit_BLE_FirmataClass::sendDigitalPort(byte portNumber, int portData) +{ + // create a three byte buffer + uint8_t sendbuffer[3]; + + sendbuffer[0] = DIGITAL_MESSAGE | (portNumber & 0xF); + sendbuffer[1] = (byte)portData % 128; // Tx bits 0-6 + sendbuffer[2] = portData >> 7; // Tx bits 7-13 + FirmataSerial.write(sendbuffer, 3); +} + + +void Adafruit_BLE_FirmataClass::sendSysex(byte command, byte bytec, byte* bytev) +{ + byte i; + startSysex(); + FirmataSerial.write(command); + for(i=0; i + +//#include "Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */ + +//#define BLE_DEBUG + +// move the following defines to Firmata.h? +#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 MAX_QUERIES 8 +#define MINIMUM_SAMPLING_INTERVAL 10 + +#define REGISTER_NOT_SPECIFIED -1 + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. This number can be queried so that host + * software can test whether it will be compatible with the currently + * installed firmware. */ +#define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_MINOR_VERSION 3 // for backwards compatible changes +#define FIRMATA_BUGFIX_VERSION 1 // for bugfix releases + +#define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages + +// message command bytes (128-255/0x80-0xFF) +#define DIGITAL_MESSAGE 0x90 // send data for a digital pin +#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) +#define REPORT_ANALOG 0xC0 // enable analog input by pin # +#define REPORT_DIGITAL 0xD0 // enable digital input by port pair +// +#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc +// +#define REPORT_VERSION 0xF9 // report protocol version +#define SYSTEM_RESET 0xFF // reset from MIDI +// +#define START_SYSEX 0xF0 // start a MIDI Sysex message +#define END_SYSEX 0xF7 // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ +#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq +#define STRING_DATA 0x71 // a string message with 14-bits per char +#define SHIFT_DATA 0x75 // a bitstream to/from a shift register +#define I2C_REQUEST 0x76 // send an I2C read/write request +#define I2C_REPLY 0x77 // a reply to an I2C read request +#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins +#define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin +#define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value +#define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value +#define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins +#define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution +#define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers +#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info +#define REPORT_FIRMWARE 0x79 // report name and version of the firmware +#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop +#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages +#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages +// these are DEPRECATED to make the naming more consistent +#define FIRMATA_STRING 0x71 // same as STRING_DATA +#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST +#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY +#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL + +// pin modes +//#define INPUT 0x00 // defined in wiring.h +//#define OUTPUT 0x01 // defined in wiring.h +#define ANALOG 0x02 // analog pin in analogInput mode +#define PWM 0x03 // digital pin in PWM output mode +#define SERVO 0x04 // digital pin in Servo output mode +#define SHIFT 0x05 // shiftIn/shiftOut mode +#define I2C 0x06 // pin included in I2C setup +#define TOTAL_PIN_MODES 7 + +extern "C" { +// callback function types + typedef void (*callbackFunction)(byte, int); + typedef void (*systemResetCallbackFunction)(void); + typedef void (*stringCallbackFunction)(char*); + typedef void (*sysexCallbackFunction)(byte command, byte argc, byte*argv); +} + + +// TODO make it a subclass of a generic Serial/Stream base class +class Adafruit_BLE_FirmataClass +{ +public: + Adafruit_BLE_FirmataClass(Stream &s); + +/* Arduino constructors */ + void begin(); + void begin(Stream &s); +/* querying functions */ + void printVersion(void); + void blinkVersion(void); + void printFirmwareVersion(void); + //void setFirmwareVersion(byte major, byte minor); // see macro below + void setFirmwareNameAndVersion(const char *name, byte major, byte minor); +/* serial receive handling */ + int available(void); + int processInput(void); +/* serial send handling */ + void sendAnalog(byte pin, int value); + void sendDigital(byte pin, int value); // TODO implement this + void sendDigitalPort(byte portNumber, int portData); + void sendString(const char* string); + void sendString(byte command, const char* string); + void sendSysex(byte command, byte bytec, byte* bytev); +/* attach & detach callback functions to messages */ + void attach(byte command, callbackFunction newFunction); + void attach(byte command, systemResetCallbackFunction newFunction); + void attach(byte command, stringCallbackFunction newFunction); + void attach(byte command, sysexCallbackFunction newFunction); + void detach(byte command); + + + /* board details */ + void setUsablePins(uint8_t *digitaliopins, uint8_t num_digitaliopins, + uint8_t *analogiopins, uint8_t num_analogiopins, + uint8_t *pwmpins, uint8_t num_pwmpins, + uint8_t *servopins, uint8_t num_servopins, + uint8_t sdapin, uint8_t sclpin); + + boolean IS_PIN_DIGITAL(uint8_t p) { return contains(_digitaliopins,_num_digitaliopins, p); } + uint8_t PIN_TO_DIGITAL(uint8_t p) { return p; } + boolean IS_PIN_ANALOG(uint8_t p) { return contains(_analogiopins, _num_analogiopins, p); } + uint8_t PIN_TO_ANALOG(uint8_t p); + boolean IS_PIN_PWM(uint8_t p) { return contains(_pwmpins, _num_pwmpins, p); } + uint8_t PIN_TO_PWM(uint8_t p) { return p; } + boolean IS_PIN_SERVO(uint8_t p) { return contains(_servopins, _num_servopins, p); } + uint8_t PIN_TO_SERVO(uint8_t p) { return p-2;} + boolean IS_PIN_I2C(uint8_t p) { return (p == _sdapin) || (p == _sclpin); } + + unsigned char readPort(byte port, byte bitmask); + unsigned char writePort(byte port, byte value, byte bitmask); + + + uint8_t _num_analogiopins; + +private: + Stream &FirmataSerial; + + uint8_t *_digitaliopins, _num_digitaliopins; + uint8_t *_pwmpins, _num_pwmpins; + uint8_t *_analogiopins; + uint8_t *_servopins, _num_servopins; + uint8_t _sdapin, _sclpin; + + boolean contains(uint8_t *set, uint8_t num, uint8_t test); + uint8_t location(uint8_t *set, uint8_t num, uint8_t test); + +/* firmware name and version */ + byte firmwareVersionCount; + byte *firmwareVersionVector; +/* input message handling */ + byte waitForData; // this flag says the next serial input will be data + byte executeMultiByteCommand; // execute this after getting multi-byte data + byte multiByteChannel; // channel data for multiByteCommands + byte storedInputData[MAX_DATA_BYTES]; // multi-byte data +/* sysex */ + boolean parsingSysex; + int sysexBytesRead; +/* callback functions */ + callbackFunction currentAnalogCallback; + callbackFunction currentDigitalCallback; + callbackFunction currentReportAnalogCallback; + callbackFunction currentReportDigitalCallback; + callbackFunction currentPinModeCallback; + systemResetCallbackFunction currentSystemResetCallback; + stringCallbackFunction currentStringCallback; + sysexCallbackFunction currentSysexCallback; + +/* private methods ------------------------------ */ + void processSysexMessage(void); + void systemReset(void); + void pin13strobe(int count, int onInterval, int offInterval); + void sendValueAsTwo7bitBytes(int value); + void startSysex(void); + void endSysex(void); + +}; + +extern Adafruit_BLE_FirmataClass BLE_Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the + * firmware name. It needs to be a macro so that __FILE__ is included in the + * firmware source file rather than the library source file. + */ +#define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y) + +#endif /* BLE_Firmata_h */ + diff --git a/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata_Boards.h b/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata_Boards.h new file mode 100644 index 0000000..bef8a0f --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/Adafruit_BLE_Firmata_Boards.h @@ -0,0 +1,135 @@ +/* Boards.h - Hardware Abstraction Layer for Firmata library */ + +#ifndef BLE_Firmata_Boards_h +#define BLE_Firmata_Boards_h + +#include + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" // for digitalRead, digitalWrite, etc +#else +#include "WProgram.h" +#endif + +// Normally Servo.h must be included before Firmata.h (which then includes +// this file). If Servo.h wasn't included, this allows the code to still +// compile, but without support for any Servos. Hopefully that's what the +// user intended by not including Servo.h +#ifndef MAX_SERVOS +#define MAX_SERVOS 0 +#endif + +/* + Firmata Hardware Abstraction Layer + +Firmata is built on top of the hardware abstraction functions of Arduino, +specifically digitalWrite, digitalRead, analogWrite, analogRead, and +pinMode. While these functions offer simple integer pin numbers, Firmata +needs more information than is provided by Arduino. This file provides +all other hardware specific details. To make Firmata support a new board, +only this file should require editing. + +The key concept is every "pin" implemented by Firmata may be mapped to +any pin as implemented by Arduino. Usually a simple 1-to-1 mapping is +best, but such mapping should not be assumed. This hardware abstraction +layer allows Firmata to implement any number of pins which map onto the +Arduino implemented pins in almost any arbitrary way. + + +General Constants: + +These constants provide basic information Firmata requires. + +TOTAL_PINS: The total number of pins Firmata implemented by Firmata. + Usually this will match the number of pins the Arduino functions + implement, including any pins pins capable of analog or digital. + However, Firmata may implement any number of pins. For example, + on Arduino Mini with 8 analog inputs, 6 of these may be used + for digital functions, and 2 are analog only. On such boards, + Firmata can implement more pins than Arduino's pinMode() + function, in order to accommodate those special pins. The + Firmata protocol supports a maximum of 128 pins, so this + constant must not exceed 128. + +TOTAL_ANALOG_PINS: The total number of analog input pins implemented. + The Firmata protocol allows up to 16 analog inputs, accessed + using offsets 0 to 15. Because Firmata presents the analog + inputs using different offsets than the actual pin numbers + (a legacy of Arduino's analogRead function, and the way the + analog input capable pins are physically labeled on all + Arduino boards), the total number of analog input signals + must be specified. 16 is the maximum. + +VERSION_BLINK_PIN: When Firmata starts up, it will blink the version + number. This constant is the Arduino pin number where a + LED is connected. + + +Pin Mapping Macros: + +These macros provide the mapping between pins as implemented by +Firmata protocol and the actual pin numbers used by the Arduino +functions. Even though such mappings are often simple, pin +numbers received by Firmata protocol should always be used as +input to these macros, and the result of the macro should be +used with with any Arduino function. + +When Firmata is extended to support a new pin mode or feature, +a pair of macros should be added and used for all hardware +access. For simple 1:1 mapping, these macros add no actual +overhead, yet their consistent use allows source code which +uses them consistently to be easily adapted to all other boards +with different requirements. + +IS_PIN_XXXX(pin): The IS_PIN macros resolve to true or non-zero + if a pin as implemented by Firmata corresponds to a pin + that actually implements the named feature. + +PIN_TO_XXXX(pin): The PIN_TO macros translate pin numbers as + implemented by Firmata to the pin numbers needed as inputs + to the Arduino functions. The corresponding IS_PIN macro + should always be tested before using a PIN_TO macro, so + these macros only need to handle valid Firmata pin + numbers for the named feature. + + +Port Access Inline Funtions: + +For efficiency, Firmata protocol provides access to digital +input and output pins grouped by 8 bit ports. When these +groups of 8 correspond to actual 8 bit ports as implemented +by the hardware, these inline functions can provide high +speed direct port access. Otherwise, a default implementation +using 8 calls to digitalWrite or digitalRead is used. + +When porting Firmata to a new board, it is recommended to +use the default functions first and focus only on the constants +and macros above. When those are working, if optimized port +access is desired, these inline functions may be extended. +The recommended approach defines a symbol indicating which +optimization to use, and then conditional complication is +used within these functions. + +readPort(port, bitmask): Read an 8 bit port, returning the value. + port: The port number, Firmata pins port*8 to port*8+7 + bitmask: The actual pins to read, indicated by 1 bits. + +writePort(port, value, bitmask): Write an 8 bit port. + port: The port number, Firmata pins port*8 to port*8+7 + value: The 8 bit value to write + bitmask: The actual pins to write, indicated by 1 bits. +*/ + +/*============================================================================== + * Board Specific Configuration + *============================================================================*/ + + +#define VERSION_BLINK_PIN 99 +#define ARDUINO_PINOUT_OPTIMIZE 0 + + + + +#endif /* BLE_Firmata_Boards_h */ + diff --git a/libraries/Adafruit_BLEFirmata/LICENSE.txt b/libraries/Adafruit_BLEFirmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Adafruit_BLEFirmata/README.md b/libraries/Adafruit_BLEFirmata/README.md new file mode 100644 index 0000000..614ba36 --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/README.md @@ -0,0 +1,125 @@ +#Firmata + +Firmata is a protocol for communicating with microcontrollers from software on a host computer. The [protocol](http://firmata.org/wiki/Protocol) can be implemented in firmware on any microcontroller architecture as well as software on any host computer software package. The arduino repository described here is a Firmata library for Arduino and Arduino-compatible devices. See the [firmata wiki](http://firmata.org/wiki/Main_Page) for additional informataion. If you would like to contribute to Firmata, please see the [Contributing](#contributing) section below. + +##Usage + +There are two main models of usage of Firmata. In one model, the author of the Arduino sketch uses the various methods provided by the Firmata library to selectively send and receive data between the Arduino device and the software running on the host computer. For example, a user can send analog data to the host using ``` Firmata.sendAnalog(analogPin, analogRead(analogPin)) ``` or send data packed in a string using ``` Firmata.sendString(stringToSend) ```. See File -> Examples -> Firmata -> AnalogFirmata & EchoString respectively for examples. + +The second and more common model is to load a general purpose sketch called StandardFirmata on the Arduino board and then use the host computer exclusively to interact with the Arduino board. StandardFirmata is located in the Arduino IDE in File -> Examples -> Firmata. + +##Firmata Client Libraries +Most of the time you will be interacting with arduino with a client library on the host computers. Several Firmata client libraries have been implemented in a variety of popular programming languages: + +* procesing + * [https://github.com/firmata/processing] + * [http://funnel.cc] +* python + * [https://github.com/firmata/pyduino] + * [https://github.com/lupeke/python-firmata] + * [https://github.com/tino/pyFirmata] +* perl + * [https://github.com/ntruchsess/perl-firmata] + * [https://github.com/rcaputo/rx-firmata] +* ruby + * [https://github.com/hardbap/firmata] + * [https://github.com/PlasticLizard/rufinol] + * [http://funnel.cc] +* clojure + * [https://github.com/nakkaya/clodiuno] +* javascript + * [https://github.com/jgautier/firmata] + * [http://breakoutjs.com] + * [https://github.com/rwldrn/johnny-five] +* java + * [https://github.com/4ntoine/Firmata] + * [https://github.com/shigeodayo/Javarduino] +* .NET + * [http://www.imagitronics.org/projects/firmatanet/] +* Flash/AS3 + * [http://funnel.cc] + * [http://code.google.com/p/as3glue/] +* PHP + * [https://bitbucket.org/ThomasWeinert/carica-firmata] + +Note: The above libraries may support various versions of the Firmata protocol and therefore may not support all features of the latest Firmata spec nor all arduino and arduino-compatible boards. Refer to the respective projects for details. + +##Updating Firmata in the Arduino IDE (< Arduino 1.5) +The version of firmata in the Arduino IDE contains an outdated version of Firmata. To update Firmata, clone the repo into the location of firmata in the arduino IDE or download the latest [tagged version](https://github.com/firmata/arduino/tags) (stable), rename the folder to "Firmata" and replace the existing Firmata folder in your Ardino application. + +**Mac OSX**: + +```bash +$ rm -r /Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata +$ git clone git@github.com:firmata/arduino.git /Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata +``` + +If you are downloading the latest tagged version of Firmata, rename it to "Firmata" and copy to /Applications/Arduino.app/Contents/Resources/Java/libraries/ overwriting the existing Firmata directory. Right-click (or conrol + click) on the Arduino application and choose "Show Package Contents" and navigate to the libraries directory. + +**Windows**: + +Using the Git Shell application installed with [GitHub for Windows](http://windows.github.com/) (set default shell in options to Git Bash) or other command line based git tool: + +update the path and arduino version as necessary +```bash +$ rm -r c:/Program\ Files/arduino-1.x/libraries/Firmata +$ git clone git@github.com:firmata/arduino.git c:/Program\ Files/arduino-1.x/libraries/Firmata +``` + +Note: If you use GitHub for Windows, you must clone the firmata/arduino repository using the Git Shell application as described above. You can use the Github for Windows GUI only after you have cloned the repository. Drag the Firmata file into the Github for Windows GUI to track it. + +**Linux**: + +update the path and arduino version as necessary +```bash +$ rm -r ~/arduino-1.x/libraries/Firmata +$ git clone git@github.com:firmata/arduino.git ~/arduino-1.x/libraries/Firmata +``` + +##Updating Firmata in the Arduino IDE (>= Arduino 1.5.2) +As of Arduino 1.5.2 and there are separate library directories for the sam and +avr architectures. To update Firmata in Arduino 1.5.2 or higher, follow the +instructions above for pre Arduino 1.5 versions but update the path as follows: + +**Mac OSX**: +``` +/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/libraries/Firmata +/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/libraries/Firmata +``` + +**Windows**: +``` +/Program\ Files/arduino-1.5.x/hardware/arduino/avr/libraries/Firmata +/Program\ Files/arduino-1.5.x/hardware/arduino/sam/libraries/Firmata +``` + +**Linux** +``` +~/arduino-1.5.x/hardware/arduino/avr/libraries/Firmata +~/arduino-1.5.x/hardware/arduino/sam/libraries/Firmata +``` + + +##Contributing + +If you discover a bug or would like to propose a new feature, please open a new [issue](https://github.com/firmata/arduino/issues?sort=created&state=open). Due to the limited memory of standard Arduino boards we cannot add every requested feature to StandardFirmata. Requests to add new features to StandardFirmata will be evaluated by the Firmata developers. However it is still possible to add new features to other Firmata implementations (Firmata is a protocol whereas StandardFirmata is just one of many possible implementations). + +To contribute, fork this respository and create a new topic branch for the bug, feature or other existing issue you are addressing. Submit the pull request against the *dev* branch. + +If you would like to contribute but don't have a specific bugfix or new feature to contribute, you can take on an existing issue, see issues labeled "pull-request-encouraged". Add a comment to the issue to express your intent to begin work and/or to get any additional information about the issue. + +You must thorougly test your contributed code. In your pull request, describe tests performed to ensure that no existing code is broken and that any changes maintain backwards compatibility with the existing api. Test on multiple Arduino board variants if possible. We hope to enable some form of automated (or at least semi-automated) testing in the future, but for now any tests will need to be executed manually by the contributor and reviewsers. + +Maintain the existing code style: + +- Indentation is 2 spaces +- Use spaces instead of tabs +- Use camel case for both private and public properties and methods +- Document functions (specific doc style is TBD... for now just be sure to document) +- Insert first block bracket on line following the function definition: + +
void someFunction()
+{
+  // do something
+}
+
diff --git a/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitConfig.h b/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitConfig.h new file mode 100644 index 0000000..be6cbb1 --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitConfig.h @@ -0,0 +1,56 @@ +// COMMON SETTINGS +// ---------------------------------------------------------------------------------------------- +// These settings are used in both SW UART, HW UART and SPI mode +// ---------------------------------------------------------------------------------------------- +#define BUFSIZE 128 // Size of the read buffer for incoming data +#define VERBOSE_MODE true // If set to 'true' enables debug output + + +// SOFTWARE UART SETTINGS +// ---------------------------------------------------------------------------------------------- +// The following macros declare the pins that will be used for 'SW' serial. +// You should use this option if you are connecting the UART Friend to an UNO +// ---------------------------------------------------------------------------------------------- +#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial! +#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial! +#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial! +#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused + + +// HARDWARE UART SETTINGS +// ---------------------------------------------------------------------------------------------- +// The following macros declare the HW serial port you are using. Uncomment +// this line if you are connecting the BLE to Leonardo/Micro or Flora +// ---------------------------------------------------------------------------------------------- +#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1 + #define BLUEFRUIT_HWSERIAL_NAME Serial1 +#endif + + +// SHARED UART SETTINGS +// ---------------------------------------------------------------------------------------------- +// The following sets the optional Mode pin, its recommended but not required +// ---------------------------------------------------------------------------------------------- +#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused + + +// SHARED SPI SETTINGS +// ---------------------------------------------------------------------------------------------- +// The following macros declare the pins to use for HW and SW SPI communication. +// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when +// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules +// that use SPI (Bluefruit LE SPI Friend). +// ---------------------------------------------------------------------------------------------- +#define BLUEFRUIT_SPI_CS 8 +#define BLUEFRUIT_SPI_IRQ 7 +#define BLUEFRUIT_SPI_RST 4 + +// SOFTWARE SPI SETTINGS +// ---------------------------------------------------------------------------------------------- +// The following macros declare the pins to use for SW SPI communication. +// This should be used with nRF51822 based Bluefruit LE modules that use SPI +// (Bluefruit LE SPI Friend). +// ---------------------------------------------------------------------------------------------- +#define BLUEFRUIT_SPI_SCK 13 +#define BLUEFRUIT_SPI_MISO 12 +#define BLUEFRUIT_SPI_MOSI 11 diff --git a/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitLE_nrf51822.ino b/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitLE_nrf51822.ino new file mode 100644 index 0000000..75efc48 --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/examples/BluefruitLE_nrf51822/BluefruitLE_nrf51822.ino @@ -0,0 +1,859 @@ +#include +#include +#include +#include +#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_) + #include +#endif + +// Change this to whatever is the Serial console you want, either Serial or SerialUSB +#define FIRMATADEBUG Serial +// Pause for Serial console before beginning? +#define WAITFORSERIAL true +// Print all BLE interactions? +#define VERBOSE_MODE false + + +/************************ CONFIGURATION SECTION ***********************************/ +/* +Don't forget to also change the BluefruitConfig.h for the SPI or UART connection +and pinout you are using! + +Then below, you can edit the list of pins that are available. Remove any pins +that are used for accessories or for talking to the BLE module! +*/ + +/************** For Bluefruit Micro or Feather 32u4 Bluefruit ************/ +uint8_t boards_digitaliopins[] = {0,1,2,3,5,6,9,10,11,12,13,A0,A1,A2,A3,A4,A5}; + +/************** For UNO + nRF58122 SPI & shield ************/ +//uint8_t boards_digitaliopins[] = {2, 3, 5, 6, 9, 10, A0, A1, A2, A3, A4, A5}; + +/************** For Bluefruit M0 Bluefruit ************/ +//uint8_t boards_digitaliopins[] = {0,1,5,6,9,10,11,12,13,20,21,A0,A1,A2,A3,A4,A5}; + +#if defined(__AVR_ATmega328P__) + // Standard setup for UNO, no need to tweak + uint8_t boards_analogiopins[] = {A0, A1, A2, A3, A4, A5}; // A0 == digital 14, etc + uint8_t boards_pwmpins[] = {3, 5, 6, 9, 10, 11}; + uint8_t boards_servopins[] = {9, 10}; + uint8_t boards_i2cpins[] = {SDA, SCL}; +#elif defined(__AVR_ATmega32U4__) + uint8_t boards_analogiopins[] = {A0, A1, A2, A3, A4, A5}; // A0 == digital 14, etc + uint8_t boards_pwmpins[] = {3, 5, 6, 9, 10, 11, 13}; + uint8_t boards_servopins[] = {9, 10}; + uint8_t boards_i2cpins[] = {SDA, SCL}; +#elif defined(__SAMD21G18A__) + #define SDA PIN_WIRE_SDA + #define SCL PIN_WIRE_SCL + uint8_t boards_analogiopins[] = {PIN_A0, PIN_A1, PIN_A2, PIN_A3, PIN_A4, PIN_A5,PIN_A6, PIN_A7}; // A0 == digital 14, etc + uint8_t boards_pwmpins[] = {3,4,5,6,8,10,11,12,A0,A1,A2,A3,A4,A5}; + uint8_t boards_servopins[] = {9, 10}; + uint8_t boards_i2cpins[] = {SDA, SCL}; + #define NUM_DIGITAL_PINS 26 +#endif + + +#define TOTAL_PINS NUM_DIGITAL_PINS /* highest number in boards_digitaliopins MEMEFIXME:automate */ +#define TOTAL_PORTS ((TOTAL_PINS + 7) / 8) + + +/***********************************************************/ + + +#include "Adafruit_BLE_Firmata_Boards.h" + +#include "Adafruit_BLE.h" +#include "Adafruit_BluefruitLE_SPI.h" +#include "Adafruit_BluefruitLE_UART.h" +#include "BluefruitConfig.h" + + +// Create the bluefruit object, either software serial...uncomment these lines +/* +SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN); + +Adafruit_BluefruitLE_UART bluefruit(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, + BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN); +*/ + +/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */ +// Adafruit_BluefruitLE_UART bluefruit(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN); + +/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */ +Adafruit_BluefruitLE_SPI bluefruit(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); + +/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */ +//Adafruit_BluefruitLE_SPI bluefruit(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO, +// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS, +// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); + + +#define AUTO_INPUT_PULLUPS true + +// our current connection status +boolean lastBTLEstatus, BTLEstatus; + +// make one instance for the user to use +Adafruit_BLE_FirmataClass BLE_Firmata = Adafruit_BLE_FirmataClass(bluefruit); + +// A small helper +void error(const __FlashStringHelper*err) { + FIRMATADEBUG.println(err); + while (1); +} + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int lastAnalogReads[NUM_ANALOG_INPUTS]; + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte pinConfig[TOTAL_PINS]; // configuration of every pin +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else +int pinState[TOTAL_PINS]; // any value that has been written + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +int samplingInterval = 200; // how often to run the main loop (in ms) +#define MINIMUM_SAMPLE_DELAY 150 +#define ANALOG_SAMPLE_DELAY 50 + + +/* i2c data */ +struct i2c_device_info { + byte addr; + byte reg; + byte bytes; +}; + +/* for i2c read continuous more */ +i2c_device_info query[MAX_QUERIES]; + +byte i2cRxData[32]; +boolean isI2CEnabled = false; +signed char queryIndex = -1; +unsigned int i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom() + +Servo servos[MAX_SERVOS]; +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void readAndReportData(byte address, int theRegister, byte numBytes) { + // 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 != REGISTER_NOT_SPECIFIED) { + Wire.beginTransmission(address); + #if ARDUINO >= 100 + Wire.write((byte)theRegister); + #else + Wire.send((byte)theRegister); + #endif + Wire.endTransmission(); + delayMicroseconds(i2cReadDelayTime); // delay is necessary for some devices such as WiiNunchuck + } 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()) { + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + for (int i = 0; i < numBytes; i++) { + #if ARDUINO >= 100 + i2cRxData[2 + i] = Wire.read(); + #else + i2cRxData[2 + i] = Wire.receive(); + #endif + } + } + else { + if(numBytes > Wire.available()) { + BLE_Firmata.sendString("I2C Read Error: Too many bytes received"); + } else { + BLE_Firmata.sendString("I2C Read Error: Too few bytes received"); + } + } + + // send slave address, register and received bytes + BLE_Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +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) { + //FIRMATADEBUG.print(F("Sending update for port ")); FIRMATADEBUG.print(portNumber); FIRMATADEBUG.print(" = 0x"); FIRMATADEBUG.println(portValue, HEX); + BLE_Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using () */ +void checkDigitalInputs(boolean forceSend = false) +{ + /* 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. */ + for (uint8_t i=0; i TOTAL_PINS) lastPin = TOTAL_PINS; + for (pin=port*8; pin < lastPin; pin++) { + // do not disturb non-digital pins (eg, Rx & Tx) + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + // only write to OUTPUT + // do not touch pins in PWM, ANALOG, SERVO or other modes + if (pinConfig[pin] == OUTPUT) { + pinWriteMask |= mask; + pinState[pin] = ((byte)value & mask) ? 1 : 0; + } + } + mask = mask << 1; + } + FIRMATADEBUG.print(F("Write digital port #")); FIRMATADEBUG.print(port); + FIRMATADEBUG.print(F(" = 0x")); FIRMATADEBUG.print(value, HEX); + FIRMATADEBUG.print(F(" mask = 0x")); FIRMATADEBUG.println(pinWriteMask, HEX); + BLE_Firmata.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) +{ + if (analogPin < BLE_Firmata._num_analogiopins) { + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << analogPin); + FIRMATADEBUG.print(F("Stop reporting analog pin #")); FIRMATADEBUG.println(analogPin); + } else { + analogInputsToReport |= (1 << analogPin); + FIRMATADEBUG.print(F("Will report analog pin #")); FIRMATADEBUG.println(analogPin); + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + //FIRMATADEBUG.print(F("Will report 0x")); FIRMATADEBUG.print(value, HEX); FIRMATADEBUG.print(F(" digital mask on port ")); FIRMATADEBUG.println(port); + reportPINs[port] = (byte)value; + } + // 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 slaveAddress; + byte slaveRegister; + byte data; + unsigned int delayTime; + + FIRMATADEBUG.println("********** Sysex callback"); + switch(command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + //BLE_Firmata.sendString("10-bit addressing mode is not yet supported"); + FIRMATADEBUG.println(F("10-bit addressing mode is not yet supported")); + return; + } + else { + slaveAddress = argv[0]; + } + + switch(mode) { + case I2C_WRITE: + Wire.beginTransmission(slaveAddress); + for (byte i = 2; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + #if ARDUINO >= 100 + Wire.write(data); + #else + Wire.send(data); + #endif + } + 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 + readAndReportData(slaveAddress, (int)slaveRegister, data); + } + else { + // a slave register is NOT specified + data = argv[2] + (argv[3] << 7); // bytes to read + readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data); + } + break; + case I2C_READ_CONTINUOUSLY: + if ((queryIndex + 1) >= MAX_QUERIES) { + // too many queries, just ignore + BLE_Firmata.sendString("too many queries"); + break; + } + queryIndex++; + query[queryIndex].addr = slaveAddress; + query[queryIndex].reg = argv[2] + (argv[3] << 7); + query[queryIndex].bytes = argv[4] + (argv[5] << 7); + 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 { + // 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 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if(argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + if (servos[BLE_Firmata.PIN_TO_SERVO(pin)].attached()) + servos[BLE_Firmata.PIN_TO_SERVO(pin)].detach(); + servos[BLE_Firmata.PIN_TO_SERVO(pin)].attach(BLE_Firmata.PIN_TO_DIGITAL(pin), minPulse, maxPulse); + setPinModeCallback(pin, SERVO); + } + } + break; + case SAMPLING_INTERVAL: + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + } else { + //BLE_Firmata.sendString("Not enough data"); + } + 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: + bluefruit.write(START_SYSEX); + bluefruit.write(CAPABILITY_RESPONSE); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(START_SYSEX, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(CAPABILITY_RESPONSE, HEX); + delay(10); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + //FIRMATADEBUG.print("\t#"); FIRMATADEBUG.println(pin); + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + bluefruit.write((byte)INPUT); + bluefruit.write(1); + bluefruit.write((byte)OUTPUT); + bluefruit.write(1); + + /* + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(INPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(1, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(OUTPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(1, HEX); + */ + delay(20); + } else { + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + delay(20); + continue; + } + if (BLE_Firmata.IS_PIN_ANALOG(pin)) { + bluefruit.write(ANALOG); + bluefruit.write(10); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(ANALOG, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(10, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_PWM(pin)) { + bluefruit.write(PWM); + bluefruit.write(8); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(PWM, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(8, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + bluefruit.write(SERVO); + bluefruit.write(14); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(SERVO, HEX);FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(14, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_I2C(pin)) { + bluefruit.write(I2C); + bluefruit.write(1); // to do: determine appropriate value + delay(20); + } + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + } + bluefruit.write(END_SYSEX); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(END_SYSEX, HEX); + break; + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin=argv[0]; + bluefruit.write(START_SYSEX); + bluefruit.write(PIN_STATE_RESPONSE); + bluefruit.write(pin); + if (pin < TOTAL_PINS) { + bluefruit.write((byte)pinConfig[pin]); + bluefruit.write((byte)pinState[pin] & 0x7F); + if (pinState[pin] & 0xFF80) bluefruit.write((byte)(pinState[pin] >> 7) & 0x7F); + if (pinState[pin] & 0xC000) bluefruit.write((byte)(pinState[pin] >> 14) & 0x7F); + } + bluefruit.write(END_SYSEX); + } + break; + case ANALOG_MAPPING_QUERY: + FIRMATADEBUG.println("Analog mapping query"); + bluefruit.write(START_SYSEX); + bluefruit.write(ANALOG_MAPPING_RESPONSE); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + bluefruit.write(BLE_Firmata.IS_PIN_ANALOG(pin) ? BLE_Firmata.PIN_TO_ANALOG(pin) : 127); + } + bluefruit.write(END_SYSEX); + break; + } +} + +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(BLE_Firmata.IS_PIN_I2C(i)) { + // mark pins as i2c so they are ignore in non i2c data requests + setPinModeCallback(i, I2C); + } + } + + isI2CEnabled = true; + + // is there enough time before the first I2C request to call this here? + 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; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a defalt state + FIRMATADEBUG.println(F("***RESET***")); + // TODO: option to load config from EEPROM instead of default + if (isI2CEnabled) { + disableI2CPins(); + } + for (byte i=0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i=0; i < TOTAL_PINS; i++) { + if (BLE_Firmata.IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, ANALOG); + } else { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, INPUT); + } + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ +} + +void setup() +{ + if (WAITFORSERIAL) { + while (!FIRMATADEBUG) delay(1); + } + + FIRMATADEBUG.begin(9600); + FIRMATADEBUG.println(F("Adafruit Bluefruit LE Firmata test")); + + FIRMATADEBUG.print("Total pins: "); FIRMATADEBUG.println(NUM_DIGITAL_PINS); + FIRMATADEBUG.print("Analog pins: "); FIRMATADEBUG.println(sizeof(boards_analogiopins)); + //for (uint8_t i=0; i samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + + for(pin=0; pin= 100 + Wire.write((byte)theRegister); + #else + Wire.send((byte)theRegister); + #endif + Wire.endTransmission(); + delayMicroseconds(i2cReadDelayTime); // delay is necessary for some devices such as WiiNunchuck + } 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()) { + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + for (int i = 0; i < numBytes; i++) { + #if ARDUINO >= 100 + i2cRxData[2 + i] = Wire.read(); + #else + i2cRxData[2 + i] = Wire.receive(); + #endif + } + } + else { + if(numBytes > Wire.available()) { + BLE_Firmata.sendString("I2C Read Error: Too many bytes received"); + } else { + BLE_Firmata.sendString("I2C Read Error: Too few bytes received"); + } + } + + // send slave address, register and received bytes + BLE_Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +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) { + //FIRMATADEBUG.print(F("Sending update for port ")); FIRMATADEBUG.print(portNumber); FIRMATADEBUG.print(" = 0x"); FIRMATADEBUG.println(portValue, HEX); + BLE_Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using () */ +void checkDigitalInputs(boolean forceSend = false) +{ + /* 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. */ + for (uint8_t i=0; i TOTAL_PINS) lastPin = TOTAL_PINS; + for (pin=port*8; pin < lastPin; pin++) { + // do not disturb non-digital pins (eg, Rx & Tx) + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + // only write to OUTPUT + // do not touch pins in PWM, ANALOG, SERVO or other modes + if (pinConfig[pin] == OUTPUT) { + pinWriteMask |= mask; + pinState[pin] = ((byte)value & mask) ? 1 : 0; + } + } + mask = mask << 1; + } + FIRMATADEBUG.print(F("Write digital port #")); FIRMATADEBUG.print(port); + FIRMATADEBUG.print(F(" = 0x")); FIRMATADEBUG.print(value, HEX); + FIRMATADEBUG.print(F(" mask = 0x")); FIRMATADEBUG.println(pinWriteMask, HEX); + BLE_Firmata.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) +{ + if (analogPin < BLE_Firmata._num_analogiopins) { + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << analogPin); + FIRMATADEBUG.print(F("Stop reporting analog pin #")); FIRMATADEBUG.println(analogPin); + } else { + analogInputsToReport |= (1 << analogPin); + FIRMATADEBUG.print(F("Will report analog pin #")); FIRMATADEBUG.println(analogPin); + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + //FIRMATADEBUG.print(F("Will report 0x")); FIRMATADEBUG.print(value, HEX); FIRMATADEBUG.print(F(" digital mask on port ")); FIRMATADEBUG.println(port); + reportPINs[port] = (byte)value; + } + // 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 slaveAddress; + byte slaveRegister; + byte data; + unsigned int delayTime; + + FIRMATADEBUG.println("********** Sysex callback"); + switch(command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + //BLE_Firmata.sendString("10-bit addressing mode is not yet supported"); + FIRMATADEBUG.println(F("10-bit addressing mode is not yet supported")); + return; + } + else { + slaveAddress = argv[0]; + } + + switch(mode) { + case I2C_WRITE: + Wire.beginTransmission(slaveAddress); + for (byte i = 2; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + #if ARDUINO >= 100 + Wire.write(data); + #else + Wire.send(data); + #endif + } + 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 + readAndReportData(slaveAddress, (int)slaveRegister, data); + } + else { + // a slave register is NOT specified + data = argv[2] + (argv[3] << 7); // bytes to read + readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data); + } + break; + case I2C_READ_CONTINUOUSLY: + if ((queryIndex + 1) >= MAX_QUERIES) { + // too many queries, just ignore + BLE_Firmata.sendString("too many queries"); + break; + } + queryIndex++; + query[queryIndex].addr = slaveAddress; + query[queryIndex].reg = argv[2] + (argv[3] << 7); + query[queryIndex].bytes = argv[4] + (argv[5] << 7); + 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 { + // 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 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if(argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + if (servos[BLE_Firmata.PIN_TO_SERVO(pin)].attached()) + servos[BLE_Firmata.PIN_TO_SERVO(pin)].detach(); + servos[BLE_Firmata.PIN_TO_SERVO(pin)].attach(BLE_Firmata.PIN_TO_DIGITAL(pin), minPulse, maxPulse); + setPinModeCallback(pin, SERVO); + } + } + break; + case SAMPLING_INTERVAL: + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + } else { + //BLE_Firmata.sendString("Not enough data"); + } + 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: + bluefruit.write(START_SYSEX); + bluefruit.write(CAPABILITY_RESPONSE); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(START_SYSEX, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(CAPABILITY_RESPONSE, HEX); + delay(10); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + //FIRMATADEBUG.print("\t#"); FIRMATADEBUG.println(pin); + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + bluefruit.write((byte)INPUT); + bluefruit.write(1); + bluefruit.write((byte)OUTPUT); + bluefruit.write(1); + + /* + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(INPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(1, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(OUTPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(1, HEX); + */ + delay(20); + } else { + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + delay(20); + continue; + } + if (BLE_Firmata.IS_PIN_ANALOG(pin)) { + bluefruit.write(ANALOG); + bluefruit.write(10); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(ANALOG, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(10, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_PWM(pin)) { + bluefruit.write(PWM); + bluefruit.write(8); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(PWM, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(8, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + bluefruit.write(SERVO); + bluefruit.write(14); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(SERVO, HEX);FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(14, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_I2C(pin)) { + bluefruit.write(I2C); + bluefruit.write(1); // to do: determine appropriate value + delay(20); + } + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + } + bluefruit.write(END_SYSEX); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(END_SYSEX, HEX); + break; + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin=argv[0]; + bluefruit.write(START_SYSEX); + bluefruit.write(PIN_STATE_RESPONSE); + bluefruit.write(pin); + if (pin < TOTAL_PINS) { + bluefruit.write((byte)pinConfig[pin]); + bluefruit.write((byte)pinState[pin] & 0x7F); + if (pinState[pin] & 0xFF80) bluefruit.write((byte)(pinState[pin] >> 7) & 0x7F); + if (pinState[pin] & 0xC000) bluefruit.write((byte)(pinState[pin] >> 14) & 0x7F); + } + bluefruit.write(END_SYSEX); + } + break; + case ANALOG_MAPPING_QUERY: + FIRMATADEBUG.println("Analog mapping query"); + bluefruit.write(START_SYSEX); + bluefruit.write(ANALOG_MAPPING_RESPONSE); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + bluefruit.write(BLE_Firmata.IS_PIN_ANALOG(pin) ? BLE_Firmata.PIN_TO_ANALOG(pin) : 127); + } + bluefruit.write(END_SYSEX); + break; + } +} + +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(BLE_Firmata.IS_PIN_I2C(i)) { + // mark pins as i2c so they are ignore in non i2c data requests + setPinModeCallback(i, I2C); + } + } + + isI2CEnabled = true; + + // is there enough time before the first I2C request to call this here? + 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; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a defalt state + FIRMATADEBUG.println(F("***RESET***")); + // TODO: option to load config from EEPROM instead of default + if (isI2CEnabled) { + disableI2CPins(); + } + for (byte i=0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i=0; i < TOTAL_PINS; i++) { + if (BLE_Firmata.IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, ANALOG); + } else { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, INPUT); + } + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ +} + +void setup() +{ + if (WAITFORSERIAL) { + while (!FIRMATADEBUG) delay(1); + } + + FIRMATADEBUG.begin(9600); + FIRMATADEBUG.println(F("Adafruit Bluefruit LE Firmata test")); + + FIRMATADEBUG.print("Total pins: "); FIRMATADEBUG.println(NUM_DIGITAL_PINS); + FIRMATADEBUG.print("Analog pins: "); FIRMATADEBUG.println(sizeof(boards_analogiopins)); + //for (uint8_t i=0; i samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + + for(pin=0; pin= 100 + Wire.write((byte)theRegister); + #else + Wire.send((byte)theRegister); + #endif + Wire.endTransmission(); + delayMicroseconds(i2cReadDelayTime); // delay is necessary for some devices such as WiiNunchuck + } 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()) { + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + for (int i = 0; i < numBytes; i++) { + #if ARDUINO >= 100 + i2cRxData[2 + i] = Wire.read(); + #else + i2cRxData[2 + i] = Wire.receive(); + #endif + } + } + else { + if(numBytes > Wire.available()) { + BLE_Firmata.sendString("I2C Read Error: Too many bytes received"); + } else { + BLE_Firmata.sendString("I2C Read Error: Too few bytes received"); + } + } + + // send slave address, register and received bytes + BLE_Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +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) { + //FIRMATADEBUG.print(F("Sending update for port ")); FIRMATADEBUG.print(portNumber); FIRMATADEBUG.print(" = 0x"); FIRMATADEBUG.println(portValue, HEX); + BLE_Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using () */ +void checkDigitalInputs(boolean forceSend = false) +{ + /* 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. */ + for (uint8_t i=0; i TOTAL_PINS) lastPin = TOTAL_PINS; + for (pin=port*8; pin < lastPin; pin++) { + // do not disturb non-digital pins (eg, Rx & Tx) + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + // only write to OUTPUT + // do not touch pins in PWM, ANALOG, SERVO or other modes + if (pinConfig[pin] == OUTPUT) { + pinWriteMask |= mask; + pinState[pin] = ((byte)value & mask) ? 1 : 0; + } + } + mask = mask << 1; + } + //FIRMATADEBUG.print(F("Write digital port #")); FIRMATADEBUG.print(port); + //FIRMATADEBUG.print(F(" = 0x")); FIRMATADEBUG.print(value, HEX); + //FIRMATADEBUG.print(F(" mask = 0x")); FIRMATADEBUG.println(pinWriteMask, HEX); + BLE_Firmata.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) +{ + if (analogPin < BLE_Firmata._num_analogiopins) { + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << analogPin); + //FIRMATADEBUG.print(F("Stop reporting analog pin #")); FIRMATADEBUG.println(analogPin); + } else { + analogInputsToReport |= (1 << analogPin); + //FIRMATADEBUG.print(F("Will report analog pin #")); FIRMATADEBUG.println(analogPin); + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + //FIRMATADEBUG.print(F("Will report 0x")); FIRMATADEBUG.print(value, HEX); FIRMATADEBUG.print(F(" digital mask on port ")); FIRMATADEBUG.println(port); + reportPINs[port] = (byte)value; + } + // 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 slaveAddress; + byte slaveRegister; + byte data; + unsigned int delayTime; + + FIRMATADEBUG.println("********** Sysex callback"); + + switch(command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + //BLE_Firmata.sendString("10-bit addressing mode is not yet supported"); + //FIRMATADEBUG.println(F("10-bit addressing mode is not yet supported")); + return; + } + else { + slaveAddress = argv[0]; + } + + switch(mode) { + case I2C_WRITE: + Wire.beginTransmission(slaveAddress); + for (byte i = 2; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + #if ARDUINO >= 100 + Wire.write(data); + #else + Wire.send(data); + #endif + } + 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 + readAndReportData(slaveAddress, (int)slaveRegister, data); + } + else { + // a slave register is NOT specified + data = argv[2] + (argv[3] << 7); // bytes to read + readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data); + } + break; + case I2C_READ_CONTINUOUSLY: + if ((queryIndex + 1) >= MAX_QUERIES) { + // too many queries, just ignore + BLE_Firmata.sendString("too many queries"); + break; + } + queryIndex++; + query[queryIndex].addr = slaveAddress; + query[queryIndex].reg = argv[2] + (argv[3] << 7); + query[queryIndex].bytes = argv[4] + (argv[5] << 7); + 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 { + // 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 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if(argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + if (servos[BLE_Firmata.PIN_TO_SERVO(pin)].attached()) + servos[BLE_Firmata.PIN_TO_SERVO(pin)].detach(); + servos[BLE_Firmata.PIN_TO_SERVO(pin)].attach(BLE_Firmata.PIN_TO_DIGITAL(pin), minPulse, maxPulse); + setPinModeCallback(pin, SERVO); + } + } + break; + case SAMPLING_INTERVAL: + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + } else { + //BLE_Firmata.sendString("Not enough data"); + } + 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: + bluefruit.write(START_SYSEX); + bluefruit.write(CAPABILITY_RESPONSE); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(START_SYSEX, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(CAPABILITY_RESPONSE, HEX); + delay(10); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + //FIRMATADEBUG.print("\t#"); FIRMATADEBUG.println(pin); + if (BLE_Firmata.IS_PIN_DIGITAL(pin)) { + bluefruit.write((byte)INPUT); + bluefruit.write(1); + bluefruit.write((byte)OUTPUT); + bluefruit.write(1); + + /* + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(INPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(1, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(OUTPUT, HEX); + FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(1, HEX); + */ + delay(20); + } else { + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + delay(20); + continue; + } + if (BLE_Firmata.IS_PIN_ANALOG(pin)) { + bluefruit.write(ANALOG); + bluefruit.write(10); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(ANALOG, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(10, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_PWM(pin)) { + bluefruit.write(PWM); + bluefruit.write(8); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(PWM, HEX); FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(8, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_SERVO(pin)) { + bluefruit.write(SERVO); + bluefruit.write(14); + + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.print(SERVO, HEX);FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(14, HEX); + delay(20); + } + if (BLE_Firmata.IS_PIN_I2C(pin)) { + bluefruit.write(I2C); + bluefruit.write(1); // to do: determine appropriate value + delay(20); + } + bluefruit.write(127); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(127, HEX); + } + bluefruit.write(END_SYSEX); + //FIRMATADEBUG.print(" 0x"); FIRMATADEBUG.println(END_SYSEX, HEX); + break; + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin=argv[0]; + bluefruit.write(START_SYSEX); + bluefruit.write(PIN_STATE_RESPONSE); + bluefruit.write(pin); + if (pin < TOTAL_PINS) { + bluefruit.write((byte)pinConfig[pin]); + bluefruit.write((byte)pinState[pin] & 0x7F); + if (pinState[pin] & 0xFF80) bluefruit.write((byte)(pinState[pin] >> 7) & 0x7F); + if (pinState[pin] & 0xC000) bluefruit.write((byte)(pinState[pin] >> 14) & 0x7F); + } + bluefruit.write(END_SYSEX); + } + break; + case ANALOG_MAPPING_QUERY: + //FIRMATADEBUG.println("Analog mapping query"); + bluefruit.write(START_SYSEX); + bluefruit.write(ANALOG_MAPPING_RESPONSE); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + bluefruit.write(BLE_Firmata.IS_PIN_ANALOG(pin) ? BLE_Firmata.PIN_TO_ANALOG(pin) : 127); + } + bluefruit.write(END_SYSEX); + break; + } +} + +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(BLE_Firmata.IS_PIN_I2C(i)) { + // mark pins as i2c so they are ignore in non i2c data requests + setPinModeCallback(i, I2C); + } + } + + isI2CEnabled = true; + + // is there enough time before the first I2C request to call this here? + 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; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a defalt state + FIRMATADEBUG.println(F("***RESET***")); + // TODO: option to load config from EEPROM instead of default + if (isI2CEnabled) { + disableI2CPins(); + } + for (byte i=0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i=0; i < TOTAL_PINS; i++) { + if (BLE_Firmata.IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, ANALOG); + } else { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, INPUT); + } + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ +} + +void setup() +{ + if (WAITFORSERIAL) { + while (!FIRMATADEBUG) delay(1); + } + + FIRMATADEBUG.begin(9600); + FIRMATADEBUG.println(F("Adafruit Bluefruit nRF8001 Firmata test")); + + FIRMATADEBUG.print("Total pins: "); FIRMATADEBUG.println(NUM_DIGITAL_PINS); + FIRMATADEBUG.print("Analog pins: "); FIRMATADEBUG.println(sizeof(boards_analogiopins)); + //for (uint8_t i=0; i samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + + for(pin=0; pin +# +# - Write prototypes for all your functions (or define them before you +# call them). A prototype declares the types of parameters a +# function will take and what type of value it will return. This +# means that you can have a call to a function before the definition +# of the function. A function prototype looks like the first line of +# the function, with a semi-colon at the end. For example: +# int digitalRead(int pin); +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. +# +# 2. Below, modify the line containing "TARGET" to refer to the name of +# of your program's file without an extension (e.g. TARGET = foo). +# +# 3. Modify the line containg "ARDUINO" to point the directory that +# contains the Arduino core (for normal Arduino installations, this +# is the hardware/cores/arduino sub-directory). +# +# 4. Modify the line containing "PORT" to refer to the filename +# representing the USB or serial connection to your Arduino board +# (e.g. PORT = /dev/tty.USB0). If the exact name of this file +# changes, you can use * as a wildcard (e.g. PORT = /dev/tty.USB*). +# +# 5. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 6. Type "make" and press enter to compile/verify your program. +# +# 7. Type "make upload", reset your Arduino board, and press enter to +# upload your program to the Arduino board. +# +# $Id: Makefile,v 1.7 2007/04/13 05:28:23 eighthave Exp $ + +PORT = /dev/tty.usbserial-* +TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|') +ARDUINO = /Applications/arduino +ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino +ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries +ARDUINO_TOOLS = $(ARDUINO)/hardware/tools +INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \ + -I$(ARDUINO_LIB_SRC)/EEPROM \ + -I$(ARDUINO_LIB_SRC)/Firmata \ + -I$(ARDUINO_LIB_SRC)/Matrix \ + -I$(ARDUINO_LIB_SRC)/Servo \ + -I$(ARDUINO_LIB_SRC)/Wire \ + -I$(ARDUINO_LIB_SRC) +SRC = $(wildcard $(ARDUINO_SRC)/*.c) +CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \ + $(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \ + $(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \ + $(ARDUINO_LIB_SRC)/Servo/Servo.cpp \ + $(ARDUINO_SRC)/Print.cpp \ + $(ARDUINO_SRC)/WMath.cpp +HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h) + +MCU = atmega168 +#MCU = atmega8 +F_CPU = 16000000 +FORMAT = ihex +UPLOAD_RATE = 19200 + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +DEBUG = stabs + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU) +CXXDEFS = -DF_CPU=$(F_CPU) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +CWARN = -Wall -Wstrict-prototypes +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) + +CFLAGS = $(CDEBUG) $(CDEFS) $(INCLUDE) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEFS) $(INCLUDE) -O$(OPT) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = + + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PROGRAMMER = stk500 +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex +AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ + -b $(UPLOAD_RATE) -q -V + +# Program settings +ARDUINO_AVR_BIN = $(ARDUINO_TOOLS)/avr/bin +CC = $(ARDUINO_AVR_BIN)/avr-gcc +CXX = $(ARDUINO_AVR_BIN)/avr-g++ +OBJCOPY = $(ARDUINO_AVR_BIN)/avr-objcopy +OBJDUMP = $(ARDUINO_AVR_BIN)/avr-objdump +SIZE = $(ARDUINO_AVR_BIN)/avr-size +NM = $(ARDUINO_AVR_BIN)/avr-nm +#AVRDUDE = $(ARDUINO_AVR_BIN)/avrdude +AVRDUDE = avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) +ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + +# Default target. +all: build + +build: applet/$(TARGET).hex + +eep: applet/$(TARGET).eep +lss: applet/$(TARGET).lss +sym: applet/$(TARGET).sym + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + + +coff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +extcoff: applet/$(TARGET).elf + $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf applet/$(TARGET).cof + + +.SUFFIXES: .elf .hex .eep .lss .sym .pde + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +.elf.sym: + $(NM) -n $< > $@ + + +# Compile: create object files from C++ source files. +.cpp.o: $(HEADERS) + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: $(HEADERS) + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + + +applet/$(TARGET).cpp: $(TARGET).pde + test -d applet || mkdir applet + echo '#include "WProgram.h"' > applet/$(TARGET).cpp + echo '#include "avr/interrupt.h"' >> applet/$(TARGET).cpp + sed -n 's|^\(void .*)\).*|\1;|p' $(TARGET).pde | grep -v 'setup()' | \ + grep -v 'loop()' >> applet/$(TARGET).cpp + cat $(TARGET).pde >> applet/$(TARGET).cpp + cat $(ARDUINO_SRC)/main.cxx >> applet/$(TARGET).cpp + +# Link: create ELF output file from object files. +applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ) + $(CC) $(ALL_CFLAGS) $(OBJ) -lm --output $@ $(LDFLAGS) +# $(CC) $(ALL_CFLAGS) $(OBJ) $(ARDUINO_TOOLS)/avr/avr/lib/avr5/crtm168.o --output $@ $(LDFLAGS) + +pd_close_serial: + echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true + +# Program the device. +upload: applet/$(TARGET).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) + + +pd_test: build pd_close_serial upload + +# Target: clean project. +clean: + $(REMOVE) -- applet/$(TARGET).hex applet/$(TARGET).eep \ + applet/$(TARGET).cof applet/$(TARGET).elf $(TARGET).map \ + applet/$(TARGET).sym applet/$(TARGET).lss applet/$(TARGET).cpp \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + rmdir -- applet + +depend: + if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ + then \ + sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ + $(MAKEFILE).$$$$ && \ + $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ + fi + echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ + >> $(MAKEFILE); \ + $(CC) -M -mmcu=$(MCU) $(CDEFS) $(INCLUDE) $(SRC) $(ASRC) >> $(MAKEFILE) + +.PHONY: all build eep lss sym coff extcoff clean depend pd_close_serial pd_test + +# for emacs +etags: + make etags_`uname -s` + etags *.pde \ + $(ARDUINO_SRC)/*.[ch] \ + $(ARDUINO_SRC)/*.cpp \ + $(ARDUINO_LIB_SRC)/*/*.[ch] \ + $(ARDUINO_LIB_SRC)/*/*.cpp \ + $(ARDUINO)/hardware/tools/avr/avr/include/avr/*.[ch] \ + $(ARDUINO)/hardware/tools/avr/avr/include/*.[ch] + +etags_Darwin: +# etags -a + +etags_Linux: +# etags -a /usr/include/*.h linux/input.h /usr/include/sys/*.h + +etags_MINGW: +# etags -a /usr/include/*.h /usr/include/sys/*.h + + +path: + echo $(PATH) + echo $$PATH + diff --git a/libraries/Adafruit_BLEFirmata/examples/StandardFirmata/StandardFirmata.ino b/libraries/Adafruit_BLEFirmata/examples/StandardFirmata/StandardFirmata.ino new file mode 100644 index 0000000..36c64df --- /dev/null +++ b/libraries/Adafruit_BLEFirmata/examples/StandardFirmata/StandardFirmata.ino @@ -0,0 +1,738 @@ +/* + * 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. + * + * This version is modified to specifically work with Adafruit's BLE + * library. It no longer works over the standard "USB" connection! + */ + +/* + 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-2011 Jeff Hoefs. All rights reserved. + Copyright (C) 2014 Limor Fried/Kevin Townsend 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. + + formatted using the GNU C formatting and indenting +*/ + +/* + * TODO: use Program Control to load stored profiles from EEPROM + */ + +#include +#include +#include +#include +#include "Adafruit_BLE_UART.h" + +#define AUTO_INPUT_PULLUPS true + +// Connect CLK/MISO/MOSI to hardware SPI +// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11 +#define ADAFRUITBLE_REQ 10 +#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3 +#define ADAFRUITBLE_RST 9 + +// so we have digital 3-8 and analog 0-6 + +Adafruit_BLE_UART BLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST); + + +// make one instance for the user to use +Adafruit_BLE_FirmataClass BLE_Firmata(BLEserial); + + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int lastAnalogReads[NUM_ANALOG_INPUTS]; + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte pinConfig[TOTAL_PINS]; // configuration of every pin +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else +int pinState[TOTAL_PINS]; // any value that has been written + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +int samplingInterval = 200; // how often to run the main loop (in ms) +#define MINIMUM_SAMPLE_DELAY 150 +#define ANALOG_SAMPLE_DELAY 50 + + +/* i2c data */ +struct i2c_device_info { + byte addr; + byte reg; + byte bytes; +}; + +/* for i2c read continuous more */ +i2c_device_info query[MAX_QUERIES]; + +byte i2cRxData[32]; +boolean isI2CEnabled = false; +signed char queryIndex = -1; +unsigned int i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom() + +Servo servos[MAX_SERVOS]; +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void readAndReportData(byte address, int theRegister, byte numBytes) { + // 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 != REGISTER_NOT_SPECIFIED) { + Wire.beginTransmission(address); + #if ARDUINO >= 100 + Wire.write((byte)theRegister); + #else + Wire.send((byte)theRegister); + #endif + Wire.endTransmission(); + delayMicroseconds(i2cReadDelayTime); // delay is necessary for some devices such as WiiNunchuck + } 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()) { + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + for (int i = 0; i < numBytes; i++) { + #if ARDUINO >= 100 + i2cRxData[2 + i] = Wire.read(); + #else + i2cRxData[2 + i] = Wire.receive(); + #endif + } + } + else { + if(numBytes > Wire.available()) { + BLE_Firmata.sendString("I2C Read Error: Too many bytes received"); + } else { + BLE_Firmata.sendString("I2C Read Error: Too few bytes received"); + } + } + + // send slave address, register and received bytes + BLE_Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +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) { + Serial.print(F("Sending update for port ")); Serial.print(portNumber); Serial.print(" = 0x"); Serial.println(portValue, HEX); + BLE_Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(boolean forceSend = false) +{ + /* 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. */ + for (uint8_t i=0; i 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)) { + // only write to OUTPUT and INPUT (enables pullup) + // do not touch pins in PWM, ANALOG, SERVO or other modes + if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) { + pinWriteMask |= mask; + pinState[pin] = ((byte)value & mask) ? 1 : 0; + + if (AUTO_INPUT_PULLUPS && ( pinConfig[pin] == INPUT)) { + value |= mask; + } + } + } + mask = mask << 1; + } + Serial.print(F("Write digital port #")); Serial.print(port); + Serial.print(F(" = 0x")); Serial.print(value, HEX); + Serial.print(F(" mask = 0x")); Serial.println(pinWriteMask, HEX); + 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) +{ + if (analogPin < TOTAL_ANALOG_PINS) { + if(value == 0) { + analogInputsToReport = analogInputsToReport &~ (1 << analogPin); + Serial.print(F("Stop reporting analog pin #")); Serial.println(analogPin); + } else { + analogInputsToReport |= (1 << analogPin); + Serial.print(F("Will report analog pin #")); Serial.println(analogPin); + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + Serial.print(F("Will report 0x")); Serial.print(value, HEX); Serial.print(F(" digital mask on port ")); Serial.println(port); + reportPINs[port] = (byte)value; + } + // 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 slaveAddress; + byte slaveRegister; + byte data; + unsigned int delayTime; + + switch(command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + //BLE_Firmata.sendString("10-bit addressing mode is not yet supported"); + Serial.println(F("10-bit addressing mode is not yet supported")); + return; + } + else { + slaveAddress = argv[0]; + } + + switch(mode) { + case I2C_WRITE: + Wire.beginTransmission(slaveAddress); + for (byte i = 2; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + #if ARDUINO >= 100 + Wire.write(data); + #else + Wire.send(data); + #endif + } + 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 + readAndReportData(slaveAddress, (int)slaveRegister, data); + } + else { + // a slave register is NOT specified + data = argv[2] + (argv[3] << 7); // bytes to read + readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data); + } + break; + case I2C_READ_CONTINUOUSLY: + if ((queryIndex + 1) >= MAX_QUERIES) { + // too many queries, just ignore + BLE_Firmata.sendString("too many queries"); + break; + } + queryIndex++; + query[queryIndex].addr = slaveAddress; + query[queryIndex].reg = argv[2] + (argv[3] << 7); + query[queryIndex].bytes = argv[4] + (argv[5] << 7); + 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 { + // 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 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if(argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_SERVO(pin)) { + if (servos[PIN_TO_SERVO(pin)].attached()) + servos[PIN_TO_SERVO(pin)].detach(); + servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + setPinModeCallback(pin, SERVO); + } + } + break; + case SAMPLING_INTERVAL: + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + } else { + //BLE_Firmata.sendString("Not enough data"); + } + 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: + Serial.write(START_SYSEX); + Serial.write(CAPABILITY_RESPONSE); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_DIGITAL(pin)) { + Serial.write((byte)INPUT); + Serial.write(1); + Serial.write((byte)OUTPUT); + Serial.write(1); + } + if (IS_PIN_ANALOG(pin)) { + Serial.write(ANALOG); + Serial.write(10); + } + if (IS_PIN_PWM(pin)) { + Serial.write(PWM); + Serial.write(8); + } + if (IS_PIN_SERVO(pin)) { + Serial.write(SERVO); + Serial.write(14); + } + if (IS_PIN_I2C(pin)) { + Serial.write(I2C); + Serial.write(1); // to do: determine appropriate value + } + Serial.write(127); + } + Serial.write(END_SYSEX); + break; + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin=argv[0]; + Serial.write(START_SYSEX); + Serial.write(PIN_STATE_RESPONSE); + Serial.write(pin); + if (pin < TOTAL_PINS) { + Serial.write((byte)pinConfig[pin]); + Serial.write((byte)pinState[pin] & 0x7F); + if (pinState[pin] & 0xFF80) Serial.write((byte)(pinState[pin] >> 7) & 0x7F); + if (pinState[pin] & 0xC000) Serial.write((byte)(pinState[pin] >> 14) & 0x7F); + } + Serial.write(END_SYSEX); + } + break; + case ANALOG_MAPPING_QUERY: + Serial.write(START_SYSEX); + Serial.write(ANALOG_MAPPING_RESPONSE); + for (byte pin=0; pin < TOTAL_PINS; pin++) { + Serial.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Serial.write(END_SYSEX); + break; + } +} + +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, I2C); + } + } + + isI2CEnabled = true; + + // is there enough time before the first I2C request to call this here? + 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; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a defalt state + Serial.println(F("***RESET***")); + // TODO: option to load config from EEPROM instead of default + if (isI2CEnabled) { + disableI2CPins(); + } + for (byte i=0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i=0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, ANALOG); + } else { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, INPUT); + } + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ +} + + +aci_evt_opcode_t lastBTLEstatus, BTLEstatus; + +void setup() +{ + Serial.begin(9600); + Serial.println(F("Adafruit BTLE Firmata test")); + + BLEserial.begin(); + + BTLEstatus = lastBTLEstatus = ACI_EVT_DISCONNECTED; +} + +void firmataInit() { + Serial.println(F("Init firmata")); + //BLE_Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION); + //Serial.println(F("firmata analog")); + BLE_Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + //Serial.println(F("firmata digital")); + BLE_Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + //Serial.println(F("firmata analog report")); + BLE_Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + //Serial.println(F("firmata digital report")); + BLE_Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + //Serial.println(F("firmata pinmode")); + BLE_Firmata.attach(SET_PIN_MODE, setPinModeCallback); + //Serial.println(F("firmata sysex")); + BLE_Firmata.attach(START_SYSEX, sysexCallback); + //Serial.println(F("firmata reset")); + BLE_Firmata.attach(SYSTEM_RESET, systemResetCallback); + + Serial.println(F("Begin firmata")); + BLE_Firmata.begin(); + systemResetCallback(); // reset to default config +} +/*============================================================================== + * LOOP() + *============================================================================*/ + +void loop() +{ + // Check the BTLE link, how're we doing? + BLEserial.pollACI(); + // Link status check + BTLEstatus = BLEserial.getState(); + + // Check if something has changed + if (BTLEstatus != lastBTLEstatus) { + // print it out! + if (BTLEstatus == ACI_EVT_DEVICE_STARTED) { + Serial.println(F("* Advertising started")); + } + if (BTLEstatus == ACI_EVT_CONNECTED) { + Serial.println(F("* Connected!")); + // initialize Firmata cleanly + firmataInit(); + } + if (BTLEstatus == ACI_EVT_DISCONNECTED) { + Serial.println(F("* Disconnected or advertising timed out")); + } + // OK set the last status change to this one + lastBTLEstatus = BTLEstatus; + } + + // if not connected... bail + if (BTLEstatus != ACI_EVT_CONNECTED) { + delay(100); + return; + } + + // For debugging, see if there's data on the serial console, we would forwad it to BTLE + if (Serial.available()) { + BLEserial.write(Serial.read()); + } + + // Onto the Firmata main loop + + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * BTLE buffer using Serial.print() */ + checkDigitalInputs(); + + /* SERIALREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while(BLE_Firmata.available()) { + //Serial.println(F("*data available*")); + BLE_Firmata.processInput(); + } + /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + + // make the sampling interval longer if we have more analog inputs! + uint8_t analogreportnums = 0; + for(uint8_t a=0; a<8; a++) { + if (analogInputsToReport & (1 << a)) { + analogreportnums++; + } + } + + samplingInterval = (uint16_t)MINIMUM_SAMPLE_DELAY + (uint16_t)ANALOG_SAMPLE_DELAY * (1+analogreportnums); + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for(pin=0; pinsetTimeout(rcv); + receiveTimeout = rcv; + } + if(rst) resetTimeout = rst; + if(con) connectTimeout = con; + if(ipd) ipdTimeout = ipd; +} + +// Override boot marker string, or pass NULL to restore default. +void Adafruit_ESP8266::setBootMarker(Fstr *s) { + bootMarker = s ? s : defaultBootMarker; +} + +// Anything printed to the EPS8266 object will be split to both the WiFi +// and debug streams. Saves having to print everything twice in debug code. +size_t Adafruit_ESP8266::write(uint8_t c) { + if(debug) { + if(!writing) { + debug->print(F("---> ")); + writing = true; + } + debug->write(c); + } + return stream->write(c); +} + +// Equivalent to Arduino Stream find() function, but with search string in +// flash/PROGMEM rather than RAM-resident. Returns true if string found +// (any further pending input remains in stream), false if timeout occurs. +// Can optionally pass NULL (or no argument) to read/purge the OK+CR/LF +// returned by most AT commands. The ipd flag indicates this call follows +// a CIPSEND request and might be broken into multiple sections with +IPD +// delimiters, which must be parsed and handled (as the search string may +// cross these delimiters and/or contain \r or \n itself). +boolean Adafruit_ESP8266::find(Fstr *str, boolean ipd) { + uint8_t stringLength, matchedLength = 0; + int c; + boolean found = false; + uint32_t t, save; + uint16_t bytesToGo = 0; + + if(ipd) { // IPD stream stalls really long occasionally, what gives? + save = receiveTimeout; + setTimeouts(ipdTimeout); + } + + if(str == NULL) str = F("OK\r\n"); + stringLength = strlen_P((Pchr *)str); + + if(debug && writing) { + debug->print(F("<--- '")); + writing = false; + } + + for(t = millis();;) { + if(ipd && !bytesToGo) { // Expecting next IPD marker? + if(find(F("+IPD,"))) { // Find marker in stream + for(;;) { + if((c = stream->read()) > 0) { // Read subsequent chars... + if(debug) debug->write(c); + if(c == ':') break; // ...until delimiter. + bytesToGo = (bytesToGo * 10) + (c - '0'); // Keep count + t = millis(); // Timeout resets w/each byte received + } else if(c < 0) { // No data on stream, check for timeout + if((millis() - t) > receiveTimeout) goto bail; + } else goto bail; // EOD on stream + } + } else break; // OK (EOD) or ERROR + } + + if((c = stream->read()) > 0) { // Character received? + if(debug) debug->write(c); // Copy to debug stream + bytesToGo--; + if(c == pgm_read_byte((Pchr *)str + + matchedLength)) { // Match next byte? + if(++matchedLength == stringLength) { // Matched whole string? + found = true; // Winner! + break; + } + } else { // Character mismatch; reset match pointer+counter + matchedLength = 0; + } + t = millis(); // Timeout resets w/each byte received + } else if(c < 0) { // No data on stream, check for timeout + if((millis() - t) > receiveTimeout) break; // You lose, good day sir + } else break; // End-of-data on stream + } + + bail: // Sorry, dreaded goto. Because nested loops. + if(debug) debug->println('\''); + if(ipd) setTimeouts(save); + return found; +} + +// Read from ESP8266 stream into RAM, up to a given size. Max number of +// chars read is 1 less than this, so NUL can be appended on string. +int Adafruit_ESP8266::readLine(char *buf, int bufSiz) { + if(debug && writing) { + debug->print(F("<--- '")); + writing = false; + } + int bytesRead = stream->readBytesUntil('\r', buf, bufSiz-1); + buf[bytesRead] = 0; + if(debug) { + debug->print(buf); + debug->println('\''); + } + while(stream->read() != '\n'); // Discard thru newline + return bytesRead; +} + +// ESP8266 is reset by momentarily connecting RST to GND. Level shifting is +// not necessary provided you don't accidentally set the pin to HIGH output. +// It's generally safe-ish as the default Arduino pin state is INPUT (w/no +// pullup) -- setting to LOW provides an open-drain for reset. +// Returns true if expected boot message is received (or if RST is unused), +// false otherwise. +boolean Adafruit_ESP8266::hardReset(void) { + if(reset_pin < 0) return true; + digitalWrite(reset_pin, LOW); + pinMode(reset_pin, OUTPUT); // Open drain; reset -> GND + delay(10); // Hold a moment + pinMode(reset_pin, INPUT); // Back to high-impedance pin state + return find(bootMarker); // Purge boot message from stream +} + +// Soft reset. Returns true if expected boot message received, else false. +boolean Adafruit_ESP8266::softReset(void) { + boolean found = false; + uint32_t save = receiveTimeout; // Temporarily override recveive timeout, + receiveTimeout = resetTimeout; // reset time is longer than normal I/O. + println(F("AT+RST")); // Issue soft-reset command + if(find(bootMarker)) { // Wait for boot message + println(F("ATE0")); // Turn off echo + found = find(); // OK? + } + receiveTimeout = save; // Restore normal receive timeout + return found; +} + +// For interactive debugging...shuttle data between Serial Console <-> WiFi +void Adafruit_ESP8266::debugLoop(void) { + if(!debug) for(;;); // If no debug connection, nothing to do. + + debug->println(F("\n========================")); + for(;;) { + if(debug->available()) stream->write(debug->read()); + if(stream->available()) debug->write(stream->read()); + } +} + +// Connect to WiFi access point. SSID and password are flash-resident +// strings. May take several seconds to execute, this is normal. +// Returns true on successful connection, false otherwise. +boolean Adafruit_ESP8266::connectToAP(Fstr *ssid, Fstr *pass) { + char buf[256]; + + println(F("AT+CWMODE=1")); // WiFi mode = Sta + readLine(buf, sizeof(buf)); + if(!(strstr_P(buf, (Pchr *)F("OK")) || + strstr_P(buf, (Pchr *)F("no change")))) return false; + + print(F("AT+CWJAP=\"")); // Join access point + print(ssid); + print(F("\",\"")); + print(pass); + println('\"'); + uint32_t save = receiveTimeout; // Temporarily override recv timeout, + receiveTimeout = connectTimeout; // connection time is much longer! + boolean found = find(); // Await 'OK' message + receiveTimeout = save; // Restore normal receive timeout + if(found) { + println(F("AT+CIPMUX=0")); // Set single-client mode + found = find(); // Await 'OK' + } + + return found; +} + +void Adafruit_ESP8266::closeAP(void) { + println(F("AT+CWQAP")); // Quit access point + find(); // Purge 'OK' +} + +// Open TCP connection. Hostname is flash-resident string. +// Returns true on successful connection, else false. +boolean Adafruit_ESP8266::connectTCP(Fstr *h, int port) { + + print(F("AT+CIPSTART=\"TCP\",\"")); + print(h); + print(F("\",")); + println(port); + + if(find(F("Linked"))) { + host = h; + return true; + } + return false; +} + +void Adafruit_ESP8266::closeTCP(void) { + println(F("AT+CIPCLOSE")); + find(F("Unlink\r\n")); +} + +// Requests page from currently-open TCP connection. URL is +// flash-resident string. Returns true if request issued successfully, +// else false. Calling function should then handle data returned, may +// need to parse IPD delimiters (see notes in find() function. +// (Can call find(F("Unlink"), true) to dump to debug.) +boolean Adafruit_ESP8266::requestURL(Fstr *url) { + print(F("AT+CIPSEND=")); + println(25 + strlen_P((Pchr *)url) + strlen_P((Pchr *)host)); + if(find(F("> "))) { // Wait for prompt + print(F("GET ")); // 4 + print(url); + print(F(" HTTP/1.1\r\nHost: ")); // 17 + print(host); + print(F("\r\n\r\n")); // 4 + return(find()); // Gets 'SEND OK' line + } + return false; +} + +// Requests page from currently-open TCP connection. URL is +// character string in SRAM. Returns true if request issued successfully, +// else false. Calling function should then handle data returned, may +// need to parse IPD delimiters (see notes in find() function. +// (Can call find(F("Unlink"), true) to dump to debug.) +boolean Adafruit_ESP8266::requestURL(char* url) { + print(F("AT+CIPSEND=")); + println(25 + strlen(url) + strlen_P((Pchr *)host)); + if(find(F("> "))) { // Wait for prompt + print(F("GET ")); // 4 + print(url); + print(F(" HTTP/1.1\r\nHost: ")); // 17 + print(host); + print(F("\r\n\r\n")); // 4 + return(find()); // Gets 'SEND OK' line + } + return false; +} \ No newline at end of file diff --git a/libraries/Adafruit_ESP8266/Adafruit_ESP8266.h b/libraries/Adafruit_ESP8266/Adafruit_ESP8266.h new file mode 100644 index 0000000..9ec9e95 --- /dev/null +++ b/libraries/Adafruit_ESP8266/Adafruit_ESP8266.h @@ -0,0 +1,65 @@ +/*------------------------------------------------------------------------ + An Arduino library for the ESP8266 WiFi-serial bridge + + https://www.adafruit.com/product/2282 + + The ESP8266 is a 3.3V device. Safe operation with 5V devices (most + Arduino boards) requires a logic-level shifter for TX and RX signals. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried and Phil Burgess for Adafruit Industries. + MIT license, all text above must be included in any redistribution. + ------------------------------------------------------------------------*/ + +#ifndef _ADAFRUIT_ESP8266_H_ +#define _ADAFRUIT_ESP8266_H_ + +#include + +#define ESP_RECEIVE_TIMEOUT 1000L +#define ESP_RESET_TIMEOUT 5000L +#define ESP_CONNECT_TIMEOUT 15000L +#define ESP_IPD_TIMEOUT 120000L + +typedef const __FlashStringHelper Fstr; // PROGMEM/flash-resident string +typedef const PROGMEM char Pchr; // Ditto, kindasorta + +#define defaultBootMarker F("ready\r\n") + +// Subclassing Print makes debugging easier -- output en route to +// WiFi module can be duplicated on a second stream (e.g. Serial). +class Adafruit_ESP8266 : public Print { + public: + Adafruit_ESP8266(Stream *s = &Serial, Stream *d = NULL, int8_t r = -1); + boolean hardReset(void), + softReset(void), + find(Fstr *str = NULL, boolean ipd = false), + connectToAP(Fstr *ssid, Fstr *pass), + connectTCP(Fstr *host, int port), + requestURL(Fstr *url), + requestURL(char* url); + int readLine(char *buf, int bufSiz); + void closeAP(void), + closeTCP(void), + debugLoop(void), + setDebug(Stream *d = NULL), + setTimeouts(uint32_t rcv = ESP_RECEIVE_TIMEOUT, + uint32_t rst = ESP_RESET_TIMEOUT, + uint32_t con = ESP_CONNECT_TIMEOUT, + uint32_t ipd = ESP_IPD_TIMEOUT), + setBootMarker(Fstr *s = NULL); + private: + Stream *stream, // -> ESP8266, e.g. SoftwareSerial or Serial1 + *debug; // -> host, e.g. Serial + uint32_t receiveTimeout, resetTimeout, connectTimeout, ipdTimeout; + int8_t reset_pin; // -1 if RST not connected + Fstr *host, // Non-NULL when TCP connection open + *bootMarker; // String indicating successful boot + boolean writing; + virtual size_t write(uint8_t); // Because Print subclass +}; + +#endif // _ADAFRUIT_ESP8266_H_ diff --git a/libraries/Adafruit_ESP8266/README.md b/libraries/Adafruit_ESP8266/README.md new file mode 100644 index 0000000..901c6f4 --- /dev/null +++ b/libraries/Adafruit_ESP8266/README.md @@ -0,0 +1,4 @@ +Adafruit_ESP8266 +================ + +Example code for ESP8266 chipset diff --git a/libraries/Adafruit_ESP8266/examples/webclient/webclient.ino b/libraries/Adafruit_ESP8266/examples/webclient/webclient.ino new file mode 100644 index 0000000..599868e --- /dev/null +++ b/libraries/Adafruit_ESP8266/examples/webclient/webclient.ino @@ -0,0 +1,117 @@ +/*------------------------------------------------------------------------ + Simple ESP8266 test. Requires SoftwareSerial and an ESP8266 that's been + flashed with recent 'AT' firmware operating at 9600 baud. Only tested + w/Adafruit-programmed modules: https://www.adafruit.com/product/2282 + + The ESP8266 is a 3.3V device. Safe operation with 5V devices (most + Arduino boards) requires a logic-level shifter for TX and RX signals. + ------------------------------------------------------------------------*/ + +#include +#include + +#define ESP_RX 2 +#define ESP_TX 3 +#define ESP_RST 4 +SoftwareSerial softser(ESP_RX, ESP_TX); + +// Must declare output stream before Adafruit_ESP8266 constructor; can be +// a SoftwareSerial stream, or Serial/Serial1/etc. for UART. +Adafruit_ESP8266 wifi(&softser, &Serial, ESP_RST); +// Must call begin() on the stream(s) before using Adafruit_ESP8266 object. + +#define ESP_SSID "SSIDNAME" // Your network name here +#define ESP_PASS "PASSWORD" // Your network password here + +#define HOST "www.adafruit.com" // Host to contact +#define PAGE "/testwifi/index.html" // Web page to request +#define PORT 80 // 80 = HTTP default port + +#define LED_PIN 13 + +void setup() { + char buffer[50]; + + // Flash LED on power-up + pinMode(LED_PIN, OUTPUT); + for(uint8_t i=0; i<3; i++) { + digitalWrite(13, HIGH); delay(50); + digitalWrite(13, LOW); delay(100); + } + + // This might work with other firmware versions (no guarantees) + // by providing a string to ID the tail end of the boot message: + + // comment/replace this if you are using something other than v 0.9.2.4! + wifi.setBootMarker(F("Version:0.9.2.4]\r\n\r\nready")); + + softser.begin(9600); // Soft serial connection to ESP8266 + Serial.begin(57600); while(!Serial); // UART serial debug + + Serial.println(F("Adafruit ESP8266 Demo")); + + // Test if module is ready + Serial.print(F("Hard reset...")); + if(!wifi.hardReset()) { + Serial.println(F("no response from module.")); + for(;;); + } + Serial.println(F("OK.")); + + Serial.print(F("Soft reset...")); + if(!wifi.softReset()) { + Serial.println(F("no response from module.")); + for(;;); + } + Serial.println(F("OK.")); + + Serial.print(F("Checking firmware version...")); + wifi.println(F("AT+GMR")); + if(wifi.readLine(buffer, sizeof(buffer))) { + Serial.println(buffer); + wifi.find(); // Discard the 'OK' that follows + } else { + Serial.println(F("error")); + } + + Serial.print(F("Connecting to WiFi...")); + if(wifi.connectToAP(F(ESP_SSID), F(ESP_PASS))) { + + // IP addr check isn't part of library yet, but + // we can manually request and place in a string. + Serial.print(F("OK\nChecking IP addr...")); + wifi.println(F("AT+CIFSR")); + if(wifi.readLine(buffer, sizeof(buffer))) { + Serial.println(buffer); + wifi.find(); // Discard the 'OK' that follows + + Serial.print(F("Connecting to host...")); + if(wifi.connectTCP(F(HOST), PORT)) { + Serial.print(F("OK\nRequesting page...")); + if(wifi.requestURL(F(PAGE))) { + Serial.println("OK\nSearching for string..."); + // Search for a phrase in the open stream. + // Must be a flash-resident string (F()). + if(wifi.find(F("working"), true)) { + Serial.println(F("found!")); + } else { + Serial.println(F("not found.")); + } + } else { // URL request failed + Serial.println(F("error")); + } + wifi.closeTCP(); + } else { // TCP connect failed + Serial.println(F("D'oh!")); + } + } else { // IP addr check failed + Serial.println(F("error")); + } + wifi.closeAP(); + } else { // WiFi connection failed + Serial.println(F("FAIL")); + } +} + +void loop() { +} diff --git a/libraries/Adafruit_ESP8266/library.properties b/libraries/Adafruit_ESP8266/library.properties new file mode 100644 index 0000000..cdbc454 --- /dev/null +++ b/libraries/Adafruit_ESP8266/library.properties @@ -0,0 +1,9 @@ +name=Adafruit ESP8266 +version=1.0.0 +author=Adafruit +maintainer=Adafruit +sentence=Example code for ESP8266 chipset +paragraph=Example code for ESP8266 chipset +category=Communication +url=https://github.com/adafruit/Adafruit_ESP8266 +architectures=* diff --git a/libraries/Adafruit_ESP8266/previous/Adafruit_ESP8266.ino b/libraries/Adafruit_ESP8266/previous/Adafruit_ESP8266.ino new file mode 100644 index 0000000..edc97b1 --- /dev/null +++ b/libraries/Adafruit_ESP8266/previous/Adafruit_ESP8266.ino @@ -0,0 +1,367 @@ +// This is a super simple demo program for ESP8266's that can use software serial +// @ 9600 baud. Requires firmware that runs at 9600 baud, only tested with Adafruit +// programmed modules! + +#include +#define SSID "SSIDNAME" //your wifi ssid here +#define PASS "PASSWORD" //your wifi key here + +// www.adafruit.com/testwifi/index.html +#define HOST "www.adafruit.com" +#define WEBPAGE "/testwifi/index.html" +#define PORT "80" + +#define ESP_RST 4 + +// Use software serial (check to make sure these are valid softserial pins!) +#define ESP_RX 2 +#define ESP_TX 3 +SoftwareSerial softser(ESP_RX, ESP_TX); // RX, TX +Stream *esp = &softser; + +// can also do +// Stream *esp = &Serial1; + +#define REPLYBUFFSIZ 255 +char replybuffer[REPLYBUFFSIZ]; +uint8_t getReply(char *send, uint16_t timeout = 500, boolean echo = true); +uint8_t espreadline(uint16_t timeout = 500, boolean multiline = false); +boolean sendCheckReply(char *send, char *reply, uint16_t timeout = 500); + + +enum {WIFI_ERROR_NONE=0, WIFI_ERROR_AT, WIFI_ERROR_RST, WIFI_ERROR_SSIDPWD, WIFI_ERROR_SERVER, WIFI_ERROR_UNKNOWN}; + +void setup() +{ + pinMode(9, OUTPUT); + pinMode(13, OUTPUT); + + //blink led13 to indicate power up + for(int i = 0; i<3; i++) + { + digitalWrite(13,HIGH); + delay(50); + digitalWrite(13,LOW); + delay(100); + } + + // Serial debug console + Serial.begin(115200); + // Set time to wait for response strings to be found + + //Open software serial for chatting to ESP + softser.begin(9600); // requires new firmware! + // OR use hardware serial + //Serial1.begin(9600); + + Serial.println(F("Adafruit's ESP8266 Demo")); + + Serial.println(F("Hard reset...")); + // hard reset if you can + pinMode(ESP_RST, INPUT); + digitalWrite(ESP_RST, LOW); + pinMode(ESP_RST, OUTPUT); + delay(100); + pinMode(ESP_RST, INPUT); + delay(2000); + + //test if the module is ready + if(! espReset()) { + Serial.println("Module didn't respond :("); + debugLoop(); + } + + Serial.println(F("ESP Module is ready! :)")); + + //connect to the wifi + byte err = setupWiFi(); + + if (err) { + // error, print error code + Serial.print("setup error:"); Serial.println((int)err); + + debugLoop(); + } + + // success, print IP + uint32_t ip = getIP(); + Serial.print("ESP setup success, my IP addr:"); + if (ip) { + Serial.println(ip, HEX); + } else { + Serial.println("none"); + } + + sendCheckReply("AT+CIPSTO=0", "OK"); + + //set the single connection mode +} + +boolean ESP_GETpage(char *host, uint16_t port, char *page) { + String cmd = "AT+CIPSTART=\"TCP\",\""; + cmd += host; + cmd += "\","; + cmd += port; + cmd.toCharArray(replybuffer, REPLYBUFFSIZ); + + getReply(replybuffer); + + if (strcmp(replybuffer, "OK") != 0) { + // this is OK! could be a version that says "Linked" + if (strcmp(replybuffer, "Linked") != 0) { + sendCheckReply("AT+CIPCLOSE", "OK"); + return false; + } + } + + String request = "GET "; + request += page; + request += " HTTP/1.1\r\nHost: "; + request += host; + request += "\r\n\r\n"; + + cmd = "AT+CIPSEND="; + cmd += request.length(); + cmd.toCharArray(replybuffer, REPLYBUFFSIZ); + sendCheckReply(replybuffer, ">"); + + Serial.print("Sending: "); Serial.println(request.length()); + Serial.println(F("*********SENDING*********")); + Serial.print(request); + Serial.println(F("*************************")); + + request.toCharArray(replybuffer, REPLYBUFFSIZ); + + esp->println(request); + + while (true) { + espreadline(3000); // this is the 'echo' from the data + Serial.print(">"); Serial.println(replybuffer); // probably the 'busy s...' + + // LOOK AT ALL THESE POSSIBLE ARBITRARY RESPONSES!!! + if (strstr(replybuffer, "wrong syntax")) + continue; + else if (strstr(replybuffer, "ERROR")) + continue; + else if (strstr(replybuffer, "busy s...")) + continue; + else break; + } + + if (! strstr(replybuffer, "SEND OK") ) return false; + + espreadline(1000); Serial.print("3>"); Serial.println(replybuffer); + char *s = strstr(replybuffer, "+IPD,"); + if (!s) return false; + uint16_t len = atoi(s+5); + //Serial.print(len); Serial.println(" bytes total"); + + int16_t contentlen = 0; + while (1) { + espreadline(50); + s = strstr(replybuffer, "Content-Length: "); + if (s) { + //Serial.println(replybuffer); + contentlen = atoi(s+16); + Serial.print(F("C-Len = ")); Serial.println(contentlen); + } + s = strstr(replybuffer, "Content-Type: "); + if (s && contentlen) { + int16_t i; + char c; + + for (i=-2; iavailable()); + c = esp->read(); //UDR0 = c; + if (i >= 0) { + replybuffer[i] = c; + } + } + replybuffer[i] = 0; + return true; + } + } + //while (1) { + // if (esp.available()) UDR0 = esp.read(); + //} +} + +void loop() +{ + ESP_GETpage(HOST, 80, WEBPAGE); + + Serial.println(F("**********REPLY***********")); + Serial.print(replybuffer); + Serial.println(F("**************************")); + + sendCheckReply("AT+CIPCLOSE", "OK"); + + debugLoop(); + + delay(1000); + + while (1); + +} + +boolean getVersion() { + // Get version? + getReply("AT+GMR", 250, true); +} + +boolean espReset() { + getReply("AT+RST", 1000, true); + if (! strstr(replybuffer, "OK")) return false; + delay(2000); + + // turn off echo + getReply("ATE0", 250, true); + + return true; +} + +boolean ESPconnectAP(char *s, char *p) { + + getReply("AT+CWMODE=1", 500, true); + if (! (strstr(replybuffer, "OK") || strstr(replybuffer, "no change")) ) + return false; + + String connectStr = "AT+CWJAP=\""; + connectStr += SSID; + connectStr += "\",\""; + connectStr += PASS; + connectStr += "\""; + connectStr.toCharArray(replybuffer, REPLYBUFFSIZ); + getReply(replybuffer, 500, true); + espreadline(5000); + Serial.print("<-- "); Serial.println(replybuffer); + + return (strstr(replybuffer, "OK") != 0); +} + + +byte setupWiFi() { + // reset WiFi module + Serial.println(F("Soft resetting...")); + if (!espReset()) + return WIFI_ERROR_RST; + + delay(1000); + + Serial.println(F("Checking for ESP AT response")); + + if (!sendCheckReply("AT", "OK")) + return WIFI_ERROR_AT; + + getVersion(); + Serial.print(F("Firmware Version #")); Serial.println(replybuffer); + + Serial.print(F("Connecting to ")); Serial.println(SSID); + if (!ESPconnectAP(SSID, PASS)) + return WIFI_ERROR_SSIDPWD; + + Serial.println(F("Single Client Mode")); + if (!sendCheckReply("AT+CIPMUX=0", "OK")) + return WIFI_ERROR_SERVER; + + return WIFI_ERROR_NONE; +} + +// NOT IMPLEMENTED YET! +uint32_t getIP() { + getReply("AT+CIFSR", 500, true); + + return 0; +} + + + + +/************************/ +uint8_t espreadline(uint16_t timeout, boolean multiline) { + uint16_t replyidx = 0; + + while (timeout--) { + if (replyidx > REPLYBUFFSIZ-1) break; + + while(esp->available()) { + char c = esp->read(); + if (c == '\r') continue; + if (c == 0xA) { + if (replyidx == 0) // the first 0x0A is ignored + continue; + + if (!multiline) { + timeout = 0; // the second 0x0A is the end of the line + break; + } + } + replybuffer[replyidx] = c; + // Serial.print(c, HEX); Serial.print("#"); Serial.println(c); + replyidx++; + } + + if (timeout == 0) break; + delay(1); + } + replybuffer[replyidx] = 0; // null term + return replyidx; +} + +uint8_t getReply(char *send, uint16_t timeout, boolean echo) { + // flush input + while(esp->available()) { + esp->read(); + } + + if (echo) { + Serial.print("---> "); Serial.println(send); + } + esp->println(send); + + // eat first reply sentence (echo) + uint8_t readlen = espreadline(timeout); + + //Serial.print("echo? "); Serial.print(readlen); Serial.print(" vs "); Serial.println(strlen(send)); + + if (strncmp(send, replybuffer, readlen) == 0) { + // its an echo, read another line! + readlen = espreadline(); + } + + if (echo) { + Serial.print ("<--- "); Serial.println(replybuffer); + } + return readlen; +} + +boolean sendCheckReply(char *send, char *reply, uint16_t timeout) { + getReply(send, timeout, true); + +/* + for (uint8_t i=0; iwrite(Serial.read()); + delay(1); + } + if (esp->available()) { + Serial.write(esp->read()); + delay(1); + } + } +} diff --git a/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp b/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp new file mode 100644 index 0000000..8490d0f --- /dev/null +++ b/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp @@ -0,0 +1,2238 @@ +/*------------------------------------------------------------------------- + Arduino library to control a wide variety of WS2811- and WS2812-based RGB + LED devices such as Adafruit FLORA RGB Smart Pixels and NeoPixel strips. + Currently handles 400 and 800 KHz bitstreams on 8, 12 and 16 MHz ATmega + MCUs, with LEDs wired for various color orders. Handles most output pins + (possible exception with upper PORT registers on the Arduino Mega). + + Written by Phil Burgess / Paint Your Dragon for Adafruit Industries, + contributions by PJRC, Michael Miller and other members of the open + source community. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products + from Adafruit! + + ------------------------------------------------------------------------- + This file is part of the Adafruit NeoPixel library. + + NeoPixel 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 3 of + the License, or (at your option) any later version. + + NeoPixel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with NeoPixel. If not, see + . + -------------------------------------------------------------------------*/ + +#include "Adafruit_NeoPixel.h" + +#if defined(NRF52) +#include "nrf.h" + +// Interrupt is only disabled if there is no PWM device available +// Note: Adafruit Bluefruit nrf52 does not use this option +//#define NRF52_DISABLE_INT +#endif + +// Constructor when length, pin and type are known at compile-time: +Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, neoPixelType t) : + begun(false), brightness(0), pixels(NULL), endTime(0) +{ + updateType(t); + updateLength(n); + setPin(p); +} + +// via Michael Vogt/neophob: empty constructor is used when strand length +// isn't known at compile-time; situations where program config might be +// read from internal flash memory or an SD card, or arrive via serial +// command. If using this constructor, MUST follow up with updateType(), +// updateLength(), etc. to establish the strand type, length and pin number! +Adafruit_NeoPixel::Adafruit_NeoPixel() : +#ifdef NEO_KHZ400 + is800KHz(true), +#endif + begun(false), numLEDs(0), numBytes(0), pin(-1), brightness(0), pixels(NULL), + rOffset(1), gOffset(0), bOffset(2), wOffset(1), endTime(0) +{ +} + +Adafruit_NeoPixel::~Adafruit_NeoPixel() { + if(pixels) free(pixels); + if(pin >= 0) pinMode(pin, INPUT); +} + +void Adafruit_NeoPixel::begin(void) { + if(pin >= 0) { + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); + } + begun = true; + +} + +void Adafruit_NeoPixel::updateLength(uint16_t n) { + if(pixels) free(pixels); // Free existing data (if any) + + // Allocate new data -- note: ALL PIXELS ARE CLEARED + numBytes = n * ((wOffset == rOffset) ? 3 : 4); + if((pixels = (uint8_t *)malloc(numBytes))) { + memset(pixels, 0, numBytes); + numLEDs = n; + } else { + numLEDs = numBytes = 0; + } +} + +void Adafruit_NeoPixel::updateType(neoPixelType t) { + boolean oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW + + wOffset = (t >> 6) & 0b11; // See notes in header file + rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets + gOffset = (t >> 2) & 0b11; + bOffset = t & 0b11; +#ifdef NEO_KHZ400 + is800KHz = (t < 256); // 400 KHz flag is 1<<8 +#endif + + // If bytes-per-pixel has changed (and pixel data was previously + // allocated), re-allocate to new size. Will clear any data. + if(pixels) { + boolean newThreeBytesPerPixel = (wOffset == rOffset); + if(newThreeBytesPerPixel != oldThreeBytesPerPixel) updateLength(numLEDs); + } +} + +#if defined(ESP8266) +// ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution +extern "C" void ICACHE_RAM_ATTR espShow( + uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t type); +#elif defined(ESP32) +extern "C" void espShow( + uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t type); +#endif // ESP8266 + +void Adafruit_NeoPixel::show(void) { + + if(!pixels) return; + + // Data latch = 300+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while(!canShow()); + // endTime is a private member (rather than global var) so that multiple + // instances on different pins can be quickly issued in succession (each + // instance doesn't delay the next). + + // In order to make this code runtime-configurable to work with any pin, + // SBI/CBI instructions are eschewed in favor of full PORT writes via the + // OUT or ST instructions. It relies on two facts: that peripheral + // functions (such as PWM) take precedence on output pins, so our PORT- + // wide writes won't interfere, and that interrupts are globally disabled + // while data is being issued to the LEDs, so no other code will be + // accessing the PORT. The code takes an initial 'snapshot' of the PORT + // state, computes 'pin high' and 'pin low' values, and writes these back + // to the PORT register as needed. + + // NRF52 may use PWM + DMA (if available), may not need to disable interrupt +#ifndef NRF52 + noInterrupts(); // Need 100% focus on instruction timing +#endif + +#ifdef __AVR__ +// AVR MCUs -- ATmega & ATtiny (no XMEGA) --------------------------------- + + volatile uint16_t + i = numBytes; // Loop counter + volatile uint8_t + *ptr = pixels, // Pointer to next byte + b = *ptr++, // Current byte value + hi, // PORT w/output bit set high + lo; // PORT w/output bit set low + + // Hand-tuned assembly code issues data to the LED drivers at a specific + // rate. There's separate code for different CPU speeds (8, 12, 16 MHz) + // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The + // datastream timing for the LED drivers allows a little wiggle room each + // way (listed in the datasheets), so the conditions for compiling each + // case are set up for a range of frequencies rather than just the exact + // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on + // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based + // on the datasheet figures and have not been extensively tested outside + // the canonical 8/12/16 MHz speeds; there's no guarantee these will work + // close to the extremes (or possibly they could be pushed further). + // Keep in mind only one CPU speed case actually gets compiled; the + // resulting program isn't as massive as it might look from source here. + +// 8 MHz(ish) AVR --------------------------------------------------------- +#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + + volatile uint8_t n1, n2 = 0; // First, next bits out + + // Squeezing an 800 KHz stream out of an 8 MHz chip requires code + // specific to each PORT register. + + // 10 instruction clocks per bit: HHxxxxxLLL + // OUT instructions: ^ ^ ^ (T=0,2,7) + + // PORTD OUTPUT ---------------------------------------------------- + +#if defined(PORTD) + #if defined(PORTB) || defined(PORTC) || defined(PORTF) + if(port == &PORTD) { + #endif // defined(PORTB/C/F) + + hi = PORTD | pinMask; + lo = PORTD & ~pinMask; + n1 = lo; + if(b & 0x80) n1 = hi; + + // Dirty trick: RJMPs proceeding to the next instruction are used + // to delay two clock cycles in one instruction word (rather than + // using two NOPs). This was necessary in order to squeeze the + // loop down to exactly 64 words -- the maximum possible for a + // relative branch. + + asm volatile( + "headD:" "\n\t" // Clk Pseudocode + // Bit 7: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 6: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 5: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 4: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 3: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 2: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 1: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet) + // Bit 0: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "brne headD" "\n" // 2 while(i) (Z flag set above) + : [byte] "+r" (b), + [n1] "+r" (n1), + [n2] "+r" (n2), + [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTD)), + [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTB) || defined(PORTC) || defined(PORTF) + } else // other PORT(s) + #endif // defined(PORTB/C/F) +#endif // defined(PORTD) + + // PORTB OUTPUT ---------------------------------------------------- + +#if defined(PORTB) + #if defined(PORTD) || defined(PORTC) || defined(PORTF) + if(port == &PORTB) { + #endif // defined(PORTD/C/F) + + // Same as above, just switched to PORTB and stripped of comments. + hi = PORTB | pinMask; + lo = PORTB & ~pinMask; + n1 = lo; + if(b & 0x80) n1 = hi; + + asm volatile( + "headB:" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 6" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 5" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 4" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 3" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 2" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 1" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 0" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "brne headB" "\n" + : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTC) || defined(PORTF) + } + #endif + #if defined(PORTC) || defined(PORTF) + else + #endif // defined(PORTC/F) +#endif // defined(PORTB) + + // PORTC OUTPUT ---------------------------------------------------- + +#if defined(PORTC) + #if defined(PORTD) || defined(PORTB) || defined(PORTF) + if(port == &PORTC) { + #endif // defined(PORTD/B/F) + + // Same as above, just switched to PORTC and stripped of comments. + hi = PORTC | pinMask; + lo = PORTC & ~pinMask; + n1 = lo; + if(b & 0x80) n1 = hi; + + asm volatile( + "headC:" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 6" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 5" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 4" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 3" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 2" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 1" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 0" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "brne headC" "\n" + : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTB) || defined(PORTF) + } + #endif // defined(PORTD/B/F) + #if defined(PORTF) + else + #endif +#endif // defined(PORTC) + + // PORTF OUTPUT ---------------------------------------------------- + +#if defined(PORTF) + #if defined(PORTD) || defined(PORTB) || defined(PORTC) + if(port == &PORTF) { + #endif // defined(PORTD/B/C) + + hi = PORTF | pinMask; + lo = PORTF & ~pinMask; + n1 = lo; + if(b & 0x80) n1 = hi; + + asm volatile( + "headF:" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 6" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 5" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 4" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 3" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 2" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 1" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 0" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "brne headF" "\n" + : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTB) || defined(PORTC) + } + #endif // defined(PORTD/B/C) +#endif // defined(PORTF) + +#ifdef NEO_KHZ400 + } else { // end 800 KHz, do 400 KHz + + // Timing is more relaxed; unrolling the inner loop for each bit is + // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out + // of need but just to trim the code size down a little. + // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical + // to the 800-on-16 code later -- the hi/lo timing between WS2811 and + // WS2812 is not simply a 2:1 scale! + + // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,4,10) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "dec %[bit]" "\n\t" // 1 bit-- (T = 8) + "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) + "nextbyte20:" "\n\t" // (T = 10) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) + "nop" "\n\t" // 1 nop (T = 13) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) + "brne head20" "\n" // 2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [hi] "r" (hi), + [lo] "r" (lo), + [ptr] "e" (ptr)); + } +#endif // NEO_KHZ400 + +// 12 MHz(ish) AVR -------------------------------------------------------- +#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + + // In the 12 MHz case, an optimized 800 KHz datastream (no dead time + // between bytes) requires a PORT-specific loop similar to the 8 MHz + // code (but a little more relaxed in this case). + + // 15 instruction clocks per bit: HHHHxxxxxxLLLLL + // OUT instructions: ^ ^ ^ (T=0,4,10) + + volatile uint8_t next; + + // PORTD OUTPUT ---------------------------------------------------- + +#if defined(PORTD) + #if defined(PORTB) || defined(PORTC) || defined(PORTF) + if(port == &PORTD) { + #endif // defined(PORTB/C/F) + + hi = PORTD | pinMask; + lo = PORTD & ~pinMask; + next = lo; + if(b & 0x80) next = hi; + + // Don't "optimize" the OUT calls into the bitTime subroutine; + // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs! + asm volatile( + "headD:" "\n\t" // (T = 0) + "out %[port], %[hi]" "\n\t" // (T = 1) + "rcall bitTimeD" "\n\t" // Bit 7 (T = 15) + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 6 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 5 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 4 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 3 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 2 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 1 + // Bit 0: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "rjmp .+0" "\n\t" // 2 nop nop (T = 3) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5) + "out %[port] , %[next]" "\n\t" // 1 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13) + "brne headD" "\n\t" // 2 if(i != 0) -> (next byte) + "rjmp doneD" "\n\t" + "bitTimeD:" "\n\t" // nop nop nop (T = 4) + "out %[port], %[next]" "\n\t" // 1 PORT = next (T = 5) + "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7) + "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "out %[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "ret" "\n\t" // 4 nop nop nop nop (T = 15) + "doneD:" "\n" + : [byte] "+r" (b), + [next] "+r" (next), + [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTD)), + [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTB) || defined(PORTC) || defined(PORTF) + } else // other PORT(s) + #endif // defined(PORTB/C/F) +#endif // defined(PORTD) + + // PORTB OUTPUT ---------------------------------------------------- + +#if defined(PORTB) + #if defined(PORTD) || defined(PORTC) || defined(PORTF) + if(port == &PORTB) { + #endif // defined(PORTD/C/F) + + hi = PORTB | pinMask; + lo = PORTB & ~pinMask; + next = lo; + if(b & 0x80) next = hi; + + // Same as above, just set for PORTB & stripped of comments + asm volatile( + "headB:" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port] , %[hi]" "\n\t" + "rjmp .+0" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "out %[port] , %[next]" "\n\t" + "mov %[next] , %[lo]" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[next] , %[hi]" "\n\t" + "nop" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "brne headB" "\n\t" + "rjmp doneB" "\n\t" + "bitTimeB:" "\n\t" + "out %[port], %[next]" "\n\t" + "mov %[next], %[lo]" "\n\t" + "rol %[byte]" "\n\t" + "sbrc %[byte], 7" "\n\t" + "mov %[next], %[hi]" "\n\t" + "nop" "\n\t" + "out %[port], %[lo]" "\n\t" + "ret" "\n\t" + "doneB:" "\n" + : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTC) || defined(PORTF) + } + #endif + #if defined(PORTC) || defined(PORTF) + else + #endif // defined(PORTC/F) +#endif // defined(PORTB) + + // PORTC OUTPUT ---------------------------------------------------- + +#if defined(PORTC) + #if defined(PORTD) || defined(PORTB) || defined(PORTF) + if(port == &PORTC) { + #endif // defined(PORTD/B/F) + + hi = PORTC | pinMask; + lo = PORTC & ~pinMask; + next = lo; + if(b & 0x80) next = hi; + + // Same as above, just set for PORTC & stripped of comments + asm volatile( + "headC:" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port] , %[hi]" "\n\t" + "rjmp .+0" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "out %[port] , %[next]" "\n\t" + "mov %[next] , %[lo]" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[next] , %[hi]" "\n\t" + "nop" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "brne headC" "\n\t" + "rjmp doneC" "\n\t" + "bitTimeC:" "\n\t" + "out %[port], %[next]" "\n\t" + "mov %[next], %[lo]" "\n\t" + "rol %[byte]" "\n\t" + "sbrc %[byte], 7" "\n\t" + "mov %[next], %[hi]" "\n\t" + "nop" "\n\t" + "out %[port], %[lo]" "\n\t" + "ret" "\n\t" + "doneC:" "\n" + : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTB) || defined(PORTF) + } + #endif // defined(PORTD/B/F) + #if defined(PORTF) + else + #endif +#endif // defined(PORTC) + + // PORTF OUTPUT ---------------------------------------------------- + +#if defined(PORTF) + #if defined(PORTD) || defined(PORTB) || defined(PORTC) + if(port == &PORTF) { + #endif // defined(PORTD/B/C) + + hi = PORTF | pinMask; + lo = PORTF & ~pinMask; + next = lo; + if(b & 0x80) next = hi; + + // Same as above, just set for PORTF & stripped of comments + asm volatile( + "headF:" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeC" "\n\t" + "out %[port] , %[hi]" "\n\t" + "rjmp .+0" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "out %[port] , %[next]" "\n\t" + "mov %[next] , %[lo]" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[next] , %[hi]" "\n\t" + "nop" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "brne headF" "\n\t" + "rjmp doneC" "\n\t" + "bitTimeC:" "\n\t" + "out %[port], %[next]" "\n\t" + "mov %[next], %[lo]" "\n\t" + "rol %[byte]" "\n\t" + "sbrc %[byte], 7" "\n\t" + "mov %[next], %[hi]" "\n\t" + "nop" "\n\t" + "out %[port], %[lo]" "\n\t" + "ret" "\n\t" + "doneC:" "\n" + : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); + + #if defined(PORTD) || defined(PORTB) || defined(PORTC) + } + #endif // defined(PORTD/B/C) +#endif // defined(PORTF) + +#ifdef NEO_KHZ400 + } else { // 400 KHz + + // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,6,15) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head30:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "rjmp .+0" "\n\t" // 2 nop nop (T = 6) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 8) + "rjmp .+0" "\n\t" // 2 nop nop (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "nop" "\n\t" // 1 nop (T = 15) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 17) + "rjmp .+0" "\n\t" // 2 nop nop (T = 19) + "dec %[bit]" "\n\t" // 1 bit-- (T = 20) + "breq nextbyte30" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22) + "rjmp .+0" "\n\t" // 2 nop nop (T = 24) + "rjmp .+0" "\n\t" // 2 nop nop (T = 26) + "rjmp .+0" "\n\t" // 2 nop nop (T = 28) + "rjmp head30" "\n\t" // 2 -> head30 (next bit out) + "nextbyte30:" "\n\t" // (T = 22) + "nop" "\n\t" // 1 nop (T = 23) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28) + "brne head30" "\n" // 1-2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [hi] "r" (hi), + [lo] "r" (lo), + [ptr] "e" (ptr)); + } +#endif // NEO_KHZ400 + +// 16 MHz(ish) AVR -------------------------------------------------------- +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + + // WS2811 and WS2812 have different hi/lo duty cycles; this is + // similar but NOT an exact copy of the prior 400-on-8 code. + + // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL + // ST instructions: ^ ^ ^ (T=0,5,13) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "dec %[bit]" "\n\t" // 1 bit-- (T = 5) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 7) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8) + "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "nop" "\n\t" // 1 nop (T = 13) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) + "nop" "\n\t" // 1 nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) + "nextbyte20:" "\n\t" // (T = 10) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) + "nop" "\n\t" // 1 nop (T = 16) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) + "brne head20" "\n" // 2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +#ifdef NEO_KHZ400 + } else { // 400 KHz + + // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version. + + // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,8,20) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head40:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "rjmp .+0" "\n\t" // 2 nop nop (T = 6) + "rjmp .+0" "\n\t" // 2 nop nop (T = 8) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp .+0" "\n\t" // 2 nop nop (T = 20) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 22) + "nop" "\n\t" // 1 nop (T = 23) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24) + "dec %[bit]" "\n\t" // 1 bit-- (T = 25) + "breq nextbyte40" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27) + "nop" "\n\t" // 1 nop (T = 28) + "rjmp .+0" "\n\t" // 2 nop nop (T = 30) + "rjmp .+0" "\n\t" // 2 nop nop (T = 32) + "rjmp .+0" "\n\t" // 2 nop nop (T = 34) + "rjmp .+0" "\n\t" // 2 nop nop (T = 36) + "rjmp .+0" "\n\t" // 2 nop nop (T = 38) + "rjmp head40" "\n\t" // 2 -> head40 (next bit out) + "nextbyte40:" "\n\t" // (T = 27) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30) + "rjmp .+0" "\n\t" // 2 nop nop (T = 32) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 34) + "rjmp .+0" "\n\t" // 2 nop nop (T = 36) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38) + "brne head40" "\n" // 1-2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + } +#endif // NEO_KHZ400 + +#else + #error "CPU SPEED NOT SUPPORTED" +#endif // end F_CPU ifdefs on __AVR__ + +// END AVR ---------------------------------------------------------------- + + +#elif defined(__arm__) + +// ARM MCUs -- Teensy 3.0, 3.1, LC, Arduino Due --------------------------- + +#if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6 +#define CYCLES_800_T0H (F_CPU / 4000000) +#define CYCLES_800_T1H (F_CPU / 1250000) +#define CYCLES_800 (F_CPU / 800000) +#define CYCLES_400_T0H (F_CPU / 2000000) +#define CYCLES_400_T1H (F_CPU / 833333) +#define CYCLES_400 (F_CPU / 400000) + + uint8_t *p = pixels, + *end = p + numBytes, pix, mask; + volatile uint8_t *set = portSetRegister(pin), + *clr = portClearRegister(pin); + uint32_t cyc; + + ARM_DEMCR |= ARM_DEMCR_TRCENA; + ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + cyc = ARM_DWT_CYCCNT + CYCLES_800; + while(p < end) { + pix = *p++; + for(mask = 0x80; mask; mask >>= 1) { + while(ARM_DWT_CYCCNT - cyc < CYCLES_800); + cyc = ARM_DWT_CYCCNT; + *set = 1; + if(pix & mask) { + while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H); + } else { + while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H); + } + *clr = 1; + } + } + while(ARM_DWT_CYCCNT - cyc < CYCLES_800); +#ifdef NEO_KHZ400 + } else { // 400 kHz bitstream + cyc = ARM_DWT_CYCCNT + CYCLES_400; + while(p < end) { + pix = *p++; + for(mask = 0x80; mask; mask >>= 1) { + while(ARM_DWT_CYCCNT - cyc < CYCLES_400); + cyc = ARM_DWT_CYCCNT; + *set = 1; + if(pix & mask) { + while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H); + } else { + while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H); + } + *clr = 1; + } + } + while(ARM_DWT_CYCCNT - cyc < CYCLES_400); + } +#endif // NEO_KHZ400 + +#elif defined(TEENSYDUINO) && defined(__MKL26Z64__) // Teensy-LC + +#if F_CPU == 48000000 + uint8_t *p = pixels, + pix, count, dly, + bitmask = digitalPinToBitMask(pin); + volatile uint8_t *reg = portSetRegister(pin); + uint32_t num = numBytes; + asm volatile( + "L%=_begin:" "\n\t" + "ldrb %[pix], [%[p], #0]" "\n\t" + "lsl %[pix], #24" "\n\t" + "movs %[count], #7" "\n\t" + "L%=_loop:" "\n\t" + "lsl %[pix], #1" "\n\t" + "bcs L%=_loop_one" "\n\t" + "L%=_loop_zero:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_loop_delay_T0H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T0H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_loop_delay_T0L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T0L" "\n\t" + "b L%=_next" "\n\t" + "L%=_loop_one:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_loop_delay_T1H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T1H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_loop_delay_T1L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T1L" "\n\t" + "nop" "\n\t" + "L%=_next:" "\n\t" + "sub %[count], #1" "\n\t" + "bne L%=_loop" "\n\t" + "lsl %[pix], #1" "\n\t" + "bcs L%=_last_one" "\n\t" + "L%=_last_zero:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_last_delay_T0H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T0H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #10" "\n\t" + "L%=_last_delay_T0L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T0L" "\n\t" + "b L%=_repeat" "\n\t" + "L%=_last_one:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_last_delay_T1H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T1H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #1" "\n\t" + "L%=_last_delay_T1L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T1L" "\n\t" + "nop" "\n\t" + "L%=_repeat:" "\n\t" + "add %[p], #1" "\n\t" + "sub %[num], #1" "\n\t" + "bne L%=_begin" "\n\t" + "L%=_done:" "\n\t" + : [p] "+r" (p), + [pix] "=&r" (pix), + [count] "=&r" (count), + [dly] "=&r" (dly), + [num] "+r" (num) + : [bitmask] "r" (bitmask), + [reg] "r" (reg) + ); +#else +#error "Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz" +#endif // F_CPU == 48000000 + +// Begin of support for NRF52832 based boards ------------------------- + +#elif defined(NRF52) +// [[[Begin of the Neopixel NRF52 EasyDMA implementation +// by the Hackerspace San Salvador]]] +// This technique uses the PWM peripheral on the NRF52. The PWM uses the +// EasyDMA feature included on the chip. This technique loads the duty +// cycle configuration for each cycle when the PWM is enabled. For this +// to work we need to store a 16 bit configuration for each bit of the +// RGB(W) values in the pixel buffer. +// Comparator values for the PWM were hand picked and are guaranteed to +// be 100% organic to preserve freshness and high accuracy. Current +// parameters are: +// * PWM Clock: 16Mhz +// * Minimum step time: 62.5ns +// * Time for zero in high (T0H): 0.31ms +// * Time for one in high (T1H): 0.75ms +// * Cycle time: 1.25us +// * Frequency: 800Khz +// For 400Khz we just double the calculated times. +// ---------- BEGIN Constants for the EasyDMA implementation ----------- +// The PWM starts the duty cycle in LOW. To start with HIGH we +// need to set the 15th bit on each register. + +// WS2812 (rev A) timing is 0.35 and 0.7us +//#define MAGIC_T0H 5UL | (0x8000) // 0.3125us +//#define MAGIC_T1H 12UL | (0x8000) // 0.75us + +// WS2812B (rev B) timing is 0.4 and 0.8 us +#define MAGIC_T0H 6UL | (0x8000) // 0.375us +#define MAGIC_T1H 13UL | (0x8000) // 0.8125us + +// WS2811 (400 khz) timing is 0.5 and 1.2 +#define MAGIC_T0H_400KHz 8UL | (0x8000) // 0.5us +#define MAGIC_T1H_400KHz 19UL | (0x8000) // 1.1875us + +// For 400Khz, we double value of CTOPVAL +#define CTOPVAL 20UL // 1.25us +#define CTOPVAL_400KHz 40UL // 2.5us + +// ---------- END Constants for the EasyDMA implementation ------------- +// +// If there is no device available an alternative cycle-counter +// implementation is tried. +// The nRF52832 runs with a fixed clock of 64Mhz. The alternative +// implementation is the same as the one used for the Teensy 3.0/1/2 but +// with the Nordic SDK HAL & registers syntax. +// The number of cycles was hand picked and is guaranteed to be 100% +// organic to preserve freshness and high accuracy. +// ---------- BEGIN Constants for cycle counter implementation --------- +#define CYCLES_800_T0H 18 // ~0.36 uS +#define CYCLES_800_T1H 41 // ~0.76 uS +#define CYCLES_800 71 // ~1.25 uS + +#define CYCLES_400_T0H 26 // ~0.50 uS +#define CYCLES_400_T1H 70 // ~1.26 uS +#define CYCLES_400 156 // ~2.50 uS +// ---------- END of Constants for cycle counter implementation -------- + + // To support both the SoftDevice + Neopixels we use the EasyDMA + // feature from the NRF25. However this technique implies to + // generate a pattern and store it on the memory. The actual + // memory used in bytes corresponds to the following formula: + // totalMem = numBytes*8*2+(2*2) + // The two additional bytes at the end are needed to reset the + // sequence. + // + // If there is not enough memory, we will fall back to cycle counter + // using DWT + uint32_t pattern_size = numBytes*8*sizeof(uint16_t)+2*sizeof(uint16_t); + uint16_t* pixels_pattern = NULL; + + NRF_PWM_Type* pwm = NULL; + + // Try to find a free PWM device, which is not enabled + // and has no connected pins + NRF_PWM_Type* PWM[3] = {NRF_PWM0, NRF_PWM1, NRF_PWM2}; + for(int device = 0; device<3; device++) { + if( (PWM[device]->ENABLE == 0) && + (PWM[device]->PSEL.OUT[0] & PWM_PSEL_OUT_CONNECT_Msk) && + (PWM[device]->PSEL.OUT[1] & PWM_PSEL_OUT_CONNECT_Msk) && + (PWM[device]->PSEL.OUT[2] & PWM_PSEL_OUT_CONNECT_Msk) && + (PWM[device]->PSEL.OUT[3] & PWM_PSEL_OUT_CONNECT_Msk) + ) { + pwm = PWM[device]; + break; + } + } + + // only malloc if there is PWM device available + if ( pwm != NULL ) { + #ifdef ARDUINO_FEATHER52 // use thread-safe malloc + pixels_pattern = (uint16_t *) rtos_malloc(pattern_size); + #else + pixels_pattern = (uint16_t *) malloc(pattern_size); + #endif + } + + // Use the identified device to choose the implementation + // If a PWM device is available use DMA + if( (pixels_pattern != NULL) && (pwm != NULL) ) { + uint16_t pos = 0; // bit position + + for(uint16_t n=0; n0; mask >>= 1, i++) { + #ifdef NEO_KHZ400 + if( !is800KHz ) { + pixels_pattern[pos] = (pix & mask) ? MAGIC_T1H_400KHz : MAGIC_T0H_400KHz; + }else + #endif + { + pixels_pattern[pos] = (pix & mask) ? MAGIC_T1H : MAGIC_T0H; + } + + pos++; + } + } + + // Zero padding to indicate the end of que sequence + pixels_pattern[++pos] = 0 | (0x8000); // Seq end + pixels_pattern[++pos] = 0 | (0x8000); // Seq end + + // Set the wave mode to count UP + pwm->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos); + + // Set the PWM to use the 16MHz clock + pwm->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos); + + // Setting of the maximum count + // but keeping it on 16Mhz allows for more granularity just + // in case someone wants to do more fine-tuning of the timing. +#ifdef NEO_KHZ400 + if( !is800KHz ) { + pwm->COUNTERTOP = (CTOPVAL_400KHz << PWM_COUNTERTOP_COUNTERTOP_Pos); + }else +#endif + { + pwm->COUNTERTOP = (CTOPVAL << PWM_COUNTERTOP_COUNTERTOP_Pos); + } + + // Disable loops, we want the sequence to repeat only once + pwm->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos); + + // On the "Common" setting the PWM uses the same pattern for the + // for supported sequences. The pattern is stored on half-word + // of 16bits + pwm->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) | + (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos); + + // Pointer to the memory storing the patter + pwm->SEQ[0].PTR = (uint32_t)(pixels_pattern) << PWM_SEQ_PTR_PTR_Pos; + + // Calculation of the number of steps loaded from memory. + pwm->SEQ[0].CNT = (pattern_size/sizeof(uint16_t)) << PWM_SEQ_CNT_CNT_Pos; + + // The following settings are ignored with the current config. + pwm->SEQ[0].REFRESH = 0; + pwm->SEQ[0].ENDDELAY = 0; + + // The Neopixel implementation is a blocking algorithm. DMA + // allows for non-blocking operation. To "simulate" a blocking + // operation we enable the interruption for the end of sequence + // and block the execution thread until the event flag is set by + // the peripheral. +// pwm->INTEN |= (PWM_INTEN_SEQEND0_Enabled<PSEL.OUT[0] = g_ADigitalPinMap[pin]; + + // Enable the PWM + pwm->ENABLE = 1; + + // After all of this and many hours of reading the documentation + // we are ready to start the sequence... + pwm->EVENTS_SEQEND[0] = 0; + pwm->TASKS_SEQSTART[0] = 1; + + // But we have to wait for the flag to be set. + while(!pwm->EVENTS_SEQEND[0]) + { + #ifdef ARDUINO_FEATHER52 + yield(); + #endif + } + + // Before leave we clear the flag for the event. + pwm->EVENTS_SEQEND[0] = 0; + + // We need to disable the device and disconnect + // all the outputs before leave or the device will not + // be selected on the next call. + // TODO: Check if disabling the device causes performance issues. + pwm->ENABLE = 0; + + pwm->PSEL.OUT[0] = 0xFFFFFFFFUL; + + #ifdef ARDUINO_FEATHER52 // use thread-safe free + rtos_free(pixels_pattern); + #else + free(pixels_pattern); + #endif + }// End of DMA implementation + // --------------------------------------------------------------------- + else{ + // Fall back to DWT + #ifdef ARDUINO_FEATHER52 + // Bluefruit Feather 52 uses freeRTOS + // Critical Section is used since it does not block SoftDevice execution + taskENTER_CRITICAL(); + #elif defined(NRF52_DISABLE_INT) + // If you are using the Bluetooth SoftDevice we advise you to not disable + // the interrupts. Disabling the interrupts even for short periods of time + // causes the SoftDevice to stop working. + // Disable the interrupts only in cases where you need high performance for + // the LEDs and if you are not using the EasyDMA feature. + __disable_irq(); + #endif + + uint32_t pinMask = 1UL << g_ADigitalPinMap[pin]; + + uint32_t CYCLES_X00 = CYCLES_800; + uint32_t CYCLES_X00_T1H = CYCLES_800_T1H; + uint32_t CYCLES_X00_T0H = CYCLES_800_T0H; + +#ifdef NEO_KHZ400 + if( !is800KHz ) + { + CYCLES_X00 = CYCLES_400; + CYCLES_X00_T1H = CYCLES_400_T1H; + CYCLES_X00_T0H = CYCLES_400_T0H; + } +#endif + + // Enable DWT in debug core + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; + + // Tries to re-send the frame if is interrupted by the SoftDevice. + while(1) { + uint8_t *p = pixels; + + uint32_t cycStart = DWT->CYCCNT; + uint32_t cyc = 0; + + for(uint16_t n=0; n>= 1) { + while(DWT->CYCCNT - cyc < CYCLES_X00); + cyc = DWT->CYCCNT; + + NRF_GPIO->OUTSET |= pinMask; + + if(pix & mask) { + while(DWT->CYCCNT - cyc < CYCLES_X00_T1H); + } else { + while(DWT->CYCCNT - cyc < CYCLES_X00_T0H); + } + + NRF_GPIO->OUTCLR |= pinMask; + } + } + while(DWT->CYCCNT - cyc < CYCLES_X00); + + + // If total time longer than 25%, resend the whole data. + // Since we are likely to be interrupted by SoftDevice + if ( (DWT->CYCCNT - cycStart) < ( 8*numBytes*((CYCLES_X00*5)/4) ) ) { + break; + } + + // re-send need 300us delay + delayMicroseconds(300); + } + + // Enable interrupts again + #ifdef ARDUINO_FEATHER52 + taskEXIT_CRITICAL(); + #elif defined(NRF52_DISABLE_INT) + __enable_irq(); + #endif + } +// END of NRF52 implementation + +#elif defined (__SAMD21E17A__) || defined(__SAMD21G18A__) || defined(__SAMD21E18A__) || defined(__SAMD21J18A__) // Arduino Zero, Gemma/Trinket M0, SODAQ Autonomo and others + // Tried this with a timer/counter, couldn't quite get adequate + // resolution. So yay, you get a load of goofball NOPs... + + uint8_t *ptr, *end, p, bitMask, portNum; + uint32_t pinMask; + + portNum = g_APinDescription[pin].ulPort; + pinMask = 1ul << g_APinDescription[pin].ulPin; + ptr = pixels; + end = ptr + numBytes; + p = *ptr++; + bitMask = 0x80; + + volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg), + *clr = &(PORT->Group[portNum].OUTCLR.reg); + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + for(;;) { + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;"); + if(p & bitMask) { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + *clr = pinMask; + } else { + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + } + if(bitMask >>= 1) { + asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;"); + } else { + if(ptr >= end) break; + p = *ptr++; + bitMask = 0x80; + } + } +#ifdef NEO_KHZ400 + } else { // 400 KHz bitstream + for(;;) { + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;"); + if(p & bitMask) { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop;"); + *clr = pinMask; + } else { + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop;"); + } + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;"); + if(bitMask >>= 1) { + asm("nop; nop; nop; nop; nop; nop; nop;"); + } else { + if(ptr >= end) break; + p = *ptr++; + bitMask = 0x80; + } + } + } +#endif + +#elif defined (__SAMD51__) // M4 @ 120mhz + // Tried this with a timer/counter, couldn't quite get adequate + // resolution. So yay, you get a load of goofball NOPs... + + uint8_t *ptr, *end, p, bitMask, portNum; + uint32_t pinMask; + + portNum = g_APinDescription[pin].ulPort; + pinMask = 1ul << g_APinDescription[pin].ulPin; + ptr = pixels; + end = ptr + numBytes; + p = *ptr++; + bitMask = 0x80; + + volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg), + *clr = &(PORT->Group[portNum].OUTCLR.reg); + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + for(;;) { + if(p & bitMask) { // ONE + // High 800ns + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;"); + // Low 450ns + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop;"); + } else { // ZERO + // High 400ns + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop;"); + // Low 850ns + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;"); + } + if(bitMask >>= 1) { + // Move on to the next pixel + asm("nop;"); + } else { + if(ptr >= end) break; + p = *ptr++; + bitMask = 0x80; + } + } +#ifdef NEO_KHZ400 + } else { // 400 KHz bitstream + // ToDo! + } +#endif + +#elif defined (ARDUINO_STM32_FEATHER) // FEATHER WICED (120MHz) + + // Tried this with a timer/counter, couldn't quite get adequate + // resolution. So yay, you get a load of goofball NOPs... + + uint8_t *ptr, *end, p, bitMask; + uint32_t pinMask; + + pinMask = BIT(PIN_MAP[pin].gpio_bit); + ptr = pixels; + end = ptr + numBytes; + p = *ptr++; + bitMask = 0x80; + + volatile uint16_t *set = &(PIN_MAP[pin].gpio_device->regs->BSRRL); + volatile uint16_t *clr = &(PIN_MAP[pin].gpio_device->regs->BSRRH); + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + for(;;) { + if(p & bitMask) { // ONE + // High 800ns + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop;"); + // Low 450ns + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop;"); + } else { // ZERO + // High 400ns + *set = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop;"); + // Low 850ns + *clr = pinMask; + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + } + if(bitMask >>= 1) { + // Move on to the next pixel + asm("nop;"); + } else { + if(ptr >= end) break; + p = *ptr++; + bitMask = 0x80; + } + } +#ifdef NEO_KHZ400 + } else { // 400 KHz bitstream + // ToDo! + } +#endif + +#elif defined (NRF51) + uint8_t *p = pixels, + pix, count, mask; + int32_t num = numBytes; + unsigned int bitmask = ( 1 << g_ADigitalPinMap[pin] ); +// https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/variants/BBCmicrobit/variant.cpp + + volatile unsigned int *reg = (unsigned int *) (0x50000000UL + 0x508); + +// https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/cores/nRF5/SDK/components/device/nrf51.h +// http://www.iot-programmer.com/index.php/books/27-micro-bit-iot-in-c/chapters-micro-bit-iot-in-c/47-micro-bit-iot-in-c-fast-memory-mapped-gpio?showall=1 +// https://github.com/Microsoft/pxt-neopixel/blob/master/sendbuffer.asm + + asm volatile( + // "cpsid i" ; disable irq + + // b .start + "b L%=_start" "\n\t" + // .nextbit: ; C0 + "L%=_nextbit:" "\n\t" //; C0 + // str r1, [r3, #0] ; pin := hi C2 + "strb %[bitmask], [%[reg], #0]" "\n\t" //; pin := hi C2 + // tst r6, r0 ; C3 + "tst %[mask], %[pix]" "\n\t"// ; C3 + // bne .islate ; C4 + "bne L%=_islate" "\n\t" //; C4 + // str r1, [r2, #0] ; pin := lo C6 + "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo C6 + // .islate: + "L%=_islate:" "\n\t" + // lsrs r6, r6, #1 ; r6 >>= 1 C7 + "lsr %[mask], %[mask], #1" "\n\t" //; r6 >>= 1 C7 + // bne .justbit ; C8 + "bne L%=_justbit" "\n\t" //; C8 + + // ; not just a bit - need new byte + // adds r4, #1 ; r4++ C9 + "add %[p], #1" "\n\t" //; r4++ C9 + // subs r5, #1 ; r5-- C10 + "sub %[num], #1" "\n\t" //; r5-- C10 + // bcc .stop ; if (r5<0) goto .stop C11 + "bcc L%=_stop" "\n\t" //; if (r5<0) goto .stop C11 + // .start: + "L%=_start:" + // movs r6, #0x80 ; reset mask C12 + "movs %[mask], #0x80" "\n\t" //; reset mask C12 + // nop ; C13 + "nop" "\n\t" //; C13 + + // .common: ; C13 + "L%=_common:" "\n\t" //; C13 + // str r1, [r2, #0] ; pin := lo C15 + "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo C15 + // ; always re-load byte - it just fits with the cycles better this way + // ldrb r0, [r4, #0] ; r0 := *r4 C17 + "ldrb %[pix], [%[p], #0]" "\n\t" //; r0 := *r4 C17 + // b .nextbit ; C20 + "b L%=_nextbit" "\n\t" //; C20 + + // .justbit: ; C10 + "L%=_justbit:" "\n\t" //; C10 + // ; no nops, branch taken is already 3 cycles + // b .common ; C13 + "b L%=_common" "\n\t" //; C13 + + // .stop: + "L%=_stop:" "\n\t" + // str r1, [r2, #0] ; pin := lo + "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo + // cpsie i ; enable irq + + : [p] "+r" (p), + [pix] "=&r" (pix), + [count] "=&r" (count), + [mask] "=&r" (mask), + [num] "+r" (num) + : [bitmask] "r" (bitmask), + [reg] "r" (reg) + ); + +#elif defined(__SAM3X8E__) // Arduino Due + + #define SCALE VARIANT_MCK / 2UL / 1000000UL + #define INST (2UL * F_CPU / VARIANT_MCK) + #define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST)) + #define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST)) + #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST)) + #define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST)) + #define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST)) + #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST)) + + int pinMask, time0, time1, period, t; + Pio *port; + volatile WoReg *portSet, *portClear, *timeValue, *timeReset; + uint8_t *p, *end, pix, mask; + + pmc_set_writeprotect(false); + pmc_enable_periph_clk((uint32_t)TC3_IRQn); + TC_Configure(TC1, 0, + TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1); + TC_Start(TC1, 0); + + pinMask = g_APinDescription[pin].ulPin; // Don't 'optimize' these into + port = g_APinDescription[pin].pPort; // declarations above. Want to + portSet = &(port->PIO_SODR); // burn a few cycles after + portClear = &(port->PIO_CODR); // starting timer to minimize + timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'. + timeReset = &(TC1->TC_CHANNEL[0].TC_CCR); + p = pixels; + end = p + numBytes; + pix = *p++; + mask = 0x80; + +#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled + if(is800KHz) { +#endif + time0 = TIME_800_0; + time1 = TIME_800_1; + period = PERIOD_800; +#ifdef NEO_KHZ400 + } else { // 400 KHz bitstream + time0 = TIME_400_0; + time1 = TIME_400_1; + period = PERIOD_400; + } +#endif + + for(t = time0;; t = time0) { + if(pix & mask) t = time1; + while(*timeValue < period); + *portSet = pinMask; + *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG; + while(*timeValue < t); + *portClear = pinMask; + if(!(mask >>= 1)) { // This 'inside-out' loop logic utilizes + if(p >= end) break; // idle time to minimize inter-byte delays. + pix = *p++; + mask = 0x80; + } + } + while(*timeValue < period); // Wait for last bit + TC_Stop(TC1, 0); + +#endif // end Due + +// END ARM ---------------------------------------------------------------- + + +#elif defined(ESP8266) || defined(ESP32) + +// ESP8266 ---------------------------------------------------------------- + + // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution + espShow(pin, pixels, numBytes, is800KHz); + +#elif defined(__ARDUINO_ARC__) + +// Arduino 101 ----------------------------------------------------------- + +#define NOPx7 { __builtin_arc_nop(); \ + __builtin_arc_nop(); __builtin_arc_nop(); \ + __builtin_arc_nop(); __builtin_arc_nop(); \ + __builtin_arc_nop(); __builtin_arc_nop(); } + + PinDescription *pindesc = &g_APinDescription[pin]; + register uint32_t loop = 8 * numBytes; // one loop to handle all bytes and all bits + register uint8_t *p = pixels; + register uint32_t currByte = (uint32_t) (*p); + register uint32_t currBit = 0x80 & currByte; + register uint32_t bitCounter = 0; + register uint32_t first = 1; + + // The loop is unusual. Very first iteration puts all the way LOW to the wire - + // constant LOW does not affect NEOPIXEL, so there is no visible effect displayed. + // During that very first iteration CPU caches instructions in the loop. + // Because of the caching process, "CPU slows down". NEOPIXEL pulse is very time sensitive + // that's why we let the CPU cache first and we start regular pulse from 2nd iteration + if (pindesc->ulGPIOType == SS_GPIO) { + register uint32_t reg = pindesc->ulGPIOBase + SS_GPIO_SWPORTA_DR; + uint32_t reg_val = __builtin_arc_lr((volatile uint32_t)reg); + register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId); + register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId); + + loop += 1; // include first, special iteration + while(loop--) { + if(!first) { + currByte <<= 1; + bitCounter++; + } + + // 1 is >550ns high and >450ns low; 0 is 200..500ns high and >450ns low + __builtin_arc_sr(first ? reg_bit_low : reg_bit_high, (volatile uint32_t)reg); + if(currBit) { // ~400ns HIGH (740ns overall) + NOPx7 + NOPx7 + } + // ~340ns HIGH + NOPx7 + __builtin_arc_nop(); + + // 820ns LOW; per spec, max allowed low here is 5000ns */ + __builtin_arc_sr(reg_bit_low, (volatile uint32_t)reg); + NOPx7 + NOPx7 + + if(bitCounter >= 8) { + bitCounter = 0; + currByte = (uint32_t) (*++p); + } + + currBit = 0x80 & currByte; + first = 0; + } + } else if(pindesc->ulGPIOType == SOC_GPIO) { + register uint32_t reg = pindesc->ulGPIOBase + SOC_GPIO_SWPORTA_DR; + uint32_t reg_val = MMIO_REG_VAL(reg); + register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId); + register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId); + + loop += 1; // include first, special iteration + while(loop--) { + if(!first) { + currByte <<= 1; + bitCounter++; + } + MMIO_REG_VAL(reg) = first ? reg_bit_low : reg_bit_high; + if(currBit) { // ~430ns HIGH (740ns overall) + NOPx7 + NOPx7 + __builtin_arc_nop(); + } + // ~310ns HIGH + NOPx7 + + // 850ns LOW; per spec, max allowed low here is 5000ns */ + MMIO_REG_VAL(reg) = reg_bit_low; + NOPx7 + NOPx7 + + if(bitCounter >= 8) { + bitCounter = 0; + currByte = (uint32_t) (*++p); + } + + currBit = 0x80 & currByte; + first = 0; + } + } + +#else +#error Architecture not supported +#endif + + +// END ARCHITECTURE SELECT ------------------------------------------------ + +#ifndef NRF52 + interrupts(); +#endif + + endTime = micros(); // Save EOD time for latch on next call +} + +// Set the output pin number +void Adafruit_NeoPixel::setPin(uint8_t p) { + if(begun && (pin >= 0)) pinMode(pin, INPUT); + pin = p; + if(begun) { + pinMode(p, OUTPUT); + digitalWrite(p, LOW); + } +#ifdef __AVR__ + port = portOutputRegister(digitalPinToPort(p)); + pinMask = digitalPinToBitMask(p); +#endif +} + +// Set pixel color from separate R,G,B components: +void Adafruit_NeoPixel::setPixelColor( + uint16_t n, uint8_t r, uint8_t g, uint8_t b) { + + if(n < numLEDs) { + if(brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + } + uint8_t *p; + if(wOffset == rOffset) { // Is an RGB-type strip + p = &pixels[n * 3]; // 3 bytes per pixel + } else { // Is a WRGB-type strip + p = &pixels[n * 4]; // 4 bytes per pixel + p[wOffset] = 0; // But only R,G,B passed -- set W to 0 + } + p[rOffset] = r; // R,G,B always stored + p[gOffset] = g; + p[bOffset] = b; + } +} + +void Adafruit_NeoPixel::setPixelColor( + uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + + if(n < numLEDs) { + if(brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + w = (w * brightness) >> 8; + } + uint8_t *p; + if(wOffset == rOffset) { // Is an RGB-type strip + p = &pixels[n * 3]; // 3 bytes per pixel (ignore W) + } else { // Is a WRGB-type strip + p = &pixels[n * 4]; // 4 bytes per pixel + p[wOffset] = w; // Store W + } + p[rOffset] = r; // Store R,G,B + p[gOffset] = g; + p[bOffset] = b; + } +} + +// Set pixel color from 'packed' 32-bit RGB color: +void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) { + if(n < numLEDs) { + uint8_t *p, + r = (uint8_t)(c >> 16), + g = (uint8_t)(c >> 8), + b = (uint8_t)c; + if(brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + } + if(wOffset == rOffset) { + p = &pixels[n * 3]; + } else { + p = &pixels[n * 4]; + uint8_t w = (uint8_t)(c >> 24); + p[wOffset] = brightness ? ((w * brightness) >> 8) : w; + } + p[rOffset] = r; + p[gOffset] = g; + p[bOffset] = b; + } +} + +// Convert separate R,G,B into packed 32-bit RGB color. +// Packed format is always RGB, regardless of LED strand color order. +uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) { + return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; +} + +// Convert separate R,G,B,W into packed 32-bit WRGB color. +// Packed format is always WRGB, regardless of LED strand color order. +uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; +} + +// Query color from previously-set pixel (returns packed 32-bit RGB value) +uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const { + if(n >= numLEDs) return 0; // Out of bounds, return no color. + + uint8_t *p; + + if(wOffset == rOffset) { // Is RGB-type device + p = &pixels[n * 3]; + if(brightness) { + // Stored color was decimated by setBrightness(). Returned value + // attempts to scale back to an approximation of the original 24-bit + // value used when setting the pixel color, but there will always be + // some error -- those bits are simply gone. Issue is most + // pronounced at low brightness levels. + return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) | + (((uint32_t)(p[gOffset] << 8) / brightness) << 8) | + ( (uint32_t)(p[bOffset] << 8) / brightness ); + } else { + // No brightness adjustment has been made -- return 'raw' color + return ((uint32_t)p[rOffset] << 16) | + ((uint32_t)p[gOffset] << 8) | + (uint32_t)p[bOffset]; + } + } else { // Is RGBW-type device + p = &pixels[n * 4]; + if(brightness) { // Return scaled color + return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) | + (((uint32_t)(p[rOffset] << 8) / brightness) << 16) | + (((uint32_t)(p[gOffset] << 8) / brightness) << 8) | + ( (uint32_t)(p[bOffset] << 8) / brightness ); + } else { // Return raw color + return ((uint32_t)p[wOffset] << 24) | + ((uint32_t)p[rOffset] << 16) | + ((uint32_t)p[gOffset] << 8) | + (uint32_t)p[bOffset]; + } + } +} + +// Returns pointer to pixels[] array. Pixel data is stored in device- +// native format and is not translated here. Application will need to be +// aware of specific pixel data format and handle colors appropriately. +uint8_t *Adafruit_NeoPixel::getPixels(void) const { + return pixels; +} + +uint16_t Adafruit_NeoPixel::numPixels(void) const { + return numLEDs; +} + +// Adjust output brightness; 0=darkest (off), 255=brightest. This does +// NOT immediately affect what's currently displayed on the LEDs. The +// next call to show() will refresh the LEDs at this level. However, +// this process is potentially "lossy," especially when increasing +// brightness. The tight timing in the WS2811/WS2812 code means there +// aren't enough free cycles to perform this scaling on the fly as data +// is issued. So we make a pass through the existing color data in RAM +// and scale it (subsequent graphics commands also work at this +// brightness level). If there's a significant step up in brightness, +// the limited number of steps (quantization) in the old data will be +// quite visible in the re-scaled version. For a non-destructive +// change, you'll need to re-render the full strip data. C'est la vie. +void Adafruit_NeoPixel::setBrightness(uint8_t b) { + // Stored brightness value is different than what's passed. + // This simplifies the actual scaling math later, allowing a fast + // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t, + // adding 1 here may (intentionally) roll over...so 0 = max brightness + // (color values are interpreted literally; no scaling), 1 = min + // brightness (off), 255 = just below max brightness. + uint8_t newBrightness = b + 1; + if(newBrightness != brightness) { // Compare against prior value + // Brightness has changed -- re-scale existing data in RAM + uint8_t c, + *ptr = pixels, + oldBrightness = brightness - 1; // De-wrap old brightness value + uint16_t scale; + if(oldBrightness == 0) scale = 0; // Avoid /0 + else if(b == 255) scale = 65535 / oldBrightness; + else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness; + for(uint16_t i=0; i> 8; + } + brightness = newBrightness; + } +} + +//Return the brightness value +uint8_t Adafruit_NeoPixel::getBrightness(void) const { + return brightness - 1; +} + +void Adafruit_NeoPixel::clear() { + memset(pixels, 0, numBytes); +} + +/* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255). + Copy & paste this snippet into a Python REPL to regenerate: +import math +for x in range(256): + print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))), + if x&15 == 15: print +*/ +static const uint8_t PROGMEM _sineTable[256] = { + 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173, + 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215, + 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244, + 245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255, + 255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246, + 245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220, + 218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179, + 176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131, + 128,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82, + 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, + 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, + 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, + 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, + 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, + 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124}; + +/* Similar to above, but for an 8-bit gamma-correction table. + Copy & paste this snippet into a Python REPL to regenerate: +import math +gamma=2.6 +for x in range(256): + print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))), + if x&15 == 15: print +*/ +static const uint8_t PROGMEM _gammaTable[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, + 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, + 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, + 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, + 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, + 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, + 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, 68, 69, 70, 71, 72, 73, 75, + 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, + 97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120, + 122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148, + 150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180, + 182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215, + 218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255}; + +uint8_t Adafruit_NeoPixel::sine8(uint8_t x) const { + return pgm_read_byte(&_sineTable[x]); // 0-255 in, 0-255 out +} + +uint8_t Adafruit_NeoPixel::gamma8(uint8_t x) const { + return pgm_read_byte(&_gammaTable[x]); // 0-255 in, 0-255 out +} + diff --git a/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h b/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h new file mode 100644 index 0000000..9fa35bc --- /dev/null +++ b/libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h @@ -0,0 +1,182 @@ +/*-------------------------------------------------------------------- + This file is part of the Adafruit NeoPixel library. + + NeoPixel 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 3 of + the License, or (at your option) any later version. + + NeoPixel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with NeoPixel. If not, see + . + --------------------------------------------------------------------*/ + +#ifndef ADAFRUIT_NEOPIXEL_H +#define ADAFRUIT_NEOPIXEL_H + +#if (ARDUINO >= 100) + #include +#else + #include + #include +#endif + +// The order of primary colors in the NeoPixel data stream can vary +// among device types, manufacturers and even different revisions of +// the same item. The third parameter to the Adafruit_NeoPixel +// constructor encodes the per-pixel byte offsets of the red, green +// and blue primaries (plus white, if present) in the data stream -- +// the following #defines provide an easier-to-use named version for +// each permutation. e.g. NEO_GRB indicates a NeoPixel-compatible +// device expecting three bytes per pixel, with the first byte +// containing the green value, second containing red and third +// containing blue. The in-memory representation of a chain of +// NeoPixels is the same as the data-stream order; no re-ordering of +// bytes is required when issuing data to the chain. + +// Bits 5,4 of this value are the offset (0-3) from the first byte of +// a pixel to the location of the red color byte. Bits 3,2 are the +// green offset and 1,0 are the blue offset. If it is an RGBW-type +// device (supporting a white primary in addition to R,G,B), bits 7,6 +// are the offset to the white byte...otherwise, bits 7,6 are set to +// the same value as 5,4 (red) to indicate an RGB (not RGBW) device. +// i.e. binary representation: +// 0bWWRRGGBB for RGBW devices +// 0bRRRRGGBB for RGB + +// RGB NeoPixel permutations; white and red offsets are always same +// Offset: W R G B +#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) +#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) +#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) +#define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) +#define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) +#define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) + +// RGBW NeoPixel permutations; all 4 offsets are distinct +// Offset: W R G B +#define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) +#define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) +#define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) +#define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) +#define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) +#define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) + +#define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) +#define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) +#define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) +#define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) +#define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) +#define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) + +#define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) +#define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) +#define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) +#define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) +#define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) +#define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) + +#define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) +#define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) +#define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) +#define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) +#define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) +#define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) + +// Add NEO_KHZ400 to the color order value to indicate a 400 KHz +// device. All but the earliest v1 NeoPixels expect an 800 KHz data +// stream, this is the default if unspecified. Because flash space +// is very limited on ATtiny devices (e.g. Trinket, Gemma), v1 +// NeoPixels aren't handled by default on those chips, though it can +// be enabled by removing the ifndef/endif below -- but code will be +// bigger. Conversely, can disable the NEO_KHZ400 line on other MCUs +// to remove v1 support and save a little space. + +#define NEO_KHZ800 0x0000 // 800 KHz datastream +#ifndef __AVR_ATtiny85__ +#define NEO_KHZ400 0x0100 // 400 KHz datastream +#endif + +// If 400 KHz support is enabled, the third parameter to the constructor +// requires a 16-bit value (in order to select 400 vs 800 KHz speed). +// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value +// is sufficient to encode pixel color order, saving some space. + +#ifdef NEO_KHZ400 +typedef uint16_t neoPixelType; +#else +typedef uint8_t neoPixelType; +#endif + +class Adafruit_NeoPixel { + + public: + + // Constructor: number of LEDs, pin number, LED type + Adafruit_NeoPixel(uint16_t n, uint8_t p=6, neoPixelType t=NEO_GRB + NEO_KHZ800); + Adafruit_NeoPixel(void); + ~Adafruit_NeoPixel(); + + void + begin(void), + show(void), + setPin(uint8_t p), + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w), + setPixelColor(uint16_t n, uint32_t c), + setBrightness(uint8_t), + clear(), + updateLength(uint16_t n), + updateType(neoPixelType t); + uint8_t + *getPixels(void) const, + getBrightness(void) const, + sine8(uint8_t) const, + gamma8(uint8_t) const; + int8_t + getPin(void) { return pin; }; + uint16_t + numPixels(void) const; + static uint32_t + Color(uint8_t r, uint8_t g, uint8_t b), + Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w); + uint32_t + getPixelColor(uint16_t n) const; + inline bool + canShow(void) { return (micros() - endTime) >= 300L; } + + protected: + + boolean +#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled... + is800KHz, // ...true if 800 KHz pixels +#endif + begun; // true if begin() previously called + uint16_t + numLEDs, // Number of RGB LEDs in strip + numBytes; // Size of 'pixels' buffer below (3 or 4 bytes/pixel) + int8_t + pin; // Output pin number (-1 if not yet set) + uint8_t + brightness, + *pixels, // Holds LED color values (3 or 4 bytes each) + rOffset, // Index of red byte within each 3- or 4-byte pixel + gOffset, // Index of green byte + bOffset, // Index of blue byte + wOffset; // Index of white byte (same as rOffset if no white) + uint32_t + endTime; // Latch timing reference +#ifdef __AVR__ + volatile uint8_t + *port; // Output PORT register + uint8_t + pinMask; // Output PORT bitmask +#endif +}; + +#endif // ADAFRUIT_NEOPIXEL_H diff --git a/libraries/Adafruit_NeoPixel/COPYING b/libraries/Adafruit_NeoPixel/COPYING new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/libraries/Adafruit_NeoPixel/COPYING @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/libraries/Adafruit_NeoPixel/README.md b/libraries/Adafruit_NeoPixel/README.md new file mode 100644 index 0000000..356bb31 --- /dev/null +++ b/libraries/Adafruit_NeoPixel/README.md @@ -0,0 +1,51 @@ +# Adafruit NeoPixel Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_NeoPixel.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_NeoPixel) + +Arduino library for controlling single-wire-based LED pixels and strip such as the [Adafruit 60 LED/meter Digital LED strip][strip], the [Adafruit FLORA RGB Smart Pixel][flora], the [Adafruit Breadboard-friendly RGB Smart Pixel][pixel], the [Adafruit NeoPixel Stick][stick], and the [Adafruit NeoPixel Shield][shield]. + +After downloading, rename folder to 'Adafruit_NeoPixel' and install in Arduino Libraries folder. Restart Arduino IDE, then open File->Sketchbook->Library->Adafruit_NeoPixel->strandtest sketch. + +Compatibility notes: Port A is not supported on any AVR processors at this time + +[flora]: http://adafruit.com/products/1060 +[strip]: http://adafruit.com/products/1138 +[pixel]: http://adafruit.com/products/1312 +[stick]: http://adafruit.com/products/1426 +[shield]: http://adafruit.com/products/1430 + +--- + +## Supported chipsets + +We have included code for the following chips - *sometimes these break for exciting reasons that we can't control* in which case please open an issue! + + * AVR ATmega and ATtiny (any 8-bit) - 8 MHz, 12 MHz and 16 MHz + * Teensy 3.x and LC + * Arduino Due + * Arduino 101 + * ATSAMD21 (Arduino Zero/M0 and other SAMD21 boards) @ 48 MHz + * ATSAMD51 @ 120 MHz + * Adafruit STM32 Feather @ 120 MHz + * ESP8266 any speed + * ESP32 any speed + * Nordic nRF52 (Adafruit Feather nRF52), nRF51 (micro:bit) + +Check forks for other architectures not listed here! + +--- + +### Roadmap + +The PRIME DIRECTIVE is to maintain backward compatibility with existing Arduino sketches -- many are hosted elsewhere and don't track changes here, some are in print and can never be changed! + +Please don't reformat code for the sake of reformatting code. The resulting large "visual diff" makes it impossible to untangle actual bug fixes from merely rearranged lines. (Exception for first item in wishlist below.) + +Things I'd Like To Do But There's No Official Timeline So Please Don't Count On Any Of This Ever Being Canonical: + + * For the show() function (with all the delicate pixel timing stuff), break out each architecture into separate source files rather than the current unmaintainable tangle of #ifdef statements! + * Really the only reason I've never incorporated an HSV color function is that I haven't settled on a type and range for the hue element (mathematically an integer from 0 to 1529 yields a "most correct" approach but it's weird to use and would probably annoy people). + * Add a fill function with the arguments: (color, first, count). Count, if unspecified, fills to end of strip. First, if unspecified, is zero. Color, if unspecified, is zero (effectively a strip clear operation). Do NOT then implement fifty billion minor variations (such as first, last). No. This argument sequence was very specifically chosen because reasons, and equivalents to such variations are trivially made in one's call. Just one fill function, please. + * At such time that the prior two items are settled, revisit the DotStar library (and maybe even LPD8806 or anything else we've got) and add the same functions and behaviors so there's a good degree of sketch compatibility across different pixel types. + * I wouldn't mind paring down strandtest a bit. More diagnostic, less Amiga demo. + * Please don't use updateLength() or updateType() in new code. They should not have been implemented this way (use the C++ 'new' operator with the regular constructor instead) and are only sticking around because of the Prime Directive. setPin() is OK for now though, it's a trick we can use to 'recycle' pixel memory across multiple strips. + * In the M0 and M4 code, use the hardware systick counter for bit timing rather than hand-tweaked NOPs (a temporary kludge at the time because I wasn't reading systick correctly). + * As currently written, brightness scaling is still a "destructive" operation -- pixel values are altered in RAM and the original value as set can't be accurately read back, only approximated, which has been confusing and frustrating to users. It was done this way at the time because NeoPixel timing is strict, AVR microcontrollers (all we had at the time) are limited, and assembly language is hard. All the 32-bit architectures should have no problem handling nondestructive brightness scaling -- calculating each byte immediately before it's sent out the wire, maintaining the original set value in RAM -- the work just hasn't been done. There's a fair chance even the AVR code could manage it with some intense focus. (The DotStar library achieves nondestructive brightness scaling because it doesn't have to manage data timing so carefully...every architecture, even ATtiny, just takes whatever cycles it needs for the multiply/shift operations.) diff --git a/libraries/Adafruit_NeoPixel/esp8266.c b/libraries/Adafruit_NeoPixel/esp8266.c new file mode 100644 index 0000000..e316ee7 --- /dev/null +++ b/libraries/Adafruit_NeoPixel/esp8266.c @@ -0,0 +1,82 @@ +// This is a mash-up of the Due show() code + insights from Michael Miller's +// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus +// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution. + +#if defined(ESP8266) || defined(ESP32) + +#include +#ifdef ESP8266 +#include +#endif + +static uint32_t _getCycleCount(void) __attribute__((always_inline)); +static inline uint32_t _getCycleCount(void) { + uint32_t ccount; + __asm__ __volatile__("rsr %0,ccount":"=a" (ccount)); + return ccount; +} + +#ifdef ESP8266 +void ICACHE_RAM_ATTR espShow( + uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) { +#else +void espShow( + uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) { +#endif + +#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us +#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us +#define CYCLES_800 (F_CPU / 800000) // 1.25us per bit +#define CYCLES_400_T0H (F_CPU / 2000000) // 0.5uS +#define CYCLES_400_T1H (F_CPU / 833333) // 1.2us +#define CYCLES_400 (F_CPU / 400000) // 2.5us per bit + + uint8_t *p, *end, pix, mask; + uint32_t t, time0, time1, period, c, startTime, pinMask; + + pinMask = _BV(pin); + p = pixels; + end = p + numBytes; + pix = *p++; + mask = 0x80; + startTime = 0; + +#ifdef NEO_KHZ400 + if(is800KHz) { +#endif + time0 = CYCLES_800_T0H; + time1 = CYCLES_800_T1H; + period = CYCLES_800; +#ifdef NEO_KHZ400 + } else { // 400 KHz bitstream + time0 = CYCLES_400_T0H; + time1 = CYCLES_400_T1H; + period = CYCLES_400; + } +#endif + + for(t = time0;; t = time0) { + if(pix & mask) t = time1; // Bit high duration + while(((c = _getCycleCount()) - startTime) < period); // Wait for bit start +#ifdef ESP8266 + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinMask); // Set high +#else + gpio_set_level(pin, HIGH); +#endif + startTime = c; // Save start time + while(((c = _getCycleCount()) - startTime) < t); // Wait high duration +#ifdef ESP8266 + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinMask); // Set low +#else + gpio_set_level(pin, LOW); +#endif + if(!(mask >>= 1)) { // Next bit/byte + if(p >= end) break; + pix = *p++; + mask = 0x80; + } + } + while((_getCycleCount() - startTime) < period); // Wait for last bit +} + +#endif // ESP8266 diff --git a/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.esp8266.test.skip b/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.esp8266.test.skip new file mode 100644 index 0000000..e69de29 diff --git a/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.trinket.test.skip b/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/.trinket.test.skip new file mode 100644 index 0000000..e69de29 diff --git a/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/RGBWstrandtest.ino b/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/RGBWstrandtest.ino new file mode 100644 index 0000000..bd6dab7 --- /dev/null +++ b/libraries/Adafruit_NeoPixel/examples/RGBWstrandtest/RGBWstrandtest.ino @@ -0,0 +1,261 @@ +#include +#ifdef __AVR__ + #include +#endif + +#define PIN 6 + +#define NUM_LEDS 60 + +#define BRIGHTNESS 50 + +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800); + +byte neopix_gamma[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, + 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, + 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, + 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, + 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, + 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, + 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, + 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, + 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, + 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, + 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, + 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, + 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, + 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; + + +void setup() { + // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket + #if defined (__AVR_ATtiny85__) + if (F_CPU == 16000000) clock_prescale_set(clock_div_1); + #endif + // End of trinket special code + strip.setBrightness(BRIGHTNESS); + strip.begin(); + strip.show(); // Initialize all pixels to 'off' +} + +void loop() { + // Some example procedures showing how to display to the pixels: + colorWipe(strip.Color(255, 0, 0), 50); // Red + colorWipe(strip.Color(0, 255, 0), 50); // Green + colorWipe(strip.Color(0, 0, 255), 50); // Blue + colorWipe(strip.Color(0, 0, 0, 255), 50); // White + + whiteOverRainbow(20,75,5); + + pulseWhite(5); + + // fullWhite(); + // delay(2000); + + rainbowFade2White(3,3,1); + + +} + +// Fill the dots one after the other with a color +void colorWipe(uint32_t c, uint8_t wait) { + for(uint16_t i=0; i= 0 ; j--){ + for(uint16_t i=0; i 255 - fadeMax ){ + fadeVal--; + } + + strip.show(); + delay(wait); + } + + } + + + + delay(500); + + + for(int k = 0 ; k < whiteLoops ; k ++){ + + for(int j = 0; j < 256 ; j++){ + + for(uint16_t i=0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) ); + } + strip.show(); + } + + delay(2000); + for(int j = 255; j >= 0 ; j--){ + + for(uint16_t i=0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) ); + } + strip.show(); + } + } + + delay(500); + + +} + +void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) { + + if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1; + + int head = whiteLength - 1; + int tail = 0; + + int loops = 3; + int loopNum = 0; + + static unsigned long lastTime = 0; + + + while(true){ + for(int j=0; j<256; j++) { + for(uint16_t i=0; i= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){ + strip.setPixelColor(i, strip.Color(0,0,0, 255 ) ); + } + else{ + strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); + } + + } + + if(millis() - lastTime > whiteSpeed) { + head++; + tail++; + if(head == strip.numPixels()){ + loopNum++; + } + lastTime = millis(); + } + + if(loopNum == loops) return; + + head%=strip.numPixels(); + tail%=strip.numPixels(); + strip.show(); + delay(wait); + } + } + +} +void fullWhite() { + + for(uint16_t i=0; i> 16); +} +uint8_t green(uint32_t c) { + return (c >> 8); +} +uint8_t blue(uint32_t c) { + return (c); +} + diff --git a/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/.test.skip b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/.test.skip new file mode 100644 index 0000000..e69de29 diff --git a/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp new file mode 100644 index 0000000..d1693de --- /dev/null +++ b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp @@ -0,0 +1,133 @@ +#include "BLESerial.h" + +// #define BLE_SERIAL_DEBUG + +BLESerial* BLESerial::_instance = NULL; + +BLESerial::BLESerial(unsigned char req, unsigned char rdy, unsigned char rst) : + BLEPeripheral(req, rdy, rst) +{ + this->_txCount = 0; + this->_rxHead = this->_rxTail = 0; + this->_flushed = 0; + BLESerial::_instance = this; + + addAttribute(this->_uartService); + addAttribute(this->_uartNameDescriptor); + setAdvertisedServiceUuid(this->_uartService.uuid()); + addAttribute(this->_rxCharacteristic); + addAttribute(this->_rxNameDescriptor); + this->_rxCharacteristic.setEventHandler(BLEWritten, BLESerial::_received); + addAttribute(this->_txCharacteristic); + addAttribute(this->_txNameDescriptor); +} + +void BLESerial::begin(...) { + BLEPeripheral::begin(); + #ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLESerial::begin()")); + #endif +} + +void BLESerial::poll() { + if (millis() < this->_flushed + 100) { + BLEPeripheral::poll(); + } else { + flush(); + } +} + +void BLESerial::end() { + this->_rxCharacteristic.setEventHandler(BLEWritten, NULL); + this->_rxHead = this->_rxTail = 0; + flush(); + BLEPeripheral::disconnect(); +} + +int BLESerial::available(void) { + BLEPeripheral::poll(); + int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer); + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::available() = ")); + Serial.println(retval); + #endif + return retval; +} + +int BLESerial::peek(void) { + BLEPeripheral::poll(); + if (this->_rxTail == this->_rxHead) return -1; + uint8_t byte = this->_rxBuffer[this->_rxTail]; + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::peek() = ")); + Serial.print((char) byte); + Serial.print(F(" 0x")); + Serial.println(byte, HEX); + #endif + return byte; +} + +int BLESerial::read(void) { + BLEPeripheral::poll(); + if (this->_rxTail == this->_rxHead) return -1; + this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer); + uint8_t byte = this->_rxBuffer[this->_rxTail]; + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::read() = ")); + Serial.print((char) byte); + Serial.print(F(" 0x")); + Serial.println(byte, HEX); + #endif + return byte; +} + +void BLESerial::flush(void) { + if (this->_txCount == 0) return; + this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); + this->_flushed = millis(); + this->_txCount = 0; + BLEPeripheral::poll(); + #ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLESerial::flush()")); + #endif +} + +size_t BLESerial::write(uint8_t byte) { + BLEPeripheral::poll(); + if (this->_txCharacteristic.subscribed() == false) return 0; + this->_txBuffer[this->_txCount++] = byte; + if (this->_txCount == sizeof(this->_txBuffer)) flush(); + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::write(")); + Serial.print((char) byte); + Serial.print(F(" 0x")); + Serial.print(byte, HEX); + Serial.println(F(") = 1")); + #endif + return 1; +} + +BLESerial::operator bool() { + bool retval = BLEPeripheral::connected(); + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::operator bool() = ")); + Serial.println(retval); + #endif + return retval; +} + +void BLESerial::_received(const uint8_t* data, size_t size) { + for (int i = 0; i < size; i++) { + this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer); + this->_rxBuffer[this->_rxHead] = data[i]; + } + #ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLESerial::received(")); + for (int i = 0; i < size; i++) Serial.print((char) data[i]); + Serial.println(F(")")); + #endif +} + +void BLESerial::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic) { + BLESerial::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength()); +} diff --git a/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.h b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.h new file mode 100644 index 0000000..01904c7 --- /dev/null +++ b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.h @@ -0,0 +1,46 @@ +#ifndef _BLE_SERIAL_H_ +#define _BLE_SERIAL_H_ + +#include +#include + +class BLESerial : public BLEPeripheral, public Stream +{ + public: + BLESerial(unsigned char req, unsigned char rdy, unsigned char rst); + + void begin(...); + void poll(); + void end(); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t byte); + using Print::write; + virtual operator bool(); + + private: + unsigned long _flushed; + static BLESerial* _instance; + + size_t _rxHead; + size_t _rxTail; + size_t _rxCount() const; + uint8_t _rxBuffer[BLE_ATTRIBUTE_MAX_VALUE_LENGTH]; + size_t _txCount; + uint8_t _txBuffer[BLE_ATTRIBUTE_MAX_VALUE_LENGTH]; + + BLEService _uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); + BLEDescriptor _uartNameDescriptor = BLEDescriptor("2901", "UART"); + BLECharacteristic _rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, BLE_ATTRIBUTE_MAX_VALUE_LENGTH); + BLEDescriptor _rxNameDescriptor = BLEDescriptor("2901", "RX - Receive Data (Write)"); + BLECharacteristic _txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, BLE_ATTRIBUTE_MAX_VALUE_LENGTH); + BLEDescriptor _txNameDescriptor = BLEDescriptor("2901", "TX - Transfer Data (Notify)"); + + void _received(const uint8_t* data, size_t size); + static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic); +}; + +#endif diff --git a/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/StrandtestBLE.ino b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/StrandtestBLE.ino new file mode 100644 index 0000000..fae060d --- /dev/null +++ b/libraries/Adafruit_NeoPixel/examples/StrandtestBLE/StrandtestBLE.ino @@ -0,0 +1,208 @@ +/**************************************************************************** + * This example was developed by the Hackerspace San Salvador to demonstrate + * the simultaneous use of the NeoPixel library and the Bluetooth SoftDevice. + * To compile this example you'll need to add support for the NRF52 based + * following the instructions at: + * https://github.com/sandeepmistry/arduino-nRF5 + * Or adding the following URL to the board manager URLs on Arduino preferences: + * https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json + * Then you can install the BLEPeripheral library avaiable at: + * https://github.com/sandeepmistry/arduino-BLEPeripheral + * To test it, compile this example and use the UART module from the nRF + * Toolbox App for Android. Edit the interface and send the characters + * 'a' to 'i' to switch the animation. + * There is a delay because this example blocks the thread of execution but + * the change will be shown after the current animation ends. (This might + * take a couple of seconds) + * For more info write us at: info _at- teubi.co + */ +#include +#include +#include "BLESerial.h" +#include + +#define PIN 15 + +// Parameter 1 = number of pixels in strip +// Parameter 2 = Arduino pin number (most are valid) +// Parameter 3 = pixel type flags, add together as needed: +// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) +// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) +// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) +// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) +// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) +Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800); + +// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across +// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input +// and minimize distance between Arduino and first pixel. Avoid connecting +// on a live circuit...if you must, connect GND first. + +// define pins (varies per shield/board) +#define BLE_REQ 10 +#define BLE_RDY 2 +#define BLE_RST 9 + +// create ble serial instance, see pinouts above +BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST); + +uint8_t current_state = 0; +uint8_t rgb_values[3]; + +void setup() { + Serial.begin(115200); + Serial.println("Hello World!"); + // custom services and characteristics can be added as well + BLESerial.setLocalName("UART_HS"); + BLESerial.begin(); + + strip.begin(); + changeColor(strip.Color(0, 0, 0)); + + //pinMode(PIN, OUTPUT); + //digitalWrite(PIN, LOW); + + current_state = 'a'; +} + + +void loop() { + while(BLESerial.available()) { + uint8_t character = BLESerial.read(); + switch(character) { + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + current_state = character; + break; + }; + } + switch(current_state) { + case 'a': + colorWipe(strip.Color(255, 0, 0), 20); // Red + break; + case 'b': + colorWipe(strip.Color(0, 255, 0), 20); // Green + break; + case 'c': + colorWipe(strip.Color(0, 0, 255), 20); // Blue + break; + case 'd': + theaterChase(strip.Color(255, 0, 0), 20); // Red + break; + case 'e': + theaterChase(strip.Color(0, 255, 0), 20); // Green + break; + case 'f': + theaterChase(strip.Color(255, 0, 255), 20); // Green + break; + case 'g': + rainbowCycle(20); + break; + case 'h': + rainbow(20); + break; + case 'i': + theaterChaseRainbow(20); + break; + } +} + +void changeColor(uint32_t c) { + for(uint16_t i=0; i + +#define BUTTON_PIN 2 // Digital IO pin connected to the button. This will be + // driven with a pull-up resistor so the switch should + // pull the pin to ground momentarily. On a high -> low + // transition the button press logic will execute. + +#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels. + +#define PIXEL_COUNT 16 + +// Parameter 1 = number of pixels in strip, neopixel stick has 8 +// Parameter 2 = pin number (most are valid) +// Parameter 3 = pixel type flags, add together as needed: +// NEO_RGB Pixels are wired for RGB bitstream +// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick +// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) +// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick +Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); + +bool oldState = HIGH; +int showType = 0; + +void setup() { + pinMode(BUTTON_PIN, INPUT_PULLUP); + strip.begin(); + strip.show(); // Initialize all pixels to 'off' +} + +void loop() { + // Get current button state. + bool newState = digitalRead(BUTTON_PIN); + + // Check if state changed from high to low (button press). + if (newState == LOW && oldState == HIGH) { + // Short delay to debounce button. + delay(20); + // Check if button is still low after debounce. + newState = digitalRead(BUTTON_PIN); + if (newState == LOW) { + showType++; + if (showType > 9) + showType=0; + startShow(showType); + } + } + + // Set the last button state to the old state. + oldState = newState; +} + +void startShow(int i) { + switch(i){ + case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off + break; + case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red + break; + case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green + break; + case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue + break; + case 4: theaterChase(strip.Color(127, 127, 127), 50); // White + break; + case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red + break; + case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue + break; + case 7: rainbow(20); + break; + case 8: rainbowCycle(20); + break; + case 9: theaterChaseRainbow(50); + break; + } +} + +// Fill the dots one after the other with a color +void colorWipe(uint32_t c, uint8_t wait) { + for(uint16_t i=0; i +#ifdef __AVR__ + #include +#endif + +// Which pin on the Arduino is connected to the NeoPixels? +// On a Trinket or Gemma we suggest changing this to 1 +#define PIN 6 + +// How many NeoPixels are attached to the Arduino? +#define NUMPIXELS 16 + +// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. +// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest +// example for more information on possible values. +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); + +int delayval = 500; // delay for half a second + +void setup() { + // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket +#if defined (__AVR_ATtiny85__) + if (F_CPU == 16000000) clock_prescale_set(clock_div_1); +#endif + // End of trinket special code + + pixels.begin(); // This initializes the NeoPixel library. +} + +void loop() { + + // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one. + + for(int i=0;i +#ifdef __AVR__ + #include +#endif + +#define PIN 6 + +// Parameter 1 = number of pixels in strip +// Parameter 2 = Arduino pin number (most are valid) +// Parameter 3 = pixel type flags, add together as needed: +// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) +// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) +// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) +// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) +// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) +Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); + +// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across +// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input +// and minimize distance between Arduino and first pixel. Avoid connecting +// on a live circuit...if you must, connect GND first. + +void setup() { + // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket + #if defined (__AVR_ATtiny85__) + if (F_CPU == 16000000) clock_prescale_set(clock_div_1); + #endif + // End of trinket special code + + + strip.begin(); + strip.show(); // Initialize all pixels to 'off' +} + +void loop() { + // Some example procedures showing how to display to the pixels: + colorWipe(strip.Color(255, 0, 0), 50); // Red + colorWipe(strip.Color(0, 255, 0), 50); // Green + colorWipe(strip.Color(0, 0, 255), 50); // Blue +//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW + // Send a theater pixel chase in... + theaterChase(strip.Color(127, 127, 127), 50); // White + theaterChase(strip.Color(127, 0, 0), 50); // Red + theaterChase(strip.Color(0, 0, 127), 50); // Blue + + rainbow(20); + rainbowCycle(20); + theaterChaseRainbow(50); +} + +// Fill the dots one after the other with a color +void colorWipe(uint32_t c, uint8_t wait) { + for(uint16_t i=0; i +sentence=Arduino library for controlling single-wire-based LED pixels and strip. +paragraph=Arduino library for controlling single-wire-based LED pixels and strip. +category=Display +url=https://github.com/adafruit/Adafruit_NeoPixel +architectures=* diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.cpp b/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.cpp new file mode 100644 index 0000000..2d1b53a --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.cpp @@ -0,0 +1,194 @@ +/*************************************************** + This is a library for our Adafruit 16-channel PWM & Servo driver + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These displays use I2C to communicate, 2 pins are required to + interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#include +#include + +// Set to true to print some debug messages, or false to disable them. +//#define ENABLE_DEBUG_OUTPUT + + +/**************************************************************************/ +/*! + @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on the Wire interface. On Due we use Wire1 since its the interface on the 'default' I2C pins. + @param addr The 7-bit I2C address to locate this chip, default is 0x40 +*/ +/**************************************************************************/ +Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) { + _i2caddr = addr; + +#if defined(ARDUINO_SAM_DUE) + _i2c = &Wire1; +#else + _i2c = &Wire; +#endif +} + +/**************************************************************************/ +/*! + @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a TwoWire interface + @param i2c A pointer to a 'Wire' compatible object that we'll use to communicate with + @param addr The 7-bit I2C address to locate this chip, default is 0x40 +*/ +/**************************************************************************/ +Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(TwoWire *i2c, uint8_t addr) { + _i2c = i2c; + _i2caddr = addr; +} + +/**************************************************************************/ +/*! + @brief Setups the I2C interface and hardware +*/ +/**************************************************************************/ +void Adafruit_PWMServoDriver::begin(void) { + _i2c->begin(); + reset(); + // set a default frequency + setPWMFreq(1000); +} + + +/**************************************************************************/ +/*! + @brief Sends a reset command to the PCA9685 chip over I2C +*/ +/**************************************************************************/ +void Adafruit_PWMServoDriver::reset(void) { + write8(PCA9685_MODE1, 0x80); + delay(10); +} + +/**************************************************************************/ +/*! + @brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz + @param freq Floating point frequency that we will attempt to match +*/ +/**************************************************************************/ +void Adafruit_PWMServoDriver::setPWMFreq(float freq) { +#ifdef ENABLE_DEBUG_OUTPUT + Serial.print("Attempting to set freq "); + Serial.println(freq); +#endif + + freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11). + float prescaleval = 25000000; + prescaleval /= 4096; + prescaleval /= freq; + prescaleval -= 1; + +#ifdef ENABLE_DEBUG_OUTPUT + Serial.print("Estimated pre-scale: "); Serial.println(prescaleval); +#endif + + uint8_t prescale = floor(prescaleval + 0.5); +#ifdef ENABLE_DEBUG_OUTPUT + Serial.print("Final pre-scale: "); Serial.println(prescale); +#endif + + uint8_t oldmode = read8(PCA9685_MODE1); + uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep + write8(PCA9685_MODE1, newmode); // go to sleep + write8(PCA9685_PRESCALE, prescale); // set the prescaler + write8(PCA9685_MODE1, oldmode); + delay(5); + write8(PCA9685_MODE1, oldmode | 0xa0); // This sets the MODE1 register to turn on auto increment. + +#ifdef ENABLE_DEBUG_OUTPUT + Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX); +#endif +} + +/**************************************************************************/ +/*! + @brief Sets the PWM output of one of the PCA9685 pins + @param num One of the PWM output pins, from 0 to 15 + @param on At what point in the 4096-part cycle to turn the PWM output ON + @param off At what point in the 4096-part cycle to turn the PWM output OFF +*/ +/**************************************************************************/ +void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) { +#ifdef ENABLE_DEBUG_OUTPUT + Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off); +#endif + + _i2c->beginTransmission(_i2caddr); + _i2c->write(LED0_ON_L+4*num); + _i2c->write(on); + _i2c->write(on>>8); + _i2c->write(off); + _i2c->write(off>>8); + _i2c->endTransmission(); +} + +/**************************************************************************/ +/*! + @brief Helper to set pin PWM output. Sets pin without having to deal with on/off tick placement and properly handles a zero value as completely off and 4095 as completely on. Optional invert parameter supports inverting the pulse for sinking to ground. + @param num One of the PWM output pins, from 0 to 15 + @param val The number of ticks out of 4096 to be active, should be a value from 0 to 4095 inclusive. + @param invert If true, inverts the output, defaults to 'false' +*/ +/**************************************************************************/ +void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) +{ + // Clamp value between 0 and 4095 inclusive. + val = min(val, (uint16_t)4095); + if (invert) { + if (val == 0) { + // Special value for signal fully on. + setPWM(num, 4096, 0); + } + else if (val == 4095) { + // Special value for signal fully off. + setPWM(num, 0, 4096); + } + else { + setPWM(num, 0, 4095-val); + } + } + else { + if (val == 4095) { + // Special value for signal fully on. + setPWM(num, 4096, 0); + } + else if (val == 0) { + // Special value for signal fully off. + setPWM(num, 0, 4096); + } + else { + setPWM(num, 0, val); + } + } +} + +/*******************************************************************************************/ + +uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) { + _i2c->beginTransmission(_i2caddr); + _i2c->write(addr); + _i2c->endTransmission(); + + _i2c->requestFrom((uint8_t)_i2caddr, (uint8_t)1); + return _i2c->read(); +} + +void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) { + _i2c->beginTransmission(_i2caddr); + _i2c->write(addr); + _i2c->write(d); + _i2c->endTransmission(); +} diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.h b/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.h new file mode 100644 index 0000000..d6e7fd3 --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/Adafruit_PWMServoDriver.h @@ -0,0 +1,69 @@ +/*************************************************** + This is a library for our Adafruit 16-channel PWM & Servo driver + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These displays use I2C to communicate, 2 pins are required to + interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4 + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#ifndef _ADAFRUIT_PWMServoDriver_H +#define _ADAFRUIT_PWMServoDriver_H + +#if ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif +#include "Wire.h" + +#define PCA9685_SUBADR1 0x2 +#define PCA9685_SUBADR2 0x3 +#define PCA9685_SUBADR3 0x4 + +#define PCA9685_MODE1 0x0 +#define PCA9685_PRESCALE 0xFE + +#define LED0_ON_L 0x6 +#define LED0_ON_H 0x7 +#define LED0_OFF_L 0x8 +#define LED0_OFF_H 0x9 + +#define ALLLED_ON_L 0xFA +#define ALLLED_ON_H 0xFB +#define ALLLED_OFF_L 0xFC +#define ALLLED_OFF_H 0xFD + +/**************************************************************************/ +/*! + @brief Class that stores state and functions for interacting with PCA9685 PWM chip +*/ +/**************************************************************************/ +class Adafruit_PWMServoDriver { + public: + Adafruit_PWMServoDriver(uint8_t addr = 0x40); + Adafruit_PWMServoDriver(TwoWire *I2C, uint8_t addr = 0x40); + void begin(void); + void reset(void); + void setPWMFreq(float freq); + void setPWM(uint8_t num, uint16_t on, uint16_t off); + void setPin(uint8_t num, uint16_t val, bool invert=false); + + private: + uint8_t _i2caddr; + + TwoWire *_i2c; + + uint8_t read8(uint8_t addr); + void write8(uint8_t addr, uint8_t d); +}; + +#endif diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/README.md b/libraries/Adafruit_PWM_Servo_Driver_Library/README.md new file mode 100644 index 0000000..cb1956d --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/README.md @@ -0,0 +1,50 @@ +# Adafruit PCA9685 PWM Servo Driver Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_PWMServoDriver.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_PWMServoDriver) + +This is a library for our Adafruit 16-channel PWM & Servo driver, shield or FeatherWing + + + +Pick one up today in the adafruit shop! + * https://www.adafruit.com/products/815 + * https://www.adafruit.com/product/1411 + * https://www.adafruit.com/product/2928 + +These drivers use I2C to communicate, 2 pins are required to interface. + +Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, check license.txt for more information. + +All text above must be included in any redistribution + + + +## Compatibility + +MCU | Tested Works | Doesn't Work | Not Tested | Notes +------------------ | :----------: | :----------: | :---------: | ----- +Atmega328 @ 16MHz | X | | | +Atmega328 @ 12MHz | X | | | +Atmega32u4 @ 16MHz | X | | | +Atmega32u4 @ 8MHz | X | | | +ESP8266 | X | | | +Atmega2560 @ 16MHz | X | | | +ATSAM3X8E | X | | | Use SDA1/SCL1 +ATSAM21D | X | | | +ATtiny85 @ 16MHz | X | | | +ATtiny85 @ 8MHz | X | | | +Intel Curie @ 32MHz | | | X | +STM32F2 | | | X | + + * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini + * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V + * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 + * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro + * ESP8266 : Adafruit Huzzah + * ATmega2560 @ 16MHz : Arduino Mega + * ATSAM3X8E : Arduino Due + * ATSAM21D : Arduino Zero, M0 Pro + * ATtiny85 @ 16MHz : Adafruit Trinket 5V + * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V + + diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/examples/gpiotest/gpiotest.ino b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/gpiotest/gpiotest.ino new file mode 100644 index 0000000..06c116d --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/gpiotest/gpiotest.ino @@ -0,0 +1,49 @@ +/*************************************************** + This is an example for our Adafruit 16-channel PWM & Servo driver + GPIO test - this will set a pin high/low + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These drivers use I2C to communicate, 2 pins are required to + interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#include +#include + +// called this way, it uses the default address 0x40 +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); +// you can also call it with a different address you want +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41); +// you can also call it with a different address and I2C interface +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40); + +void setup() { + Serial.begin(9600); + Serial.println("GPIO test!"); + + pwm.begin(); + pwm.setPWMFreq(1000); // Set to whatever you like, we don't use it in this demo! + + // if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode + // some i2c devices dont like this so much so if you're sharing the bus, watch + // out for this! + Wire.setClock(400000); +} + +void loop() { + // Drive each pin in a 'wave' + for (uint8_t pin=0; pin<16; pin++) { + pwm.setPWM(pin, 4096, 0); // turns pin fully on + delay(100); + pwm.setPWM(pin, 0, 4096); // turns pin fully off + } +} diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/examples/pwmtest/pwmtest.ino b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/pwmtest/pwmtest.ino new file mode 100644 index 0000000..2d18228 --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/pwmtest/pwmtest.ino @@ -0,0 +1,52 @@ +/*************************************************** + This is an example for our Adafruit 16-channel PWM & Servo driver + PWM test - this will drive 16 PWMs in a 'wave' + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These drivers use I2C to communicate, 2 pins are required to + interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#include +#include + +// called this way, it uses the default address 0x40 +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); +// you can also call it with a different address you want +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41); +// you can also call it with a different address and I2C interface +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40); + +void setup() { + Serial.begin(9600); + Serial.println("16 channel PWM test!"); + + pwm.begin(); + pwm.setPWMFreq(1600); // This is the maximum PWM frequency + + // if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode + // some i2c devices dont like this so much so if you're sharing the bus, watch + // out for this! + Wire.setClock(400000); +} + +void loop() { + // Drive each PWM in a 'wave' + for (uint16_t i=0; i<4096; i += 8) { + for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) { + pwm.setPWM(pwmnum, 0, (i + (4096/16)*pwmnum) % 4096 ); + } +#ifdef ESP8266 + yield(); // take a breather, required for ESP8266 +#endif + } +} diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/examples/servo/servo.ino b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/servo/servo.ino new file mode 100644 index 0000000..5298bbe --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/examples/servo/servo.ino @@ -0,0 +1,83 @@ +/*************************************************** + This is an example for our Adafruit 16-channel PWM & Servo driver + Servo test - this will drive 8 servos, one after the other on the + first 8 pins of the PCA9685 + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These drivers use I2C to communicate, 2 pins are required to + interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + +#include +#include + +// called this way, it uses the default address 0x40 +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); +// you can also call it with a different address you want +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41); +// you can also call it with a different address and I2C interface +//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40); + +// Depending on your servo make, the pulse width min and max may vary, you +// want these to be as small/large as possible without hitting the hard stop +// for max range. You'll have to tweak them as necessary to match the servos you +// have! +#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096) +#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096) + +// our servo # counter +uint8_t servonum = 0; + +void setup() { + Serial.begin(9600); + Serial.println("8 channel Servo test!"); + + pwm.begin(); + + pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates + + delay(10); +} + +// you can use this function if you'd like to set the pulse length in seconds +// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise! +void setServoPulse(uint8_t n, double pulse) { + double pulselength; + + pulselength = 1000000; // 1,000,000 us per second + pulselength /= 60; // 60 Hz + Serial.print(pulselength); Serial.println(" us per period"); + pulselength /= 4096; // 12 bits of resolution + Serial.print(pulselength); Serial.println(" us per bit"); + pulse *= 1000000; // convert to us + pulse /= pulselength; + Serial.println(pulse); + pwm.setPWM(n, 0, pulse); +} + +void loop() { + // Drive each servo one at a time + Serial.println(servonum); + for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { + pwm.setPWM(servonum, 0, pulselen); + } + + delay(500); + for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) { + pwm.setPWM(servonum, 0, pulselen); + } + + delay(500); + + servonum ++; + if (servonum > 7) servonum = 0; +} diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/library.properties b/libraries/Adafruit_PWM_Servo_Driver_Library/library.properties new file mode 100644 index 0000000..4125ed9 --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/library.properties @@ -0,0 +1,9 @@ +name=Adafruit PWM Servo Driver Library +version=1.0.2 +author=Adafruit +maintainer=Adafruit +sentence=Adafruit PWM Servo Driver Library +paragraph=Adafruit PWM Servo Driver Library +category=Device Control +url=https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library +architectures=* diff --git a/libraries/Adafruit_PWM_Servo_Driver_Library/license.txt b/libraries/Adafruit_PWM_Servo_Driver_Library/license.txt new file mode 100644 index 0000000..f6a0f22 --- /dev/null +++ b/libraries/Adafruit_PWM_Servo_Driver_Library/license.txt @@ -0,0 +1,26 @@ +Software License Agreement (BSD License) + +Copyright (c) 2012, Adafruit Industries +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holders nor the +names of its contributors may be used to endorse or promote products +derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/libraries/ConfigurableFirmata/examples/ConfigurableFirmata/ConfigurableFirmata.ino b/libraries/ConfigurableFirmata/examples/ConfigurableFirmata/ConfigurableFirmata.ino new file mode 100644 index 0000000..2b27c97 --- /dev/null +++ b/libraries/ConfigurableFirmata/examples/ConfigurableFirmata/ConfigurableFirmata.ino @@ -0,0 +1,441 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. All rights reserved. + Copyright (C) 2009-2017 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: September 16th, 2017 +*/ + +/* + README + + This is an example use of ConfigurableFirmata. The easiest way to create a configuration is to + use http://firmatabuilder.com and select the communication transport and the firmata features + to include and an Arduino sketch (.ino) file will be generated and downloaded automatically. + + To manually configure a sketch, copy this file and follow the instructions in the + ETHERNET CONFIGURATION OPTION (if you want to use Ethernet instead of Serial/USB) and + FIRMATA FEATURE CONFIGURATION sections in this file. +*/ + +#include "ConfigurableFirmata.h" + +/*============================================================================== + * ETHERNET CONFIGURATION OPTION + * + * By default Firmata uses the Serial-port (over USB) of the Arduino. ConfigurableFirmata may also + * comunicate over ethernet using tcp/ip. To configure this sketch to use Ethernet instead of + * Serial, uncomment the approprate includes for your particular hardware. See STEPS 1 - 5 below. + * If you want to use Serial (over USB) then skip ahead to the FIRMATA FEATURE CONFIGURATION + * section further down in this file. + * + * If you enable Ethernet, you will need a Firmata client library with a network transport that can + * act as a server in order to establish a connection between ConfigurableFirmataEthernet and the + * Firmata host application (your application). + * + * To use ConfigurableFirmata with Ethernet you will need to have one of the following + * boards or shields: + * + * - Arduino Ethernet shield (or clone) + * - Arduino Ethernet board (or clone) + * - Arduino Yun + * + * If you are using an Arduino Ethernet shield you cannot use the following pins on + * the following boards. Firmata will ignore any requests to use these pins: + * + * - Arduino Uno or other ATMega328 boards: (D4, D10, D11, D12, D13) + * - Arduino Mega: (D4, D10, D50, D51, D52, D53) + * - Arduino Leonardo: (D4, D10) + * - Arduino Due: (D4, D10) + * - Arduino Zero: (D4, D10) + * + * If you are using an ArduinoEthernet board, the following pins cannot be used (same as Uno): + * - D4, D10, D11, D12, D13 + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) + +/* + * OPTION A: Configure for Arduino Ethernet board or Arduino Ethernet shield (or clone) + * + * To configure ConfigurableFirmata to use the an Arduino Ethernet Shield or Arduino Ethernet + * Board (both use the same WIZ5100-based Ethernet controller), uncomment the SPI and Ethernet + * includes below. + */ +//#include +//#include + + +/* + * OPTION B: Configure for a board or shield using an ENC28J60-based Ethernet controller, + * uncomment out the UIPEthernet include below. + * + * The UIPEthernet-library can be downloaded + * from: https://github.com/ntruchsess/arduino_uip + */ +//#include + + +/* + * OPTION C: Configure for Arduino Yun + * + * The Ethernet port on the Arduino Yun board can be used with Firmata in this configuration. + * To execute StandardFirmataEthernet on Yun uncomment the Bridge and YunClient includes below. + * + * NOTE: in order to compile for the Yun you will also need to comment out some of the includes + * and declarations in the FIRMATA FEATURE CONFIGURATION section later in this file. Including all + * features exceeds the RAM and Flash memory of the Yun. Comment out anything you don't need. + * + * On Yun there's no need to configure local_ip and mac address as this is automatically + * configured on the linux-side of Yun. + * + * Establishing a connection with the Yun may take several seconds. + */ +//#include +//#include + +#if defined ethernet_h || defined UIPETHERNET_H || defined _YUN_CLIENT_H_ +#define NETWORK_FIRMATA + +// STEP 2 [REQUIRED for all boards and shields] +// replace with IP of the server you want to connect to, comment out if using 'remote_host' +#define remote_ip IPAddress(192, 168, 0, 1) +// OR replace with hostname of server you want to connect to, comment out if using 'remote_ip' +// #define remote_host "server.local" + +// STEP 3 [REQUIRED unless using Arduino Yun] +// Replace with the port that your server is listening on +#define remote_port 3030 + +// STEP 4 [REQUIRED unless using Arduino Yun OR if not using DHCP] +// Replace with your board or Ethernet shield's IP address +// Comment out if you want to use DHCP +#define local_ip IPAddress(192, 168, 0, 6) + +// STEP 5 [REQUIRED unless using Arduino Yun] +// replace with Ethernet shield mac. Must be unique for your network +const byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x07, 0x02}; +#endif + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +#include +DigitalInputFirmata digitalInput; + +#include +DigitalOutputFirmata digitalOutput; + +#include +AnalogInputFirmata analogInput; + +#include +AnalogOutputFirmata analogOutput; + +#include +#include +ServoFirmata servo; +// ServoFirmata depends on AnalogOutputFirmata +#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +#error AnalogOutputFirmata must be included to use ServoFirmata +#endif + +#include +#include +I2CFirmata i2c; + +#include +OneWireFirmata oneWire; + +// StepperFirmata is deprecated as of ConfigurableFirmata v2.10.0. Please update your +// client implementation to use the new, more full featured and scalable AccelStepperFirmata. +#include +StepperFirmata stepper; + +#include +AccelStepperFirmata accelStepper; + +#include +SerialFirmata serial; + +#include +FirmataExt firmataExt; + +#include +FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + +// dependencies for Network Firmata. Do not comment out. +#ifdef NETWORK_FIRMATA +#if defined remote_ip && defined remote_host +#error "cannot define both remote_ip and remote_host at the same time!" +#endif +#include +#ifdef _YUN_CLIENT_H_ +YunClient client; +#else +EthernetClient client; +#endif +#if defined remote_ip && !defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, remote_ip, NULL, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, remote_port); +#endif +#endif +#if !defined remote_ip && defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, remote_port); +#endif +#endif +#endif + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + /* + * ETHERNET SETUP + */ +#ifdef NETWORK_FIRMATA +#ifdef _YUN_CLIENT_H_ + Bridge.begin(); +#else +#ifdef local_ip + Ethernet.begin((uint8_t *)mac, local_ip); //start Ethernet +#else + Ethernet.begin((uint8_t *)mac); //start Ethernet using dhcp +#endif +#endif + delay(1000); +#endif + + /* + * FIRMATA SETUP + */ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef AccelStepperFirmata_h + firmataExt.addFeature(accelStepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // Network Firmata communicates with Ethernet-shields over SPI. Therefor all + // SPI-pins must be set to PIN_MODE_IGNORE. Otherwise Firmata would break SPI-communication. + // add Pin 10 and configure pin 53 as output if using a MEGA with Ethernetshield. + // No need to ignore pin 10 on MEGA with ENC28J60, as here pin 53 should be connected to SS: +#ifdef NETWORK_FIRMATA + + #ifndef _YUN_CLIENT_H_ + // ignore SPI and pin 4 that is SS for SD-Card on Ethernet-shield + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_SPI(i) + || 4 == i // SD Card on Ethernet shield uses pin 4 for SS + || 10 == i // Ethernet-shield uses pin 10 for SS + ) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } + // pinMode(PIN_TO_DIGITAL(53), OUTPUT); configure hardware-SS as output on MEGA + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD-card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + #endif + + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA + #endif + + // start up Network Firmata: + Firmata.begin(stream); +#else + // Uncomment to save a couple of seconds by disabling the startup blink sequence. + // Firmata.disableBlinkVersion(); + + // start up the default Firmata using Serial interface: + Firmata.begin(57600); +#endif + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + + /* SEND STREAM WRITE BUFFER - TO DO: make sure that the stream buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef AccelStepperFirmata_h + accelStepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + +#if defined NETWORK_FIRMATA && !defined local_ip &&!defined _YUN_CLIENT_H_ + // only necessary when using DHCP, ensures local IP is updated appropriately if it changes + if (Ethernet.maintain()) { + stream.maintain(Ethernet.localIP()); + } +#endif +} diff --git a/libraries/ConfigurableFirmata/examples/ConfigurableFirmataBLE/ConfigurableFirmataBLE.ino b/libraries/ConfigurableFirmata/examples/ConfigurableFirmataBLE/ConfigurableFirmataBLE.ino new file mode 100644 index 0000000..cf6aacc --- /dev/null +++ b/libraries/ConfigurableFirmata/examples/ConfigurableFirmataBLE/ConfigurableFirmataBLE.ino @@ -0,0 +1,382 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. All rights reserved. + Copyright (C) 2009-2017 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 September 16th, 2017 +*/ + +/* + README + + This is an example use of ConfigurableFirmata with BLE. + + ConfigurableFirmataBLE enables the use of Firmata over a BLE connection. To configure your + connection, follow the instructions in the BLE CONFIGURATION section of this file. + + To use ConfigurableFirmataBLE you will need to have one of the following boards or shields: + - Arduino 101 (recommended) + - RedBear BLE Nano ** works with modifications ** + - RedBear BLE Shield v2 ** to be verfied ** + + If you are using an Arduino 101, make sure you have the Intel Curie Boards package v1.0.6 + or higer installed via the Arduino Boards Manager. +*/ + +#include "ConfigurableFirmata.h" + +// min cannot be < 0x0006. Adjust max if necessary +#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25) +#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25) + +/*============================================================================== + * BLE CONFIGURATION + * + * If you are using an Arduino 101, you do not need to make any changes to this + * section of the file unless you need a unique ble local name. If you are using + * another supported BLE board or shield, follow the instructions for the specific + * board or shield below. + *============================================================================*/ + +// change this to a unique name per board if running StandardFirmataBLE on multiple boards +// within the same physical space +#define FIRMATA_BLE_LOCAL_NAME "FIRMATA" + + +/* + * Arduino 101 + * + * Make sure you have the Intel Curie Boards package v1.0.6 or higher installed via the Arduino + * Boards Manager. + * + * Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27 + */ +#ifdef _VARIANT_ARDUINO_101_X_ +#include +#include "utility/BLEStream.h" +BLEStream stream; +#endif + + +/* + * RedBearLab BLE Nano (with default switch settings) + * + * Blocked on this issue: https://github.com/RedBearLab/nRF51822-Arduino/pull/97 + * Works with modifications. See comments at top of the test script referenced below. + * When the RBL nRF51822-Arduino library issue is resolved, this should work witout + * any modifications. + * + * Test script: https://gist.github.com/soundanalogous/d39bb3eb36333a0906df + * + * Note: If you have changed the solder jumpers on the Nano you may encounter issues since + * the pins are currently mapped in Firmata only for the default (factory) jumper settings. + */ +#ifdef BLE_NANO +#include +#include "utility/BLEStream.h" +BLEStream stream; +#endif + +/* + * RedBearLab BLE Shield + * + * If you are using a RedBearLab BLE shield, uncomment the define below. + * Also, change the define for BLE_RST if you have the jumper set to pin 7 rather than pin 4. + * + * You will need to use the shield with an Arduino Zero, Due, Mega, or other board with sufficient + * Flash and RAM. Arduino Uno, Leonardo and other ATmega328p and Atmega32u4 boards do not have + * enough memory to run StandardFirmataBLE. + * + * TODO: verify if this works and with which boards it works. + * + * Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27 + */ +//#define REDBEAR_BLE_SHIELD + +#ifdef REDBEAR_BLE_SHIELD +#include +#include +#include "utility/BLEStream.h" + +#define BLE_REQ 9 +#define BLE_RDY 8 +#define BLE_RST 4 // 4 or 7 via jumper on shield + +BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST); +#endif + + +#if defined(BLE_REQ) && defined(BLE_RDY) && defined(BLE_RST) +#define IS_IGNORE_BLE_PINS(p) ((p) == BLE_REQ || (p) == BLE_RDY || (p) == BLE_RST) +#endif + + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +#include +DigitalInputFirmata digitalInput; + +#include +DigitalOutputFirmata digitalOutput; + +#include +AnalogInputFirmata analogInput; + +#include +AnalogOutputFirmata analogOutput; + +#include +#include +ServoFirmata servo; +// ServoFirmata depends on AnalogOutputFirmata +#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +#error AnalogOutputFirmata must be included to use ServoFirmata +#endif + +#include +#include +I2CFirmata i2c; + +#include +OneWireFirmata oneWire; + +// StepperFirmata is deprecated as of ConfigurableFirmata v2.10.0. Please update your +// client implementation to use the new, more full featured and scalable AccelStepperFirmata. +#include +StepperFirmata stepper; + +#include +AccelStepperFirmata accelStepper; + +#include +SerialFirmata serial; + +#include +FirmataExt firmataExt; + +#include +FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void ignorePins() +{ +#ifdef BLE_REQ + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_IGNORE_BLE_PINS(i)) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } +#endif +} + +void initTransport() +{ + stream.setLocalName(FIRMATA_BLE_LOCAL_NAME); + // set the BLE connection interval - this is the fastest interval you can read inputs + stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL); + // set how often the BLE TX buffer is flushed (if not full) + stream.setFlushInterval(FIRMATA_BLE_MAX_INTERVAL); + + stream.begin(); +} + +void initFirmata() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef AccelStepperFirmata_h +firmataExt.addFeature(accelStepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + ignorePins(); + + // Initialize Firmata to use the BLE stream object as the transport. + Firmata.begin(stream); + systemResetCallback(); // reset to default config +} + +void setup() +{ + initTransport(); + + initFirmata(); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + // do not process data if no BLE connection is established + // poll will send the TX buffer at the specified flush interval or when the buffer is full + if (!stream.poll()) return; + +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef AccelStepperFirmata_h +accelStepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + +} diff --git a/libraries/ConfigurableFirmata/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino b/libraries/ConfigurableFirmata/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino new file mode 100644 index 0000000..b4523ec --- /dev/null +++ b/libraries/ConfigurableFirmata/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino @@ -0,0 +1,690 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. All rights reserved. + Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved. + Copyright (C) 2016 Jens B. 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: September 16th, 2017 +*/ + +/* + README + + This is an example use of ConfigurableFirmata with WiFi. + + ConfigurableFirmataWiFi enables the use of Firmata over a TCP connection. It can be configured as + either a TCP server or TCP client. To configure your Wi-Fi connection, follow the instructions in + the WIFI CONFIGURATION section of this file. + + To use ConfigurableFirmataWiFi you will need to have one of the following + boards or shields: + + - Arduino MKR1000 board (recommended) + - ESP8266 WiFi board compatible with ESP8266 Arduino core + - Arduino WiFi Shield 101 + - Arduino WiFi Shield (or clone) + + Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to + configure your particular hardware. + + Dependencies: + - WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino + 1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source: + https://github.com/arduino-libraries/WiFi101) + - ESP8266 requires the Arduino ESP8266 core v2.1.0 or higher which can be obtained here: + https://github.com/esp8266/Arduino + + In order to use the WiFi Shield 101 with Firmata you will need a board with at least 35k of Flash + memory. This means you cannot use the WiFi Shield 101 with an Arduino Uno or any other + ATmega328p-based microcontroller or with an Arduino Leonardo or other ATmega32u4-based + microcontroller. Some boards that will work are: + + - Arduino Zero + - Arduino Due + - Arduino 101 + - Arduino Mega + + NOTE: If you are using an Arduino WiFi (legacy) shield you cannot use the following pins on + the following boards. Firmata will ignore any requests to use these pins: + + - Arduino Uno or other ATMega328 boards: (D4, D7, D10, D11, D12, D13) + - Arduino Mega: (D4, D7, D10, D50, D51, D52, D53) + - Arduino Due, Zero or Leonardo: (D4, D7, D10) + + If you are using an Arduino WiFi 101 shield you cannot use the following pins on the following + boards: + + - Arduino Due or Zero: (D5, D7, D10) + - Arduino Mega: (D5, D7, D10, D50, D52, D53) +*/ + +#include "ConfigurableFirmata.h" + +/* + * Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your + * connection that may help in the event of connection issues. If defined, some boards may not begin + * executing this sketch until the Serial console is opened. + */ +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +#define MAX_CONN_ATTEMPTS 20 // [500 ms] -> 10 s + +/*============================================================================== + * WIFI CONFIGURATION + * + * You must configure your particular hardware. Follow the steps below. + * + * By default, ConfigurableFirmataWiFi is configured as a TCP server, to configure + * as a TCP client, see STEP 2. + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) +// Arduino MKR1000 or ESP8266 are enabled by default if compiling for either of those boards. + +/* + * OPTION A: Configure for Arduino MKR1000 or Arduino WiFi Shield 101 + * + * This will configure ConfigurableFirmataWiFi to use the WiFi101 library, which works with the + * Arduino WiFi101 shield and devices that have the WiFi101 chip built in (such as the MKR1000). + * It is compatible with 802.11 B/G/N networks. + * + * If you are using the MKR1000 board, continue on to STEP 2. If you are using the WiFi 101 shield, + * follow the instructions below. + * + * To enable for the WiFi 101 shield, uncomment the #define WIFI_101 below and verify the + * #define ARDUINO_WIFI_SHIELD is commented out for OPTION B. + * + * IMPORTANT: You must have the WiFI 101 library installed. To easily install this library, open + * the library manager via: Arduino IDE Menus: Sketch > Include Library > Manage Libraries > filter + * search for "WiFi101" > Select the result and click 'install' + */ +//#define WIFI_101 + +//do not modify the following 11 lines +#if defined(ARDUINO_SAMD_MKR1000) && !defined(WIFI_101) +// automatically include if compiling for MRK1000 +#define WIFI_101 +#endif +#ifdef WIFI_101 +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #define WIFI_LIB_INCLUDED +#endif + +/* + * OPTION B: Configure for legacy Arduino WiFi shield + * + * This will configure ConfigurableFirmataWiFi to use the original WiFi library (deprecated) provided + * with the Arduino IDE. It is supported by the Arduino WiFi shield (a discontinued product) and + * is compatible with 802.11 B/G networks. + * + * To configure ConfigurableFirmataWiFi to use the legacy Arduino WiFi shield + * leave the #define below uncommented and ensure #define WIFI_101 is commented out for OPTION A. + */ +//#define ARDUINO_WIFI_SHIELD + +//do not modify the following 10 lines +#ifdef ARDUINO_WIFI_SHIELD +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif +#endif + +/* + * OPTION C: Configure for ESP8266 + * + * This will configure ConfigurableFirmataWiFi to use the ESP8266WiFi library for boards + * with an ESP8266 chip. It is compatible with 802.11 B/G/N networks. + * + * The appropriate libraries are included automatically when compiling for the ESP8266 so + * continue on to STEP 2. + * + * IMPORTANT: You must have the esp8266 board support installed. To easily install this board see + * the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager. + */ +//do not modify the following 14 lines +#ifdef ESP8266 +// automatically include if compiling for ESP8266 +#define ESP8266_WIFI +#endif +#ifdef ESP8266_WIFI +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif +#endif + + +// STEP 2 [OPTIONAL for all boards and shields] +// By default the board/shield is configured as a TCP server. +// If you want to setup you board/shield as a TCP client, uncomment the following define and +// replace the REMOTE_SERVER_IP address below with the IP address of your remote server. +//#define REMOTE_SERVER_IP 10, 0, 0, 15 + + +// STEP 3 [REQUIRED for all boards and shields] +// replace this with your wireless network SSID +char ssid[] = "your_network_name"; + + +// STEP 4 [OPTIONAL for all boards and shields] +// If you want to use a static IP (v4) address, uncomment the line below. You can also change the IP. +// If the first line is commented out, the WiFi shield will attempt to get an IP from the DHCP server. +// If you are using a static IP with the ESP8266 then you must also uncomment the SUBNET and GATEWAY. +//#define STATIC_IP_ADDRESS 192,168,1,113 +//#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, optional for others +//#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, optional for others + + +// STEP 5 [REQUIRED for all boards and shields] +// define your port number here, you will need this to open a TCP connection to your Arduino +#define NETWORK_PORT 3030 + + +// STEP 6 [REQUIRED for all boards and shields] +// determine your network security type (OPTION A, B, or C). Option A is the most common, and the +// default. + +/* + * OPTION A: WPA / WPA2 + * + * WPA is the most common network security type. A passphrase is required to connect to this type. + * + * To enable, leave #define WIFI_WPA_SECURITY uncommented below, set your wpa_passphrase value + * appropriately, and do not uncomment the #define values under options B and C + */ +#define WIFI_WPA_SECURITY + +#ifdef WIFI_WPA_SECURITY +char wpa_passphrase[] = "your_wpa_passphrase"; +#endif //WIFI_WPA_SECURITY + + +/* + * OPTION B: WEP (not supported for ESP8266) + * + * WEP is a less common (and regarded as less safe) security type. A WEP key and its associated + * index are required to connect to this type. + * + * To enable, Uncomment the #define below, set your wep_index and wep_key values appropriately, + * and verify the #define values under options A and C are commented out. + */ +//#define WIFI_WEP_SECURITY + +#ifdef WIFI_WEP_SECURITY +//The wep_index below is a zero-indexed value. +//Valid indices are [0-3], even if your router/gateway numbers your keys [1-4]. +byte wep_index = 0; +char wep_key[] = "your_wep_key"; +#endif //WIFI_WEP_SECURITY + + +/* + * OPTION C: Open network (no security) + * + * Open networks have no security, can be connected to by any device that knows the ssid, and are + * unsafe. + * + * To enable, uncomment #define WIFI_NO_SECURITY below and verify the #define values + * under options A and B are commented out. + */ +//#define WIFI_NO_SECURITY + +/*============================================================================== + * CONFIGURATION ERROR CHECK (don't change anything here) + *============================================================================*/ + +#ifdef MULTIPLE_WIFI_LIB_INCLUDES +#error "you may not define more than one wifi device type in wifiConfig.h." +#endif + +#ifndef WIFI_LIB_INCLUDED +#error "you must define a wifi device type in wifiConfig.h." +#endif + +#if ((defined(WIFI_NO_SECURITY) && (defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY))) || (defined(WIFI_WEP_SECURITY) && defined(WIFI_WPA_SECURITY))) +#error "you may not define more than one security type at the same time in wifiConfig.h." +#endif //WIFI_* security define check + +#if !(defined(WIFI_NO_SECURITY) || defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY)) +#error "you must define a wifi security type in wifiConfig.h." +#endif //WIFI_* security define check + +#if (defined(ESP8266_WIFI) && !(defined(WIFI_NO_SECURITY) || (defined(WIFI_WPA_SECURITY)))) +#error "you must choose between WIFI_NO_SECURITY and WIFI_WPA_SECURITY" +#endif + +/*============================================================================== + * WIFI STREAM (don't change anything here) + *============================================================================*/ + +#ifdef REMOTE_SERVER_IP + WiFiClientStream stream(IPAddress(REMOTE_SERVER_IP), NETWORK_PORT); +#else + WiFiServerStream stream(NETWORK_PORT); +#endif + +/*============================================================================== + * PIN IGNORE MACROS (don't change anything here) + *============================================================================*/ + +#if defined(WIFI_101) && !defined(ARDUINO_SAMD_MKR1000) +// ignore SPI pins, pin 5 (reset WiFi101 shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +// also don't ignore SS pin if it's not pin 10. Not needed for Arduino MKR1000. +#define IS_IGNORE_PIN(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7) + +#elif defined(ARDUINO_WIFI_SHIELD) && defined(__AVR_ATmega32U4__) +// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +// On Leonardo, pin 24 maps to D4 and pin 28 maps to D10 +#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10 || (p) == 24 || (p) == 28) + +#elif defined(ARDUINO_WIFI_SHIELD) +// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10) + +#elif defined(ESP8266_WIFI) && defined(SERIAL_DEBUG) +#define IS_IGNORE_PIN(p) ((p) == 1) + +#endif + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +#include +DigitalInputFirmata digitalInput; + +#include +DigitalOutputFirmata digitalOutput; + +#include +AnalogInputFirmata analogInput; + +#include +AnalogOutputFirmata analogOutput; + +#include +#include +ServoFirmata servo; +// ServoFirmata depends on AnalogOutputFirmata +#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +#error AnalogOutputFirmata must be included to use ServoFirmata +#endif + +#include +#include +I2CFirmata i2c; + +#include +OneWireFirmata oneWire; + +// StepperFirmata is deprecated as of ConfigurableFirmata v2.10.0. Please update your +// client implementation to use the new, more full featured and scalable AccelStepperFirmata. +#include +StepperFirmata stepper; + +#include +AccelStepperFirmata accelStepper; + +#include +SerialFirmata serial; + +#include +FirmataExt firmataExt; + +#include +FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + + +#ifdef STATIC_IP_ADDRESS +IPAddress local_ip(STATIC_IP_ADDRESS); +#endif +#ifdef SUBNET_MASK +IPAddress subnet(SUBNET_MASK); +#endif +#ifdef GATEWAY_IP_ADDRESS +IPAddress gateway(GATEWAY_IP_ADDRESS); +#endif + +int connectionAttempts = 0; +bool streamConnected = false; + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +/* + * Called when a TCP connection is either connected or disconnected. + * TODO: + * - report connected or reconnected state to host (to be added to protocol) + */ +void hostConnectionCallback(byte state) +{ + switch (state) { + case HOST_CONNECTION_CONNECTED: + DEBUG_PRINTLN( "TCP connection established" ); + break; + case HOST_CONNECTION_DISCONNECTED: + DEBUG_PRINTLN( "TCP connection disconnected" ); + break; + } +} + +void printWifiStatus() { + if ( WiFi.status() != WL_CONNECTED ) + { + DEBUG_PRINT( "WiFi connection failed. Status value: " ); + DEBUG_PRINTLN( WiFi.status() ); + } + else + { + // print the SSID of the network you're attached to: + DEBUG_PRINT( "SSID: " ); + DEBUG_PRINTLN( WiFi.SSID() ); + + // print your WiFi shield's IP address: + DEBUG_PRINT( "IP Address: " ); + IPAddress ip = WiFi.localIP(); + DEBUG_PRINTLN( ip ); + + // print the received signal strength: + DEBUG_PRINT( "signal strength (RSSI): " ); + long rssi = WiFi.RSSI(); + DEBUG_PRINT( rssi ); + DEBUG_PRINTLN( " dBm" ); + } +} + +/* + * ConfigurableFirmataWiFi communicates with WiFi shields over SPI. Therefore all + * SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication. + * Additional pins may also need to be ignored depending on the particular board or + * shield in use. + */ +void ignorePins() +{ +#ifdef IS_IGNORE_PIN + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_IGNORE_PIN(i)) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } +#endif + + //Set up controls for the Arduino WiFi Shield SS for the SD Card +#ifdef ARDUINO_WIFI_SHIELD + // Arduino WiFi Shield has SD SS wired to D4 + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA +#endif + +#endif //ARDUINO_WIFI_SHIELD +} + +void initTransport() +{ + // This statement will clarify how a connection is being made + DEBUG_PRINT( "ConfigurableFirmataWiFi will attempt a WiFi connection " ); +#if defined(WIFI_101) + DEBUG_PRINTLN( "using the WiFi 101 library." ); +#elif defined(ARDUINO_WIFI_SHIELD) + DEBUG_PRINTLN( "using the legacy WiFi library." ); +#elif defined(ESP8266_WIFI) + DEBUG_PRINTLN( "using the ESP8266 WiFi library." ); + //else should never happen here as error-checking in wifiConfig.h will catch this +#endif //defined(WIFI_101) + + // Configure WiFi IP Address +#ifdef STATIC_IP_ADDRESS + DEBUG_PRINT( "Using static IP: " ); + DEBUG_PRINTLN( local_ip ); +#if defined(ESP8266_WIFI) || (defined(SUBNET_MASK) && defined(GATEWAY_IP_ADDRESS)) + stream.config( local_ip , gateway, subnet ); +#else + // you can also provide a static IP in the begin() functions, but this simplifies + // ifdef logic in this sketch due to support for all different encryption types. + stream.config( local_ip ); +#endif +#else + DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); +#endif + + stream.attach(hostConnectionCallback); + + // Configure WiFi security and initiate WiFi connection +#if defined(WIFI_WEP_SECURITY) + DEBUG_PRINT( "Attempting to connect to WEP SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid, wep_index, wep_key); +#elif defined(WIFI_WPA_SECURITY) + DEBUG_PRINT( "Attempting to connect to WPA SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid, wpa_passphrase); +#else //OPEN network + DEBUG_PRINTLN( "Attempting to connect to open SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid); +#endif //defined(WIFI_WEP_SECURITY) + DEBUG_PRINTLN( "WiFi setup done" ); + + // Wait for connection to access point to be established. + while (WiFi.status() != WL_CONNECTED && ++connectionAttempts <= MAX_CONN_ATTEMPTS) { + delay(500); + DEBUG_PRINT("."); + } + printWifiStatus(); +} + +void initFirmata() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef AccelStepperFirmata_h +firmataExt.addFeature(accelStepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + ignorePins(); + + // Initialize Firmata to use the WiFi stream object as the transport. + Firmata.begin(stream); + systemResetCallback(); // reset to default config +} + + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + DEBUG_BEGIN(9600); + + initTransport(); + + initFirmata(); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef AccelStepperFirmata_h +accelStepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + + // keep the WiFi connection live. Attempts to reconnect automatically if disconnected. + stream.maintain(); +} diff --git a/libraries/ConfigurableFirmata/extras/LICENSE.txt b/libraries/ConfigurableFirmata/extras/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/ConfigurableFirmata/extras/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/ConfigurableFirmata/keywords.txt b/libraries/ConfigurableFirmata/keywords.txt new file mode 100644 index 0000000..d713ff1 --- /dev/null +++ b/libraries/ConfigurableFirmata/keywords.txt @@ -0,0 +1,96 @@ +####################################### +# Syntax Coloring Map For Firmata +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Firmata KEYWORD1 Firmata +callbackFunction KEYWORD1 callbackFunction +systemResetCallbackFunction KEYWORD1 systemResetCallbackFunction +stringCallbackFunction KEYWORD1 stringCallbackFunction +sysexCallbackFunction KEYWORD1 sysexCallbackFunction + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +printVersion KEYWORD2 +blinkVersion KEYWORD2 +printFirmwareVersion KEYWORD2 +setFirmwareVersion KEYWORD2 +setFirmwareNameAndVersion KEYWORD2 +available KEYWORD2 +processInput KEYWORD2 +isParsingMessage KEYWORD2 +parse KEYWORD2 +sendAnalog KEYWORD2 +sendDigital KEYWORD2 +sendDigitalPort KEYWORD2 +sendString KEYWORD2 +sendSysex KEYWORD2 +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +sendValueAsTwo7bitBytes KEYWORD2 +startSysex KEYWORD2 +endSysex KEYWORD2 +attachDelayTask KEYWORD2 +delayTask KEYWORD2 +getPinMode KEYWORD2 +setPinMode KEYWORD2 +getPinState KEYWORD2 +setPinState KEYWORD2 +writePort KEYWORD2 +readPort KEYWORD2 +disableBlinkVersion KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + +FIRMATA_PROTOCOL_MAJOR_VERSION LITERAL1 +FIRMATA_PROTOCOL_MINOR_VERSION LITERAL1 +FIRMATA_PROTOCOL_BUGFIX_VERSION LITERAL1 + +FIRMATA_FIRMWARE_MAJOR_VERSION LITERAL1 +FIRMATA_FIRMWARE_MINOR_VERSION LITERAL1 +FIRMATA_FIRMWARE_BUGFIX_VERSION LITERAL1 + +MAX_DATA_BYTES LITERAL1 + +DIGITAL_MESSAGE LITERAL1 +ANALOG_MESSAGE LITERAL1 +REPORT_ANALOG LITERAL1 +REPORT_DIGITAL LITERAL1 +REPORT_VERSION LITERAL1 +SET_PIN_MODE LITERAL1 +SET_DIGITAL_PIN_VALUE LITERAL1 +SYSTEM_RESET LITERAL1 +START_SYSEX LITERAL1 +END_SYSEX LITERAL1 +REPORT_FIRMWARE LITERAL1 +STRING_DATA LITERAL1 + +PIN_MODE_ANALOG LITERAL1 +PIN_MODE_PWM LITERAL1 +PIN_MODE_SERVO LITERAL1 +PIN_MODE_SHIFT LITERAL1 +PIN_MODE_I2C LITERAL1 +PIN_MODE_ONEWIRE LITERAL1 +PIN_MODE_STEPPER LITERAL1 +PIN_MODE_ENCODER LITERAL1 +PIN_MODE_SERIAL LITERAL1 +PIN_MODE_PULLUP LITERAL1 +PIN_MODE_IGNORE LITERAL1 + +TOTAL_PINS LITERAL1 +TOTAL_ANALOG_PINS LITERAL1 +TOTAL_DIGITAL_PINS LITERAL1 +TOTAL_PIN_MODES LITERAL1 +TOTAL_PORTS LITERAL1 +ANALOG_PORT LITERAL1 +MAX_SERVOS LITERAL1 diff --git a/libraries/ConfigurableFirmata/library.json b/libraries/ConfigurableFirmata/library.json new file mode 100644 index 0000000..ee4ff5b --- /dev/null +++ b/libraries/ConfigurableFirmata/library.json @@ -0,0 +1,16 @@ +{ + "name": "ConfigurableFirmata", + "keywords": "interface, protocol", + "description": "This library implements the Firmata protocol as a set of plugins that can be used to create applications to remotely interface with an Arduino board.", + "repository": { + "type": "git", + "url": "https://github.com/firmata/ConfigurableFirmata.git" + }, + "version": "2.10.0", + "exclude": [ + "extras", + "test" + ], + "frameworks": "arduino", + "platforms": "*" +} diff --git a/libraries/ConfigurableFirmata/library.properties b/libraries/ConfigurableFirmata/library.properties new file mode 100644 index 0000000..ed53692 --- /dev/null +++ b/libraries/ConfigurableFirmata/library.properties @@ -0,0 +1,9 @@ +name=ConfigurableFirmata +version=2.10.0 +author=Firmata Developers +maintainer=https://github.com/firmata/ConfigurableFirmata +sentence=This library implements the Firmata protocol as a set of plugins that can be used to create applications to remotely interface with an Arduino board. +paragraph=ConfigurableFirmata is an implementation of the Firmata protocol that breaks features such as Digital Input, Digital Output, Analog Input, Analog Output, I2C, etc into individual classes making it easier to mix and match standard features with custom features. +category=Device Control +url=https://github.com/firmata/ConfigurableFirmata +architectures=* diff --git a/libraries/ConfigurableFirmata/readme.md b/libraries/ConfigurableFirmata/readme.md new file mode 100644 index 0000000..9c2a19a --- /dev/null +++ b/libraries/ConfigurableFirmata/readme.md @@ -0,0 +1,73 @@ +# ConfigurableFirmata + +[![Join the chat at https://gitter.im/firmata/ConfigurableFirmata](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/firmata/ConfigurableFirmata?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Firmata is a protocol for communicating with microcontrollers from software on a host computer. The [protocol](https://github.com/firmata/protocol) can be implemented in firmware on any microcontroller architecture as well as software on any host computer software package. The arduino repository described here is a Firmata library for Arduino and Arduino-compatible devices. If you would like to contribute to Firmata, please see the [Contributing](#contributing) section below. + +## Installation + +- **If you are using Arduino IDE version 1.6.4 or higher** go to `Sketch > Include Library > Manage Libraries` and then search for "ConfigurableFirmata" and click on `Install` after tapping on the ConfigurableFirmata item in the filtered results. You can also use this same method to update ConfigurableFirmata in the future. +- **If you are using an older version of the Arduino IDE**, download or clone ConfigurableFirmata to your Arduino sketchbook library folder. This is typically `/Documents/Arduino/libraries/` on Mac or Linux or `\My Documents\Arduino\libraries\` on Windows. + +## Usage + +ConfigurableFirmata is a version of Firmata that breaks features such as Digital Input, Digital Output, Analog Input, Analog Output, I2C, etc into [individual classes](https://github.com/firmata/ConfigurableFirmata/tree/master/src) making it easier to mix and match standard features with custom features. + +The easiest way to use ConfigurableFirmata is with [firmatabuilder](http://firmatabuilder.com) which is a simple web application that generates an Arduino sketch based on a selection of Firmata features. Download the generated sketch, compile and upload it to your board. + +Another way to use ConfigurableFirmata is by adding or removing various include statements in the [ConfigurableFirmata.ino](https://github.com/firmata/ConfigurableFirmata/blob/master/examples/ConfigurableFirmata/ConfigurableFirmata.ino) example file. + +## Firmata Wrapper Libraries + +You can use the ConfigurableFirmata architecture to wrap 3rd party libraries to include +functionality not included in the base ConfigurableFirmata.ino example. See [FirmataEncoder](https://github.com/firmata/FirmataEncoder) for an example of a Firmata wrapper. To include a Firmata wrapper your +ino file, you must install both the sketch and the 3rd party library into your `/Arduino/libraries/` +directory (where all 3rd party libraries are installed). + +When creating a new Firmata wrapper library, you generally should not include the 3rd party +library it wraps. For example, the Encoder library that FirmataEncoder wraps is not included with +the FirmataEncoder library. + +If you create a wrapper library, prepend the name with 'Firmata'. Hence 'FirmataEncoder' in the +referenced example. This will keep the wrapper libraries together in the user's Arduino libraries +directory. + +A Firmata wrapper template library will be published soon along with instructions for creating +a wrapper library. + +## Firmata Client Libraries +Only a few Firmata client libraries currently support ConfigurableFirmata: + +* javascript + * [https://github.com/jgautier/firmata] + * [https://github.com/rwldrn/johnny-five] + * [http://breakoutjs.com] +* perl + * [https://github.com/ntruchsess/perl-firmata] + +*Additional Firmata client libraries may work as well. If you're a client library developer and have verified that you library works with ConfigurableFirmata, please [open an issue](https://github.com/firmata/ConfigurableFirmata/issues) with a request to add the link.* + +## Contributing + +If you discover a bug or would like to propose a new feature, please open a new [issue](https://github.com/firmata/ConfigurableFirmata/issues?sort=created&state=open). + +To contribute, fork this repository and create a new topic branch for the bug, feature or other existing issue you are addressing. Submit the pull request against the *master* branch. + +You must thoroughly test your contributed code. In your pull request, describe tests performed to ensure that no existing code is broken and that any changes maintain backwards compatibility with the existing api. Test on multiple Arduino board variants if possible. We hope to enable some form of automated (or at least semi-automated) testing in the future, but for now any tests will need to be executed manually by the contributor and reviewers. + +Use [Artistic Style](http://astyle.sourceforge.net/) (astyle) to format your code. Set the following rules for the astyle formatter: + +``` +style = "" +indent = "spaces" +indent-spaces = 2 +indent-classes = true +indent-switches = true +indent-cases = true +indent-col1-comments = true +pad-oper = true +pad-header = true +keep-one-line-statements = true +``` + +If you happen to use Sublime Text, [this astyle plugin](https://github.com/timonwong/SublimeAStyleFormatter) is helpful. Set the above rules in the user settings file. diff --git a/libraries/ConfigurableFirmata/src/AccelStepperFirmata.cpp b/libraries/ConfigurableFirmata/src/AccelStepperFirmata.cpp new file mode 100644 index 0000000..6542b89 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AccelStepperFirmata.cpp @@ -0,0 +1,420 @@ +/* + 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-2017 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: September 16th, 2017 +*/ + +#include +#include "AccelStepperFirmata.h" +#include "utility/AccelStepper.h" +#include "utility/MultiStepper.h" + +boolean AccelStepperFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_STEPPER) { + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), OUTPUT); + return true; + } + } + return false; +} + +void AccelStepperFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_STEPPER); + Firmata.write(21); //21 bits used for number of steps + } +} + +// Send position data when it's requested or a move completes +void AccelStepperFirmata::reportPosition(byte deviceNum, bool complete) +{ + if (stepper[deviceNum]) { + byte data[5]; + long position = stepper[deviceNum]->currentPosition(); + encode32BitSignedInteger(position, data); + + Firmata.write(START_SYSEX); + Firmata.write(ACCELSTEPPER_DATA); + if (complete) { + Firmata.write(ACCELSTEPPER_MOVE_COMPLETE); + } else { + Firmata.write(ACCELSTEPPER_REPORT_POSITION); + } + Firmata.write(deviceNum); + Firmata.write(data[0]); + Firmata.write(data[1]); + Firmata.write(data[2]); + Firmata.write(data[3]); + Firmata.write(data[4]); + Firmata.write(END_SYSEX); + } +} + +void AccelStepperFirmata::reportGroupComplete(byte deviceNum) +{ + if (group[deviceNum]) { + Firmata.write(START_SYSEX); + Firmata.write(ACCELSTEPPER_DATA); + Firmata.write(MULTISTEPPER_MOVE_COMPLETE); + Firmata.write(deviceNum); + Firmata.write(END_SYSEX); + } +} + +/*============================================================================== + * SYSEX-BASED commands + *============================================================================*/ + +boolean AccelStepperFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == ACCELSTEPPER_DATA) { + byte stepCommand, deviceNum, interface, wireCount, stepType; + byte stepOrMotorPin1, directionOrMotorPin2; + byte motorPin3 = 0, motorPin4 = 0, enablePin = 0, invertPins = 0; + long numSteps; + + unsigned int index = 0; + + stepCommand = argv[index++]; + deviceNum = argv[index++]; + + if (deviceNum < MAX_ACCELSTEPPERS) { + + if (stepCommand == ACCELSTEPPER_CONFIG) { + interface = argv[index++]; + wireCount = (interface & 0x70) >> 4; // upper 3 bits are the wire count + stepType = (interface & 0x07) >> 1; // bits 4-6 are the step type + stepOrMotorPin1 = argv[index++]; // Step pin for driver or MotorPin1 + directionOrMotorPin2 = argv[index++]; // Direction pin for driver or motorPin2 + + if (Firmata.getPinMode(directionOrMotorPin2) == PIN_MODE_IGNORE + || Firmata.getPinMode(stepOrMotorPin1) == PIN_MODE_IGNORE) { + return false; + } + + Firmata.setPinMode(stepOrMotorPin1, PIN_MODE_STEPPER); + Firmata.setPinMode(directionOrMotorPin2, PIN_MODE_STEPPER); + + if (!stepper[deviceNum]) { + numSteppers++; + } + + if (wireCount >= 3) { + motorPin3 = argv[index++]; + if (Firmata.getPinMode(motorPin3) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(motorPin3, PIN_MODE_STEPPER); + } + + if (wireCount >= 4) { + motorPin4 = argv[index++]; + if (Firmata.getPinMode(motorPin4) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(motorPin4, PIN_MODE_STEPPER); + } + + // If we have an enable pin + if (interface & 0x01) { + enablePin = argv[index++]; + if (Firmata.getPinMode(enablePin) == PIN_MODE_IGNORE) + return false; + } + + // Instantiate our stepper + if (wireCount == 1) { + stepper[deviceNum] = new AccelStepper(AccelStepper::DRIVER, stepOrMotorPin1, directionOrMotorPin2); + } else if (wireCount == 2) { + stepper[deviceNum] = new AccelStepper(AccelStepper::FULL2WIRE, stepOrMotorPin1, directionOrMotorPin2); + } else if (wireCount == 3 && stepType == STEP_TYPE_WHOLE) { + stepper[deviceNum] = new AccelStepper(AccelStepper::FULL3WIRE, stepOrMotorPin1, directionOrMotorPin2, motorPin3); + } else if (wireCount == 3 && stepType == STEP_TYPE_HALF) { + stepper[deviceNum] = new AccelStepper(AccelStepper::HALF3WIRE, stepOrMotorPin1, directionOrMotorPin2, motorPin3); + } else if (wireCount == 4 && stepType == STEP_TYPE_WHOLE) { + stepper[deviceNum] = new AccelStepper(AccelStepper::FULL4WIRE, stepOrMotorPin1, directionOrMotorPin2, motorPin3, motorPin4, false); + } else if (wireCount == 4 && stepType == STEP_TYPE_HALF) { + stepper[deviceNum] = new AccelStepper(AccelStepper::HALF4WIRE, stepOrMotorPin1, directionOrMotorPin2, motorPin3, motorPin4, false); + } + + // If there is still another byte to read we must be inverting some pins + if (argc >= index) { + invertPins = argv[index]; + if (wireCount == 1) { + stepper[deviceNum]->setPinsInverted(invertPins & 0x01, invertPins >> 1 & 0x01, invertPins >> 4 & 0x01); + } else { + stepper[deviceNum]->setPinsInverted(invertPins & 0x01, invertPins >> 1 & 0x01, invertPins >> 2 & 0x01, invertPins >> 3 & 0x01, invertPins >> 4 & 0x01); + } + } + + if (interface & 0x01) { + stepper[deviceNum]->setEnablePin(enablePin); + } + + /* + Default to no acceleration. We set the acceleration value high enough that our speed is + reached on the first step of a movement. + More info about this hack in ACCELSTEPPER_SET_ACCELERATION. + + The lines where we are setting the speed twice are necessary because if the max speed doesn't change + from the default value then our time to next step does not get computed after raising the acceleration. + */ + stepper[deviceNum]->setMaxSpeed(2.0); + stepper[deviceNum]->setMaxSpeed(1.0); + stepper[deviceNum]->setAcceleration(MAX_ACCELERATION); + + isRunning[deviceNum] = false; + + } + + else if (stepCommand == ACCELSTEPPER_STEP) { + numSteps = decode32BitSignedInteger(argv[2], argv[3], argv[4], argv[5], argv[6]); + + if (stepper[deviceNum]) { + stepper[deviceNum]->move(numSteps); + isRunning[deviceNum] = true; + } + + } + + else if (stepCommand == ACCELSTEPPER_ZERO) { + if (stepper[deviceNum]) { + stepper[deviceNum]->setCurrentPosition(0); + } + } + + else if (stepCommand == ACCELSTEPPER_TO) { + if (stepper[deviceNum]) { + numSteps = decode32BitSignedInteger(argv[2], argv[3], argv[4], argv[5], argv[6]); + stepper[deviceNum]->moveTo(numSteps); + isRunning[deviceNum] = true; + } + } + + else if (stepCommand == ACCELSTEPPER_ENABLE) { + if (stepper[deviceNum]) { + if (argv[2] == 0x00) { + stepper[deviceNum]->disableOutputs(); + } else { + stepper[deviceNum]->enableOutputs(); + } + } + } + + else if (stepCommand == ACCELSTEPPER_STOP) { + if (stepper[deviceNum]) { + stepper[deviceNum]->stop(); + isRunning[deviceNum] = false; + reportPosition(deviceNum, true); + } + } + + else if (stepCommand == ACCELSTEPPER_REPORT_POSITION) { + if (stepper[deviceNum]) { + reportPosition(deviceNum, false); + } + } + + else if (stepCommand == ACCELSTEPPER_SET_ACCELERATION) { + float decodedAcceleration = decodeCustomFloat(argv[2], argv[3], argv[4], argv[5]); + + if (stepper[deviceNum]) { + /* + + All firmata instances of accelStepper have an acceleration value. If a user does not + want acceleration we just set the acceleration value high enough so + that the chosen speed will be realized by the first step of a movement. + This simplifies some of the logic in StepperFirmata and gives us more flexibility + should an alternative stepper library become available at a future date. + */ + if (decodedAcceleration == 0.0) { + stepper[deviceNum]->setAcceleration(MAX_ACCELERATION); + } else { + stepper[deviceNum]->setAcceleration(decodedAcceleration); + } + /* + + */ + } + + } + + else if (stepCommand == ACCELSTEPPER_SET_SPEED) { + // Sets the maxSpeed for accelStepper. We do not use setSpeed here because + // all instances of accelStepper that have been created by firmata are + // using acceleration. More info about this hack in ACCELSTEPPER_SET_ACCELERATION. + float speed = decodeCustomFloat(argv[2], argv[3], argv[4], argv[5]); + + if (stepper[deviceNum]) { + stepper[deviceNum]->setMaxSpeed(speed); + } + } + + else if (stepCommand == MULTISTEPPER_CONFIG) { + if (!group[deviceNum]) { + numGroups++; + group[deviceNum] = new MultiStepper(); + } + + for (byte i = index; i < argc; i++) { + byte stepperNumber = argv[i]; + + if (stepper[stepperNumber]) { + groupStepperCount[deviceNum]++; + group[deviceNum]->addStepper(*stepper[stepperNumber]); + } + } + + groupIsRunning[deviceNum] = false; + } + + else if (stepCommand == MULTISTEPPER_TO) { + groupIsRunning[deviceNum] = true; + long positions[groupStepperCount[deviceNum]]; + + for (byte i = 0, offset = 0; i < groupStepperCount[deviceNum]; i++) { + offset = index + (i * 5); + positions[i] = decode32BitSignedInteger(argv[offset], argv[offset + 1], argv[offset + 2], argv[offset + 3], argv[offset + 4]); + } + + group[deviceNum]->moveTo(positions); + } + + else if (stepCommand == MULTISTEPPER_STOP) { + groupIsRunning[deviceNum] = false; + reportGroupComplete(deviceNum); + } + } + return true; + + } + return false; +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void AccelStepperFirmata::reset() +{ + for (byte i = 0; i < MAX_ACCELSTEPPERS; i++) { + if (stepper[i]) { + free(stepper[i]); + stepper[i] = 0; + } + } + numSteppers = 0; + + for (byte i = 0; i < MAX_GROUPS; i++) { + if (group[i]) { + free(group[i]); + group[i] = 0; + } + } + numGroups = 0; +} + +/*============================================================================== + * Helpers + *============================================================================*/ + +float AccelStepperFirmata::decodeCustomFloat(byte arg1, byte arg2, byte arg3, byte arg4) +{ + long l4 = (long)arg4; + long significand = (long)arg1 | (long)arg2 << 7 | (long)arg3 << 14 | (l4 & 0x03) << 21; + float exponent = (float)(((l4 >> 2) & 0x0f) - 11); + bool sign = (bool)((l4 >> 6) & 0x01); + float result = (float)significand; + + if (sign) { + result *= -1; + } + + result = result * powf(10.0, exponent); + + return result; +} + +long AccelStepperFirmata::decode32BitSignedInteger(byte arg1, byte arg2, byte arg3, byte arg4, byte arg5) +{ + long result = (long)arg1 | (long)arg2 << 7 | (long)arg3 << 14 | (long)arg4 << 21 | (((long)arg5 << 28) & 0x07); + + if ((long)arg5 >> 3 == 0x01) { + result = result * -1; + } + + return result; +} + +void AccelStepperFirmata::encode32BitSignedInteger(long value, byte pdata[]) +{ + bool inv = false; + + if (value < 0) { + inv = true; + value = value * -1; + } + + pdata[0] = value & 0x7f; + pdata[1] = (value >> 7) & 0x7f; + pdata[2] = (value >> 14) & 0x7f; + pdata[3] = (value >> 21) & 0x7f; + pdata[4] = (value >> 28) & 0x7f; + + if (inv == true) { + pdata[4] = pdata[4] | 0x08; + } +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void AccelStepperFirmata::update() +{ + bool stepsLeft; + + if (numGroups > 0) { + // if one or more groups exist, update their position + for (byte i = 0; i < MAX_GROUPS; i++) { + if (group[i] && groupIsRunning[i] == true) { + stepsLeft = group[i]->run(); + + // send command to client application when stepping is complete + if (stepsLeft != true) { + groupIsRunning[i] = false; + reportGroupComplete(i); + } + + } + } + } + + if (numSteppers > 0) { + // if one or more stepper motors are used, update their position + for (byte i = 0; i < MAX_ACCELSTEPPERS; i++) { + if (stepper[i] && isRunning[i] == true) { + stepsLeft = stepper[i]->run(); + + // send command to client application when stepping is complete + if (!stepsLeft) { + isRunning[i] = false; + reportPosition(i, true); + } + + } + } + } + +} diff --git a/libraries/ConfigurableFirmata/src/AccelStepperFirmata.h b/libraries/ConfigurableFirmata/src/AccelStepperFirmata.h new file mode 100644 index 0000000..2f6ff2b --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AccelStepperFirmata.h @@ -0,0 +1,69 @@ +/* + 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-2017 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. +*/ + +#ifndef AccelStepperFirmata_h +#define AccelStepperFirmata_h + +#include +#include "utility/AccelStepper.h" +#include "utility/MultiStepper.h" +#include "FirmataFeature.h" + +#define MAX_ACCELSTEPPERS 10 // arbitrary value... may need to adjust +#define MAX_GROUPS 5 // arbitrary value... may need to adjust +#define MAX_ACCELERATION 1000000 // 1000^2 so that full speed is reached on first step +#define STEP_TYPE_WHOLE 0x00 +#define STEP_TYPE_HALF 0x01 +#define ACCELSTEPPER_CONFIG 0x00 +#define ACCELSTEPPER_ZERO 0x01 +#define ACCELSTEPPER_STEP 0x02 +#define ACCELSTEPPER_TO 0x03 +#define ACCELSTEPPER_ENABLE 0x04 +#define ACCELSTEPPER_STOP 0x05 +#define ACCELSTEPPER_REPORT_POSITION 0x06 +#define ACCELSTEPPER_LIMIT 0x07 +#define ACCELSTEPPER_SET_ACCELERATION 0x08 +#define ACCELSTEPPER_SET_SPEED 0x09 +#define ACCELSTEPPER_MOVE_COMPLETE 0x0a +#define MULTISTEPPER_CONFIG 0x20 +#define MULTISTEPPER_TO 0x21 +#define MULTISTEPPER_STOP 0x23 +#define MULTISTEPPER_MOVE_COMPLETE 0x24 + +class AccelStepperFirmata: public FirmataFeature +{ + public: + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + void reportPosition(byte deviceNum, bool complete); + void reportGroupComplete(byte deviceNum); + boolean handleSysex(byte command, byte argc, byte *argv); + float decodeCustomFloat(byte arg1, byte arg2, byte arg3, byte arg4); + long decode28BitUnsignedInteger(byte arg1, byte arg2, byte arg3, byte arg4); + long decode32BitSignedInteger(byte arg1, byte arg2, byte arg3, byte arg4, byte arg5); + void encode32BitSignedInteger(long value, byte pdata[]); + void update(); + void reset(); + private: + AccelStepper *stepper[MAX_ACCELSTEPPERS]; + MultiStepper *group[MAX_GROUPS]; + bool isRunning[MAX_ACCELSTEPPERS]; + bool groupIsRunning[MAX_GROUPS]; + byte numSteppers; + byte numGroups; + byte groupStepperCount[MAX_GROUPS]; +}; + +#endif /* AccelStepperFirmata_h */ diff --git a/libraries/ConfigurableFirmata/src/AnalogFirmata.cpp b/libraries/ConfigurableFirmata/src/AnalogFirmata.cpp new file mode 100644 index 0000000..f2f3fe7 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogFirmata.cpp @@ -0,0 +1,32 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#include +#include "AnalogFirmata.h" + +boolean handleAnalogFirmataSysex(byte command, byte argc, byte* argv) +{ + if (command == ANALOG_MAPPING_QUERY) { + Firmata.write(START_SYSEX); + Firmata.write(ANALOG_MAPPING_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + return true; + } + return false; +} diff --git a/libraries/ConfigurableFirmata/src/AnalogFirmata.h b/libraries/ConfigurableFirmata/src/AnalogFirmata.h new file mode 100644 index 0000000..69ae0fd --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogFirmata.h @@ -0,0 +1,24 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogFirmata_h +#define AnalogFirmata_h + +#include + +boolean handleAnalogFirmataSysex(byte command, byte argc, byte* argv); + +#endif diff --git a/libraries/ConfigurableFirmata/src/AnalogInputFirmata.cpp b/libraries/ConfigurableFirmata/src/AnalogInputFirmata.cpp new file mode 100644 index 0000000..4ac2377 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogInputFirmata.cpp @@ -0,0 +1,109 @@ +/* + AnalogFirmata.h - 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-2015 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: November 22nd, 2015 +*/ + +#include +#include "AnalogFirmata.h" +#include "AnalogInputFirmata.h" + +AnalogInputFirmata *AnalogInputFirmataInstance; + +void reportAnalogInputCallback(byte analogPin, int value) +{ + AnalogInputFirmataInstance->reportAnalog(analogPin, value); +} + +AnalogInputFirmata::AnalogInputFirmata() +{ + AnalogInputFirmataInstance = this; + analogInputsToReport = 0; + Firmata.attach(REPORT_ANALOG, reportAnalogInputCallback); +} + +// ----------------------------------------------------------------------------- +/* sets bits in a bit array (int) to toggle the reporting of the analogIns + */ +//void FirmataClass::setAnalogPinReporting(byte pin, byte state) { +//} +void AnalogInputFirmata::reportAnalog(byte analogPin, int value) +{ + 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 (!Firmata.isResetting()) { + // Send pin value immediately. This is helpful when connected via + // ethernet, wi-fi or bluetooth so pin states can be known upon + // reconnecting. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +boolean AnalogInputFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_ANALOG(pin)) { + if (mode == PIN_MODE_ANALOG) { + reportAnalog(PIN_TO_ANALOG(pin), 1); // turn on reporting + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver + } + return true; + } else { + reportAnalog(PIN_TO_ANALOG(pin), 0); // turn off reporting + } + } + return false; +} + +void AnalogInputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_ANALOG(pin)) { + Firmata.write(PIN_MODE_ANALOG); + Firmata.write(10); // 10 = 10-bit resolution + } +} + +boolean AnalogInputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + return handleAnalogFirmataSysex(command, argc, argv); +} + +void AnalogInputFirmata::reset() +{ + // by default, do not report any analog inputs + analogInputsToReport = 0; +} + +void AnalogInputFirmata::report() +{ + byte pin, analogPin; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } +} diff --git a/libraries/ConfigurableFirmata/src/AnalogInputFirmata.h b/libraries/ConfigurableFirmata/src/AnalogInputFirmata.h new file mode 100644 index 0000000..3c2f1bf --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogInputFirmata.h @@ -0,0 +1,42 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogInputFirmata_h +#define AnalogInputFirmata_h + +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +void reportAnalogInputCallback(byte analogPin, int value); + +class AnalogInputFirmata: public FirmataFeature +{ + public: + AnalogInputFirmata(); + void reportAnalog(byte analogPin, int value); + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + void report(); + + private: + /* analog inputs */ + int analogInputsToReport; // bitwise array to store pin reporting +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.cpp b/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.cpp new file mode 100644 index 0000000..27522cf --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.cpp @@ -0,0 +1,65 @@ +/* + AnalogFirmata.h - 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-2015 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 December 23rd, 2016 +*/ + +#include +#include "AnalogFirmata.h" +#include "AnalogOutputFirmata.h" + +AnalogOutputFirmata::AnalogOutputFirmata() +{ + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); +} + +void AnalogOutputFirmata::reset() +{ + +} + +boolean AnalogOutputFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_PWM && IS_PIN_PWM(pin)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + return true; + } + return false; +} + +void AnalogOutputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_PWM(pin)) { + Firmata.write(PIN_MODE_PWM); + Firmata.write(DEFAULT_PWM_RESOLUTION); + } +} + +boolean AnalogOutputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == 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); + return true; + } + return false; + } else { + return handleAnalogFirmataSysex(command, argc, argv); + } +} diff --git a/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.h b/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.h new file mode 100644 index 0000000..cf1761e --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogOutputFirmata.h @@ -0,0 +1,38 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogOutputFirmata_h +#define AnalogOutputFirmata_h + +#include +#include "FirmataFeature.h" + +// analogWriteCallback is defined in AnalogWrite.h but must also be declared here in order +// for AnalogOutputFirmata to compile +void analogWriteCallback(byte pin, int value); + +class AnalogOutputFirmata: public FirmataFeature +{ + public: + AnalogOutputFirmata(); + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + private: +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/AnalogWrite.h b/libraries/ConfigurableFirmata/src/AnalogWrite.h new file mode 100644 index 0000000..f8b4edd --- /dev/null +++ b/libraries/ConfigurableFirmata/src/AnalogWrite.h @@ -0,0 +1,51 @@ +/* + AnalogWrite.h - 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-2015 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: November 15th, 2015 +*/ + +#ifndef AnalogWrite_h +#define AnalogWrite_h + +#include + +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h + +void analogWriteCallback(byte pin, int value) +{ + if (pin < TOTAL_PINS) { + switch (Firmata.getPinMode(pin)) { +#ifdef ServoFirmata_h + case PIN_MODE_SERVO: + if (IS_PIN_SERVO(pin)) { + servoAnalogWrite(pin, value); + Firmata.setPinState(pin, value); + } + break; +#endif +#ifdef AnalogOutputFirmata_h + case PIN_MODE_PWM: + if (IS_PIN_PWM(pin)) { + analogWrite(PIN_TO_PWM(pin), value); + Firmata.setPinState(pin, value); + } + break; +#endif + } + } +} + +#endif +#endif diff --git a/libraries/ConfigurableFirmata/src/ConfigurableFirmata.cpp b/libraries/ConfigurableFirmata/src/ConfigurableFirmata.cpp new file mode 100644 index 0000000..b8d3556 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/ConfigurableFirmata.cpp @@ -0,0 +1,710 @@ +/* + ConfigurableFirmata.pp - ConfigurableFirmata library v2.10.0 - 2017-6-16 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-2017 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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "ConfigurableFirmata.h" +#include "HardwareSerial.h" + +extern "C" { +#include +#include +} + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +/** + * Split a 16-bit byte into two 7-bit values and write each value. + * @param value The 16-bit value to be split and written separately. + */ +void FirmataClass::sendValueAsTwo7bitBytes(int value) +{ + FirmataStream->write(value & B01111111); // LSB + FirmataStream->write(value >> 7 & B01111111); // MSB +} + +/** + * A helper method to write the beginning of a Sysex message transmission. + */ +void FirmataClass::startSysex(void) +{ + FirmataStream->write(START_SYSEX); +} + +/** + * A helper method to write the end of a Sysex message transmission. + */ +void FirmataClass::endSysex(void) +{ + FirmataStream->write(END_SYSEX); +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +/** + * The Firmata class. + * An instance named "Firmata" is created automatically for the user. + */ +FirmataClass::FirmataClass() +{ + firmwareVersionCount = 0; + firmwareVersionVector = 0; + blinkVersionDisabled = false; + systemReset(); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/** + * Initialize the default Serial transport at the default baud of 57600. + */ +void FirmataClass::begin(void) +{ + begin(57600); +} + +/** + * Initialize the default Serial transport and override the default baud. + * Sends the protocol version to the host application followed by the firmware version and name. + * blinkVersion is also called. To skip the call to blinkVersion, call Firmata.disableBlinkVersion() + * before calling Firmata.begin(baud). + * @param speed The baud to use. 57600 baud is the default value. + */ +void FirmataClass::begin(long speed) +{ + Serial.begin(speed); + FirmataStream = &Serial; + blinkVersion(); + printVersion(); // send the protocol version + printFirmwareVersion(); // send the firmware name and version +} + +/** + * Reassign the Firmata stream transport. + * @param s A reference to the Stream transport object. This can be any type of + * transport that implements the Stream interface. Some examples include Ethernet, WiFi + * and other UARTs on the board (Serial1, Serial2, etc). + */ +void FirmataClass::begin(Stream &s) +{ + FirmataStream = &s; + // do not call blinkVersion() here because some hardware such as the + // Ethernet shield use pin 13 + printVersion(); // send the protocol version + printFirmwareVersion(); // send the firmware name and version +} + +/** + * Send the Firmata protocol version to the Firmata host application. + */ +void FirmataClass::printVersion(void) +{ + FirmataStream->write(REPORT_VERSION); + FirmataStream->write(FIRMATA_PROTOCOL_MAJOR_VERSION); + FirmataStream->write(FIRMATA_PROTOCOL_MINOR_VERSION); +} + +/** + * Blink the Firmata protocol version to the onboard LEDs (if the board has an onboard LED). + * If VERSION_BLINK_PIN is not defined in Boards.h for a particular board, then this method + * does nothing. + * The first series of flashes indicates the firmware major version (2 flashes = 2). + * The second series of flashes indicates the firmware minor version (5 flashes = 5). + */ +void FirmataClass::blinkVersion(void) +{ +#if defined(VERSION_BLINK_PIN) + if (blinkVersionDisabled) return; + // flash the pin with the protocol version + pinMode(VERSION_BLINK_PIN, OUTPUT); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MAJOR_VERSION, 40, 210); + delay(250); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MINOR_VERSION, 40, 210); + delay(125); +#endif +} + +/** + * Provides a means to disable the version blink sequence on the onboard LED, trimming startup + * time by a couple of seconds. + * Call this before Firmata.begin(). It only applies when using the default Serial transport. + */ +void FirmataClass::disableBlinkVersion() +{ + blinkVersionDisabled = true; +} + +/** + * Sends the firmware name and version to the Firmata host application. The major and minor version + * numbers are the first 2 bytes in the message. The following bytes are the characters of the + * firmware name. + */ +void FirmataClass::printFirmwareVersion(void) +{ + byte i; + + if (firmwareVersionCount) { // make sure that the name has been set before reporting + startSysex(); + FirmataStream->write(REPORT_FIRMWARE); + FirmataStream->write(firmwareVersionVector[0]); // major version number + FirmataStream->write(firmwareVersionVector[1]); // minor version number + for (i = 2; i < firmwareVersionCount; ++i) { + sendValueAsTwo7bitBytes(firmwareVersionVector[i]); + } + endSysex(); + } +} + +/** + * Sets the name and version of the firmware. This is not the same version as the Firmata protocol + * (although at times the firmware version and protocol version may be the same number). + * @param name A pointer to the name char array + * @param major The major version number + * @param minor The minor version number + */ +void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte minor) +{ + const char *firmwareName; + const char *extension; + + // parse out ".cpp" and "applet/" that comes from using __FILE__ + extension = strstr(name, ".cpp"); + firmwareName = strrchr(name, '/'); + + if (!firmwareName) { + // windows + firmwareName = strrchr(name, '\\'); + } + if (!firmwareName) { + // user passed firmware name + firmwareName = name; + } else { + firmwareName ++; + } + + if (!extension) { + firmwareVersionCount = strlen(firmwareName) + 2; + } else { + firmwareVersionCount = extension - firmwareName + 2; + } + + // in case anyone calls setFirmwareNameAndVersion more than once + free(firmwareVersionVector); + + firmwareVersionVector = (byte *) malloc(firmwareVersionCount + 1); + firmwareVersionVector[firmwareVersionCount] = 0; + firmwareVersionVector[0] = major; + firmwareVersionVector[1] = minor; + strncpy((char *)firmwareVersionVector + 2, firmwareName, firmwareVersionCount - 2); +} + +//------------------------------------------------------------------------------ +// Input Stream Handling + +/** + * A wrapper for Stream::available() + * @return The number of bytes remaining in the input stream buffer. + */ +int FirmataClass::available(void) +{ + return FirmataStream->available(); +} + +/** + * Process incoming sysex messages. Handles REPORT_FIRMWARE and STRING_DATA internally. + * Calls callback function for STRING_DATA and all other sysex messages. + * @private + */ +void FirmataClass::processSysexMessage(void) +{ + switch (storedInputData[0]) { //first byte in buffer is command + case REPORT_FIRMWARE: + printFirmwareVersion(); + break; + case STRING_DATA: + if (currentStringCallback) { + byte bufferLength = (sysexBytesRead - 1) / 2; + byte i = 1; + byte j = 0; + while (j < bufferLength) { + // The string length will only be at most half the size of the + // stored input buffer so we can decode the string within the buffer. + storedInputData[j] = storedInputData[i]; + i++; + storedInputData[j] += (storedInputData[i] << 7); + i++; + j++; + } + // Make sure string is null terminated. This may be the case for data + // coming from client libraries in languages that don't null terminate + // strings. + if (storedInputData[j - 1] != '\0') { + storedInputData[j] = '\0'; + } + (*currentStringCallback)((char *)&storedInputData[0]); + } + break; + default: + if (currentSysexCallback) + (*currentSysexCallback)(storedInputData[0], sysexBytesRead - 1, storedInputData + 1); + } +} + + +/** + * Read a single int from the input stream. If the value is not = -1, pass it on to parse(byte) + */ +void FirmataClass::processInput(void) +{ + int inputData = FirmataStream->read(); // this is 'int' to handle -1 when no data + if (inputData != -1) { + parse(inputData); + } +} + +/** + * Parse data from the input stream. + * @param inputData A single byte to be added to the parser. + */ +void FirmataClass::parse(byte inputData) +{ + int command; + + // TODO make sure it handles -1 properly + + if (parsingSysex) { + if (inputData == END_SYSEX) { + //stop sysex byte + parsingSysex = false; + //fire off handler function + processSysexMessage(); + } else { + //normal data byte - add to buffer + storedInputData[sysexBytesRead] = inputData; + sysexBytesRead++; + } + } else if ( (waitForData > 0) && (inputData < 128) ) { + waitForData--; + storedInputData[waitForData] = inputData; + if ( (waitForData == 0) && executeMultiByteCommand ) { // got the whole message + switch (executeMultiByteCommand) { + case ANALOG_MESSAGE: + if (currentAnalogCallback) { + (*currentAnalogCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case DIGITAL_MESSAGE: + if (currentDigitalCallback) { + (*currentDigitalCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case SET_PIN_MODE: + setPinMode(storedInputData[1], storedInputData[0]); + break; + case SET_DIGITAL_PIN_VALUE: + if (currentPinValueCallback) + (*currentPinValueCallback)(storedInputData[1], storedInputData[0]); + break; + case REPORT_ANALOG: + if (currentReportAnalogCallback) + (*currentReportAnalogCallback)(multiByteChannel, storedInputData[0]); + break; + case REPORT_DIGITAL: + if (currentReportDigitalCallback) + (*currentReportDigitalCallback)(multiByteChannel, storedInputData[0]); + break; + } + executeMultiByteCommand = 0; + } + } else { + // remove channel info from command byte if less than 0xF0 + if (inputData < 0xF0) { + command = inputData & 0xF0; + multiByteChannel = inputData & 0x0F; + } else { + command = inputData; + // commands in the 0xF* range don't use channel data + } + switch (command) { + case ANALOG_MESSAGE: + case DIGITAL_MESSAGE: + case SET_PIN_MODE: + case SET_DIGITAL_PIN_VALUE: + waitForData = 2; // two data bytes needed + executeMultiByteCommand = command; + break; + case REPORT_ANALOG: + case REPORT_DIGITAL: + waitForData = 1; // one data byte needed + executeMultiByteCommand = command; + break; + case START_SYSEX: + parsingSysex = true; + sysexBytesRead = 0; + break; + case SYSTEM_RESET: + systemReset(); + break; + case REPORT_VERSION: + Firmata.printVersion(); + break; + } + } +} + +/** + * @return Returns true if the parser is actively parsing data. + */ +boolean FirmataClass::isParsingMessage(void) +{ + return (waitForData > 0 || parsingSysex); +} + +/** + * @return Returns true if the SYSTEM_RESET message is being executed + */ +boolean FirmataClass::isResetting(void) +{ + return resetting; +} + +//------------------------------------------------------------------------------ +// Output Stream Handling + +/** + * Send an analog message to the Firmata host application. The range of pins is limited to [0..15] + * when using the ANALOG_MESSAGE. The maximum value of the ANALOG_MESSAGE is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin to send the value of (limited to pins 0 - 15). + * @param value The value of the analog pin (0 - 1024 for 10-bit analog, 0 - 4096 for 12-bit, etc). + * The maximum value is 14-bits (16384). + */ +void FirmataClass::sendAnalog(byte pin, int value) +{ + // pin can only be 0-15, so chop higher bits + FirmataStream->write(ANALOG_MESSAGE | (pin & 0xF)); + sendValueAsTwo7bitBytes(value); +} + +/* (intentionally left out asterix here) + * STUB - NOT IMPLEMENTED + * Send a single digital pin value to the Firmata host application. + * @param pin The digital pin to send the value of. + * @param value The value of the pin. + */ +void FirmataClass::sendDigital(byte pin, int value) +{ + /* TODO add single pin digital messages to the protocol, this needs to + * track the last digital data sent so that it can be sure to change just + * one bit in the packet. This is complicated by the fact that the + * numbering of the pins will probably differ on Arduino, Wiring, and + * other boards. The DIGITAL_MESSAGE sends 14 bits at a time, but it is + * probably easier to send 8 bit ports for any board with more than 14 + * digital pins. + */ + + // TODO: the digital message should not be sent on the serial port every + // time sendDigital() is called. Instead, it should add it to an int + // which will be sent on a schedule. If a pin changes more than once + // before the digital message is sent on the serial port, it should send a + // digital message for each change. + + // if(value == 0) + // sendDigitalPortPair(); +} + + +/** + * Send an 8-bit port in a single digital message (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number to send. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + * @param portData The value of the port. The value of each pin in the port is represented by a bit. + */ +void FirmataClass::sendDigitalPort(byte portNumber, int portData) +{ + FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF)); + FirmataStream->write((byte)portData % 128); // Tx bits 0-6 + FirmataStream->write(portData >> 7); // Tx bits 7-13 +} + +/** + * Send a sysex message where all values after the command byte are packet as 2 7-bit bytes + * (this is not always the case so this function is not always used to send sysex messages). + * @param command The sysex command byte. + * @param bytec The number of data bytes in the message (excludes start, command and end bytes). + * @param bytev A pointer to the array of data bytes to send in the message. + */ +void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev) +{ + byte i; + startSysex(); + FirmataStream->write(command); + for (i = 0; i < bytec; i++) { + sendValueAsTwo7bitBytes(bytev[i]); + } + endSysex(); +} + +/** + * Send a string to the Firmata host application. + * @param command Must be STRING_DATA + * @param string A pointer to the char string + */ +void FirmataClass::sendString(byte command, const char *string) +{ + sendSysex(command, strlen(string), (byte *)string); +} + +/** + * Send a string to the Firmata host application. + * @param string A pointer to the char string + */ +void FirmataClass::sendString(const char *string) +{ + sendString(STRING_DATA, string); +} + +/** + * A wrapper for Stream::available(). + * Write a single byte to the output stream. + * @param c The byte to be written. + */ +void FirmataClass::write(byte c) +{ + FirmataStream->write(c); +} + + +/** + * Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE, + * DIGITAL_MESSAGE, REPORT_ANALOG, REPORT DIGITAL, SET_PIN_MODE and SET_DIGITAL_PIN_VALUE). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + */ +void FirmataClass::attach(byte command, callbackFunction newFunction) +{ + switch (command) { + case ANALOG_MESSAGE: currentAnalogCallback = newFunction; break; + case DIGITAL_MESSAGE: currentDigitalCallback = newFunction; break; + case REPORT_ANALOG: currentReportAnalogCallback = newFunction; break; + case REPORT_DIGITAL: currentReportDigitalCallback = newFunction; break; + case SET_PIN_MODE: currentPinModeCallback = newFunction; break; + case SET_DIGITAL_PIN_VALUE: currentPinValueCallback = newFunction; break; + } +} + +/** + * Attach a callback function for the SYSTEM_RESET command. + * @param command Must be set to SYSTEM_RESET or it will be ignored. + * @param newFunction A reference to the system reset callback function to attach. + */ +void FirmataClass::attach(byte command, systemResetCallbackFunction newFunction) +{ + switch (command) { + case SYSTEM_RESET: currentSystemResetCallback = newFunction; break; + } +} + +/** + * Attach a callback function for the STRING_DATA command. + * @param command Must be set to STRING_DATA or it will be ignored. + * @param newFunction A reference to the string callback function to attach. + */ +void FirmataClass::attach(byte command, stringCallbackFunction newFunction) +{ + switch (command) { + case STRING_DATA: currentStringCallback = newFunction; break; + } +} + +/** + * Attach a generic sysex callback function to sysex command. + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the sysex callback function to attach. + */ +void FirmataClass::attach(byte command, sysexCallbackFunction newFunction) +{ + currentSysexCallback = newFunction; +} + +/** + * Detach a callback function for a specified command (such as SYSTEM_RESET, STRING_DATA, + * ANALOG_MESSAGE, DIGITAL_MESSAGE, etc). + * @param command The ID of the command to detatch the callback function from. + */ +void FirmataClass::detach(byte command) +{ + switch (command) { + case SYSTEM_RESET: currentSystemResetCallback = NULL; break; + case STRING_DATA: currentStringCallback = NULL; break; + case START_SYSEX: currentSysexCallback = NULL; break; + default: + attach(command, (callbackFunction)NULL); + } +} + +/** + * Detach a callback function for a delayed task when using FirmataScheduler + * @see FirmataScheduler + * @param newFunction A reference to the delay task callback function to attach. + */ +void FirmataClass::attachDelayTask(delayTaskCallbackFunction newFunction) +{ + delayTaskCallback = newFunction; +} + +/** + * Call the delayTask callback function when using FirmataScheduler. Must first attach a callback + * using attachDelayTask. + * @see FirmataScheduler + * @param delay The amount of time to delay in milliseconds. + */ +void FirmataClass::delayTask(long delay) +{ + if (delayTaskCallback) { + (*delayTaskCallback)(delay); + } +} + +/** + * @param pin The pin to get the configuration of. + * @return The configuration of the specified pin. + */ +byte FirmataClass::getPinMode(byte pin) +{ + return pinConfig[pin]; +} + +/** + * Set the pin mode/configuration. The pin configuration (or mode) in Firmata represents the + * current function of the pin. Examples are digital input or output, analog input, pwm, i2c, + * serial (uart), etc. + * @param pin The pin to configure. + * @param config The configuration value for the specified pin. + */ +void FirmataClass::setPinMode(byte pin, byte config) +{ + if (pinConfig[pin] == PIN_MODE_IGNORE) + return; + pinState[pin] = 0; + pinConfig[pin] = config; + if (currentPinModeCallback) + (*currentPinModeCallback)(pin, config); +} + +/** + * @param pin The pin to get the state of. + * @return The state of the specified pin. + */ +int FirmataClass::getPinState(byte pin) +{ + return pinState[pin]; +} + +/** + * Set the pin state. The pin state of an output pin is the pin value. The state of an + * input pin is 0, unless the pin has it's internal pull up resistor enabled, then the value is 1. + * @param pin The pin to set the state of + * @param state Set the state of the specified pin + */ +void FirmataClass::setPinState(byte pin, int state) +{ + pinState[pin] = state; +} + + +// sysex callbacks +/* + * this is too complicated for analogReceive, but maybe for Sysex? + void FirmataClass::attachSysex(sysexFunction newFunction) + { + byte i; + byte tmpCount = analogReceiveFunctionCount; + analogReceiveFunction* tmpArray = analogReceiveFunctionArray; + analogReceiveFunctionCount++; + analogReceiveFunctionArray = (analogReceiveFunction*) calloc(analogReceiveFunctionCount, sizeof(analogReceiveFunction)); + for(i = 0; i < tmpCount; i++) { + analogReceiveFunctionArray[i] = tmpArray[i]; + } + analogReceiveFunctionArray[tmpCount] = newFunction; + free(tmpArray); + } +*/ + +//****************************************************************************** +//* Private Methods +//****************************************************************************** + +/** + * Resets the system state upon a SYSTEM_RESET message from the host software. + * @private + */ +void FirmataClass::systemReset(void) +{ + resetting = true; + byte i; + + waitForData = 0; // this flag says the next serial input will be data + executeMultiByteCommand = 0; // execute this after getting multi-byte data + multiByteChannel = 0; // channel data for multiByteCommands + + for (i = 0; i < MAX_DATA_BYTES; i++) { + storedInputData[i] = 0; + } + + parsingSysex = false; + sysexBytesRead = 0; + + if (currentSystemResetCallback) + (*currentSystemResetCallback)(); + + resetting = false; +} + +/** + * Flashing the pin for the version number + * @private + * @param pin The pin the LED is attached to. + * @param count The number of times to flash the LED. + * @param onInterval The number of milliseconds for the LED to be ON during each interval. + * @param offInterval The number of milliseconds for the LED to be OFF during each interval. + */ +void FirmataClass::strobeBlinkPin(byte pin, int count, int onInterval, int offInterval) +{ + byte i; + for (i = 0; i < count; i++) { + delay(offInterval); + digitalWrite(pin, HIGH); + delay(onInterval); + digitalWrite(pin, LOW); + } +} + +// make one instance for the user to use +FirmataClass Firmata; diff --git a/libraries/ConfigurableFirmata/src/ConfigurableFirmata.h b/libraries/ConfigurableFirmata/src/ConfigurableFirmata.h new file mode 100644 index 0000000..c5d093c --- /dev/null +++ b/libraries/ConfigurableFirmata/src/ConfigurableFirmata.h @@ -0,0 +1,241 @@ +/* + ConfigurableFirmata.h - ConfigurableFirmata library v2.10.0 - 2017-6-16 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-2017 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. +*/ + +#ifndef Configurable_Firmata_h +#define Configurable_Firmata_h + +#include "utility/Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */ + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. + * Query using the REPORT_VERSION message. + */ +#define FIRMATA_PROTOCOL_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_PROTOCOL_MINOR_VERSION 6 // for backwards compatible changes +#define FIRMATA_PROTOCOL_BUGFIX_VERSION 0 // for bugfix releases + +/* + * Version numbers for the Firmata library. + * ConfigurableFirmata 2.10.0 implements version 2.6.0 of the Firmata protocol. + * The firmware version will not always equal the protocol version going forward. + * Query using the REPORT_FIRMWARE message. + */ +#define FIRMATA_FIRMWARE_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_FIRMWARE_MINOR_VERSION 10 // for backwards compatible changes +#define FIRMATA_FIRMWARE_BUGFIX_VERSION 0 // for bugfix releases + +// DEPRECATED as of ConfigurableFirmata v2.8.1. +// Use FIRMATA_PROTOCOL_[MAJOR|MINOR|BUGFIX]_VERSION instead. +#define FIRMATA_MAJOR_VERSION 2 +#define FIRMATA_MINOR_VERSION 6 +#define FIRMATA_BUGFIX_VERSION 0 +// DEPRECATED as of ConfigurableFirmata v2.8.1. +//Use FIRMATA_FIRMWARE_[MAJOR|MINOR|BUGFIX]_VERSION instead. +#define FIRMWARE_MAJOR_VERSION 2 +#define FIRMWARE_MINOR_VERSION 10 +#define FIRMWARE_BUGFIX_VERSION 0 + +#define MAX_DATA_BYTES 64 // max number of data bytes in incoming messages + +// Arduino 101 also defines SET_PIN_MODE as a macro in scss_registers.h +#ifdef SET_PIN_MODE +#undef SET_PIN_MODE +#endif + +// message command bytes (128-255/0x80-0xFF) +#define DIGITAL_MESSAGE 0x90 // send data for a digital pin +#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) +#define REPORT_ANALOG 0xC0 // enable analog input by pin # +#define REPORT_DIGITAL 0xD0 // enable digital input by port pair +// +#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc +#define SET_DIGITAL_PIN_VALUE 0xF5 // set value of an individual digital pin +// +#define REPORT_VERSION 0xF9 // report protocol version +#define SYSTEM_RESET 0xFF // reset from MIDI +// +#define START_SYSEX 0xF0 // start a MIDI Sysex message +#define END_SYSEX 0xF7 // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ +#define SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards +#define ENCODER_DATA 0x61 // reply with encoders current positions +#define ACCELSTEPPER_DATA 0x62 // control a stepper motor +#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq +#define STRING_DATA 0x71 // a string message with 14-bits per char +#define STEPPER_DATA 0x72 // control a stepper motor +#define ONEWIRE_DATA 0x73 // send an OneWire read/write/reset/select/skip/search request +#define SHIFT_DATA 0x75 // a bitstream to/from a shift register +#define I2C_REQUEST 0x76 // send an I2C read/write request +#define I2C_REPLY 0x77 // a reply to an I2C read request +#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins +#define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin +#define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value +#define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value +#define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins +#define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution +#define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers +#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info +#define REPORT_FIRMWARE 0x79 // report name and version of the firmware +#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop +#define SCHEDULER_DATA 0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler +#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages +#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages + +// these are DEPRECATED to make the naming more consistent +#define FIRMATA_STRING 0x71 // same as STRING_DATA +#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST +#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY +#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL + +// pin modes +//#define INPUT 0x00 // defined in Arduino.h +//#define OUTPUT 0x01 // defined in Arduino.h +#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode +#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode +#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode +#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode +#define PIN_MODE_I2C 0x06 // pin included in I2C setup +#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire +#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor +#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders +#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication +#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin +#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse +#define TOTAL_PIN_MODES 13 +// DEPRECATED as of Firmata v2.5 +#define ANALOG 0x02 // same as PIN_MODE_ANALOG +#define PWM 0x03 // same as PIN_MODE_PWM +#define SERVO 0x04 // same as PIN_MODE_SERVO +#define SHIFT 0x05 // same as PIN_MODE_SHIFT +#define I2C 0x06 // same as PIN_MODE_I2C +#define ONEWIRE 0x07 // same as PIN_MODE_ONEWIRE +#define STEPPER 0x08 // same as PIN_MODE_STEPPER +#define ENCODER 0x09 // same as PIN_MODE_ENCODER +#define IGNORE 0x7F // same as PIN_MODE_IGNORE + +extern "C" { + // callback function types + typedef void (*callbackFunction)(byte, int); + typedef void (*systemResetCallbackFunction)(void); + typedef void (*stringCallbackFunction)(char *); + typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv); + typedef void (*delayTaskCallbackFunction)(long delay); +} + + +// TODO make it a subclass of a generic Serial/Stream base class +class FirmataClass +{ + public: + FirmataClass(); + /* Arduino constructors */ + void begin(); + void begin(long); + void begin(Stream &s); + /* querying functions */ + void printVersion(void); + void blinkVersion(void); + void printFirmwareVersion(void); + //void setFirmwareVersion(byte major, byte minor); // see macro below + void setFirmwareNameAndVersion(const char *name, byte major, byte minor); + void disableBlinkVersion(); + /* serial receive handling */ + int available(void); + void processInput(void); + void parse(unsigned char value); + boolean isParsingMessage(void); + boolean isResetting(void); + /* serial send handling */ + void sendAnalog(byte pin, int value); + void sendDigital(byte pin, int value); // TODO implement this + void sendDigitalPort(byte portNumber, int portData); + void sendString(const char *string); + void sendString(byte command, const char *string); + void sendSysex(byte command, byte bytec, byte *bytev); + void write(byte c); + /* attach & detach callback functions to messages */ + void attach(byte command, callbackFunction newFunction); + void attach(byte command, systemResetCallbackFunction newFunction); + void attach(byte command, stringCallbackFunction newFunction); + void attach(byte command, sysexCallbackFunction newFunction); + void detach(byte command); + /* delegate to Scheduler (if any) */ + void attachDelayTask(delayTaskCallbackFunction newFunction); + void delayTask(long delay); + /* access pin config */ + byte getPinMode(byte pin); + void setPinMode(byte pin, byte config); + /* access pin state */ + int getPinState(byte pin); + void setPinState(byte pin, int state); + + /* utility methods */ + void sendValueAsTwo7bitBytes(int value); + void startSysex(void); + void endSysex(void); + + private: + Stream *FirmataStream; + /* firmware name and version */ + byte firmwareVersionCount; + byte *firmwareVersionVector; + /* input message handling */ + byte waitForData; // this flag says the next serial input will be data + byte executeMultiByteCommand; // execute this after getting multi-byte data + byte multiByteChannel; // channel data for multiByteCommands + byte storedInputData[MAX_DATA_BYTES]; // multi-byte data + /* sysex */ + boolean parsingSysex; + int sysexBytesRead; + /* pins configuration */ + byte pinConfig[TOTAL_PINS]; // configuration of every pin + int pinState[TOTAL_PINS]; // any value that has been written + + boolean resetting; + + /* callback functions */ + callbackFunction currentAnalogCallback; + callbackFunction currentDigitalCallback; + callbackFunction currentReportAnalogCallback; + callbackFunction currentReportDigitalCallback; + callbackFunction currentPinModeCallback; + callbackFunction currentPinValueCallback; + systemResetCallbackFunction currentSystemResetCallback; + stringCallbackFunction currentStringCallback; + sysexCallbackFunction currentSysexCallback; + delayTaskCallbackFunction delayTaskCallback; + + boolean blinkVersionDisabled; + + /* private methods ------------------------------ */ + void processSysexMessage(void); + void systemReset(void); + void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval); +}; + +extern FirmataClass Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the + * firmware name. It needs to be a macro so that __FILE__ is included in the + * firmware source file rather than the library source file. + */ +#define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y) + +#endif /* Configurable_Firmata_h */ diff --git a/libraries/ConfigurableFirmata/src/DigitalInputFirmata.cpp b/libraries/ConfigurableFirmata/src/DigitalInputFirmata.cpp new file mode 100644 index 0000000..565066d --- /dev/null +++ b/libraries/ConfigurableFirmata/src/DigitalInputFirmata.cpp @@ -0,0 +1,127 @@ +/* + DigitalInputFirmata.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-2015 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: November 15th, 2015 +*/ + +#include +#include "DigitalInputFirmata.h" + +DigitalInputFirmata *DigitalInputFirmataInstance; + +void reportDigitalInputCallback(byte port, int value) +{ + DigitalInputFirmataInstance->reportDigital(port, value); +} + +DigitalInputFirmata::DigitalInputFirmata() +{ + DigitalInputFirmataInstance = this; + Firmata.attach(REPORT_DIGITAL, reportDigitalInputCallback); +} + +boolean DigitalInputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + return false; +} + +void DigitalInputFirmata::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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void DigitalInputFirmata::report(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +void DigitalInputFirmata::reportDigital(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)value; + 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 +} + +boolean DigitalInputFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_DIGITAL(pin)) { + if (mode == INPUT || mode == PIN_MODE_PULLUP) { + portConfigInputs[pin / 8] |= (1 << (pin & 7)); + if (mode == INPUT) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); + } else { + pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP); + Firmata.setPinState(pin, 1); + } + return true; + } else { + portConfigInputs[pin / 8] &= ~(1 << (pin & 7)); + } + } + return false; +} + +void DigitalInputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write((byte)INPUT); + Firmata.write(1); + Firmata.write((byte)PIN_MODE_PULLUP); + Firmata.write(1); + } +} + +void DigitalInputFirmata::reset() +{ + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } +} diff --git a/libraries/ConfigurableFirmata/src/DigitalInputFirmata.h b/libraries/ConfigurableFirmata/src/DigitalInputFirmata.h new file mode 100644 index 0000000..2c896b1 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/DigitalInputFirmata.h @@ -0,0 +1,46 @@ +/* + DigitalInputFirmata.h - 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-2015 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. +*/ + +#ifndef DigitalInputFirmata_h +#define DigitalInputFirmata_h + +#include +#include "FirmataFeature.h" + +void reportDigitalInputCallback(byte port, int value); + +class DigitalInputFirmata: public FirmataFeature +{ + public: + DigitalInputFirmata(); + void reportDigital(byte port, int value); + void report(void); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + boolean handlePinMode(byte pin, int mode); + void reset(); + + private: + /* digital input ports */ + byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence + byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + + /* pins configuration */ + byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + void outputPort(byte portNumber, byte portValue, byte forceSend); +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.cpp b/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.cpp new file mode 100644 index 0000000..a4448f0 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.cpp @@ -0,0 +1,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 +#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) == 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) == 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) { + 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 == 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)OUTPUT); + Firmata.write(1); + } +} diff --git a/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.h b/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.h new file mode 100644 index 0000000..dcd7979 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/DigitalOutputFirmata.h @@ -0,0 +1,40 @@ +/* + DigitalOutputFirmata.h - 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-2015 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: November 15th, 2015 +*/ + +#ifndef DigitalOutputFirmata_h +#define DigitalOutputFirmata_h + +#include +#include "FirmataFeature.h" + +void digitalOutputWriteCallback(byte port, int value); +void handleSetPinValueCallback(byte pin, int value); + +class DigitalOutputFirmata: public FirmataFeature +{ + public: + DigitalOutputFirmata(); + void digitalWritePort(byte port, int value); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + boolean handlePinMode(byte pin, int mode); + void reset(); + private: +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/Encoder7Bit.cpp b/libraries/ConfigurableFirmata/src/Encoder7Bit.cpp new file mode 100644 index 0000000..c11c33c --- /dev/null +++ b/libraries/ConfigurableFirmata/src/Encoder7Bit.cpp @@ -0,0 +1,64 @@ +/* + Encoder7Bit.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#include "Encoder7Bit.h" +#include + +Encoder7BitClass::Encoder7BitClass() +{ + previous = 0; + shift = 0; +} + +void Encoder7BitClass::startBinaryWrite() +{ + shift = 0; +} + +void Encoder7BitClass::endBinaryWrite() +{ + if (shift > 0) { + Firmata.write(previous); + } +} + +void Encoder7BitClass::writeBinary(byte data) +{ + if (shift == 0) { + Firmata.write(data & 0x7f); + shift++; + previous = data >> 7; + } + else { + Firmata.write(((data << shift) & 0x7f) | previous); + if (shift == 6) { + Firmata.write(data >> 1); + shift = 0; + } + else { + shift++; + previous = data >> (8 - shift); + } + } +} + +void Encoder7BitClass::readBinary(int outBytes, byte *inData, byte *outData) +{ + for (int i = 0; i < outBytes; i++) { + int j = i << 3; + int pos = j / 7; + byte shift = j % 7; + outData[i] = (inData[pos] >> shift) | ((inData[pos + 1] << (7 - shift)) & 0xFF); + } +} + +Encoder7BitClass Encoder7Bit; diff --git a/libraries/ConfigurableFirmata/src/Encoder7Bit.h b/libraries/ConfigurableFirmata/src/Encoder7Bit.h new file mode 100644 index 0000000..e3538b2 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/Encoder7Bit.h @@ -0,0 +1,35 @@ +/* + Encoder7Bit.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#ifndef Encoder7Bit_h +#define Encoder7Bit_h +#include + +#define num7BitOutbytes(a)(((a)*7)>>3) + +class Encoder7BitClass +{ + public: + Encoder7BitClass(); + void startBinaryWrite(); + void endBinaryWrite(); + void writeBinary(byte data); + void readBinary(int outBytes, byte *inData, byte *outData); + + private: + byte previous; + int shift; +}; + +extern Encoder7BitClass Encoder7Bit; + +#endif diff --git a/libraries/ConfigurableFirmata/src/EthernetClientStream.cpp b/libraries/ConfigurableFirmata/src/EthernetClientStream.cpp new file mode 100644 index 0000000..f4f8968 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/EthernetClientStream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in EthernetClientStream.h to avoid linker issues. + */ diff --git a/libraries/ConfigurableFirmata/src/EthernetClientStream.h b/libraries/ConfigurableFirmata/src/EthernetClientStream.h new file mode 100644 index 0000000..f42c729 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/EthernetClientStream.h @@ -0,0 +1,141 @@ +/* + EthernetClientStream.h + An Arduino-Stream that wraps an instance of Client reconnecting to + the remote-ip in a transparent way. A disconnected client may be + recognized by the returnvalues -1 from calls to peek or read and + a 0 from calls to write. + + Copyright (C) 2013 Norbert Truchsess. 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 June 18th, 2016 + */ + +#ifndef ETHERNETCLIENTSTREAM_H +#define ETHERNETCLIENTSTREAM_H + +#include +#include + +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +#define MILLIS_RECONNECT 5000 + +class EthernetClientStream : public Stream +{ + public: + EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port); + int available(); + int read(); + int peek(); + void flush(); + size_t write(uint8_t); + void maintain(IPAddress localip); + + private: + Client &client; + IPAddress localip; + IPAddress ip; + const char* host; + uint16_t port; + bool connected; + uint32_t time_connect; + bool maintain(); + void stop(); +}; + + +/* + * EthernetClientStream.cpp + * Copied here as a hack to linker issues with 3rd party board packages that don't properly + * implement the Arduino network APIs. + */ +EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port) + : client(client), + localip(localip), + ip(ip), + host(host), + port(port), + connected(false) +{ +} + +int +EthernetClientStream::available() +{ + return maintain() ? client.available() : 0; +} + +int +EthernetClientStream::read() +{ + return maintain() ? client.read() : -1; +} + +int +EthernetClientStream::peek() +{ + return maintain() ? client.peek() : -1; +} + +void EthernetClientStream::flush() +{ + if (maintain()) + client.flush(); +} + +size_t +EthernetClientStream::write(uint8_t c) +{ + return maintain() ? client.write(c) : 0; +} + +void +EthernetClientStream::maintain(IPAddress localip) +{ + // ensure the local IP is updated in the case that it is changed by the DHCP server + if (this->localip != localip) { + this->localip = localip; + if (connected) + stop(); + } +} + +void +EthernetClientStream::stop() +{ + client.stop(); + connected = false; + time_connect = millis(); +} + +bool +EthernetClientStream::maintain() +{ + if (client && client.connected()) + return true; + + if (connected) { + stop(); + } + // if the client is disconnected, attempt to reconnect every 5 seconds + else if (millis() - time_connect >= MILLIS_RECONNECT) { + connected = host ? client.connect(host, port) : client.connect(ip, port); + if (!connected) { + time_connect = millis(); + DEBUG_PRINTLN("connection failed. attempting to reconnect..."); + } else { + DEBUG_PRINTLN("connected"); + } + } + return connected; +} + +#endif /* ETHERNETCLIENTSTREAM_H */ diff --git a/libraries/ConfigurableFirmata/src/FirmataExt.cpp b/libraries/ConfigurableFirmata/src/FirmataExt.cpp new file mode 100644 index 0000000..53bcb0c --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataExt.cpp @@ -0,0 +1,117 @@ +/* + FirmataExt.h - 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-2015 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: November 15th, 2015 +*/ + +#include +#include "FirmataExt.h" + +FirmataExt *FirmataExtInstance; + +void handleSetPinModeCallback(byte pin, int mode) +{ + if (!FirmataExtInstance->handlePinMode(pin, mode) && mode != PIN_MODE_IGNORE) { + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } +} + +void handleSysexCallback(byte command, byte argc, byte* argv) +{ + if (!FirmataExtInstance->handleSysex(command, argc, argv)) { + Firmata.sendString("Unhandled sysex command"); + } +} + +FirmataExt::FirmataExt() +{ + FirmataExtInstance = this; + Firmata.attach(SET_PIN_MODE, handleSetPinModeCallback); + Firmata.attach((byte)START_SYSEX, handleSysexCallback); + numFeatures = 0; +} + +void FirmataExt::handleCapability(byte pin) +{ + +} + +boolean FirmataExt::handlePinMode(byte pin, int mode) +{ + boolean known = false; + for (byte i = 0; i < numFeatures; i++) { + known |= features[i]->handlePinMode(pin, mode); + } + return known; +} + +boolean FirmataExt::handleSysex(byte command, byte argc, byte* argv) +{ + switch (command) { + + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin = argv[0]; + if (pin < TOTAL_PINS) { + Firmata.write(START_SYSEX); + Firmata.write(PIN_STATE_RESPONSE); + Firmata.write(pin); + Firmata.write(Firmata.getPinMode(pin)); + int pinState = Firmata.getPinState(pin); + Firmata.write((byte)pinState & 0x7F); + if (pinState & 0xFF80) Firmata.write((byte)(pinState >> 7) & 0x7F); + if (pinState & 0xC000) Firmata.write((byte)(pinState >> 14) & 0x7F); + Firmata.write(END_SYSEX); + return true; + } + } + break; + case CAPABILITY_QUERY: + Firmata.write(START_SYSEX); + Firmata.write(CAPABILITY_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + if (Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + for (byte i = 0; i < numFeatures; i++) { + features[i]->handleCapability(pin); + } + } + Firmata.write(127); + } + Firmata.write(END_SYSEX); + return true; + default: + for (byte i = 0; i < numFeatures; i++) { + if (features[i]->handleSysex(command, argc, argv)) { + return true; + } + } + break; + } + return false; +} + +void FirmataExt::addFeature(FirmataFeature &capability) +{ + if (numFeatures < MAX_FEATURES) { + features[numFeatures++] = &capability; + } +} + +void FirmataExt::reset() +{ + for (byte i = 0; i < numFeatures; i++) { + features[i]->reset(); + } +} diff --git a/libraries/ConfigurableFirmata/src/FirmataExt.h b/libraries/ConfigurableFirmata/src/FirmataExt.h new file mode 100644 index 0000000..04427e6 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataExt.h @@ -0,0 +1,44 @@ +/* + FirmataExt.h - 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-2015 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. +*/ + +#ifndef FirmataExt_h +#define FirmataExt_h + +#include +#include "FirmataFeature.h" + +#define MAX_FEATURES TOTAL_PIN_MODES + 1 + +void handleSetPinModeCallback(byte pin, int mode); + +void handleSysexCallback(byte command, byte argc, byte* argv); + +class FirmataExt: public FirmataFeature +{ + public: + FirmataExt(); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void addFeature(FirmataFeature &capability); + void reset(); + + private: + FirmataFeature *features[MAX_FEATURES]; + byte numFeatures; +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/FirmataFeature.h b/libraries/ConfigurableFirmata/src/FirmataFeature.h new file mode 100644 index 0000000..e83653b --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataFeature.h @@ -0,0 +1,31 @@ +/* + FirmataExt.h - 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-2015 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. +*/ + +#ifndef FirmataFeature_h +#define FirmataFeature_h + +#include + +class FirmataFeature +{ + public: + virtual void handleCapability(byte pin) = 0; + virtual boolean handlePinMode(byte pin, int mode) = 0; + virtual boolean handleSysex(byte command, byte argc, byte* argv) = 0; + virtual void reset() = 0; +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/FirmataReporting.cpp b/libraries/ConfigurableFirmata/src/FirmataReporting.cpp new file mode 100644 index 0000000..b1705f7 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataReporting.cpp @@ -0,0 +1,66 @@ +/* + FirmataReporting.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. +*/ + +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +void FirmataReporting::setSamplingInterval(int interval) +{ + samplingInterval = interval; +} + +void FirmataReporting::handleCapability(byte pin) +{ + +} + +boolean FirmataReporting::handlePinMode(byte pin, int mode) +{ + return false; +} + +boolean FirmataReporting::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SAMPLING_INTERVAL) { + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + return true; + } + } + return false; +} + +boolean FirmataReporting::elapsed() +{ + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + if (currentMillis - previousMillis > samplingInterval) + previousMillis = currentMillis - samplingInterval; + return true; + } + return false; +} + +void FirmataReporting::reset() +{ + previousMillis = millis(); + samplingInterval = 19; +} diff --git a/libraries/ConfigurableFirmata/src/FirmataReporting.h b/libraries/ConfigurableFirmata/src/FirmataReporting.h new file mode 100644 index 0000000..88105e6 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataReporting.h @@ -0,0 +1,43 @@ +/* + FirmataReporting.h - 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: January 23rd, 2016 +*/ + +#ifndef FirmataReporting_h +#define FirmataReporting_h + +#include +#include "FirmataFeature.h" + +#define MINIMUM_SAMPLING_INTERVAL 1 + +class FirmataReporting: public FirmataFeature +{ + public: + void setSamplingInterval(int interval); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); //empty method + boolean handleSysex(byte command, byte argc, byte* argv); + boolean elapsed(); + void reset(); + private: + /* timer variables */ + unsigned long currentMillis; // store the current value from millis() + unsigned long previousMillis; // for comparison with currentMillis + unsigned int samplingInterval; // how often to run the main loop (in ms) +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/FirmataScheduler.cpp b/libraries/ConfigurableFirmata/src/FirmataScheduler.cpp new file mode 100644 index 0000000..32a9bfb --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataScheduler.cpp @@ -0,0 +1,300 @@ +/* + FirmataScheduler.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#include +#include "FirmataFeature.h" +#include "Encoder7Bit.h" +#include "FirmataScheduler.h" +#include "FirmataExt.h" + +FirmataScheduler *FirmataSchedulerInstance; + +void delayTaskCallback(long delay) +{ + FirmataSchedulerInstance->delayTask(delay); +} + +FirmataScheduler::FirmataScheduler() +{ + FirmataSchedulerInstance = this; + tasks = NULL; + running = NULL; + Firmata.attachDelayTask(delayTaskCallback); +} + +void FirmataScheduler::handleCapability(byte pin) +{ + +} + +boolean FirmataScheduler::handlePinMode(byte pin, int mode) +{ + return false; +} + +boolean FirmataScheduler::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SCHEDULER_DATA) { + if (argc > 0) { + switch (argv[0]) { + case CREATE_FIRMATA_TASK: + { + if (argc == 4) { + createTask(argv[1], argv[2] | argv[3] << 7); + } + break; + } + case DELETE_FIRMATA_TASK: + { + if (argc == 2) { + deleteTask(argv[1]); + } + break; + } + case ADD_TO_FIRMATA_TASK: + { + if (argc > 2) { + int len = num7BitOutbytes(argc - 2); + Encoder7Bit.readBinary(len, argv + 2, argv + 2); //decode inplace + addToTask(argv[1], len, argv + 2); //addToTask copies data... + } + break; + } + case DELAY_FIRMATA_TASK: + { + if (argc == 6) { + argv++; + Encoder7Bit.readBinary(4, argv, argv); //decode inplace + delayTask(*(long*)((byte*)argv)); + } + break; + } + case SCHEDULE_FIRMATA_TASK: + { + if (argc == 7) { //one byte taskid, 5 bytes to encode 4 bytes of long + Encoder7Bit.readBinary(4, argv + 2, argv + 2); //decode inplace + schedule(argv[1], *(long*)((byte*)argv + 2)); //argv[1] | argv[2]<<8 | argv[3]<<16 | argv[4]<<24 + } + break; + } + case QUERY_ALL_FIRMATA_TASKS: + { + queryAllTasks(); + break; + } + case QUERY_FIRMATA_TASK: + { + if (argc == 2) { + queryTask(argv[1]); + } + break; + } + case RESET_FIRMATA_TASKS: + { + reset(); + } + } + } + return true; + } + return false; +}; + +void FirmataScheduler::createTask(byte id, int len) +{ + firmata_task *existing = findTask(id); + if (existing) { + reportTask(id, existing, true); + } + else { + firmata_task *newTask = (firmata_task*)malloc(sizeof(firmata_task) + len); + newTask->id = id; + newTask->time_ms = 0; + newTask->len = len; + newTask->nextTask = tasks; + newTask->pos = 0; + tasks = newTask; + } +}; + +void FirmataScheduler::deleteTask(byte id) +{ + firmata_task *current = tasks; + firmata_task *previous = NULL; + while (current) { + if (current->id == id) { + if (previous) { + previous->nextTask = current->nextTask; + } + else { + tasks = current->nextTask; + } + free (current); + return; + } + else { + previous = current; + current = current->nextTask; + } + } +}; + +void FirmataScheduler::addToTask(byte id, int additionalBytes, byte *message) +{ + firmata_task *existing = findTask(id); + if (existing) { //task exists and has not been fully loaded yet + if (existing->pos + additionalBytes <= existing->len) { + for (int i = 0; i < additionalBytes; i++) { + existing->messages[existing->pos++] = message[i]; + } + } + } + else { + reportTask(id, NULL, true); + } +}; + +void FirmataScheduler::schedule(byte id, long delay_ms) +{ + firmata_task *existing = findTask(id); + if (existing) { + existing->pos = 0; + existing->time_ms = millis() + delay_ms; + } + else { + reportTask(id, NULL, true); + } +}; + +void FirmataScheduler::delayTask(long delay_ms) +{ + if (running) { + long now = millis(); + running->time_ms += delay_ms; + if (running->time_ms < now) { //if delay time allready passed by schedule to 'now'. + running->time_ms = now; + } + } +} + +void FirmataScheduler::queryAllTasks() +{ + Firmata.write(START_SYSEX); + Firmata.write(SCHEDULER_DATA); + Firmata.write(QUERY_ALL_TASKS_REPLY); + firmata_task *task = tasks; + while (task) { + Firmata.write(task->id); + task = task->nextTask; + } + Firmata.write(END_SYSEX); +}; + +void FirmataScheduler::queryTask(byte id) +{ + firmata_task *task = findTask(id); + reportTask(id, task, false); +} + +void FirmataScheduler::reportTask(byte id, firmata_task *task, boolean error) +{ + Firmata.write(START_SYSEX); + Firmata.write(SCHEDULER_DATA); + if (error) { + Firmata.write(ERROR_TASK_REPLY); + } else { + Firmata.write(QUERY_TASK_REPLY); + } + Firmata.write(id); + if (task) { + Encoder7Bit.startBinaryWrite(); + for (int i = 3; i < firmata_task_len(task); i++) { + Encoder7Bit.writeBinary(((byte *)task)[i]); //don't write first 3 bytes (firmata_task*, byte); makes use of AVR byteorder (LSB first) + } + Encoder7Bit.endBinaryWrite(); + } + Firmata.write(END_SYSEX); +}; + +void FirmataScheduler::runTasks() +{ + if (tasks) { + long now = millis(); + firmata_task *current = tasks; + firmata_task *previous = NULL; + while (current) { + if (current->time_ms > 0 && current->time_ms < now) { // TODO handle overflow + if (execute(current)) { + previous = current; + current = current->nextTask; + } + else { + if (previous) { + previous->nextTask = current->nextTask; + free(current); + current = previous->nextTask; + } + else { + tasks = current->nextTask; + free(current); + current = tasks; + } + } + } + else { + current = current->nextTask; + } + } + } +}; + +void FirmataScheduler::reset() +{ + while (tasks) { + firmata_task *nextTask = tasks->nextTask; + free(tasks); + tasks = nextTask; + } +}; + +//private +boolean FirmataScheduler::execute(firmata_task *task) +{ + long start = task->time_ms; + int pos = task->pos; + int len = task->len; + byte *messages = task->messages; + running = task; + while (pos < len) { + Firmata.parse(messages[pos++]); + if (start != task->time_ms) { // return true if task got rescheduled during run. + task->pos = ( pos == len ? 0 : pos ); // last message executed? -> start over next time + running = NULL; + return true; + } + }; + running = NULL; + return false; +} + +firmata_task *FirmataScheduler::findTask(byte id) +{ + firmata_task *currentTask = tasks; + while (currentTask) { + if (id == currentTask->id) { + return currentTask; + } else { + currentTask = currentTask->nextTask; + } + }; + return NULL; +} diff --git a/libraries/ConfigurableFirmata/src/FirmataScheduler.h b/libraries/ConfigurableFirmata/src/FirmataScheduler.h new file mode 100644 index 0000000..bf6bd70 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/FirmataScheduler.h @@ -0,0 +1,73 @@ +/* + FirmataScheduler.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#ifndef FirmataScheduler_h +#define FirmataScheduler_h + +#include +#include "FirmataFeature.h" +#include "Encoder7Bit.h" + +//subcommands +#define CREATE_FIRMATA_TASK 0 +#define DELETE_FIRMATA_TASK 1 +#define ADD_TO_FIRMATA_TASK 2 +#define DELAY_FIRMATA_TASK 3 +#define SCHEDULE_FIRMATA_TASK 4 +#define QUERY_ALL_FIRMATA_TASKS 5 +#define QUERY_FIRMATA_TASK 6 +#define RESET_FIRMATA_TASKS 7 +#define ERROR_TASK_REPLY 8 +#define QUERY_ALL_TASKS_REPLY 9 +#define QUERY_TASK_REPLY 10 + +#define firmata_task_len(a)(sizeof(firmata_task)+(a)->len) + +void delayTaskCallback(long delay); + +struct firmata_task +{ + firmata_task *nextTask; + byte id; //only 7bits used -> supports 127 tasks + long time_ms; + int len; + int pos; + byte messages[]; +}; + +class FirmataScheduler: public FirmataFeature +{ + public: + FirmataScheduler(); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); //empty method + boolean handleSysex(byte command, byte argc, byte* argv); + void runTasks(); + void reset(); + void createTask(byte id, int len); + void deleteTask(byte id); + void addToTask(byte id, int len, byte *message); + void schedule(byte id, long time_ms); + void delayTask(long time_ms); + void queryAllTasks(); + void queryTask(byte id); + + private: + firmata_task *tasks; + firmata_task *running; + + boolean execute(firmata_task *task); + firmata_task *findTask(byte id); + void reportTask(byte id, firmata_task *task, boolean error); +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/I2CFirmata.cpp b/libraries/ConfigurableFirmata/src/I2CFirmata.cpp new file mode 100644 index 0000000..c74ccc3 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/I2CFirmata.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in I2CFirmata.h to avoid having to include Wire.h in all + * sketch files that include ConfigurableFirmata.h + */ diff --git a/libraries/ConfigurableFirmata/src/I2CFirmata.h b/libraries/ConfigurableFirmata/src/I2CFirmata.h new file mode 100644 index 0000000..e837a7b --- /dev/null +++ b/libraries/ConfigurableFirmata/src/I2CFirmata.h @@ -0,0 +1,333 @@ +/* + I2CFirmata.h - 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. + + I2CFirmata.cpp has been merged into this header file as a hack to avoid having to + include Wire.h for every arduino sketch that includes ConfigurableFirmata. + + Last updated by Jeff Hoefs: January 23rd, 2015 +*/ + +#ifndef I2CFirmata_h +#define I2CFirmata_h + +#include +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +#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 + +/* i2c data */ +struct i2c_device_info { + byte addr; + int reg; + byte bytes; + byte stopTX; +}; + +class I2CFirmata: public FirmataFeature +{ + public: + I2CFirmata(); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + void report(); + + private: + /* for i2c read continuous more */ + i2c_device_info query[I2C_MAX_QUERIES]; + + byte i2cRxData[32]; + boolean isI2CEnabled; + signed char queryIndex; + unsigned int i2cReadDelayTime; // default delay time between i2c read request and Wire.requestFrom() + + void readAndReportData(byte address, int theRegister, byte numBytes, byte stopTX); + void handleI2CRequest(byte argc, byte *argv); + boolean handleI2CConfig(byte argc, byte *argv); + boolean enableI2CPins(); + void disableI2CPins(); +}; + + +/* + * I2CFirmata.cpp + * Copied here as a hack to avoid having to include Wire.h in all sketch files that + * include ConfigurableFirmata.h + */ + +I2CFirmata::I2CFirmata() +{ + isI2CEnabled = false; + queryIndex = -1; + i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom() +} + +void I2CFirmata::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); + Wire.write((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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("I2C: Too few bytes received"); + } + + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + + for (int i = 0; i < numBytes && Wire.available(); i++) { + i2cRxData[2 + i] = Wire.read(); + } + + // send slave address, register and received bytes + Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +boolean I2CFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_I2C(pin)) { + if (mode == PIN_MODE_I2C) { + // the user must call I2C_CONFIG to enable I2C for a device + return true; + } else if (isI2CEnabled) { + // disable i2c so pins can be used for other functions + // the following if statements should reconfigure the pins properly + if (Firmata.getPinMode(pin) == PIN_MODE_I2C) { + disableI2CPins(); + } + } + } + return false; +} + +void I2CFirmata::handleCapability(byte pin) +{ + if (IS_PIN_I2C(pin)) { + Firmata.write(PIN_MODE_I2C); + Firmata.write(1); // TODO: could assign a number to map to SCL or SDA + } +} + +boolean I2CFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + switch (command) { + case I2C_REQUEST: + if (isI2CEnabled) { + handleI2CRequest(argc, argv); + return true; + } + case I2C_CONFIG: + return handleI2CConfig(argc, argv); + } + return false; +} + +void I2CFirmata::handleI2CRequest(byte argc, byte *argv) +{ + byte mode; + byte stopTX; + byte slaveAddress; + byte data; + int slaveRegister; + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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); + Wire.write(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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } +} + +boolean I2CFirmata::handleI2CConfig(byte argc, byte *argv) +{ + unsigned int delayTime = (argv[0] + (argv[1] << 7)); + + if (delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + return isI2CEnabled; +} + +boolean I2CFirmata::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)) { + if (Firmata.getPinMode(i) == PIN_MODE_IGNORE) { + return false; + } + // mark pins as i2c so they are ignore in non i2c data requests + Firmata.setPinMode(i, PIN_MODE_I2C); + pinMode(i, PIN_MODE_I2C); + } + } + + isI2CEnabled = true; + + Wire.begin(); + return true; +} + +/* disable the i2c pins so they can be used for other functions */ +void I2CFirmata::disableI2CPins() +{ + isI2CEnabled = false; + // disable read continuous mode for all devices + queryIndex = -1; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +void I2CFirmata::reset() +{ + if (isI2CEnabled) { + disableI2CPins(); + } +} + +void I2CFirmata::report() +{ + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } +} + +#endif /* I2CFirmata_h */ diff --git a/libraries/ConfigurableFirmata/src/OneWireFirmata.cpp b/libraries/ConfigurableFirmata/src/OneWireFirmata.cpp new file mode 100644 index 0000000..80cf5ae --- /dev/null +++ b/libraries/ConfigurableFirmata/src/OneWireFirmata.cpp @@ -0,0 +1,178 @@ +/* + OneWireFirmata.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. All rights reserved. + Copyright (C) 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: January 23rd, 2016 +*/ + +#include +#include "OneWireFirmata.h" +#include "Encoder7Bit.h" + +boolean OneWireFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_DIGITAL(pin) && mode == PIN_MODE_ONEWIRE) { + oneWireConfig(pin, ONEWIRE_POWER); + return true; + } + return false; +} + +void OneWireFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_ONEWIRE); + Firmata.write(1); + } +} + +void OneWireFirmata::oneWireConfig(byte pin, boolean power) +{ + ow_device_info *info = &pinOneWire[pin]; + if (info->device == NULL) { + info->device = new OneWire(pin); + } + for (int i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + info->power = power; +} + +boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == ONEWIRE_DATA) { + if (argc > 1) { + byte subcommand = argv[0]; + byte pin = argv[1]; + ow_device_info *info = &pinOneWire[pin]; + OneWire *device = info->device; + if (device || subcommand == ONEWIRE_CONFIG_REQUEST) { + switch (subcommand) { + case ONEWIRE_SEARCH_REQUEST: + case ONEWIRE_SEARCH_ALARMS_REQUEST: + { + device->reset_search(); + Firmata.write(START_SYSEX); + Firmata.write(ONEWIRE_DATA); + boolean isAlarmSearch = (subcommand == ONEWIRE_SEARCH_ALARMS_REQUEST); + Firmata.write(isAlarmSearch ? (byte)ONEWIRE_SEARCH_ALARMS_REPLY : (byte)ONEWIRE_SEARCH_REPLY); + Firmata.write(pin); + Encoder7Bit.startBinaryWrite(); + byte addrArray[8]; + while (isAlarmSearch ? device->search(addrArray, false) : device->search(addrArray)) { + for (int i = 0; i < 8; i++) { + Encoder7Bit.writeBinary(addrArray[i]); + } + } + Encoder7Bit.endBinaryWrite(); + Firmata.write(END_SYSEX); + break; + } + case ONEWIRE_CONFIG_REQUEST: + { + if (argc == 3 && Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + Firmata.setPinMode(pin, PIN_MODE_ONEWIRE); + oneWireConfig(pin, argv[2]); // this calls oneWireConfig again, this time setting the correct config (which doesn't cause harm though) + } else { + return false; + } + break; + } + default: + { + if (subcommand & ONEWIRE_RESET_REQUEST_BIT) { + device->reset(); + for (int i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + } + if (subcommand & ONEWIRE_SKIP_REQUEST_BIT) { + device->skip(); + for (byte i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + } + if (subcommand & ONEWIRE_WITHDATA_REQUEST_BITS) { + int numBytes = num7BitOutbytes(argc - 2); + int numReadBytes = 0; + int correlationId; + argv += 2; + Encoder7Bit.readBinary(numBytes, argv, argv); //decode inplace + + if (subcommand & ONEWIRE_SELECT_REQUEST_BIT) { + if (numBytes < 8) break; + device->select(argv); + for (int i = 0; i < 8; i++) { + info->addr[i] = argv[i]; + } + argv += 8; + numBytes -= 8; + } + + if (subcommand & ONEWIRE_READ_REQUEST_BIT) { + if (numBytes < 4) break; + numReadBytes = *((int*)argv); + argv += 2; + correlationId = *((int*)argv); + argv += 2; + numBytes -= 4; + } + + if (subcommand & ONEWIRE_DELAY_REQUEST_BIT) { + if (numBytes < 4) break; + Firmata.delayTask(*((long*)argv)); + argv += 4; + numBytes -= 4; + } + + if (subcommand & ONEWIRE_WRITE_REQUEST_BIT) { + for (int i = 0; i < numBytes; i++) { + info->device->write(argv[i], info->power); + } + } + + if (numReadBytes > 0) { + Firmata.write(START_SYSEX); + Firmata.write(ONEWIRE_DATA); + Firmata.write(ONEWIRE_READ_REPLY); + Firmata.write(pin); + Encoder7Bit.startBinaryWrite(); + Encoder7Bit.writeBinary(correlationId & 0xFF); + Encoder7Bit.writeBinary((correlationId >> 8) & 0xFF); + for (int i = 0; i < numReadBytes; i++) { + Encoder7Bit.writeBinary(device->read()); + } + Encoder7Bit.endBinaryWrite(); + Firmata.write(END_SYSEX); + } + } + } + } + } + return true; + } + } + return false; +} + +void OneWireFirmata::reset() +{ + for (int i = 0; i < TOTAL_PINS; i++) { + if (pinOneWire[i].device) { + free(pinOneWire[i].device); + pinOneWire[i].device = NULL; + } + for (int j = 0; j < 8; j++) { + pinOneWire[i].addr[j] = 0; + } + pinOneWire[i].power = false; + } +} diff --git a/libraries/ConfigurableFirmata/src/OneWireFirmata.h b/libraries/ConfigurableFirmata/src/OneWireFirmata.h new file mode 100644 index 0000000..9c68aef --- /dev/null +++ b/libraries/ConfigurableFirmata/src/OneWireFirmata.h @@ -0,0 +1,63 @@ +/* + OneWireFirmata.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. All rights reserved. + Copyright (C) 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. +*/ + +#ifndef OneWireFirmata_h +#define OneWireFirmata_h + +#include +#include "utility/OneWire.h" +#include "FirmataFeature.h" + +//subcommands: +#define ONEWIRE_SEARCH_REQUEST 0x40 +#define ONEWIRE_CONFIG_REQUEST 0x41 +#define ONEWIRE_SEARCH_REPLY 0x42 +#define ONEWIRE_READ_REPLY 0x43 +#define ONEWIRE_SEARCH_ALARMS_REQUEST 0x44 +#define ONEWIRE_SEARCH_ALARMS_REPLY 0x45 + +#define ONEWIRE_RESET_REQUEST_BIT 0x01 +#define ONEWIRE_SKIP_REQUEST_BIT 0x02 +#define ONEWIRE_SELECT_REQUEST_BIT 0x04 +#define ONEWIRE_READ_REQUEST_BIT 0x08 +#define ONEWIRE_DELAY_REQUEST_BIT 0x10 +#define ONEWIRE_WRITE_REQUEST_BIT 0x20 + +#define ONEWIRE_WITHDATA_REQUEST_BITS 0x3C + +#define ONEWIRE_CRC 0 //for OneWire.h: crc-functions are not used by Firmata + +//default value for power: +#define ONEWIRE_POWER 1 + +struct ow_device_info +{ + OneWire* device; + byte addr[8]; + boolean power; +}; + +class OneWireFirmata: public FirmataFeature +{ + public: + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + + private: + ow_device_info pinOneWire[TOTAL_PINS]; + void oneWireConfig(byte pin, boolean power); +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/SerialFirmata.cpp b/libraries/ConfigurableFirmata/src/SerialFirmata.cpp new file mode 100644 index 0000000..ae35e85 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/SerialFirmata.cpp @@ -0,0 +1,360 @@ +/* + SerialFirmata.cpp - Firmata library + Copyright (C) 2015-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 December 23rd, 2016 +*/ + +#include "SerialFirmata.h" + +SerialFirmata::SerialFirmata() +{ +#if defined(SoftwareSerial_h) + swSerial0 = NULL; + swSerial1 = NULL; + swSerial2 = NULL; + swSerial3 = NULL; +#endif + + serialIndex = -1; +} + +boolean SerialFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_SERIAL) { + // nothing else to do here since the mode is set in SERIAL_CONFIG + return true; + } + return false; +} + +void SerialFirmata::handleCapability(byte pin) +{ + if (IS_PIN_SERIAL(pin)) { + Firmata.write(PIN_MODE_SERIAL); + Firmata.write(getSerialPinType(pin)); + } +} + +boolean SerialFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == SERIAL_MESSAGE) { + + Stream *serialPort; + byte mode = argv[0] & SERIAL_MODE_MASK; + byte portId = argv[0] & SERIAL_PORT_ID_MASK; + if (portId >= SERIAL_READ_ARR_LEN) return false; + + switch (mode) { + case SERIAL_CONFIG: + { + long baud = (long)argv[1] | ((long)argv[2] << 7) | ((long)argv[3] << 14); + serial_pins pins; + lastAvailableBytes[portId] = 0; + lastReceive[portId] = 0; +// this ifdef will be removed once a command to enable RX buffering has been added to the protocol +#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING) + // 8N1 = 10 bits per char, max. 50 bits -> 50000 = 50bits * 1000ms/s + // char delay value (ms) to detect the end of a message, defaults to 50 bits * 1000 / baud rate + // a value of 0 will disable RX buffering, resulting in single byte transfers to the host with + // baud rates below approximately 56k (varies with CPU speed) + maxCharDelay[portId] = 50000 / baud; +#else + maxCharDelay[portId] = 0; +#endif + if (portId < 8) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + pins = getSerialPinNumbers(portId); + if (pins.rx != 0 && pins.tx != 0) { + Firmata.setPinMode(pins.rx, PIN_MODE_SERIAL); + Firmata.setPinMode(pins.tx, PIN_MODE_SERIAL); + // Fixes an issue where some serial devices would not work properly with Arduino Due + // because all Arduino pins are set to OUTPUT by default in StandardFirmata. + pinMode(pins.rx, INPUT); + } + ((HardwareSerial*)serialPort)->begin(baud); + } + } else { +#if defined(SoftwareSerial_h) + byte swTxPin, swRxPin; + if (argc > 4) { + swRxPin = argv[4]; + swTxPin = argv[5]; + } else { + // RX and TX pins must be specified when using software serial + Firmata.sendString("Specify serial RX and TX pins"); + return false; + } + switch (portId) { + case SW_SERIAL0: + if (swSerial0 == NULL) { + swSerial0 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL1: + if (swSerial1 == NULL) { + swSerial1 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL2: + if (swSerial2 == NULL) { + swSerial2 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL3: + if (swSerial3 == NULL) { + swSerial3 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + } + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + Firmata.setPinMode(swRxPin, PIN_MODE_SERIAL); + Firmata.setPinMode(swTxPin, PIN_MODE_SERIAL); + ((SoftwareSerial*)serialPort)->begin(baud); + } +#endif + } + break; // SERIAL_CONFIG + } + case SERIAL_WRITE: + { + byte data; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + break; + } + for (byte i = 1; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + serialPort->write(data); + } + break; // SERIAL_WRITE + } + case SERIAL_READ: + if (argv[1] == SERIAL_READ_CONTINUOUSLY) { + if (serialIndex + 1 >= MAX_SERIAL_PORTS) { + break; + } + + if (argc > 2) { + // maximum number of bytes to read from buffer per iteration of loop() + serialBytesToRead[portId] = (int)argv[2] | ((int)argv[3] << 7); + } else { + // read all available bytes per iteration of loop() + serialBytesToRead[portId] = 0; + } + serialIndex++; + reportSerial[serialIndex] = portId; + } else if (argv[1] == SERIAL_STOP_READING) { + byte serialIndexToSkip = 0; + if (serialIndex <= 0) { + serialIndex = -1; + } else { + for (byte i = 0; i < serialIndex + 1; i++) { + if (reportSerial[i] == portId) { + serialIndexToSkip = i; + break; + } + } + // shift elements over to fill space left by removed element + for (byte i = serialIndexToSkip; i < serialIndex + 1; i++) { + if (i < MAX_SERIAL_PORTS) { + reportSerial[i] = reportSerial[i + 1]; + } + } + serialIndex--; + } + } + break; // SERIAL_READ + case SERIAL_CLOSE: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + if (portId < 8) { + ((HardwareSerial*)serialPort)->end(); + } else { +#if defined(SoftwareSerial_h) + ((SoftwareSerial*)serialPort)->end(); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } +#endif + } + } + break; // SERIAL_CLOSE + case SERIAL_FLUSH: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + getPortFromId(portId)->flush(); + } + break; // SERIAL_FLUSH +#if defined(SoftwareSerial_h) + case SERIAL_LISTEN: + // can only call listen() on software serial ports + if (portId > 7) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + ((SoftwareSerial*)serialPort)->listen(); + } + } + break; // SERIAL_LISTEN +#endif + } + return true; + } + return false; +} + +void SerialFirmata::update() +{ + checkSerial(); +} + +void SerialFirmata::reset() +{ +#if defined(SoftwareSerial_h) + Stream *serialPort; + // free memory allocated for SoftwareSerial ports + for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1; i++) { + serialPort = getPortFromId(i); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } + } +#endif + + serialIndex = -1; + for (byte i = 0; i < SERIAL_READ_ARR_LEN; i++) { + serialBytesToRead[i] = 0; + } +} + +// get a pointer to the serial port associated with the specified port id +Stream* SerialFirmata::getPortFromId(byte portId) +{ + switch (portId) { + case HW_SERIAL0: + // block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented + //return &Serial; + return NULL; +#if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + return &Serial1; +#endif +#if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + return &Serial2; +#endif +#if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + return &Serial3; +#endif +#if defined(SoftwareSerial_h) + case SW_SERIAL0: + if (swSerial0 != NULL) { + // instances of SoftwareSerial are already pointers so simply return the instance + return swSerial0; + } + break; + case SW_SERIAL1: + if (swSerial1 != NULL) { + return swSerial1; + } + break; + case SW_SERIAL2: + if (swSerial2 != NULL) { + return swSerial2; + } + break; + case SW_SERIAL3: + if (swSerial3 != NULL) { + return swSerial3; + } + break; +#endif + } + return NULL; +} + +// Check serial ports that have READ_CONTINUOUS mode set and relay any data +// for each port to the device attached to that port. +void SerialFirmata::checkSerial() +{ + byte portId, serialData; + int bytesToRead = 0; + int numBytesToRead = 0; + Stream* serialPort; + + if (serialIndex > -1) { + + unsigned long currentMillis = millis(); + + // loop through all reporting (READ_CONTINUOUS) serial ports + for (byte i = 0; i < serialIndex + 1; i++) { + portId = reportSerial[i]; + bytesToRead = serialBytesToRead[portId]; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + continue; + } +#if defined(SoftwareSerial_h) + // only the SoftwareSerial port that is "listening" can read data + if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening()) { + continue; + } +#endif + int availableBytes = serialPort->available(); + if (availableBytes > 0) { + bool read = true; + + // check if reading should be delayed to collect some bytes before + // forwarding (for baud rates significantly below 57600 baud) + if (maxCharDelay[portId]) { + // inter character delay exceeded or more than 48 bytes available or more bytes available than required + read = (lastAvailableBytes[portId] > 0 && (currentMillis - lastReceive[portId]) >= maxCharDelay[portId]) + || (bytesToRead == 0 && availableBytes >= 48) || (bytesToRead > 0 && availableBytes >= bytesToRead); + if (availableBytes > lastAvailableBytes[portId]) { + lastReceive[portId] = currentMillis; + lastAvailableBytes[portId] = availableBytes; + } + } + + if (read) { + Firmata.write(START_SYSEX); + Firmata.write(SERIAL_MESSAGE); + Firmata.write(SERIAL_REPLY | portId); + + if (bytesToRead == 0 || (serialPort->available() <= bytesToRead)) { + numBytesToRead = serialPort->available(); + } else { + numBytesToRead = bytesToRead; + } + + if (lastAvailableBytes[portId] - numBytesToRead >= 0) { + lastAvailableBytes[portId] -= numBytesToRead; + } else { + lastAvailableBytes[portId] = 0; + } + + // relay serial data to the serial device + while (numBytesToRead > 0) { + serialData = serialPort->read(); + Firmata.write(serialData & 0x7F); + Firmata.write((serialData >> 7) & 0x7F); + numBytesToRead--; + } + Firmata.write(END_SYSEX); + } + } + } + } +} diff --git a/libraries/ConfigurableFirmata/src/SerialFirmata.h b/libraries/ConfigurableFirmata/src/SerialFirmata.h new file mode 100644 index 0000000..87f58d9 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/SerialFirmata.h @@ -0,0 +1,166 @@ +/* + SerialFirmata.h - Firmata library + Copyright (C) 2015-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 December 23rd, 2016 +*/ + +#ifndef SerialFirmata_h +#define SerialFirmata_h + +#include +#include "FirmataFeature.h" +// SoftwareSerial is currently only supported for AVR-based boards and the Arduino 101. +// Limited to Arduino 1.6.6 or higher because Arduino builder cannot find SoftwareSerial +// prior to this release. +#if (ARDUINO > 10605) && (defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ARC32)) +#include +#endif + +// uncomment FIRMATA_SERIAL_PORT_RX_BUFFERING to collect bytes received by serial port until the +// receive buffer gets filled or a data gap is detected to avoid forwarding single bytes at baud +// rates below 50000 +//#define FIRMATA_SERIAL_PORT_RX_BUFFERING + +// Serial port Ids +#define HW_SERIAL0 0x00 +#define HW_SERIAL1 0x01 +#define HW_SERIAL2 0x02 +#define HW_SERIAL3 0x03 +// extensible up to 0x07 + +#define SW_SERIAL0 0x08 +#define SW_SERIAL1 0x09 +#define SW_SERIAL2 0x0A +#define SW_SERIAL3 0x0B +// extensible up to 0x0F + +#define SERIAL_PORT_ID_MASK 0x0F +#define MAX_SERIAL_PORTS 8 +#define SERIAL_READ_ARR_LEN 12 + +// map configuration query response resolution value to serial pin type +#define RES_RX1 0x02 +#define RES_TX1 0x03 +#define RES_RX2 0x04 +#define RES_TX2 0x05 +#define RES_RX3 0x06 +#define RES_TX3 0x07 + +// Serial command bytes +#define SERIAL_CONFIG 0x10 +#define SERIAL_WRITE 0x20 +#define SERIAL_READ 0x30 +#define SERIAL_REPLY 0x40 +#define SERIAL_CLOSE 0x50 +#define SERIAL_FLUSH 0x60 +#define SERIAL_LISTEN 0x70 + +// Serial read modes +#define SERIAL_READ_CONTINUOUSLY 0x00 +#define SERIAL_STOP_READING 0x01 +#define SERIAL_MODE_MASK 0xF0 + +struct serial_pins { + uint8_t rx; + uint8_t tx; +}; + +/* + * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin. + */ +inline uint8_t getSerialPinType(uint8_t pin) { +#if defined(PIN_SERIAL_RX) + // TODO when use of HW_SERIAL0 is enabled +#endif +#if defined(PIN_SERIAL1_RX) + if (pin == PIN_SERIAL1_RX) return RES_RX1; + if (pin == PIN_SERIAL1_TX) return RES_TX1; +#endif +#if defined(PIN_SERIAL2_RX) + if (pin == PIN_SERIAL2_RX) return RES_RX2; + if (pin == PIN_SERIAL2_TX) return RES_TX2; +#endif +#if defined(PIN_SERIAL3_RX) + if (pin == PIN_SERIAL3_RX) return RES_RX3; + if (pin == PIN_SERIAL3_TX) return RES_TX3; +#endif + return 0; +} + +/* + * Get the RX and TX pins numbers for the specified HW serial port. + */ +inline serial_pins getSerialPinNumbers(uint8_t portId) { + serial_pins pins; + switch (portId) { +#if defined(PIN_SERIAL_RX) + // case HW_SERIAL0: + // // TODO when use of HW_SERIAL0 is enabled + // break; +#endif +#if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + pins.rx = PIN_SERIAL1_RX; + pins.tx = PIN_SERIAL1_TX; + break; +#endif +#if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + pins.rx = PIN_SERIAL2_RX; + pins.tx = PIN_SERIAL2_TX; + break; +#endif +#if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + pins.rx = PIN_SERIAL3_RX; + pins.tx = PIN_SERIAL3_TX; + break; +#endif + default: + pins.rx = 0; + pins.tx = 0; + } + return pins; +} + + +class SerialFirmata: public FirmataFeature +{ + public: + SerialFirmata(); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void update(); + void reset(); + void checkSerial(); + + private: + byte reportSerial[MAX_SERIAL_PORTS]; + int serialBytesToRead[SERIAL_READ_ARR_LEN]; + signed char serialIndex; + + unsigned long lastReceive[SERIAL_READ_ARR_LEN]; + unsigned char maxCharDelay[SERIAL_READ_ARR_LEN]; + int lastAvailableBytes[SERIAL_READ_ARR_LEN]; + +#if defined(SoftwareSerial_h) + Stream *swSerial0; + Stream *swSerial1; + Stream *swSerial2; + Stream *swSerial3; +#endif + + Stream* getPortFromId(byte portId); + +}; + +#endif /* SerialFirmata_h */ diff --git a/libraries/ConfigurableFirmata/src/ServoFirmata.cpp b/libraries/ConfigurableFirmata/src/ServoFirmata.cpp new file mode 100644 index 0000000..307453f --- /dev/null +++ b/libraries/ConfigurableFirmata/src/ServoFirmata.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in ServoFirmata.h to avoid having to include Servo.h in all + * sketch files that include ConfigurableFirmata.h + */ diff --git a/libraries/ConfigurableFirmata/src/ServoFirmata.h b/libraries/ConfigurableFirmata/src/ServoFirmata.h new file mode 100644 index 0000000..bac4ed9 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/ServoFirmata.h @@ -0,0 +1,151 @@ +/* + ServoFirmata.h - 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-2015 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. + + ServoFirmata.cpp has been merged into this header file as a hack to avoid having to + include Servo.h for every arduino sketch that includes ConfigurableFirmata. + + Last updated by Jeff Hoefs: November 15th, 2015 +*/ + +#ifndef ServoFirmata_h +#define ServoFirmata_h + +#include +#include +#include "FirmataFeature.h" + +void servoAnalogWrite(byte pin, int value); + +class ServoFirmata: public FirmataFeature +{ + public: + ServoFirmata(); + boolean analogWrite(byte pin, int value); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + private: + Servo *servos[MAX_SERVOS]; + void attach(byte pin, int minPulse, int maxPulse); + void detach(byte pin); +}; + + +/* + * ServoFirmata.cpp + * Copied here as a hack to avoid having to include Servo.h in all sketch files that + * include ConfigurableFirmata.h + */ + +ServoFirmata *ServoInstance; + +void servoAnalogWrite(byte pin, int value) +{ + ServoInstance->analogWrite(pin, value); +} + +ServoFirmata::ServoFirmata() +{ + ServoInstance = this; +} + +boolean ServoFirmata::analogWrite(byte pin, int value) +{ + if (IS_PIN_SERVO(pin)) { + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (servo) { + servo->write(value); + return true; + } + } + return false; +} + +boolean ServoFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_SERVO(pin)) { + if (mode == PIN_MODE_SERVO) { + attach(pin, -1, -1); + return true; + } else { + detach(pin); + } + } + return false; +} + +void ServoFirmata::handleCapability(byte pin) +{ + if (IS_PIN_SERVO(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); //14 bit resolution (Servo takes int as argument) + } +} + +boolean ServoFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SERVO_CONFIG) { + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + if (IS_PIN_SERVO(pin) && Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + Firmata.setPinMode(pin, PIN_MODE_SERVO); + attach(pin, minPulse, maxPulse); + return true; + } + } + } + return false; +} + +void ServoFirmata::attach(byte pin, int minPulse, int maxPulse) +{ + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (!servo) { + servo = new Servo(); + servos[PIN_TO_SERVO(pin)] = servo; + } + if (servo->attached()) + servo->detach(); + if (minPulse >= 0 || maxPulse >= 0) + servo->attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + else + servo->attach(PIN_TO_DIGITAL(pin)); +} + +void ServoFirmata::detach(byte pin) +{ + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (servo) { + if (servo->attached()) + servo->detach(); + free(servo); + servos[PIN_TO_SERVO(pin)] = NULL; + } +} + +void ServoFirmata::reset() +{ + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_SERVO(pin)) { + detach(pin); + } + } +} + +#endif /* ServoFirmata_h */ diff --git a/libraries/ConfigurableFirmata/src/StepperFirmata.cpp b/libraries/ConfigurableFirmata/src/StepperFirmata.cpp new file mode 100644 index 0000000..a5618ff --- /dev/null +++ b/libraries/ConfigurableFirmata/src/StepperFirmata.cpp @@ -0,0 +1,151 @@ +/* + 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: January 23rd, 2016 +*/ + +#include +#include "StepperFirmata.h" +#include "utility/FirmataStepper.h" + +boolean StepperFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_STEPPER) { + if (IS_PIN_DIGITAL(pin)) { + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM + pinMode(PIN_TO_DIGITAL(pin), OUTPUT); + return true; + } + } + return false; +} + +void StepperFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_STEPPER); + Firmata.write(21); //21 bits used for number of steps + } +} + +/*============================================================================== + * SYSEX-BASED commands + *============================================================================*/ + +boolean StepperFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == STEPPER_DATA) { + byte stepCommand, deviceNum, directionPin, stepPin, stepDirection; + byte interface, interfaceType; + byte motorPin3, motorPin4; + unsigned int stepsPerRev; + long numSteps; + int stepSpeed; + int accel; + int decel; + + stepCommand = argv[0]; + deviceNum = argv[1]; + + if (deviceNum < MAX_STEPPERS) { + if (stepCommand == STEPPER_CONFIG) { + + interface = argv[2]; // upper 4 bits are the stepDelay, lower 4 bits are the interface type + interfaceType = interface & 0x0F; // the interface type is specified by the lower 4 bits + stepsPerRev = (argv[3] + (argv[4] << 7)); + + directionPin = argv[5]; // or motorPin1 for TWO_WIRE or FOUR_WIRE interface + stepPin = argv[6]; // // or motorPin2 for TWO_WIRE or FOUR_WIRE interface + if (Firmata.getPinMode(directionPin) == PIN_MODE_IGNORE || Firmata.getPinMode(stepPin) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(directionPin, PIN_MODE_STEPPER); + Firmata.setPinMode(stepPin, PIN_MODE_STEPPER); + + if (!stepper[deviceNum]) { + numSteppers++; + } + if (interfaceType == FirmataStepper::DRIVER || interfaceType == FirmataStepper::TWO_WIRE) { + stepper[deviceNum] = new FirmataStepper(interface, stepsPerRev, directionPin, stepPin); + } else if (interfaceType == FirmataStepper::FOUR_WIRE) { + motorPin3 = argv[7]; + motorPin4 = argv[8]; + if (Firmata.getPinMode(motorPin3) == PIN_MODE_IGNORE || Firmata.getPinMode(motorPin4) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(motorPin3, PIN_MODE_STEPPER); + Firmata.setPinMode(motorPin4, PIN_MODE_STEPPER); + stepper[deviceNum] = new FirmataStepper(interface, stepsPerRev, directionPin, stepPin, motorPin3, motorPin4); + } + } + else if (stepCommand == STEPPER_STEP) { + stepDirection = argv[2]; + numSteps = (long)argv[3] | ((long)argv[4] << 7) | ((long)argv[5] << 14); + stepSpeed = (argv[6] + (argv[7] << 7)); + + if (stepDirection == 0) { + numSteps *= -1; + } + if (stepper[deviceNum]) { + if (argc >= 8 && argc < 12) { + // num steps, speed (0.01*rad/sec) + stepper[deviceNum]->setStepsToMove(numSteps, stepSpeed); + } else if (argc == 12) { + accel = (argv[8] + (argv[9] << 7)); + decel = (argv[10] + (argv[11] << 7)); + // num steps, speed (0.01*rad/sec), accel (0.01*rad/sec^2), decel (0.01*rad/sec^2) + stepper[deviceNum]->setStepsToMove(numSteps, stepSpeed, accel, decel); + } + } + } + return true; + } + } + return false; +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void StepperFirmata::reset() +{ + for (byte i = 0; i < MAX_STEPPERS; i++) { + if (stepper[i]) { + free(stepper[i]); + stepper[i] = 0; + } + } + numSteppers = 0; +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void StepperFirmata::update() +{ + if (numSteppers > 0) { + // if one or more stepper motors are used, update their position + for (byte i = 0; i < MAX_STEPPERS; i++) { + if (stepper[i]) { + bool done = stepper[i]->update(); + // send command to client application when stepping is complete + if (done) { + Firmata.write(START_SYSEX); + Firmata.write(STEPPER_DATA); + Firmata.write(i); + Firmata.write(END_SYSEX); + } + } + } + } +} diff --git a/libraries/ConfigurableFirmata/src/StepperFirmata.h b/libraries/ConfigurableFirmata/src/StepperFirmata.h new file mode 100644 index 0000000..7fc9459 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/StepperFirmata.h @@ -0,0 +1,40 @@ +/* + 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. +*/ + +#ifndef StepperFirmata_h +#define StepperFirmata_h + +#include +#include "utility/FirmataStepper.h" +#include "FirmataFeature.h" + +#define MAX_STEPPERS 6 // arbitrary value... may need to adjust +#define STEPPER_CONFIG 0 +#define STEPPER_STEP 1 + +class StepperFirmata: public FirmataFeature +{ + public: + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte *argv); + void update(); + void reset(); + private: + FirmataStepper *stepper[MAX_STEPPERS]; + byte numSteppers; +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/AccelStepper.cpp b/libraries/ConfigurableFirmata/src/utility/AccelStepper.cpp new file mode 100644 index 0000000..2bc17bd --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/AccelStepper.cpp @@ -0,0 +1,651 @@ +// AccelStepper.cpp +// +// Copyright (C) 2009-2013 Mike McCauley +// $Id: AccelStepper.cpp,v 1.23 2016/08/09 00:39:10 mikem Exp $ + +#include "AccelStepper.h" + +#if 0 +// Some debugging assistance +void dump(uint8_t* p, int l) +{ + int i; + + for (i = 0; i < l; i++) + { + Serial.print(p[i], HEX); + Serial.print(" "); + } + Serial.println(""); +} +#endif + +void AccelStepper::moveTo(long absolute) +{ + if (_targetPos != absolute) + { + _targetPos = absolute; + computeNewSpeed(); + // compute new n? + } +} + +void AccelStepper::move(long relative) +{ + moveTo(_currentPos + relative); +} + +// Implements steps according to the current step interval +// You must call this at least once per step +// returns true if a step occurred +boolean AccelStepper::runSpeed() +{ + // Dont do anything unless we actually have a step interval + if (!_stepInterval) + return false; + + unsigned long time = micros(); + if (time - _lastStepTime >= _stepInterval) + { + if (_direction == DIRECTION_CW) + { + // Clockwise + _currentPos += 1; + } + else + { + // Anticlockwise + _currentPos -= 1; + } + step(_currentPos); + + _lastStepTime = time; // Caution: does not account for costs in step() + + return true; + } + else + { + return false; + } +} + +long AccelStepper::distanceToGo() +{ + return _targetPos - _currentPos; +} + +long AccelStepper::targetPosition() +{ + return _targetPos; +} + +long AccelStepper::currentPosition() +{ + return _currentPos; +} + +// Useful during initialisations or after initial positioning +// Sets speed to 0 +void AccelStepper::setCurrentPosition(long position) +{ + _targetPos = _currentPos = position; + _n = 0; + _stepInterval = 0; + _speed = 0.0; +} + +void AccelStepper::computeNewSpeed() +{ + long distanceTo = distanceToGo(); // +ve is clockwise from curent location + + long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16 + + if (distanceTo == 0 && stepsToStop <= 1) + { + // We are at the target and its time to stop + _stepInterval = 0; + _speed = 0.0; + _n = 0; + return; + } + + if (distanceTo > 0) + { + // We are anticlockwise from the target + // Need to go clockwise from here, maybe decelerate now + if (_n > 0) + { + // Currently accelerating, need to decel now? Or maybe going the wrong way? + if ((stepsToStop >= distanceTo) || _direction == DIRECTION_CCW) + _n = -stepsToStop; // Start deceleration + } + else if (_n < 0) + { + // Currently decelerating, need to accel again? + if ((stepsToStop < distanceTo) && _direction == DIRECTION_CW) + _n = -_n; // Start accceleration + } + } + else if (distanceTo < 0) + { + // We are clockwise from the target + // Need to go anticlockwise from here, maybe decelerate + if (_n > 0) + { + // Currently accelerating, need to decel now? Or maybe going the wrong way? + if ((stepsToStop >= -distanceTo) || _direction == DIRECTION_CW) + _n = -stepsToStop; // Start deceleration + } + else if (_n < 0) + { + // Currently decelerating, need to accel again? + if ((stepsToStop < -distanceTo) && _direction == DIRECTION_CCW) + _n = -_n; // Start accceleration + } + } + + // Need to accelerate or decelerate + if (_n == 0) + { + // First step from stopped + _cn = _c0; + _direction = (distanceTo > 0) ? DIRECTION_CW : DIRECTION_CCW; + } + else + { + // Subsequent step. Works for accel (n is +_ve) and decel (n is -ve). + _cn = _cn - ((2.0 * _cn) / ((4.0 * _n) + 1)); // Equation 13 + _cn = max(_cn, _cmin); + } + _n++; + _stepInterval = _cn; + _speed = 1000000.0 / _cn; + if (_direction == DIRECTION_CCW) + _speed = -_speed; + +#if 0 + Serial.println(_speed); + Serial.println(_acceleration); + Serial.println(_cn); + Serial.println(_c0); + Serial.println(_n); + Serial.println(_stepInterval); + Serial.println(distanceTo); + Serial.println(stepsToStop); + Serial.println("-----"); +#endif +} + +// Run the motor to implement speed and acceleration in order to proceed to the target position +// You must call this at least once per step, preferably in your main loop +// If the motor is in the desired position, the cost is very small +// returns true if the motor is still running to the target position. +boolean AccelStepper::run() +{ + if (runSpeed()) + computeNewSpeed(); + return _speed != 0.0 || distanceToGo() != 0; +} + +AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, bool enable) +{ + _interface = interface; + _currentPos = 0; + _targetPos = 0; + _speed = 0.0; + _maxSpeed = 1.0; + _acceleration = 0.0; + _sqrt_twoa = 1.0; + _stepInterval = 0; + _minPulseWidth = 1; + _enablePin = 0xff; + _lastStepTime = 0; + _pin[0] = pin1; + _pin[1] = pin2; + _pin[2] = pin3; + _pin[3] = pin4; + + // NEW + _n = 0; + _c0 = 0.0; + _cn = 0.0; + _cmin = 1.0; + _direction = DIRECTION_CCW; + + int i; + for (i = 0; i < 4; i++) + _pinInverted[i] = 0; + if (enable) + enableOutputs(); + // Some reasonable default + setAcceleration(1); +} + +AccelStepper::AccelStepper(void (*forward)(), void (*backward)()) +{ + _interface = 0; + _currentPos = 0; + _targetPos = 0; + _speed = 0.0; + _maxSpeed = 1.0; + _acceleration = 0.0; + _sqrt_twoa = 1.0; + _stepInterval = 0; + _minPulseWidth = 1; + _enablePin = 0xff; + _lastStepTime = 0; + _pin[0] = 0; + _pin[1] = 0; + _pin[2] = 0; + _pin[3] = 0; + _forward = forward; + _backward = backward; + + // NEW + _n = 0; + _c0 = 0.0; + _cn = 0.0; + _cmin = 1.0; + _direction = DIRECTION_CCW; + + int i; + for (i = 0; i < 4; i++) + _pinInverted[i] = 0; + // Some reasonable default + setAcceleration(1); +} + +void AccelStepper::setMaxSpeed(float speed) +{ + if (speed < 0.0) + speed = -speed; + if (_maxSpeed != speed) + { + _maxSpeed = speed; + _cmin = 1000000.0 / speed; + // Recompute _n from current speed and adjust speed if accelerating or cruising + if (_n > 0) + { + _n = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16 + computeNewSpeed(); + } + } +} + +float AccelStepper::maxSpeed() +{ + return _maxSpeed; +} + +void AccelStepper::setAcceleration(float acceleration) +{ + if (acceleration == 0.0) + return; + if (acceleration < 0.0) + acceleration = -acceleration; + if (_acceleration != acceleration) + { + // Recompute _n per Equation 17 + _n = _n * (_acceleration / acceleration); + // New c0 per Equation 7, with correction per Equation 15 + _c0 = 0.676 * sqrt(2.0 / acceleration) * 1000000.0; // Equation 15 + _acceleration = acceleration; + computeNewSpeed(); + } +} + +void AccelStepper::setSpeed(float speed) +{ + if (speed == _speed) + return; + speed = constrain(speed, -_maxSpeed, _maxSpeed); + if (speed == 0.0) + _stepInterval = 0; + else + { + _stepInterval = fabs(1000000.0 / speed); + _direction = (speed > 0.0) ? DIRECTION_CW : DIRECTION_CCW; + } + _speed = speed; +} + +float AccelStepper::speed() +{ + return _speed; +} + +// Subclasses can override +void AccelStepper::step(long step) +{ + switch (_interface) + { + case FUNCTION: + step0(step); + break; + + case DRIVER: + step1(step); + break; + + case FULL2WIRE: + step2(step); + break; + + case FULL3WIRE: + step3(step); + break; + + case FULL4WIRE: + step4(step); + break; + + case HALF3WIRE: + step6(step); + break; + + case HALF4WIRE: + step8(step); + break; + } +} + +// You might want to override this to implement eg serial output +// bit 0 of the mask corresponds to _pin[0] +// bit 1 of the mask corresponds to _pin[1] +// .... +void AccelStepper::setOutputPins(uint8_t mask) +{ + uint8_t numpins = 2; + if (_interface == FULL4WIRE || _interface == HALF4WIRE) + numpins = 4; + else if (_interface == FULL3WIRE || _interface == HALF3WIRE) + numpins = 3; + uint8_t i; + for (i = 0; i < numpins; i++) + digitalWrite(_pin[i], (mask & (1 << i)) ? (HIGH ^ _pinInverted[i]) : (LOW ^ _pinInverted[i])); +} + +// 0 pin step function (ie for functional usage) +void AccelStepper::step0(long step) +{ + (void)(step); // Unused + if (_speed > 0) + _forward(); + else + _backward(); +} + +// 1 pin step function (ie for stepper drivers) +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step1(long step) +{ + (void)(step); // Unused + + // _pin[0] is step, _pin[1] is direction + setOutputPins(_direction ? 0b10 : 0b00); // Set direction first else get rogue pulses + setOutputPins(_direction ? 0b11 : 0b01); // step HIGH + // Caution 200ns setup time + // Delay the minimum allowed pulse width + delayMicroseconds(_minPulseWidth); + setOutputPins(_direction ? 0b10 : 0b00); // step LOW +} + + +// 2 pin step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step2(long step) +{ + switch (step & 0x3) + { + case 0: /* 01 */ + setOutputPins(0b10); + break; + + case 1: /* 11 */ + setOutputPins(0b11); + break; + + case 2: /* 10 */ + setOutputPins(0b01); + break; + + case 3: /* 00 */ + setOutputPins(0b00); + break; + } +} +// 3 pin step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step3(long step) +{ + switch (step % 3) + { + case 0: // 100 + setOutputPins(0b100); + break; + + case 1: // 001 + setOutputPins(0b001); + break; + + case 2: //010 + setOutputPins(0b010); + break; + + } +} + +// 4 pin step function for half stepper +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step4(long step) +{ + switch (step & 0x3) + { + case 0: // 1010 + setOutputPins(0b0101); + break; + + case 1: // 0110 + setOutputPins(0b0110); + break; + + case 2: //0101 + setOutputPins(0b1010); + break; + + case 3: //1001 + setOutputPins(0b1001); + break; + } +} + +// 3 pin half step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step6(long step) +{ + switch (step % 6) + { + case 0: // 100 + setOutputPins(0b100); + break; + + case 1: // 101 + setOutputPins(0b101); + break; + + case 2: // 001 + setOutputPins(0b001); + break; + + case 3: // 011 + setOutputPins(0b011); + break; + + case 4: // 010 + setOutputPins(0b010); + break; + + case 5: // 011 + setOutputPins(0b110); + break; + + } +} + +// 4 pin half step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step8(long step) +{ + switch (step & 0x7) + { + case 0: // 1000 + setOutputPins(0b0001); + break; + + case 1: // 1010 + setOutputPins(0b0101); + break; + + case 2: // 0010 + setOutputPins(0b0100); + break; + + case 3: // 0110 + setOutputPins(0b0110); + break; + + case 4: // 0100 + setOutputPins(0b0010); + break; + + case 5: //0101 + setOutputPins(0b1010); + break; + + case 6: // 0001 + setOutputPins(0b1000); + break; + + case 7: //1001 + setOutputPins(0b1001); + break; + } +} + +// Prevents power consumption on the outputs +void AccelStepper::disableOutputs() +{ + if (! _interface) return; + + setOutputPins(0); // Handles inversion automatically + if (_enablePin != 0xff) + { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, LOW ^ _enableInverted); + } +} + +void AccelStepper::enableOutputs() +{ + if (! _interface) + return; + + pinMode(_pin[0], OUTPUT); + pinMode(_pin[1], OUTPUT); + if (_interface == FULL4WIRE || _interface == HALF4WIRE) + { + pinMode(_pin[2], OUTPUT); + pinMode(_pin[3], OUTPUT); + } + else if (_interface == FULL3WIRE || _interface == HALF3WIRE) + { + pinMode(_pin[2], OUTPUT); + } + + if (_enablePin != 0xff) + { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, HIGH ^ _enableInverted); + } +} + +void AccelStepper::setMinPulseWidth(unsigned int minWidth) +{ + _minPulseWidth = minWidth; +} + +void AccelStepper::setEnablePin(uint8_t enablePin) +{ + _enablePin = enablePin; + + // This happens after construction, so init pin now. + if (_enablePin != 0xff) + { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, HIGH ^ _enableInverted); + } +} + +void AccelStepper::setPinsInverted(bool directionInvert, bool stepInvert, bool enableInvert) +{ + _pinInverted[0] = stepInvert; + _pinInverted[1] = directionInvert; + _enableInverted = enableInvert; +} + +void AccelStepper::setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert) +{ + _pinInverted[0] = pin1Invert; + _pinInverted[1] = pin2Invert; + _pinInverted[2] = pin3Invert; + _pinInverted[3] = pin4Invert; + _enableInverted = enableInvert; +} + +// Blocks until the target position is reached and stopped +void AccelStepper::runToPosition() +{ + while (run()) + ; +} + +boolean AccelStepper::runSpeedToPosition() +{ + if (_targetPos == _currentPos) + return false; + if (_targetPos >_currentPos) + _direction = DIRECTION_CW; + else + _direction = DIRECTION_CCW; + return runSpeed(); +} + +// Blocks until the new target position is reached +void AccelStepper::runToNewPosition(long position) +{ + moveTo(position); + runToPosition(); +} + +void AccelStepper::stop() +{ + if (_speed != 0.0) + { + long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)) + 1; // Equation 16 (+integer rounding) + if (_speed > 0) + move(stepsToStop); + else + move(-stepsToStop); + } +} + +bool AccelStepper::isRunning() +{ + return !(_speed == 0.0 && _targetPos == _currentPos); +} diff --git a/libraries/ConfigurableFirmata/src/utility/AccelStepper.h b/libraries/ConfigurableFirmata/src/utility/AccelStepper.h new file mode 100644 index 0000000..c4fd852 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/AccelStepper.h @@ -0,0 +1,728 @@ +// AccelStepper.h +// +/// \mainpage AccelStepper library for Arduino +/// +/// This is the Arduino AccelStepper library. +/// It provides an object-oriented interface for 2, 3 or 4 pin stepper motors and motor drivers. +/// +/// The standard Arduino IDE includes the Stepper library +/// (http://arduino.cc/en/Reference/Stepper) for stepper motors. It is +/// perfectly adequate for simple, single motor applications. +/// +/// AccelStepper significantly improves on the standard Arduino Stepper library in several ways: +/// \li Supports acceleration and deceleration +/// \li Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper +/// \li API functions never delay() or block +/// \li Supports 2, 3 and 4 wire steppers, plus 3 and 4 wire half steppers. +/// \li Supports alternate stepping functions to enable support of AFMotor (https://github.com/adafruit/Adafruit-Motor-Shield-library) +/// \li Supports stepper drivers such as the Sparkfun EasyDriver (based on 3967 driver chip) +/// \li Very slow speeds are supported +/// \li Extensive API +/// \li Subclass support +/// +/// The latest version of this documentation can be downloaded from +/// http://www.airspayce.com/mikem/arduino/AccelStepper +/// The version of the package that this documentation refers to can be downloaded +/// from http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.57.zip +/// +/// Example Arduino programs are included to show the main modes of use. +/// +/// You can also find online help and discussion at http://groups.google.com/group/accelstepper +/// Please use that group for all questions and discussions on this topic. +/// Do not contact the author directly, unless it is to discuss commercial licensing. +/// Before asking a question or reporting a bug, please read +/// - http://en.wikipedia.org/wiki/Wikipedia:Reference_desk/How_to_ask_a_software_question +/// - http://www.catb.org/esr/faqs/smart-questions.html +/// - http://www.chiark.greenend.org.uk/~shgtatham/bugs.html +/// +/// Tested on Arduino Diecimila and Mega with arduino-0018 & arduino-0021 +/// on OpenSuSE 11.1 and avr-libc-1.6.1-1.15, +/// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5. +/// Tested on Teensy http://www.pjrc.com/teensy including Teensy 3.1 built using Arduino IDE 1.0.5 with +/// teensyduino addon 1.18 and later. +/// +/// \par Installation +/// +/// Install in the usual way: unzip the distribution zip file to the libraries +/// sub-folder of your sketchbook. +/// +/// \par Theory +/// +/// This code uses speed calculations as described in +/// "Generate stepper-motor speed profiles in real time" by David Austin +/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf or +/// http://www.embedded.com/design/mcus-processors-and-socs/4006438/Generate-stepper-motor-speed-profiles-in-real-time or +/// http://web.archive.org/web/20140705143928/http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf +/// with the exception that AccelStepper uses steps per second rather than radians per second +/// (because we dont know the step angle of the motor) +/// An initial step interval is calculated for the first step, based on the desired acceleration +/// On subsequent steps, shorter step intervals are calculated based +/// on the previous step until max speed is achieved. +/// +/// \par Adafruit Motor Shield V2 +/// +/// The included examples AFMotor_* are for Adafruit Motor Shield V1 and do not work with Adafruit Motor Shield V2. +/// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library for examples that work with Adafruit Motor Shield V2. +/// +/// \par Donations +/// +/// This library is offered under a free GPL license for those who want to use it that way. +/// We try hard to keep it up to date, fix bugs +/// and to provide free support. If this library has helped you save time or money, please consider donating at +/// http://www.airspayce.com or here: +/// +/// \htmlonly
\endhtmlonly +/// +/// \par Trademarks +/// +/// AccelStepper is a trademark of AirSpayce Pty Ltd. The AccelStepper mark was first used on April 26 2010 for +/// international trade, and is used only in relation to motor control hardware and software. +/// It is not to be confused with any other similar marks covering other goods and services. +/// +/// \par Copyright +/// +/// This software is Copyright (C) 2010 Mike McCauley. Use is subject to license +/// conditions. The main licensing options available are GPL V2 or Commercial: +/// +/// \par Open Source Licensing GPL V2 +/// This is the appropriate option if you want to share the source code of your +/// application with everyone you distribute it to, and you also want to give them +/// the right to share who uses it. If you wish to use this software under Open +/// Source Licensing, you must contribute all your source code to the open source +/// community in accordance with the GPL Version 2 when your application is +/// distributed. See https://www.gnu.org/licenses/gpl-2.0.html +/// +/// \par Commercial Licensing +/// This is the appropriate option if you are creating proprietary applications +/// and you are not prepared to distribute and share the source code of your +/// application. Purchase commercial licenses at http://airspayce.binpress.com/ +/// +/// \par Revision History +/// \version 1.0 Initial release +/// +/// \version 1.1 Added speed() function to get the current speed. +/// \version 1.2 Added runSpeedToPosition() submitted by Gunnar Arndt. +/// \version 1.3 Added support for stepper drivers (ie with Step and Direction inputs) with _pins == 1 +/// \version 1.4 Added functional contructor to support AFMotor, contributed by Limor, with example sketches. +/// \version 1.5 Improvements contributed by Peter Mousley: Use of microsecond steps and other speed improvements +/// to increase max stepping speed to about 4kHz. New option for user to set the min allowed pulse width. +/// Added checks for already running at max speed and skip further calcs if so. +/// \version 1.6 Fixed a problem with wrapping of microsecond stepping that could cause stepping to hang. +/// Reported by Sandy Noble. +/// Removed redundant _lastRunTime member. +/// \version 1.7 Fixed a bug where setCurrentPosition() did not always work as expected. +/// Reported by Peter Linhart. +/// \version 1.8 Added support for 4 pin half-steppers, requested by Harvey Moon +/// \version 1.9 setCurrentPosition() now also sets motor speed to 0. +/// \version 1.10 Builds on Arduino 1.0 +/// \version 1.11 Improvments from Michael Ellison: +/// Added optional enable line support for stepper drivers +/// Added inversion for step/direction/enable lines for stepper drivers +/// \version 1.12 Announce Google Group +/// \version 1.13 Improvements to speed calculation. Cost of calculation is now less in the worst case, +/// and more or less constant in all cases. This should result in slightly beter high speed performance, and +/// reduce anomalous speed glitches when other steppers are accelerating. +/// However, its hard to see how to replace the sqrt() required at the very first step from 0 speed. +/// \version 1.14 Fixed a problem with compiling under arduino 0021 reported by EmbeddedMan +/// \version 1.15 Fixed a problem with runSpeedToPosition which did not correctly handle +/// running backwards to a smaller target position. Added examples +/// \version 1.16 Fixed some cases in the code where abs() was used instead of fabs(). +/// \version 1.17 Added example ProportionalControl +/// \version 1.18 Fixed a problem: If one calls the funcion runSpeed() when Speed is zero, it makes steps +/// without counting. reported by Friedrich, Klappenbach. +/// \version 1.19 Added MotorInterfaceType and symbolic names for the number of pins to use +/// for the motor interface. Updated examples to suit. +/// Replaced individual pin assignment variables _pin1, _pin2 etc with array _pin[4]. +/// _pins member changed to _interface. +/// Added _pinInverted array to simplify pin inversion operations. +/// Added new function setOutputPins() which sets the motor output pins. +/// It can be overridden in order to provide, say, serial output instead of parallel output +/// Some refactoring and code size reduction. +/// \version 1.20 Improved documentation and examples to show need for correctly +/// specifying AccelStepper::FULL4WIRE and friends. +/// \version 1.21 Fixed a problem where desiredSpeed could compute the wrong step acceleration +/// when _speed was small but non-zero. Reported by Brian Schmalz. +/// Precompute sqrt_twoa to improve performance and max possible stepping speed +/// \version 1.22 Added Bounce.pde example +/// Fixed a problem where calling moveTo(), setMaxSpeed(), setAcceleration() more +/// frequently than the step time, even +/// with the same values, would interfere with speed calcs. Now a new speed is computed +/// only if there was a change in the set value. Reported by Brian Schmalz. +/// \version 1.23 Rewrite of the speed algorithms in line with +/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf +/// Now expect smoother and more linear accelerations and decelerations. The desiredSpeed() +/// function was removed. +/// \version 1.24 Fixed a problem introduced in 1.23: with runToPosition, which did never returned +/// \version 1.25 Now ignore attempts to set acceleration to 0.0 +/// \version 1.26 Fixed a problem where certina combinations of speed and accelration could cause +/// oscillation about the target position. +/// \version 1.27 Added stop() function to stop as fast as possible with current acceleration parameters. +/// Also added new Quickstop example showing its use. +/// \version 1.28 Fixed another problem where certain combinations of speed and accelration could cause +/// oscillation about the target position. +/// Added support for 3 wire full and half steppers such as Hard Disk Drive spindle. +/// Contributed by Yuri Ivatchkovitch. +/// \version 1.29 Fixed a problem that could cause a DRIVER stepper to continually step +/// with some sketches. Reported by Vadim. +/// \version 1.30 Fixed a problem that could cause stepper to back up a few steps at the end of +/// accelerated travel with certain speeds. Reported and patched by jolo. +/// \version 1.31 Updated author and distribution location details to airspayce.com +/// \version 1.32 Fixed a problem with enableOutputs() and setEnablePin on Arduino Due that +/// prevented the enable pin changing stae correctly. Reported by Duane Bishop. +/// \version 1.33 Fixed an error in example AFMotor_ConstantSpeed.pde did not setMaxSpeed(); +/// Fixed a problem that caused incorrect pin sequencing of FULL3WIRE and HALF3WIRE. +/// Unfortunately this meant changing the signature for all step*() functions. +/// Added example MotorShield, showing how to use AdaFruit Motor Shield to control +/// a 3 phase motor such as a HDD spindle motor (and without using the AFMotor library. +/// \version 1.34 Added setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert) +/// to allow inversion of 2, 3 and 4 wire stepper pins. Requested by Oleg. +/// \version 1.35 Removed default args from setPinsInverted(bool, bool, bool, bool, bool) to prevent ambiguity with +/// setPinsInverted(bool, bool, bool). Reported by Mac Mac. +/// \version 1.36 Changed enableOutputs() and disableOutputs() to be virtual so can be overridden. +/// Added new optional argument 'enable' to constructor, which allows you toi disable the +/// automatic enabling of outputs at construction time. Suggested by Guido. +/// \version 1.37 Fixed a problem with step1 that could cause a rogue step in the +/// wrong direction (or not, +/// depending on the setup-time requirements of the connected hardware). +/// Reported by Mark Tillotson. +/// \version 1.38 run() function incorrectly always returned true. Updated function and doc so it returns true +/// if the motor is still running to the target position. +/// \version 1.39 Updated typos in keywords.txt, courtesey Jon Magill. +/// \version 1.40 Updated documentation, including testing on Teensy 3.1 +/// \version 1.41 Fixed an error in the acceleration calculations, resulting in acceleration of haldf the intended value +/// \version 1.42 Improved support for FULL3WIRE and HALF3WIRE output pins. These changes were in Yuri's original +/// contribution but did not make it into production.
+/// \version 1.43 Added DualMotorShield example. Shows how to use AccelStepper to control 2 x 2 phase steppers using the +/// Itead Studio Arduino Dual Stepper Motor Driver Shield model IM120417015.
+/// \version 1.44 examples/DualMotorShield/DualMotorShield.ino examples/DualMotorShield/DualMotorShield.pde +/// was missing from the distribution.
+/// \version 1.45 Fixed a problem where if setAcceleration was not called, there was no default +/// acceleration. Reported by Michael Newman.
+/// \version 1.45 Fixed inaccuracy in acceleration rate by using Equation 15, suggested by Sebastian Gracki.
+/// Performance improvements in runSpeed suggested by Jaakko Fagerlund.
+/// \version 1.46 Fixed error in documentation for runToPosition(). +/// Reinstated time calculations in runSpeed() since new version is reported +/// not to work correctly under some circumstances. Reported by Oleg V Gavva.
+/// \version 1.48 2015-08-25 +/// Added new class MultiStepper that can manage multiple AccelSteppers, +/// and cause them all to move +/// to selected positions at such a (constant) speed that they all arrive at their +/// target position at the same time. Suitable for X-Y flatbeds etc.
+/// Added new method maxSpeed() to AccelStepper to return the currently configured maxSpeed.
+/// \version 1.49 2016-01-02 +/// Testing with VID28 series instrument stepper motors and EasyDriver. +/// OK, although with light pointers +/// and slow speeds like 180 full steps per second the motor movement can be erratic, +/// probably due to some mechanical resonance. Best to accelerate through this speed.
+/// Added isRunning().
+/// \version 1.50 2016-02-25 +/// AccelStepper::disableOutputs now sets the enable pion to OUTPUT mode if the enable pin is defined. +/// Patch from Piet De Jong.
+/// Added notes about the fact that AFMotor_* examples do not work with Adafruit Motor Shield V2.
+/// \version 1.51 2016-03-24 +/// Fixed a problem reported by gregor: when resetting the stepper motor position using setCurrentPosition() the +/// stepper speed is reset by setting _stepInterval to 0, but _speed is not +/// reset. this results in the stepper motor not starting again when calling +/// setSpeed() with the same speed the stepper was set to before. +/// \version 1.52 2016-08-09 +/// Added MultiStepper to keywords.txt. +/// Improvements to efficiency of AccelStepper::runSpeed() as suggested by David Grayson. +/// Improvements to speed accuracy as suggested by David Grayson. +/// \version 1.53 2016-08-14 +/// Backed out Improvements to speed accuracy from 1.52 as it did not work correctly. +/// \version 1.54 2017-01-24 +/// Fixed some warnings about unused arguments. +/// \version 1.55 2017-01-25 +/// Fixed another warning in MultiStepper.cpp +/// \version 1.56 2017-02-03 +/// Fixed minor documentation error with DIRECTION_CCW and DIRECTION_CW. Reported by David Mutterer. +/// Added link to Binpress commercial license purchasing. +/// \version 1.57 2017-03-28 +/// _direction moved to protected at the request of Rudy Ercek. +/// setMaxSpeed() and setAcceleration() now correct negative values to be positive. +/// +/// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS +// Copyright (C) 2009-2013 Mike McCauley +// $Id: AccelStepper.h,v 1.27 2016/08/14 10:26:54 mikem Exp mikem $ + +#ifndef AccelStepper_h +#define AccelStepper_h + +#include +#if ARDUINO >= 100 +#include +#else +#include +#include +#endif + +// These defs cause trouble on some versions of Arduino +#undef round + +///////////////////////////////////////////////////////////////////// +/// \class AccelStepper AccelStepper.h +/// \brief Support for stepper motors with acceleration etc. +/// +/// This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional +/// acceleration, deceleration, absolute positioning commands etc. Multiple +/// simultaneous steppers are supported, all moving +/// at different speeds and accelerations. +/// +/// \par Operation +/// This module operates by computing a step time in microseconds. The step +/// time is recomputed after each step and after speed and acceleration +/// parameters are changed by the caller. The time of each step is recorded in +/// microseconds. The run() function steps the motor once if a new step is due. +/// The run() function must be called frequently until the motor is in the +/// desired position, after which time run() will do nothing. +/// +/// \par Positioning +/// Positions are specified by a signed long integer. At +/// construction time, the current position of the motor is consider to be 0. Positive +/// positions are clockwise from the initial position; negative positions are +/// anticlockwise. The current position can be altered for instance after +/// initialization positioning. +/// +/// \par Caveats +/// This is an open loop controller: If the motor stalls or is oversped, +/// AccelStepper will not have a correct +/// idea of where the motor really is (since there is no feedback of the motor's +/// real position. We only know where we _think_ it is, relative to the +/// initial starting point). +/// +/// \par Performance +/// The fastest motor speed that can be reliably supported is about 4000 steps per +/// second at a clock frequency of 16 MHz on Arduino such as Uno etc. +/// Faster processors can support faster stepping speeds. +/// However, any speed less than that +/// down to very slow speeds (much less than one per second) are also supported, +/// provided the run() function is called frequently enough to step the motor +/// whenever required for the speed set. +/// Calling setAcceleration() is expensive, +/// since it requires a square root to be calculated. +/// +/// Gregor Christandl reports that with an Arduino Due and a simple test program, +/// he measured 43163 steps per second using runSpeed(), +/// and 16214 steps per second using run(); +class AccelStepper +{ +public: + /// \brief Symbolic names for number of pins. + /// Use this in the pins argument the AccelStepper constructor to + /// provide a symbolic name for the number of pins + /// to use. + typedef enum + { + FUNCTION = 0, ///< Use the functional interface, implementing your own driver functions (internal use only) + DRIVER = 1, ///< Stepper Driver, 2 driver pins required + FULL2WIRE = 2, ///< 2 wire stepper, 2 motor pins required + FULL3WIRE = 3, ///< 3 wire stepper, such as HDD spindle, 3 motor pins required + FULL4WIRE = 4, ///< 4 wire full stepper, 4 motor pins required + HALF3WIRE = 6, ///< 3 wire half stepper, such as HDD spindle, 3 motor pins required + HALF4WIRE = 8 ///< 4 wire half stepper, 4 motor pins required + } MotorInterfaceType; + + /// Constructor. You can have multiple simultaneous steppers, all moving + /// at different speeds and accelerations, provided you call their run() + /// functions at frequent enough intervals. Current Position is set to 0, target + /// position is set to 0. MaxSpeed and Acceleration default to 1.0. + /// The motor pins will be initialised to OUTPUT mode during the + /// constructor by a call to enableOutputs(). + /// \param[in] interface Number of pins to interface to. Integer values are + /// supported, but it is preferred to use the \ref MotorInterfaceType symbolic names. + /// AccelStepper::DRIVER (1) means a stepper driver (with Step and Direction pins). + /// If an enable line is also needed, call setEnablePin() after construction. + /// You may also invert the pins using setPinsInverted(). + /// AccelStepper::FULL2WIRE (2) means a 2 wire stepper (2 pins required). + /// AccelStepper::FULL3WIRE (3) means a 3 wire stepper, such as HDD spindle (3 pins required). + /// AccelStepper::FULL4WIRE (4) means a 4 wire stepper (4 pins required). + /// AccelStepper::HALF3WIRE (6) means a 3 wire half stepper, such as HDD spindle (3 pins required) + /// AccelStepper::HALF4WIRE (8) means a 4 wire half stepper (4 pins required) + /// Defaults to AccelStepper::FULL4WIRE (4) pins. + /// \param[in] pin1 Arduino digital pin number for motor pin 1. Defaults + /// to pin 2. For a AccelStepper::DRIVER (interface==1), + /// this is the Step input to the driver. Low to high transition means to step) + /// \param[in] pin2 Arduino digital pin number for motor pin 2. Defaults + /// to pin 3. For a AccelStepper::DRIVER (interface==1), + /// this is the Direction input the driver. High means forward. + /// \param[in] pin3 Arduino digital pin number for motor pin 3. Defaults + /// to pin 4. + /// \param[in] pin4 Arduino digital pin number for motor pin 4. Defaults + /// to pin 5. + /// \param[in] enable If this is true (the default), enableOutputs() will be called to enable + /// the output pins at construction time. + AccelStepper(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5, bool enable = true); + + /// Alternate Constructor which will call your own functions for forward and backward steps. + /// You can have multiple simultaneous steppers, all moving + /// at different speeds and accelerations, provided you call their run() + /// functions at frequent enough intervals. Current Position is set to 0, target + /// position is set to 0. MaxSpeed and Acceleration default to 1.0. + /// Any motor initialization should happen before hand, no pins are used or initialized. + /// \param[in] forward void-returning procedure that will make a forward step + /// \param[in] backward void-returning procedure that will make a backward step + AccelStepper(void (*forward)(), void (*backward)()); + + /// Set the target position. The run() function will try to move the motor (at most one step per call) + /// from the current position to the target position set by the most + /// recent call to this function. Caution: moveTo() also recalculates the speed for the next step. + /// If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo(). + /// \param[in] absolute The desired absolute position. Negative is + /// anticlockwise from the 0 position. + void moveTo(long absolute); + + /// Set the target position relative to the current position + /// \param[in] relative The desired position relative to the current position. Negative is + /// anticlockwise from the current position. + void move(long relative); + + /// Poll the motor and step it if a step is due, implementing + /// accelerations and decelerations to acheive the target position. You must call this as + /// frequently as possible, but at least once per minimum step time interval, + /// preferably in your main loop. Note that each call to run() will make at most one step, and then only when a step is due, + /// based on the current speed and the time since the last step. + /// \return true if the motor is still running to the target position. + boolean run(); + + /// Poll the motor and step it if a step is due, implementing a constant + /// speed as set by the most recent call to setSpeed(). You must call this as + /// frequently as possible, but at least once per step interval, + /// \return true if the motor was stepped. + boolean runSpeed(); + + /// Sets the maximum permitted speed. The run() function will accelerate + /// up to the speed set by this function. + /// Caution: the maximum speed achievable depends on your processor and clock speed. + /// \param[in] speed The desired maximum speed in steps per second. Must + /// be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may + /// Result in non-linear accelerations and decelerations. + void setMaxSpeed(float speed); + + /// returns the maximum speed configured for this stepper + /// that was previously set by setMaxSpeed(); + /// \return The currently configured maximum speed + float maxSpeed(); + + /// Sets the acceleration/deceleration rate. + /// \param[in] acceleration The desired acceleration in steps per second + /// per second. Must be > 0.0. This is an expensive call since it requires a square + /// root to be calculated. Dont call more ofthen than needed + void setAcceleration(float acceleration); + + /// Sets the desired constant speed for use with runSpeed(). + /// \param[in] speed The desired constant speed in steps per + /// second. Positive is clockwise. Speeds of more than 1000 steps per + /// second are unreliable. Very slow speeds may be set (eg 0.00027777 for + /// once per hour, approximately. Speed accuracy depends on the Arduino + /// crystal. Jitter depends on how frequently you call the runSpeed() function. + void setSpeed(float speed); + + /// The most recently set speed + /// \return the most recent speed in steps per second + float speed(); + + /// The distance from the current position to the target position. + /// \return the distance from the current position to the target position + /// in steps. Positive is clockwise from the current position. + long distanceToGo(); + + /// The most recently set target position. + /// \return the target position + /// in steps. Positive is clockwise from the 0 position. + long targetPosition(); + + /// The currently motor position. + /// \return the current motor position + /// in steps. Positive is clockwise from the 0 position. + long currentPosition(); + + /// Resets the current position of the motor, so that wherever the motor + /// happens to be right now is considered to be the new 0 position. Useful + /// for setting a zero position on a stepper after an initial hardware + /// positioning move. + /// Has the side effect of setting the current motor speed to 0. + /// \param[in] position The position in steps of wherever the motor + /// happens to be right now. + void setCurrentPosition(long position); + + /// Moves the motor (with acceleration/deceleration) + /// to the target position and blocks until it is at + /// position. Dont use this in event loops, since it blocks. + void runToPosition(); + + /// Runs at the currently selected speed until the target position is reached + /// Does not implement accelerations. + /// \return true if it stepped + boolean runSpeedToPosition(); + + /// Moves the motor (with acceleration/deceleration) + /// to the new target position and blocks until it is at + /// position. Dont use this in event loops, since it blocks. + /// \param[in] position The new target position. + void runToNewPosition(long position); + + /// Sets a new target position that causes the stepper + /// to stop as quickly as possible, using the current speed and acceleration parameters. + void stop(); + + /// Disable motor pin outputs by setting them all LOW + /// Depending on the design of your electronics this may turn off + /// the power to the motor coils, saving power. + /// This is useful to support Arduino low power modes: disable the outputs + /// during sleep and then reenable with enableOutputs() before stepping + /// again. + /// If the enable Pin is defined, sets it to OUTPUT mode and clears the pin to disabled. + virtual void disableOutputs(); + + /// Enable motor pin outputs by setting the motor pins to OUTPUT + /// mode. Called automatically by the constructor. + /// If the enable Pin is defined, sets it to OUTPUT mode and sets the pin to enabled. + virtual void enableOutputs(); + + /// Sets the minimum pulse width allowed by the stepper driver. The minimum practical pulse width is + /// approximately 20 microseconds. Times less than 20 microseconds + /// will usually result in 20 microseconds or so. + /// \param[in] minWidth The minimum pulse width in microseconds. + void setMinPulseWidth(unsigned int minWidth); + + /// Sets the enable pin number for stepper drivers. + /// 0xFF indicates unused (default). + /// Otherwise, if a pin is set, the pin will be turned on when + /// enableOutputs() is called and switched off when disableOutputs() + /// is called. + /// \param[in] enablePin Arduino digital pin number for motor enable + /// \sa setPinsInverted + void setEnablePin(uint8_t enablePin = 0xff); + + /// Sets the inversion for stepper driver pins + /// \param[in] directionInvert True for inverted direction pin, false for non-inverted + /// \param[in] stepInvert True for inverted step pin, false for non-inverted + /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted + void setPinsInverted(bool directionInvert = false, bool stepInvert = false, bool enableInvert = false); + + /// Sets the inversion for 2, 3 and 4 wire stepper pins + /// \param[in] pin1Invert True for inverted pin1, false for non-inverted + /// \param[in] pin2Invert True for inverted pin2, false for non-inverted + /// \param[in] pin3Invert True for inverted pin3, false for non-inverted + /// \param[in] pin4Invert True for inverted pin4, false for non-inverted + /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted + void setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert); + + /// Checks to see if the motor is currently running to a target + /// \return true if the speed is not zero or not at the target position + bool isRunning(); + +protected: + + /// \brief Direction indicator + /// Symbolic names for the direction the motor is turning + typedef enum + { + DIRECTION_CCW = 0, ///< Counter-Clockwise + DIRECTION_CW = 1 ///< Clockwise + } Direction; + + /// Forces the library to compute a new instantaneous speed and set that as + /// the current speed. It is called by + /// the library: + /// \li after each step + /// \li after change to maxSpeed through setMaxSpeed() + /// \li after change to acceleration through setAcceleration() + /// \li after change to target position (relative or absolute) through + /// move() or moveTo() + void computeNewSpeed(); + + /// Low level function to set the motor output pins + /// bit 0 of the mask corresponds to _pin[0] + /// bit 1 of the mask corresponds to _pin[1] + /// You can override this to impment, for example serial chip output insted of using the + /// output pins directly + virtual void setOutputPins(uint8_t mask); + + /// Called to execute a step. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default calls step1(), step2(), step4() or step8() depending on the + /// number of pins defined for the stepper. + /// \param[in] step The current step phase number (0 to 7) + virtual void step(long step); + + /// Called to execute a step using stepper functions (pins = 0) Only called when a new step is + /// required. Calls _forward() or _backward() to perform the step + /// \param[in] step The current step phase number (0 to 7) + virtual void step0(long step); + + /// Called to execute a step on a stepper driver (ie where pins == 1). Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of Step pin1 to step, + /// and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond + /// which is the minimum STEP pulse width for the 3967 driver. + /// \param[in] step The current step phase number (0 to 7) + virtual void step1(long step); + + /// Called to execute a step on a 2 pin motor. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1 and pin2 + /// \param[in] step The current step phase number (0 to 7) + virtual void step2(long step); + + /// Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1, pin2, + /// pin3 + /// \param[in] step The current step phase number (0 to 7) + virtual void step3(long step); + + /// Called to execute a step on a 4 pin motor. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1, pin2, + /// pin3, pin4. + /// \param[in] step The current step phase number (0 to 7) + virtual void step4(long step); + + /// Called to execute a step on a 3 pin motor, such as HDD spindle. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1, pin2, + /// pin3 + /// \param[in] step The current step phase number (0 to 7) + virtual void step6(long step); + + /// Called to execute a step on a 4 pin half-steper motor. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1, pin2, + /// pin3, pin4. + /// \param[in] step The current step phase number (0 to 7) + virtual void step8(long step); + + /// Current direction motor is spinning in + /// Protected because some peoples subclasses need it to be so + boolean _direction; // 1 == CW + +private: + /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a + /// bipolar, and 4 pins is a unipolar. + uint8_t _interface; // 0, 1, 2, 4, 8, See MotorInterfaceType + + /// Arduino pin number assignments for the 2 or 4 pins required to interface to the + /// stepper motor or driver + uint8_t _pin[4]; + + /// Whether the _pins is inverted or not + uint8_t _pinInverted[4]; + + /// The current absolution position in steps. + long _currentPos; // Steps + + /// The target position in steps. The AccelStepper library will move the + /// motor from the _currentPos to the _targetPos, taking into account the + /// max speed, acceleration and deceleration + long _targetPos; // Steps + + /// The current motos speed in steps per second + /// Positive is clockwise + float _speed; // Steps per second + + /// The maximum permitted speed in steps per second. Must be > 0. + float _maxSpeed; + + /// The acceleration to use to accelerate or decelerate the motor in steps + /// per second per second. Must be > 0 + float _acceleration; + float _sqrt_twoa; // Precomputed sqrt(2*_acceleration) + + /// The current interval between steps in microseconds. + /// 0 means the motor is currently stopped with _speed == 0 + unsigned long _stepInterval; + + /// The last step time in microseconds + unsigned long _lastStepTime; + + /// The minimum allowed pulse width in microseconds + unsigned int _minPulseWidth; + + /// Is the direction pin inverted? + ///bool _dirInverted; /// Moved to _pinInverted[1] + + /// Is the step pin inverted? + ///bool _stepInverted; /// Moved to _pinInverted[0] + + /// Is the enable pin inverted? + bool _enableInverted; + + /// Enable pin for stepper driver, or 0xFF if unused. + uint8_t _enablePin; + + /// The pointer to a forward-step procedure + void (*_forward)(); + + /// The pointer to a backward-step procedure + void (*_backward)(); + + /// The step counter for speed calculations + long _n; + + /// Initial step size in microseconds + float _c0; + + /// Last step size in microseconds + float _cn; + + /// Min step size in microseconds based on maxSpeed + float _cmin; // at max speed + +}; + +/// @example Random.pde +/// Make a single stepper perform random changes in speed, position and acceleration + +/// @example Overshoot.pde +/// Check overshoot handling +/// which sets a new target position and then waits until the stepper has +/// achieved it. This is used for testing the handling of overshoots + +/// @example MultipleSteppers.pde +/// Shows how to multiple simultaneous steppers +/// Runs one stepper forwards and backwards, accelerating and decelerating +/// at the limits. Runs other steppers at the same time + +/// @example ConstantSpeed.pde +/// Shows how to run AccelStepper in the simplest, +/// fixed speed mode with no accelerations + +/// @example Blocking.pde +/// Shows how to use the blocking call runToNewPosition +/// Which sets a new target position and then waits until the stepper has +/// achieved it. + +/// @example AFMotor_MultiStepper.pde +/// Control both Stepper motors at the same time with different speeds +/// and accelerations. + +/// @example AFMotor_ConstantSpeed.pde +/// Shows how to run AccelStepper in the simplest, +/// fixed speed mode with no accelerations + +/// @example ProportionalControl.pde +/// Make a single stepper follow the analog value read from a pot or whatever +/// The stepper will move at a constant speed to each newly set posiiton, +/// depending on the value of the pot. + +/// @example Bounce.pde +/// Make a single stepper bounce from one limit to another, observing +/// accelrations at each end of travel + +/// @example Quickstop.pde +/// Check stop handling. +/// Calls stop() while the stepper is travelling at full speed, causing +/// the stepper to stop as quickly as possible, within the constraints of the +/// current acceleration. + +/// @example MotorShield.pde +/// Shows how to use AccelStepper to control a 3-phase motor, such as a HDD spindle motor +/// using the Adafruit Motor Shield http://www.ladyada.net/make/mshield/index.html. + +/// @example DualMotorShield.pde +/// Shows how to use AccelStepper to control 2 x 2 phase steppers using the +/// Itead Studio Arduino Dual Stepper Motor Driver Shield +/// model IM120417015 + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/BLEStream.cpp b/libraries/ConfigurableFirmata/src/utility/BLEStream.cpp new file mode 100644 index 0000000..ea50840 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/BLEStream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in BLEStream.h to avoid linker issues. + */ diff --git a/libraries/ConfigurableFirmata/src/utility/BLEStream.h b/libraries/ConfigurableFirmata/src/utility/BLEStream.h new file mode 100644 index 0000000..56b3648 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/BLEStream.h @@ -0,0 +1,243 @@ +/* + BLEStream.h + + Based on BLESerial.cpp by Voita Molda + https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h + + Last updated April 4th, 2016 + */ + +#ifndef _BLE_STREAM_H_ +#define _BLE_STREAM_H_ + +#include +#if defined(_VARIANT_ARDUINO_101_X_) +#include +#define _MAX_ATTR_DATA_LEN_ BLE_MAX_ATTR_DATA_LEN +#else +#include +#define _MAX_ATTR_DATA_LEN_ BLE_ATTRIBUTE_MAX_VALUE_LENGTH +#endif + +#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80 +#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer + +// #define BLE_SERIAL_DEBUG + +class BLEStream : public BLEPeripheral, public Stream +{ + public: + BLEStream(unsigned char req = 0, unsigned char rdy = 0, unsigned char rst = 0); + + void begin(...); + bool poll(); + void end(); + void setFlushInterval(int); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t byte); + using Print::write; + virtual operator bool(); + + private: + bool _connected; + unsigned long _flushed; + int _flushInterval; + static BLEStream* _instance; + + size_t _rxHead; + size_t _rxTail; + size_t _rxCount() const; + unsigned char _rxBuffer[256]; + size_t _txCount; + unsigned char _txBuffer[_MAX_ATTR_DATA_LEN_]; + + BLEService _uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); + BLEDescriptor _uartNameDescriptor = BLEDescriptor("2901", "UART"); + BLECharacteristic _rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, _MAX_ATTR_DATA_LEN_); + BLEDescriptor _rxNameDescriptor = BLEDescriptor("2901", "RX - Receive Data (Write)"); + BLECharacteristic _txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, _MAX_ATTR_DATA_LEN_); + BLEDescriptor _txNameDescriptor = BLEDescriptor("2901", "TX - Transfer Data (Notify)"); + + void _received(const unsigned char* data, size_t size); + static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic); +}; + + +/* + * BLEStream.cpp + * Copied here as a hack to avoid having to install the BLEPeripheral libarary even if it's + * not needed. + */ + +BLEStream* BLEStream::_instance = NULL; + +BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) : +#if defined(_VARIANT_ARDUINO_101_X_) + BLEPeripheral() +#else + BLEPeripheral(req, rdy, rst) +#endif +{ + this->_txCount = 0; + this->_rxHead = this->_rxTail = 0; + this->_flushed = 0; + this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL; + BLEStream::_instance = this; + + addAttribute(this->_uartService); + addAttribute(this->_uartNameDescriptor); + setAdvertisedServiceUuid(this->_uartService.uuid()); + addAttribute(this->_rxCharacteristic); + addAttribute(this->_rxNameDescriptor); + this->_rxCharacteristic.setEventHandler(BLEWritten, BLEStream::_received); + addAttribute(this->_txCharacteristic); + addAttribute(this->_txNameDescriptor); +} + +void BLEStream::begin(...) +{ + BLEPeripheral::begin(); +#ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLEStream::begin()")); +#endif +} + +bool BLEStream::poll() +{ + // BLEPeripheral::poll is called each time connected() is called + this->_connected = BLEPeripheral::connected(); + if (millis() > this->_flushed + this->_flushInterval) { + flush(); + } + return this->_connected; +} + +void BLEStream::end() +{ + this->_rxCharacteristic.setEventHandler(BLEWritten, (void(*)(BLECentral&, BLECharacteristic&))NULL); + this->_rxHead = this->_rxTail = 0; + flush(); + BLEPeripheral::disconnect(); +} + +int BLEStream::available(void) +{ +// BLEPeripheral::poll only calls delay(1) in CurieBLE so skipping it here to avoid the delay +#ifndef _VARIANT_ARDUINO_101_X_ + // TODO Need to do more testing to determine if all of these calls to BLEPeripheral::poll are + // actually necessary. Seems to run fine without them, but only minimal testing so far. + BLEPeripheral::poll(); +#endif + int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer); +#ifdef BLE_SERIAL_DEBUG + if (retval > 0) { + Serial.print(F("BLEStream::available() = ")); + Serial.println(retval); + } +#endif + return retval; +} + +int BLEStream::peek(void) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_rxTail == this->_rxHead) return -1; + uint8_t byte = this->_rxBuffer[this->_rxTail]; +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::peek() = 0x")); + Serial.println(byte, HEX); +#endif + return byte; +} + +int BLEStream::read(void) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_rxTail == this->_rxHead) return -1; + this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer); + uint8_t byte = this->_rxBuffer[this->_rxTail]; +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::read() = 0x")); + Serial.println(byte, HEX); +#endif + return byte; +} + +void BLEStream::flush(void) +{ + if (this->_txCount == 0) return; +#ifndef _VARIANT_ARDUINO_101_X_ + // ensure there are available packets before sending + while(!this->_txCharacteristic.canNotify()) { + BLEPeripheral::poll(); + } +#endif + this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); + this->_flushed = millis(); + this->_txCount = 0; +#ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLEStream::flush()")); +#endif +} + +size_t BLEStream::write(uint8_t byte) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_txCharacteristic.subscribed() == false) return 0; + this->_txBuffer[this->_txCount++] = byte; + if (this->_txCount == sizeof(this->_txBuffer)) flush(); +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::write( 0x")); + Serial.print(byte, HEX); + Serial.println(F(") = 1")); +#endif + return 1; +} + +BLEStream::operator bool() +{ + bool retval = this->_connected = BLEPeripheral::connected(); +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::operator bool() = ")); + Serial.println(retval); +#endif + return retval; +} + +void BLEStream::setFlushInterval(int interval) +{ + if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) { + this->_flushInterval = interval; + } +} + +void BLEStream::_received(const unsigned char* data, size_t size) +{ + for (size_t i = 0; i < size; i++) { + this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer); + this->_rxBuffer[this->_rxHead] = data[i]; + } +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::received(")); + for (int i = 0; i < size; i++) Serial.print(data[i], HEX); + Serial.println(F(")")); +#endif +} + +void BLEStream::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic) +{ + BLEStream::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength()); +} + + +#endif // _BLE_STREAM_H_ diff --git a/libraries/ConfigurableFirmata/src/utility/Boards.h b/libraries/ConfigurableFirmata/src/utility/Boards.h new file mode 100644 index 0000000..762a47d --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/Boards.h @@ -0,0 +1,697 @@ +/* + Boards.h - Hardware Abstraction Layer for Firmata library + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-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 December 23rd, 2016 +*/ + +#ifndef Firmata_Boards_h +#define Firmata_Boards_h + +#include + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" // for digitalRead, digitalWrite, etc +#else +#include "WProgram.h" +#endif + +// Normally Servo.h must be included before Firmata.h (which then includes +// this file). If Servo.h wasn't included, this allows the code to still +// compile, but without support for any Servos. Hopefully that's what the +// user intended by not including Servo.h +#ifndef MAX_SERVOS +#define MAX_SERVOS 0 +#endif + +/* + Firmata Hardware Abstraction Layer + +Firmata is built on top of the hardware abstraction functions of Arduino, +specifically digitalWrite, digitalRead, analogWrite, analogRead, and +pinMode. While these functions offer simple integer pin numbers, Firmata +needs more information than is provided by Arduino. This file provides +all other hardware specific details. To make Firmata support a new board, +only this file should require editing. + +The key concept is every "pin" implemented by Firmata may be mapped to +any pin as implemented by Arduino. Usually a simple 1-to-1 mapping is +best, but such mapping should not be assumed. This hardware abstraction +layer allows Firmata to implement any number of pins which map onto the +Arduino implemented pins in almost any arbitrary way. + + +General Constants: + +These constants provide basic information Firmata requires. + +TOTAL_PINS: The total number of pins Firmata implemented by Firmata. + Usually this will match the number of pins the Arduino functions + implement, including any pins pins capable of analog or digital. + However, Firmata may implement any number of pins. For example, + on Arduino Mini with 8 analog inputs, 6 of these may be used + for digital functions, and 2 are analog only. On such boards, + Firmata can implement more pins than Arduino's pinMode() + function, in order to accommodate those special pins. The + Firmata protocol supports a maximum of 128 pins, so this + constant must not exceed 128. + +TOTAL_ANALOG_PINS: The total number of analog input pins implemented. + The Firmata protocol allows up to 16 analog inputs, accessed + using offsets 0 to 15. Because Firmata presents the analog + inputs using different offsets than the actual pin numbers + (a legacy of Arduino's analogRead function, and the way the + analog input capable pins are physically labeled on all + Arduino boards), the total number of analog input signals + must be specified. 16 is the maximum. + +VERSION_BLINK_PIN: When Firmata starts up, it will blink the version + number. This constant is the Arduino pin number where a + LED is connected. + + +Pin Mapping Macros: + +These macros provide the mapping between pins as implemented by +Firmata protocol and the actual pin numbers used by the Arduino +functions. Even though such mappings are often simple, pin +numbers received by Firmata protocol should always be used as +input to these macros, and the result of the macro should be +used with any Arduino function. + +When Firmata is extended to support a new pin mode or feature, +a pair of macros should be added and used for all hardware +access. For simple 1:1 mapping, these macros add no actual +overhead, yet their consistent use allows source code which +uses them consistently to be easily adapted to all other boards +with different requirements. + +IS_PIN_XXXX(pin): The IS_PIN macros resolve to true or non-zero + if a pin as implemented by Firmata corresponds to a pin + that actually implements the named feature. + +PIN_TO_XXXX(pin): The PIN_TO macros translate pin numbers as + implemented by Firmata to the pin numbers needed as inputs + to the Arduino functions. The corresponding IS_PIN macro + should always be tested before using a PIN_TO macro, so + these macros only need to handle valid Firmata pin + numbers for the named feature. + + +Port Access Inline Funtions: + +For efficiency, Firmata protocol provides access to digital +input and output pins grouped by 8 bit ports. When these +groups of 8 correspond to actual 8 bit ports as implemented +by the hardware, these inline functions can provide high +speed direct port access. Otherwise, a default implementation +using 8 calls to digitalWrite or digitalRead is used. + +When porting Firmata to a new board, it is recommended to +use the default functions first and focus only on the constants +and macros above. When those are working, if optimized port +access is desired, these inline functions may be extended. +The recommended approach defines a symbol indicating which +optimization to use, and then conditional complication is +used within these functions. + +readPort(port, bitmask): Read an 8 bit port, returning the value. + port: The port number, Firmata pins port*8 to port*8+7 + bitmask: The actual pins to read, indicated by 1 bits. + +writePort(port, value, bitmask): Write an 8 bit port. + port: The port number, Firmata pins port*8 to port*8+7 + value: The 8 bit value to write + bitmask: The actual pins to write, indicated by 1 bits. +*/ + +/*============================================================================== + * Board Specific Configuration + *============================================================================*/ + +#ifndef digitalPinHasPWM +#define digitalPinHasPWM(p) IS_PIN_DIGITAL(p) +#endif + +#if defined(digitalPinToInterrupt) && defined(NOT_AN_INTERRUPT) +#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT) +#else +#define IS_PIN_INTERRUPT(p) (0) +#endif + +// Arduino Duemilanove, Diecimila, and NG +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) +#if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6 +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#else +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 22 // 14 digital + 8 analog +#endif +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Wiring (and board) +#elif defined(WIRING) +#define VERSION_BLINK_PIN WLED +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= FIRST_ANALOG_PIN && (p) < (FIRST_ANALOG_PIN+TOTAL_ANALOG_PINS)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - FIRST_ANALOG_PIN) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// old Arduinos +#elif defined(__AVR_ATmega8__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Arduino Mega +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TOTAL_ANALOG_PINS 16 +#define TOTAL_PINS 70 // 54 digital + 16 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino DUE +#elif defined(__SAM3X8E__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 66 // 54 digital + 12 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71 +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino/Genuino MKR1000 +#elif defined(ARDUINO_SAMD_MKR1000) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 22 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21) && !IS_PIN_SERIAL(p)) +#define IS_PIN_ANALOG(p) ((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino MKRZero +#elif defined(ARDUINO_SAMD_MKRZERO) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 34 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog + 3 usb + 1 aref + 5 sd + 1 bottom pad + 1 led + 1 battery adc +#define IS_PIN_DIGITAL(p) ((((p) >= 0 && (p) <= 21) || (p) == 32) && !IS_PIN_SERIAL(p)) +#define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 33) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino Zero +// Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro +// Arduino M0 Pro does not properly map pins to the board labeled pin numbers +#elif defined(_VARIANT_ARDUINO_ZERO_) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi +#define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins +#define VERSION_BLINK_PIN LED_BUILTIN +//#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h +//#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2 +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino 101 +#elif defined(_VARIANT_ARDUINO_101_X_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog +#define VERSION_BLINK_PIN LED_BUILTIN +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9 +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Teensy 1.0 +#elif defined(__AVR_AT90USB162__) +#define TOTAL_ANALOG_PINS 0 +#define TOTAL_PINS 21 // 21 digital + no analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) (0) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) (0) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (0) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 2.0 +#elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 25 // 11 digital + 12 analog +#define VERSION_BLINK_PIN 11 +#define PIN_SERIAL1_RX 7 +#define PIN_SERIAL1_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 5 || (p) == 6) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 3.5 and 3.6 +// reference: https://github.com/PaulStoffregen/cores/blob/master/teensy3/pins_arduino.h +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define TOTAL_ANALOG_PINS 27 // 3.5 has 27 and 3.6 has 25 +#define TOTAL_PINS 70 // 43 digital + 21 analog-digital + 6 analog (64-69) +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +// The following 2 UARTs are not yet available via SerialFirmata +#define PIN_SERIAL4_RX 31 +#define PIN_SERIAL5_TX 32 +#define PIN_SERIAL6_RX 34 +#define PIN_SERIAL6_TX 33 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 63) +#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 31 && (p) <= 39) || ((p) >= 49 && (p) <= 50) || ((p) >= 64 && (p) <= 69)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +// A0-A9 = D14-D23; A12-A20 = D31-D39; A23-A24 = D49-D50; A10-A11 = D64-D65; A21-A22 = D66-D67; A25-A26 = D68-D69 +#define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (((p) <= 39) ? (p) - 19 : (((p) <= 50) ? (p) - 26 : (((p) <= 65) ? (p) - 55 : (((p) <= 67) ? (p) - 45 : (p) - 43))))) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 3.0, 3.1 and 3.2 +#elif defined(__MK20DX128__) || defined(__MK20DX256__) +#define TOTAL_ANALOG_PINS 14 +#define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33) +#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy-LC +#elif defined(__MKL26Z64__) +#define TOTAL_ANALOG_PINS 13 +#define TOTAL_PINS 27 // 27 digital + 13 analog-digital +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26) +#define IS_PIN_ANALOG(p) ((p) >= 14) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy++ 1.0 and 2.0 +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 46 // 38 digital + 8 analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 0 || (p) == 1) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 38) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Leonardo +#elif defined(__AVR_ATmega32U4__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header) +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 2 || (p) == 3) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p) - 18 +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Intel Galileo Board (gen 1 and 2) and Intel Edison +#elif defined(ARDUINO_LINUX) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// RedBearLab BLE Nano with factory switch settings (S1 - S10) +#elif defined(BLE_NANO) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 15 // 9 digital + 3 analog +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 14) +#define IS_PIN_ANALOG(p) ((p) == 8 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 12 || (p) == 14) //A0~A5 +#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) <= 7) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == CS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 8) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Pinoccio Scout +// Note: digital pins 9-16 are usable but not labeled on the board numerically. +// SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16 +#elif defined(ARDUINO_PINOCCIO) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS NUM_DIGITAL_PINS // 32 +#define VERSION_BLINK_PIN 23 +#define PIN_SERIAL1_RX 13 +#define PIN_SERIAL1_TX 14 +#define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31)) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Sanguino +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 32 // 24 digital + 8 analog +#define VERSION_BLINK_PIN 0 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 16 || (p) == 17) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Illuminato +#elif defined(__AVR_ATmega645__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 42 // 36 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 4 || (p) == 5) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 36) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// ESP8266 +// note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO +#elif defined(ESP8266) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS A0 + NUM_ANALOG_INPUTS +#define PIN_SERIAL_RX 3 +#define PIN_SERIAL_TX 1 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0)) +#define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - A0) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) +#define DEFAULT_PWM_RESOLUTION 10 + + +// anything else +#else +#error "Please edit Boards.h with a hardware abstraction for this board" +#endif + +// as long this is not defined for all boards: +#ifndef IS_PIN_SPI +#define IS_PIN_SPI(p) (0) +#endif + +#ifndef IS_PIN_SERIAL +#define IS_PIN_SERIAL(p) 0 +#endif + +#ifndef DEFAULT_PWM_RESOLUTION +#define DEFAULT_PWM_RESOLUTION 8 +#endif + +/*============================================================================== + * readPort() - Read an 8 bit port + *============================================================================*/ + +static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char readPort(byte port, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1 + if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask; + if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask; + return 0; +#else + unsigned char out = 0, pin = port * 8; + if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01; + if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02; + if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04; + if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08; + if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10; + if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20; + if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40; + if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80; + return out; +#endif +} + +/*============================================================================== + * writePort() - Write an 8 bit port, only touch pins specified by a bitmask + *============================================================================*/ + +static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char writePort(byte port, byte value, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) { + bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins + byte valD = value & bitmask; + byte maskD = ~bitmask; + cli(); + PORTD = (PORTD & maskD) | valD; + sei(); + } else if (port == 1) { + byte valB = (value & bitmask) & 0x3F; + byte valC = (value & bitmask) >> 6; + byte maskB = ~(bitmask & 0x3F); + byte maskC = ~((bitmask & 0xC0) >> 6); + cli(); + PORTB = (PORTB & maskB) | valB; + PORTC = (PORTC & maskC) | valC; + sei(); + } else if (port == 2) { + bitmask = bitmask & 0x0F; + byte valC = (value & bitmask) << 2; + byte maskC = ~(bitmask << 2); + cli(); + PORTC = (PORTC & maskC) | valC; + sei(); + } + return 1; +#else + byte pin = port * 8; + if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01)); + if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02)); + if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04)); + if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08)); + if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10)); + if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20)); + if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40)); + if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80)); + return 1; +#endif +} + + +#ifndef TOTAL_PORTS +#define TOTAL_PORTS ((TOTAL_PINS + 7) / 8) +#endif + + +#endif /* Firmata_Boards_h */ diff --git a/libraries/ConfigurableFirmata/src/utility/FirmataStepper.cpp b/libraries/ConfigurableFirmata/src/utility/FirmataStepper.cpp new file mode 100644 index 0000000..8632d1e --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/FirmataStepper.cpp @@ -0,0 +1,403 @@ +/** + FirmataStepper is a simple non-blocking stepper motor library + for 2 and 4 wire bipolar and unipolar stepper motor drive circuits + as well as EasyDriver (http://schmalzhaus.com/EasyDriver/) and + other step + direction drive circuits. + + FirmataStepper (0.3) by Jeff Hoefs + + EasyDriver support based on modifications by Chris Coleman + + Acceleration / Deceleration algorithms and code based on: + app note: http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf + source code: http://www.atmel.com/dyn/resources/prod_documents/AVR446.zip + + stepMotor function based on Stepper.cpp Stepper library for + Wiring/Arduino created by Tom Igoe, Sebastian Gassner + David Mellis and Noah Shibley. + + Relevant notes from Stepper.cpp: + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +#include "FirmataStepper.h" + +/** + * Constructor. + * + * Configure a stepper for an EasyDriver or other step + direction interface or + * configure a bipolar or unipolar stepper motor for 2 wire drive mode. + * Configure a bipolar or unipolar stepper for 4 wire drive mode. + * @param interface Lower 3 bits: + * The interface type: FirmataStepper::DRIVER, + * FirmataStepper::TWO_WIRE or FirmataStepper::FOUR_WIRE + * Upper 4 bits: Any bits set = use 2 microsecond delay + * @param steps_per_rev The number of steps to make 1 revolution. + * @param first_pin The direction pin (EasyDriver) or the pin attached to the + * 1st motor coil (2 wire drive mode) + * @param second_pin The step pin (EasyDriver) or the pin attached to the 2nd + * motor coil (2 wire drive mode) + * @param motor_pin_3 The pin attached to the 3rd motor coil + * @param motor_pin_4 The pin attached to the 4th motor coil + */ +FirmataStepper::FirmataStepper(byte interface, + int steps_per_rev, + byte pin1, + byte pin2, + byte pin3, + byte pin4) +{ + this->step_number = 0; // which step the motor is on + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->steps_per_rev = steps_per_rev; // total number of steps for this motor + this->running = false; + this->interface = interface & 0x0F; // default to Easy Stepper (or other step + direction driver) + + // could update this in future to support additional delays if necessary + if (((interface & 0xF0) >> 4) > 0) { + // high current driver + this->stepDelay = 2; // microseconds + } else { + this->stepDelay = 1; // microseconds + } + + this->motor_pin_1 = pin1; + this->motor_pin_2 = pin2; + this->dir_pin = pin1; + this->step_pin = pin2; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + + if (this->interface == FirmataStepper::FOUR_WIRE) { + this->motor_pin_3 = pin3; + this->motor_pin_4 = pin4; + pinMode(this->motor_pin_3, OUTPUT); + pinMode(this->motor_pin_4, OUTPUT); + } + + this->alpha = PI_2 / this->steps_per_rev; + this->at_x100 = (long)(this->alpha * T1_FREQ * 100); + this->ax20000 = (long)(this->alpha * 20000); + this->alpha_x2 = this->alpha * 2; +} + +/** + * Move the stepper a given number of steps at the specified + * speed (rad/sec), acceleration (rad/sec^2) and deceleration (rad/sec^2). + * + * @param steps_to_move The number ofsteps to move the motor + * @param speed Max speed in 0.01*rad/sec + * @param accel [optional] Acceleration in 0.01*rad/sec^2 + * @param decel [optional] Deceleration in 0.01*rad/sec^2 + */ +void FirmataStepper::setStepsToMove(long steps_to_move, int speed, int accel, int decel) +{ + unsigned long maxStepLimit; + unsigned long accelerationLimit; + + this->step_number = 0; + this->lastAccelDelay = 0; + this->stepCount = 0; + this->rest = 0; + + // ensure steps_to_move is always a positive value + if (steps_to_move < 0) { + this->direction = FirmataStepper::CCW; + steps_to_move = -steps_to_move; + } else { + this->direction = FirmataStepper::CW; + } + + this->steps_to_move = steps_to_move; + + // set max speed limit, by calc min_delay + // min_delay = (alpha / tt)/w + this->min_delay = this->at_x100 / speed; + + // if acceleration or deceleration are not defined + // start in RUN state and do no decelerate + if (accel == 0 || decel == 0) { + this->step_delay = this->min_delay; + + this->decel_start = steps_to_move; + this->run_state = FirmataStepper::RUN; + this->accel_count = 0; + this->running = true; + + return; + } + + // if only moving 1 step + if (steps_to_move == 1) { + + // move one step + this->accel_count = -1; + this->run_state = FirmataStepper::DECEL; + + this->step_delay = this->min_delay; + this->running = true; + } else if (steps_to_move != 0) { + // set initial step delay + // step_delay = 1/tt * sqrt(2*alpha/accel) + // step_delay = ( tfreq*0.676/100 )*100 * sqrt( (2*alpha*10000000000) / (accel*100) )/10000 + this->step_delay = (long)((T1_FREQ_148 * sqrt(alpha_x2 / accel)) * 1000); + + // find out after how many steps does the speed hit the max speed limit. + // maxSpeedLimit = speed^2 / (2*alpha*accel) + maxStepLimit = (long)speed * speed / (long)(((long)this->ax20000 * accel) / 100); + + // if we hit max spped limit before 0.5 step it will round to 0. + // but in practice we need to move at least 1 step to get any speed at all. + if (maxStepLimit == 0) { + maxStepLimit = 1; + } + + // find out after how many steps we must start deceleration. + // n1 = (n1+n2)decel / (accel + decel) + accelerationLimit = (long)((steps_to_move * decel) / (accel + decel)); + + // we must accelerate at least 1 step before we can start deceleration + if (accelerationLimit == 0) { + accelerationLimit = 1; + } + + // use the limit we hit first to calc decel + if (accelerationLimit <= maxStepLimit) { + this->decel_val = accelerationLimit - steps_to_move; + } else { + this->decel_val = -(long)(maxStepLimit * accel) / decel; + } + + // we must decelerate at least 1 step to stop + if (this->decel_val == 0) { + this->decel_val = -1; + } + + // find step to start deceleration + this->decel_start = steps_to_move + this->decel_val; + + // if the max speed is so low that we don't need to go via acceleration state. + if (this->step_delay <= this->min_delay) { + this->step_delay = this->min_delay; + this->run_state = FirmataStepper::RUN; + } else { + this->run_state = FirmataStepper::ACCEL; + } + + // reset counter + this->accel_count = 0; + this->running = true; + } +} + +bool FirmataStepper::update() +{ + bool done = false; + unsigned long newStepDelay = 0; + + unsigned long curTimeVal = micros(); + unsigned long timeDiff = curTimeVal - this->last_step_time; + + if (this->running == true && + (timeDiff >= this->step_delay || this->run_state == FirmataStepper::STOP)) { + + this->last_step_time = curTimeVal; + + switch (this->run_state) { + case FirmataStepper::STOP: + this->stepCount = 0; + this->rest = 0; + done = true; + this->running = false; + break; + + case FirmataStepper::ACCEL: + updateStepPosition(); + this->stepCount++; + this->accel_count++; + newStepDelay = this->step_delay - + (((2 * (long)this->step_delay) + this->rest) / (4 * this->accel_count + 1)); + // Keep track of remainder from new_step-delay calculation to incrase accurancy + this->rest = ((2 * (long)this->step_delay) + this->rest) % (4 * this->accel_count + 1); + + // check if we should start deceleration + if (this->stepCount >= this->decel_start) { + this->accel_count = this->decel_val; + this->run_state = FirmataStepper::DECEL; + // check if we hit max speed + } else if (newStepDelay <= this->min_delay) { + this->lastAccelDelay = newStepDelay; + newStepDelay = this->min_delay; + this->rest = 0; + this->run_state = FirmataStepper::RUN; + } + break; + + case FirmataStepper::RUN: + updateStepPosition(); + this->stepCount++; + newStepDelay = this->min_delay; + + // if no accel or decel was specified, go directly to STOP state + if ((long)this->stepCount >= this->steps_to_move) { + this->run_state = FirmataStepper::STOP; + // check if we should start deceleration + } else if (this->stepCount >= this->decel_start) { + this->accel_count = this->decel_val; + // start deceleration with same delay that accel ended with + newStepDelay = this->lastAccelDelay; + this->run_state = FirmataStepper::DECEL; + } + break; + + case FirmataStepper::DECEL: + updateStepPosition(); + this->stepCount++; + this->accel_count++; + + newStepDelay = this->step_delay - + (((2 * (long)this->step_delay) + this->rest) / (4 * this->accel_count + 1)); + this->rest = ((2 * (long)this->step_delay) + this->rest) % (4 * this->accel_count + 1); + + // check if we ar at the last step + if (this->accel_count >= 0) { + this->run_state = FirmataStepper::STOP; + } + break; + } + + this->step_delay = newStepDelay; + + } + + return done; +} + +/** + * Update the step position. + * @private + */ +void FirmataStepper::updateStepPosition() +{ + // increment or decrement the step number, + // depending on direction: + if (this->direction == FirmataStepper::CW) { + this->step_number++; + if (this->step_number >= this->steps_per_rev) { + this->step_number = 0; + } + } else { + if (this->step_number <= 0) { + this->step_number = this->steps_per_rev; + } + this->step_number--; + } + + // step the motor to step number 0, 1, 2, or 3: + stepMotor(this->step_number % 4, this->direction); +} + +/** + * Moves the motor forward or backwards. + * @param step_num For 2 or 4 wire configurations, this is the current step in + * the 2 or 4 step sequence. + * @param direction The direction of rotation + */ +void FirmataStepper::stepMotor(byte step_num, byte direction) +{ + if (this->interface == FirmataStepper::DRIVER) { + digitalWrite(dir_pin, direction); + delayMicroseconds(this->stepDelay); + digitalWrite(step_pin, LOW); + delayMicroseconds(this->stepDelay); + digitalWrite(step_pin, HIGH); + } else if (this->interface == FirmataStepper::TWO_WIRE) { + switch (step_num) { + case 0: /* 01 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + break; + case 1: /* 11 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + break; + case 2: /* 10 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + break; + case 3: /* 00 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + break; + } + } else if (this->interface == FirmataStepper::FOUR_WIRE) { + switch (step_num) { + case 0: // 1010 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 1: // 0110 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 2: //0101 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + case 3: //1001 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + } + } +} + +/** + * @return The version number of this library. + */ +byte FirmataStepper::version(void) +{ + return 3; +} diff --git a/libraries/ConfigurableFirmata/src/utility/FirmataStepper.h b/libraries/ConfigurableFirmata/src/utility/FirmataStepper.h new file mode 100644 index 0000000..8db3470 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/FirmataStepper.h @@ -0,0 +1,145 @@ +/* + FirmataStepper is a simple non-blocking stepper motor library + for 2 and 4 wire bipolar and unipolar stepper motor drive circuits + as well as EasyDriver (http://schmalzhaus.com/EasyDriver/) and + other step + direction drive circuits. + + FirmataStepper (0.3) by Jeff Hoefs + + EasyDriver support based on modifications by Chris Coleman + + Acceleration / Deceleration algorithms and code based on: + app note: http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf + source code: http://www.atmel.com/dyn/resources/prod_documents/AVR446.zip + + stepMotor function based on Stepper.cpp Stepper library for + Wiring/Arduino created by Tom Igoe, Sebastian Gassner + David Mellis and Noah Shibley. + + Relevant notes from Stepper.cpp: + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +// ensure this library description is only included once +#ifndef FirmataStepper_h +#define FirmataStepper_h + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#define PI_2 2*3.14159 +#define T1_FREQ 1000000L // provides the most accurate step delay values +#define T1_FREQ_148 ((long)((T1_FREQ*0.676)/100)) // divided by 100 and scaled by 0.676 + +// library interface description +class FirmataStepper +{ + public: + FirmataStepper(byte interface = FirmataStepper::DRIVER, + int steps_per_rev = 200, + byte pin1 = 2, + byte pin2 = 3, + byte pin3 = 4, + byte pin4 = 5); + + enum Interface + { + DRIVER = 1, + TWO_WIRE = 2, + FOUR_WIRE = 4 + }; + + enum RunState + { + STOP = 0, + ACCEL = 1, + DECEL = 2, + RUN = 3 + }; + + enum Direction + { + CCW = 0, + CW = 1 + }; + + void setStepsToMove(long steps_to_move, int speed, int accel = 0, int decel = 0); + + // update the stepper position + bool update(); + + byte version(void); + + private: + void stepMotor(byte step_num, byte direction); + void updateStepPosition(); + bool running; + byte interface; // Type of interface: DRIVER, TWO_WIRE or FOUR_WIRE + byte direction; // Direction of rotation + unsigned long step_delay; // delay between steps, in microseconds + int steps_per_rev; // number of steps to make one revolution + long step_number; // which step the motor is on + long steps_to_move; // total number of teps to move + byte stepDelay; // delay between steps (default = 1, increase for high current drivers) + + byte run_state; + int accel_count; + unsigned long min_delay; + unsigned long decel_start; + int decel_val; + + long lastAccelDelay; + unsigned long stepCount; + unsigned int rest; + + float alpha; // PI * 2 / steps_per_rev + long at_x100; // alpha * T1_FREQ * 100 + long ax20000; // alph a* 20000 + float alpha_x2; // alpha * 2 + + // motor pin numbers: + byte dir_pin; + byte step_pin; + byte motor_pin_1; + byte motor_pin_2; + byte motor_pin_3; + byte motor_pin_4; + + unsigned long last_step_time; // time stamp in microseconds of when the last step was taken +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/MultiStepper.cpp b/libraries/ConfigurableFirmata/src/utility/MultiStepper.cpp new file mode 100644 index 0000000..de0ec94 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/MultiStepper.cpp @@ -0,0 +1,73 @@ +// MultiStepper.cpp +// +// Copyright (C) 2015 Mike McCauley +// $Id: MultiStepper.cpp,v 1.2 2015/10/04 05:16:38 mikem Exp $ + +#include "MultiStepper.h" +#include "AccelStepper.h" + +MultiStepper::MultiStepper() + : _num_steppers(0) +{ +} + +boolean MultiStepper::addStepper(AccelStepper& stepper) +{ + if (_num_steppers >= MULTISTEPPER_MAX_STEPPERS) + return false; // No room for more + _steppers[_num_steppers++] = &stepper; + return true; +} + +void MultiStepper::moveTo(long absolute[]) +{ + // First find the stepper that will take the longest time to move + float longestTime = 0.0; + + uint8_t i; + for (i = 0; i < _num_steppers; i++) + { + long thisDistance = absolute[i] - _steppers[i]->currentPosition(); + float thisTime = abs(thisDistance) / _steppers[i]->maxSpeed(); + + if (thisTime > longestTime) + longestTime = thisTime; + } + + if (longestTime > 0.0) + { + // Now work out a new max speed for each stepper so they will all + // arrived at the same time of longestTime + for (i = 0; i < _num_steppers; i++) + { + long thisDistance = absolute[i] - _steppers[i]->currentPosition(); + float thisSpeed = thisDistance / longestTime; + _steppers[i]->moveTo(absolute[i]); // New target position (resets speed) + _steppers[i]->setSpeed(thisSpeed); // New speed + } + } +} + +// Returns true if any motor is still running to the target position. +boolean MultiStepper::run() +{ + uint8_t i; + boolean ret = false; + for (i = 0; i < _num_steppers; i++) + { + if ( _steppers[i]->distanceToGo() != 0) + { + _steppers[i]->runSpeed(); + ret = true; + } + } + return ret; +} + +// Blocks until all steppers reach their target position and are stopped +void MultiStepper::runSpeedToPosition() +{ + while (run()) + ; +} + diff --git a/libraries/ConfigurableFirmata/src/utility/MultiStepper.h b/libraries/ConfigurableFirmata/src/utility/MultiStepper.h new file mode 100644 index 0000000..d801bb0 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/MultiStepper.h @@ -0,0 +1,78 @@ +// MultiStepper.h + +#ifndef MultiStepper_h +#define MultiStepper_h + +#include +#if ARDUINO >= 100 +#include +#else +#include +#include +#endif + +#define MULTISTEPPER_MAX_STEPPERS 10 + +class AccelStepper; + +///////////////////////////////////////////////////////////////////// +/// \class MultiStepper MultiStepper.h +/// \brief Operate multiple AccelSteppers in a co-ordinated fashion +/// +/// This class can manage multiple AccelSteppers (up to MULTISTEPPER_MAX_STEPPERS = 10), +/// and cause them all to move +/// to selected positions at such a (constant) speed that they all arrive at their +/// target position at the same time. This can be used to support devices with multiple steppers +/// on say multiple axes to cause linear diagonal motion. Suitable for use with X-Y plotters, flatbeds, +/// 3D printers etc +/// to get linear straight line movement between arbitrary 2d (or 3d or ...) positions. +/// +/// Caution: only constant speed stepper motion is supported: acceleration and deceleration is not supported +/// All the steppers managed by MultiStepper will step at a constant speed to their +/// target (albeit perhaps different speeds for each stepper). +class MultiStepper +{ +public: + /// Constructor + MultiStepper(); + + /// Add a stepper to the set of managed steppers + /// There is an upper limit of MULTISTEPPER_MAX_STEPPERS = 10 to the number of steppers that can be managed + /// \param[in] stepper Reference to a stepper to add to the managed list + /// \return true if successful. false if the number of managed steppers would exceed MULTISTEPPER_MAX_STEPPERS + boolean addStepper(AccelStepper& stepper); + + /// Set the target positions of all managed steppers + /// according to a coordinate array. + /// New speeds will be computed for each stepper so they will all arrive at their + /// respective targets at very close to the same time. + /// \param[in] absolute An array of desired absolute stepper positions. absolute[0] will be used to set + /// the absolute position of the first stepper added by addStepper() etc. The array must be at least as long as + /// the number of steppers that have been added by addStepper, else results are undefined. + void moveTo(long absolute[]); + + /// Calls runSpeed() on all the managed steppers + /// that have not acheived their target position. + /// \return true if any stepper is still in the process of running to its target position. + boolean run(); + + /// Runs all managed steppers until they acheived their target position. + /// Blocks until all that position is acheived. If you dont + /// want blocking consider using run() instead. + void runSpeedToPosition(); + +private: + /// Array of pointers to the steppers we are controlling. + /// Fills from 0 onwards + AccelStepper* _steppers[MULTISTEPPER_MAX_STEPPERS]; + + /// Number of steppers we are controlling and the number + /// of steppers in _steppers[] + uint8_t _num_steppers; +}; + +/// @example MultiStepper.pde +/// Use MultiStepper class to manage multiple steppers and make them all move to +/// the same position at the same time for linear 2d (or 3d) motion. + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/OneWire.cpp b/libraries/ConfigurableFirmata/src/utility/OneWire.cpp new file mode 100644 index 0000000..cf34933 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/OneWire.cpp @@ -0,0 +1,567 @@ +/* +Copyright (c) 2007, Jim Studt (original old version - many contributors since) + +The latest version of this library may be found at: + http://www.pjrc.com/teensy/td_libs_OneWire.html + +OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since +January 2010. At the time, it was in need of many bug fixes, but had +been abandoned the original author (Jim Studt). None of the known +contributors were interested in maintaining OneWire. Paul typically +works on OneWire every 6 to 12 months. Patches usually wait that +long. If anyone is interested in more actively maintaining OneWire, +please contact Paul. + +Version 2.3: + Unknonw chip fallback mode, Roger Clark + Teensy-LC compatibility, Paul Stoffregen + Search bug fix, Love Nystrom + +Version 2.2: + Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com + Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 + Fix DS18B20 example negative temperature + Fix DS18B20 example's low res modes, Ken Butcher + Improve reset timing, Mark Tillotson + Add const qualifiers, Bertrik Sikken + Add initial value input to crc16, Bertrik Sikken + Add target_search() function, Scott Roberts + +Version 2.1: + Arduino 1.0 compatibility, Paul Stoffregen + Improve temperature example, Paul Stoffregen + DS250x_PROM example, Guillermo Lovato + PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com + Improvements from Glenn Trewitt: + - crc16() now works + - check_crc16() does all of calculation/checking work. + - Added read_bytes() and write_bytes(), to reduce tedious loops. + - Added ds2408 example. + Delete very old, out-of-date readme file (info is here) + +Version 2.0: Modifications by Paul Stoffregen, January 2010: +http://www.pjrc.com/teensy/td_libs_OneWire.html + Search fix from Robin James + http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + Use direct optimized I/O in all cases + Disable interrupts during timing critical sections + (this solves many random communication errors) + Disable interrupts during read-modify-write I/O + Reduce RAM consumption by eliminating unnecessary + variables and trimming many to 8 bits + Optimize both crc8 - table version moved to flash + +Modified to work with larger numbers of devices - avoids loop. +Tested in Arduino 11 alpha with 12 sensors. +26 Sept 2008 -- Robin James +http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + +Updated to work with arduino-0008 and to include skip() as of +2007/07/06. --RJL20 + +Modified to calculate the 8-bit CRC directly, avoiding the need for +the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 +-- Tom Pollard, Jan 23, 2008 + +Jim Studt's original library was modified by Josh Larios. + +Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008 + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Much of the code was inspired by Derek Yerger's code, though I don't +think much of that remains. In any event that was.. + (copyleft) 2006 by Derek Yerger - Free to distribute freely. + +The CRC code was excerpted and inspired by the Dallas Semiconductor +sample code bearing this copyright. +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//-------------------------------------------------------------------------- +*/ + +#include "OneWire.h" + + +OneWire::OneWire(uint8_t pin) +{ + pinMode(pin, INPUT); + bitmask = PIN_TO_BITMASK(pin); + baseReg = PIN_TO_BASEREG(pin); +#if ONEWIRE_SEARCH + reset_search(); +#endif +} + + +// Perform the onewire reset function. We will wait up to 250uS for +// the bus to come high, if it doesn't then it is broken or shorted +// and we return a 0; +// +// Returns 1 if a device asserted a presence pulse, 0 otherwise. +// +uint8_t OneWire::reset(void) +{ + IO_REG_TYPE mask = bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + uint8_t retries = 125; + + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); + interrupts(); + // wait until the wire is high... just in case + do { + if (--retries == 0) return 0; + delayMicroseconds(2); + } while ( !DIRECT_READ(reg, mask)); + + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + interrupts(); + delayMicroseconds(480); + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); // allow it to float + delayMicroseconds(70); + r = !DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(410); + return r; +} + +// +// Write a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +void OneWire::write_bit(uint8_t v) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + + if (v & 1) { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(10); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(55); + } else { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(65); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(5); + } +} + +// +// Read a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +uint8_t OneWire::read_bit(void) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + + noInterrupts(); + DIRECT_MODE_OUTPUT(reg, mask); + DIRECT_WRITE_LOW(reg, mask); + delayMicroseconds(3); + DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise + delayMicroseconds(10); + r = DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(53); + return r; +} + +// +// Write a byte. The writing code uses the active drivers to raise the +// pin high, if you need power after the write (e.g. DS18S20 in +// parasite power mode) then set 'power' to 1, otherwise the pin will +// go tri-state at the end of the write to avoid heating in a short or +// other mishap. +// +void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { + uint8_t bitMask; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + OneWire::write_bit( (bitMask & v)?1:0); + } + if ( !power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { + for (uint16_t i = 0 ; i < count ; i++) + write(buf[i]); + if (!power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +// +// Read a byte +// +uint8_t OneWire::read() { + uint8_t bitMask; + uint8_t r = 0; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + if ( OneWire::read_bit()) r |= bitMask; + } + return r; +} + +void OneWire::read_bytes(uint8_t *buf, uint16_t count) { + for (uint16_t i = 0 ; i < count ; i++) + buf[i] = read(); +} + +// +// Do a ROM select +// +void OneWire::select(const uint8_t rom[8]) +{ + uint8_t i; + + write(0x55); // Choose ROM + + for (i = 0; i < 8; i++) write(rom[i]); +} + +// +// Do a ROM skip +// +void OneWire::skip() +{ + write(0xCC); // Skip ROM +} + +void OneWire::depower() +{ + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + interrupts(); +} + +#if ONEWIRE_SEARCH + +// +// You need to use this function to start a search again from the beginning. +// You do not need to do it for the first search, though you could. +// +void OneWire::reset_search() +{ + // reset the search state + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + for(int i = 7; ; i--) { + ROM_NO[i] = 0; + if ( i == 0) break; + } +} + +// Setup the search to find the device type 'family_code' on the next call +// to search(*newAddr) if it is present. +// +void OneWire::target_search(uint8_t family_code) +{ + // set the search state to find SearchFamily type devices + ROM_NO[0] = family_code; + for (uint8_t i = 1; i < 8; i++) + ROM_NO[i] = 0; + LastDiscrepancy = 64; + LastFamilyDiscrepancy = 0; + LastDeviceFlag = FALSE; +} + +// +// Perform a search. If this function returns a '1' then it has +// enumerated the next device and you may retrieve the ROM from the +// OneWire::address variable. If there are no devices, no further +// devices, or something horrible happens in the middle of the +// enumeration then a 0 is returned. If a new device is found then +// its address is copied to newAddr. Use OneWire::reset_search() to +// start over. +// +// --- Replaced by the one from the Dallas Semiconductor web site --- +//-------------------------------------------------------------------------- +// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing +// search state. +// Return TRUE : device found, ROM number in ROM_NO buffer +// FALSE : device not found, end of search +// +uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) +{ + uint8_t id_bit_number; + uint8_t last_zero, rom_byte_number, search_result; + uint8_t id_bit, cmp_id_bit; + + unsigned char rom_byte_mask, search_direction; + + // initialize for search + id_bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + search_result = 0; + + // if the last call was not the last one + if (!LastDeviceFlag) + { + // 1-Wire reset + if (!reset()) + { + // reset the search + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + return FALSE; + } + + // issue the search command + if (search_mode == true) { + write(0xF0); // NORMAL SEARCH + } else { + write(0xEC); // CONDITIONAL SEARCH + } + + // loop to do the search + do + { + // read a bit and its complement + id_bit = read_bit(); + cmp_id_bit = read_bit(); + + // check for no devices on 1-wire + if ((id_bit == 1) && (cmp_id_bit == 1)) + break; + else + { + // all devices coupled have 0 or 1 + if (id_bit != cmp_id_bit) + search_direction = id_bit; // bit write value for search + else + { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (id_bit_number < LastDiscrepancy) + search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == LastDiscrepancy); + + // if 0 was picked then record its position in LastZero + if (search_direction == 0) + { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == 1) + ROM_NO[rom_byte_number] |= rom_byte_mask; + else + ROM_NO[rom_byte_number] &= ~rom_byte_mask; + + // serial number search direction write bit + write_bit(search_direction); + + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) + { + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 + + // if the search was successful then + if (!(id_bit_number < 65)) + { + // search successful so set LastDiscrepancy,LastDeviceFlag,search_result + LastDiscrepancy = last_zero; + + // check for last device + if (LastDiscrepancy == 0) + LastDeviceFlag = TRUE; + + search_result = TRUE; + } + } + + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || !ROM_NO[0]) + { + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + search_result = FALSE; + } else { + for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; + } + return search_result; + } + +#endif + +#if ONEWIRE_CRC +// The 1-Wire CRC scheme is described in Maxim Application Note 27: +// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" +// + +#if ONEWIRE_CRC8_TABLE +// This table comes from Dallas sample code where it is freely reusable, +// though Copyright (C) 2000 Dallas Semiconductor Corporation +static const uint8_t PROGMEM dscrc_table[] = { + 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, + 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, + 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, + 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, + 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, + 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, + 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, + 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, + 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, + 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, + 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, + 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, + 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, + 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, + 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, + 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; + +// +// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM +// and the registers. (note: this might better be done without to +// table, it would probably be smaller and certainly fast enough +// compared to all those delayMicrosecond() calls. But I got +// confused, so I use this table from the examples.) +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); + } + return crc; +} +#else +// +// Compute a Dallas Semiconductor 8 bit CRC directly. +// this is much slower, but much smaller, than the lookup table. +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } + } + return crc; +} +#endif + +#if ONEWIRE_CRC16 +bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc) +{ + crc = ~crc16(input, len, crc); + return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; +} + +uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) +{ + static const uint8_t oddparity[16] = + { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + + for (uint16_t i = 0 ; i < len ; i++) { + // Even though we're just copying a byte from the input, + // we'll be doing 16-bit computation with it. + uint16_t cdata = input[i]; + cdata = (cdata ^ crc) & 0xff; + crc >>= 8; + + if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) + crc ^= 0xC001; + + cdata <<= 6; + crc ^= cdata; + cdata <<= 1; + crc ^= cdata; + } + return crc; +} +#endif + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/OneWire.h b/libraries/ConfigurableFirmata/src/utility/OneWire.h new file mode 100644 index 0000000..8753e91 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/OneWire.h @@ -0,0 +1,367 @@ +#ifndef OneWire_h +#define OneWire_h + +#include + +#if ARDUINO >= 100 +#include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc +#else +#include "WProgram.h" // for delayMicroseconds +#include "pins_arduino.h" // for digitalPinToBitMask, etc +#endif + +// You can exclude certain features from OneWire. In theory, this +// might save some space. In practice, the compiler automatically +// removes unused code (technically, the linker, using -fdata-sections +// and -ffunction-sections when compiling, and Wl,--gc-sections +// when linking), so most of these will not result in any code size +// reduction. Well, unless you try to use the missing features +// and redesign your program to not need them! ONEWIRE_CRC8_TABLE +// is the exception, because it selects a fast but large algorithm +// or a small but slow algorithm. + +// you can exclude onewire_search by defining that to 0 +#ifndef ONEWIRE_SEARCH +#define ONEWIRE_SEARCH 1 +#endif + +// You can exclude CRC checks altogether by defining this to 0 +#ifndef ONEWIRE_CRC +#define ONEWIRE_CRC 1 +#endif + +// Select the table-lookup method of computing the 8-bit CRC +// by setting this to 1. The lookup table enlarges code size by +// about 250 bytes. It does NOT consume RAM (but did in very +// old versions of OneWire). If you disable this, a slower +// but very compact algorithm is used. +#ifndef ONEWIRE_CRC8_TABLE +#define ONEWIRE_CRC8_TABLE 1 +#endif + +// You can allow 16-bit CRC checks by defining this to 1 +// (Note that ONEWIRE_CRC must also be 1.) +#ifndef ONEWIRE_CRC16 +#define ONEWIRE_CRC16 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif + +// Platform specific I/O definitions + +#if defined(__AVR__) +#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM asm("r30") +#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (1) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (*((base)+512)) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) + +#elif defined(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) + +#elif defined(__SAM3X8E__) +// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. +// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268 +// If you have trouble with OneWire on Arduino Due, please check the +// status of delayMicroseconds() before reporting a bug in OneWire! +#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask)) +#ifndef PROGMEM +#define PROGMEM +#endif +#ifndef pgm_read_byte +#define pgm_read_byte(addr) (*(const uint8_t *)(addr)) +#endif + +#elif defined(__PIC32MX__) +#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 +#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08 +#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04 +#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24 +#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 + +#elif defined(ARDUINO_ARCH_ESP8266) +#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) +#define PIN_TO_BITMASK(pin) (1 << pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS +#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS +#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS +#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS +#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS + +#elif defined(__SAMD21G18A__) +#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) + +#elif defined(RBL_NRF51822) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) + +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ + +#include "scss_registers.h" +#include "portable.h" +#include "avr/pgmspace.h" + +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 + +/* GPIO registers base address */ +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); +} + +static inline __attribute__((always_inline)) +void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } +} + +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) + +#else +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE unsigned int +#define IO_REG_ASM +#define DIRECT_READ(base, pin) digitalRead(pin) +#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW) +#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH) +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) +#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) +#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." + +#endif + + +class OneWire +{ + private: + IO_REG_TYPE bitmask; + volatile IO_REG_TYPE *baseReg; + +#if ONEWIRE_SEARCH + // global search state + unsigned char ROM_NO[8]; + uint8_t LastDiscrepancy; + uint8_t LastFamilyDiscrepancy; + uint8_t LastDeviceFlag; +#endif + + public: + OneWire( uint8_t pin); + + // Perform a 1-Wire reset cycle. Returns 1 if a device responds + // with a presence pulse. Returns 0 if there is no device or the + // bus is shorted or otherwise held low for more than 250uS + uint8_t reset(void); + + // Issue a 1-Wire rom select command, you do the reset first. + void select(const uint8_t rom[8]); + + // Issue a 1-Wire rom skip command, to address all on bus. + void skip(void); + + // Write a byte. If 'power' is one then the wire is held high at + // the end for parasitically powered devices. You are responsible + // for eventually depowering it by calling depower() or doing + // another read or write. + void write(uint8_t v, uint8_t power = 0); + + void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); + + // Read a byte. + uint8_t read(void); + + void read_bytes(uint8_t *buf, uint16_t count); + + // Write a bit. The bus is always left powered at the end, see + // note in write() about that. + void write_bit(uint8_t v); + + // Read a bit. + uint8_t read_bit(void); + + // Stop forcing power onto the bus. You only need to do this if + // you used the 'power' flag to write() or used a write_bit() call + // and aren't about to do another read or write. You would rather + // not leave this powered if you don't have to, just in case + // someone shorts your bus. + void depower(void); + +#if ONEWIRE_SEARCH + // Clear the search state so that if will start from the beginning again. + void reset_search(); + + // Setup the search to find the device type 'family_code' on the next call + // to search(*newAddr) if it is present. + void target_search(uint8_t family_code); + + // Look for the next device. Returns 1 if a new address has been + // returned. A zero might mean that the bus is shorted, there are + // no devices, or you have already retrieved all of them. It + // might be a good idea to check the CRC to make sure you didn't + // get garbage. The order is deterministic. You will always get + // the same devices in the same order. + uint8_t search(uint8_t *newAddr, bool search_mode = true); +#endif + +#if ONEWIRE_CRC + // Compute a Dallas Semiconductor 8 bit CRC, these are used in the + // ROM and scratchpad registers. + static uint8_t crc8(const uint8_t *addr, uint8_t len); + +#if ONEWIRE_CRC16 + // Compute the 1-Wire CRC16 and compare it against the received CRC. + // Example usage (reading a DS2408): + // // Put everything in a buffer so we can compute the CRC easily. + // uint8_t buf[13]; + // buf[0] = 0xF0; // Read PIO Registers + // buf[1] = 0x88; // LSB address + // buf[2] = 0x00; // MSB address + // WriteBytes(net, buf, 3); // Write 3 cmd bytes + // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 + // if (!CheckCRC16(buf, 11, &buf[11])) { + // // Handle error. + // } + // + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param inverted_crc - The two CRC16 bytes in the received data. + // This should just point into the received data, + // *not* at a 16-bit integer. + // @param crc - The crc starting value (optional) + // @return True, iff the CRC matches. + static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); + + // Compute a Dallas Semiconductor 16 bit CRC. This is required to check + // the integrity of data received from many 1-Wire devices. Note that the + // CRC computed here is *not* what you'll get from the 1-Wire network, + // for two reasons: + // 1) The CRC is transmitted bitwise inverted. + // 2) Depending on the endian-ness of your processor, the binary + // representation of the two-byte return value may have a different + // byte order than the two bytes you get from 1-Wire. + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param crc - The crc starting value (optional) + // @return The CRC16, as defined by Dallas Semiconductor. + static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); +#endif +#endif +}; + +#endif diff --git a/libraries/ConfigurableFirmata/src/utility/WiFiClientStream.h b/libraries/ConfigurableFirmata/src/utility/WiFiClientStream.h new file mode 100644 index 0000000..7fd30af --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/WiFiClientStream.h @@ -0,0 +1,105 @@ +/* + WiFiClientStream.h + + An Arduino Stream that wraps an instance of a WiFiClient. For use + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2016 Jens B. 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. + + Parts of this class are based on + + - EthernetClientStream - Copyright (C) 2013 Norbert Truchsess. All rights reserved. + + published under the same license. + + Last updated April 23rd, 2016 + */ + +#ifndef WIFI_CLIENT_STREAM_H +#define WIFI_CLIENT_STREAM_H + +#include "WiFiStream.h" + +#define MILLIS_RECONNECT 5000 + +class WiFiClientStream : public WiFiStream +{ +protected: + uint32_t _time_connect = 0; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual inline bool connect_client() + { + if ( _connected ) + { + if ( _client && _client.connected() ) return true; + stop(); + } + + // active TCP connect + if ( WiFi.status() == WL_CONNECTED ) + { + // if the client is disconnected, try to reconnect every 5 seconds + if ( millis() - _time_connect >= MILLIS_RECONNECT ) + { + _connected = _client.connect( _remote_ip, _port ); + if ( !_connected ) + { + _time_connect = millis(); + } + else if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED); + } + } + } + + return _connected; + } + +public: + /** + * create a WiFi stream with a TCP client + */ + WiFiClientStream(IPAddress server_ip, uint16_t server_port) : WiFiStream(server_ip, server_port) {} + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual inline bool maintain() + { + return connect_client(); + } + + /** + * stop client connection + */ + virtual inline void stop() + { + if ( _client) + { + _client.stop(); + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED); + } + } + _connected = false; + _time_connect = millis(); + } + +}; + +#endif //WIFI_CLIENT_STREAM_H diff --git a/libraries/ConfigurableFirmata/src/utility/WiFiServerStream.h b/libraries/ConfigurableFirmata/src/utility/WiFiServerStream.h new file mode 100644 index 0000000..1404b05 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/WiFiServerStream.h @@ -0,0 +1,107 @@ +/* + WiFiServerStream.h + + An Arduino Stream extension for a WiFiClient or WiFiServer to be used + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2016 Jens B. 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. + + Parts of this class are based on + + - WiFiStream - Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + + published under the same license. + + Last updated April 23rd, 2016 + */ + +#ifndef WIFI_SERVER_STREAM_H +#define WIFI_SERVER_STREAM_H + +#include "WiFiStream.h" + +class WiFiServerStream : public WiFiStream +{ +protected: + WiFiServer _server = WiFiServer(3030); + bool _listening = false; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual inline bool connect_client() + { + if ( _connected ) + { + if ( _client && _client.connected() ) return true; + stop(); + } + + // passive TCP connect (accept) + WiFiClient newClient = _server.available(); + if ( !newClient ) return false; + _client = newClient; + _connected = true; + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED); + } + + return true; + } + +public: + /** + * create a WiFi stream with a TCP server + */ + WiFiServerStream(uint16_t server_port) : WiFiStream(server_port) {} + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual inline bool maintain() + { + if ( connect_client() ) return true; + + stop(); + + if ( !_listening && WiFi.status() == WL_CONNECTED ) + { + // start TCP server after first WiFi connect + _server = WiFiServer(_port); + _server.begin(); + _listening = true; + } + + return false; + } + + /** + * stop client connection + */ + virtual inline void stop() + { + if ( _client) + { + _client.stop(); + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED); + } + } + _connected = false; + } + +}; + +#endif //WIFI_SERVER_STREAM_H diff --git a/libraries/ConfigurableFirmata/src/utility/WiFiStream.cpp b/libraries/ConfigurableFirmata/src/utility/WiFiStream.cpp new file mode 100644 index 0000000..9b54a5a --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/WiFiStream.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which + * will cause linker errors whenever Firmata.h is included. + */ diff --git a/libraries/ConfigurableFirmata/src/utility/WiFiStream.h b/libraries/ConfigurableFirmata/src/utility/WiFiStream.h new file mode 100644 index 0000000..1ad44bb --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/WiFiStream.h @@ -0,0 +1,226 @@ +/* + WiFiStream.h + + An Arduino Stream extension for a WiFiClient or WiFiServer to be used + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + Copyright (C) 2016 Jens B. 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 April 23rd, 2016 + */ + +#ifndef WIFI_STREAM_H +#define WIFI_STREAM_H + +#include +#include + +#define HOST_CONNECTION_DISCONNECTED 0 +#define HOST_CONNECTION_CONNECTED 1 + +extern "C" { + // callback function types + typedef void (*hostConnectionCallbackFunction)(byte); +} + +class WiFiStream : public Stream +{ +protected: + WiFiClient _client; + bool _connected = false; + hostConnectionCallbackFunction _currentHostConnectionCallback; + + //configuration members + IPAddress _local_ip; // DHCP + IPAddress _subnet; + IPAddress _gateway; + IPAddress _remote_ip; + uint16_t _port; + uint8_t _key_idx; //WEP + const char *_key = nullptr; //WEP + const char *_passphrase = nullptr; //WPA + char *_ssid = nullptr; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual bool connect_client() = 0; + +public: + /** constructor for TCP server */ + WiFiStream(uint16_t server_port) : _port(server_port) {} + + /** constructor for TCP client */ + WiFiStream(IPAddress server_ip, uint16_t server_port) : _remote_ip(server_ip), _port(server_port) {} + + inline void attach( hostConnectionCallbackFunction newFunction ) { _currentHostConnectionCallback = newFunction; } + +/****************************************************************************** + * network configuration + ******************************************************************************/ + +#ifndef ESP8266 + /** + * configure a static local IP address without defining the local network + * DHCP will be used as long as local IP address is not defined + */ + inline void config(IPAddress local_ip) + { + _local_ip = local_ip; + WiFi.config( local_ip ); + } +#endif + + /** + * configure a static local IP address + * DHCP will be used as long as local IP address is not defined + */ + inline void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) + { + _local_ip = local_ip; + _subnet = subnet; + _gateway = gateway; +#ifndef ESP8266 + WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet ); +#else + WiFi.config( local_ip, gateway, subnet ); +#endif + } + + /** + * @return local IP address + */ + inline IPAddress getLocalIP() + { + return WiFi.localIP(); + } + +/****************************************************************************** + * network functions + ******************************************************************************/ + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual bool maintain() = 0; + +#ifdef ESP8266 + /** + * get status of TCP connection + * @return status of TCP connection + * CLOSED = 0 (typical) + * LISTEN = 1 (not used) + * SYN_SENT = 2 + * SYN_RCVD = 3 + * ESTABLISHED = 4 (typical) + * FIN_WAIT_1 = 5 + * FIN_WAIT_2 = 6 + * CLOSE_WAIT = 7 + * CLOSING = 8 + * LAST_ACK = 9 + * TIME_WAIT = 10 + */ + inline uint8_t status() + { + return _client.status(); + } +#endif + + /** + * close TCP client connection + */ + virtual void stop() = 0; + +/****************************************************************************** + * WiFi configuration + ******************************************************************************/ + + /** + * initialize WiFi without security (open) and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid) + { + _ssid = ssid; + + WiFi.begin(ssid); + int result = WiFi.status(); + return WiFi.status(); + } + +#ifndef ESP8266 + /** + * initialize WiFi with WEP security and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid, uint8_t key_idx, const char *key) + { + _ssid = ssid; + _key_idx = key_idx; + _key = key; + + WiFi.begin( ssid, key_idx, key ); + return WiFi.status(); + } +#endif + + /** + * initialize WiFi with WPA-PSK security and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid, const char *passphrase) + { + _ssid = ssid; + _passphrase = passphrase; + + WiFi.begin(ssid, passphrase); + return WiFi.status(); + } + + +/****************************************************************************** + * stream functions + ******************************************************************************/ + + inline int available() + { + return connect_client() ? _client.available() : 0; + } + + inline void flush() + { + if( _client ) _client.flush(); + } + + inline int peek() + { + return connect_client() ? _client.peek(): 0; + } + + inline int read() + { + return connect_client() ? _client.read() : -1; + } + + inline size_t write(uint8_t byte) + { + return connect_client() ? _client.write( byte ) : 0; + } + +}; + +#endif //WIFI_STREAM_H diff --git a/libraries/ConfigurableFirmata/src/utility/firmataDebug.h b/libraries/ConfigurableFirmata/src/utility/firmataDebug.h new file mode 100644 index 0000000..6e364b0 --- /dev/null +++ b/libraries/ConfigurableFirmata/src/utility/firmataDebug.h @@ -0,0 +1,14 @@ +#ifndef FIRMATA_DEBUG_H +#define FIRMATA_DEBUG_H + +#ifdef SERIAL_DEBUG + #define DEBUG_BEGIN(baud) Serial.begin(baud); while(!Serial) {;} + #define DEBUG_PRINTLN(x) Serial.println (x) + #define DEBUG_PRINT(x) Serial.print (x) +#else + #define DEBUG_BEGIN(baud) + #define DEBUG_PRINTLN(x) + #define DEBUG_PRINT(x) +#endif + +#endif /* FIRMATA_DEBUG_H */ diff --git a/libraries/ConfigurableFirmata/test/firmata_test/firmata_test.ino b/libraries/ConfigurableFirmata/test/firmata_test/firmata_test.ino new file mode 100644 index 0000000..f96d119 --- /dev/null +++ b/libraries/ConfigurableFirmata/test/firmata_test/firmata_test.ino @@ -0,0 +1,136 @@ +/* + * To run this test suite, you must first install the ArduinoUnit library + * to your Arduino/libraries/ directory. + * You can get ArduinoUnit here: https://github.com/mmurdoch/arduinounit + * Download version 2.0 or greater. + */ + +#include +#include + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Test::run(); +} + +test(beginPrintsVersion) +{ + FakeStream stream; + + Firmata.begin(stream); + + char expected[] = { + REPORT_VERSION, + FIRMATA_MAJOR_VERSION, + FIRMATA_MINOR_VERSION, + 0 + }; + assertEqual(expected, stream.bytesWritten()); +} + +void processMessage(const byte *message, size_t length) +{ + FakeStream stream; + Firmata.begin(stream); + + for (size_t i = 0; i < length; i++) { + stream.nextByte(message[i]); + Firmata.processInput(); + } +} + +byte _digitalPort; +int _digitalPortValue; +void writeToDigitalPort(byte port, int value) +{ + _digitalPort = port; + _digitalPortValue = value; +} + +void setupDigitalPort() +{ + _digitalPort = 0; + _digitalPortValue = 0; +} + +test(processWriteDigital_0) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPortValue); +} + +test(processWriteDigital_127) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 127, 0 }; + processMessage(message, 3); + + assertEqual(127, _digitalPortValue); +} + +test(processWriteDigital_128) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 1 }; + processMessage(message, 3); + + assertEqual(128, _digitalPortValue); +} + +test(processWriteLargestDigitalValue) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0x7F, 0x7F }; + processMessage(message, 3); + + // Maximum of 14 bits can be set (B0011111111111111) + assertEqual(0x3FFF, _digitalPortValue); +} + +test(defaultDigitalWritePortIsZero) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPort); +} + +test(specifiedDigitalWritePort) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE + 1, 0, 0 }; + processMessage(message, 3); + + assertEqual(1, _digitalPort); +} + +test(setFirmwareVersionDoesNotLeakMemory) +{ + Firmata.setFirmwareVersion(1, 0); + int initialMemory = freeMemory(); + + Firmata.setFirmwareVersion(1, 0); + + assertEqual(0, initialMemory - freeMemory()); +} diff --git a/libraries/ConfigurableFirmata/test/readme.md b/libraries/ConfigurableFirmata/test/readme.md new file mode 100644 index 0000000..726cbcb --- /dev/null +++ b/libraries/ConfigurableFirmata/test/readme.md @@ -0,0 +1,13 @@ +#Testing Firmata + +Tests tests are written using the [ArduinoUnit](https://github.com/mmurdoch/arduinounit) library (version 2.0). + +Follow the instructions in the [ArduinoUnit readme](https://github.com/mmurdoch/arduinounit/blob/master/readme.md) to install the library. + +Compile and upload the test sketch as you would any other sketch. Then open the +Serial Monitor to view the test results. + +If you make changes to Firmata.cpp, run the tests in /test/ to ensure +that your changes have not produced any unexpected errors. + +You should also perform manual tests against actual hardware. diff --git a/libraries/ConfigurableFirmata/test/stepper_test/stepper_test.ino b/libraries/ConfigurableFirmata/test/stepper_test/stepper_test.ino new file mode 100644 index 0000000..065c4b5 --- /dev/null +++ b/libraries/ConfigurableFirmata/test/stepper_test/stepper_test.ino @@ -0,0 +1,91 @@ +/* + * To run this test suite, you must first install the ArduinoUnit library + * to your Arduino/libraries/ directory. + * You can get ArduinoUnit here: https://github.com/mmurdoch/arduinounit + * Download version 2.0 or greater. + */ + +#include +#include + +void setup() +{ + Serial.begin(9600); +} + +test(decode_custom_float) +{ + AccelStepperFirmata stepper; + float result; + + result = stepper.decodeCustomFloat( 110, 92, 44, 32 ); + result = fabs(732.782 - result); + + assertLessOrEqual(result,.01); + +} + +test(decode_negative_custom_float) +{ + AccelStepperFirmata stepper; + float result; + + result = stepper.decodeCustomFloat( 110, 92, 44, 96 ); + result = fabs(-732.782 - result); + + assertLessOrEqual(result,.01); + +} + +test(encode_32_bit_signed_integer) +{ + AccelStepperFirmata stepper; + byte result[5]; + + stepper.encode32BitSignedInteger(5786, result); + + assertEqual(result[0], 26); + assertEqual(result[1], 45); + assertEqual(result[2], 0); + assertEqual(result[3], 0); + assertEqual(result[4], 0); +} + +test(encode_negative_32_bit_signed_integer) +{ + AccelStepperFirmata stepper; + byte result[5]; + + stepper.encode32BitSignedInteger(-5786, result); + + assertEqual(result[0], 26); + assertEqual(result[1], 45); + assertEqual(result[2], 0); + assertEqual(result[3], 0); + assertEqual(result[4], 8); +} + +test(decode_32_bit_signed_integer) +{ + AccelStepperFirmata stepper; + long result; + + result = stepper.decode32BitSignedInteger(26, 45, 0, 0, 0); + + assertEqual(result, 5786); +} + +test(decode_negative_32_bit_signed_integer) +{ + AccelStepperFirmata stepper; + long result; + + result = stepper.decode32BitSignedInteger(26, 45, 0, 0, 8); + + assertEqual(result, -5786); +} + +void loop() +{ + Test::run(); +} diff --git a/libraries/Firmata/Boards.h b/libraries/Firmata/Boards.h new file mode 100644 index 0000000..ace8d97 --- /dev/null +++ b/libraries/Firmata/Boards.h @@ -0,0 +1,953 @@ +/* + Boards.h - Hardware Abstraction Layer for Firmata library + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (C) 2009-2017 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 April 15th, 2018 +*/ + +#ifndef Firmata_Boards_h +#define Firmata_Boards_h + +#include + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" // for digitalRead, digitalWrite, etc +#else +#include "WProgram.h" +#endif + +// Normally Servo.h must be included before Firmata.h (which then includes +// this file). If Servo.h wasn't included, this allows the code to still +// compile, but without support for any Servos. Hopefully that's what the +// user intended by not including Servo.h +#ifndef MAX_SERVOS +#define MAX_SERVOS 0 +#endif + +/* + Firmata Hardware Abstraction Layer + +Firmata is built on top of the hardware abstraction functions of Arduino, +specifically digitalWrite, digitalRead, analogWrite, analogRead, and +pinMode. While these functions offer simple integer pin numbers, Firmata +needs more information than is provided by Arduino. This file provides +all other hardware specific details. To make Firmata support a new board, +only this file should require editing. + +The key concept is every "pin" implemented by Firmata may be mapped to +any pin as implemented by Arduino. Usually a simple 1-to-1 mapping is +best, but such mapping should not be assumed. This hardware abstraction +layer allows Firmata to implement any number of pins which map onto the +Arduino implemented pins in almost any arbitrary way. + + +General Constants: + +These constants provide basic information Firmata requires. + +TOTAL_PINS: The total number of pins Firmata implemented by Firmata. + Usually this will match the number of pins the Arduino functions + implement, including any pins pins capable of analog or digital. + However, Firmata may implement any number of pins. For example, + on Arduino Mini with 8 analog inputs, 6 of these may be used + for digital functions, and 2 are analog only. On such boards, + Firmata can implement more pins than Arduino's pinMode() + function, in order to accommodate those special pins. The + Firmata protocol supports a maximum of 128 pins, so this + constant must not exceed 128. + +TOTAL_ANALOG_PINS: The total number of analog input pins implemented. + The Firmata protocol allows up to 16 analog inputs, accessed + using offsets 0 to 15. Because Firmata presents the analog + inputs using different offsets than the actual pin numbers + (a legacy of Arduino's analogRead function, and the way the + analog input capable pins are physically labeled on all + Arduino boards), the total number of analog input signals + must be specified. 16 is the maximum. + +VERSION_BLINK_PIN: When Firmata starts up, it will blink the version + number. This constant is the Arduino pin number where a + LED is connected. + + +Pin Mapping Macros: + +These macros provide the mapping between pins as implemented by +Firmata protocol and the actual pin numbers used by the Arduino +functions. Even though such mappings are often simple, pin +numbers received by Firmata protocol should always be used as +input to these macros, and the result of the macro should be +used with with any Arduino function. + +When Firmata is extended to support a new pin mode or feature, +a pair of macros should be added and used for all hardware +access. For simple 1:1 mapping, these macros add no actual +overhead, yet their consistent use allows source code which +uses them consistently to be easily adapted to all other boards +with different requirements. + +IS_PIN_XXXX(pin): The IS_PIN macros resolve to true or non-zero + if a pin as implemented by Firmata corresponds to a pin + that actually implements the named feature. + +PIN_TO_XXXX(pin): The PIN_TO macros translate pin numbers as + implemented by Firmata to the pin numbers needed as inputs + to the Arduino functions. The corresponding IS_PIN macro + should always be tested before using a PIN_TO macro, so + these macros only need to handle valid Firmata pin + numbers for the named feature. + + +Port Access Inline Funtions: + +For efficiency, Firmata protocol provides access to digital +input and output pins grouped by 8 bit ports. When these +groups of 8 correspond to actual 8 bit ports as implemented +by the hardware, these inline functions can provide high +speed direct port access. Otherwise, a default implementation +using 8 calls to digitalWrite or digitalRead is used. + +When porting Firmata to a new board, it is recommended to +use the default functions first and focus only on the constants +and macros above. When those are working, if optimized port +access is desired, these inline functions may be extended. +The recommended approach defines a symbol indicating which +optimization to use, and then conditional complication is +used within these functions. + +readPort(port, bitmask): Read an 8 bit port, returning the value. + port: The port number, Firmata pins port*8 to port*8+7 + bitmask: The actual pins to read, indicated by 1 bits. + +writePort(port, value, bitmask): Write an 8 bit port. + port: The port number, Firmata pins port*8 to port*8+7 + value: The 8 bit value to write + bitmask: The actual pins to write, indicated by 1 bits. +*/ + +/*============================================================================== + * Board Specific Configuration + *============================================================================*/ + +#ifndef digitalPinHasPWM +#define digitalPinHasPWM(p) IS_PIN_DIGITAL(p) +#endif + +// Arduino Duemilanove, Diecimila, and NG +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) +#if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6 +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#else +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 22 // 14 digital + 8 analog +#endif +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Wiring (and board) +#elif defined(WIRING) +#define VERSION_BLINK_PIN WLED +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= FIRST_ANALOG_PIN && (p) < (FIRST_ANALOG_PIN+TOTAL_ANALOG_PINS)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - FIRST_ANALOG_PIN) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// old Arduinos +#elif defined(__AVR_ATmega8__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Arduino Mega +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TOTAL_ANALOG_PINS 16 +#define TOTAL_PINS 70 // 54 digital + 16 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino DUE +#elif defined(__SAM3X8E__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 66 // 54 digital + 12 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71 +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino/Genuino MKR1000 +#elif defined(ARDUINO_SAMD_MKR1000) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 22 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 21) +#define IS_PIN_ANALOG(p) ((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino MKRZero +#elif defined(ARDUINO_SAMD_MKRZERO) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 34 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog + 3 usb + 1 aref + 5 sd + 1 bottom pad + 1 led + 1 battery adc +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21) || (p) == 32) +#define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 33) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + +// Arduino MKRFox1200 +#elif defined(ARDUINO_SAMD_MKRFox1200) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 33 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog + 3 usb + 1 aref + 5 sd + 1 bottom pad + 1 battery adc +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21)) +#define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + +// Arduino MKR WAN 1300 +#elif defined(ARDUINO_SAMD_MKRWAN1300) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 33 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21)) +#define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + +// Arduino MKR GSM 1400 +#elif defined(ARDUINO_SAMD_MKRGSM1400) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 33 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21)) +#define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + +// Arduino Zero +// Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro +// Arduino M0 Pro does not properly map pins to the board labeled pin numbers +#elif defined(_VARIANT_ARDUINO_ZERO_) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi +#define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins +#define VERSION_BLINK_PIN LED_BUILTIN +//#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h +//#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2 +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + +// Arduino Primo +#elif defined(ARDUINO_PRIMO) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 22 //14 digital + 6 analog + 2 i2c +#define VERSION_BLINK_PIN LED_BUILTIN +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < 20) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 20) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS+2) +#define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL) // SDA = 20, SCL = 21 +#define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK)) // 10, 11, 12, 13 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + +// Arduino 101 +#elif defined(_VARIANT_ARDUINO_101_X_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog +#define VERSION_BLINK_PIN LED_BUILTIN +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9 +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Teensy 1.0 +#elif defined(__AVR_AT90USB162__) +#define TOTAL_ANALOG_PINS 0 +#define TOTAL_PINS 21 // 21 digital + no analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) (0) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) (0) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (0) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 2.0 +#elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 25 // 11 digital + 12 analog +#define VERSION_BLINK_PIN 11 +#define PIN_SERIAL1_RX 7 +#define PIN_SERIAL1_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 5 || (p) == 6) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 3.5 and 3.6 +// reference: https://github.com/PaulStoffregen/cores/blob/master/teensy3/pins_arduino.h +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define TOTAL_ANALOG_PINS 27 // 3.5 has 27 and 3.6 has 25 +#define TOTAL_PINS 70 // 43 digital + 21 analog-digital + 6 analog (64-69) +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define PIN_SERIAL4_RX 31 +#define PIN_SERIAL4_TX 32 +#define PIN_SERIAL5_RX 34 +#define PIN_SERIAL5_TX 33 +#define PIN_SERIAL6_RX 47 +#define PIN_SERIAL6_TX 48 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 63) +#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 31 && (p) <= 39) || ((p) >= 49 && (p) <= 50) || ((p) >= 64 && (p) <= 69)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1) || ((p) > 30 && (p) < 35) || ((p) == 47 || (p) == 48)) +#define PIN_TO_DIGITAL(p) (p) +// A0-A9 = D14-D23; A12-A20 = D31-D39; A23-A24 = D49-D50; A10-A11 = D64-D65; A21-A22 = D66-D67; A25-A26 = D68-D69 +#define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (((p) <= 39) ? (p) - 19 : (((p) <= 50) ? (p) - 26 : (((p) <= 65) ? (p) - 55 : (((p) <= 67) ? (p) - 45 : (p) - 43))))) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 3.0, 3.1 and 3.2 +#elif defined(__MK20DX128__) || defined(__MK20DX256__) +#define TOTAL_ANALOG_PINS 14 +#define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33) +#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy-LC +#elif defined(__MKL26Z64__) +#define TOTAL_ANALOG_PINS 13 +#define TOTAL_PINS 27 // 27 digital + 13 analog-digital +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26) +#define IS_PIN_ANALOG(p) ((p) >= 14) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy++ 1.0 and 2.0 +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 46 // 38 digital + 8 analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 0 || (p) == 1) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 38) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Leonardo +#elif defined(__AVR_ATmega32U4__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header) +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 2 || (p) == 3) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p) - 18 +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Intel Galileo Board (gen 1 and 2) and Intel Edison +#elif defined(ARDUINO_LINUX) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// RedBearLab BLE Nano with factory switch settings (S1 - S10) +#elif defined(BLE_NANO) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 15 // 9 digital + 3 analog +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 14) +#define IS_PIN_ANALOG(p) ((p) == 8 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 12 || (p) == 14) //A0~A5 +#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) <= 7) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == CS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 8) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Sanguino +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 32 // 24 digital + 8 analog +#define VERSION_BLINK_PIN 0 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 16 || (p) == 17) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Illuminato +#elif defined(__AVR_ATmega645__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 42 // 36 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 4 || (p) == 5) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 36) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Pic32 chipKIT FubarinoSD +#elif defined(_BOARD_FUBARINO_SD_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 15 +#define TOTAL_PINS NUM_DIGITAL_PINS // 45, All pins can be digital +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) 1 +#define IS_PIN_ANALOG(p) ((p) >= 30 && (p) <= 44) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 1 || (p) == 2) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (14 - (p - 30)) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT FubarinoMini +// Note, FubarinoMini analog pin 20 will not function in Firmata as analog input due to limitation in analog mapping +#elif defined(_BOARD_FUBARINO_MINI_) +#define TOTAL_ANALOG_PINS 14 // We have to fake this because of the poor analog pin mapping planning in FubarinoMini +#define TOTAL_PINS NUM_DIGITAL_PINS // 33 +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) != 14 && (p) != 15 && (p) != 31 && (p) != 32) +#define IS_PIN_ANALOG(p) ((p) == 0 || ((p) >= 3 && (p) <= 13)) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 25 || (p) == 26) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT UNO32 +#elif defined(_BOARD_UNO_) && defined(__PIC32) // NOTE: no _BOARD_UNO32_ to use +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12 +#define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital +#define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) >= 2) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 45 || (p) == 46) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT DP32 +#elif defined(_BOARD_DP32_) +#define TOTAL_ANALOG_PINS 15 // Really only has 9, but have to override because of mistake in variant file +#define TOTAL_PINS NUM_DIGITAL_PINS // 19 +#define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) (((p) != 1) && ((p) != 4) && ((p) != 5) && ((p) != 15) && ((p) != 16)) +#define IS_PIN_ANALOG(p) ((p) >= 6 && (p) <= 14) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 2 || (p) == 3) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT uC32 +#elif defined(_BOARD_UC32_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12 +#define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital +#define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) >= 2) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 45 || (p) == 46) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT WF32 +#elif defined(_BOARD_WF32_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS +#define TOTAL_PINS NUM_DIGITAL_PINS +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 49) // Accounts for SD and WiFi dedicated pins +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 34 || (p) == 35) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT WiFire +#elif defined(_BOARD_WIFIRE_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 14 +#define TOTAL_PINS NUM_DIGITAL_PINS // 71 +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 47) // Accounts for SD and WiFi dedicated pins +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 34 || (p) == 35) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) <= 25 ? ((p) - 14) : (p) - 36) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT MAX32 +#elif defined(_BOARD_MEGA_) && defined(__PIC32) // NOTE: no _BOARD_MAX32_ to use +#define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 16 +#define TOTAL_PINS NUM_DIGITAL_PINS // 87 +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) ((p) >= 2) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) <= 69) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 34 || (p) == 35) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + + +// Pic32 chipKIT Pi +#elif defined(_BOARD_CHIPKIT_PI_) +#define TOTAL_ANALOG_PINS 16 +#define TOTAL_PINS NUM_DIGITAL_PINS // 19 +#define MAX_SERVOS NUM_DIGITAL_PINS +#define VERSION_BLINK_PIN PIN_LED1 +#define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 3) || (((p) >= 8) && ((p) <= 13)) || (((p) >= 14) && ((p) <= 17))) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 17) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == 16 || (p) == 17) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) <= 15 ? (p) - 14 : (p) - 12) +//#define PIN_TO_ANALOG(p) (((p) <= 16) ? ((p) - 14) : ((p) - 16)) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + +// Pinoccio Scout +// Note: digital pins 9-16 are usable but not labeled on the board numerically. +// SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16 +#elif defined(ARDUINO_PINOCCIO) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS NUM_DIGITAL_PINS // 32 +#define VERSION_BLINK_PIN 23 +#define PIN_SERIAL1_RX 13 +#define PIN_SERIAL1_TX 14 +#define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31)) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + +// ESP8266 +// note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO +#elif defined(ESP8266) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS A0 + NUM_ANALOG_INPUTS +#define PIN_SERIAL_RX 3 +#define PIN_SERIAL_TX 1 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0)) +#define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - A0) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) +#define DEFAULT_PWM_RESOLUTION 10 + +// STM32 based boards +#elif defined(ARDUINO_ARCH_STM32) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS NUM_DIGITAL_PINS +#define TOTAL_PORTS MAX_NB_PORT +#define VERSION_BLINK_PIN LED_BUILTIN +// PIN_SERIALY_RX/TX defined in the variant.h +#define IS_PIN_DIGITAL(p) (digitalPinIsValid(p) && !pinIsSerial(p)) +#define IS_PIN_ANALOG(p) ((p >= A0) && (p < (A0 + TOTAL_ANALOG_PINS)) && !pinIsSerial(p)) +#define IS_PIN_PWM(p) (IS_PIN_DIGITAL(p) && digitalPinHasPWM(p)) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) (IS_PIN_DIGITAL(p) && digitalPinHasI2C(p)) +#define IS_PIN_SPI(p) (IS_PIN_DIGITAL(p) && digitalPinHasSPI(p)) +#define IS_PIN_INTERRUPT(p) (IS_PIN_DIGITAL(p) && (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT))) +#define IS_PIN_SERIAL(p) (digitalPinHasSerial(p) && !pinIsSerial(p)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p-A0) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) +#define DEFAULT_PWM_RESOLUTION PWM_RESOLUTION + +// Adafruit Bluefruit nRF52 boards +#elif defined(ARDUINO_NRF52_ADAFRUIT) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS 32 +#define VERSION_BLINK_PIN LED_BUILTIN +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) == PIN_A0 || (p) == PIN_A1 || (p) == PIN_A2 || (p) == PIN_A3 || \ + (p) == PIN_A4 || (p) == PIN_A5 || (p) == PIN_A6 || (p) == PIN_A7) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ( ((p) == PIN_A0) ? 0 : ((p) == PIN_A1) ? 1 : ((p) == PIN_A2) ? 2 : ((p) == PIN_A3) ? 3 : \ + ((p) == PIN_A4) ? 4 : ((p) == PIN_A5) ? 5 : ((p) == PIN_A6) ? 6 : ((p) == PIN_A7) ? 7 : (127)) +#define PIN_TO_PWM(p) (p) +#define PIN_TO_SERVO(p) (p) + +// anything else +#else +#error "Please edit Boards.h with a hardware abstraction for this board" +#endif + +// as long this is not defined for all boards: +#ifndef IS_PIN_SPI +#define IS_PIN_SPI(p) 0 +#endif + +#ifndef IS_PIN_SERIAL +#define IS_PIN_SERIAL(p) 0 +#endif + +#ifndef DEFAULT_PWM_RESOLUTION +#define DEFAULT_PWM_RESOLUTION 8 +#endif + +/*============================================================================== + * readPort() - Read an 8 bit port + *============================================================================*/ + +static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char readPort(byte port, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1 + if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask; + if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask; + return 0; +#else + unsigned char out = 0, pin = port * 8; + if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01; + if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02; + if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04; + if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08; + if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10; + if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20; + if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40; + if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80; + return out; +#endif +} + +/*============================================================================== + * writePort() - Write an 8 bit port, only touch pins specified by a bitmask + *============================================================================*/ + +static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char writePort(byte port, byte value, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) { + bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins + byte valD = value & bitmask; + byte maskD = ~bitmask; + cli(); + PORTD = (PORTD & maskD) | valD; + sei(); + } else if (port == 1) { + byte valB = (value & bitmask) & 0x3F; + byte valC = (value & bitmask) >> 6; + byte maskB = ~(bitmask & 0x3F); + byte maskC = ~((bitmask & 0xC0) >> 6); + cli(); + PORTB = (PORTB & maskB) | valB; + PORTC = (PORTC & maskC) | valC; + sei(); + } else if (port == 2) { + bitmask = bitmask & 0x0F; + byte valC = (value & bitmask) << 2; + byte maskC = ~(bitmask << 2); + cli(); + PORTC = (PORTC & maskC) | valC; + sei(); + } + return 1; +#else + byte pin = port * 8; + if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01)); + if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02)); + if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04)); + if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08)); + if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10)); + if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20)); + if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40)); + if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80)); + return 1; +#endif +} + + + + +#ifndef TOTAL_PORTS +#define TOTAL_PORTS ((TOTAL_PINS + 7) / 8) +#endif + + +#endif /* Firmata_Boards_h */ diff --git a/libraries/Firmata/Firmata.cpp b/libraries/Firmata/Firmata.cpp new file mode 100644 index 0000000..ee01f8f --- /dev/null +++ b/libraries/Firmata/Firmata.cpp @@ -0,0 +1,554 @@ +/* + Firmata.cpp - Firmata library v2.5.8 - 2018-04-15 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (C) 2009-2017 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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "Firmata.h" +#include "HardwareSerial.h" + +#include +#include + +using namespace firmata; + +//****************************************************************************** +//* Static Members +//****************************************************************************** +// make one instance for the user to use +FirmataClass Firmata; + +/* callback functions */ +callbackFunction FirmataClass::currentAnalogCallback = (callbackFunction)NULL; +callbackFunction FirmataClass::currentDigitalCallback = (callbackFunction)NULL; +callbackFunction FirmataClass::currentPinModeCallback = (callbackFunction)NULL; +callbackFunction FirmataClass::currentPinValueCallback = (callbackFunction)NULL; +callbackFunction FirmataClass::currentReportAnalogCallback = (callbackFunction)NULL; +callbackFunction FirmataClass::currentReportDigitalCallback = (callbackFunction)NULL; +stringCallbackFunction FirmataClass::currentStringCallback = (stringCallbackFunction)NULL; +sysexCallbackFunction FirmataClass::currentSysexCallback = (sysexCallbackFunction)NULL; +systemCallbackFunction FirmataClass::currentSystemResetCallback = (systemCallbackFunction)NULL; + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +/** + * Split a 16-bit byte into two 7-bit values and write each value. + * @param value The 16-bit value to be split and written separately. + */ +void FirmataClass::sendValueAsTwo7bitBytes(int value) +{ + marshaller.encodeByteStream(sizeof(value), reinterpret_cast(&value), sizeof(value)); +} + +/** + * A helper method to write the beginning of a Sysex message transmission. + */ +void FirmataClass::startSysex(void) +{ + FirmataStream->write(START_SYSEX); +} + +/** + * A helper method to write the end of a Sysex message transmission. + */ +void FirmataClass::endSysex(void) +{ + FirmataStream->write(END_SYSEX); +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +/** + * The Firmata class. + * An instance named "Firmata" is created automatically for the user. + */ +FirmataClass::FirmataClass() +: + parser(FirmataParser(parserBuffer, MAX_DATA_BYTES)) +{ + firmwareVersionCount = 0; + firmwareVersionVector = 0; + blinkVersionDisabled = false; + + // Establish callback translation to parser callbacks + parser.attach(ANALOG_MESSAGE, (FirmataParser::callbackFunction)staticAnalogCallback, (void *)NULL); + parser.attach(DIGITAL_MESSAGE, (FirmataParser::callbackFunction)staticDigitalCallback, (void *)NULL); + parser.attach(REPORT_ANALOG, (FirmataParser::callbackFunction)staticReportAnalogCallback, (void *)NULL); + parser.attach(REPORT_DIGITAL, (FirmataParser::callbackFunction)staticReportDigitalCallback, (void *)NULL); + parser.attach(SET_PIN_MODE, (FirmataParser::callbackFunction)staticPinModeCallback, (void *)NULL); + parser.attach(SET_DIGITAL_PIN_VALUE, (FirmataParser::callbackFunction)staticPinValueCallback, (void *)NULL); + parser.attach(STRING_DATA, (FirmataParser::stringCallbackFunction)staticStringCallback, (void *)NULL); + parser.attach(START_SYSEX, (FirmataParser::sysexCallbackFunction)staticSysexCallback, (void *)NULL); + parser.attach(REPORT_FIRMWARE, (FirmataParser::versionCallbackFunction)staticReportFirmwareCallback, this); + parser.attach(REPORT_VERSION, (FirmataParser::systemCallbackFunction)staticReportVersionCallback, this); + parser.attach(SYSTEM_RESET, (FirmataParser::systemCallbackFunction)staticSystemResetCallback, (void *)NULL); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/** + * Initialize the default Serial transport at the default baud of 57600. + */ +void FirmataClass::begin(void) +{ + begin(57600); +} + +/** + * Initialize the default Serial transport and override the default baud. + * Sends the protocol version to the host application followed by the firmware version and name. + * blinkVersion is also called. To skip the call to blinkVersion, call Firmata.disableBlinkVersion() + * before calling Firmata.begin(baud). + * @param speed The baud to use. 57600 baud is the default value. + */ +void FirmataClass::begin(long speed) +{ + Serial.begin(speed); + blinkVersion(); + begin(Serial); +} + +/** + * Reassign the Firmata stream transport. + * @param s A reference to the Stream transport object. This can be any type of + * transport that implements the Stream interface. Some examples include Ethernet, WiFi + * and other UARTs on the board (Serial1, Serial2, etc). + */ +void FirmataClass::begin(Stream &s) +{ + FirmataStream = &s; + marshaller.begin(s); + // do not call blinkVersion() here because some hardware such as the + // Ethernet shield use pin 13 + printVersion(); // send the protocol version + printFirmwareVersion(); // send the firmware name and version +} + +/** + * Send the Firmata protocol version to the Firmata host application. + */ +void FirmataClass::printVersion(void) +{ + marshaller.sendVersion(FIRMATA_PROTOCOL_MAJOR_VERSION, FIRMATA_PROTOCOL_MINOR_VERSION); +} + +/** + * Blink the Firmata protocol version to the onboard LEDs (if the board has an onboard LED). + * If VERSION_BLINK_PIN is not defined in Boards.h for a particular board, then this method + * does nothing. + * The first series of flashes indicates the firmware major version (2 flashes = 2). + * The second series of flashes indicates the firmware minor version (5 flashes = 5). + */ +void FirmataClass::blinkVersion(void) +{ +#if defined(VERSION_BLINK_PIN) + if (blinkVersionDisabled) return; + // flash the pin with the protocol version + pinMode(VERSION_BLINK_PIN, OUTPUT); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MAJOR_VERSION, 40, 210); + delay(250); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MINOR_VERSION, 40, 210); + delay(125); +#endif +} + +/** + * Provides a means to disable the version blink sequence on the onboard LED, trimming startup + * time by a couple of seconds. + * Call this before Firmata.begin(). It only applies when using the default Serial transport. + */ +void FirmataClass::disableBlinkVersion() +{ + blinkVersionDisabled = true; +} + +/** + * Sends the firmware name and version to the Firmata host application. The major and minor version + * numbers are the first 2 bytes in the message. The following bytes are the characters of the + * firmware name. + */ +void FirmataClass::printFirmwareVersion(void) +{ + if (firmwareVersionCount) { // make sure that the name has been set before reporting + marshaller.sendFirmwareVersion(static_cast(firmwareVersionVector[0]), static_cast(firmwareVersionVector[1]), (firmwareVersionCount - 2), reinterpret_cast(&firmwareVersionVector[2])); + } +} + +/** + * Sets the name and version of the firmware. This is not the same version as the Firmata protocol + * (although at times the firmware version and protocol version may be the same number). + * @param name A pointer to the name char array + * @param major The major version number + * @param minor The minor version number + */ +void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte minor) +{ + const char *firmwareName; + const char *extension; + + // parse out ".cpp" and "applet/" that comes from using __FILE__ + extension = strstr(name, ".cpp"); + firmwareName = strrchr(name, '/'); + + if (!firmwareName) { + // windows + firmwareName = strrchr(name, '\\'); + } + if (!firmwareName) { + // user passed firmware name + firmwareName = name; + } else { + firmwareName ++; + } + + if (!extension) { + firmwareVersionCount = strlen(firmwareName) + 2; + } else { + firmwareVersionCount = extension - firmwareName + 2; + } + + // in case anyone calls setFirmwareNameAndVersion more than once + free(firmwareVersionVector); + + firmwareVersionVector = (byte *) malloc(firmwareVersionCount + 1); + firmwareVersionVector[firmwareVersionCount] = 0; + firmwareVersionVector[0] = major; + firmwareVersionVector[1] = minor; + strncpy((char *)firmwareVersionVector + 2, firmwareName, firmwareVersionCount - 2); +} + +//------------------------------------------------------------------------------ +// Serial Receive Handling + +/** + * A wrapper for Stream::available() + * @return The number of bytes remaining in the input stream buffer. + */ +int FirmataClass::available(void) +{ + return FirmataStream->available(); +} + +/** + * Read a single int from the input stream. If the value is not = -1, pass it on to parse(byte) + */ +void FirmataClass::processInput(void) +{ + int inputData = FirmataStream->read(); // this is 'int' to handle -1 when no data + if (inputData != -1) { + parser.parse(inputData); + } +} + +/** + * Parse data from the input stream. + * @param inputData A single byte to be added to the parser. + */ +void FirmataClass::parse(byte inputData) +{ + parser.parse(inputData); +} + +/** + * @return Returns true if the parser is actively parsing data. + */ +boolean FirmataClass::isParsingMessage(void) +{ + return parser.isParsingMessage(); +} + +//------------------------------------------------------------------------------ +// Output Stream Handling + +/** + * Send an analog message to the Firmata host application. The range of pins is limited to [0..15] + * when using the ANALOG_MESSAGE. The maximum value of the ANALOG_MESSAGE is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin to send the value of (limited to pins 0 - 15). + * @param value The value of the analog pin (0 - 1024 for 10-bit analog, 0 - 4096 for 12-bit, etc). + * The maximum value is 14-bits (16384). + */ +void FirmataClass::sendAnalog(byte pin, int value) +{ + marshaller.sendAnalog(pin, value); +} + +/* (intentionally left out asterix here) + * STUB - NOT IMPLEMENTED + * Send a single digital pin value to the Firmata host application. + * @param pin The digital pin to send the value of. + * @param value The value of the pin. + */ +void FirmataClass::sendDigital(byte pin, int value) +{ + (void)pin; + (void)value; + /* TODO add single pin digital messages to the protocol, this needs to + * track the last digital data sent so that it can be sure to change just + * one bit in the packet. This is complicated by the fact that the + * numbering of the pins will probably differ on Arduino, Wiring, and + * other boards. + */ + + // TODO: the digital message should not be sent on the serial port every + // time sendDigital() is called. Instead, it should add it to an int + // which will be sent on a schedule. If a pin changes more than once + // before the digital message is sent on the serial port, it should send a + // digital message for each change. + + // if(value == 0) + // sendDigitalPortPair(); +} + + +/** + * Send an 8-bit port in a single digital message (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number to send. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + * @param portData The value of the port. The value of each pin in the port is represented by a bit. + */ +void FirmataClass::sendDigitalPort(byte portNumber, int portData) +{ + marshaller.sendDigitalPort(portNumber, portData); +} + +/** + * Send a sysex message where all values after the command byte are packet as 2 7-bit bytes + * (this is not always the case so this function is not always used to send sysex messages). + * @param command The sysex command byte. + * @param bytec The number of data bytes in the message (excludes start, command and end bytes). + * @param bytev A pointer to the array of data bytes to send in the message. + */ +void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev) +{ + marshaller.sendSysex(command, bytec, bytev); +} + +/** + * Send a string to the Firmata host application. + * @param command Must be STRING_DATA + * @param string A pointer to the char string + */ +void FirmataClass::sendString(byte command, const char *string) +{ + if (command == STRING_DATA) { + marshaller.sendString(string); + } +} + +/** + * Send a string to the Firmata host application. + * @param string A pointer to the char string + */ +void FirmataClass::sendString(const char *string) +{ + marshaller.sendString(string); +} + +/** + * A wrapper for Stream::available(). + * Write a single byte to the output stream. + * @param c The byte to be written. + */ +void FirmataClass::write(byte c) +{ + FirmataStream->write(c); +} + +/** + * Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE, + * DIGITAL_MESSAGE, REPORT_ANALOG, REPORT DIGITAL, SET_PIN_MODE and SET_DIGITAL_PIN_VALUE). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + */ +void FirmataClass::attach(uint8_t command, ::callbackFunction newFunction) +{ + switch (command) { + case ANALOG_MESSAGE: + currentAnalogCallback = newFunction; + break; + case DIGITAL_MESSAGE: + currentDigitalCallback = newFunction; + break; + case REPORT_ANALOG: + currentReportAnalogCallback = newFunction; + break; + case REPORT_DIGITAL: + currentReportDigitalCallback = newFunction; + break; + case SET_PIN_MODE: + currentPinModeCallback = newFunction; + break; + case SET_DIGITAL_PIN_VALUE: + currentPinValueCallback = newFunction; + break; + } +} + +/** + * Attach a callback function for the SYSTEM_RESET command. + * @param command Must be set to SYSTEM_RESET or it will be ignored. + * @param newFunction A reference to the system reset callback function to attach. + */ +void FirmataClass::attach(uint8_t command, systemCallbackFunction newFunction) +{ + switch (command) { + case SYSTEM_RESET: + currentSystemResetCallback = newFunction; + break; + } +} + +/** + * Attach a callback function for the STRING_DATA command. + * @param command Must be set to STRING_DATA or it will be ignored. + * @param newFunction A reference to the string callback function to attach. + */ +void FirmataClass::attach(uint8_t command, stringCallbackFunction newFunction) +{ + switch (command) { + case STRING_DATA: + currentStringCallback = newFunction; + break; + } +} + +/** + * Attach a generic sysex callback function to sysex command. + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the sysex callback function to attach. + */ +void FirmataClass::attach(uint8_t command, sysexCallbackFunction newFunction) +{ + (void)command; + currentSysexCallback = newFunction; +} + +/** + * Detach a callback function for a specified command (such as SYSTEM_RESET, STRING_DATA, + * ANALOG_MESSAGE, DIGITAL_MESSAGE, etc). + * @param command The ID of the command to detatch the callback function from. + */ +void FirmataClass::detach(uint8_t command) +{ + switch (command) { + case SYSTEM_RESET: + attach(command, (systemCallbackFunction)NULL); + break; + case STRING_DATA: + attach(command, (stringCallbackFunction)NULL); + break; + case START_SYSEX: + attach(command, (sysexCallbackFunction)NULL); + break; + default: + attach(command, (callbackFunction)NULL); + break; + } +} + +/** + * @param pin The pin to get the configuration of. + * @return The configuration of the specified pin. + */ +byte FirmataClass::getPinMode(byte pin) +{ + return pinConfig[pin]; +} + +/** + * Set the pin mode/configuration. The pin configuration (or mode) in Firmata represents the + * current function of the pin. Examples are digital input or output, analog input, pwm, i2c, + * serial (uart), etc. + * @param pin The pin to configure. + * @param config The configuration value for the specified pin. + */ +void FirmataClass::setPinMode(byte pin, byte config) +{ + if (pinConfig[pin] == PIN_MODE_IGNORE) + return; + + pinConfig[pin] = config; +} + +/** + * @param pin The pin to get the state of. + * @return The state of the specified pin. + */ +int FirmataClass::getPinState(byte pin) +{ + return pinState[pin]; +} + +/** + * Set the pin state. The pin state of an output pin is the pin value. The state of an + * input pin is 0, unless the pin has it's internal pull up resistor enabled, then the value is 1. + * @param pin The pin to set the state of + * @param state Set the state of the specified pin + */ +void FirmataClass::setPinState(byte pin, int state) +{ + pinState[pin] = state; +} + +// sysex callbacks +/* + * this is too complicated for analogReceive, but maybe for Sysex? + void FirmataClass::attachSysex(sysexFunction newFunction) + { + byte i; + byte tmpCount = analogReceiveFunctionCount; + analogReceiveFunction* tmpArray = analogReceiveFunctionArray; + analogReceiveFunctionCount++; + analogReceiveFunctionArray = (analogReceiveFunction*) calloc(analogReceiveFunctionCount, sizeof(analogReceiveFunction)); + for(i = 0; i < tmpCount; i++) { + analogReceiveFunctionArray[i] = tmpArray[i]; + } + analogReceiveFunctionArray[tmpCount] = newFunction; + free(tmpArray); + } +*/ + +//****************************************************************************** +//* Private Methods +//****************************************************************************** + +/** + * Flashing the pin for the version number + * @private + * @param pin The pin the LED is attached to. + * @param count The number of times to flash the LED. + * @param onInterval The number of milliseconds for the LED to be ON during each interval. + * @param offInterval The number of milliseconds for the LED to be OFF during each interval. + */ +void FirmataClass::strobeBlinkPin(byte pin, int count, int onInterval, int offInterval) +{ + byte i; + for (i = 0; i < count; i++) { + delay(offInterval); + digitalWrite(pin, HIGH); + delay(onInterval); + digitalWrite(pin, LOW); + } +} + diff --git a/libraries/Firmata/Firmata.h b/libraries/Firmata/Firmata.h new file mode 100644 index 0000000..fa0fa6c --- /dev/null +++ b/libraries/Firmata/Firmata.h @@ -0,0 +1,180 @@ +/* + Firmata.h - Firmata library v2.5.8 - 2018-04-15 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (C) 2009-2017 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. +*/ + +#ifndef Firmata_h +#define Firmata_h + +#include "Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */ +#include "FirmataDefines.h" +#include "FirmataMarshaller.h" +#include "FirmataParser.h" + +/* DEPRECATED as of Firmata v2.5.1. As of 2.5.1 there are separate version numbers for + * the protocol version and the firmware version. + */ +#define FIRMATA_MAJOR_VERSION 2 // same as FIRMATA_PROTOCOL_MAJOR_VERSION +#define FIRMATA_MINOR_VERSION 5 // same as FIRMATA_PROTOCOL_MINOR_VERSION +#define FIRMATA_BUGFIX_VERSION 1 // same as FIRMATA_PROTOCOL_BUGFIX_VERSION + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ +// these are DEPRECATED to make the naming more consistent +#define FIRMATA_STRING 0x71 // same as STRING_DATA +#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST +#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY +#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL + +// pin modes +//#define INPUT 0x00 // defined in Arduino.h +//#define OUTPUT 0x01 // defined in Arduino.h +// DEPRECATED as of Firmata v2.5 +#define ANALOG 0x02 // same as PIN_MODE_ANALOG +#define PWM 0x03 // same as PIN_MODE_PWM +#define SERVO 0x04 // same as PIN_MODE_SERVO +#define SHIFT 0x05 // same as PIN_MODE_SHIFT +#define I2C 0x06 // same as PIN_MODE_I2C +#define ONEWIRE 0x07 // same as PIN_MODE_ONEWIRE +#define STEPPER 0x08 // same as PIN_MODE_STEPPER +#define ENCODER 0x09 // same as PIN_MODE_ENCODER +#define IGNORE 0x7F // same as PIN_MODE_IGNORE + +namespace firmata { + +// TODO make it a subclass of a generic Serial/Stream base class +class FirmataClass +{ + public: + typedef void (*callbackFunction)(uint8_t, int); + typedef void (*systemCallbackFunction)(void); + typedef void (*stringCallbackFunction)(char *); + typedef void (*sysexCallbackFunction)(uint8_t command, uint8_t argc, uint8_t *argv); + + FirmataClass(); + + /* Arduino constructors */ + void begin(); + void begin(long); + void begin(Stream &s); + + /* querying functions */ + void printVersion(void); + void blinkVersion(void); + void printFirmwareVersion(void); + + //void setFirmwareVersion(byte major, byte minor); // see macro below + void setFirmwareNameAndVersion(const char *name, byte major, byte minor); + void disableBlinkVersion(); + + /* serial receive handling */ + int available(void); + void processInput(void); + void parse(unsigned char value); + boolean isParsingMessage(void); + + /* serial send handling */ + void sendAnalog(byte pin, int value); + void sendDigital(byte pin, int value); // TODO implement this + void sendDigitalPort(byte portNumber, int portData); + void sendString(const char *string); + void sendString(byte command, const char *string); + void sendSysex(byte command, byte bytec, byte *bytev); + void write(byte c); + + /* attach & detach callback functions to messages */ + void attach(uint8_t command, callbackFunction newFunction); + void attach(uint8_t command, systemCallbackFunction newFunction); + void attach(uint8_t command, stringCallbackFunction newFunction); + void attach(uint8_t command, sysexCallbackFunction newFunction); + void detach(uint8_t command); + + /* access pin state and config */ + byte getPinMode(byte pin); + void setPinMode(byte pin, byte config); + + /* access pin state */ + int getPinState(byte pin); + void setPinState(byte pin, int state); + + /* utility methods */ + void sendValueAsTwo7bitBytes(int value); + void startSysex(void); + void endSysex(void); + + private: + uint8_t parserBuffer[MAX_DATA_BYTES]; + FirmataMarshaller marshaller; + FirmataParser parser; + Stream *FirmataStream; + + /* firmware name and version */ + byte firmwareVersionCount; + byte *firmwareVersionVector; + + /* pin configuration */ + byte pinConfig[TOTAL_PINS]; + int pinState[TOTAL_PINS]; + + boolean blinkVersionDisabled; + + /* private methods ------------------------------ */ + void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval); + friend void FirmataMarshaller::encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes = 0) const; + + /* callback functions */ + static callbackFunction currentAnalogCallback; + static callbackFunction currentDigitalCallback; + static callbackFunction currentPinModeCallback; + static callbackFunction currentPinValueCallback; + static callbackFunction currentReportAnalogCallback; + static callbackFunction currentReportDigitalCallback; + static stringCallbackFunction currentStringCallback; + static sysexCallbackFunction currentSysexCallback; + static systemCallbackFunction currentSystemResetCallback; + + /* static callbacks */ + inline static void staticAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentAnalogCallback ) { currentAnalogCallback(command,(int)value); } } + inline static void staticDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentDigitalCallback ) { currentDigitalCallback(command, (int)value); } } + inline static void staticPinModeCallback (void *, uint8_t command, uint16_t value) { if ( currentPinModeCallback ) { currentPinModeCallback(command, (int)value); } } + inline static void staticPinValueCallback (void *, uint8_t command, uint16_t value) { if ( currentPinValueCallback ) { currentPinValueCallback(command, (int)value); } } + inline static void staticReportAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentReportAnalogCallback ) { currentReportAnalogCallback(command, (int)value); } } + inline static void staticReportDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentReportDigitalCallback ) { currentReportDigitalCallback(command, (int)value); } } + inline static void staticStringCallback (void *, const char * c_str) { if ( currentStringCallback ) { currentStringCallback((char *)c_str); } } + inline static void staticSysexCallback (void *, uint8_t command, size_t argc, uint8_t *argv) { if ( currentSysexCallback ) { currentSysexCallback(command, (uint8_t)argc, argv); } } + inline static void staticReportFirmwareCallback (void * context, size_t, size_t, const char *) { if ( context ) { ((FirmataClass *)context)->printFirmwareVersion(); } } + inline static void staticReportVersionCallback (void * context) { if ( context ) { ((FirmataClass *)context)->printVersion(); } } + inline static void staticSystemResetCallback (void *) { if ( currentSystemResetCallback ) { currentSystemResetCallback(); } } +}; + +} // namespace firmata + +extern "C" { + // callback function types + typedef firmata::FirmataClass::callbackFunction callbackFunction; + typedef firmata::FirmataClass::systemCallbackFunction systemCallbackFunction; + typedef firmata::FirmataClass::stringCallbackFunction stringCallbackFunction; + typedef firmata::FirmataClass::sysexCallbackFunction sysexCallbackFunction; +} + +extern firmata::FirmataClass Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the + * firmware name. It needs to be a macro so that __FILE__ is included in the + * firmware source file rather than the library source file. + */ +#define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y) + +#endif /* Firmata_h */ diff --git a/libraries/Firmata/FirmataConstants.h b/libraries/Firmata/FirmataConstants.h new file mode 100644 index 0000000..dee8407 --- /dev/null +++ b/libraries/Firmata/FirmataConstants.h @@ -0,0 +1,97 @@ +/* + FirmataConstants.h + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (C) 2009-2017 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. +*/ + +#ifndef FirmataConstants_h +#define FirmataConstants_h + +namespace firmata { +/* Version numbers for the Firmata library. + * The firmware version will not always equal the protocol version going forward. + * Query using the REPORT_FIRMWARE message. + */ +static const int FIRMWARE_MAJOR_VERSION = 2; +static const int FIRMWARE_MINOR_VERSION = 5; +static const int FIRMWARE_BUGFIX_VERSION = 7; + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. + * Query using the REPORT_VERSION message. + */ +static const int PROTOCOL_MAJOR_VERSION = 2; // for non-compatible changes +static const int PROTOCOL_MINOR_VERSION = 5; // for backwards compatible changes +static const int PROTOCOL_BUGFIX_VERSION = 1; // for bugfix releases + +static const int MAX_DATA_BYTES = 64; // max number of data bytes in incoming messages + +// message command bytes (128-255/0x80-0xFF) + +static const int DIGITAL_MESSAGE = 0x90; // send data for a digital port (collection of 8 pins) +static const int ANALOG_MESSAGE = 0xE0; // send data for an analog pin (or PWM) +static const int REPORT_ANALOG = 0xC0; // enable analog input by pin # +static const int REPORT_DIGITAL = 0xD0; // enable digital input by port pair +// +static const int SET_PIN_MODE = 0xF4; // set a pin to INPUT/OUTPUT/PWM/etc +static const int SET_DIGITAL_PIN_VALUE = 0xF5; // set value of an individual digital pin +// +static const int REPORT_VERSION = 0xF9; // report protocol version +static const int SYSTEM_RESET = 0xFF; // reset from MIDI +// +static const int START_SYSEX = 0xF0; // start a MIDI Sysex message +static const int END_SYSEX = 0xF7; // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ + +static const int SERIAL_DATA = 0x60; // communicate with serial devices, including other boards +static const int ENCODER_DATA = 0x61; // reply with encoders current positions +static const int SERVO_CONFIG = 0x70; // set max angle, minPulse, maxPulse, freq +static const int STRING_DATA = 0x71; // a string message with 14-bits per char +static const int STEPPER_DATA = 0x72; // control a stepper motor +static const int ONEWIRE_DATA = 0x73; // send an OneWire read/write/reset/select/skip/search request +static const int SHIFT_DATA = 0x75; // a bitstream to/from a shift register +static const int I2C_REQUEST = 0x76; // send an I2C read/write request +static const int I2C_REPLY = 0x77; // a reply to an I2C read request +static const int I2C_CONFIG = 0x78; // config I2C settings such as delay times and power pins +static const int REPORT_FIRMWARE = 0x79; // report name and version of the firmware +static const int EXTENDED_ANALOG = 0x6F; // analog write (PWM, Servo, etc) to any pin +static const int PIN_STATE_QUERY = 0x6D; // ask for a pin's current mode and value +static const int PIN_STATE_RESPONSE = 0x6E; // reply with pin's current mode and value +static const int CAPABILITY_QUERY = 0x6B; // ask for supported modes and resolution of all pins +static const int CAPABILITY_RESPONSE = 0x6C; // reply with supported modes and resolution +static const int ANALOG_MAPPING_QUERY = 0x69; // ask for mapping of analog to pin numbers +static const int ANALOG_MAPPING_RESPONSE = 0x6A; // reply with mapping info +static const int SAMPLING_INTERVAL = 0x7A; // set the poll rate of the main loop +static const int SCHEDULER_DATA = 0x7B; // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler +static const int SYSEX_NON_REALTIME = 0x7E; // MIDI Reserved for non-realtime messages +static const int SYSEX_REALTIME = 0x7F; // MIDI Reserved for realtime messages + +// pin modes +static const int PIN_MODE_INPUT = 0x00; // same as INPUT defined in Arduino.h +static const int PIN_MODE_OUTPUT = 0x01; // same as OUTPUT defined in Arduino.h +static const int PIN_MODE_ANALOG = 0x02; // analog pin in analogInput mode +static const int PIN_MODE_PWM = 0x03; // digital pin in PWM output mode +static const int PIN_MODE_SERVO = 0x04; // digital pin in Servo output mode +static const int PIN_MODE_SHIFT = 0x05; // shiftIn/shiftOut mode +static const int PIN_MODE_I2C = 0x06; // pin included in I2C setup +static const int PIN_MODE_ONEWIRE = 0x07; // pin configured for 1-wire +static const int PIN_MODE_STEPPER = 0x08; // pin configured for stepper motor +static const int PIN_MODE_ENCODER = 0x09; // pin configured for rotary encoders +static const int PIN_MODE_SERIAL = 0x0A; // pin configured for serial communication +static const int PIN_MODE_PULLUP = 0x0B; // enable internal pull-up resistor for pin +static const int PIN_MODE_IGNORE = 0x7F; // pin configured to be ignored by digitalWrite and capabilityResponse + +static const int TOTAL_PIN_MODES = 13; + +} // namespace firmata + +#endif // FirmataConstants_h diff --git a/libraries/Firmata/FirmataDefines.h b/libraries/Firmata/FirmataDefines.h new file mode 100644 index 0000000..fb95fb5 --- /dev/null +++ b/libraries/Firmata/FirmataDefines.h @@ -0,0 +1,283 @@ +/* + FirmataDefines.h + Copyright (c) 2006-2008 Hans-Christoph Steiner. 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. +*/ + +#ifndef FirmataDefines_h +#define FirmataDefines_h + +#include "FirmataConstants.h" + +/* Version numbers for the Firmata library. + * The firmware version will not always equal the protocol version going forward. + * Query using the REPORT_FIRMWARE message. + */ +#define FIRMATA_FIRMWARE_MAJOR_VERSION firmata::FIRMWARE_MAJOR_VERSION +#define FIRMATA_FIRMWARE_MINOR_VERSION firmata::FIRMWARE_MINOR_VERSION +#define FIRMATA_FIRMWARE_BUGFIX_VERSION firmata::FIRMWARE_BUGFIX_VERSION + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. + * Query using the REPORT_VERSION message. + */ +#define FIRMATA_PROTOCOL_MAJOR_VERSION firmata::PROTOCOL_MAJOR_VERSION // for non-compatible changes +#define FIRMATA_PROTOCOL_MINOR_VERSION firmata::PROTOCOL_MINOR_VERSION // for backwards compatible changes +#define FIRMATA_PROTOCOL_BUGFIX_VERSION firmata::PROTOCOL_BUGFIX_VERSION // for bugfix releases + +#ifdef MAX_DATA_BYTES +#undef MAX_DATA_BYTES +#endif +#define MAX_DATA_BYTES firmata::MAX_DATA_BYTES // max number of data bytes in incoming messages + +// message command bytes (128-255/0x80-0xFF) + +#ifdef DIGITAL_MESSAGE +#undef DIGITAL_MESSAGE +#endif +#define DIGITAL_MESSAGE firmata::DIGITAL_MESSAGE // send data for a digital port (collection of 8 pins) + +#ifdef ANALOG_MESSAGE +#undef ANALOG_MESSAGE +#endif +#define ANALOG_MESSAGE firmata::ANALOG_MESSAGE // send data for an analog pin (or PWM) + +#ifdef REPORT_ANALOG +#undef REPORT_ANALOG +#endif +#define REPORT_ANALOG firmata::REPORT_ANALOG // enable analog input by pin # + +#ifdef REPORT_DIGITAL +#undef REPORT_DIGITAL +#endif +#define REPORT_DIGITAL firmata::REPORT_DIGITAL // enable digital input by port pair + +// + +#ifdef SET_PIN_MODE +#undef SET_PIN_MODE +#endif +#define SET_PIN_MODE firmata::SET_PIN_MODE // set a pin to INPUT/OUTPUT/PWM/etc + +#ifdef SET_DIGITAL_PIN_VALUE +#undef SET_DIGITAL_PIN_VALUE +#endif +#define SET_DIGITAL_PIN_VALUE firmata::SET_DIGITAL_PIN_VALUE // set value of an individual digital pin + +// + +#ifdef REPORT_VERSION +#undef REPORT_VERSION +#endif +#define REPORT_VERSION firmata::REPORT_VERSION // report protocol version + +#ifdef SYSTEM_RESET +#undef SYSTEM_RESET +#endif +#define SYSTEM_RESET firmata::SYSTEM_RESET // reset from MIDI + +// + +#ifdef START_SYSEX +#undef START_SYSEX +#endif +#define START_SYSEX firmata::START_SYSEX // start a MIDI Sysex message + +#ifdef END_SYSEX +#undef END_SYSEX +#endif +#define END_SYSEX firmata::END_SYSEX // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ + +#ifdef SERIAL_MESSAGE +#undef SERIAL_MESSAGE +#endif +#define SERIAL_MESSAGE firmata::SERIAL_DATA // communicate with serial devices, including other boards + +#ifdef ENCODER_DATA +#undef ENCODER_DATA +#endif +#define ENCODER_DATA firmata::ENCODER_DATA // reply with encoders current positions + +#ifdef SERVO_CONFIG +#undef SERVO_CONFIG +#endif +#define SERVO_CONFIG firmata::SERVO_CONFIG // set max angle, minPulse, maxPulse, freq + +#ifdef STRING_DATA +#undef STRING_DATA +#endif +#define STRING_DATA firmata::STRING_DATA // a string message with 14-bits per char + +#ifdef STEPPER_DATA +#undef STEPPER_DATA +#endif +#define STEPPER_DATA firmata::STEPPER_DATA // control a stepper motor + +#ifdef ONEWIRE_DATA +#undef ONEWIRE_DATA +#endif +#define ONEWIRE_DATA firmata::ONEWIRE_DATA // send an OneWire read/write/reset/select/skip/search request + +#ifdef SHIFT_DATA +#undef SHIFT_DATA +#endif +#define SHIFT_DATA firmata::SHIFT_DATA // a bitstream to/from a shift register + +#ifdef I2C_REQUEST +#undef I2C_REQUEST +#endif +#define I2C_REQUEST firmata::I2C_REQUEST // send an I2C read/write request + +#ifdef I2C_REPLY +#undef I2C_REPLY +#endif +#define I2C_REPLY firmata::I2C_REPLY // a reply to an I2C read request + +#ifdef I2C_CONFIG +#undef I2C_CONFIG +#endif +#define I2C_CONFIG firmata::I2C_CONFIG // config I2C settings such as delay times and power pins + +#ifdef REPORT_FIRMWARE +#undef REPORT_FIRMWARE +#endif +#define REPORT_FIRMWARE firmata::REPORT_FIRMWARE // report name and version of the firmware + +#ifdef EXTENDED_ANALOG +#undef EXTENDED_ANALOG +#endif +#define EXTENDED_ANALOG firmata::EXTENDED_ANALOG // analog write (PWM, Servo, etc) to any pin + +#ifdef PIN_STATE_QUERY +#undef PIN_STATE_QUERY +#endif +#define PIN_STATE_QUERY firmata::PIN_STATE_QUERY // ask for a pin's current mode and value + +#ifdef PIN_STATE_RESPONSE +#undef PIN_STATE_RESPONSE +#endif +#define PIN_STATE_RESPONSE firmata::PIN_STATE_RESPONSE // reply with pin's current mode and value + +#ifdef CAPABILITY_QUERY +#undef CAPABILITY_QUERY +#endif +#define CAPABILITY_QUERY firmata::CAPABILITY_QUERY // ask for supported modes and resolution of all pins + +#ifdef CAPABILITY_RESPONSE +#undef CAPABILITY_RESPONSE +#endif +#define CAPABILITY_RESPONSE firmata::CAPABILITY_RESPONSE // reply with supported modes and resolution + +#ifdef ANALOG_MAPPING_QUERY +#undef ANALOG_MAPPING_QUERY +#endif +#define ANALOG_MAPPING_QUERY firmata::ANALOG_MAPPING_QUERY // ask for mapping of analog to pin numbers + +#ifdef ANALOG_MAPPING_RESPONSE +#undef ANALOG_MAPPING_RESPONSE +#endif +#define ANALOG_MAPPING_RESPONSE firmata::ANALOG_MAPPING_RESPONSE // reply with mapping info + +#ifdef SAMPLING_INTERVAL +#undef SAMPLING_INTERVAL +#endif +#define SAMPLING_INTERVAL firmata::SAMPLING_INTERVAL // set the poll rate of the main loop + +#ifdef SCHEDULER_DATA +#undef SCHEDULER_DATA +#endif +#define SCHEDULER_DATA firmata::SCHEDULER_DATA // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler + +#ifdef SYSEX_NON_REALTIME +#undef SYSEX_NON_REALTIME +#endif +#define SYSEX_NON_REALTIME firmata::SYSEX_NON_REALTIME // MIDI Reserved for non-realtime messages + +#ifdef SYSEX_REALTIME +#undef SYSEX_REALTIME +#endif +#define SYSEX_REALTIME firmata::SYSEX_REALTIME // MIDI Reserved for realtime messages + +// pin modes + +#ifdef PIN_MODE_INPUT +#undef PIN_MODE_INPUT +#endif +#define PIN_MODE_INPUT firmata::PIN_MODE_INPUT // same as INPUT defined in Arduino.h + +#ifdef PIN_MODE_OUTPUT +#undef PIN_MODE_OUTPUT +#endif +#define PIN_MODE_OUTPUT firmata::PIN_MODE_OUTPUT // same as OUTPUT defined in Arduino.h + +#ifdef PIN_MODE_ANALOG +#undef PIN_MODE_ANALOG +#endif +#define PIN_MODE_ANALOG firmata::PIN_MODE_ANALOG // analog pin in analogInput mode + +#ifdef PIN_MODE_PWM +#undef PIN_MODE_PWM +#endif +#define PIN_MODE_PWM firmata::PIN_MODE_PWM // digital pin in PWM output mode + +#ifdef PIN_MODE_SERVO +#undef PIN_MODE_SERVO +#endif +#define PIN_MODE_SERVO firmata::PIN_MODE_SERVO // digital pin in Servo output mode + +#ifdef PIN_MODE_SHIFT +#undef PIN_MODE_SHIFT +#endif +#define PIN_MODE_SHIFT firmata::PIN_MODE_SHIFT // shiftIn/shiftOut mode + +#ifdef PIN_MODE_I2C +#undef PIN_MODE_I2C +#endif +#define PIN_MODE_I2C firmata::PIN_MODE_I2C // pin included in I2C setup + +#ifdef PIN_MODE_ONEWIRE +#undef PIN_MODE_ONEWIRE +#endif +#define PIN_MODE_ONEWIRE firmata::PIN_MODE_ONEWIRE // pin configured for 1-wire + +#ifdef PIN_MODE_STEPPER +#undef PIN_MODE_STEPPER +#endif +#define PIN_MODE_STEPPER firmata::PIN_MODE_STEPPER // pin configured for stepper motor + +#ifdef PIN_MODE_ENCODER +#undef PIN_MODE_ENCODER +#endif +#define PIN_MODE_ENCODER firmata::PIN_MODE_ENCODER // pin configured for rotary encoders + +#ifdef PIN_MODE_SERIAL +#undef PIN_MODE_SERIAL +#endif +#define PIN_MODE_SERIAL firmata::PIN_MODE_SERIAL // pin configured for serial communication + +#ifdef PIN_MODE_PULLUP +#undef PIN_MODE_PULLUP +#endif +#define PIN_MODE_PULLUP firmata::PIN_MODE_PULLUP // enable internal pull-up resistor for pin + +#ifdef PIN_MODE_IGNORE +#undef PIN_MODE_IGNORE +#endif +#define PIN_MODE_IGNORE firmata::PIN_MODE_IGNORE // pin configured to be ignored by digitalWrite and capabilityResponse + +#ifdef TOTAL_PIN_MODES +#undef TOTAL_PIN_MODES +#endif +#define TOTAL_PIN_MODES firmata::TOTAL_PIN_MODES + +#endif // FirmataConstants_h diff --git a/libraries/Firmata/FirmataMarshaller.cpp b/libraries/Firmata/FirmataMarshaller.cpp new file mode 100644 index 0000000..6cb5200 --- /dev/null +++ b/libraries/Firmata/FirmataMarshaller.cpp @@ -0,0 +1,431 @@ +/* + FirmataMarshaller.cpp + Copyright (c) 2006-2008 Hans-Christoph Steiner. 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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "FirmataMarshaller.h" + +#if defined(__cplusplus) && !defined(ARDUINO) + #include +#else + #include +#endif + +#include "FirmataConstants.h" + +using namespace firmata; + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +/** + * Request or halt a stream of analog readings from the Firmata host application. The range of pins is + * limited to [0..15] when using the REPORT_ANALOG. The maximum result of the REPORT_ANALOG is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin for which to request the value (limited to pins 0 - 15). + * @param stream_enable A zero value will disable the stream, a non-zero will enable the stream + * @note The maximum resulting value is 14-bits (16384). + */ +void FirmataMarshaller::reportAnalog(uint8_t pin, bool stream_enable) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + // pin can only be 0-15, so chop higher bits + FirmataStream->write(REPORT_ANALOG | (pin & 0xF)); + FirmataStream->write(stream_enable); +} + +/** + * Request or halt an 8-bit port stream from the Firmata host application (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number for which to request the value. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + * @param stream_enable A zero value will disable the stream, a non-zero will enable the stream + */ +void FirmataMarshaller::reportDigitalPort(uint8_t portNumber, bool stream_enable) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(REPORT_DIGITAL | (portNumber & 0xF)); + FirmataStream->write(stream_enable); +} + +/** + * An alternative to the normal analog message, this extended version allows addressing beyond + * pin 15 and supports sending analog values with any number of bits. + * @param pin The analog pin to which the value is sent. + * @param bytec The size of the storage for the analog value + * @param bytev The pointer to the location of the analog value + */ +void FirmataMarshaller::sendExtendedAnalog(uint8_t pin, size_t bytec, uint8_t * bytev) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(START_SYSEX); + FirmataStream->write(EXTENDED_ANALOG); + FirmataStream->write(pin); + encodeByteStream(bytec, bytev, bytec); + FirmataStream->write(END_SYSEX); +} + +/** + * Transform 8-bit stream into 7-bit message + * @param bytec The number of data bytes in the message. + * @param bytev A pointer to the array of data bytes to send in the message. + * @param max_bytes Force message to be n bytes, regardless of data bits. + */ +void FirmataMarshaller::encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes) +const +{ + static const size_t transmit_bits = 7; + static const uint8_t transmit_mask = ((1 << transmit_bits) - 1); + + size_t bytes_sent = 0; + size_t outstanding_bits = 0; + uint8_t outstanding_bit_cache = *bytev; + + if ( !max_bytes ) { max_bytes = static_cast(-1); } + for (size_t i = 0 ; (i < bytec) && (bytes_sent < max_bytes) ; ++i) { + uint8_t transmit_byte = (outstanding_bit_cache|(bytev[i] << outstanding_bits)); + FirmataStream->write(transmit_mask & transmit_byte); + ++bytes_sent; + outstanding_bit_cache = (bytev[i] >> (transmit_bits - outstanding_bits)); + outstanding_bits = (outstanding_bits + (8 - transmit_bits)); + for ( ; (outstanding_bits >= transmit_bits) && (bytes_sent < max_bytes) ; ) { + transmit_byte = outstanding_bit_cache; + FirmataStream->write(transmit_mask & transmit_byte); + ++bytes_sent; + outstanding_bit_cache >>= transmit_bits; + outstanding_bits -= transmit_bits; + } + } + if ( outstanding_bits && (bytes_sent < max_bytes) ) { + FirmataStream->write(static_cast((1 << outstanding_bits) - 1) & outstanding_bit_cache); + } +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +/** + * The FirmataMarshaller class. + */ +FirmataMarshaller::FirmataMarshaller() +: + FirmataStream((Stream *)NULL) +{ +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/** + * Reassign the Firmata stream transport. + * @param s A reference to the Stream transport object. This can be any type of + * transport that implements the Stream interface. Some examples include Ethernet, WiFi + * and other UARTs on the board (Serial1, Serial2, etc). + */ +void FirmataMarshaller::begin(Stream &s) +{ + FirmataStream = &s; +} + +/** + * Closes the FirmataMarshaller stream by setting its stream reference to `(Stream *)NULL` + */ +void FirmataMarshaller::end(void) +{ + FirmataStream = (Stream *)NULL; +} + +//****************************************************************************** +//* Output Stream Handling +//****************************************************************************** + +/** + * Query the target's firmware name and version + */ +void FirmataMarshaller::queryFirmwareVersion(void) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(START_SYSEX); + FirmataStream->write(REPORT_FIRMWARE); + FirmataStream->write(END_SYSEX); +} + +/** + * Query the target's Firmata protocol version + */ +void FirmataMarshaller::queryVersion(void) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(REPORT_VERSION); +} + +/** + * Halt the stream of analog readings from the Firmata host application. The range of pins is + * limited to [0..15] when using the REPORT_ANALOG. The maximum result of the REPORT_ANALOG is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin for which to request the value (limited to pins 0 - 15). + */ +void FirmataMarshaller::reportAnalogDisable(uint8_t pin) +const +{ + reportAnalog(pin, false); +} + +/** + * Request a stream of analog readings from the Firmata host application. The range of pins is + * limited to [0..15] when using the REPORT_ANALOG. The maximum result of the REPORT_ANALOG is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin for which to request the value (limited to pins 0 - 15). + */ +void FirmataMarshaller::reportAnalogEnable(uint8_t pin) +const +{ + reportAnalog(pin, true); +} + +/** + * Halt an 8-bit port stream from the Firmata host application (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number for which to request the value. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + */ +void FirmataMarshaller::reportDigitalPortDisable(uint8_t portNumber) +const +{ + reportDigitalPort(portNumber, false); +} + +/** + * Request an 8-bit port stream from the Firmata host application (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number for which to request the value. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + */ +void FirmataMarshaller::reportDigitalPortEnable(uint8_t portNumber) +const +{ + reportDigitalPort(portNumber, true); +} + +/** + * Send an analog message to the Firmata host application. The range of pins is limited to [0..15] + * when using the ANALOG_MESSAGE. The maximum value of the ANALOG_MESSAGE is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin to which the value is sent. + * @param value The value of the analog pin (0 - 1024 for 10-bit analog, 0 - 4096 for 12-bit, etc). + * @note The maximum value is 14-bits (16384). + */ +void FirmataMarshaller::sendAnalog(uint8_t pin, uint16_t value) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + if ( (0xF >= pin) && (0x3FFF >= value) ) { + FirmataStream->write(ANALOG_MESSAGE|pin); + encodeByteStream(sizeof(value), reinterpret_cast(&value), sizeof(value)); + } else { + sendExtendedAnalog(pin, sizeof(value), reinterpret_cast(&value)); + } +} + +/** + * Send an analog mapping query to the Firmata host application. The resulting sysex message will + * have an ANALOG_MAPPING_RESPONSE command byte, followed by a list of pins [0-n]; where each + * pin will specify its corresponding analog pin number or 0x7F (127) if not applicable. + */ +void FirmataMarshaller::sendAnalogMappingQuery(void) +const +{ + sendSysex(ANALOG_MAPPING_QUERY, 0, NULL); +} + +/** + * Send a capability query to the Firmata host application. The resulting sysex message will have + * a CAPABILITY_RESPONSE command byte, followed by a list of byte tuples (mode and mode resolution) + * for each pin; where each pin list is terminated by 0x7F (127). + */ +void FirmataMarshaller::sendCapabilityQuery(void) +const +{ + sendSysex(CAPABILITY_QUERY, 0, NULL); +} + +/** + * Send a single digital pin value to the Firmata host application. + * @param pin The digital pin to send the value of. + * @param value The value of the pin. + */ +void FirmataMarshaller::sendDigital(uint8_t pin, uint8_t value) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(SET_DIGITAL_PIN_VALUE); + FirmataStream->write(pin & 0x7F); + FirmataStream->write(value != 0); +} + + +/** + * Send an 8-bit port in a single digital message (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number to send. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + * @param portData The value of the port. The value of each pin in the port is represented by a bit. + */ +void FirmataMarshaller::sendDigitalPort(uint8_t portNumber, uint16_t portData) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF)); + // Tx bits 0-6 (protocol v1 and higher) + // Tx bits 7-13 (bit 7 only for protocol v2 and higher) + encodeByteStream(sizeof(portData), reinterpret_cast(&portData), sizeof(portData)); +} + +/** + * Sends the firmware name and version to the Firmata host application. + * @param major The major verison number + * @param minor The minor version number + * @param bytec The length of the firmware name + * @param bytev The firmware name array + */ +void FirmataMarshaller::sendFirmwareVersion(uint8_t major, uint8_t minor, size_t bytec, uint8_t *bytev) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + size_t i; + FirmataStream->write(START_SYSEX); + FirmataStream->write(REPORT_FIRMWARE); + FirmataStream->write(major); + FirmataStream->write(minor); + for (i = 0; i < bytec; ++i) { + encodeByteStream(sizeof(bytev[i]), reinterpret_cast(&bytev[i])); + } + FirmataStream->write(END_SYSEX); +} + +/** + * Send the Firmata protocol version to the Firmata host application. + * @param major The major verison number + * @param minor The minor version number + */ +void FirmataMarshaller::sendVersion(uint8_t major, uint8_t minor) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(REPORT_VERSION); + FirmataStream->write(major); + FirmataStream->write(minor); +} + +/** + * Send the pin mode/configuration. The pin configuration (or mode) in Firmata represents the + * current function of the pin. Examples are digital input or output, analog input, pwm, i2c, + * serial (uart), etc. + * @param pin The pin to configure. + * @param config The configuration value for the specified pin. + */ +void FirmataMarshaller::sendPinMode(uint8_t pin, uint8_t config) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(SET_PIN_MODE); + FirmataStream->write(pin); + FirmataStream->write(config); +} + +/** + * Send a pin state query to the Firmata host application. The resulting sysex message will have + * a PIN_STATE_RESPONSE command byte, followed by the pin number, the pin mode and a stream of + * bits to indicate any *data* written to the pin (pin state). + * @param pin The pin to query + * @note The pin state is any data written to the pin (i.e. pin state != pin value) + */ +void FirmataMarshaller::sendPinStateQuery(uint8_t pin) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(START_SYSEX); + FirmataStream->write(PIN_STATE_QUERY); + FirmataStream->write(pin); + FirmataStream->write(END_SYSEX); +} + +/** + * Send a sysex message where all values after the command byte are packet as 2 7-bit bytes + * (this is not always the case so this function is not always used to send sysex messages). + * @param command The sysex command byte. + * @param bytec The number of data bytes in the message (excludes start, command and end bytes). + * @param bytev A pointer to the array of data bytes to send in the message. + */ +void FirmataMarshaller::sendSysex(uint8_t command, size_t bytec, uint8_t *bytev) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + size_t i; + FirmataStream->write(START_SYSEX); + FirmataStream->write(command); + for (i = 0; i < bytec; ++i) { + encodeByteStream(sizeof(bytev[i]), reinterpret_cast(&bytev[i])); + } + FirmataStream->write(END_SYSEX); +} + +/** + * Send a string to the Firmata host application. + * @param string A pointer to the char string + */ +void FirmataMarshaller::sendString(const char *string) +const +{ + sendSysex(STRING_DATA, strlen(string), reinterpret_cast(const_cast(string))); +} + +/** + * The sampling interval sets how often analog data and i2c data is reported to the client. + * @param interval_ms The interval (in milliseconds) at which to sample + * @note The default sampling interval is 19ms + */ +void FirmataMarshaller::setSamplingInterval(uint16_t interval_ms) +const +{ + sendSysex(SAMPLING_INTERVAL, sizeof(interval_ms), reinterpret_cast(&interval_ms)); +} + +/** + * Perform a software reset on the target. For example, StandardFirmata.ino will initialize + * everything to a known state and reset the parsing buffer. + */ +void FirmataMarshaller::systemReset(void) +const +{ + if ( (Stream *)NULL == FirmataStream ) { return; } + FirmataStream->write(SYSTEM_RESET); +} diff --git a/libraries/Firmata/FirmataMarshaller.h b/libraries/Firmata/FirmataMarshaller.h new file mode 100644 index 0000000..3fa83f6 --- /dev/null +++ b/libraries/Firmata/FirmataMarshaller.h @@ -0,0 +1,75 @@ +/* + FirmataMarshaller.h + Copyright (c) 2006-2008 Hans-Christoph Steiner. 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. +*/ + +#ifndef FirmataMarshaller_h +#define FirmataMarshaller_h + +#if defined(__cplusplus) && !defined(ARDUINO) + #include + #include +#else + #include + #include +#endif + +#include + +namespace firmata { + +class FirmataMarshaller +{ + friend class FirmataClass; + + public: + /* constructors */ + FirmataMarshaller(); + + /* public methods */ + void begin(Stream &s); + void end(); + + /* serial send handling */ + void queryFirmwareVersion(void) const; + void queryVersion(void) const; + void reportAnalogDisable(uint8_t pin) const; + void reportAnalogEnable(uint8_t pin) const; + void reportDigitalPortDisable(uint8_t portNumber) const; + void reportDigitalPortEnable(uint8_t portNumber) const; + void sendAnalog(uint8_t pin, uint16_t value) const; + void sendAnalogMappingQuery(void) const; + void sendCapabilityQuery(void) const; + void sendDigital(uint8_t pin, uint8_t value) const; + void sendDigitalPort(uint8_t portNumber, uint16_t portData) const; + void sendFirmwareVersion(uint8_t major, uint8_t minor, size_t bytec, uint8_t *bytev) const; + void sendVersion(uint8_t major, uint8_t minor) const; + void sendPinMode(uint8_t pin, uint8_t config) const; + void sendPinStateQuery(uint8_t pin) const; + void sendString(const char *string) const; + void sendSysex(uint8_t command, size_t bytec, uint8_t *bytev) const; + void setSamplingInterval(uint16_t interval_ms) const; + void systemReset(void) const; + + private: + /* utility methods */ + void reportAnalog(uint8_t pin, bool stream_enable) const; + void reportDigitalPort(uint8_t portNumber, bool stream_enable) const; + void sendExtendedAnalog(uint8_t pin, size_t bytec, uint8_t * bytev) const; + void encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes = 0) const; + + Stream * FirmataStream; +}; + +} // namespace firmata + +#endif /* FirmataMarshaller_h */ + diff --git a/libraries/Firmata/FirmataParser.cpp b/libraries/Firmata/FirmataParser.cpp new file mode 100644 index 0000000..d402fdf --- /dev/null +++ b/libraries/Firmata/FirmataParser.cpp @@ -0,0 +1,480 @@ +/* + FirmataParser.cpp + Copyright (c) 2006-2008 Hans-Christoph Steiner. 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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "FirmataParser.h" + +#include "FirmataConstants.h" + +using namespace firmata; + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +/** + * The FirmataParser class. + * @param dataBuffer A pointer to an external buffer used to store parsed data + * @param dataBufferSize The size of the external buffer + */ +FirmataParser::FirmataParser(uint8_t * const dataBuffer, size_t dataBufferSize) +: + dataBuffer(dataBuffer), + dataBufferSize(dataBufferSize), + executeMultiByteCommand(0), + multiByteChannel(0), + waitForData(0), + parsingSysex(false), + sysexBytesRead(0), + currentAnalogCallbackContext((void *)NULL), + currentDigitalCallbackContext((void *)NULL), + currentReportAnalogCallbackContext((void *)NULL), + currentReportDigitalCallbackContext((void *)NULL), + currentPinModeCallbackContext((void *)NULL), + currentPinValueCallbackContext((void *)NULL), + currentReportFirmwareCallbackContext((void *)NULL), + currentReportVersionCallbackContext((void *)NULL), + currentDataBufferOverflowCallbackContext((void *)NULL), + currentStringCallbackContext((void *)NULL), + currentSysexCallbackContext((void *)NULL), + currentSystemResetCallbackContext((void *)NULL), + currentAnalogCallback((callbackFunction)NULL), + currentDigitalCallback((callbackFunction)NULL), + currentReportAnalogCallback((callbackFunction)NULL), + currentReportDigitalCallback((callbackFunction)NULL), + currentPinModeCallback((callbackFunction)NULL), + currentPinValueCallback((callbackFunction)NULL), + currentDataBufferOverflowCallback((dataBufferOverflowCallbackFunction)NULL), + currentStringCallback((stringCallbackFunction)NULL), + currentSysexCallback((sysexCallbackFunction)NULL), + currentReportFirmwareCallback((versionCallbackFunction)NULL), + currentReportVersionCallback((systemCallbackFunction)NULL), + currentSystemResetCallback((systemCallbackFunction)NULL) +{ + allowBufferUpdate = ((uint8_t *)NULL == dataBuffer); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +//------------------------------------------------------------------------------ +// Serial Receive Handling + +/** + * Parse data from the input stream. + * @param inputData A single byte to be added to the parser. + */ +void FirmataParser::parse(uint8_t inputData) +{ + uint8_t command; + + if (parsingSysex) { + if (inputData == END_SYSEX) { + //stop sysex byte + parsingSysex = false; + //fire off handler function + processSysexMessage(); + } else { + //normal data byte - add to buffer + bufferDataAtPosition(inputData, sysexBytesRead); + ++sysexBytesRead; + } + } else if ( (waitForData > 0) && (inputData < 128) ) { + --waitForData; + bufferDataAtPosition(inputData, waitForData); + if ( (waitForData == 0) && executeMultiByteCommand ) { // got the whole message + switch (executeMultiByteCommand) { + case ANALOG_MESSAGE: + if (currentAnalogCallback) { + (*currentAnalogCallback)(currentAnalogCallbackContext, + multiByteChannel, + (dataBuffer[0] << 7) + + dataBuffer[1]); + } + break; + case DIGITAL_MESSAGE: + if (currentDigitalCallback) { + (*currentDigitalCallback)(currentDigitalCallbackContext, + multiByteChannel, + (dataBuffer[0] << 7) + + dataBuffer[1]); + } + break; + case SET_PIN_MODE: + if (currentPinModeCallback) + (*currentPinModeCallback)(currentPinModeCallbackContext, dataBuffer[1], dataBuffer[0]); + break; + case SET_DIGITAL_PIN_VALUE: + if (currentPinValueCallback) + (*currentPinValueCallback)(currentPinValueCallbackContext, dataBuffer[1], dataBuffer[0]); + break; + case REPORT_ANALOG: + if (currentReportAnalogCallback) + (*currentReportAnalogCallback)(currentReportAnalogCallbackContext, multiByteChannel, dataBuffer[0]); + break; + case REPORT_DIGITAL: + if (currentReportDigitalCallback) + (*currentReportDigitalCallback)(currentReportDigitalCallbackContext, multiByteChannel, dataBuffer[0]); + break; + } + executeMultiByteCommand = 0; + } + } else { + // remove channel info from command byte if less than 0xF0 + if (inputData < 0xF0) { + command = inputData & 0xF0; + multiByteChannel = inputData & 0x0F; + } else { + command = inputData; + // commands in the 0xF* range don't use channel data + } + switch (command) { + case ANALOG_MESSAGE: + case DIGITAL_MESSAGE: + case SET_PIN_MODE: + case SET_DIGITAL_PIN_VALUE: + waitForData = 2; // two data bytes needed + executeMultiByteCommand = command; + break; + case REPORT_ANALOG: + case REPORT_DIGITAL: + waitForData = 1; // one data byte needed + executeMultiByteCommand = command; + break; + case START_SYSEX: + parsingSysex = true; + sysexBytesRead = 0; + break; + case SYSTEM_RESET: + systemReset(); + break; + case REPORT_VERSION: + if (currentReportVersionCallback) + (*currentReportVersionCallback)(currentReportVersionCallbackContext); + break; + } + } +} + +/** + * @return Returns true if the parser is actively parsing data. + */ +bool FirmataParser::isParsingMessage(void) +const +{ + return (waitForData > 0 || parsingSysex); +} + +/** + * Provides a mechanism to either set or update the working buffer of the parser. + * The method will be enabled when no buffer has been provided, or an overflow + * condition exists. + * @param dataBuffer A pointer to an external buffer used to store parsed data + * @param dataBufferSize The size of the external buffer + */ +int FirmataParser::setDataBufferOfSize(uint8_t * dataBuffer, size_t dataBufferSize) +{ + int result; + + if ( !allowBufferUpdate ) { + result = __LINE__; + } else if ((uint8_t *)NULL == dataBuffer) { + result = __LINE__; + } else { + this->dataBuffer = dataBuffer; + this->dataBufferSize = dataBufferSize; + allowBufferUpdate = false; + result = 0; + } + + return result; +} + +/** + * Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE, + * DIGITAL_MESSAGE, REPORT_ANALOG, REPORT DIGITAL, SET_PIN_MODE and SET_DIGITAL_PIN_VALUE). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(uint8_t command, callbackFunction newFunction, void * context) +{ + switch (command) { + case ANALOG_MESSAGE: + currentAnalogCallback = newFunction; + currentAnalogCallbackContext = context; + break; + case DIGITAL_MESSAGE: + currentDigitalCallback = newFunction; + currentDigitalCallbackContext = context; + break; + case REPORT_ANALOG: + currentReportAnalogCallback = newFunction; + currentReportAnalogCallbackContext = context; + break; + case REPORT_DIGITAL: + currentReportDigitalCallback = newFunction; + currentReportDigitalCallbackContext = context; + break; + case SET_PIN_MODE: + currentPinModeCallback = newFunction; + currentPinModeCallbackContext = context; + break; + case SET_DIGITAL_PIN_VALUE: + currentPinValueCallback = newFunction; + currentPinValueCallbackContext = context; + break; + } +} + +/** + * Attach a version callback function (supported option: REPORT_FIRMWARE). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(uint8_t command, versionCallbackFunction newFunction, void * context) +{ + switch (command) { + case REPORT_FIRMWARE: + currentReportFirmwareCallback = newFunction; + currentReportFirmwareCallbackContext = context; + break; + } +} + +/** + * Attach a system callback function (supported options are: SYSTEM_RESET, REPORT_VERSION). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(uint8_t command, systemCallbackFunction newFunction, void * context) +{ + switch (command) { + case REPORT_VERSION: + currentReportVersionCallback = newFunction; + currentReportVersionCallbackContext = context; + break; + case SYSTEM_RESET: + currentSystemResetCallback = newFunction; + currentSystemResetCallbackContext = context; + break; + } +} + +/** + * Attach a callback function for the STRING_DATA command. + * @param command Must be set to STRING_DATA or it will be ignored. + * @param newFunction A reference to the string callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(uint8_t command, stringCallbackFunction newFunction, void * context) +{ + switch (command) { + case STRING_DATA: + currentStringCallback = newFunction; + currentStringCallbackContext = context; + break; + } +} + +/** + * Attach a generic sysex callback function to sysex command. + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the sysex callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(uint8_t command, sysexCallbackFunction newFunction, void * context) +{ + (void)command; + currentSysexCallback = newFunction; + currentSysexCallbackContext = context; +} + +/** + * Attach a buffer overflow callback + * @param newFunction A reference to the buffer overflow callback function to attach. + * @param context An optional context to be provided to the callback function (NULL by default). + * @note The context parameter is provided so you can pass a parameter, by reference, to + * your callback function. + */ +void FirmataParser::attach(dataBufferOverflowCallbackFunction newFunction, void * context) +{ + currentDataBufferOverflowCallback = newFunction; + currentDataBufferOverflowCallbackContext = context; +} + +/** + * Detach a callback function for a specified command (such as SYSTEM_RESET, STRING_DATA, + * ANALOG_MESSAGE, DIGITAL_MESSAGE, etc). + * @param command The ID of the command to detatch the callback function from. + */ +void FirmataParser::detach(uint8_t command) +{ + switch (command) { + case REPORT_FIRMWARE: + attach(command, (versionCallbackFunction)NULL, NULL); + break; + case REPORT_VERSION: + case SYSTEM_RESET: + attach(command, (systemCallbackFunction)NULL, NULL); + break; + case STRING_DATA: + attach(command, (stringCallbackFunction)NULL, NULL); + break; + case START_SYSEX: + attach(command, (sysexCallbackFunction)NULL, NULL); + break; + default: + attach(command, (callbackFunction)NULL, NULL); + break; + } +} + +/** + * Detach the buffer overflow callback + * @param Any pointer of type dataBufferOverflowCallbackFunction. + */ +void FirmataParser::detach(dataBufferOverflowCallbackFunction) +{ + currentDataBufferOverflowCallback = (dataBufferOverflowCallbackFunction)NULL; + currentDataBufferOverflowCallbackContext = (void *)NULL; +} + +//****************************************************************************** +//* Private Methods +//****************************************************************************** + +/** + * Buffer abstraction to prevent memory corruption + * @param data The byte to put into the buffer + * @param pos The position to insert the byte into the buffer + * @return writeError A boolean to indicate if an error occured + * @private + */ +bool FirmataParser::bufferDataAtPosition(const uint8_t data, const size_t pos) +{ + bool bufferOverflow = (pos >= dataBufferSize); + + // Notify of overflow condition + if ( bufferOverflow + && ((dataBufferOverflowCallbackFunction)NULL != currentDataBufferOverflowCallback) ) + { + allowBufferUpdate = true; + currentDataBufferOverflowCallback(currentDataBufferOverflowCallbackContext); + // Check if overflow was resolved during callback + bufferOverflow = (pos >= dataBufferSize); + } + + // Write data to buffer if no overflow condition persist + if ( !bufferOverflow ) + { + dataBuffer[pos] = data; + } + + return bufferOverflow; +} + +/** + * Transform 7-bit firmata message into 8-bit stream + * @param bytec The encoded data byte length of the message (max: 16383). + * @param bytev A pointer to the encoded array of data bytes. + * @return The length of the decoded data. + * @note The conversion will be done in place on the provided buffer. + * @private + */ +size_t FirmataParser::decodeByteStream(size_t bytec, uint8_t * bytev) { + size_t decoded_bytes, i; + + for ( i = 0, decoded_bytes = 0 ; i < bytec ; ++decoded_bytes, ++i ) { + bytev[decoded_bytes] = bytev[i]; + bytev[decoded_bytes] |= (uint8_t)(bytev[++i] << 7); + } + + return decoded_bytes; +} + +/** + * Process incoming sysex messages. Handles REPORT_FIRMWARE and STRING_DATA internally. + * Calls callback function for STRING_DATA and all other sysex messages. + * @private + */ +void FirmataParser::processSysexMessage(void) +{ + switch (dataBuffer[0]) { //first byte in buffer is command + case REPORT_FIRMWARE: + if (currentReportFirmwareCallback) { + const size_t major_version_offset = 1; + const size_t minor_version_offset = 2; + const size_t string_offset = 3; + // Test for malformed REPORT_FIRMWARE message (used to query firmware prior to Firmata v3.0.0) + if ( 3 > sysexBytesRead ) { + (*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext, 0, 0, (const char *)NULL); + } else { + const size_t end_of_string = (string_offset + decodeByteStream((sysexBytesRead - string_offset), &dataBuffer[string_offset])); + bufferDataAtPosition('\0', end_of_string); // NULL terminate the string + (*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext, (size_t)dataBuffer[major_version_offset], (size_t)dataBuffer[minor_version_offset], (const char *)&dataBuffer[string_offset]); + } + } + break; + case STRING_DATA: + if (currentStringCallback) { + const size_t string_offset = 1; + const size_t end_of_string = (string_offset + decodeByteStream((sysexBytesRead - string_offset), &dataBuffer[string_offset])); + bufferDataAtPosition('\0', end_of_string); // NULL terminate the string + (*currentStringCallback)(currentStringCallbackContext, (const char *)&dataBuffer[string_offset]); + } + break; + default: + if (currentSysexCallback) + (*currentSysexCallback)(currentSysexCallbackContext, dataBuffer[0], sysexBytesRead - 1, dataBuffer + 1); + } +} + +/** + * Resets the system state upon a SYSTEM_RESET message from the host software. + * @private + */ +void FirmataParser::systemReset(void) +{ + size_t i; + + waitForData = 0; // this flag says the next serial input will be data + executeMultiByteCommand = 0; // execute this after getting multi-byte data + multiByteChannel = 0; // channel data for multiByteCommands + + for (i = 0; i < dataBufferSize; ++i) { + dataBuffer[i] = 0; + } + + parsingSysex = false; + sysexBytesRead = 0; + + if (currentSystemResetCallback) + (*currentSystemResetCallback)(currentSystemResetCallbackContext); +} diff --git a/libraries/Firmata/FirmataParser.h b/libraries/Firmata/FirmataParser.h new file mode 100644 index 0000000..bb0c8be --- /dev/null +++ b/libraries/Firmata/FirmataParser.h @@ -0,0 +1,105 @@ +/* + FirmataParser.h + Copyright (c) 2006-2008 Hans-Christoph Steiner. 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. +*/ + +#ifndef FirmataParser_h +#define FirmataParser_h + +#if defined(__cplusplus) && !defined(ARDUINO) + #include + #include +#else + #include + #include +#endif + +namespace firmata { + +class FirmataParser +{ + public: + /* callback function types */ + typedef void (*callbackFunction)(void * context, uint8_t command, uint16_t value); + typedef void (*dataBufferOverflowCallbackFunction)(void * context); + typedef void (*stringCallbackFunction)(void * context, const char * c_str); + typedef void (*sysexCallbackFunction)(void * context, uint8_t command, size_t argc, uint8_t * argv); + typedef void (*systemCallbackFunction)(void * context); + typedef void (*versionCallbackFunction)(void * context, size_t sv_major, size_t sv_minor, const char * firmware); + + FirmataParser(uint8_t * dataBuffer = (uint8_t *)NULL, size_t dataBufferSize = 0); + + /* serial receive handling */ + void parse(uint8_t value); + bool isParsingMessage(void) const; + int setDataBufferOfSize(uint8_t * dataBuffer, size_t dataBufferSize); + + /* attach & detach callback functions to messages */ + void attach(uint8_t command, callbackFunction newFunction, void * context = NULL); + void attach(dataBufferOverflowCallbackFunction newFunction, void * context = NULL); + void attach(uint8_t command, stringCallbackFunction newFunction, void * context = NULL); + void attach(uint8_t command, sysexCallbackFunction newFunction, void * context = NULL); + void attach(uint8_t command, systemCallbackFunction newFunction, void * context = NULL); + void attach(uint8_t command, versionCallbackFunction newFunction, void * context = NULL); + void detach(uint8_t command); + void detach(dataBufferOverflowCallbackFunction); + + private: + /* input message handling */ + bool allowBufferUpdate; + uint8_t * dataBuffer; // multi-byte data + size_t dataBufferSize; + uint8_t executeMultiByteCommand; // execute this after getting multi-byte data + uint8_t multiByteChannel; // channel data for multiByteCommands + size_t waitForData; // this flag says the next serial input will be data + + /* sysex */ + bool parsingSysex; + size_t sysexBytesRead; + + /* callback context */ + void * currentAnalogCallbackContext; + void * currentDigitalCallbackContext; + void * currentReportAnalogCallbackContext; + void * currentReportDigitalCallbackContext; + void * currentPinModeCallbackContext; + void * currentPinValueCallbackContext; + void * currentReportFirmwareCallbackContext; + void * currentReportVersionCallbackContext; + void * currentDataBufferOverflowCallbackContext; + void * currentStringCallbackContext; + void * currentSysexCallbackContext; + void * currentSystemResetCallbackContext; + + /* callback functions */ + callbackFunction currentAnalogCallback; + callbackFunction currentDigitalCallback; + callbackFunction currentReportAnalogCallback; + callbackFunction currentReportDigitalCallback; + callbackFunction currentPinModeCallback; + callbackFunction currentPinValueCallback; + dataBufferOverflowCallbackFunction currentDataBufferOverflowCallback; + stringCallbackFunction currentStringCallback; + sysexCallbackFunction currentSysexCallback; + versionCallbackFunction currentReportFirmwareCallback; + systemCallbackFunction currentReportVersionCallback; + systemCallbackFunction currentSystemResetCallback; + + /* private methods ------------------------------ */ + bool bufferDataAtPosition(const uint8_t data, const size_t pos); + size_t decodeByteStream(size_t bytec, uint8_t * bytev); + void processSysexMessage(void); + void systemReset(void); +}; + +} // firmata + +#endif /* FirmataParser_h */ diff --git a/libraries/Firmata/LICENSE.txt b/libraries/Firmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/AllInputsFirmata/AllInputsFirmata.ino b/libraries/Firmata/examples/AllInputsFirmata/AllInputsFirmata.ino new file mode 100644 index 0000000..fb9b0fd --- /dev/null +++ b/libraries/Firmata/examples/AllInputsFirmata/AllInputsFirmata.ino @@ -0,0 +1,90 @@ +/* + * 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 + */ + +/* + * This firmware reads all inputs and sends them as fast as it can. It was + * inspired by the ease-of-use of the Arduino2Max program. + * + * This example code is in the public domain. + */ +#include + +byte pin; + +int analogValue; +int previousAnalogValues[TOTAL_ANALOG_PINS]; + +byte portStatus[TOTAL_PORTS]; // each bit: 1=pin is digital input, 0=other/ignore +byte previousPINs[TOTAL_PORTS]; + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +/* make sure that the FTDI buffer doesn't go over 60 bytes, otherwise you + get long, random delays. So only read analogs every 20ms or so */ +int samplingInterval = 19; // how often to run the main loop (in ms) + +void sendPort(byte portNumber, byte portValue) +{ + portValue = portValue & portStatus[portNumber]; + if (previousPINs[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +void setup() +{ + byte i, port, status; + + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + for (pin = 0; pin < TOTAL_PINS; pin++) { + if IS_PIN_DIGITAL(pin) pinMode(PIN_TO_DIGITAL(pin), INPUT); + } + + for (port = 0; port < TOTAL_PORTS; port++) { + status = 0; + for (i = 0; i < 8; i++) { + if (IS_PIN_DIGITAL(port * 8 + i)) status |= (1 << i); + } + portStatus[port] = status; + } + + Firmata.begin(57600); +} + +void loop() +{ + byte i; + + for (i = 0; i < TOTAL_PORTS; i++) { + sendPort(i, readPort(i, 0xff)); + } + /* make sure that the FTDI buffer doesn't go over 60 bytes, otherwise you + get long, random delays. So only read analogs every 20ms or so */ + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + while (Firmata.available()) { + Firmata.processInput(); + } + for (pin = 0; pin < TOTAL_ANALOG_PINS; pin++) { + analogValue = analogRead(pin); + if (analogValue != previousAnalogValues[pin]) { + Firmata.sendAnalog(pin, analogValue); + previousAnalogValues[pin] = analogValue; + } + } + } +} + + diff --git a/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.ino b/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.ino new file mode 100644 index 0000000..689c685 --- /dev/null +++ b/libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.ino @@ -0,0 +1,94 @@ +/* + * 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 + */ + +/* This firmware supports as many analog ports as possible, all analog inputs, + * four PWM outputs, and two with servo support. + * + * This example code is in the public domain. + */ +#include +#include + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* servos */ +Servo servo9, servo10; // one instance per pin +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int analogPin = 0; // counter for reading analog pins +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis + + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void analogWriteCallback(byte pin, int value) +{ + switch (pin) { + case 9: servo9.write(value); break; + case 10: servo10.write(value); break; + case 3: + case 5: + case 6: + case 11: // PWM pins + analogWrite(pin, value); + break; + } +} +// ----------------------------------------------------------------------------- +// sets bits in a bit array (int) to toggle the reporting of the analogIns +void reportAnalogCallback(byte pin, int value) +{ + if (value == 0) { + analogInputsToReport = analogInputsToReport & ~ (1 << pin); + } + else { // everything but 0 enables reporting of that pin + analogInputsToReport = analogInputsToReport | (1 << pin); + } + // TODO: save status to EEPROM here, if changed +} + +/*============================================================================== + * SETUP() + *============================================================================*/ +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + + servo9.attach(9); + servo10.attach(10); + Firmata.begin(57600); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + while (Firmata.available()) + Firmata.processInput(); + currentMillis = millis(); + if (currentMillis - previousMillis > 20) { + previousMillis += 20; // run this every 20ms + for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { + if ( analogInputsToReport & (1 << analogPin) ) + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } +} + diff --git a/libraries/Firmata/examples/EchoString/EchoString.ino b/libraries/Firmata/examples/EchoString/EchoString.ino new file mode 100644 index 0000000..e1651d5 --- /dev/null +++ b/libraries/Firmata/examples/EchoString/EchoString.ino @@ -0,0 +1,44 @@ +/* + * 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 + */ + +/* This sketch accepts strings and raw sysex messages and echos them back. + * + * This example code is in the public domain. + */ +#include + +void stringCallback(char *myString) +{ + Firmata.sendString(myString); +} + + +void sysexCallback(byte command, byte argc, byte *argv) +{ + Firmata.sendSysex(command, argc, argv); +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(STRING_DATA, stringCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.begin(57600); +} + +void loop() +{ + while (Firmata.available()) { + Firmata.processInput(); + } +} + + diff --git a/libraries/Firmata/examples/OldStandardFirmata/LICENSE.txt b/libraries/Firmata/examples/OldStandardFirmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/OldStandardFirmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/OldStandardFirmata/OldStandardFirmata.ino b/libraries/Firmata/examples/OldStandardFirmata/OldStandardFirmata.ino new file mode 100644 index 0000000..5b79de5 --- /dev/null +++ b/libraries/Firmata/examples/OldStandardFirmata/OldStandardFirmata.ino @@ -0,0 +1,239 @@ +/* + * 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. + + 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. + */ + +/* + * This is an old version of StandardFirmata (v2.0). It is kept here because + * its the last version that works on an ATMEGA8 chip. Also, it can be used + * for host software that has not been updated to a newer version of the + * protocol. It also uses the old baud rate of 115200 rather than 57600. + */ + +#include +#include + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting +int analogPin = 0; // counter for reading analog pins + +/* digital pins */ +byte reportPINs[TOTAL_PORTS]; // PIN == input port +byte previousPINs[TOTAL_PORTS]; // PIN == input port +byte pinStatus[TOTAL_PINS]; // store pin status, default OUTPUT +byte portStatus[TOTAL_PORTS]; + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis + + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void outputPort(byte portNumber, byte portValue) +{ + portValue = portValue & ~ portStatus[portNumber]; + if (previousPINs[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + Firmata.sendDigitalPort(portNumber, portValue); + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + byte i, tmp; + for (i = 0; i < TOTAL_PORTS; i++) { + if (reportPINs[i]) { + switch (i) { + case 0: outputPort(0, PIND & ~ B00000011); break; // ignore Rx/Tx 0/1 + case 1: outputPort(1, PINB); break; + case 2: outputPort(2, PINC); break; + } + } + } +} + +// ----------------------------------------------------------------------------- +/* 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) { + byte port = 0; + byte offset = 0; + + if (pin < 8) { + port = 0; + offset = 0; + } else if (pin < 14) { + port = 1; + offset = 8; + } else if (pin < 22) { + port = 2; + offset = 14; + } + + if (pin > 1) { // ignore RxTx (pins 0 and 1) + pinStatus[pin] = mode; + switch (mode) { + case INPUT: + pinMode(pin, INPUT); + portStatus[port] = portStatus[port] & ~ (1 << (pin - offset)); + break; + case OUTPUT: + digitalWrite(pin, LOW); // disable PWM + case PWM: + pinMode(pin, OUTPUT); + portStatus[port] = portStatus[port] | (1 << (pin - offset)); + break; + //case ANALOG: // TODO figure this out + default: + Firmata.sendString(""); + } + // TODO: save status to EEPROM here, if changed + } +} + +void analogWriteCallback(byte pin, int value) +{ + setPinModeCallback(pin, PIN_MODE_PWM); + analogWrite(pin, value); +} + +void digitalWriteCallback(byte port, int value) +{ + switch (port) { + case 0: // pins 2-7 (don't change Rx/Tx, pins 0 and 1) + // 0xFF03 == B1111111100000011 0x03 == B00000011 + PORTD = (value & ~ 0xFF03) | (PORTD & 0x03); + break; + case 1: // pins 8-13 (14,15 are disabled for the crystal) + PORTB = (byte)value; + break; + case 2: // analog pins used as digital + PORTC = (byte)value; + break; + } +} + +// ----------------------------------------------------------------------------- +/* sets bits in a bit array (int) to toggle the reporting of the analogIns + */ +//void FirmataClass::setAnalogPinReporting(byte pin, byte state) { +//} +void reportAnalogCallback(byte pin, int value) +{ + if (value == 0) { + analogInputsToReport = analogInputsToReport & ~ (1 << pin); + } + else { // everything but 0 enables reporting of that pin + analogInputsToReport = analogInputsToReport | (1 << pin); + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + reportPINs[port] = (byte)value; + if (port == 2) // turn off analog reporting when used as digital + analogInputsToReport = 0; +} + +/*============================================================================== + * SETUP() + *============================================================================*/ +void setup() +{ + byte i; + + Firmata.setFirmwareVersion(2, 0); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + + portStatus[0] = B00000011; // ignore Tx/RX pins + portStatus[1] = B11000000; // ignore 14/15 pins + portStatus[2] = B00000000; + + // for(i=0; i 20) { + previousMillis += 20; // run this every 20ms + /* SERIALREAD - Serial.read() uses a 128 byte circular buffer, so handle + * all serialReads at once, i.e. empty the buffer */ + while (Firmata.available()) + Firmata.processInput(); + /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + + /* ANALOGREAD - right after the event character, do all of the + * analogReads(). These only need to be done every 4ms. */ + for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { + if ( analogInputsToReport & (1 << analogPin) ) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } +} diff --git a/libraries/Firmata/examples/ServoFirmata/ServoFirmata.ino b/libraries/Firmata/examples/ServoFirmata/ServoFirmata.ino new file mode 100644 index 0000000..52b1f1b --- /dev/null +++ b/libraries/Firmata/examples/ServoFirmata/ServoFirmata.ino @@ -0,0 +1,65 @@ +/* + * 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 + */ + +/* This firmware supports as many servos as possible using the Servo library + * included in Arduino 0017 + * + * This example code is in the public domain. + */ + +#include +#include + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte servoCount = 0; + +void analogWriteCallback(byte pin, int value) +{ + if (IS_PIN_DIGITAL(pin)) { + servos[servoPinMap[pin]].write(value); + } +} + +void systemResetCallback() +{ + servoCount = 0; +} + +void setup() +{ + byte pin; + + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + Firmata.begin(57600); + systemResetCallback(); + + // attach servos from first digital pin up to max number of + // servos supported for the board + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_DIGITAL(pin)) { + if (servoCount < MAX_SERVOS) { + servoPinMap[pin] = servoCount; + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + servoCount++; + } + } + } +} + +void loop() +{ + while (Firmata.available()) + Firmata.processInput(); +} diff --git a/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.ino b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.ino new file mode 100644 index 0000000..98a6adf --- /dev/null +++ b/libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.ino @@ -0,0 +1,46 @@ +/* + * 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 + */ + +/* Supports as many analog inputs and analog PWM outputs as possible. + * + * This example code is in the public domain. + */ +#include + +byte analogPin = 0; + +void analogWriteCallback(byte pin, int value) +{ + if (IS_PIN_PWM(pin)) { + pinMode(PIN_TO_DIGITAL(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), value); + } +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.begin(57600); +} + +void loop() +{ + while (Firmata.available()) { + Firmata.processInput(); + } + // do one analogRead per loop, so if PC is sending a lot of + // analog write messages, we will only delay 1 analogRead + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + analogPin = analogPin + 1; + if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0; +} + diff --git a/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.ino b/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.ino new file mode 100644 index 0000000..91db333 --- /dev/null +++ b/libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.ino @@ -0,0 +1,72 @@ +/* + * 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 + */ + +/* Supports as many digital inputs and outputs as possible. + * + * This example code is in the public domain. + */ +#include + +byte previousPIN[TOTAL_PORTS]; // PIN means PORT for input +byte previousPORT[TOTAL_PORTS]; + +void outputPort(byte portNumber, byte portValue) +{ + // only send the data when it changes, otherwise you get too many messages! + if (previousPIN[portNumber] != portValue) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPIN[portNumber] = portValue; + } +} + +void setPinModeCallback(byte pin, int mode) { + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), mode); + } +} + +void digitalWriteCallback(byte port, int value) +{ + byte i; + byte currentPinValue, previousPinValue; + + if (port < TOTAL_PORTS && value != previousPORT[port]) { + for (i = 0; i < 8; i++) { + currentPinValue = (byte) value & (1 << i); + previousPinValue = previousPORT[port] & (1 << i); + if (currentPinValue != previousPinValue) { + digitalWrite(i + (port * 8), currentPinValue); + } + } + previousPORT[port] = value; + } +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.begin(57600); +} + +void loop() +{ + byte i; + + for (i = 0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, 0xff)); + } + + while (Firmata.available()) { + Firmata.processInput(); + } +} diff --git a/libraries/Firmata/examples/StandardFirmata/LICENSE.txt b/libraries/Firmata/examples/StandardFirmata/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmata/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino b/libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino new file mode 100644 index 0000000..b043c11 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino @@ -0,0 +1,823 @@ +/* + 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. + + 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 August 17th, 2017 +*/ + +#include +#include +#include + +#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 + + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to run the main loop (in ms) + +/* 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; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + 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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega, + // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this: + // Serial1.begin(57600); + // Firmata.begin(Serial1); + // However do not do this if you are using SERIAL_MESSAGE + + Firmata.begin(57600); + while (!Serial) { + ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101 + } + + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * FTDI buffer using Serial.print() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif +} diff --git a/libraries/Firmata/examples/StandardFirmataBLE/LICENSE.txt b/libraries/Firmata/examples/StandardFirmataBLE/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataBLE/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmataBLE/StandardFirmataBLE.ino b/libraries/Firmata/examples/StandardFirmataBLE/StandardFirmataBLE.ino new file mode 100644 index 0000000..7b17b89 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataBLE/StandardFirmataBLE.ino @@ -0,0 +1,835 @@ +/* + 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. + + 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 April 15th, 2018 +*/ + +#include +#include +#include + +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +/* + * Uncomment the following include to enable interfacing + * with Serial devices via hardware or software serial. + */ +// In order to use software serial, you will need to compile this sketch with +// Arduino IDE v1.6.6 or higher. Hardware serial should work back to Arduino 1.0. +//#include "utility/SerialFirmata.h" + +// follow the instructions in bleConfig.h to configure your BLE hardware +#include "bleConfig.h" + +#define I2C_WRITE 0x00 //B00000000 +#define I2C_READ 0x08 //B00001000 +#define I2C_READ_CONTINUOUSLY 0x10 //B00010000 +#define I2C_STOP_READING 0x18 //B00011000 +#define I2C_READ_WRITE_MODE_MASK 0x18 //B00011000 +#define I2C_10BIT_ADDRESS_MODE_MASK 0x20 //B00100000 +#define I2C_END_TX_MASK 0x40 //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 + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to run the main loop (in ms) + +/* 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; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + 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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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(8); // 8 = 8-bit resolution + } + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + isResetting = false; +} + +void setup() +{ + DEBUG_BEGIN(9600); + + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + stream.setLocalName(FIRMATA_BLE_LOCAL_NAME); + + // set the BLE connection interval - this is the fastest interval you can read inputs + stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL); + // set how often the BLE TX buffer is flushed (if not full) + stream.setFlushInterval(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL); + +#ifdef IS_IGNORE_BLE_PINS + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_IGNORE_BLE_PINS(i)) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } +#endif + + stream.begin(); + Firmata.begin(stream); + + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + // do not process data if no BLE connection is established + // poll will send the TX buffer at the specified flush interval or when the buffer is full + if (!stream.poll()) return; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * Stream buffer using Stream.write() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis = currentMillis; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif +} diff --git a/libraries/Firmata/examples/StandardFirmataBLE/bleConfig.h b/libraries/Firmata/examples/StandardFirmataBLE/bleConfig.h new file mode 100644 index 0000000..412bb84 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataBLE/bleConfig.h @@ -0,0 +1,168 @@ +/*================================================================================================== + * BLE CONFIGURATION + * + * If you are using an Arduino 101, you do not need to make any changes to this file (unless you + * need a unique ble local name (see below). If you are using another supported BLE board or shield, + * follow the instructions for the specific board or shield below. + * + * Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino + * Boards Manager. + * + * Supported boards and shields: + * - Arduino 101 (recommended) + * - RedBearLab BLE Shield (v2) ** to be verified ** + * - RedBearLab BLE Nano ** works with modifications ** + * - Adafruit Feather M0 Bluefruit LE + * + *================================================================================================*/ + +// change this to a unique name per board if running StandardFirmataBLE on multiple boards +// within the same physical space +#define FIRMATA_BLE_LOCAL_NAME "FIRMATA" + +/* + * Arduino 101 + * + * Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino + * Boards Manager. + * + * Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27 + */ +#ifdef _VARIANT_ARDUINO_101_X_ +// After conversion to units of 1.25ms, both values must be between +// 0x0006 (7.5ms) and 0x0c80 (4s) +#define FIRMATA_BLE_MIN_INTERVAL 8 // ( 8 * 1000) / 1250 == 0x06 -> 7.5ms +#define FIRMATA_BLE_MAX_INTERVAL 30 // (30 * 1000) / 1250 == 0x18 -> 30ms +#endif + + +/* + * RedBearLab BLE Shield + * + * If you are using a RedBearLab BLE shield, uncomment the define below. + * Also, change the define for BLE_RST if you have the jumper set to pin 7 rather than pin 4. + * + * You will need to use the shield with an Arduino Zero, Due, Mega, or other board with sufficient + * Flash and RAM. Arduino Uno, Leonardo and other ATmega328p and Atmega32u4 boards to not have + * enough memory to run StandardFirmataBLE. + * + * TODO: verify if this works and with which boards it works. + * + * Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27 + */ +//#define REDBEAR_BLE_SHIELD + +#ifdef REDBEAR_BLE_SHIELD +#define BLE_REQ 9 +#define BLE_RDY 8 +#define BLE_RST 4 // 4 or 7 via jumper on shield +#endif + + +/* + * Adafruit Feather M0 Bluefruit LE + * + * If you are using an Adafruit Feather M0 Bluefruit LE, uncomment the define below. + * This configuration should also work with other Bluefruit LE boards/modules that communicate + * with the nRF51822 via SPI (e.g. Bluefruit LE SPI Friend, Bluefruit LE Shield), although + * you may need to change the values of BLE_SPI_CS, BLE_SPI_IRQ, and/or BLE_SPI_RST below. + * + * You will need to install a lightly-modified version of the Adafruit BluefruitLE nRF51 + * package, available at: + * https://github.com/cstawarz/Adafruit_BluefruitLE_nRF51/archive/firmata_fixes.zip + */ +//#define BLUEFRUIT_LE_SPI + +#ifdef BLUEFRUIT_LE_SPI +// Both values must be between 10ms and 4s +#define FIRMATA_BLE_MIN_INTERVAL 10 // 10ms +#define FIRMATA_BLE_MAX_INTERVAL 20 // 20ms + +#define BLE_SPI_CS 8 +#define BLE_SPI_IRQ 7 +#define BLE_SPI_RST 4 +#endif + + +/* + * Generic settings + */ +#if !defined(FIRMATA_BLE_MIN_INTERVAL) && !defined(FIRMATA_BLE_MAX_INTERVAL) +// These values apply to all devices using the Arduino BLEPeripheral library +// with a Nordic nRF8001 or nRF51822. Both values must be between +// 0x0006 (7.5ms) and 0x0c80 (4s). +#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25) +#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25) +#endif + +#if !defined(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL) +#define FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL 30 // 30ms +#endif + + +/*================================================================================================== + * END BLE CONFIGURATION - you should not need to change anything below this line + *================================================================================================*/ + +#ifdef _VARIANT_ARDUINO_101_X_ +#include "utility/BLEStream.h" +BLEStream stream; +#endif + + +#ifdef REDBEAR_BLE_SHIELD +#include +#include "utility/BLEStream.h" +BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST); +#endif + + +#ifdef BLUEFRUIT_LE_SPI +#include "utility/BluefruitLE_SPI_Stream.h" +BluefruitLE_SPI_Stream stream(BLE_SPI_CS, BLE_SPI_IRQ, BLE_SPI_RST); +#endif + + +/* + * RedBearLab BLE Nano (with default switch settings) + * + * Blocked on this issue: https://github.com/RedBearLab/nRF51822-Arduino/issues/46 + * Works with modifications. See comments at top of the test script referenced below. + * When the RBL nRF51822-Arduino library issue is resolved, this should work witout + * any modifications. + * + * Test script: https://gist.github.com/soundanalogous/d39bb3eb36333a0906df + * + * Note: If you have changed the solder jumpers on the Nano you may encounter issues since + * the pins are currently mapped in Firmata only for the default (factory) jumper settings. + */ +// #ifdef BLE_NANO +// #include "utility/BLEStream.h" +// BLEStream stream; +// #endif + + +/* + * RedBearLab Blend and Blend Micro + * + * StandardFirmataBLE requires too much Flash and RAM to run on the ATmega32u4-based Blend + * and Blend Micro boards. It may work with ConfigurableFirmata selecting only analog and/or + * digital I/O. + */ +// #if defined(BLEND_MICRO) || defined(BLEND) +// #include +// #include "utility/BLEStream.h" + +// #define BLE_REQ 6 +// #define BLE_RDY 7 +// #define BLE_RST 4 + +// BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST); +// #endif + + +#if defined(BLE_REQ) && defined(BLE_RDY) && defined(BLE_RST) +#define IS_IGNORE_BLE_PINS(p) ((p) == BLE_REQ || (p) == BLE_RDY || (p) == BLE_RST) +#elif defined(BLE_SPI_CS) && defined(BLE_SPI_IRQ) && defined(BLE_SPI_RST) +#define IS_IGNORE_BLE_PINS(p) ((p) == BLE_SPI_CS || (p) == BLE_SPI_IRQ || (p) == BLE_SPI_RST) +#endif diff --git a/libraries/Firmata/examples/StandardFirmataChipKIT/LICENSE.txt b/libraries/Firmata/examples/StandardFirmataChipKIT/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataChipKIT/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino b/libraries/Firmata/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino new file mode 100644 index 0000000..e13d166 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino @@ -0,0 +1,802 @@ +/* + 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) 2015 Brian Schmalz. 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 August 17th, 2017 +*/ + +#include // Gives us PWM and Servo on every pin +#include +#include + +#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 + + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to run the main loop (in ms) + +/* 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; + +SoftServo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* Sets a pin that is in Servo mode to a particular output value + * (i.e. pulse width). Different boards may have different ways of + * setting servo values, so putting it in a function keeps things cleaner. + */ +void servoWrite(byte pin, int value) +{ + SoftPWMServoPWMWrite(PIN_TO_PWM(pin), value); +} + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + servoWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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; + default: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + case PIN_MODE_PWM: + if (IS_PIN_PWM(pin)) + servoWrite(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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + if (IS_PIN_I2C(pin)) { + Firmata.write(PIN_MODE_I2C); + Firmata.write(1); // TODO: could assign a number to map to SCL or SDA + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + /* For chipKIT Pi board, we need to use Serial1. All others just use Serial. */ +#if defined(_BOARD_CHIPKIT_PI_) + Serial1.begin(57600); + Firmata.begin(Serial1); +#else + Firmata.begin(57600); +#endif + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * FTDI buffer using Serial.print() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } +} diff --git a/libraries/Firmata/examples/StandardFirmataEthernet/LICENSE.txt b/libraries/Firmata/examples/StandardFirmataEthernet/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataEthernet/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino b/libraries/Firmata/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino new file mode 100644 index 0000000..f512cce --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino @@ -0,0 +1,969 @@ +/* + 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-2017 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 August 17th, 2017 +*/ + +/* + README + + StandardFirmataEthernet is a TCP client/server implementation. You will need a Firmata client library + with a network transport that can act as a TCP server or client in order to establish a connection between + StandardFirmataEthernet and the Firmata client application. + + To use StandardFirmataEthernet you will need to have one of the following + boards or shields: + + - Arduino Ethernet shield (or clone) + - Arduino Ethernet board (or clone) + - Arduino Yun + + Follow the instructions in the ethernetConfig.h file (ethernetConfig.h tab in Arduino IDE) to + configure your particular hardware. + + NOTE: If you are using an Arduino Ethernet shield you cannot use the following pins on + the following boards. Firmata will ignore any requests to use these pins: + + - Arduino Uno or other ATMega328 boards: (D4, D10, D11, D12, D13) + - Arduino Mega: (D4, D10, D50, D51, D52, D53) + - Arduino Leonardo: (D4, D10) + - Arduino Due: (D4, D10) + - Arduino Zero: (D4, D10) + + If you are using an ArduinoEthernet board, the following pins cannot be used (same as Uno): + - D4, D10, D11, D12, D13 +*/ + +#include +#include +#include + +/* + * Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your + * connection that may help in the event of connection issues. If defined, some boards may not begin + * executing this sketch until the Serial console is opened. + */ +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +// follow the instructions in ethernetConfig.h to configure your particular hardware +#include "ethernetConfig.h" +#include "utility/EthernetClientStream.h" +#include "utility/EthernetServerStream.h" + +/* + * Uncomment the following include to enable interfacing with Serial devices via hardware or + * software serial. + * + * DO NOT uncomment if you are running StandardFirmataEthernet on an Arduino Leonardo, + * Arduino Micro or other ATMega32u4-based board or you will not have enough Flash and RAM + * remaining to reliably run Firmata. Arduino Yun is okay because it doesn't import the Ethernet + * libraries. + */ +// In order to use software serial, you will need to compile this sketch with +// Arduino IDE v1.6.6 or higher. Hardware serial should work back to Arduino 1.0. +//#include "utility/SerialFirmata.h" + +#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 + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#if defined remote_ip && !defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, remote_ip, NULL, network_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, network_port); +#endif +#endif + +#if !defined remote_ip && defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, network_port ); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, network_port); +#endif +#endif + +#if !defined remote_ip && !defined remote_host +#ifdef local_ip +EthernetServerStream stream(local_ip, network_port); +#else +EthernetServerStream stream(IPAddress(0, 0, 0, 0), network_port); +#endif +#endif + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to sample analog inputs (in ms) + +/* i2c data */ +struct i2c_device_info { + byte addr; + int reg; + byte bytes; + byte stopTX; +}; + +/* for i2c read continuous mode */ +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; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Stream output queue using Stream.write() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + if (IS_PIN_ANALOG(pin)) { + // turn on/off reporting + reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + 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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +void printEthernetStatus() +{ + DEBUG_PRINT("Local IP Address: "); + IPAddress ip = Ethernet.localIP(); + DEBUG_PRINTLN(ip); +#ifdef remote_ip + DEBUG_PRINT("Connecting to server at: "); + DEBUG_PRINTLN(remote_ip); +#endif +} + +/* + * StandardFirmataEthernet communicates with Ethernet shields over SPI. Therefore all + * SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication. + * Additional pins may also need to be ignored depending on the particular board or + * shield in use. + */ +void ignorePins() +{ +#ifdef IS_IGNORE_PIN + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_IGNORE_PIN(i)) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } +#endif + +#ifdef WIZ5100_ETHERNET + // Arduino Ethernet and Arduino EthernetShield have SD SS wired to D4 + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA +#endif + +#endif // WIZ5100_ETHERNET +} + +void initTransport() +{ +#ifdef YUN_ETHERNET + Bridge.begin(); +#else +#ifdef local_ip + Ethernet.begin((uint8_t *)mac, local_ip); //start ethernet +#else + DEBUG_PRINTLN("Local IP will be requested from DHCP..."); + //start ethernet using dhcp + if (Ethernet.begin((uint8_t *)mac) == 0) { + DEBUG_PRINTLN("Failed to configure Ethernet using DHCP"); + } +#endif +#endif + printEthernetStatus(); +} + +void initFirmata() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + ignorePins(); + + // start up Network Firmata: + Firmata.begin(stream); + systemResetCallback(); // Initialize default configuration +} + +void setup() +{ + DEBUG_BEGIN(9600); + + initTransport(); + + initFirmata(); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * Stream buffer using Stream.write() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif + +#if !defined local_ip && !defined YUN_ETHERNET + // only necessary when using DHCP, ensures local IP is updated appropriately if it changes + if (Ethernet.maintain()) { + stream.maintain(Ethernet.localIP()); + } +#endif + +} diff --git a/libraries/Firmata/examples/StandardFirmataEthernet/ethernetConfig.h b/libraries/Firmata/examples/StandardFirmataEthernet/ethernetConfig.h new file mode 100644 index 0000000..baf3465 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataEthernet/ethernetConfig.h @@ -0,0 +1,94 @@ +/*============================================================================== + * NETWORK CONFIGURATION + * + * You must configure your particular hardware. Follow the steps below. + * + * By default, StandardFirmataEthernet is configured as a TCP client. + * To configure as a TCP server, see STEP 2 + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A or B) +// Option A is enabled by default. + +/* + * OPTION A: Configure for Arduino Ethernet board or Arduino Ethernet shield (or clone) + * + * To configure StandardFirmataEthernet to use the original WIZ5100-based + * ethernet shield or Arduino Ethernet uncomment the WIZ5100_ETHERNET define below + */ +#define WIZ5100_ETHERNET + +#ifdef WIZ5100_ETHERNET +#include +#include +EthernetClient client; +#endif + +/* + * OPTION B: Configure for Arduin Yun + * + * The Ethernet port on the Arduino Yun board can be used with Firmata in this configuration. + * + * To execute StandardFirmataEthernet on Yun uncomment the YUN_ETHERNET define below and make + * sure the WIZ5100_ETHERNET define (above) is commented out. + * + * On Yun there's no need to configure local_ip and mac address as this is automatically + * configured on the linux-side of Yun. + * + * Note that it may take several seconds to establish a connection with the Yun. + */ +//#define YUN_ETHERNET + +#ifdef YUN_ETHERNET +#include +#include +YunClient client; +#endif + +// STEP 2 [REQUIRED for all boards and shields] +// TCP Client configuration: +// To configure your board as a TCP client, set the IP address of the server you want to connect to. +// TCP Server configuration: +// To configure your board as a TCP server, comment out the following line and also ensure that +// remote_host is also commented out. +#define remote_ip IPAddress(10, 0, 0, 3) +// *** REMOTE HOST IS NOT YET WORKING *** +// replace with hostname of server you want to connect to, comment out if using 'remote_ip' +// #define remote_host "server.local" + +// STEP 3 [REQUIRED] +// Replace with the port that your client or server is listening on. +#define network_port 3030 + +// STEP 4 [REQUIRED unless using DHCP] +// Replace with your board or ethernet shield's IP address +// Comment out if you want to use DHCP +#define local_ip IPAddress(10, 0, 0, 15) + +// STEP 5 [REQUIRED] +// replace with ethernet shield mac. Must be unique for your network +const byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x53, 0xE5}; + +/*============================================================================== + * CONFIGURATION ERROR CHECK (don't change anything here) + *============================================================================*/ + +#if !defined WIZ5100_ETHERNET && !defined YUN_ETHERNET +#error "you must define either WIZ5100_ETHERNET or YUN_ETHERNET in ethernetConfig.h" +#endif + +#if defined remote_ip && defined remote_host +#error "cannot define both remote_ip and remote_host at the same time in ethernetConfig.h" +#endif + +/*============================================================================== + * PIN IGNORE MACROS (don't change anything here) + *============================================================================*/ + +#if defined(WIZ5100_ETHERNET) + +// ignore SPI pins, pin 10 (Ethernet SS) and pin 4 (SS for SD-Card on Ethernet shield) +#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 10) + +#endif diff --git a/libraries/Firmata/examples/StandardFirmataPlus/LICENSE.txt b/libraries/Firmata/examples/StandardFirmataPlus/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataPlus/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmataPlus/StandardFirmataPlus.ino b/libraries/Firmata/examples/StandardFirmataPlus/StandardFirmataPlus.ino new file mode 100644 index 0000000..8fb51eb --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataPlus/StandardFirmataPlus.ino @@ -0,0 +1,851 @@ +/* + 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. + + 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 August 17th, 2017 +*/ + +/* + README + + StandardFirmataPlus adds additional features that may exceed the Flash and + RAM sizes of Arduino boards such as ATMega328p (Uno) and ATMega32u4 + (Leonardo, Micro, Yun, etc). It is best to use StandardFirmataPlus with higher + memory boards such as the Arduino Mega, Arduino Due, Teensy 3.0/3.1/3.2. + + All Firmata examples that are appended with "Plus" add the following features: + + - Ability to interface with serial devices using UART, USART, or SoftwareSerial + depending on the capatilities of the board. + + NOTE: In order to use SoftwareSerial with the Firmata Serial feature, + StandardFirmataPlus must be compiled with Arduino v1.6.6 or newer. + + At the time of this writing, StandardFirmataPlus will still compile and run + on ATMega328p and ATMega32u4-based boards, but future versions of this sketch + may not as new features are added. +*/ + +#include +#include +#include + +// In order to use software serial, you will need to compile this sketch with +// Arduino IDE v1.6.6 or higher. Hardware serial should work back to Arduino 1.0. +#include "utility/SerialFirmata.h" + +#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 + + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to run the main loop (in ms) + +/* 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; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + 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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // Save a couple of seconds by disabling the startup blink sequence. + Firmata.disableBlinkVersion(); + + // to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega, + // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this: + // Serial1.begin(57600); + // Firmata.begin(Serial1); + // However do not do this if you are using SERIAL_MESSAGE + + Firmata.begin(57600); + while (!Serial) { + ; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101 + } + + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * FTDI buffer using Serial.print() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) + Firmata.processInput(); + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif +} diff --git a/libraries/Firmata/examples/StandardFirmataWiFi/LICENSE.txt b/libraries/Firmata/examples/StandardFirmataWiFi/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataWiFi/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/Firmata/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino b/libraries/Firmata/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino new file mode 100644 index 0000000..4f7e416 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino @@ -0,0 +1,1048 @@ +/* + 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) 2015-2016 Jesse Frush. All rights reserved. + Copyright (C) 2016 Jens B. 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 August 17th, 2017 +*/ + +/* + README + + StandardFirmataWiFi enables the use of Firmata over a TCP connection. It can be configured as + either a TCP server or TCP client. + + To use StandardFirmataWiFi you will need to have one of the following + boards or shields: + + - Arduino WiFi Shield (or clone) + - Arduino WiFi Shield 101 + - Arduino MKR1000 board + - ESP8266 WiFi board compatible with ESP8266 Arduino core + + Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to + configure your particular hardware. + + Dependencies: + - WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino + 1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source: + https://github.com/arduino-libraries/WiFi101) + - ESP8266 requires the Arduino ESP8266 core v2.1.0 or higher which can be obtained here: + https://github.com/esp8266/Arduino + + In order to use the WiFi Shield 101 with Firmata you will need a board with at least 35k of Flash + memory. This means you cannot use the WiFi Shield 101 with an Arduino Uno or any other + ATmega328p-based microcontroller or with an Arduino Leonardo or other ATmega32u4-based + microcontroller. Some boards that will work are: + + - Arduino Zero + - Arduino Due + - Arduino 101 + - Arduino Mega + + NOTE: If you are using an Arduino WiFi (legacy) shield you cannot use the following pins on + the following boards. Firmata will ignore any requests to use these pins: + + - Arduino Uno or other ATMega328 boards: (D4, D7, D10, D11, D12, D13) + - Arduino Mega: (D4, D7, D10, D50, D51, D52, D53) + - Arduino Due, Zero or Leonardo: (D4, D7, D10) + + If you are using an Arduino WiFi 101 shield you cannot use the following pins on the following + boards: + + - Arduino Due or Zero: (D5, D7, D10) + - Arduino Mega: (D5, D7, D10, D50, D52, D53) +*/ + +#include +#include +#include + +/* + * Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your + * connection that may help in the event of connection issues. If defined, some boards may not begin + * executing this sketch until the Serial console is opened. + */ +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +/* + * Uncomment the following include to enable interfacing with Serial devices via hardware or + * software serial. + */ +// In order to use software serial, you will need to compile this sketch with +// Arduino IDE v1.6.6 or higher. Hardware serial should work back to Arduino 1.0. +//#include "utility/SerialFirmata.h" + +// follow the instructions in wifiConfig.h to configure your particular hardware +#include "wifiConfig.h" + +#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 MAX_CONN_ATTEMPTS 20 // [500 ms] -> 10 s + +/*============================================================================== + * GLOBAL VARIABLES + *============================================================================*/ + +#ifdef FIRMATA_SERIAL_FEATURE +SerialFirmata serialFeature; +#endif + +#ifdef STATIC_IP_ADDRESS +IPAddress local_ip(STATIC_IP_ADDRESS); +#endif +#ifdef SUBNET_MASK +IPAddress subnet(SUBNET_MASK); +#endif +#ifdef GATEWAY_IP_ADDRESS +IPAddress gateway(GATEWAY_IP_ADDRESS); +#endif + +int connectionAttempts = 0; +bool streamConnected = false; + +/* analog inputs */ +int analogInputsToReport = 0; // bitwise array to store pin reporting + +/* digital input ports */ +byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence +byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + +/* pins configuration */ +byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + +/* timer variables */ +unsigned long currentMillis; // store the current value from millis() +unsigned long previousMillis; // for comparison with currentMillis +unsigned int samplingInterval = 19; // how often to sample analog inputs (in ms) + +/* i2c data */ +struct i2c_device_info { + byte addr; + int reg; + byte bytes; + byte stopTX; +}; + +/* for i2c read continuous mode */ +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; + +Servo servos[MAX_SERVOS]; +byte servoPinMap[TOTAL_PINS]; +byte detachedServos[MAX_SERVOS]; +byte detachedServoCount = 0; +byte servoCount = 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) +{ +#if ARDUINO >= 100 + Wire.write((byte)data); +#else + Wire.send(data); +#endif +} + +byte wireRead(void) +{ +#if ARDUINO >= 100 + return Wire.read(); +#else + return Wire.receive(); +#endif +} + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void attachServo(byte pin, int minPulse, int maxPulse) +{ + if (servoCount < MAX_SERVOS) { + // reuse indexes of detached servos until all have been reallocated + if (detachedServoCount > 0) { + servoPinMap[pin] = detachedServos[detachedServoCount - 1]; + if (detachedServoCount > 0) detachedServoCount--; + } else { + servoPinMap[pin] = servoCount; + servoCount++; + } + if (minPulse > 0 && maxPulse > 0) { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + } else { + servos[servoPinMap[pin]].attach(PIN_TO_DIGITAL(pin)); + } + } else { + Firmata.sendString("Max servos attached"); + } +} + +void detachServo(byte pin) +{ + servos[servoPinMap[pin]].detach(); + // if we're detaching the last servo, decrement the count + // otherwise store the index of the detached servo + if (servoPinMap[pin] == servoCount && servoCount > 0) { + servoCount--; + } else if (servoCount > 0) { + // keep track of detached servos because we want to reuse their indexes + // before incrementing the count of attached servos + detachedServoCount++; + detachedServos[detachedServoCount - 1] = servoPinMap[pin]; + } + + servoPinMap[pin] = 255; +} + +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; + + 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; +} + +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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("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 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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Stream output queue using Stream.write() */ +void checkDigitalInputs(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +// ----------------------------------------------------------------------------- +// function forward declarations for xtensa compiler (ESP8266) +void enableI2CPins(); +void disableI2CPins(); +void reportAnalogCallback(byte analogPin, int value); + +// ----------------------------------------------------------------------------- +/* 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_DIGITAL(pin) && mode != PIN_MODE_SERVO) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + } + 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)); + } + } + 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 +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + } + Firmata.setPinMode(pin, PIN_MODE_ANALOG); + } + break; + case INPUT: + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver +#if ARDUINO <= 100 + // deprecated since Arduino 1.0.1 - TODO: drop support in Firmata 2.6 + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups +#endif + 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)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + Firmata.setPinMode(pin, PIN_MODE_PWM); + } + break; + case PIN_MODE_SERVO: + if (IS_PIN_DIGITAL(pin)) { + Firmata.setPinMode(pin, PIN_MODE_SERVO); + if (servoPinMap[pin] == 255 || !servos[servoPinMap[pin]].attached()) { + // pass -1 for min and max pulse values to use default values set + // by Servo library + attachServo(pin, -1, -1); + } + } + 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: + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } + // TODO: save status to EEPROM here, if changed +} + +/* + * 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_SERVO: + if (IS_PIN_DIGITAL(pin)) + servos[servoPinMap[pin]].write(value); + Firmata.setPinState(pin, value); + break; + 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 +#if ARDUINO > 100 + pinMode(pin, INPUT_PULLUP); +#else + // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier + pinWriteMask |= mask; +#endif + } + 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) +{ + 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. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +void reportDigitalCallback(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)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; + + switch (command) { + case I2C_REQUEST: + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } + break; + case I2C_CONFIG: + delayTime = (argv[0] + (argv[1] << 7)); + + if (argc > 1 && delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + + break; + case SERVO_CONFIG: + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + + if (IS_PIN_DIGITAL(pin)) { + if (servoPinMap[pin] < MAX_SERVOS && servos[servoPinMap[pin]].attached()) { + detachServo(pin); + } + attachServo(pin, minPulse, maxPulse); + setPinModeCallback(pin, PIN_MODE_SERVO); + } + } + 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"); + } + 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_DIGITAL(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); + } + 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++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + break; + + case SERIAL_MESSAGE: +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.handleSysex(command, argc, argv); +#endif + break; + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void systemResetCallback() +{ + isResetting = true; + + // initialize a defalt state + // TODO: option to load config from EEPROM instead of default + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.reset(); +#endif + + if (isI2CEnabled) { + disableI2CPins(); + } + + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } + + for (byte i = 0; i < TOTAL_PINS; i++) { + // pins with analog capability default to analog input + // otherwise, pins default to digital output + if (IS_PIN_ANALOG(i)) { + // turns off pullup, configures everything + setPinModeCallback(i, PIN_MODE_ANALOG); + } else if (IS_PIN_DIGITAL(i)) { + // sets the output to 0, configures portConfigInputs + setPinModeCallback(i, OUTPUT); + } + + servoPinMap[i] = 255; + } + // by default, do not report any analog inputs + analogInputsToReport = 0; + + detachedServoCount = 0; + servoCount = 0; + + /* send digital inputs to set the initial state on the host computer, + * since once in the loop(), this firmware will only send on change */ + /* + TODO: this can never execute, since no pins default to digital input + but it will be needed when/if we support EEPROM stored config + for (byte i=0; i < TOTAL_PORTS; i++) { + outputPort(i, readPort(i, portConfigInputs[i]), true); + } + */ + isResetting = false; +} + +/* + * Called when a TCP connection is either connected or disconnected. + * TODO: + * - report connected or reconnected state to host (to be added to protocol) + * - report current state to host (to be added to protocol) + */ +void hostConnectionCallback(byte state) +{ + switch (state) { + case HOST_CONNECTION_CONNECTED: + DEBUG_PRINTLN( "TCP connection established" ); + break; + case HOST_CONNECTION_DISCONNECTED: + DEBUG_PRINTLN( "TCP connection disconnected" ); + break; + } +} + +/* + * Print the status of the WiFi connection. This is the connection to the access point rather + * than the TCP connection. + */ +void printWifiStatus() { + if ( WiFi.status() != WL_CONNECTED ) + { + DEBUG_PRINT( "WiFi connection failed. Status value: " ); + DEBUG_PRINTLN( WiFi.status() ); + } + else + { + // print the SSID of the network you're attached to: + DEBUG_PRINT( "SSID: " ); + DEBUG_PRINTLN( WiFi.SSID() ); + + // print your WiFi shield's IP address: + DEBUG_PRINT( "IP Address: " ); + IPAddress ip = WiFi.localIP(); + DEBUG_PRINTLN( ip ); + + // print the received signal strength: + DEBUG_PRINT( "signal strength (RSSI): " ); + long rssi = WiFi.RSSI(); + DEBUG_PRINT( rssi ); + DEBUG_PRINTLN( " dBm" ); + } +} + +/* + * StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all + * SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication. + * Additional pins may also need to be ignored depending on the particular board or + * shield in use. + */ +void ignorePins() +{ +#ifdef IS_IGNORE_PIN + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_IGNORE_PIN(i)) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } +#endif + + //Set up controls for the Arduino WiFi Shield SS for the SD Card +#ifdef ARDUINO_WIFI_SHIELD + // Arduino WiFi Shield has SD SS wired to D4 + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA +#endif //defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + +#endif //ARDUINO_WIFI_SHIELD +} + +void initTransport() +{ + // This statement will clarify how a connection is being made + DEBUG_PRINT( "StandardFirmataWiFi will attempt a WiFi connection " ); +#if defined(WIFI_101) + DEBUG_PRINTLN( "using the WiFi 101 library." ); +#elif defined(ARDUINO_WIFI_SHIELD) + DEBUG_PRINTLN( "using the legacy WiFi library." ); +#elif defined(ESP8266_WIFI) + DEBUG_PRINTLN( "using the ESP8266 WiFi library." ); +#elif defined(HUZZAH_WIFI) + DEBUG_PRINTLN( "using the HUZZAH WiFi library." ); + //else should never happen here as error-checking in wifiConfig.h will catch this +#endif //defined(WIFI_101) + + // Configure WiFi IP Address +#ifdef STATIC_IP_ADDRESS + DEBUG_PRINT( "Using static IP: " ); + DEBUG_PRINTLN( local_ip ); +#if defined(ESP8266_WIFI) || (defined(SUBNET_MASK) && defined(GATEWAY_IP_ADDRESS)) + stream.config( local_ip , gateway, subnet ); +#else + // you can also provide a static IP in the begin() functions, but this simplifies + // ifdef logic in this sketch due to support for all different encryption types. + stream.config( local_ip ); +#endif +#else + DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); +#endif + + stream.attach(hostConnectionCallback); + + // Configure WiFi security and initiate WiFi connection +#if defined(WIFI_WEP_SECURITY) + DEBUG_PRINT( "Attempting to connect to WEP SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid, wep_index, wep_key); +#elif defined(WIFI_WPA_SECURITY) + DEBUG_PRINT( "Attempting to connect to WPA SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid, wpa_passphrase); +#else //OPEN network + DEBUG_PRINTLN( "Attempting to connect to open SSID: " ); + DEBUG_PRINTLN(ssid); + stream.begin(ssid); +#endif //defined(WIFI_WEP_SECURITY) + DEBUG_PRINTLN( "WiFi setup done" ); + + // Wait for connection to access point to be established. + while (WiFi.status() != WL_CONNECTED && ++connectionAttempts <= MAX_CONN_ATTEMPTS) { + delay(500); + DEBUG_PRINT("."); + } + printWifiStatus(); +} + +void initFirmata() +{ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); + Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); + Firmata.attach(REPORT_ANALOG, reportAnalogCallback); + Firmata.attach(REPORT_DIGITAL, reportDigitalCallback); + Firmata.attach(SET_PIN_MODE, setPinModeCallback); + Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback); + Firmata.attach(START_SYSEX, sysexCallback); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + ignorePins(); + + // Initialize Firmata to use the WiFi stream object as the transport. + Firmata.begin(stream); + systemResetCallback(); // reset to default config +} + +void setup() +{ + DEBUG_BEGIN(9600); + + initTransport(); + + initFirmata(); +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + byte pin, analogPin; + + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * Stream buffer using Stream.write() */ + checkDigitalInputs(); + + /* STREAMREAD - processing incoming messagse as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); + } + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } + } + +#ifdef FIRMATA_SERIAL_FEATURE + serialFeature.update(); +#endif + + stream.maintain(); +} diff --git a/libraries/Firmata/examples/StandardFirmataWiFi/wifiConfig.h b/libraries/Firmata/examples/StandardFirmataWiFi/wifiConfig.h new file mode 100644 index 0000000..bedc744 --- /dev/null +++ b/libraries/Firmata/examples/StandardFirmataWiFi/wifiConfig.h @@ -0,0 +1,240 @@ +/*============================================================================== + * WIFI CONFIGURATION + * + * You must configure your particular hardware. Follow the steps below. + * + * By default, StandardFirmataWiFi is configured as a TCP server, to configure + * as a TCP client, see STEP 2. + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) +// Arduino MKR1000 or ESP8266 are enabled by default if compiling for either of those boards. + +/* + * OPTION A: Configure for Arduino MKR1000 or Arduino WiFi Shield 101 + * + * This will configure StandardFirmataWiFi to use the WiFi101 library, which works with the + * Arduino WiFi101 shield and devices that have the WiFi101 chip built in (such as the MKR1000). + * It is compatible with 802.11 B/G/N networks. + * + * If you are using the MKR1000 board, continue on to STEP 2. If you are using the WiFi 101 shield, + * follow the instructions below. + * + * To enable for the WiFi 101 shield, uncomment the #define WIFI_101 below and verify the + * #define ARDUINO_WIFI_SHIELD is commented out for OPTION B. + * + * IMPORTANT: You must have the WiFI 101 library installed. To easily install this library, open + * the library manager via: Arduino IDE Menus: Sketch > Include Library > Manage Libraries > filter + * search for "WiFi101" > Select the result and click 'install' + */ +//#define WIFI_101 + +//do not modify the following 11 lines +#if defined(ARDUINO_SAMD_MKR1000) && !defined(WIFI_101) +// automatically include if compiling for MRK1000 +#define WIFI_101 +#endif +#ifdef WIFI_101 +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #define WIFI_LIB_INCLUDED +#endif + +/* + * OPTION B: Configure for legacy Arduino WiFi shield + * + * This will configure StandardFirmataWiFi to use the original WiFi library (deprecated) provided + * with the Arduino IDE. It is supported by the Arduino WiFi shield (a discontinued product) and + * is compatible with 802.11 B/G networks. + * + * To configure StandardFirmataWiFi to use the legacy Arduino WiFi shield + * leave the #define below uncommented and ensure #define WIFI_101 is commented out for OPTION A. + */ +//#define ARDUINO_WIFI_SHIELD + +//do not modify the following 10 lines +#ifdef ARDUINO_WIFI_SHIELD +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif +#endif + +/* + * OPTION C: Configure for ESP8266 + * + * This will configure StandardFirmataWiFi to use the ESP8266WiFi library for boards + * with an ESP8266 chip. It is compatible with 802.11 B/G/N networks. + * + * The appropriate libraries are included automatically when compiling for the ESP8266 so + * continue on to STEP 2. + * + * IMPORTANT: You must have the esp8266 board support installed. To easily install this board see + * the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager. + */ +//do not modify the following 14 lines +#ifdef ESP8266 +// automatically include if compiling for ESP8266 +#define ESP8266_WIFI +#endif +#ifdef ESP8266_WIFI +#include +#include "utility/WiFiClientStream.h" +#include "utility/WiFiServerStream.h" + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif +#endif + +/* + * OPTION D: Configure for HUZZAH + * + * HUZZAH with CC3000 is not yet supported, this will be added in a later revision to + * StandardFirmataWiFi. + * For HUZZAH with ESP8266 use ESP8266_WIFI. + */ + +//------------------------------ +// TODO +//------------------------------ +//#define HUZZAH_WIFI + + +// STEP 2 [OPTIONAL for all boards and shields] +// If you want to setup you board as a TCP client, uncomment the following define and replace +// the IP address with the IP address of your server. +//#define SERVER_IP 10, 0, 0, 15 + + +// STEP 3 [REQUIRED for all boards and shields] +// replace this with your wireless network SSID +char ssid[] = "your_network_name"; + + +// STEP 4 [OPTIONAL for all boards and shields] +// If you want to use a static IP (v4) address, uncomment the line below. You can also change the IP. +// If the first line is commented out, the WiFi shield will attempt to get an IP from the DHCP server. +// If you are using a static IP with the ESP8266 then you must also uncomment the SUBNET and GATEWAY. +//#define STATIC_IP_ADDRESS 192,168,1,113 +//#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, optional for others +//#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, optional for others + + +// STEP 5 [REQUIRED for all boards and shields] +// define your port number here, you will need this to open a TCP connection to your Arduino +#define SERVER_PORT 3030 + + +// STEP 6 [REQUIRED for all boards and shields] +// determine your network security type (OPTION A, B, or C). Option A is the most common, and the +// default. + +/* + * OPTION A: WPA / WPA2 + * + * WPA is the most common network security type. A passphrase is required to connect to this type. + * + * To enable, leave #define WIFI_WPA_SECURITY uncommented below, set your wpa_passphrase value + * appropriately, and do not uncomment the #define values under options B and C + */ +#define WIFI_WPA_SECURITY + +#ifdef WIFI_WPA_SECURITY +char wpa_passphrase[] = "your_wpa_passphrase"; +#endif //WIFI_WPA_SECURITY + + +/* + * OPTION B: WEP + * + * WEP is a less common (and regarded as less safe) security type. A WEP key and its associated + * index are required to connect to this type. + * + * To enable, Uncomment the #define below, set your wep_index and wep_key values appropriately, + * and verify the #define values under options A and C are commented out. + */ +//#define WIFI_WEP_SECURITY + +#ifdef WIFI_WEP_SECURITY +//The wep_index below is a zero-indexed value. +//Valid indices are [0-3], even if your router/gateway numbers your keys [1-4]. +byte wep_index = 0; +char wep_key[] = "your_wep_key"; +#endif //WIFI_WEP_SECURITY + + +/* + * OPTION C: Open network (no security) + * + * Open networks have no security, can be connected to by any device that knows the ssid, and are + * unsafe. + * + * To enable, uncomment #define WIFI_NO_SECURITY below and verify the #define values + * under options A and B are commented out. + */ +//#define WIFI_NO_SECURITY + +/*============================================================================== + * CONFIGURATION ERROR CHECK (don't change anything here) + *============================================================================*/ + +#ifdef MULTIPLE_WIFI_LIB_INCLUDES +#error "you may not define more than one wifi device type in wifiConfig.h." +#endif + +#ifndef WIFI_LIB_INCLUDED +#error "you must define a wifi device type in wifiConfig.h." +#endif + +#if ((defined(WIFI_NO_SECURITY) && (defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY))) || (defined(WIFI_WEP_SECURITY) && defined(WIFI_WPA_SECURITY))) +#error "you may not define more than one security type at the same time in wifiConfig.h." +#endif //WIFI_* security define check + +#if !(defined(WIFI_NO_SECURITY) || defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY)) +#error "you must define a wifi security type in wifiConfig.h." +#endif //WIFI_* security define check + +#if (defined(ESP8266_WIFI) && !(defined(WIFI_NO_SECURITY) || (defined(WIFI_WPA_SECURITY)))) +#error "you must choose between WIFI_NO_SECURITY and WIFI_WPA_SECURITY" +#endif + +/*============================================================================== + * WIFI STREAM (don't change anything here) + *============================================================================*/ + +#ifdef SERVER_IP + WiFiClientStream stream(IPAddress(SERVER_IP), SERVER_PORT); +#else + WiFiServerStream stream(SERVER_PORT); +#endif + +/*============================================================================== + * PIN IGNORE MACROS (don't change anything here) + *============================================================================*/ + +#if defined(WIFI_101) && !defined(ARDUINO_SAMD_MKR1000) +// ignore SPI pins, pin 5 (reset WiFi101 shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +// also don't ignore SS pin if it's not pin 10. Not needed for Arduino MKR1000. +#define IS_IGNORE_PIN(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7) + +#elif defined(ARDUINO_WIFI_SHIELD) && defined(__AVR_ATmega32U4__) +// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +// On Leonardo, pin 24 maps to D4 and pin 28 maps to D10 +#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10 || (p) == 24 || (p) == 28) + +#elif defined(ARDUINO_WIFI_SHIELD) +// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10) + +#elif defined(ESP8266_WIFI) && defined(SERIAL_DEBUG) +#define IS_IGNORE_PIN(p) ((p) == 1) + +#endif diff --git a/libraries/Firmata/extras/revisions.txt b/libraries/Firmata/extras/revisions.txt new file mode 100644 index 0000000..6290a72 --- /dev/null +++ b/libraries/Firmata/extras/revisions.txt @@ -0,0 +1,265 @@ +FIRMATA 2.5.7 - Aug 19, 2017 + +[core library] +* Added support for Adafruit nrf52 boards (hathach) +* Added TCP server option to StandardFirmataEthernet (MJPees) +* Added support for STM32-based boards (fpistm) +* Added support for MKRFox1200 (sandeepmistry) + +[StandardFirmata & variants] +* Fixed I2C config parameter interpretation (zfields) +* Improve debug output in StandardFirmataEthernet + +FIRMATA 2.5.6 - Mar 18, 2017 + +[core library] +* Fixed string encoder/decoder bug that also affected I2C (Zak Fields) +* Added support for Arduino Primo (chiararuggeri) +* Added unit tests for Firmata string message encoding/decoding + +FIRMATA 2.5.5 - Mar 6, 2017 + +[core library] +* Updated BLEStream for compatibility with CurieBLE v2 (Sandeep Mistry) +* Added support for MKRZero (Sandeep Mistry) + +This update also includes a number of changes from an ongoing refactor by +Zak Fields of the Firmata core. These changes don't impact user facing sketches, +they are all internal only. Changes include: + +* Split out parser logic into new lib free of Arduino-specific dependencies. +* Add new class to support cross platform marshalling of Firmata procedure calls. +* Split out core constants to separate file. +* Split out core defines to separate file. +* Added firmata namespace to core library classes. + +FIRMATA 2.5.4 - Oct 23, 2016 + +[core library] +* Added Teensy 3.5 and 3.6 to Boards.h +* Assign blinkVersionDisabled in constructor to fix compiler issue in Arduino 1.0.6 + +[StandardFirmata & variants] +* Only disable PWM when setting pinMode to OUTPUT if pinMode was previously PWM +* Forward declare some functions to fix compiler issues with older IDE versions + +FIRMATA 2.5.3 - Jun 18, 2016 + +[core library] +* Added ESP8266 support (Jens B. & Jacob Rosenthal) +* Added host connection callback (Jens B.) +* Added Wi-Fi TCP client (Jens B.) +* Added BLE transport (BLEStream based on BLESerial by Volta Molda) +* Fixed Arduino Galileo and Edison compile issues + +[StandardFirmata & variants] +* Added StandardFirmataBLE (for use with Arduino 101) +* Added ability to choose between Wi-Fi TCP client or server (Jens B.) +* Various updates to StandardFirmataWiFi (Jens B.) +* Increased I2C RX data buffer from 32 to 64 bytes (Rick Waldron) +* Removed StandardFirmataEthernetPlus +* Made StandardFirmataEtherent configurable (to optionally add Plus functionality) +* Improved configuration instructions for StandardFirmataEthernet and StandardFirmataWiFi + +FIRMATA 2.5.2 - Feb 15, 2016 + +[core library] +* Added Wi-Fi transport (Jesse Frush) +* Added support for Arduino MKR1000 (Jesse Frush) +* Moved Serial feature to own class SerialFirmata +* Moved pin config and pin state handling to Firmata.cpp +* Added new method disableBlinkVersion to provide a way to optionally bypass startup blink sequence + +[StandardFirmata & variants] +* Added StandardFirmataWiFi (Jesse Frush) +* Added ethernetConfig.h for StandardFirmataEthernet and StandardFirmtaEthernetPlus +* Removed serialUtils.h and using SerialFirmata class instead for Serial feature + +FIRMATA 2.5.1 - Dec 26, 2015 + +[core library] +* Added support for Arduino 101 +* Make VERSION_BLINK_PIN optional +* Separate protocol version from firmware version. + Use FIRMATA_PROTOCOL_VERSION_[MAJOR/MINOR/BUGFIX] for protocol and use + FIRMATA_FIRMWARE_VERSION_[MAJOR/MINOR/BUGFIX] for firmware (library version). + +[StandardFirmata & variants] +* Added ability to auto-restart I2C transmission by setting bit 6 of byte 3 + of the I2C_REQUEST message. + +FIRMATA 2.5.0 - Nov 7, 2015 + +[core library] +* Added Serial feature for interfacing with serial devices via hardware + or software serial. See github.com/firmata/protocol/serial.md for details +* Added ability to set the value of a pin by sending a single value instead + of a port value. See 'set digital pin value' in github.com/firmata/protocol/protocol.md + for details +* Added support for Arduino Zero board +* Added support for Teensy LC board (copied from Teensy Firmata lib) +* Added support for Pinoccio Scout board (Pawel Szymczykowski) +* Lowered minimun sampling interval from 10 to 1 millisecond +* Added new pin mode (PIN_MODE_PULLUP) for setting the INPUT_PULLUP pin mode +* Changed pin mode defines to safer names (old names still included but + deprecated) - see Firmata.h + +[StandardFirmata & variants] +* Created new StandardFirmataPlus that implements the Serial feature + Note: The new Serial features is only implemented in the "Plus" versions of + StandardFirmata. +* Created new StandardFirmataEthernetPlus that implements the Serial feature +* Fixed issue where StandardFirmata was not compiling for Intel Galileo boards +* Moved StandardFirmataYun to its own repo (github.com/firmata/StandardFirmataYun) + +FIRMATA 2.4.4 - Aug 9, 2015 + +[core library] +* Added support for chipKIT boards (Brian Schmalz, Rick Anderson and Keith Vogel) +* Added support for ATmega328 boards (previously only ATmega328p was supported) + +[StandardFirmata] +* Added StandardFirmataChipKIT for ChipKIT boards (Brian Schmalz, Rick Anderson and Keith Vogel) +* Ensure Serial is ready on Leonardo and other ATMega32u4-based boards + +FIRMATA 2.4.3 - Apr 11, 2015 + +[core library] +* Added debug macros (utility/firmataDebug.h) +* Added Norbert Truchsess' EthernetClientStream lib from the configurable branch + +[examples] +* Added StandardFirmataEthernet to enable Firmata over Ethernet +* Minor updates to StandardFirmata and StandardFirmataYun + +FIRMATA 2.4.2 - Mar 16, 2015 + +[core library] +* Add support for Teesy 3.1 (Olivier Louvignes) + +FIRMATA 2.4.1 - Feb 7, 2015 + +[core library] +* Fixed off-by-one bug in setFirmwareNameAndVersion (Brian Schmalz) + +[StandardFirmata] +* Prevent analog values from being reported during system reset + +FIRMATA 2.4.0 - Dec 21, 2014 + +Changes from 2.3.6 to 2.4 that may impact existing Firmata client implementations: + +* When sending a string from the client application to the board (STRING_DATA) a +static buffer is now used for the incoming string in place of a dynamically allocated +block of memory (see Firmata.cpp lines 181 - 205). In Firmata 2.3.6 and older, +the dynamically allocated block was never freed, causing a memory leak. If your +client library had freed this memory in the string callback method, that code +will break in Firmata 2.4. If the string data needs to persist beyond the string +callback, it should be copied within the string callback. + +* As of Firmata 2.4, when digital port reporting or analog pin reporting is enabled, +the value of the port (digital) or pin (analog) is immediately sent back to the client +application. This will likely not have a negative impact on existing client +implementations, but may be unexpected. This feature was added to better support +non-serial streams (such as Ethernet, Wi-Fi, Bluetooth, etc) that may lose +connectivity and need a quick way to get the current state of the pins upon +reestablishing a connection. + +[core library] +* Changed sendValueAsTwo7bitBytes, startSysex and endSysex from private to + public methods. +* Added Intel Galileo to Boards.h +* Renamed FirmataSerial to FirmataStream +* Updated to latest Arduino library format +* writePort function in Boards.h now returns 1 (to suppress compiler warning) +* Updated syntax highlighting (keywords.txt) +* Fixed IS_PIN_SPI ifndef condition in boards.h +* Added constants to Firmata.h to reserve configurable firmata features +* Fixed issue where firmwareName was not reported correctly in Windows +* Ensure incoming String via STRING_DATA command is null-terminated +* Fixed memory leak when receiving String via STRING_DATA command + (note this change may break existing code if you were manually deallocating + the incoming string in your string callback function. See code for details) +* Added ability for user to specify a filename when calling setFirmwareNameAndVersion +* Increased input data buffer size from 32 to 64 bytes + +[StandardFirmata] +* Updated I2C_READ_CONTINUOUSLY to work with or without slaveRegister (Rick Waldron) +* Added Yun variant of StandardFirmata +* When a digital port is enabled, its value is now immediately sent to the client +* When an analog pin is enabled, its value is now immediately sent to the client +* Changed the way servo pins are mapped to enable use of servos on + a wider range of pins, including analog pins. +* Fixed management of unexpected sized I2C replies (Nahuel Greco) +* Fixed a bug when removing a monitored device with I2C_STOP_Reading (Nahuel Greco) +* Fixed conditional expression in I2C_STOP_READING case +* Changed samplingInterval from type int to type unsigned int +* Shortened error message strings to save a few bytes + +[examples] +* Updated FirmataServo example to use new pin mapping technique +* Removed makefiles from examples (because they were not being updated) +* Updated all examples to set current firmware version + +FIRMATA 2.3.6 - Jun 18, 2013 (Version included with Arduino core libraries) + +[core library] +* Fixed bug introduced in 2.3.5 that broke ability to use Ethernet. + +FIRMATA 2.3.5 - May 21, 2013 + +[core library] +* Added Arduino Due to Boards.h +* Added Teensy 3.0 to Boards.h +* Updated unit tests to use ArduinoUnit v2.0 +* Renamed pin13strobe to strobeBlinkPin +* Removed blinkVersion method from begin method for non-serial streams +* Fixed memory leak in setting firmware version (Matthew Murdoch) +* Added unit tests for a few core functions (Matthew Murdoch) +* Added IS_PIN_SPI macro to all board definitions in Board.h (Norbert Truchsess) + +FIRMATA 2.3.4 - Feb 11, 2013 + +[core library] +* Fixed Stream implementation so Firmata can be used with Streams other than + Serial (Norbert Truchsess) + +FIRMATA 2.3.3 - Oct 6, 2012 + +[core library] +* Added write method to expose FirmataSerial.write +* Added Arduino Leonardo to Boards.h + +[StandardFirmata] +* Changed all instances of Serial.write to Firmata.write +* Fixed delayMicroseconds(0) bug in readAndReportData + +FIRMATA 2.3.0 - 2.3.2 + +* Removed angle from servo config +* Changed file extensions from .pde to .ino +* Added MEGA2560 to Boards.h +* Added I2C pins to Boards.h +* Modified examples to be compatible with Arduino 0022 and 1.0 or greater +* Removed I2CFirmata example +* Changes to StandardFirmata + * Added I2C support + * Added system reset message to reset all pins to default config on sysex reset + +FIRMATA 2.2 (changes prior to Firmata 2.3.0 were not well documented) + +* changes undocumented + +FIRMATA 2.1 + +* added support for changing the sampling interval +* added Servo support + +FIRMATA 2.0 + +* changed to 8-bit port-based digital messages to mirror ports from previous 14-bit ports modeled after the standard Arduino board. +* switched order of version message so major version is reported first + +FIRMATA 1.0 + +* switched to MIDI-compatible packet format (though the message interpretation differs) diff --git a/libraries/Firmata/keywords.txt b/libraries/Firmata/keywords.txt new file mode 100644 index 0000000..5ab13d0 --- /dev/null +++ b/libraries/Firmata/keywords.txt @@ -0,0 +1,90 @@ +####################################### +# Syntax Coloring Map For Firmata +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Firmata KEYWORD1 Firmata +callbackFunction KEYWORD1 callbackFunction +systemResetCallbackFunction KEYWORD1 systemResetCallbackFunction +stringCallbackFunction KEYWORD1 stringCallbackFunction +sysexCallbackFunction KEYWORD1 sysexCallbackFunction + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +printVersion KEYWORD2 +blinkVersion KEYWORD2 +printFirmwareVersion KEYWORD2 +setFirmwareVersion KEYWORD2 +setFirmwareNameAndVersion KEYWORD2 +available KEYWORD2 +processInput KEYWORD2 +isParsingMessage KEYWORD2 +parse KEYWORD2 +sendAnalog KEYWORD2 +sendDigital KEYWORD2 +sendDigitalPort KEYWORD2 +sendString KEYWORD2 +sendSysex KEYWORD2 +getPinMode KEYWORD2 +setPinMode KEYWORD2 +getPinState KEYWORD2 +setPinState KEYWORD2 +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +sendValueAsTwo7bitBytes KEYWORD2 +startSysex KEYWORD2 +endSysex KEYWORD2 +writePort KEYWORD2 +readPort KEYWORD2 +disableBlinkVersion KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + +FIRMATA_MAJOR_VERSION LITERAL1 +FIRMATA_MINOR_VERSION LITERAL1 +FIRMATA_BUGFIX_VERSION LITERAL1 + +MAX_DATA_BYTES LITERAL1 + +DIGITAL_MESSAGE LITERAL1 +ANALOG_MESSAGE LITERAL1 +REPORT_ANALOG LITERAL1 +REPORT_DIGITAL LITERAL1 +REPORT_VERSION LITERAL1 +SET_PIN_MODE LITERAL1 +SET_DIGITAL_PIN_VALUE LITERAL1 +SYSTEM_RESET LITERAL1 +START_SYSEX LITERAL1 +END_SYSEX LITERAL1 +REPORT_FIRMWARE LITERAL1 +STRING_DATA LITERAL1 + +PIN_MODE_ANALOG LITERAL1 +PIN_MODE_PWM LITERAL1 +PIN_MODE_SERVO LITERAL1 +PIN_MODE_SHIFT LITERAL1 +PIN_MODE_I2C LITERAL1 +PIN_MODE_ONEWIRE LITERAL1 +PIN_MODE_STEPPER LITERAL1 +PIN_MODE_ENCODER LITERAL1 +PIN_MODE_SERIAL LITERAL1 +PIN_MODE_PULLUP LITERAL1 +PIN_MODE_IGNORE LITERAL1 + +TOTAL_PINS LITERAL1 +TOTAL_ANALOG_PINS LITERAL1 +TOTAL_DIGITAL_PINS LITERAL1 +TOTAL_PIN_MODES LITERAL1 +TOTAL_PORTS LITERAL1 +ANALOG_PORT LITERAL1 +MAX_SERVOS LITERAL1 diff --git a/libraries/Firmata/library.properties b/libraries/Firmata/library.properties new file mode 100644 index 0000000..0dc9a34 --- /dev/null +++ b/libraries/Firmata/library.properties @@ -0,0 +1,9 @@ +name=Firmata +version=2.5.8 +author=Firmata Developers +maintainer=https://github.com/firmata/arduino +sentence=Enables the communication with computer apps using a standard serial protocol. For all Arduino/Genuino boards. +paragraph=The Firmata library implements the Firmata protocol for communicating with software on the host computer. This allows you to write custom firmware without having to create your own protocol and objects for the programming environment that you are using. +category=Device Control +url=https://github.com/firmata/arduino +architectures=* diff --git a/libraries/Firmata/readme.md b/libraries/Firmata/readme.md new file mode 100644 index 0000000..988edfd --- /dev/null +++ b/libraries/Firmata/readme.md @@ -0,0 +1,194 @@ +# Firmata + +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/firmata/arduino?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Firmata is a protocol for communicating with microcontrollers from software on a host computer. The [protocol](https://github.com/firmata/protocol) can be implemented in firmware on any microcontroller architecture as well as software on any host computer software package. The Arduino repository described here is a Firmata library for Arduino and Arduino-compatible devices. If you would like to contribute to Firmata, please see the [Contributing](#contributing) section below. + +# Contents + +- [Usage](#usage) +- [Firmata Client Libraries](#firmata-client-libraries) +- [Updating Firmata in the Arduino IDE - Arduino 1.6.4 and higher](#updating-firmata-in-the-arduino-ide---arduino-164-and-higher) +- [Cloning Firmata](#cloning-firmata) +- [Updating Firmata in the Arduino IDE - older versions (<= 1.6.3 or 1.0.x)](#updating-firmata-in-the-arduino-ide---older-versions--163-or-10x) + - [Mac OSX:](#mac-osx) + - [Windows](#windows) + - [Linux](#linux) +- [Using the Source code rather than release archive (only for versions older than Arduino 1.6.3)](#using-the-source-code-rather-than-release-archive-only-for-versions-older-than-arduino-163) +- [Contributing](#contributing) + +## Usage + +There are two main models of usage of Firmata. In one model, the author of the Arduino sketch uses the various methods provided by the Firmata library to selectively send and receive data between the Arduino device and the software running on the host computer. For example, a user can send analog data to the host using ``` Firmata.sendAnalog(analogPin, analogRead(analogPin)) ``` or send data packed in a string using ``` Firmata.sendString(stringToSend) ```. See File -> Examples -> Firmata -> AnalogFirmata & EchoString respectively for examples. + +The second and more common model is to load a general purpose sketch called StandardFirmata (or one of the variants such as StandardFirmataPlus or StandardFirmataEthernet depending on your needs) on the Arduino board and then use the host computer exclusively to interact with the Arduino board. StandardFirmata is located in the Arduino IDE in File -> Examples -> Firmata. + +## Firmata Client Libraries +Most of the time you will be interacting with Arduino with a client library on the host computers. Several Firmata client libraries have been implemented in a variety of popular programming languages: + +* processing + * [https://github.com/firmata/processing](https://github.com/firmata/processing) + * [http://funnel.cc](http://funnel.cc) +* python + * [https://github.com/MrYsLab/pymata-aio](https://github.com/MrYsLab/pymata-aio) + * [https://github.com/MrYsLab/PyMata]([https://github.com/MrYsLab/PyMata) + * [https://github.com/tino/pyFirmata](https://github.com/tino/pyFirmata) + * [https://github.com/lupeke/python-firmata](https://github.com/lupeke/python-firmata) + * [https://github.com/firmata/pyduino](https://github.com/firmata/pyduino) +* perl + * [https://github.com/ntruchsess/perl-firmata](https://github.com/ntruchsess/perl-firmata) + * [https://github.com/rcaputo/rx-firmata](https://github.com/rcaputo/rx-firmata) +* ruby + * [https://github.com/hardbap/firmata](https://github.com/hardbap/firmata) + * [https://github.com/PlasticLizard/rufinol](https://github.com/PlasticLizard/rufinol) + * [http://funnel.cc](http://funnel.cc) +* clojure + * [https://github.com/nakkaya/clodiuno](https://github.com/nakkaya/clodiuno) + * [https://github.com/peterschwarz/clj-firmata](https://github.com/peterschwarz/clj-firmata) +* javascript + * [https://github.com/firmata/firmata.js](https://github.com/firmata/firmata.js) + * [https://github.com/rwldrn/johnny-five](https://github.com/rwldrn/johnny-five) + * [http://breakoutjs.com](http://breakoutjs.com) +* java + * [https://github.com/kurbatov/firmata4j](https://github.com/kurbatov/firmata4j) + * [https://github.com/4ntoine/Firmata](https://github.com/4ntoine/Firmata) + * [https://github.com/reapzor/FiloFirmata](https://github.com/reapzor/FiloFirmata) +* .NET + * [https://github.com/SolidSoils/Arduino](https://github.com/SolidSoils/Arduino) + * [http://www.acraigie.com/programming/firmatavb/default.html](http://www.acraigie.com/programming/firmatavb/default.html) +* Flash/AS3 + * [http://funnel.cc](http://funnel.cc) + * [http://code.google.com/p/as3glue/](http://code.google.com/p/as3glue/) +* PHP + * [https://github.com/ThomasWeinert/carica-firmata]() + * [https://github.com/oasynnoum/phpmake_firmata](https://github.com/oasynnoum/phpmake_firmata) +* Haskell + * [http://hackage.haskell.org/package/hArduino](http://hackage.haskell.org/package/hArduino) +* iOS + * [https://github.com/jacobrosenthal/iosfirmata](https://github.com/jacobrosenthal/iosfirmata) +* Dart + * [https://github.com/nfrancois/firmata](https://github.com/nfrancois/firmata) +* Max/MSP + * [http://www.maxuino.org/](http://www.maxuino.org/) +* Elixir + * [https://github.com/kfatehi/firmata](https://github.com/kfatehi/firmata) +* Modelica + * [https://www.wolfram.com/system-modeler/libraries/model-plug/](https://www.wolfram.com/system-modeler/libraries/model-plug/) +* Go + * [https://github.com/kraman/go-firmata](https://github.com/kraman/go-firmata) +* vvvv + * [https://vvvv.org/blog/arduino-second-service](https://vvvv.org/blog/arduino-second-service) +* openFrameworks + * [http://openframeworks.cc/documentation/communication/ofArduino/](http://openframeworks.cc/documentation/communication/ofArduino/) +* Rust + * [https://github.com/zankich/rust-firmata](https://github.com/zankich/rust-firmata) + +Note: The above libraries may support various versions of the Firmata protocol and therefore may not support all features of the latest Firmata spec nor all Arduino and Arduino-compatible boards. Refer to the respective projects for details. + +## Updating Firmata in the Arduino IDE - Arduino 1.6.4 and higher + +If you want to update to the latest stable version: + +1. Open the Arduino IDE and navigate to: `Sketch > Include Library > Manage Libraries` +2. Filter by "Firmata" and click on the "Firmata by Firmata Developers" item in the list of results. +3. Click the `Select version` dropdown and select the most recent version (note you can also install previous versions) +4. Click `Install`. + +### Cloning Firmata + +If you are contributing to Firmata or otherwise need a version newer than the latest tagged release, you can clone Firmata directly to your Arduino/libraries/ directory (where 3rd party libraries are installed). This only works for Arduino 1.6.4 and higher, for older versions you need to clone into the Arduino application directory (see section below titled "Using the Source code rather than release archive"). Be sure to change the name to Firmata as follows: + +```bash +$ git clone git@github.com:firmata/arduino.git ~/Documents/Arduino/libraries/Firmata +``` + +*Update path above if you're using Windows or Linux or changed the default Arduino directory on OS X* + + +## Updating Firmata in the Arduino IDE - older versions (<= 1.6.3 or 1.0.x) + +Download the latest [release](https://github.com/firmata/arduino/releases/tag/2.5.8) (for Arduino 1.0.x or Arduino 1.5.6 or higher) and replace the existing Firmata folder in your Arduino application. See the instructions below for your platform. + +*Note that Arduino 1.5.0 - 1.5.5 are not supported. Please use Arduino 1.5.6 or higher (or Arduino 1.0.5 or 1.0.6).* + +### Mac OSX: + +The Firmata library is contained within the Arduino package. + +1. Navigate to the Arduino application +2. Right click on the application icon and select `Show Package Contents` +3. Navigate to: `/Contents/Resources/Java/libraries/` and replace the existing +`Firmata` folder with latest [Firmata release](https://github.com/firmata/arduino/releases/tag/2.5.8) (note there is a different download +for Arduino 1.0.x vs 1.6.x) +4. Restart the Arduino application and the latest version of Firmata will be available. + +*If you are using the Java 7 version of Arduino 1.5.7 or higher, the file path +will differ slightly: `Contents/Java/libraries/Firmata` (no Resources directory).* + +### Windows: + +1. Navigate to `c:/Program\ Files/arduino-1.x/libraries/` and replace the existing +`Firmata` folder with the latest [Firmata release](https://github.com/firmata/arduino/releases/tag/2.5.8) (note there is a different download +for Arduino 1.0.x vs 1.6.x). +2. Restart the Arduino application and the latest version of Firmata will be available. + +*Update the path and Arduino version as necessary* + +### Linux: + +1. Navigate to `~/arduino-1.x/libraries/` and replace the existing +`Firmata` folder with the latest [Firmata release](https://github.com/firmata/arduino/releases/tag/2.5.8) (note there is a different download +for Arduino 1.0.x vs 1.6.x). +2. Restart the Arduino application and the latest version of Firmata will be available. + +*Update the path and Arduino version as necessary* + +### Using the Source code rather than release archive (only for versions older than Arduino 1.6.3) + +*It is recommended you update to Arduino 1.6.4 or higher if possible, that way you can clone directly into the external Arduino/libraries/ directory which persists between Arduino application updates. Otherwise you will need to move your clone each time you update to a newer version of the Arduino IDE.* + +If you're stuck with an older version of the IDE, then follow these keep reading otherwise jump up to the "Cloning Firmata section above". + +Clone this repo directly into the core Arduino application libraries directory. If you are using +Arduino 1.5.x or <= 1.6.3, the repo directory structure will not match the Arduino +library format, however it should still compile as long as you are using Arduino 1.5.7 +or higher. + +You will first need to remove the existing Firmata library, then clone firmata/arduino +into an empty Firmata directory: + +```bash +$ rm -r /Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata +$ git clone git@github.com:firmata/arduino.git /Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata +``` + +*Update paths if you're using Windows or Linux* + +To generate properly formatted versions of Firmata (for Arduino 1.0.x and Arduino 1.6.x), run the +`release.sh` script. + +## Contributing + +If you discover a bug or would like to propose a new feature, please open a new [issue](https://github.com/firmata/arduino/issues?sort=created&state=open). Due to the limited memory of standard Arduino boards we cannot add every requested feature to StandardFirmata. Requests to add new features to StandardFirmata will be evaluated by the Firmata developers. However it is still possible to add new features to other Firmata implementations (Firmata is a protocol whereas StandardFirmata is just one of many possible implementations). + +To contribute, fork this repository and create a new topic branch for the bug, feature or other existing issue you are addressing. Submit the pull request against the *master* branch. + +If you would like to contribute but don't have a specific bugfix or new feature to contribute, you can take on an existing issue, see issues labeled "pull-request-encouraged". Add a comment to the issue to express your intent to begin work and/or to get any additional information about the issue. + +You must thoroughly test your contributed code. In your pull request, describe tests performed to ensure that no existing code is broken and that any changes maintain backwards compatibility with the existing api. Test on multiple Arduino board variants if possible. We hope to enable some form of automated (or at least semi-automated) testing in the future, but for now any tests will need to be executed manually by the contributor and reviewers. + +Use [Artistic Style](http://astyle.sourceforge.net/) (astyle) to format your code. Set the following rules for the astyle formatter: + +``` +style = "" +indent-spaces = 2 +indent-classes = true +indent-switches = true +indent-cases = true +indent-col1-comments = true +pad-oper = true +pad-header = true +keep-one-line-statements = true +``` + +If you happen to use Sublime Text, [this astyle plugin](https://github.com/timonwong/SublimeAStyleFormatter) is helpful. Set the above rules in the user settings file. diff --git a/libraries/Firmata/release.sh b/libraries/Firmata/release.sh new file mode 100644 index 0000000..0c47db8 --- /dev/null +++ b/libraries/Firmata/release.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +# use this script to package Firmata for distribution + +# package for Arduino 1.0.x +mkdir -p temp/Firmata +cp -r examples temp/Firmata +cp -r extras temp/Firmata +cp -r utility temp/Firmata +cp *.cpp temp/Firmata +cp *.h temp/Firmata +cp keywords.txt temp/Firmata +cp readme.md temp/Firmata +cd temp +find . -name "*.DS_Store" -type f -delete +zip -r Firmata.zip ./Firmata/ +cd .. +mv ./temp/Firmata.zip Arduino-1.0.x-Firmata-2.5.8.zip + +#package for Arduino 1.6.x and 1.8.x +cp library.properties temp/Firmata +cd temp/Firmata +mv readme.md ./extras/ +mkdir src +mv *.cpp ./src/ +mv *.h ./src/ +mv utility ./src/ +cd .. +find . -name "*.DS_Store" -type f -delete +zip -r Firmata.zip ./Firmata/ +cd .. +mv ./temp/Firmata.zip Firmata-2.5.8.zip +rm -r ./temp diff --git a/libraries/Firmata/test/firmata_test/firmata_test.ino b/libraries/Firmata/test/firmata_test/firmata_test.ino new file mode 100644 index 0000000..db05853 --- /dev/null +++ b/libraries/Firmata/test/firmata_test/firmata_test.ino @@ -0,0 +1,172 @@ +/* + * To run this test suite, you must first install the ArduinoUnit library + * to your Arduino/libraries/ directory. + * You can get ArduinoUnit here: https://github.com/mmurdoch/arduinounit + * Download version 2.0 or greater or install it via the Arduino library manager. + */ + +#include +#include + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Test::run(); +} + +test(beginPrintsVersion) +{ + FakeStream stream; + + Firmata.begin(stream); + + char expected[] = { + REPORT_VERSION, + FIRMATA_PROTOCOL_MAJOR_VERSION, + FIRMATA_PROTOCOL_MINOR_VERSION, + 0 + }; + assertEqual(expected, stream.bytesWritten()); +} + +void processMessage(const byte *message, size_t length) +{ + FakeStream stream; + Firmata.begin(stream); + + for (size_t i = 0; i < length; i++) { + stream.nextByte(message[i]); + Firmata.processInput(); + } +} + +byte _digitalPort; +int _digitalPortValue; +void writeToDigitalPort(byte port, int value) +{ + _digitalPort = port; + _digitalPortValue = value; +} + +void setupDigitalPort() +{ + _digitalPort = 0; + _digitalPortValue = 0; +} + +char * _receivedString; +void handleStringCallback(char *str) +{ + _receivedString = str; +} + +test(processWriteDigital_0) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPortValue); +} + +test(processWriteDigital_127) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 127, 0 }; + processMessage(message, 3); + + assertEqual(127, _digitalPortValue); +} + +test(processWriteDigital_128) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 1 }; + processMessage(message, 3); + + assertEqual(128, _digitalPortValue); +} + +test(processWriteLargestDigitalValue) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0x7F, 0x7F }; + processMessage(message, 3); + + // Maximum of 14 bits can be set (B0011111111111111) + assertEqual(0x3FFF, _digitalPortValue); +} + +test(defaultDigitalWritePortIsZero) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPort); +} + +test(specifiedDigitalWritePort) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE + 1, 0, 0 }; + processMessage(message, 3); + + assertEqual(1, _digitalPort); +} + +test(setFirmwareVersionDoesNotLeakMemory) +{ + Firmata.setFirmwareVersion(1, 0); + int initialMemory = freeMemory(); + + Firmata.setFirmwareVersion(1, 0); + + assertEqual(0, initialMemory - freeMemory()); +} + +test(sendStringShouldEncode2BytesPerChar) +{ + FakeStream stream; + Firmata.begin(stream); + // reset the buffer because the firmware name string will be sent on Firmata.begin + stream.reset(); + + char testString[] = "hi!"; + Firmata.sendString(testString); + + byte expected[] = { START_SYSEX, STRING_DATA, 'h', 0, 'i', 0, '!', 0, END_SYSEX }; + + int len = stream.bytesWritten().length(); + assertEqual(sizeof(expected), len); + for (byte i = 0; i < len; i++) { + assertEqual(expected[i], (byte)stream.bytesWritten().charAt(i)); + } +} + +test(receivedStringShouldDecodeFrom2BytesPerChar) +{ + Firmata.attach(STRING_DATA, handleStringCallback); + + byte message[] = { START_SYSEX, STRING_DATA, 'b', 0, 'y', 0, 'e', 0, '!', 0, END_SYSEX }; + processMessage(message, 11); + + assertEqual("bye!", _receivedString); +} + diff --git a/libraries/Firmata/test/readme.md b/libraries/Firmata/test/readme.md new file mode 100644 index 0000000..ab8f8c3 --- /dev/null +++ b/libraries/Firmata/test/readme.md @@ -0,0 +1,13 @@ +# Testing Firmata + +Tests tests are written using the [ArduinoUnit](https://github.com/mmurdoch/arduinounit) library (version 2.0). + +Follow the instructions in the [ArduinoUnit readme](https://github.com/mmurdoch/arduinounit/blob/master/readme.md) to install the library. + +Compile and upload the test sketch as you would any other sketch. Then open the +Serial Monitor to view the test results. + +If you make changes to Firmata.cpp, run the tests in /test/ to ensure +that your changes have not produced any unexpected errors. + +You should also perform manual tests against actual hardware. diff --git a/libraries/Firmata/utility/BLEStream.cpp b/libraries/Firmata/utility/BLEStream.cpp new file mode 100644 index 0000000..ea50840 --- /dev/null +++ b/libraries/Firmata/utility/BLEStream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in BLEStream.h to avoid linker issues. + */ diff --git a/libraries/Firmata/utility/BLEStream.h b/libraries/Firmata/utility/BLEStream.h new file mode 100644 index 0000000..56b3648 --- /dev/null +++ b/libraries/Firmata/utility/BLEStream.h @@ -0,0 +1,243 @@ +/* + BLEStream.h + + Based on BLESerial.cpp by Voita Molda + https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h + + Last updated April 4th, 2016 + */ + +#ifndef _BLE_STREAM_H_ +#define _BLE_STREAM_H_ + +#include +#if defined(_VARIANT_ARDUINO_101_X_) +#include +#define _MAX_ATTR_DATA_LEN_ BLE_MAX_ATTR_DATA_LEN +#else +#include +#define _MAX_ATTR_DATA_LEN_ BLE_ATTRIBUTE_MAX_VALUE_LENGTH +#endif + +#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80 +#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer + +// #define BLE_SERIAL_DEBUG + +class BLEStream : public BLEPeripheral, public Stream +{ + public: + BLEStream(unsigned char req = 0, unsigned char rdy = 0, unsigned char rst = 0); + + void begin(...); + bool poll(); + void end(); + void setFlushInterval(int); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t byte); + using Print::write; + virtual operator bool(); + + private: + bool _connected; + unsigned long _flushed; + int _flushInterval; + static BLEStream* _instance; + + size_t _rxHead; + size_t _rxTail; + size_t _rxCount() const; + unsigned char _rxBuffer[256]; + size_t _txCount; + unsigned char _txBuffer[_MAX_ATTR_DATA_LEN_]; + + BLEService _uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); + BLEDescriptor _uartNameDescriptor = BLEDescriptor("2901", "UART"); + BLECharacteristic _rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, _MAX_ATTR_DATA_LEN_); + BLEDescriptor _rxNameDescriptor = BLEDescriptor("2901", "RX - Receive Data (Write)"); + BLECharacteristic _txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, _MAX_ATTR_DATA_LEN_); + BLEDescriptor _txNameDescriptor = BLEDescriptor("2901", "TX - Transfer Data (Notify)"); + + void _received(const unsigned char* data, size_t size); + static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic); +}; + + +/* + * BLEStream.cpp + * Copied here as a hack to avoid having to install the BLEPeripheral libarary even if it's + * not needed. + */ + +BLEStream* BLEStream::_instance = NULL; + +BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) : +#if defined(_VARIANT_ARDUINO_101_X_) + BLEPeripheral() +#else + BLEPeripheral(req, rdy, rst) +#endif +{ + this->_txCount = 0; + this->_rxHead = this->_rxTail = 0; + this->_flushed = 0; + this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL; + BLEStream::_instance = this; + + addAttribute(this->_uartService); + addAttribute(this->_uartNameDescriptor); + setAdvertisedServiceUuid(this->_uartService.uuid()); + addAttribute(this->_rxCharacteristic); + addAttribute(this->_rxNameDescriptor); + this->_rxCharacteristic.setEventHandler(BLEWritten, BLEStream::_received); + addAttribute(this->_txCharacteristic); + addAttribute(this->_txNameDescriptor); +} + +void BLEStream::begin(...) +{ + BLEPeripheral::begin(); +#ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLEStream::begin()")); +#endif +} + +bool BLEStream::poll() +{ + // BLEPeripheral::poll is called each time connected() is called + this->_connected = BLEPeripheral::connected(); + if (millis() > this->_flushed + this->_flushInterval) { + flush(); + } + return this->_connected; +} + +void BLEStream::end() +{ + this->_rxCharacteristic.setEventHandler(BLEWritten, (void(*)(BLECentral&, BLECharacteristic&))NULL); + this->_rxHead = this->_rxTail = 0; + flush(); + BLEPeripheral::disconnect(); +} + +int BLEStream::available(void) +{ +// BLEPeripheral::poll only calls delay(1) in CurieBLE so skipping it here to avoid the delay +#ifndef _VARIANT_ARDUINO_101_X_ + // TODO Need to do more testing to determine if all of these calls to BLEPeripheral::poll are + // actually necessary. Seems to run fine without them, but only minimal testing so far. + BLEPeripheral::poll(); +#endif + int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer); +#ifdef BLE_SERIAL_DEBUG + if (retval > 0) { + Serial.print(F("BLEStream::available() = ")); + Serial.println(retval); + } +#endif + return retval; +} + +int BLEStream::peek(void) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_rxTail == this->_rxHead) return -1; + uint8_t byte = this->_rxBuffer[this->_rxTail]; +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::peek() = 0x")); + Serial.println(byte, HEX); +#endif + return byte; +} + +int BLEStream::read(void) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_rxTail == this->_rxHead) return -1; + this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer); + uint8_t byte = this->_rxBuffer[this->_rxTail]; +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::read() = 0x")); + Serial.println(byte, HEX); +#endif + return byte; +} + +void BLEStream::flush(void) +{ + if (this->_txCount == 0) return; +#ifndef _VARIANT_ARDUINO_101_X_ + // ensure there are available packets before sending + while(!this->_txCharacteristic.canNotify()) { + BLEPeripheral::poll(); + } +#endif + this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); + this->_flushed = millis(); + this->_txCount = 0; +#ifdef BLE_SERIAL_DEBUG + Serial.println(F("BLEStream::flush()")); +#endif +} + +size_t BLEStream::write(uint8_t byte) +{ +#ifndef _VARIANT_ARDUINO_101_X_ + BLEPeripheral::poll(); +#endif + if (this->_txCharacteristic.subscribed() == false) return 0; + this->_txBuffer[this->_txCount++] = byte; + if (this->_txCount == sizeof(this->_txBuffer)) flush(); +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::write( 0x")); + Serial.print(byte, HEX); + Serial.println(F(") = 1")); +#endif + return 1; +} + +BLEStream::operator bool() +{ + bool retval = this->_connected = BLEPeripheral::connected(); +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::operator bool() = ")); + Serial.println(retval); +#endif + return retval; +} + +void BLEStream::setFlushInterval(int interval) +{ + if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) { + this->_flushInterval = interval; + } +} + +void BLEStream::_received(const unsigned char* data, size_t size) +{ + for (size_t i = 0; i < size; i++) { + this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer); + this->_rxBuffer[this->_rxHead] = data[i]; + } +#ifdef BLE_SERIAL_DEBUG + Serial.print(F("BLEStream::received(")); + for (int i = 0; i < size; i++) Serial.print(data[i], HEX); + Serial.println(F(")")); +#endif +} + +void BLEStream::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic) +{ + BLEStream::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength()); +} + + +#endif // _BLE_STREAM_H_ diff --git a/libraries/Firmata/utility/BluefruitLE_SPI_Stream.cpp b/libraries/Firmata/utility/BluefruitLE_SPI_Stream.cpp new file mode 100644 index 0000000..93953e9 --- /dev/null +++ b/libraries/Firmata/utility/BluefruitLE_SPI_Stream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in BluefruitLE_SPI_Stream.h to avoid linker issues. + */ diff --git a/libraries/Firmata/utility/BluefruitLE_SPI_Stream.h b/libraries/Firmata/utility/BluefruitLE_SPI_Stream.h new file mode 100644 index 0000000..372e5aa --- /dev/null +++ b/libraries/Firmata/utility/BluefruitLE_SPI_Stream.h @@ -0,0 +1,157 @@ +/* + BluefruitLE_SPI_Stream.h + + Documentation for the various AT commands used below is available at + https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/at-commands + */ + +#ifndef _BLUEFRUIT_LE_SPI_STREAM_H_ +#define _BLUEFRUIT_LE_SPI_STREAM_H_ + +#include + + +class BluefruitLE_SPI_Stream : public Stream +{ + public: + BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin); + + void setLocalName(const char *localName); + void setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval); + void setFlushInterval(int flushInterval); + + void begin(); + bool poll(); + void end(); + + // Print overrides + size_t write(uint8_t byte); + using Print::write; // Expose other write variants + + // Stream overrides + int available(); + int read(); + int peek(); + void flush(); + + private: + Adafruit_BluefruitLE_SPI ble; + + String localName; + unsigned short minConnInterval; + unsigned short maxConnInterval; + + uint8_t txBuffer[SDEP_MAX_PACKETSIZE]; + size_t txCount; +}; + + +BluefruitLE_SPI_Stream::BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin) : + ble(csPin, irqPin, rstPin), + minConnInterval(0), + maxConnInterval(0), + txCount(0) +{ } + +void BluefruitLE_SPI_Stream::setLocalName(const char *localName) +{ + this->localName = localName; +} + +void BluefruitLE_SPI_Stream::setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval) +{ + this->minConnInterval = minConnInterval; + this->maxConnInterval = maxConnInterval; +} + +void BluefruitLE_SPI_Stream::setFlushInterval(int flushInterval) +{ + // Not used +} + +void BluefruitLE_SPI_Stream::begin() +{ + // Initialize the SPI interface + ble.begin(); + + // Perform a factory reset to make sure everything is in a known state + ble.factoryReset(); + + // Disable command echo from Bluefruit + ble.echo(false); + + // Change the MODE LED to indicate BLE UART activity + ble.println("AT+HWMODELED=BLEUART"); + + // Set local name + if (localName.length() > 0) { + ble.print("AT+GAPDEVNAME="); + ble.println(localName); + } + + // Set connection interval + if (minConnInterval > 0 && maxConnInterval > 0) { + ble.print("AT+GAPINTERVALS="); + ble.print(minConnInterval); + ble.print(","); + ble.print(maxConnInterval); + ble.println(",,,"); + } + + // Disable real and simulated mode switch (i.e. "+++") command + ble.println("AT+MODESWITCHEN=local,0"); + ble.enableModeSwitchCommand(false); + + // Switch to data mode + ble.setMode(BLUEFRUIT_MODE_DATA); +} + +bool BluefruitLE_SPI_Stream::poll() +{ + // If there's outgoing data in the buffer, just send it. The firmware on + // the nRF51822 will decide when to transmit the data in its TX FIFO. + if (txCount) flush(); + + // In order to check for a connection, we would need to switch from data to + // command mode and back again. However, due to the internal workings of + // Adafruit_BluefruitLE_SPI, this can lead to unread incoming data being + // lost. Therefore, we always return true. + return true; +} + +void BluefruitLE_SPI_Stream::end() +{ + flush(); + ble.end(); +} + +size_t BluefruitLE_SPI_Stream::write(uint8_t byte) +{ + txBuffer[txCount++] = byte; + if (txCount == sizeof(txBuffer)) flush(); + return 1; +} + +int BluefruitLE_SPI_Stream::available() +{ + return ble.available(); +} + +int BluefruitLE_SPI_Stream::read() +{ + return ble.read(); +} + +int BluefruitLE_SPI_Stream::peek() +{ + return ble.peek(); +} + +void BluefruitLE_SPI_Stream::flush() +{ + ble.write(txBuffer, txCount); + txCount = 0; +} + + +#endif // _BLUEFRUIT_LE_SPI_STREAM_H_ diff --git a/libraries/Firmata/utility/EthernetClientStream.cpp b/libraries/Firmata/utility/EthernetClientStream.cpp new file mode 100644 index 0000000..f4f8968 --- /dev/null +++ b/libraries/Firmata/utility/EthernetClientStream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in EthernetClientStream.h to avoid linker issues. + */ diff --git a/libraries/Firmata/utility/EthernetClientStream.h b/libraries/Firmata/utility/EthernetClientStream.h new file mode 100644 index 0000000..1b7d2e2 --- /dev/null +++ b/libraries/Firmata/utility/EthernetClientStream.h @@ -0,0 +1,141 @@ +/* + EthernetClientStream.h + An Arduino-Stream that wraps an instance of Client reconnecting to + the remote-ip in a transparent way. A disconnected client may be + recognized by the returnvalues -1 from calls to peek or read and + a 0 from calls to write. + + Copyright (C) 2013 Norbert Truchsess. 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 June 18th, 2016 + */ + +#ifndef ETHERNETCLIENTSTREAM_H +#define ETHERNETCLIENTSTREAM_H + +#include +#include + +//#define SERIAL_DEBUG +#include "firmataDebug.h" + +#define MILLIS_RECONNECT 5000 + +class EthernetClientStream : public Stream +{ + public: + EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port); + int available(); + int read(); + int peek(); + void flush(); + size_t write(uint8_t); + void maintain(IPAddress localip); + + private: + Client &client; + IPAddress localip; + IPAddress ip; + const char* host; + uint16_t port; + bool connected; + uint32_t time_connect; + bool maintain(); + void stop(); +}; + + +/* + * EthernetClientStream.cpp + * Copied here as a hack to linker issues with 3rd party board packages that don't properly + * implement the Arduino network APIs. + */ +EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port) + : client(client), + localip(localip), + ip(ip), + host(host), + port(port), + connected(false) +{ +} + +int +EthernetClientStream::available() +{ + return maintain() ? client.available() : 0; +} + +int +EthernetClientStream::read() +{ + return maintain() ? client.read() : -1; +} + +int +EthernetClientStream::peek() +{ + return maintain() ? client.peek() : -1; +} + +void EthernetClientStream::flush() +{ + if (maintain()) + client.flush(); +} + +size_t +EthernetClientStream::write(uint8_t c) +{ + return maintain() ? client.write(c) : 0; +} + +void +EthernetClientStream::maintain(IPAddress localip) +{ + // ensure the local IP is updated in the case that it is changed by the DHCP server + if (this->localip != localip) { + this->localip = localip; + if (connected) + stop(); + } +} + +void +EthernetClientStream::stop() +{ + client.stop(); + connected = false; + time_connect = millis(); +} + +bool +EthernetClientStream::maintain() +{ + if (client && client.connected()) + return true; + + if (connected) { + stop(); + } + // if the client is disconnected, attempt to reconnect every 5 seconds + else if (millis() - time_connect >= MILLIS_RECONNECT) { + connected = host ? client.connect(host, port) : client.connect(ip, port); + if (!connected) { + time_connect = millis(); + DEBUG_PRINTLN("Connection failed. Attempting to reconnect..."); + } else { + DEBUG_PRINTLN("Connected"); + } + } + return connected; +} + +#endif /* ETHERNETCLIENTSTREAM_H */ diff --git a/libraries/Firmata/utility/EthernetServerStream.cpp b/libraries/Firmata/utility/EthernetServerStream.cpp new file mode 100644 index 0000000..de466f5 --- /dev/null +++ b/libraries/Firmata/utility/EthernetServerStream.cpp @@ -0,0 +1,3 @@ +/* + * Implementation is in EthernetServerStream.h to avoid linker issues. + */ diff --git a/libraries/Firmata/utility/EthernetServerStream.h b/libraries/Firmata/utility/EthernetServerStream.h new file mode 100644 index 0000000..56f541e --- /dev/null +++ b/libraries/Firmata/utility/EthernetServerStream.h @@ -0,0 +1,147 @@ +/* + EthernetServerStream.h + + Copyright (C) 2017 Marc Josef Pees. 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 July 10th, 2017 + */ + +#ifndef ETHERNETSERVERSTREAM_H +#define ETHERNETSERVERSTREAM_H + +#include +#include +#include + +//#define SERIAL_DEBUG +#include "firmataDebug.h" + +class EthernetServerStream : public Stream +{ + public: + EthernetServerStream(IPAddress localip, uint16_t port); + int available(); + int read(); + int peek(); + void flush(); + size_t write(uint8_t); + void maintain(IPAddress localip); + + private: + EthernetClient client; + IPAddress localip; + uint16_t port; + bool connected; + bool maintain(); + void stop(); + + protected: + EthernetServer server = EthernetServer(3030); + bool listening = false; + bool connect_client(); +}; + + +/* + * EthernetServerStream.cpp + * Copied here as a hack to linker issues with 3rd party board packages that don't properly + * implement the Arduino network APIs. + */ +EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port) + : localip(localip), + port(port), + connected(false) +{ +} + +bool EthernetServerStream::connect_client() + { + if ( connected ) + { + if ( client && client.connected() ) return true; + stop(); + } + + EthernetClient newClient = server.available(); + if ( !newClient ) return false; + client = newClient; + connected = true; + DEBUG_PRINTLN("Connected"); + return true; + } + +int +EthernetServerStream::available() +{ + return maintain() ? client.available() : 0; +} + +int +EthernetServerStream::read() +{ + return maintain() ? client.read() : -1; +} + +int +EthernetServerStream::peek() +{ + return maintain() ? client.peek() : -1; +} + +void EthernetServerStream::flush() +{ + if (maintain()) + client.flush(); +} + +size_t +EthernetServerStream::write(uint8_t c) +{ + return maintain() ? client.write(c) : 0; +} + +void +EthernetServerStream::maintain(IPAddress localip) +{ + // ensure the local IP is updated in the case that it is changed by the DHCP server + if (this->localip != localip) { + this->localip = localip; + if (connected) + stop(); + } +} + +void +EthernetServerStream::stop() +{ + if(client) + { + client.stop(); + } + connected = false; +} + +bool +EthernetServerStream::maintain() +{ + if (connect_client()) return true; + + stop(); + + if(!listening) + { + server = EthernetServer(port); + server.begin(); + listening = true; + } + return false; +} + +#endif /* ETHERNETSERVERSTREAM_H */ diff --git a/libraries/Firmata/utility/FirmataFeature.h b/libraries/Firmata/utility/FirmataFeature.h new file mode 100644 index 0000000..d5e229d --- /dev/null +++ b/libraries/Firmata/utility/FirmataFeature.h @@ -0,0 +1,38 @@ +/* + FirmataFeature.h + 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. + + Interface for Firmata feature classes. + + This version of FirmataFeature.h differs from the ConfigurableFirmata + version in the following ways: + + - Imports Firmata.h rather than ConfigurableFirmata.h + + See file LICENSE.txt for further informations on licensing terms. +*/ + +#ifndef FirmataFeature_h +#define FirmataFeature_h + +#include + +class FirmataFeature +{ + public: + virtual void handleCapability(byte pin) = 0; + virtual boolean handlePinMode(byte pin, int mode) = 0; + virtual boolean handleSysex(byte command, byte argc, byte* argv) = 0; + virtual void reset() = 0; +}; + +#endif diff --git a/libraries/Firmata/utility/SerialFirmata.cpp b/libraries/Firmata/utility/SerialFirmata.cpp new file mode 100644 index 0000000..8b703e1 --- /dev/null +++ b/libraries/Firmata/utility/SerialFirmata.cpp @@ -0,0 +1,342 @@ +/* + SerialFirmata.cpp + Copyright (C) 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. + + This version of SerialFirmata.cpp differs from the ConfigurableFirmata + version in the following ways: + + - handlePinMode calls Firmata::setPinMode + + Last updated October 16th, 2016 +*/ + +#include "SerialFirmata.h" + +SerialFirmata::SerialFirmata() +{ +#if defined(SoftwareSerial_h) + swSerial0 = NULL; + swSerial1 = NULL; + swSerial2 = NULL; + swSerial3 = NULL; +#endif + + serialIndex = -1; +} + +boolean SerialFirmata::handlePinMode(byte pin, int mode) +{ + // used for both HW and SW serial + if (mode == PIN_MODE_SERIAL) { + Firmata.setPinMode(pin, PIN_MODE_SERIAL); + return true; + } + return false; +} + +void SerialFirmata::handleCapability(byte pin) +{ + if (IS_PIN_SERIAL(pin)) { + Firmata.write(PIN_MODE_SERIAL); + Firmata.write(getSerialPinType(pin)); + } +} + +boolean SerialFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == SERIAL_MESSAGE) { + + Stream *serialPort; + byte mode = argv[0] & SERIAL_MODE_MASK; + byte portId = argv[0] & SERIAL_PORT_ID_MASK; + + switch (mode) { + case SERIAL_CONFIG: + { + long baud = (long)argv[1] | ((long)argv[2] << 7) | ((long)argv[3] << 14); + serial_pins pins; + + if (portId < 8) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + pins = getSerialPinNumbers(portId); + if (pins.rx != 0 && pins.tx != 0) { + Firmata.setPinMode(pins.rx, PIN_MODE_SERIAL); + Firmata.setPinMode(pins.tx, PIN_MODE_SERIAL); + // Fixes an issue where some serial devices would not work properly with Arduino Due + // because all Arduino pins are set to OUTPUT by default in StandardFirmata. + pinMode(pins.rx, INPUT); + } + ((HardwareSerial*)serialPort)->begin(baud); + } + } else { +#if defined(SoftwareSerial_h) + byte swTxPin, swRxPin; + if (argc > 4) { + swRxPin = argv[4]; + swTxPin = argv[5]; + } else { + // RX and TX pins must be specified when using SW serial + Firmata.sendString("Specify serial RX and TX pins"); + return false; + } + switch (portId) { + case SW_SERIAL0: + if (swSerial0 == NULL) { + swSerial0 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL1: + if (swSerial1 == NULL) { + swSerial1 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL2: + if (swSerial2 == NULL) { + swSerial2 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL3: + if (swSerial3 == NULL) { + swSerial3 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + } + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + Firmata.setPinMode(swRxPin, PIN_MODE_SERIAL); + Firmata.setPinMode(swTxPin, PIN_MODE_SERIAL); + ((SoftwareSerial*)serialPort)->begin(baud); + } +#endif + } + break; // SERIAL_CONFIG + } + case SERIAL_WRITE: + { + byte data; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + break; + } + for (byte i = 1; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + serialPort->write(data); + } + break; // SERIAL_WRITE + } + case SERIAL_READ: + if (argv[1] == SERIAL_READ_CONTINUOUSLY) { + if (serialIndex + 1 >= MAX_SERIAL_PORTS) { + break; + } + + if (argc > 2) { + // maximum number of bytes to read from buffer per iteration of loop() + serialBytesToRead[portId] = (int)argv[2] | ((int)argv[3] << 7); + } else { + // read all available bytes per iteration of loop() + serialBytesToRead[portId] = 0; + } + serialIndex++; + reportSerial[serialIndex] = portId; + } else if (argv[1] == SERIAL_STOP_READING) { + byte serialIndexToSkip = 0; + if (serialIndex <= 0) { + serialIndex = -1; + } else { + for (byte i = 0; i < serialIndex + 1; i++) { + if (reportSerial[i] == portId) { + serialIndexToSkip = i; + break; + } + } + // shift elements over to fill space left by removed element + for (byte i = serialIndexToSkip; i < serialIndex + 1; i++) { + if (i < MAX_SERIAL_PORTS) { + reportSerial[i] = reportSerial[i + 1]; + } + } + serialIndex--; + } + } + break; // SERIAL_READ + case SERIAL_CLOSE: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + if (portId < 8) { + ((HardwareSerial*)serialPort)->end(); + } else { +#if defined(SoftwareSerial_h) + ((SoftwareSerial*)serialPort)->end(); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } +#endif + } + } + break; // SERIAL_CLOSE + case SERIAL_FLUSH: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + getPortFromId(portId)->flush(); + } + break; // SERIAL_FLUSH +#if defined(SoftwareSerial_h) + case SERIAL_LISTEN: + // can only call listen() on software serial ports + if (portId > 7) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + ((SoftwareSerial*)serialPort)->listen(); + } + } + break; // SERIAL_LISTEN +#endif + } // end switch + return true; + } + return false; +} + +void SerialFirmata::update() +{ + checkSerial(); +} + +void SerialFirmata::reset() +{ +#if defined(SoftwareSerial_h) + Stream *serialPort; + // free memory allocated for SoftwareSerial ports + for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1; i++) { + serialPort = getPortFromId(i); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } + } +#endif + + serialIndex = -1; + for (byte i = 0; i < SERIAL_READ_ARR_LEN; i++) { + serialBytesToRead[i] = 0; + } +} + +// get a pointer to the serial port associated with the specified port id +Stream* SerialFirmata::getPortFromId(byte portId) +{ + switch (portId) { + case HW_SERIAL0: + // block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented + //return &Serial; + return NULL; +#if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + return &Serial1; +#endif +#if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + return &Serial2; +#endif +#if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + return &Serial3; +#endif +#if defined(PIN_SERIAL4_RX) + case HW_SERIAL4: + return &Serial4; +#endif +#if defined(PIN_SERIAL5_RX) + case HW_SERIAL5: + return &Serial5; +#endif +#if defined(PIN_SERIAL6_RX) + case HW_SERIAL6: + return &Serial6; +#endif +#if defined(SoftwareSerial_h) + case SW_SERIAL0: + if (swSerial0 != NULL) { + // instances of SoftwareSerial are already pointers so simply return the instance + return swSerial0; + } + break; + case SW_SERIAL1: + if (swSerial1 != NULL) { + return swSerial1; + } + break; + case SW_SERIAL2: + if (swSerial2 != NULL) { + return swSerial2; + } + break; + case SW_SERIAL3: + if (swSerial3 != NULL) { + return swSerial3; + } + break; +#endif + } + return NULL; +} + +// Check serial ports that have READ_CONTINUOUS mode set and relay any data +// for each port to the device attached to that port. +void SerialFirmata::checkSerial() +{ + byte portId, serialData; + int bytesToRead = 0; + int numBytesToRead = 0; + Stream* serialPort; + + if (serialIndex > -1) { + + // loop through all reporting (READ_CONTINUOUS) serial ports + for (byte i = 0; i < serialIndex + 1; i++) { + portId = reportSerial[i]; + bytesToRead = serialBytesToRead[portId]; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + continue; + } +#if defined(SoftwareSerial_h) + // only the SoftwareSerial port that is "listening" can read data + if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening()) { + continue; + } +#endif + if (serialPort->available() > 0) { + Firmata.write(START_SYSEX); + Firmata.write(SERIAL_MESSAGE); + Firmata.write(SERIAL_REPLY | portId); + + if (bytesToRead == 0 || (serialPort->available() <= bytesToRead)) { + numBytesToRead = serialPort->available(); + } else { + numBytesToRead = bytesToRead; + } + + // relay serial data to the serial device + while (numBytesToRead > 0) { + serialData = serialPort->read(); + Firmata.write(serialData & 0x7F); + Firmata.write((serialData >> 7) & 0x7F); + numBytesToRead--; + } + Firmata.write(END_SYSEX); + } + + } + } +} diff --git a/libraries/Firmata/utility/SerialFirmata.h b/libraries/Firmata/utility/SerialFirmata.h new file mode 100644 index 0000000..2319951 --- /dev/null +++ b/libraries/Firmata/utility/SerialFirmata.h @@ -0,0 +1,208 @@ +/* + SerialFirmata.h + Copyright (C) 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. + + This version of SerialFirmata.h differs from the ConfigurableFirmata + version in the following ways: + + - Defines FIRMATA_SERIAL_FEATURE (could add to Configurable version as well) + - Imports Firmata.h rather than ConfigurableFirmata.h + + Last updated October 16th, 2016 +*/ + +#ifndef SerialFirmata_h +#define SerialFirmata_h + +#include +#include "FirmataFeature.h" +// SoftwareSerial is currently only supported for AVR-based boards and the Arduino 101. +// Limited to Arduino 1.6.6 or higher because Arduino builder cannot find SoftwareSerial +// prior to this release. +#if (ARDUINO > 10605) && (defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ARC32)) +#include +#endif + +#define FIRMATA_SERIAL_FEATURE + +// Serial port Ids +#define HW_SERIAL0 0x00 +#define HW_SERIAL1 0x01 +#define HW_SERIAL2 0x02 +#define HW_SERIAL3 0x03 +#define HW_SERIAL4 0x04 +#define HW_SERIAL5 0x05 +#define HW_SERIAL6 0x06 +// extensible up to 0x07 + +#define SW_SERIAL0 0x08 +#define SW_SERIAL1 0x09 +#define SW_SERIAL2 0x0A +#define SW_SERIAL3 0x0B +// extensible up to 0x0F + +#define SERIAL_PORT_ID_MASK 0x0F +#define MAX_SERIAL_PORTS 8 +#define SERIAL_READ_ARR_LEN 12 + +// map configuration query response resolution value to serial pin type +#define RES_RX1 0x02 +#define RES_TX1 0x03 +#define RES_RX2 0x04 +#define RES_TX2 0x05 +#define RES_RX3 0x06 +#define RES_TX3 0x07 +#define RES_RX4 0x08 +#define RES_TX4 0x09 +#define RES_RX5 0x0a +#define RES_TX5 0x0b +#define RES_RX6 0x0c +#define RES_TX6 0x0d + +// Serial command bytes +#define SERIAL_CONFIG 0x10 +#define SERIAL_WRITE 0x20 +#define SERIAL_READ 0x30 +#define SERIAL_REPLY 0x40 +#define SERIAL_CLOSE 0x50 +#define SERIAL_FLUSH 0x60 +#define SERIAL_LISTEN 0x70 + +// Serial read modes +#define SERIAL_READ_CONTINUOUSLY 0x00 +#define SERIAL_STOP_READING 0x01 +#define SERIAL_MODE_MASK 0xF0 + +namespace { + + struct serial_pins { + uint8_t rx; + uint8_t tx; + }; + + /* + * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin. + */ + inline uint8_t getSerialPinType(uint8_t pin) { + #if defined(PIN_SERIAL_RX) + // TODO when use of HW_SERIAL0 is enabled + #endif + #if defined(PIN_SERIAL1_RX) + if (pin == PIN_SERIAL1_RX) return RES_RX1; + if (pin == PIN_SERIAL1_TX) return RES_TX1; + #endif + #if defined(PIN_SERIAL2_RX) + if (pin == PIN_SERIAL2_RX) return RES_RX2; + if (pin == PIN_SERIAL2_TX) return RES_TX2; + #endif + #if defined(PIN_SERIAL3_RX) + if (pin == PIN_SERIAL3_RX) return RES_RX3; + if (pin == PIN_SERIAL3_TX) return RES_TX3; + #endif + #if defined(PIN_SERIAL4_RX) + if (pin == PIN_SERIAL4_RX) return RES_RX4; + if (pin == PIN_SERIAL4_TX) return RES_TX4; + #endif + #if defined(PIN_SERIAL5_RX) + if (pin == PIN_SERIAL5_RX) return RES_RX5; + if (pin == PIN_SERIAL5_TX) return RES_TX5; + #endif + #if defined(PIN_SERIAL6_RX) + if (pin == PIN_SERIAL6_RX) return RES_RX6; + if (pin == PIN_SERIAL6_TX) return RES_TX6; + #endif + return 0; + } + + /* + * Get the RX and TX pins numbers for the specified HW serial port. + */ + inline serial_pins getSerialPinNumbers(uint8_t portId) { + serial_pins pins; + switch (portId) { + #if defined(PIN_SERIAL_RX) + // case HW_SERIAL0: + // // TODO when use of HW_SERIAL0 is enabled + // break; + #endif + #if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + pins.rx = PIN_SERIAL1_RX; + pins.tx = PIN_SERIAL1_TX; + break; + #endif + #if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + pins.rx = PIN_SERIAL2_RX; + pins.tx = PIN_SERIAL2_TX; + break; + #endif + #if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + pins.rx = PIN_SERIAL3_RX; + pins.tx = PIN_SERIAL3_TX; + break; + #endif + #if defined(PIN_SERIAL4_RX) + case HW_SERIAL4: + pins.rx = PIN_SERIAL4_RX; + pins.tx = PIN_SERIAL4_TX; + break; + #endif + #if defined(PIN_SERIAL5_RX) + case HW_SERIAL5: + pins.rx = PIN_SERIAL5_RX; + pins.tx = PIN_SERIAL5_TX; + break; + #endif + #if defined(PIN_SERIAL6_RX) + case HW_SERIAL6: + pins.rx = PIN_SERIAL6_RX; + pins.tx = PIN_SERIAL6_TX; + break; + #endif + default: + pins.rx = 0; + pins.tx = 0; + } + return pins; + } + +} // end namespace + + +class SerialFirmata: public FirmataFeature +{ + public: + SerialFirmata(); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void update(); + void reset(); + void checkSerial(); + + private: + byte reportSerial[MAX_SERIAL_PORTS]; + int serialBytesToRead[SERIAL_READ_ARR_LEN]; + signed char serialIndex; + +#if defined(SoftwareSerial_h) + Stream *swSerial0; + Stream *swSerial1; + Stream *swSerial2; + Stream *swSerial3; +#endif + + Stream* getPortFromId(byte portId); + +}; + +#endif /* SerialFirmata_h */ diff --git a/libraries/Firmata/utility/WiFiClientStream.h b/libraries/Firmata/utility/WiFiClientStream.h new file mode 100644 index 0000000..7fd30af --- /dev/null +++ b/libraries/Firmata/utility/WiFiClientStream.h @@ -0,0 +1,105 @@ +/* + WiFiClientStream.h + + An Arduino Stream that wraps an instance of a WiFiClient. For use + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2016 Jens B. 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. + + Parts of this class are based on + + - EthernetClientStream - Copyright (C) 2013 Norbert Truchsess. All rights reserved. + + published under the same license. + + Last updated April 23rd, 2016 + */ + +#ifndef WIFI_CLIENT_STREAM_H +#define WIFI_CLIENT_STREAM_H + +#include "WiFiStream.h" + +#define MILLIS_RECONNECT 5000 + +class WiFiClientStream : public WiFiStream +{ +protected: + uint32_t _time_connect = 0; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual inline bool connect_client() + { + if ( _connected ) + { + if ( _client && _client.connected() ) return true; + stop(); + } + + // active TCP connect + if ( WiFi.status() == WL_CONNECTED ) + { + // if the client is disconnected, try to reconnect every 5 seconds + if ( millis() - _time_connect >= MILLIS_RECONNECT ) + { + _connected = _client.connect( _remote_ip, _port ); + if ( !_connected ) + { + _time_connect = millis(); + } + else if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED); + } + } + } + + return _connected; + } + +public: + /** + * create a WiFi stream with a TCP client + */ + WiFiClientStream(IPAddress server_ip, uint16_t server_port) : WiFiStream(server_ip, server_port) {} + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual inline bool maintain() + { + return connect_client(); + } + + /** + * stop client connection + */ + virtual inline void stop() + { + if ( _client) + { + _client.stop(); + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED); + } + } + _connected = false; + _time_connect = millis(); + } + +}; + +#endif //WIFI_CLIENT_STREAM_H diff --git a/libraries/Firmata/utility/WiFiServerStream.h b/libraries/Firmata/utility/WiFiServerStream.h new file mode 100644 index 0000000..1404b05 --- /dev/null +++ b/libraries/Firmata/utility/WiFiServerStream.h @@ -0,0 +1,107 @@ +/* + WiFiServerStream.h + + An Arduino Stream extension for a WiFiClient or WiFiServer to be used + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2016 Jens B. 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. + + Parts of this class are based on + + - WiFiStream - Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + + published under the same license. + + Last updated April 23rd, 2016 + */ + +#ifndef WIFI_SERVER_STREAM_H +#define WIFI_SERVER_STREAM_H + +#include "WiFiStream.h" + +class WiFiServerStream : public WiFiStream +{ +protected: + WiFiServer _server = WiFiServer(3030); + bool _listening = false; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual inline bool connect_client() + { + if ( _connected ) + { + if ( _client && _client.connected() ) return true; + stop(); + } + + // passive TCP connect (accept) + WiFiClient newClient = _server.available(); + if ( !newClient ) return false; + _client = newClient; + _connected = true; + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_CONNECTED); + } + + return true; + } + +public: + /** + * create a WiFi stream with a TCP server + */ + WiFiServerStream(uint16_t server_port) : WiFiStream(server_port) {} + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual inline bool maintain() + { + if ( connect_client() ) return true; + + stop(); + + if ( !_listening && WiFi.status() == WL_CONNECTED ) + { + // start TCP server after first WiFi connect + _server = WiFiServer(_port); + _server.begin(); + _listening = true; + } + + return false; + } + + /** + * stop client connection + */ + virtual inline void stop() + { + if ( _client) + { + _client.stop(); + if ( _currentHostConnectionCallback ) + { + (*_currentHostConnectionCallback)(HOST_CONNECTION_DISCONNECTED); + } + } + _connected = false; + } + +}; + +#endif //WIFI_SERVER_STREAM_H diff --git a/libraries/Firmata/utility/WiFiStream.cpp b/libraries/Firmata/utility/WiFiStream.cpp new file mode 100644 index 0000000..9b54a5a --- /dev/null +++ b/libraries/Firmata/utility/WiFiStream.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which + * will cause linker errors whenever Firmata.h is included. + */ diff --git a/libraries/Firmata/utility/WiFiStream.h b/libraries/Firmata/utility/WiFiStream.h new file mode 100644 index 0000000..1ad44bb --- /dev/null +++ b/libraries/Firmata/utility/WiFiStream.h @@ -0,0 +1,226 @@ +/* + WiFiStream.h + + An Arduino Stream extension for a WiFiClient or WiFiServer to be used + with legacy Arduino WiFi shield and other boards and shields that + are compatible with the Arduino WiFi library. + + Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + Copyright (C) 2016 Jens B. 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 April 23rd, 2016 + */ + +#ifndef WIFI_STREAM_H +#define WIFI_STREAM_H + +#include +#include + +#define HOST_CONNECTION_DISCONNECTED 0 +#define HOST_CONNECTION_CONNECTED 1 + +extern "C" { + // callback function types + typedef void (*hostConnectionCallbackFunction)(byte); +} + +class WiFiStream : public Stream +{ +protected: + WiFiClient _client; + bool _connected = false; + hostConnectionCallbackFunction _currentHostConnectionCallback; + + //configuration members + IPAddress _local_ip; // DHCP + IPAddress _subnet; + IPAddress _gateway; + IPAddress _remote_ip; + uint16_t _port; + uint8_t _key_idx; //WEP + const char *_key = nullptr; //WEP + const char *_passphrase = nullptr; //WPA + char *_ssid = nullptr; + + /** + * check if TCP client is connected + * @return true if connected + */ + virtual bool connect_client() = 0; + +public: + /** constructor for TCP server */ + WiFiStream(uint16_t server_port) : _port(server_port) {} + + /** constructor for TCP client */ + WiFiStream(IPAddress server_ip, uint16_t server_port) : _remote_ip(server_ip), _port(server_port) {} + + inline void attach( hostConnectionCallbackFunction newFunction ) { _currentHostConnectionCallback = newFunction; } + +/****************************************************************************** + * network configuration + ******************************************************************************/ + +#ifndef ESP8266 + /** + * configure a static local IP address without defining the local network + * DHCP will be used as long as local IP address is not defined + */ + inline void config(IPAddress local_ip) + { + _local_ip = local_ip; + WiFi.config( local_ip ); + } +#endif + + /** + * configure a static local IP address + * DHCP will be used as long as local IP address is not defined + */ + inline void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) + { + _local_ip = local_ip; + _subnet = subnet; + _gateway = gateway; +#ifndef ESP8266 + WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet ); +#else + WiFi.config( local_ip, gateway, subnet ); +#endif + } + + /** + * @return local IP address + */ + inline IPAddress getLocalIP() + { + return WiFi.localIP(); + } + +/****************************************************************************** + * network functions + ******************************************************************************/ + + /** + * maintain WiFi and TCP connection + * @return true if WiFi and TCP connection are established + */ + virtual bool maintain() = 0; + +#ifdef ESP8266 + /** + * get status of TCP connection + * @return status of TCP connection + * CLOSED = 0 (typical) + * LISTEN = 1 (not used) + * SYN_SENT = 2 + * SYN_RCVD = 3 + * ESTABLISHED = 4 (typical) + * FIN_WAIT_1 = 5 + * FIN_WAIT_2 = 6 + * CLOSE_WAIT = 7 + * CLOSING = 8 + * LAST_ACK = 9 + * TIME_WAIT = 10 + */ + inline uint8_t status() + { + return _client.status(); + } +#endif + + /** + * close TCP client connection + */ + virtual void stop() = 0; + +/****************************************************************************** + * WiFi configuration + ******************************************************************************/ + + /** + * initialize WiFi without security (open) and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid) + { + _ssid = ssid; + + WiFi.begin(ssid); + int result = WiFi.status(); + return WiFi.status(); + } + +#ifndef ESP8266 + /** + * initialize WiFi with WEP security and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid, uint8_t key_idx, const char *key) + { + _ssid = ssid; + _key_idx = key_idx; + _key = key; + + WiFi.begin( ssid, key_idx, key ); + return WiFi.status(); + } +#endif + + /** + * initialize WiFi with WPA-PSK security and initiate client connection + * if WiFi connection is already established + * @return WL_CONNECTED if WiFi connection is established + */ + inline int begin(char *ssid, const char *passphrase) + { + _ssid = ssid; + _passphrase = passphrase; + + WiFi.begin(ssid, passphrase); + return WiFi.status(); + } + + +/****************************************************************************** + * stream functions + ******************************************************************************/ + + inline int available() + { + return connect_client() ? _client.available() : 0; + } + + inline void flush() + { + if( _client ) _client.flush(); + } + + inline int peek() + { + return connect_client() ? _client.peek(): 0; + } + + inline int read() + { + return connect_client() ? _client.read() : -1; + } + + inline size_t write(uint8_t byte) + { + return connect_client() ? _client.write( byte ) : 0; + } + +}; + +#endif //WIFI_STREAM_H diff --git a/libraries/Firmata/utility/firmataDebug.h b/libraries/Firmata/utility/firmataDebug.h new file mode 100644 index 0000000..dce0f80 --- /dev/null +++ b/libraries/Firmata/utility/firmataDebug.h @@ -0,0 +1,14 @@ +#ifndef FIRMATA_DEBUG_H +#define FIRMATA_DEBUG_H + +#ifdef SERIAL_DEBUG + #define DEBUG_BEGIN(baud) Serial.begin(baud); while(!Serial) {;} + #define DEBUG_PRINTLN(x) Serial.println (x); Serial.flush() + #define DEBUG_PRINT(x) Serial.print (x) +#else + #define DEBUG_BEGIN(baud) + #define DEBUG_PRINTLN(x) + #define DEBUG_PRINT(x) +#endif + +#endif /* FIRMATA_DEBUG_H */ diff --git a/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmata/ConfigurableFirmata.ino b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmata/ConfigurableFirmata.ino new file mode 100644 index 0000000..4df40d7 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmata/ConfigurableFirmata.ino @@ -0,0 +1,430 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. 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: January 23rd, 2016 +*/ + +/* + README + + This is an example use of ConfigurableFirmata. The easiest way to create a configuration is to + use http://firmatabuilder.com and select the communication transport and the firmata features + to include and an Arduino sketch (.ino) file will be generated and downloaded automatically. + + To manually configure a sketch, copy this file and follow the instructions in the + ETHERNET CONFIGURATION OPTION (if you want to use Ethernet instead of Serial/USB) and + FIRMATA FEATURE CONFIGURATION sections in this file. +*/ + +#include "ConfigurableFirmata.h" + +/*============================================================================== + * ETHERNET CONFIGURATION OPTION + * + * By default Firmata uses the Serial-port (over USB) of the Arduino. ConfigurableFirmata may also + * comunicate over ethernet using tcp/ip. To configure this sketch to use Ethernet instead of + * Serial, uncomment the approprate includes for your particular hardware. See STEPS 1 - 5 below. + * If you want to use Serial (over USB) then skip ahead to the FIRMATA FEATURE CONFIGURATION + * section further down in this file. + * + * If you enable Ethernet, you will need a Firmata client library with a network transport that can + * act as a server in order to establish a connection between ConfigurableFirmataEthernet and the + * Firmata host application (your application). + * + * To use ConfigurableFirmata with Ethernet you will need to have one of the following + * boards or shields: + * + * - Arduino Ethernet shield (or clone) + * - Arduino Ethernet board (or clone) + * - Arduino Yun + * + * If you are using an Arduino Ethernet shield you cannot use the following pins on + * the following boards. Firmata will ignore any requests to use these pins: + * + * - Arduino Uno or other ATMega328 boards: (D4, D10, D11, D12, D13) + * - Arduino Mega: (D4, D10, D50, D51, D52, D53) + * - Arduino Leonardo: (D4, D10) + * - Arduino Due: (D4, D10) + * - Arduino Zero: (D4, D10) + * + * If you are using an ArduinoEthernet board, the following pins cannot be used (same as Uno): + * - D4, D10, D11, D12, D13 + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) + +/* + * OPTION A: Configure for Arduino Ethernet board or Arduino Ethernet shield (or clone) + * + * To configure ConfigurableFirmata to use the an Arduino Ethernet Shield or Arduino Ethernet + * Board (both use the same WIZ5100-based Ethernet controller), uncomment the SPI and Ethernet + * includes below. + */ +//#include +//#include + + +/* + * OPTION B: Configure for a board or shield using an ENC28J60-based Ethernet controller, + * uncomment out the UIPEthernet include below. + * + * The UIPEthernet-library can be downloaded + * from: https://github.com/ntruchsess/arduino_uip + */ +//#include + + +/* + * OPTION C: Configure for Arduino Yun + * + * The Ethernet port on the Arduino Yun board can be used with Firmata in this configuration. + * To execute StandardFirmataEthernet on Yun uncomment the Bridge and YunClient includes below. + * + * NOTE: in order to compile for the Yun you will also need to comment out some of the includes + * and declarations in the FIRMATA FEATURE CONFIGURATION section later in this file. Including all + * features exceeds the RAM and Flash memory of the Yun. Comment out anything you don't need. + * + * On Yun there's no need to configure local_ip and mac address as this is automatically + * configured on the linux-side of Yun. + * + * Establishing a connection with the Yun may take several seconds. + */ +//#include +//#include + +#if defined ethernet_h || defined UIPETHERNET_H || defined _YUN_CLIENT_H_ +#define NETWORK_FIRMATA + +// STEP 2 [REQUIRED for all boards and shields] +// replace with IP of the server you want to connect to, comment out if using 'remote_host' +#define remote_ip IPAddress(192, 168, 0, 1) +// OR replace with hostname of server you want to connect to, comment out if using 'remote_ip' +// #define remote_host "server.local" + +// STEP 3 [REQUIRED unless using Arduino Yun] +// Replace with the port that your server is listening on +#define remote_port 3030 + +// STEP 4 [REQUIRED unless using Arduino Yun OR if not using DHCP] +// Replace with your board or Ethernet shield's IP address +// Comment out if you want to use DHCP +#define local_ip IPAddress(192, 168, 0, 6) + +// STEP 5 [REQUIRED unless using Arduino Yun] +// replace with Ethernet shield mac. Must be unique for your network +const byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x07, 0x02}; +#endif + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +#include +DigitalInputFirmata digitalInput; + +#include +DigitalOutputFirmata digitalOutput; + +#include +AnalogInputFirmata analogInput; + +#include +AnalogOutputFirmata analogOutput; + +#include +#include +ServoFirmata servo; +// ServoFirmata depends on AnalogOutputFirmata +#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +#error AnalogOutputFirmata must be included to use ServoFirmata +#endif + +#include +#include +I2CFirmata i2c; + +#include +OneWireFirmata oneWire; + +#include +StepperFirmata stepper; + +#include +SerialFirmata serial; + +#include +FirmataExt firmataExt; + +#include +FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + +// dependencies for Network Firmata. Do not comment out. +#ifdef NETWORK_FIRMATA +#if defined remote_ip && defined remote_host +#error "cannot define both remote_ip and remote_host at the same time!" +#endif +#include +#ifdef _YUN_CLIENT_H_ +YunClient client; +#else +EthernetClient client; +#endif +#if defined remote_ip && !defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, remote_ip, NULL, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, remote_port); +#endif +#endif +#if !defined remote_ip && defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, remote_port); +#endif +#endif +#endif + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + /* + * ETHERNET SETUP + */ +#ifdef NETWORK_FIRMATA +#ifdef _YUN_CLIENT_H_ + Bridge.begin(); +#else +#ifdef local_ip + Ethernet.begin((uint8_t *)mac, local_ip); //start Ethernet +#else + Ethernet.begin((uint8_t *)mac); //start Ethernet using dhcp +#endif +#endif + delay(1000); +#endif + + /* + * FIRMATA SETUP + */ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // Network Firmata communicates with Ethernet-shields over SPI. Therefor all + // SPI-pins must be set to PIN_MODE_IGNORE. Otherwise Firmata would break SPI-communication. + // add Pin 10 and configure pin 53 as output if using a MEGA with Ethernetshield. + // No need to ignore pin 10 on MEGA with ENC28J60, as here pin 53 should be connected to SS: +#ifdef NETWORK_FIRMATA + + #ifndef _YUN_CLIENT_H_ + // ignore SPI and pin 4 that is SS for SD-Card on Ethernet-shield + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_SPI(i) + || 4 == i // SD Card on Ethernet shield uses pin 4 for SS + || 10 == i // Ethernet-shield uses pin 10 for SS + ) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } + // pinMode(PIN_TO_DIGITAL(53), OUTPUT); configure hardware-SS as output on MEGA + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD-card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + #endif + + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA + #endif + + // start up Network Firmata: + Firmata.begin(stream); +#else + // Uncomment to save a couple of seconds by disabling the startup blink sequence. + // Firmata.disableBlinkVersion(); + + // start up the default Firmata using Serial interface: + Firmata.begin(57600); +#endif + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + + /* SEND STREAM WRITE BUFFER - TO DO: make sure that the stream buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + +#if defined NETWORK_FIRMATA && !defined local_ip &&!defined _YUN_CLIENT_H_ + // only necessary when using DHCP, ensures local IP is updated appropriately if it changes + if (Ethernet.maintain()) { + stream.maintain(Ethernet.localIP()); + } +#endif +} diff --git a/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/ConfigurableFirmataDeviceDriver.ino b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/ConfigurableFirmataDeviceDriver.ino new file mode 100644 index 0000000..5aeec8d --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/ConfigurableFirmataDeviceDriver.ino @@ -0,0 +1,445 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. 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: January 23rd, 2016 + + This ConfigurableFirmataDeviceDriver.ino has been edited to include the + DeviceFirmata feature. None of the other features are installed. + Doug Johnson, June 2016 +*/ + +/* + README + + This is an example use of ConfigurableFirmata. The easiest way to create a configuration is to + use http://firmatabuilder.com and select the communication transport and the firmata features + to include and an Arduino sketch (.ino) file will be generated and downloaded automatically. + + To manually configure a sketch, copy this file and follow the instructions in the + ETHERNET CONFIGURATION OPTION (if you want to use Ethernet instead of Serial/USB) and + FIRMATA FEATURE CONFIGURATION sections in this file. +*/ + +#include + +/*============================================================================== + * ETHERNET CONFIGURATION OPTION + * + * By default Firmata uses the Serial-port (over USB) of the Arduino. ConfigurableFirmata may also + * comunicate over ethernet using tcp/ip. To configure this sketch to use Ethernet instead of + * Serial, uncomment the approprate includes for your particular hardware. See STEPS 1 - 5 below. + * If you want to use Serial (over USB) then skip ahead to the FIRMATA FEATURE CONFIGURATION + * section further down in this file. + * + * If you enable Ethernet, you will need a Firmata client library with a network transport that can + * act as a server in order to establish a connection between ConfigurableFirmataEthernet and the + * Firmata host application (your application). + * + * To use ConfigurableFirmata with Ethernet you will need to have one of the following + * boards or shields: + * + * - Arduino Ethernet shield (or clone) + * - Arduino Ethernet board (or clone) + * - Arduino Yun + * + * If you are using an Arduino Ethernet shield you cannot use the following pins on + * the following boards. Firmata will ignore any requests to use these pins: + * + * - Arduino Uno or other ATMega328 boards: (D4, D10, D11, D12, D13) + * - Arduino Mega: (D4, D10, D50, D51, D52, D53) + * - Arduino Leonardo: (D4, D10) + * - Arduino Due: (D4, D10) + * - Arduino Zero: (D4, D10) + * + * If you are using an ArduinoEthernet board, the following pins cannot be used (same as Uno): + * - D4, D10, D11, D12, D13 + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) + +/* + * OPTION A: Configure for Arduino Ethernet board or Arduino Ethernet shield (or clone) + * + * To configure ConfigurableFirmata to use the an Arduino Ethernet Shield or Arduino Ethernet + * Board (both use the same WIZ5100-based Ethernet controller), uncomment the SPI and Ethernet + * includes below. + */ +//#include +//#include + + +/* + * OPTION B: Configure for a board or shield using an ENC28J60-based Ethernet controller, + * uncomment out the UIPEthernet include below. + * + * The UIPEthernet-library can be downloaded + * from: https://github.com/ntruchsess/arduino_uip + */ +//#include + + +/* + * OPTION C: Configure for Arduino Yun + * + * The Ethernet port on the Arduino Yun board can be used with Firmata in this configuration. + * To execute StandardFirmataEthernet on Yun uncomment the Bridge and YunClient includes below. + * + * NOTE: in order to compile for the Yun you will also need to comment out some of the includes + * and declarations in the FIRMATA FEATURE CONFIGURATION section later in this file. Including all + * features exceeds the RAM and Flash memory of the Yun. Comment out anything you don't need. + * + * On Yun there's no need to configure local_ip and mac address as this is automatically + * configured on the linux-side of Yun. + * + * Establishing a connection with the Yun may take several seconds. + */ +//#include +//#include + +#if defined ethernet_h || defined UIPETHERNET_H || defined _YUN_CLIENT_H_ +#define NETWORK_FIRMATA + +// STEP 2 [REQUIRED for all boards and shields] +// replace with IP of the server you want to connect to, comment out if using 'remote_host' +#define remote_ip IPAddress(192, 168, 0, 1) +// OR replace with hostname of server you want to connect to, comment out if using 'remote_ip' +// #define remote_host "server.local" + +// STEP 3 [REQUIRED unless using Arduino Yun] +// Replace with the port that your server is listening on +#define remote_port 3030 + +// STEP 4 [REQUIRED unless using Arduino Yun OR if not using DHCP] +// Replace with your board or Ethernet shield's IP address +// Comment out if you want to use DHCP +#define local_ip IPAddress(192, 168, 0, 6) + +// STEP 5 [REQUIRED unless using Arduino Yun] +// replace with Ethernet shield mac. Must be unique for your network +const byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x07, 0x02}; +#endif + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +//#include +//DigitalInputFirmata digitalInput; + +//#include +//DigitalOutputFirmata digitalOutput; + +//#include +//AnalogInputFirmata analogInput; + +//#include +//AnalogOutputFirmata analogOutput; + +//#include +//#include +//ServoFirmata servo; +//// ServoFirmata depends on AnalogOutputFirmata +//#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +//#error AnalogOutputFirmata must be included to use ServoFirmata +//#endif +// +//#include +//#include +//I2CFirmata i2c; + +//#include +//OneWireFirmata oneWire; + +//#include +//StepperFirmata stepper; + +//#include +//SerialFirmata serial; + +#include +FirmataExt firmataExt; + +//#include +//FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +#include "SelectedDeviceDrivers.h" +#include +DeviceFirmata deviceMgr; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + +// dependencies for Network Firmata. Do not comment out. +#ifdef NETWORK_FIRMATA +#if defined remote_ip && defined remote_host +#error "cannot define both remote_ip and remote_host at the same time!" +#endif +#include +#ifdef _YUN_CLIENT_H_ +YunClient client; +#else +EthernetClient client; +#endif +#if defined remote_ip && !defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, remote_ip, NULL, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, remote_port); +#endif +#endif +#if !defined remote_ip && defined remote_host +#ifdef local_ip +EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, remote_port); +#else +EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, remote_port); +#endif +#endif +#endif + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + /* + * ETHERNET SETUP + */ +#ifdef NETWORK_FIRMATA +#ifdef _YUN_CLIENT_H_ + Bridge.begin(); +#else +#ifdef local_ip + Ethernet.begin((uint8_t *)mac, local_ip); //start Ethernet +#else + Ethernet.begin((uint8_t *)mac); //start Ethernet using dhcp +#endif +#endif + delay(1000); +#endif + + /* + * FIRMATA SETUP + */ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#ifdef DeviceFirmata_h + firmataExt.addFeature(deviceMgr); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // Network Firmata communicates with Ethernet-shields over SPI. Therefor all + // SPI-pins must be set to PIN_MODE_IGNORE. Otherwise Firmata would break SPI-communication. + // add Pin 10 and configure pin 53 as output if using a MEGA with Ethernetshield. + // No need to ignore pin 10 on MEGA with ENC28J60, as here pin 53 should be connected to SS: +#ifdef NETWORK_FIRMATA + + #ifndef _YUN_CLIENT_H_ + // ignore SPI and pin 4 that is SS for SD-Card on Ethernet-shield + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_SPI(i) + || 4 == i // SD Card on Ethernet shield uses pin 4 for SS + || 10 == i // Ethernet-shield uses pin 10 for SS + ) { + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } + // pinMode(PIN_TO_DIGITAL(53), OUTPUT); configure hardware-SS as output on MEGA + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD-card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + #endif + + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA + #endif + + // start up Network Firmata: + Firmata.begin(stream); +#else + // Uncomment to save a couple of seconds by disabling the startup blink sequence. + // Firmata.disableBlinkVersion(); + + // start up the default Firmata using Serial interface: + Firmata.begin(57600); +#endif + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + + /* SEND STREAM WRITE BUFFER - TO DO: make sure that the stream buffer doesn't go over + * 60 bytes. use a timer to sending an event character every 4 ms to + * trigger the buffer to dump. */ + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + +#ifdef DeviceFirmata_h + deviceMgr.update(); +#endif + +#if defined NETWORK_FIRMATA && !defined local_ip &&!defined _YUN_CLIENT_H_ + // only necessary when using DHCP, ensures local IP is updated appropriately if it changes + if (Ethernet.maintain()) { + stream.maintain(Ethernet.localIP()); + } +#endif +} diff --git a/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/SelectedDeviceDrivers.h b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/SelectedDeviceDrivers.h new file mode 100644 index 0000000..dc8cd50 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataDeviceDriver/SelectedDeviceDrivers.h @@ -0,0 +1,22 @@ +#include + +// Device Drivers + +//#include +#include +//#include +//#include + +//#include +//#include + +DeviceDriver *selectedDevices[] = { + // new DDMeta("Meta",1), + new DDHello("Hello",1), +// new DDMCP9808("MCP9808",1,0x18), +// new DDServo("Servo",2), + +// new DDSensor("Chan",16), +// new DDStepper("Stepper",6), + 0}; + diff --git a/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino new file mode 100644 index 0000000..cff209e --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/examples/ConfigurableFirmataWiFi/ConfigurableFirmataWiFi.ino @@ -0,0 +1,609 @@ +/* + 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 clink on the following link + to open the download page in your default browser. + + https://github.com/firmata/ConfigurableFirmata#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) 2013 Norbert Truchsess. All rights reserved. + Copyright (C) 2014 Nicolas Panel. All rights reserved. + Copyright (C) 2015-2016 Jesse Frush. 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: January 23rd, 2016 +*/ + +/* + README + + This is an example use of ConfigurableFirmata with WiFi. The easiest way to create a + configuration is use http://firmatabuilder.com and select the communication transport and the + firmata features to include and an Arduino sketch (.ino) file will be generated and downloaded + automatically. + + To manually configure a sketch, follow the instructions in this file. + + ConfigurableFirmataWiFi is a WiFi server application. You will need a Firmata client library with + a network transport in order to establish a connection with ConfigurableFirmataWiFi. + + To use ConfigurableFirmataWiFi you will need to have one of the following + boards or shields: + + - Arduino WiFi Shield (or clone) + - Arduino WiFi Shield 101 + - Arduino MKR1000 board (built-in WiFi 101) + - Adafruit HUZZAH CC3000 WiFi Shield (support coming soon) + + Follow the instructions in the WIFI CONFIGURATION section below to configure your particular + hardware. + + Dependencies: + - WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino + 1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source: + https://github.com/arduino-libraries/WiFi101) + + In order to use the WiFi Shield 101 with Firmata you will need a board with at least + 35k of Flash memory. This means you cannot use the WiFi Shield 101 with an Arduino Uno + or any other ATmega328p-based microcontroller or with an Arduino Leonardo or other + ATmega32u4-based microcontroller. Some boards that will work are: + + - Arduino Zero + - Arduino Due + - Arduino 101 + - Arduino Mega + + NOTE: If you are using an Arduino WiFi (legacy) shield you cannot use the following pins on + the following boards. Firmata will ignore any requests to use these pins: + + - Arduino Uno or other ATMega328 boards: (D4, D7, D10, D11, D12, D13) + - Arduino Mega: (D4, D7, D10, D50, D51, D52, D53) + - Arduino Due, Zero or Leonardo: (D4, D7, D10) + + If you are using an Arduino WiFi 101 shield you cannot use the following pins on the following + boards: + + - Arduino Due or Zero: (D5, D7, D10) + - Arduino Mega: (D5, D7, D10, D50, D52, D53) +*/ + +#include "ConfigurableFirmata.h" + +/* + * Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your + * connection that may help in the event of connection issues. If defined, some boards may not begin + * executing this sketch until the Serial console is opened. + */ +//#define SERIAL_DEBUG +#include "utility/firmataDebug.h" + +#define WIFI_MAX_CONN_ATTEMPTS 3 + +/*============================================================================== + * WIFI CONFIGURATION + * + * You must configure your particular hardware. Follow the steps below. + * + * Currently ConfigurableFirmataWiFi is configured as a server. An option to + * configure as a client may be added in the future. + *============================================================================*/ + +// STEP 1 [REQUIRED] +// Uncomment / comment the appropriate set of includes for your hardware (OPTION A, B or C) +// Option A is enabled by default. + +/* + * OPTION A: Configure for Arduino WiFi shield + * + * This will configure ConfigurableFirmataWiFi to use the original WiFi library (deprecated) + * provided with the Arduino IDE. It is supported by the Arduino WiFi shield (a discontinued + * product) and is compatible with 802.11 B/G networks. + * + * To configure ConfigurableFirmataWiFi to use the Arduino WiFi shield + * leave the #define below uncommented. + */ +#define ARDUINO_WIFI_SHIELD + +//do not modify these next 4 lines +#ifdef ARDUINO_WIFI_SHIELD +#include "utility/WiFiStream.h" +WiFiStream stream; +#endif + +/* + * OPTION B: Configure for WiFi 101 + * + * This will configure ConfigurableFirmataWiFi to use the WiFi101 library, which works with the + * Arduino WiFi101 shield and devices that have the WiFi101 chip built in (such as the MKR1000). It + * is compatible with 802.11 B/G/N networks. + * + * To enable, uncomment the #define WIFI_101 below and verify the #define values under + * options A and C are commented out. + * + * IMPORTANT: You must have the WiFI 101 library installed. To easily install this library, open + * the library manager via: Arduino IDE Menus: Sketch > Include Library > Manage Libraries > + * filter search for "WiFi101" > Select the result and click 'install' + */ +//#define WIFI_101 + +//do not modify these next 4 lines +#ifdef WIFI_101 +#include "utility/WiFi101Stream.h" +WiFi101Stream stream; +#endif + +/* + * OPTION C: Configure for HUZZAH + * + * HUZZAH is not yet supported, this will be added in a later revision to ConfigurableFirmataWiFi + */ + +//------------------------------ +// TODO +//------------------------------ +//#define HUZZAH_WIFI + + +// STEP 2 [REQUIRED for all boards and shields] +// replace this with your wireless network SSID +char ssid[] = "your_network_name"; + +// STEP 3 [OPTIONAL for all boards and shields] +// If you want to use a static IP (v4) address, uncomment the line below. You can also change the IP. +// If this line is commented out, the WiFi shield will attempt to get an IP from the DHCP server +// #define STATIC_IP_ADDRESS 192,168,1,113 + +// STEP 4 [REQUIRED for all boards and shields] +// define your port number here, you will need this to open a TCP connection to your Arduino +#define SERVER_PORT 3030 + +// STEP 5 [REQUIRED for all boards and shields] +// determine your network security type (OPTION A, B, or C). Option A is the most common, and the default. + + +/* + * OPTION A: WPA / WPA2 + * + * WPA is the most common network security type. A passphrase is required to connect to this type. + * + * To enable, leave #define WIFI_WPA_SECURITY uncommented below, set your wpa_passphrase value + * appropriately, and do not uncomment the #define values under options B and C + */ +#define WIFI_WPA_SECURITY + +#ifdef WIFI_WPA_SECURITY +char wpa_passphrase[] = "your_wpa_passphrase"; +#endif //WIFI_WPA_SECURITY + +/* + * OPTION B: WEP + * + * WEP is a less common (and regarded as less safe) security type. A WEP key and its associated + * index are required to connect to this type. + * + * To enable, Uncomment the #define below, set your wep_index and wep_key values appropriately, and + * verify the #define values under options A and C are commented out. + */ +//#define WIFI_WEP_SECURITY + +#ifdef WIFI_WEP_SECURITY +//The wep_index below is a zero-indexed value. +//Valid indices are [0-3], even if your router/gateway numbers your keys [1-4]. +byte wep_index = 0; +char wep_key[] = "your_wep_key"; +#endif //WIFI_WEP_SECURITY + + +/* + * OPTION C: Open network (no security) + * + * Open networks have no security, can be connected to by any device that knows the ssid, and are + * unsafe. + * + * To enable, uncomment #define WIFI_NO_SECURITY below and verify the #define values + * under options A and B are commented out. + */ +//#define WIFI_NO_SECURITY + +/*============================================================================== + * CONFIGURATION ERROR CHECK (don't change anything here) + *============================================================================*/ + +#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI))) +#error "you may not define more than one wifi device type in wifiConfig.h." +#endif //WIFI device type check + +#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI)) +#error "you must define a wifi device type in wifiConfig.h." +#endif + +#if ((defined(WIFI_NO_SECURITY) && (defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY))) || (defined(WIFI_WEP_SECURITY) && defined(WIFI_WPA_SECURITY))) +#error "you may not define more than one security type at the same time in wifiConfig.h." +#endif //WIFI_* security define check + +#if !(defined(WIFI_NO_SECURITY) || defined(WIFI_WEP_SECURITY) || defined(WIFI_WPA_SECURITY)) +#error "you must define a wifi security type in wifiConfig.h." +#endif //WIFI_* security define check + +/*============================================================================== + * PIN IGNORE MACROS (don't change anything here) + *============================================================================*/ + +// ignore SPI pins, pin 5 (reset WiFi101 shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +// also don't ignore SS pin if it's not pin 10 +// TODO - need to differentiate between Arduino WiFi1 101 Shield and Arduino MKR1000 +#define IS_IGNORE_WIFI101_SHIELD(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7) + +// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) +#define IS_IGNORE_WIFI_SHIELD(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10) + + +/*============================================================================== + * FIRMATA FEATURE CONFIGURATION + * + * Comment out the include and declaration for any features that you do not need + * below. + * + * WARNING: Including all of the following features (especially if also using + * Ethernet) may exceed the Flash and/or RAM of lower memory boards such as the + * Arduino Uno or Leonardo. + *============================================================================*/ + +#include +DigitalInputFirmata digitalInput; + +#include +DigitalOutputFirmata digitalOutput; + +#include +AnalogInputFirmata analogInput; + +#include +AnalogOutputFirmata analogOutput; + +#include +#include +ServoFirmata servo; +// ServoFirmata depends on AnalogOutputFirmata +#if defined ServoFirmata_h && ! defined AnalogOutputFirmata_h +#error AnalogOutputFirmata must be included to use ServoFirmata +#endif + +#include +#include +I2CFirmata i2c; + +#include +OneWireFirmata oneWire; + +#include +StepperFirmata stepper; + +#include +SerialFirmata serial; + +#include +FirmataExt firmataExt; + +#include +FirmataScheduler scheduler; + +// To add Encoder support you must first install the FirmataEncoder and Encoder libraries: +// https://github.com/firmata/FirmataEncoder +// https://www.pjrc.com/teensy/td_libs_Encoder.html +// #include +// #include +// FirmataEncoder encoder; + +/*=================================================================================== + * END FEATURE CONFIGURATION - you should not need to change anything below this line + *==================================================================================*/ + +// dependencies. Do not comment out the following lines +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h +#include +#endif + +#if defined AnalogInputFirmata_h || defined I2CFirmata_h || defined FirmataEncoder_h +#include +FirmataReporting reporting; +#endif + + +#ifdef STATIC_IP_ADDRESS +IPAddress local_ip(STATIC_IP_ADDRESS); +#endif + +int wifiConnectionAttemptCounter = 0; +int wifiStatus = WL_IDLE_STATUS; + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a default state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i = 0; i < TOTAL_PINS; i++) { + if (IS_PIN_ANALOG(i)) { +#ifdef AnalogInputFirmata_h + // turns off pull-up, configures everything + Firmata.setPinMode(i, PIN_MODE_ANALOG); +#endif + } else if (IS_PIN_DIGITAL(i)) { +#ifdef DigitalOutputFirmata_h + // sets the output to 0, configures portConfigInputs + Firmata.setPinMode(i, OUTPUT); +#endif + } + } + +#ifdef FirmataExt_h + firmataExt.reset(); +#endif +} + +void printWifiStatus() { +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + if ( WiFi.status() != WL_CONNECTED ) + { + DEBUG_PRINT( "WiFi connection failed. Status value: " ); + DEBUG_PRINTLN( WiFi.status() ); + } + else +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + { + // print the SSID of the network you're attached to: + DEBUG_PRINT( "SSID: " ); + +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + DEBUG_PRINTLN( WiFi.SSID() ); +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + + // print your WiFi shield's IP address: + DEBUG_PRINT( "IP Address: " ); + +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + IPAddress ip = WiFi.localIP(); + DEBUG_PRINTLN( ip ); +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + + // print the received signal strength: + DEBUG_PRINT( "signal strength (RSSI): " ); + +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + long rssi = WiFi.RSSI(); + DEBUG_PRINT( rssi ); +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) + + DEBUG_PRINTLN( " dBm" ); + } +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + /* + * WIFI SETUP + */ + DEBUG_BEGIN(9600); + + /* + * This statement will clarify how a connection is being made + */ + DEBUG_PRINT( "ConfigurableFirmataWiFi will attempt a WiFi connection " ); +#if defined(WIFI_101) + DEBUG_PRINTLN( "using the WiFi 101 library." ); +#elif defined(ARDUINO_WIFI_SHIELD) + DEBUG_PRINTLN( "using the legacy WiFi library." ); +#elif defined(HUZZAH_WIFI) + DEBUG_PRINTLN( "using the HUZZAH WiFi library." ); + //else should never happen here as error-checking in wifiConfig.h will catch this +#endif //defined(WIFI_101) + + /* + * Configure WiFi IP Address + */ +#ifdef STATIC_IP_ADDRESS + DEBUG_PRINT( "Using static IP: " ); + DEBUG_PRINTLN( local_ip ); + //you can also provide a static IP in the begin() functions, but this simplifies + //ifdef logic in this sketch due to support for all different encryption types. + stream.config( local_ip ); +#else + DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); +#endif + + /* + * Configure WiFi security + */ +#if defined(WIFI_WEP_SECURITY) + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINT( "Attempting to connect to WEP SSID: " ); + DEBUG_PRINTLN(ssid); + wifiStatus = stream.begin( ssid, wep_index, wep_key, SERVER_PORT ); + delay(5000); // TODO - determine minimum delay + if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break; + } + +#elif defined(WIFI_WPA_SECURITY) + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINT( "Attempting to connect to WPA SSID: " ); + DEBUG_PRINTLN(ssid); + wifiStatus = stream.begin(ssid, wpa_passphrase, SERVER_PORT); + delay(5000); // TODO - determine minimum delay + if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break; + } + +#else //OPEN network + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINTLN( "Attempting to connect to open SSID: " ); + DEBUG_PRINTLN(ssid); + wifiStatus = stream.begin(ssid, SERVER_PORT); + delay(5000); // TODO - determine minimum delay + if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break; + } +#endif //defined(WIFI_WEP_SECURITY) + + DEBUG_PRINTLN( "WiFi setup done" ); + printWifiStatus(); + + /* + * FIRMATA SETUP + */ + Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION); + +#ifdef FirmataExt_h +#ifdef DigitalInputFirmata_h + firmataExt.addFeature(digitalInput); +#endif +#ifdef DigitalOutputFirmata_h + firmataExt.addFeature(digitalOutput); +#endif +#ifdef AnalogInputFirmata_h + firmataExt.addFeature(analogInput); +#endif +#ifdef AnalogOutputFirmata_h + firmataExt.addFeature(analogOutput); +#endif +#ifdef ServoFirmata_h + firmataExt.addFeature(servo); +#endif +#ifdef I2CFirmata_h + firmataExt.addFeature(i2c); +#endif +#ifdef OneWireFirmata_h + firmataExt.addFeature(oneWire); +#endif +#ifdef StepperFirmata_h + firmataExt.addFeature(stepper); +#endif +#ifdef SerialFirmata_h + firmataExt.addFeature(serial); +#endif +#ifdef FirmataReporting_h + firmataExt.addFeature(reporting); +#endif +#ifdef FirmataScheduler_h + firmataExt.addFeature(scheduler); +#endif +#ifdef FirmataEncoder_h + firmataExt.addFeature(encoder); +#endif +#endif + /* systemResetCallback is declared here (in ConfigurableFirmata.ino) */ + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // ConfigurableFirmataWiFi communicates with WiFi shields over SPI. Therefore all + // SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication. + // Additional pins may also need to be ignored depending on the particular board or + // shield in use. + + for (byte i = 0; i < TOTAL_PINS; i++) { +#if defined(ARDUINO_WIFI_SHIELD) + if (IS_IGNORE_WIFI_SHIELD(i) + #if defined(__AVR_ATmega32U4__) + || 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10 + || 28 == i + #endif //defined(__AVR_ATmega32U4__) + ) { +#elif defined (WIFI_101) + if (IS_IGNORE_WIFI101_SHIELD(i)) { +#elif defined (HUZZAH_WIFI) + // TODO + if (false) { +#else + if (false) { +#endif + Firmata.setPinMode(i, PIN_MODE_IGNORE); + } + } + + //Set up controls for the Arduino WiFi Shield SS for the SD Card +#ifdef ARDUINO_WIFI_SHIELD + // Arduino WiFi, Arduino WiFi Shield and Arduino Yun all have SD SS wired to D4 + pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata + digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low; + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA +#endif //defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + +#endif //ARDUINO_WIFI_SHIELD + + // start up Network Firmata: + Firmata.begin(stream); + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ +#ifdef DigitalInputFirmata_h + /* DIGITALREAD - as fast as possible, check for changes and output them to the + * stream buffer using Firmata.write() */ + digitalInput.report(); +#endif + + /* STREAMREAD - processing incoming message as soon as possible, while still + * checking digital inputs. */ + while (Firmata.available()) { + Firmata.processInput(); +#ifdef FirmataScheduler_h + if (!Firmata.isParsingMessage()) { + goto runtasks; + } + } + if (!Firmata.isParsingMessage()) { +runtasks: scheduler.runTasks(); +#endif + } + + // TODO - ensure that Stream buffer doesn't go over 60 bytes + +#ifdef FirmataReporting_h + if (reporting.elapsed()) { +#ifdef AnalogInputFirmata_h + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + analogInput.report(); +#endif +#ifdef I2CFirmata_h + // report i2c data for all device with read continuous mode enabled + i2c.report(); +#endif +#ifdef FirmataEncoder_h + // report encoders positions if reporting enabled. + encoder.report(); +#endif + } +#endif +#ifdef StepperFirmata_h + stepper.update(); +#endif +#ifdef SerialFirmata_h + serial.update(); +#endif + + // keep the WiFi connection live. Attempts to reconnect automatically if disconnected. + stream.maintain(); +} diff --git a/libraries/FirmataWithDeviceFeature/extras/LICENSE.txt b/libraries/FirmataWithDeviceFeature/extras/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/extras/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/libraries/FirmataWithDeviceFeature/extras/v0.9-release-notes.md b/libraries/FirmataWithDeviceFeature/extras/v0.9-release-notes.md new file mode 100644 index 0000000..a947270 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/extras/v0.9-release-notes.md @@ -0,0 +1,48 @@ +##FirmataWithDeviceFeature + +###A fork of Configurable Firmata with the addition of the DeviceFirmata feature, enabling the use of device driver modules as defined by LuniLib. + +####Summary + +ConfigurableFirmata is a Firmata implementation that splits the various capabilities of Firmata into separate +classes that can be included or excluded when an Arduino program is compiled and linked in order to tailor the +code to the needs of the application and (hopefully) save memory. This FirmataWithDeviceFeature is a fork of +ConfigurableFirmata and includes an additional feature that recognizes and manages DEVICE\_QUERY and DEVICE\_RESPONSE +Sysex messages. + +The stable release repositories for the LuniLib related packages are available on github.com from user finson-release. + +####Release v0.9, July 2016 + +This library is one of several concurrent v0.9 releases. + +- The Arduino library LuniLib (Arduino Device Driver framework and a few example drivers) +- FirmataWithDeviceFeature, a fork of ConfigurableFirmata that includes the DeviceFirmata pull request +- LuniJS (Javascript NodeJS client package) + +Changes to FirmataWithDeviceFeature for v0.9 include: + +1. Minor changes to facilitate use of the Arduino library manager: version number, library.properties +2. Additions and updates to the documentation: release notes. + +Dependencies + +- LuniLib by Doug Johnson at https://github.com/finson-release/luni +- base64 by Adam Rudd at https://github.com/adamvr/arduino-base64 + +####Release v0.8, May 2016 + +First release for beta testing. + +This library is part of several concurrent v0.8 releases: + +- This Arduino library LuniLib +- Update to Configurable Firmata (Arduino Firmata host connection to remote clients) +- LuniJS (Javascript NodeJS client package) +- LuniFive (Javascript Johnny-Five client controller examples) [Update. No, didn't happen, see LuniJS for a Johnny-Five example.] + +There are initially three ways to use this library. + +1. Standalone LuniLib. All code is on the Arduino and there is no external control of the various device drivers. There are examples included in the library. Only the LuniLib itself is needed for this configuration. +2. NodeJS and LuniLib. The device driver library (and a device driver or two), along with an updated Configurable Firmata are on the Arduino, and NodeJS is running on a client. Required packages are Arduino LuniLib, updated Configurable Firmata, and lunijs, an addon to firmata.js. +3. Johnny-Five and LuniLib.The device driver library (and a device driver or two), along with an updated Configurable Firmata are on the Arduino, and Johnny-Five is running on a client. All four packages listed above are needed in this configuration. diff --git a/libraries/FirmataWithDeviceFeature/keywords.txt b/libraries/FirmataWithDeviceFeature/keywords.txt new file mode 100644 index 0000000..d713ff1 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/keywords.txt @@ -0,0 +1,96 @@ +####################################### +# Syntax Coloring Map For Firmata +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Firmata KEYWORD1 Firmata +callbackFunction KEYWORD1 callbackFunction +systemResetCallbackFunction KEYWORD1 systemResetCallbackFunction +stringCallbackFunction KEYWORD1 stringCallbackFunction +sysexCallbackFunction KEYWORD1 sysexCallbackFunction + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +printVersion KEYWORD2 +blinkVersion KEYWORD2 +printFirmwareVersion KEYWORD2 +setFirmwareVersion KEYWORD2 +setFirmwareNameAndVersion KEYWORD2 +available KEYWORD2 +processInput KEYWORD2 +isParsingMessage KEYWORD2 +parse KEYWORD2 +sendAnalog KEYWORD2 +sendDigital KEYWORD2 +sendDigitalPort KEYWORD2 +sendString KEYWORD2 +sendSysex KEYWORD2 +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +sendValueAsTwo7bitBytes KEYWORD2 +startSysex KEYWORD2 +endSysex KEYWORD2 +attachDelayTask KEYWORD2 +delayTask KEYWORD2 +getPinMode KEYWORD2 +setPinMode KEYWORD2 +getPinState KEYWORD2 +setPinState KEYWORD2 +writePort KEYWORD2 +readPort KEYWORD2 +disableBlinkVersion KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + +FIRMATA_PROTOCOL_MAJOR_VERSION LITERAL1 +FIRMATA_PROTOCOL_MINOR_VERSION LITERAL1 +FIRMATA_PROTOCOL_BUGFIX_VERSION LITERAL1 + +FIRMATA_FIRMWARE_MAJOR_VERSION LITERAL1 +FIRMATA_FIRMWARE_MINOR_VERSION LITERAL1 +FIRMATA_FIRMWARE_BUGFIX_VERSION LITERAL1 + +MAX_DATA_BYTES LITERAL1 + +DIGITAL_MESSAGE LITERAL1 +ANALOG_MESSAGE LITERAL1 +REPORT_ANALOG LITERAL1 +REPORT_DIGITAL LITERAL1 +REPORT_VERSION LITERAL1 +SET_PIN_MODE LITERAL1 +SET_DIGITAL_PIN_VALUE LITERAL1 +SYSTEM_RESET LITERAL1 +START_SYSEX LITERAL1 +END_SYSEX LITERAL1 +REPORT_FIRMWARE LITERAL1 +STRING_DATA LITERAL1 + +PIN_MODE_ANALOG LITERAL1 +PIN_MODE_PWM LITERAL1 +PIN_MODE_SERVO LITERAL1 +PIN_MODE_SHIFT LITERAL1 +PIN_MODE_I2C LITERAL1 +PIN_MODE_ONEWIRE LITERAL1 +PIN_MODE_STEPPER LITERAL1 +PIN_MODE_ENCODER LITERAL1 +PIN_MODE_SERIAL LITERAL1 +PIN_MODE_PULLUP LITERAL1 +PIN_MODE_IGNORE LITERAL1 + +TOTAL_PINS LITERAL1 +TOTAL_ANALOG_PINS LITERAL1 +TOTAL_DIGITAL_PINS LITERAL1 +TOTAL_PIN_MODES LITERAL1 +TOTAL_PORTS LITERAL1 +ANALOG_PORT LITERAL1 +MAX_SERVOS LITERAL1 diff --git a/libraries/FirmataWithDeviceFeature/library.properties b/libraries/FirmataWithDeviceFeature/library.properties new file mode 100644 index 0000000..f2fa315 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/library.properties @@ -0,0 +1,9 @@ +name=FirmataWithDeviceFeature +version=2.9.4 +author=Firmata Developers, Doug Johnson +maintainer=Doug Johnson +sentence=This library implements the Firmata protocol as a set of plugins that can be used to create applications to remotely interface with an Arduino board. +paragraph=FirmataWithDeviceFeature is a fork of ConfigurableFirmata 2.8.2 that adds a feature to support DeviceDrivers on the Arduino. +category=Device Control +url=https://github.com/finson-release/FirmataWithDeviceFeature +architectures=* diff --git a/libraries/FirmataWithDeviceFeature/readme.md b/libraries/FirmataWithDeviceFeature/readme.md new file mode 100644 index 0000000..2d33d6c --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/readme.md @@ -0,0 +1,74 @@ +#ConfigurableFirmata + +[![Join the chat at https://gitter.im/firmata/ConfigurableFirmata](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/firmata/ConfigurableFirmata?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +Firmata is a protocol for communicating with microcontrollers from software on a host computer. The [protocol](https://github.com/firmata/protocol) can be implemented in firmware on any microcontroller architecture as well as software on any host computer software package. The arduino repository described here is a Firmata library for Arduino and Arduino-compatible devices. If you would like to contribute to Firmata, please see the [Contributing](#contributing) section below. + +##Installation + +- **If you are using Arduino IDE version 1.6.4 or higher** go to `Sketch > Include Library > Manage Libraries` and then search for "ConfigurableFirmata" and click on `Install` after tapping on the ConfigurableFirmata item in the filtered results. You can also use this same method to update ConfigurableFirmata in the future. +- **If you are using an older version of the Arduino IDE**, download or clone ConfigurableFirmata to your Arduino sketchbook library folder. This is typically `/Documents/Arduino/libraries/` on Mac or Linux or `\My Documents\Arduino\libraries\` on Windows. + +##Usage + +ConfigurableFirmata is a version of Firmata that breaks features such as Digital Input, Digital Output, Analog Input, Analog Output, I2C, etc into [individual classes](https://github.com/firmata/ConfigurableFirmata/tree/master/src) making it easier to mix and match standard features with custom features. + +The easiest way to use ConfigurableFirmata is with [firmatabuilder](http://firmatabuilder.com) which is a simple web application that generates an Arduino sketch based on a selection of Firmata features. Download the generated sketch, compile and upload it to your board. + +Another way to use ConfigurableFirmata is by adding or removing various include statements in the [ConfigurableFirmata.ino](https://github.com/firmata/ConfigurableFirmata/blob/master/examples/ConfigurableFirmata/ConfigurableFirmata.ino) example file. + +##Firmata Wrapper Libraries + +You can use the ConfigurableFirmata architecture to wrap 3rd party libraries to include +functionality not included in the base ConfigurableFirmata.ino example. See [FirmataEncoder](https://github.com/firmata/FirmataEncoder) for an example of a Firmata wrapper. To include a Firmata wrapper your +ino file, you must install both the sketch and the 3rd party library into your `/Arduino/libraries/` +directory (where all 3rd party libraries are installed). + +When creating a new Firmata wrapper library, you generally should not include the 3rd party +library it wraps. For example, the Encoder library that FirmataEncoder wraps is not included with +the FirmataEncoder library. + +If you create a wrapper library, prepend the name with 'Firmata'. Hence 'FirmataEncoder' in the +referenced example. This will keep the wrapper libraries together in the user's Arduino libraries +directory. + +A Firmata wrapper template library will be published soon along with instructions for creating +a wrapper library. + +##Firmata Client Libraries +Only a few Firmata client libraries currently support ConfigurableFirmata: + +* javascript + * [https://github.com/jgautier/firmata] + * [https://github.com/rwldrn/johnny-five] + * [http://breakoutjs.com] +* perl + * [https://github.com/ntruchsess/perl-firmata] + +*Additional Firmata client libraries may work as well. If you're a client library developer and have verified that you library works with ConfigurableFirmata, please [open an issue](https://github.com/firmata/ConfigurableFirmata/issues) with a request to add the link.* + +
+##Contributing + +If you discover a bug or would like to propose a new feature, please open a new [issue](https://github.com/firmata/ConfigurableFirmata/issues?sort=created&state=open). + +To contribute, fork this repository and create a new topic branch for the bug, feature or other existing issue you are addressing. Submit the pull request against the *master* branch. + +You must thoroughly test your contributed code. In your pull request, describe tests performed to ensure that no existing code is broken and that any changes maintain backwards compatibility with the existing api. Test on multiple Arduino board variants if possible. We hope to enable some form of automated (or at least semi-automated) testing in the future, but for now any tests will need to be executed manually by the contributor and reviewers. + +Use [Artistic Style](http://astyle.sourceforge.net/) (astyle) to format your code. Set the following rules for the astyle formatter: + +``` +style = "" +indent = "spaces" +indent-spaces = 2 +indent-classes = true +indent-switches = true +indent-cases = true +indent-col1-comments = true +pad-oper = true +pad-header = true +keep-one-line-statements = true +``` + +If you happen to use Sublime Text, [this astyle plugin](https://github.com/timonwong/SublimeAStyleFormatter) is helpful. Set the above rules in the user settings file. diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.cpp new file mode 100644 index 0000000..f2f3fe7 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.cpp @@ -0,0 +1,32 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#include +#include "AnalogFirmata.h" + +boolean handleAnalogFirmataSysex(byte command, byte argc, byte* argv) +{ + if (command == ANALOG_MAPPING_QUERY) { + Firmata.write(START_SYSEX); + Firmata.write(ANALOG_MAPPING_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127); + } + Firmata.write(END_SYSEX); + return true; + } + return false; +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.h new file mode 100644 index 0000000..69ae0fd --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogFirmata.h @@ -0,0 +1,24 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogFirmata_h +#define AnalogFirmata_h + +#include + +boolean handleAnalogFirmataSysex(byte command, byte argc, byte* argv); + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.cpp new file mode 100644 index 0000000..4ac2377 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.cpp @@ -0,0 +1,109 @@ +/* + AnalogFirmata.h - 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-2015 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: November 22nd, 2015 +*/ + +#include +#include "AnalogFirmata.h" +#include "AnalogInputFirmata.h" + +AnalogInputFirmata *AnalogInputFirmataInstance; + +void reportAnalogInputCallback(byte analogPin, int value) +{ + AnalogInputFirmataInstance->reportAnalog(analogPin, value); +} + +AnalogInputFirmata::AnalogInputFirmata() +{ + AnalogInputFirmataInstance = this; + analogInputsToReport = 0; + Firmata.attach(REPORT_ANALOG, reportAnalogInputCallback); +} + +// ----------------------------------------------------------------------------- +/* sets bits in a bit array (int) to toggle the reporting of the analogIns + */ +//void FirmataClass::setAnalogPinReporting(byte pin, byte state) { +//} +void AnalogInputFirmata::reportAnalog(byte analogPin, int value) +{ + 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 (!Firmata.isResetting()) { + // Send pin value immediately. This is helpful when connected via + // ethernet, wi-fi or bluetooth so pin states can be known upon + // reconnecting. + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } + // TODO: save status to EEPROM here, if changed +} + +boolean AnalogInputFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_ANALOG(pin)) { + if (mode == PIN_MODE_ANALOG) { + reportAnalog(PIN_TO_ANALOG(pin), 1); // turn on reporting + if (IS_PIN_DIGITAL(pin)) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver + } + return true; + } else { + reportAnalog(PIN_TO_ANALOG(pin), 0); // turn off reporting + } + } + return false; +} + +void AnalogInputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_ANALOG(pin)) { + Firmata.write(PIN_MODE_ANALOG); + Firmata.write(10); // 10 = 10-bit resolution + } +} + +boolean AnalogInputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + return handleAnalogFirmataSysex(command, argc, argv); +} + +void AnalogInputFirmata::reset() +{ + // by default, do not report any analog inputs + analogInputsToReport = 0; +} + +void AnalogInputFirmata::report() +{ + byte pin, analogPin; + /* ANALOGREAD - do all analogReads() at the configured sampling interval */ + for (pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) { + analogPin = PIN_TO_ANALOG(pin); + if (analogInputsToReport & (1 << analogPin)) { + Firmata.sendAnalog(analogPin, analogRead(analogPin)); + } + } + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.h new file mode 100644 index 0000000..3c2f1bf --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogInputFirmata.h @@ -0,0 +1,42 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogInputFirmata_h +#define AnalogInputFirmata_h + +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +void reportAnalogInputCallback(byte analogPin, int value); + +class AnalogInputFirmata: public FirmataFeature +{ + public: + AnalogInputFirmata(); + void reportAnalog(byte analogPin, int value); + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + void report(); + + private: + /* analog inputs */ + int analogInputsToReport; // bitwise array to store pin reporting +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.cpp new file mode 100644 index 0000000..29051df --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.cpp @@ -0,0 +1,65 @@ +/* + AnalogFirmata.h - 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-2015 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: November 22nd, 2015 +*/ + +#include +#include "AnalogFirmata.h" +#include "AnalogOutputFirmata.h" + +AnalogOutputFirmata::AnalogOutputFirmata() +{ + Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); +} + +void AnalogOutputFirmata::reset() +{ + +} + +boolean AnalogOutputFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_PWM && IS_PIN_PWM(pin)) { + pinMode(PIN_TO_PWM(pin), OUTPUT); + analogWrite(PIN_TO_PWM(pin), 0); + return true; + } + return false; +} + +void AnalogOutputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_PWM(pin)) { + Firmata.write(PIN_MODE_PWM); + Firmata.write(8); // 8 = 8-bit resolution + } +} + +boolean AnalogOutputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == 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); + return true; + } + return false; + } else { + return handleAnalogFirmataSysex(command, argc, argv); + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.h new file mode 100644 index 0000000..cf1761e --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogOutputFirmata.h @@ -0,0 +1,38 @@ +/* + AnalogFirmata.h - 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-2015 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. +*/ + +#ifndef AnalogOutputFirmata_h +#define AnalogOutputFirmata_h + +#include +#include "FirmataFeature.h" + +// analogWriteCallback is defined in AnalogWrite.h but must also be declared here in order +// for AnalogOutputFirmata to compile +void analogWriteCallback(byte pin, int value); + +class AnalogOutputFirmata: public FirmataFeature +{ + public: + AnalogOutputFirmata(); + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + private: +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/AnalogWrite.h b/libraries/FirmataWithDeviceFeature/src-features/AnalogWrite.h new file mode 100644 index 0000000..f8b4edd --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/AnalogWrite.h @@ -0,0 +1,51 @@ +/* + AnalogWrite.h - 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-2015 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: November 15th, 2015 +*/ + +#ifndef AnalogWrite_h +#define AnalogWrite_h + +#include + +#if defined AnalogOutputFirmata_h || defined ServoFirmata_h + +void analogWriteCallback(byte pin, int value) +{ + if (pin < TOTAL_PINS) { + switch (Firmata.getPinMode(pin)) { +#ifdef ServoFirmata_h + case PIN_MODE_SERVO: + if (IS_PIN_SERVO(pin)) { + servoAnalogWrite(pin, value); + Firmata.setPinState(pin, value); + } + break; +#endif +#ifdef AnalogOutputFirmata_h + case PIN_MODE_PWM: + if (IS_PIN_PWM(pin)) { + analogWrite(PIN_TO_PWM(pin), value); + Firmata.setPinState(pin, value); + } + break; +#endif + } + } +} + +#endif +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.cpp new file mode 100644 index 0000000..565066d --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.cpp @@ -0,0 +1,127 @@ +/* + DigitalInputFirmata.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-2015 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: November 15th, 2015 +*/ + +#include +#include "DigitalInputFirmata.h" + +DigitalInputFirmata *DigitalInputFirmataInstance; + +void reportDigitalInputCallback(byte port, int value) +{ + DigitalInputFirmataInstance->reportDigital(port, value); +} + +DigitalInputFirmata::DigitalInputFirmata() +{ + DigitalInputFirmataInstance = this; + Firmata.attach(REPORT_DIGITAL, reportDigitalInputCallback); +} + +boolean DigitalInputFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + return false; +} + +void DigitalInputFirmata::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) { + Firmata.sendDigitalPort(portNumber, portValue); + previousPINs[portNumber] = portValue; + } +} + +/* ----------------------------------------------------------------------------- + * check all the active digital inputs for change of state, then add any events + * to the Serial output queue using Serial.print() */ +void DigitalInputFirmata::report(void) +{ + /* 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]), false); + if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false); + if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false); + if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false); + if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false); + if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false); + if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false); + if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false); + if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false); + if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false); + if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false); + if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false); + if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false); + if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false); + if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false); + if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false); +} + +void DigitalInputFirmata::reportDigital(byte port, int value) +{ + if (port < TOTAL_PORTS) { + reportPINs[port] = (byte)value; + 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 +} + +boolean DigitalInputFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_DIGITAL(pin)) { + if (mode == INPUT || mode == PIN_MODE_PULLUP) { + portConfigInputs[pin / 8] |= (1 << (pin & 7)); + if (mode == INPUT) { + pinMode(PIN_TO_DIGITAL(pin), INPUT); + } else { + pinMode(PIN_TO_DIGITAL(pin), INPUT_PULLUP); + Firmata.setPinState(pin, 1); + } + return true; + } else { + portConfigInputs[pin / 8] &= ~(1 << (pin & 7)); + } + } + return false; +} + +void DigitalInputFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write((byte)INPUT); + Firmata.write(1); + Firmata.write((byte)PIN_MODE_PULLUP); + Firmata.write(1); + } +} + +void DigitalInputFirmata::reset() +{ + for (byte i = 0; i < TOTAL_PORTS; i++) { + reportPINs[i] = false; // by default, reporting off + portConfigInputs[i] = 0; // until activated + previousPINs[i] = 0; + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.h new file mode 100644 index 0000000..2c896b1 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/DigitalInputFirmata.h @@ -0,0 +1,46 @@ +/* + DigitalInputFirmata.h - 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-2015 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. +*/ + +#ifndef DigitalInputFirmata_h +#define DigitalInputFirmata_h + +#include +#include "FirmataFeature.h" + +void reportDigitalInputCallback(byte port, int value); + +class DigitalInputFirmata: public FirmataFeature +{ + public: + DigitalInputFirmata(); + void reportDigital(byte port, int value); + void report(void); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + boolean handlePinMode(byte pin, int mode); + void reset(); + + private: + /* digital input ports */ + byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence + byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent + + /* pins configuration */ + byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else + void outputPort(byte portNumber, byte portValue, byte forceSend); +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.cpp new file mode 100644 index 0000000..a4448f0 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.cpp @@ -0,0 +1,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 +#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) == 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) == 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) { + 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 == 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)OUTPUT); + Firmata.write(1); + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.h new file mode 100644 index 0000000..dcd7979 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/DigitalOutputFirmata.h @@ -0,0 +1,40 @@ +/* + DigitalOutputFirmata.h - 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-2015 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: November 15th, 2015 +*/ + +#ifndef DigitalOutputFirmata_h +#define DigitalOutputFirmata_h + +#include +#include "FirmataFeature.h" + +void digitalOutputWriteCallback(byte port, int value); +void handleSetPinValueCallback(byte pin, int value); + +class DigitalOutputFirmata: public FirmataFeature +{ + public: + DigitalOutputFirmata(); + void digitalWritePort(byte port, int value); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + boolean handlePinMode(byte pin, int mode); + void reset(); + private: +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.cpp b/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.cpp new file mode 100644 index 0000000..c11c33c --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.cpp @@ -0,0 +1,64 @@ +/* + Encoder7Bit.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#include "Encoder7Bit.h" +#include + +Encoder7BitClass::Encoder7BitClass() +{ + previous = 0; + shift = 0; +} + +void Encoder7BitClass::startBinaryWrite() +{ + shift = 0; +} + +void Encoder7BitClass::endBinaryWrite() +{ + if (shift > 0) { + Firmata.write(previous); + } +} + +void Encoder7BitClass::writeBinary(byte data) +{ + if (shift == 0) { + Firmata.write(data & 0x7f); + shift++; + previous = data >> 7; + } + else { + Firmata.write(((data << shift) & 0x7f) | previous); + if (shift == 6) { + Firmata.write(data >> 1); + shift = 0; + } + else { + shift++; + previous = data >> (8 - shift); + } + } +} + +void Encoder7BitClass::readBinary(int outBytes, byte *inData, byte *outData) +{ + for (int i = 0; i < outBytes; i++) { + int j = i << 3; + int pos = j / 7; + byte shift = j % 7; + outData[i] = (inData[pos] >> shift) | ((inData[pos + 1] << (7 - shift)) & 0xFF); + } +} + +Encoder7BitClass Encoder7Bit; diff --git a/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.h b/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.h new file mode 100644 index 0000000..e3538b2 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/Encoder7Bit.h @@ -0,0 +1,35 @@ +/* + Encoder7Bit.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#ifndef Encoder7Bit_h +#define Encoder7Bit_h +#include + +#define num7BitOutbytes(a)(((a)*7)>>3) + +class Encoder7BitClass +{ + public: + Encoder7BitClass(); + void startBinaryWrite(); + void endBinaryWrite(); + void writeBinary(byte data); + void readBinary(int outBytes, byte *inData, byte *outData); + + private: + byte previous; + int shift; +}; + +extern Encoder7BitClass Encoder7Bit; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.cpp b/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.cpp new file mode 100644 index 0000000..375dd49 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.cpp @@ -0,0 +1,103 @@ +/* + EthernetClientStream.cpp + An Arduino-Stream that wraps an instance of Client reconnecting to + the remote-ip in a transparent way. A disconnected client may be + recognized by the returnvalues -1 from calls to peek or read and + a 0 from calls to write. + + Copyright (C) 2013 Norbert Truchsess. 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. + */ + +#include "EthernetClientStream.h" +#include + +#define MILLIS_RECONNECT 5000 + +EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port) +: client(client), + localip(localip), + ip(ip), + host(host), + port(port), + connected(false) +{ +} + +int +EthernetClientStream::available() +{ + return maintain() ? client.available() : 0; +} + +int +EthernetClientStream::read() +{ + return maintain() ? client.read() : -1; +} + +int +EthernetClientStream::peek() +{ + return maintain() ? client.peek() : -1; +} + +void EthernetClientStream::flush() +{ + if (maintain()) + client.flush(); +} + +size_t +EthernetClientStream::write(uint8_t c) +{ + return maintain() ? client.write(c) : 0; +} + +void +EthernetClientStream::maintain(IPAddress localip) +{ +// temporary hack to Firmata to compile for Intel Galileo +// the issue is documented here: https://github.com/firmata/arduino/issues/218 +#if !defined(ARDUINO_LINUX) + if (this->localip!=localip) + { + this->localip = localip; + if (connected) + stop(); + } +#endif +} + +void +EthernetClientStream::stop() +{ + client.stop(); + connected = false; + time_connect = millis(); +} + +bool +EthernetClientStream::maintain() +{ + if (client && client.connected()) + return true; + + if (connected) + { + stop(); + } + else if (millis() - time_connect >= MILLIS_RECONNECT) + { + connected = host ? client.connect(host, port) : client.connect(ip, port); + if (!connected) + time_connect = millis(); + } + return connected; +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.h b/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.h new file mode 100644 index 0000000..3d6dcf3 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/EthernetClientStream.h @@ -0,0 +1,50 @@ +/* + EthernetClientStream.h + An Arduino-Stream that wraps an instance of Client reconnecting to + the remote-ip in a transparent way. A disconnected client may be + recognized by the returnvalues -1 from calls to peek or read and + a 0 from calls to write. + + Copyright (C) 2013 Norbert Truchsess. 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. + */ + +#ifndef ETHERNETCLIENTSTREAM_H +#define ETHERNETCLIENTSTREAM_H + +#include +#include +#include +#include +#include + +class EthernetClientStream : public Stream +{ +public: + EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port); + int available(); + int read(); + int peek(); + void flush(); + size_t write(uint8_t); + void maintain(IPAddress localip); + +private: + Client &client; + IPAddress localip; + IPAddress ip; + const char* host; + uint16_t port; + bool connected; + uint32_t time_connect; + bool maintain(); + void stop(); +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.cpp b/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.cpp new file mode 100644 index 0000000..b1705f7 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.cpp @@ -0,0 +1,66 @@ +/* + FirmataReporting.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. +*/ + +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +void FirmataReporting::setSamplingInterval(int interval) +{ + samplingInterval = interval; +} + +void FirmataReporting::handleCapability(byte pin) +{ + +} + +boolean FirmataReporting::handlePinMode(byte pin, int mode) +{ + return false; +} + +boolean FirmataReporting::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SAMPLING_INTERVAL) { + if (argc > 1) { + samplingInterval = argv[0] + (argv[1] << 7); + if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) { + samplingInterval = MINIMUM_SAMPLING_INTERVAL; + } + return true; + } + } + return false; +} + +boolean FirmataReporting::elapsed() +{ + currentMillis = millis(); + if (currentMillis - previousMillis > samplingInterval) { + previousMillis += samplingInterval; + if (currentMillis - previousMillis > samplingInterval) + previousMillis = currentMillis - samplingInterval; + return true; + } + return false; +} + +void FirmataReporting::reset() +{ + previousMillis = millis(); + samplingInterval = 19; +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.h b/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.h new file mode 100644 index 0000000..88105e6 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/FirmataReporting.h @@ -0,0 +1,43 @@ +/* + FirmataReporting.h - 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: January 23rd, 2016 +*/ + +#ifndef FirmataReporting_h +#define FirmataReporting_h + +#include +#include "FirmataFeature.h" + +#define MINIMUM_SAMPLING_INTERVAL 1 + +class FirmataReporting: public FirmataFeature +{ + public: + void setSamplingInterval(int interval); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); //empty method + boolean handleSysex(byte command, byte argc, byte* argv); + boolean elapsed(); + void reset(); + private: + /* timer variables */ + unsigned long currentMillis; // store the current value from millis() + unsigned long previousMillis; // for comparison with currentMillis + unsigned int samplingInterval; // how often to run the main loop (in ms) +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.cpp b/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.cpp new file mode 100644 index 0000000..32a9bfb --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.cpp @@ -0,0 +1,300 @@ +/* + FirmataScheduler.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#include +#include "FirmataFeature.h" +#include "Encoder7Bit.h" +#include "FirmataScheduler.h" +#include "FirmataExt.h" + +FirmataScheduler *FirmataSchedulerInstance; + +void delayTaskCallback(long delay) +{ + FirmataSchedulerInstance->delayTask(delay); +} + +FirmataScheduler::FirmataScheduler() +{ + FirmataSchedulerInstance = this; + tasks = NULL; + running = NULL; + Firmata.attachDelayTask(delayTaskCallback); +} + +void FirmataScheduler::handleCapability(byte pin) +{ + +} + +boolean FirmataScheduler::handlePinMode(byte pin, int mode) +{ + return false; +} + +boolean FirmataScheduler::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SCHEDULER_DATA) { + if (argc > 0) { + switch (argv[0]) { + case CREATE_FIRMATA_TASK: + { + if (argc == 4) { + createTask(argv[1], argv[2] | argv[3] << 7); + } + break; + } + case DELETE_FIRMATA_TASK: + { + if (argc == 2) { + deleteTask(argv[1]); + } + break; + } + case ADD_TO_FIRMATA_TASK: + { + if (argc > 2) { + int len = num7BitOutbytes(argc - 2); + Encoder7Bit.readBinary(len, argv + 2, argv + 2); //decode inplace + addToTask(argv[1], len, argv + 2); //addToTask copies data... + } + break; + } + case DELAY_FIRMATA_TASK: + { + if (argc == 6) { + argv++; + Encoder7Bit.readBinary(4, argv, argv); //decode inplace + delayTask(*(long*)((byte*)argv)); + } + break; + } + case SCHEDULE_FIRMATA_TASK: + { + if (argc == 7) { //one byte taskid, 5 bytes to encode 4 bytes of long + Encoder7Bit.readBinary(4, argv + 2, argv + 2); //decode inplace + schedule(argv[1], *(long*)((byte*)argv + 2)); //argv[1] | argv[2]<<8 | argv[3]<<16 | argv[4]<<24 + } + break; + } + case QUERY_ALL_FIRMATA_TASKS: + { + queryAllTasks(); + break; + } + case QUERY_FIRMATA_TASK: + { + if (argc == 2) { + queryTask(argv[1]); + } + break; + } + case RESET_FIRMATA_TASKS: + { + reset(); + } + } + } + return true; + } + return false; +}; + +void FirmataScheduler::createTask(byte id, int len) +{ + firmata_task *existing = findTask(id); + if (existing) { + reportTask(id, existing, true); + } + else { + firmata_task *newTask = (firmata_task*)malloc(sizeof(firmata_task) + len); + newTask->id = id; + newTask->time_ms = 0; + newTask->len = len; + newTask->nextTask = tasks; + newTask->pos = 0; + tasks = newTask; + } +}; + +void FirmataScheduler::deleteTask(byte id) +{ + firmata_task *current = tasks; + firmata_task *previous = NULL; + while (current) { + if (current->id == id) { + if (previous) { + previous->nextTask = current->nextTask; + } + else { + tasks = current->nextTask; + } + free (current); + return; + } + else { + previous = current; + current = current->nextTask; + } + } +}; + +void FirmataScheduler::addToTask(byte id, int additionalBytes, byte *message) +{ + firmata_task *existing = findTask(id); + if (existing) { //task exists and has not been fully loaded yet + if (existing->pos + additionalBytes <= existing->len) { + for (int i = 0; i < additionalBytes; i++) { + existing->messages[existing->pos++] = message[i]; + } + } + } + else { + reportTask(id, NULL, true); + } +}; + +void FirmataScheduler::schedule(byte id, long delay_ms) +{ + firmata_task *existing = findTask(id); + if (existing) { + existing->pos = 0; + existing->time_ms = millis() + delay_ms; + } + else { + reportTask(id, NULL, true); + } +}; + +void FirmataScheduler::delayTask(long delay_ms) +{ + if (running) { + long now = millis(); + running->time_ms += delay_ms; + if (running->time_ms < now) { //if delay time allready passed by schedule to 'now'. + running->time_ms = now; + } + } +} + +void FirmataScheduler::queryAllTasks() +{ + Firmata.write(START_SYSEX); + Firmata.write(SCHEDULER_DATA); + Firmata.write(QUERY_ALL_TASKS_REPLY); + firmata_task *task = tasks; + while (task) { + Firmata.write(task->id); + task = task->nextTask; + } + Firmata.write(END_SYSEX); +}; + +void FirmataScheduler::queryTask(byte id) +{ + firmata_task *task = findTask(id); + reportTask(id, task, false); +} + +void FirmataScheduler::reportTask(byte id, firmata_task *task, boolean error) +{ + Firmata.write(START_SYSEX); + Firmata.write(SCHEDULER_DATA); + if (error) { + Firmata.write(ERROR_TASK_REPLY); + } else { + Firmata.write(QUERY_TASK_REPLY); + } + Firmata.write(id); + if (task) { + Encoder7Bit.startBinaryWrite(); + for (int i = 3; i < firmata_task_len(task); i++) { + Encoder7Bit.writeBinary(((byte *)task)[i]); //don't write first 3 bytes (firmata_task*, byte); makes use of AVR byteorder (LSB first) + } + Encoder7Bit.endBinaryWrite(); + } + Firmata.write(END_SYSEX); +}; + +void FirmataScheduler::runTasks() +{ + if (tasks) { + long now = millis(); + firmata_task *current = tasks; + firmata_task *previous = NULL; + while (current) { + if (current->time_ms > 0 && current->time_ms < now) { // TODO handle overflow + if (execute(current)) { + previous = current; + current = current->nextTask; + } + else { + if (previous) { + previous->nextTask = current->nextTask; + free(current); + current = previous->nextTask; + } + else { + tasks = current->nextTask; + free(current); + current = tasks; + } + } + } + else { + current = current->nextTask; + } + } + } +}; + +void FirmataScheduler::reset() +{ + while (tasks) { + firmata_task *nextTask = tasks->nextTask; + free(tasks); + tasks = nextTask; + } +}; + +//private +boolean FirmataScheduler::execute(firmata_task *task) +{ + long start = task->time_ms; + int pos = task->pos; + int len = task->len; + byte *messages = task->messages; + running = task; + while (pos < len) { + Firmata.parse(messages[pos++]); + if (start != task->time_ms) { // return true if task got rescheduled during run. + task->pos = ( pos == len ? 0 : pos ); // last message executed? -> start over next time + running = NULL; + return true; + } + }; + running = NULL; + return false; +} + +firmata_task *FirmataScheduler::findTask(byte id) +{ + firmata_task *currentTask = tasks; + while (currentTask) { + if (id == currentTask->id) { + return currentTask; + } else { + currentTask = currentTask->nextTask; + } + }; + return NULL; +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.h b/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.h new file mode 100644 index 0000000..bf6bd70 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/FirmataScheduler.h @@ -0,0 +1,73 @@ +/* + FirmataScheduler.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. 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. +*/ + +#ifndef FirmataScheduler_h +#define FirmataScheduler_h + +#include +#include "FirmataFeature.h" +#include "Encoder7Bit.h" + +//subcommands +#define CREATE_FIRMATA_TASK 0 +#define DELETE_FIRMATA_TASK 1 +#define ADD_TO_FIRMATA_TASK 2 +#define DELAY_FIRMATA_TASK 3 +#define SCHEDULE_FIRMATA_TASK 4 +#define QUERY_ALL_FIRMATA_TASKS 5 +#define QUERY_FIRMATA_TASK 6 +#define RESET_FIRMATA_TASKS 7 +#define ERROR_TASK_REPLY 8 +#define QUERY_ALL_TASKS_REPLY 9 +#define QUERY_TASK_REPLY 10 + +#define firmata_task_len(a)(sizeof(firmata_task)+(a)->len) + +void delayTaskCallback(long delay); + +struct firmata_task +{ + firmata_task *nextTask; + byte id; //only 7bits used -> supports 127 tasks + long time_ms; + int len; + int pos; + byte messages[]; +}; + +class FirmataScheduler: public FirmataFeature +{ + public: + FirmataScheduler(); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); //empty method + boolean handleSysex(byte command, byte argc, byte* argv); + void runTasks(); + void reset(); + void createTask(byte id, int len); + void deleteTask(byte id); + void addToTask(byte id, int len, byte *message); + void schedule(byte id, long time_ms); + void delayTask(long time_ms); + void queryAllTasks(); + void queryTask(byte id); + + private: + firmata_task *tasks; + firmata_task *running; + + boolean execute(firmata_task *task); + firmata_task *findTask(byte id); + void reportTask(byte id, firmata_task *task, boolean error); +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.cpp new file mode 100644 index 0000000..c74ccc3 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in I2CFirmata.h to avoid having to include Wire.h in all + * sketch files that include ConfigurableFirmata.h + */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.h new file mode 100644 index 0000000..e837a7b --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/I2CFirmata.h @@ -0,0 +1,333 @@ +/* + I2CFirmata.h - 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. + + I2CFirmata.cpp has been merged into this header file as a hack to avoid having to + include Wire.h for every arduino sketch that includes ConfigurableFirmata. + + Last updated by Jeff Hoefs: January 23rd, 2015 +*/ + +#ifndef I2CFirmata_h +#define I2CFirmata_h + +#include +#include +#include "FirmataFeature.h" +#include "FirmataReporting.h" + +#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 + +/* i2c data */ +struct i2c_device_info { + byte addr; + int reg; + byte bytes; + byte stopTX; +}; + +class I2CFirmata: public FirmataFeature +{ + public: + I2CFirmata(); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + void report(); + + private: + /* for i2c read continuous more */ + i2c_device_info query[I2C_MAX_QUERIES]; + + byte i2cRxData[32]; + boolean isI2CEnabled; + signed char queryIndex; + unsigned int i2cReadDelayTime; // default delay time between i2c read request and Wire.requestFrom() + + void readAndReportData(byte address, int theRegister, byte numBytes, byte stopTX); + void handleI2CRequest(byte argc, byte *argv); + boolean handleI2CConfig(byte argc, byte *argv); + boolean enableI2CPins(); + void disableI2CPins(); +}; + + +/* + * I2CFirmata.cpp + * Copied here as a hack to avoid having to include Wire.h in all sketch files that + * include ConfigurableFirmata.h + */ + +I2CFirmata::I2CFirmata() +{ + isI2CEnabled = false; + queryIndex = -1; + i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom() +} + +void I2CFirmata::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); + Wire.write((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()) { + Firmata.sendString("I2C: Too many bytes received"); + } else if (numBytes > Wire.available()) { + Firmata.sendString("I2C: Too few bytes received"); + } + + i2cRxData[0] = address; + i2cRxData[1] = theRegister; + + for (int i = 0; i < numBytes && Wire.available(); i++) { + i2cRxData[2 + i] = Wire.read(); + } + + // send slave address, register and received bytes + Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData); +} + +boolean I2CFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_I2C(pin)) { + if (mode == PIN_MODE_I2C) { + // the user must call I2C_CONFIG to enable I2C for a device + return true; + } else if (isI2CEnabled) { + // disable i2c so pins can be used for other functions + // the following if statements should reconfigure the pins properly + if (Firmata.getPinMode(pin) == PIN_MODE_I2C) { + disableI2CPins(); + } + } + } + return false; +} + +void I2CFirmata::handleCapability(byte pin) +{ + if (IS_PIN_I2C(pin)) { + Firmata.write(PIN_MODE_I2C); + Firmata.write(1); // TODO: could assign a number to map to SCL or SDA + } +} + +boolean I2CFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + switch (command) { + case I2C_REQUEST: + if (isI2CEnabled) { + handleI2CRequest(argc, argv); + return true; + } + case I2C_CONFIG: + return handleI2CConfig(argc, argv); + } + return false; +} + +void I2CFirmata::handleI2CRequest(byte argc, byte *argv) +{ + byte mode; + byte stopTX; + byte slaveAddress; + byte data; + int slaveRegister; + mode = argv[1] & I2C_READ_WRITE_MODE_MASK; + if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) { + Firmata.sendString("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); + Wire.write(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 + Firmata.sendString("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; + 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; + } + } + queryIndex--; + } + break; + default: + break; + } +} + +boolean I2CFirmata::handleI2CConfig(byte argc, byte *argv) +{ + unsigned int delayTime = (argv[0] + (argv[1] << 7)); + + if (delayTime > 0) { + i2cReadDelayTime = delayTime; + } + + if (!isI2CEnabled) { + enableI2CPins(); + } + return isI2CEnabled; +} + +boolean I2CFirmata::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)) { + if (Firmata.getPinMode(i) == PIN_MODE_IGNORE) { + return false; + } + // mark pins as i2c so they are ignore in non i2c data requests + Firmata.setPinMode(i, PIN_MODE_I2C); + pinMode(i, PIN_MODE_I2C); + } + } + + isI2CEnabled = true; + + Wire.begin(); + return true; +} + +/* disable the i2c pins so they can be used for other functions */ +void I2CFirmata::disableI2CPins() +{ + isI2CEnabled = false; + // disable read continuous mode for all devices + queryIndex = -1; + // uncomment the following if or when the end() method is added to Wire library + // Wire.end(); +} + +void I2CFirmata::reset() +{ + if (isI2CEnabled) { + disableI2CPins(); + } +} + +void I2CFirmata::report() +{ + // report i2c data for all device with read continuous mode enabled + if (queryIndex > -1) { + for (byte i = 0; i < queryIndex + 1; i++) { + readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX); + } + } +} + +#endif /* I2CFirmata_h */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.cpp new file mode 100644 index 0000000..80cf5ae --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.cpp @@ -0,0 +1,178 @@ +/* + OneWireFirmata.cpp - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. All rights reserved. + Copyright (C) 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: January 23rd, 2016 +*/ + +#include +#include "OneWireFirmata.h" +#include "Encoder7Bit.h" + +boolean OneWireFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_DIGITAL(pin) && mode == PIN_MODE_ONEWIRE) { + oneWireConfig(pin, ONEWIRE_POWER); + return true; + } + return false; +} + +void OneWireFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_ONEWIRE); + Firmata.write(1); + } +} + +void OneWireFirmata::oneWireConfig(byte pin, boolean power) +{ + ow_device_info *info = &pinOneWire[pin]; + if (info->device == NULL) { + info->device = new OneWire(pin); + } + for (int i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + info->power = power; +} + +boolean OneWireFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == ONEWIRE_DATA) { + if (argc > 1) { + byte subcommand = argv[0]; + byte pin = argv[1]; + ow_device_info *info = &pinOneWire[pin]; + OneWire *device = info->device; + if (device || subcommand == ONEWIRE_CONFIG_REQUEST) { + switch (subcommand) { + case ONEWIRE_SEARCH_REQUEST: + case ONEWIRE_SEARCH_ALARMS_REQUEST: + { + device->reset_search(); + Firmata.write(START_SYSEX); + Firmata.write(ONEWIRE_DATA); + boolean isAlarmSearch = (subcommand == ONEWIRE_SEARCH_ALARMS_REQUEST); + Firmata.write(isAlarmSearch ? (byte)ONEWIRE_SEARCH_ALARMS_REPLY : (byte)ONEWIRE_SEARCH_REPLY); + Firmata.write(pin); + Encoder7Bit.startBinaryWrite(); + byte addrArray[8]; + while (isAlarmSearch ? device->search(addrArray, false) : device->search(addrArray)) { + for (int i = 0; i < 8; i++) { + Encoder7Bit.writeBinary(addrArray[i]); + } + } + Encoder7Bit.endBinaryWrite(); + Firmata.write(END_SYSEX); + break; + } + case ONEWIRE_CONFIG_REQUEST: + { + if (argc == 3 && Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + Firmata.setPinMode(pin, PIN_MODE_ONEWIRE); + oneWireConfig(pin, argv[2]); // this calls oneWireConfig again, this time setting the correct config (which doesn't cause harm though) + } else { + return false; + } + break; + } + default: + { + if (subcommand & ONEWIRE_RESET_REQUEST_BIT) { + device->reset(); + for (int i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + } + if (subcommand & ONEWIRE_SKIP_REQUEST_BIT) { + device->skip(); + for (byte i = 0; i < 8; i++) { + info->addr[i] = 0x0; + } + } + if (subcommand & ONEWIRE_WITHDATA_REQUEST_BITS) { + int numBytes = num7BitOutbytes(argc - 2); + int numReadBytes = 0; + int correlationId; + argv += 2; + Encoder7Bit.readBinary(numBytes, argv, argv); //decode inplace + + if (subcommand & ONEWIRE_SELECT_REQUEST_BIT) { + if (numBytes < 8) break; + device->select(argv); + for (int i = 0; i < 8; i++) { + info->addr[i] = argv[i]; + } + argv += 8; + numBytes -= 8; + } + + if (subcommand & ONEWIRE_READ_REQUEST_BIT) { + if (numBytes < 4) break; + numReadBytes = *((int*)argv); + argv += 2; + correlationId = *((int*)argv); + argv += 2; + numBytes -= 4; + } + + if (subcommand & ONEWIRE_DELAY_REQUEST_BIT) { + if (numBytes < 4) break; + Firmata.delayTask(*((long*)argv)); + argv += 4; + numBytes -= 4; + } + + if (subcommand & ONEWIRE_WRITE_REQUEST_BIT) { + for (int i = 0; i < numBytes; i++) { + info->device->write(argv[i], info->power); + } + } + + if (numReadBytes > 0) { + Firmata.write(START_SYSEX); + Firmata.write(ONEWIRE_DATA); + Firmata.write(ONEWIRE_READ_REPLY); + Firmata.write(pin); + Encoder7Bit.startBinaryWrite(); + Encoder7Bit.writeBinary(correlationId & 0xFF); + Encoder7Bit.writeBinary((correlationId >> 8) & 0xFF); + for (int i = 0; i < numReadBytes; i++) { + Encoder7Bit.writeBinary(device->read()); + } + Encoder7Bit.endBinaryWrite(); + Firmata.write(END_SYSEX); + } + } + } + } + } + return true; + } + } + return false; +} + +void OneWireFirmata::reset() +{ + for (int i = 0; i < TOTAL_PINS; i++) { + if (pinOneWire[i].device) { + free(pinOneWire[i].device); + pinOneWire[i].device = NULL; + } + for (int j = 0; j < 8; j++) { + pinOneWire[i].addr[j] = 0; + } + pinOneWire[i].power = false; + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.h new file mode 100644 index 0000000..9c68aef --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/OneWireFirmata.h @@ -0,0 +1,63 @@ +/* + OneWireFirmata.h - Firmata library + Copyright (C) 2012-2013 Norbert Truchsess. All rights reserved. + Copyright (C) 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. +*/ + +#ifndef OneWireFirmata_h +#define OneWireFirmata_h + +#include +#include "utility/OneWire.h" +#include "FirmataFeature.h" + +//subcommands: +#define ONEWIRE_SEARCH_REQUEST 0x40 +#define ONEWIRE_CONFIG_REQUEST 0x41 +#define ONEWIRE_SEARCH_REPLY 0x42 +#define ONEWIRE_READ_REPLY 0x43 +#define ONEWIRE_SEARCH_ALARMS_REQUEST 0x44 +#define ONEWIRE_SEARCH_ALARMS_REPLY 0x45 + +#define ONEWIRE_RESET_REQUEST_BIT 0x01 +#define ONEWIRE_SKIP_REQUEST_BIT 0x02 +#define ONEWIRE_SELECT_REQUEST_BIT 0x04 +#define ONEWIRE_READ_REQUEST_BIT 0x08 +#define ONEWIRE_DELAY_REQUEST_BIT 0x10 +#define ONEWIRE_WRITE_REQUEST_BIT 0x20 + +#define ONEWIRE_WITHDATA_REQUEST_BITS 0x3C + +#define ONEWIRE_CRC 0 //for OneWire.h: crc-functions are not used by Firmata + +//default value for power: +#define ONEWIRE_POWER 1 + +struct ow_device_info +{ + OneWire* device; + byte addr[8]; + boolean power; +}; + +class OneWireFirmata: public FirmataFeature +{ + public: + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + + private: + ow_device_info pinOneWire[TOTAL_PINS]; + void oneWireConfig(byte pin, boolean power); +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.cpp new file mode 100644 index 0000000..742834a --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.cpp @@ -0,0 +1,358 @@ +/* + SerialFirmata.cpp - Firmata library + Copyright (C) 2015-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 March 6th, 2016 +*/ + +#include "SerialFirmata.h" + +SerialFirmata::SerialFirmata() +{ + swSerial0 = NULL; + swSerial1 = NULL; + swSerial2 = NULL; + swSerial3 = NULL; + + serialIndex = -1; +} + +boolean SerialFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_SERIAL) { + // nothing else to do here since the mode is set in SERIAL_CONFIG + return true; + } + return false; +} + +void SerialFirmata::handleCapability(byte pin) +{ + if (IS_PIN_SERIAL(pin)) { + Firmata.write(PIN_MODE_SERIAL); + Firmata.write(getSerialPinType(pin)); + } +} + +boolean SerialFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == SERIAL_MESSAGE) { + + Stream *serialPort; + byte mode = argv[0] & SERIAL_MODE_MASK; + byte portId = argv[0] & SERIAL_PORT_ID_MASK; + if (portId >= SERIAL_READ_ARR_LEN) return false; + + switch (mode) { + case SERIAL_CONFIG: + { + long baud = (long)argv[1] | ((long)argv[2] << 7) | ((long)argv[3] << 14); + serial_pins pins; + lastAvailableBytes[portId] = 0; + lastReceive[portId] = 0; +// this ifdef will be removed once a command to enable RX buffering has been added to the protocol +#if defined(FIRMATA_SERIAL_PORT_RX_BUFFERING) + // 8N1 = 10 bits per char, max. 50 bits -> 50000 = 50bits * 1000ms/s + // char delay value (ms) to detect the end of a message, defaults to 50 bits * 1000 / baud rate + // a value of 0 will disable RX buffering, resulting in single byte transfers to the host with + // baud rates below approximately 56k (varies with CPU speed) + maxCharDelay[portId] = 50000 / baud; +#else + maxCharDelay[portId] = 0; +#endif + if (portId < 8) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + pins = getSerialPinNumbers(portId); + if (pins.rx != 0 && pins.tx != 0) { + Firmata.setPinMode(pins.rx, PIN_MODE_SERIAL); + Firmata.setPinMode(pins.tx, PIN_MODE_SERIAL); + // Fixes an issue where some serial devices would not work properly with Arduino Due + // because all Arduino pins are set to OUTPUT by default in StandardFirmata. + pinMode(pins.rx, INPUT); + } + ((HardwareSerial*)serialPort)->begin(baud); + } + } else { +#if defined(SoftwareSerial_h) + byte swTxPin, swRxPin; + if (argc > 4) { + swRxPin = argv[4]; + swTxPin = argv[5]; + } else { + // RX and TX pins must be specified when using software serial + Firmata.sendString("Specify serial RX and TX pins"); + return false; + } + switch (portId) { + case SW_SERIAL0: + if (swSerial0 == NULL) { + swSerial0 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL1: + if (swSerial1 == NULL) { + swSerial1 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL2: + if (swSerial2 == NULL) { + swSerial2 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + case SW_SERIAL3: + if (swSerial3 == NULL) { + swSerial3 = new SoftwareSerial(swRxPin, swTxPin); + } + break; + } + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + Firmata.setPinMode(swRxPin, PIN_MODE_SERIAL); + Firmata.setPinMode(swTxPin, PIN_MODE_SERIAL); + ((SoftwareSerial*)serialPort)->begin(baud); + } +#endif + } + break; // SERIAL_CONFIG + } + case SERIAL_WRITE: + { + byte data; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + break; + } + for (byte i = 1; i < argc; i += 2) { + data = argv[i] + (argv[i + 1] << 7); + serialPort->write(data); + } + break; // SERIAL_WRITE + } + case SERIAL_READ: + if (argv[1] == SERIAL_READ_CONTINUOUSLY) { + if (serialIndex + 1 >= MAX_SERIAL_PORTS) { + break; + } + + if (argc > 2) { + // maximum number of bytes to read from buffer per iteration of loop() + serialBytesToRead[portId] = (int)argv[2] | ((int)argv[3] << 7); + } else { + // read all available bytes per iteration of loop() + serialBytesToRead[portId] = 0; + } + serialIndex++; + reportSerial[serialIndex] = portId; + } else if (argv[1] == SERIAL_STOP_READING) { + byte serialIndexToSkip = 0; + if (serialIndex <= 0) { + serialIndex = -1; + } else { + for (byte i = 0; i < serialIndex + 1; i++) { + if (reportSerial[i] == portId) { + serialIndexToSkip = i; + break; + } + } + // shift elements over to fill space left by removed element + for (byte i = serialIndexToSkip; i < serialIndex + 1; i++) { + if (i < MAX_SERIAL_PORTS) { + reportSerial[i] = reportSerial[i + 1]; + } + } + serialIndex--; + } + } + break; // SERIAL_READ + case SERIAL_CLOSE: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + if (portId < 8) { + ((HardwareSerial*)serialPort)->end(); + } else { +#if defined(SoftwareSerial_h) + ((SoftwareSerial*)serialPort)->end(); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } +#endif + } + } + break; // SERIAL_CLOSE + case SERIAL_FLUSH: + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + getPortFromId(portId)->flush(); + } + break; // SERIAL_FLUSH +#if defined(SoftwareSerial_h) + case SERIAL_LISTEN: + // can only call listen() on software serial ports + if (portId > 7) { + serialPort = getPortFromId(portId); + if (serialPort != NULL) { + ((SoftwareSerial*)serialPort)->listen(); + } + } + break; // SERIAL_LISTEN +#endif + } + return true; + } + return false; +} + +void SerialFirmata::update() +{ + checkSerial(); +} + +void SerialFirmata::reset() +{ +#if defined(SoftwareSerial_h) + Stream *serialPort; + // free memory allocated for SoftwareSerial ports + for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1; i++) { + serialPort = getPortFromId(i); + if (serialPort != NULL) { + free(serialPort); + serialPort = NULL; + } + } +#endif + + serialIndex = -1; + for (byte i = 0; i < SERIAL_READ_ARR_LEN; i++) { + serialBytesToRead[i] = 0; + } +} + +// get a pointer to the serial port associated with the specified port id +Stream* SerialFirmata::getPortFromId(byte portId) +{ + switch (portId) { + case HW_SERIAL0: + // block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented + //return &Serial; + return NULL; +#if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + return &Serial1; +#endif +#if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + return &Serial2; +#endif +#if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + return &Serial3; +#endif +#if defined(SoftwareSerial_h) + case SW_SERIAL0: + if (swSerial0 != NULL) { + // instances of SoftwareSerial are already pointers so simply return the instance + return swSerial0; + } + break; + case SW_SERIAL1: + if (swSerial1 != NULL) { + return swSerial1; + } + break; + case SW_SERIAL2: + if (swSerial2 != NULL) { + return swSerial2; + } + break; + case SW_SERIAL3: + if (swSerial3 != NULL) { + return swSerial3; + } + break; +#endif + } + return NULL; +} + +// Check serial ports that have READ_CONTINUOUS mode set and relay any data +// for each port to the device attached to that port. +void SerialFirmata::checkSerial() +{ + byte portId, serialData; + int bytesToRead = 0; + int numBytesToRead = 0; + Stream* serialPort; + + if (serialIndex > -1) { + + unsigned long currentMillis = millis(); + + // loop through all reporting (READ_CONTINUOUS) serial ports + for (byte i = 0; i < serialIndex + 1; i++) { + portId = reportSerial[i]; + bytesToRead = serialBytesToRead[portId]; + serialPort = getPortFromId(portId); + if (serialPort == NULL) { + continue; + } +#if defined(SoftwareSerial_h) + // only the SoftwareSerial port that is "listening" can read data + if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening()) { + continue; + } +#endif + int availableBytes = serialPort->available(); + if (availableBytes > 0) { + bool read = true; + + // check if reading should be delayed to collect some bytes before + // forwarding (for baud rates significantly below 57600 baud) + if (maxCharDelay[portId]) { + // inter character delay exceeded or more than 48 bytes available or more bytes available than required + read = (lastAvailableBytes[portId] > 0 && (currentMillis - lastReceive[portId]) >= maxCharDelay[portId]) + || (bytesToRead == 0 && availableBytes >= 48) || (bytesToRead > 0 && availableBytes >= bytesToRead); + if (availableBytes > lastAvailableBytes[portId]) { + lastReceive[portId] = currentMillis; + lastAvailableBytes[portId] = availableBytes; + } + } + + if (read) { + Firmata.write(START_SYSEX); + Firmata.write(SERIAL_MESSAGE); + Firmata.write(SERIAL_REPLY | portId); + + if (bytesToRead == 0 || (serialPort->available() <= bytesToRead)) { + numBytesToRead = serialPort->available(); + } else { + numBytesToRead = bytesToRead; + } + + if (lastAvailableBytes[portId] - numBytesToRead >= 0) { + lastAvailableBytes[portId] -= numBytesToRead; + } else { + lastAvailableBytes[portId] = 0; + } + + // relay serial data to the serial device + while (numBytesToRead > 0) { + serialData = serialPort->read(); + Firmata.write(serialData & 0x7F); + Firmata.write((serialData >> 7) & 0x7F); + numBytesToRead--; + } + Firmata.write(END_SYSEX); + } + } + } + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.h new file mode 100644 index 0000000..8e1cff0 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/SerialFirmata.h @@ -0,0 +1,163 @@ +/* + SerialFirmata.h - Firmata library + Copyright (C) 2015-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 March 6th, 2016 +*/ + +#ifndef SerialFirmata_h +#define SerialFirmata_h + +#include +#include "FirmataFeature.h" +// SoftwareSerial is currently only supported for AVR-based boards and the Arduino 101 +// The third condition checks if the IDE is in the 1.0.x series, if so, include SoftwareSerial +#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ARC32) || (ARDUINO >= 100 && ARDUINO < 10500) +#include +#endif + +// uncomment FIRMATA_SERIAL_PORT_RX_BUFFERING to collect bytes received by serial port until the +// receive buffer gets filled or a data gap is detected to avoid forwarding single bytes at baud +// rates below 50000 +//#define FIRMATA_SERIAL_PORT_RX_BUFFERING + +// Serial port Ids +#define HW_SERIAL0 0x00 +#define HW_SERIAL1 0x01 +#define HW_SERIAL2 0x02 +#define HW_SERIAL3 0x03 +// extensible up to 0x07 + +#define SW_SERIAL0 0x08 +#define SW_SERIAL1 0x09 +#define SW_SERIAL2 0x0A +#define SW_SERIAL3 0x0B +// extensible up to 0x0F + +#define SERIAL_PORT_ID_MASK 0x0F +#define MAX_SERIAL_PORTS 8 +#define SERIAL_READ_ARR_LEN 12 + +// map configuration query response resolution value to serial pin type +#define RES_RX1 0x02 +#define RES_TX1 0x03 +#define RES_RX2 0x04 +#define RES_TX2 0x05 +#define RES_RX3 0x06 +#define RES_TX3 0x07 + +// Serial command bytes +#define SERIAL_CONFIG 0x10 +#define SERIAL_WRITE 0x20 +#define SERIAL_READ 0x30 +#define SERIAL_REPLY 0x40 +#define SERIAL_CLOSE 0x50 +#define SERIAL_FLUSH 0x60 +#define SERIAL_LISTEN 0x70 + +// Serial read modes +#define SERIAL_READ_CONTINUOUSLY 0x00 +#define SERIAL_STOP_READING 0x01 +#define SERIAL_MODE_MASK 0xF0 + +struct serial_pins { + uint8_t rx; + uint8_t tx; +}; + +/* + * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin. + */ +inline uint8_t getSerialPinType(uint8_t pin) { +#if defined(PIN_SERIAL_RX) + // TODO when use of HW_SERIAL0 is enabled +#endif +#if defined(PIN_SERIAL1_RX) + if (pin == PIN_SERIAL1_RX) return RES_RX1; + if (pin == PIN_SERIAL1_TX) return RES_TX1; +#endif +#if defined(PIN_SERIAL2_RX) + if (pin == PIN_SERIAL2_RX) return RES_RX2; + if (pin == PIN_SERIAL2_TX) return RES_TX2; +#endif +#if defined(PIN_SERIAL3_RX) + if (pin == PIN_SERIAL3_RX) return RES_RX3; + if (pin == PIN_SERIAL3_TX) return RES_TX3; +#endif + return 0; +} + +/* + * Get the RX and TX pins numbers for the specified HW serial port. + */ +inline serial_pins getSerialPinNumbers(uint8_t portId) { + serial_pins pins; + switch (portId) { +#if defined(PIN_SERIAL_RX) + // case HW_SERIAL0: + // // TODO when use of HW_SERIAL0 is enabled + // break; +#endif +#if defined(PIN_SERIAL1_RX) + case HW_SERIAL1: + pins.rx = PIN_SERIAL1_RX; + pins.tx = PIN_SERIAL1_TX; + break; +#endif +#if defined(PIN_SERIAL2_RX) + case HW_SERIAL2: + pins.rx = PIN_SERIAL2_RX; + pins.tx = PIN_SERIAL2_TX; + break; +#endif +#if defined(PIN_SERIAL3_RX) + case HW_SERIAL3: + pins.rx = PIN_SERIAL3_RX; + pins.tx = PIN_SERIAL3_TX; + break; +#endif + default: + pins.rx = 0; + pins.tx = 0; + } + return pins; +} + + +class SerialFirmata: public FirmataFeature +{ + public: + SerialFirmata(); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void update(); + void reset(); + void checkSerial(); + + private: + byte reportSerial[MAX_SERIAL_PORTS]; + int serialBytesToRead[SERIAL_READ_ARR_LEN]; + signed char serialIndex; + + unsigned long lastReceive[SERIAL_READ_ARR_LEN]; + unsigned char maxCharDelay[SERIAL_READ_ARR_LEN]; + int lastAvailableBytes[SERIAL_READ_ARR_LEN]; + + Stream *swSerial0; + Stream *swSerial1; + Stream *swSerial2; + Stream *swSerial3; + + Stream* getPortFromId(byte portId); + +}; + +#endif /* SerialFirmata_h */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.cpp new file mode 100644 index 0000000..307453f --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in ServoFirmata.h to avoid having to include Servo.h in all + * sketch files that include ConfigurableFirmata.h + */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.h new file mode 100644 index 0000000..bac4ed9 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/ServoFirmata.h @@ -0,0 +1,151 @@ +/* + ServoFirmata.h - 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-2015 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. + + ServoFirmata.cpp has been merged into this header file as a hack to avoid having to + include Servo.h for every arduino sketch that includes ConfigurableFirmata. + + Last updated by Jeff Hoefs: November 15th, 2015 +*/ + +#ifndef ServoFirmata_h +#define ServoFirmata_h + +#include +#include +#include "FirmataFeature.h" + +void servoAnalogWrite(byte pin, int value); + +class ServoFirmata: public FirmataFeature +{ + public: + ServoFirmata(); + boolean analogWrite(byte pin, int value); + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte* argv); + void reset(); + private: + Servo *servos[MAX_SERVOS]; + void attach(byte pin, int minPulse, int maxPulse); + void detach(byte pin); +}; + + +/* + * ServoFirmata.cpp + * Copied here as a hack to avoid having to include Servo.h in all sketch files that + * include ConfigurableFirmata.h + */ + +ServoFirmata *ServoInstance; + +void servoAnalogWrite(byte pin, int value) +{ + ServoInstance->analogWrite(pin, value); +} + +ServoFirmata::ServoFirmata() +{ + ServoInstance = this; +} + +boolean ServoFirmata::analogWrite(byte pin, int value) +{ + if (IS_PIN_SERVO(pin)) { + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (servo) { + servo->write(value); + return true; + } + } + return false; +} + +boolean ServoFirmata::handlePinMode(byte pin, int mode) +{ + if (IS_PIN_SERVO(pin)) { + if (mode == PIN_MODE_SERVO) { + attach(pin, -1, -1); + return true; + } else { + detach(pin); + } + } + return false; +} + +void ServoFirmata::handleCapability(byte pin) +{ + if (IS_PIN_SERVO(pin)) { + Firmata.write(PIN_MODE_SERVO); + Firmata.write(14); //14 bit resolution (Servo takes int as argument) + } +} + +boolean ServoFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command == SERVO_CONFIG) { + if (argc > 4) { + // these vars are here for clarity, they'll optimized away by the compiler + byte pin = argv[0]; + if (IS_PIN_SERVO(pin) && Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + int minPulse = argv[1] + (argv[2] << 7); + int maxPulse = argv[3] + (argv[4] << 7); + Firmata.setPinMode(pin, PIN_MODE_SERVO); + attach(pin, minPulse, maxPulse); + return true; + } + } + } + return false; +} + +void ServoFirmata::attach(byte pin, int minPulse, int maxPulse) +{ + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (!servo) { + servo = new Servo(); + servos[PIN_TO_SERVO(pin)] = servo; + } + if (servo->attached()) + servo->detach(); + if (minPulse >= 0 || maxPulse >= 0) + servo->attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse); + else + servo->attach(PIN_TO_DIGITAL(pin)); +} + +void ServoFirmata::detach(byte pin) +{ + Servo *servo = servos[PIN_TO_SERVO(pin)]; + if (servo) { + if (servo->attached()) + servo->detach(); + free(servo); + servos[PIN_TO_SERVO(pin)] = NULL; + } +} + +void ServoFirmata::reset() +{ + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + if (IS_PIN_SERVO(pin)) { + detach(pin); + } + } +} + +#endif /* ServoFirmata_h */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.cpp b/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.cpp new file mode 100644 index 0000000..a5618ff --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.cpp @@ -0,0 +1,151 @@ +/* + 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: January 23rd, 2016 +*/ + +#include +#include "StepperFirmata.h" +#include "utility/FirmataStepper.h" + +boolean StepperFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == PIN_MODE_STEPPER) { + if (IS_PIN_DIGITAL(pin)) { + digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM + pinMode(PIN_TO_DIGITAL(pin), OUTPUT); + return true; + } + } + return false; +} + +void StepperFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) { + Firmata.write(PIN_MODE_STEPPER); + Firmata.write(21); //21 bits used for number of steps + } +} + +/*============================================================================== + * SYSEX-BASED commands + *============================================================================*/ + +boolean StepperFirmata::handleSysex(byte command, byte argc, byte *argv) +{ + if (command == STEPPER_DATA) { + byte stepCommand, deviceNum, directionPin, stepPin, stepDirection; + byte interface, interfaceType; + byte motorPin3, motorPin4; + unsigned int stepsPerRev; + long numSteps; + int stepSpeed; + int accel; + int decel; + + stepCommand = argv[0]; + deviceNum = argv[1]; + + if (deviceNum < MAX_STEPPERS) { + if (stepCommand == STEPPER_CONFIG) { + + interface = argv[2]; // upper 4 bits are the stepDelay, lower 4 bits are the interface type + interfaceType = interface & 0x0F; // the interface type is specified by the lower 4 bits + stepsPerRev = (argv[3] + (argv[4] << 7)); + + directionPin = argv[5]; // or motorPin1 for TWO_WIRE or FOUR_WIRE interface + stepPin = argv[6]; // // or motorPin2 for TWO_WIRE or FOUR_WIRE interface + if (Firmata.getPinMode(directionPin) == PIN_MODE_IGNORE || Firmata.getPinMode(stepPin) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(directionPin, PIN_MODE_STEPPER); + Firmata.setPinMode(stepPin, PIN_MODE_STEPPER); + + if (!stepper[deviceNum]) { + numSteppers++; + } + if (interfaceType == FirmataStepper::DRIVER || interfaceType == FirmataStepper::TWO_WIRE) { + stepper[deviceNum] = new FirmataStepper(interface, stepsPerRev, directionPin, stepPin); + } else if (interfaceType == FirmataStepper::FOUR_WIRE) { + motorPin3 = argv[7]; + motorPin4 = argv[8]; + if (Firmata.getPinMode(motorPin3) == PIN_MODE_IGNORE || Firmata.getPinMode(motorPin4) == PIN_MODE_IGNORE) + return false; + Firmata.setPinMode(motorPin3, PIN_MODE_STEPPER); + Firmata.setPinMode(motorPin4, PIN_MODE_STEPPER); + stepper[deviceNum] = new FirmataStepper(interface, stepsPerRev, directionPin, stepPin, motorPin3, motorPin4); + } + } + else if (stepCommand == STEPPER_STEP) { + stepDirection = argv[2]; + numSteps = (long)argv[3] | ((long)argv[4] << 7) | ((long)argv[5] << 14); + stepSpeed = (argv[6] + (argv[7] << 7)); + + if (stepDirection == 0) { + numSteps *= -1; + } + if (stepper[deviceNum]) { + if (argc >= 8 && argc < 12) { + // num steps, speed (0.01*rad/sec) + stepper[deviceNum]->setStepsToMove(numSteps, stepSpeed); + } else if (argc == 12) { + accel = (argv[8] + (argv[9] << 7)); + decel = (argv[10] + (argv[11] << 7)); + // num steps, speed (0.01*rad/sec), accel (0.01*rad/sec^2), decel (0.01*rad/sec^2) + stepper[deviceNum]->setStepsToMove(numSteps, stepSpeed, accel, decel); + } + } + } + return true; + } + } + return false; +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void StepperFirmata::reset() +{ + for (byte i = 0; i < MAX_STEPPERS; i++) { + if (stepper[i]) { + free(stepper[i]); + stepper[i] = 0; + } + } + numSteppers = 0; +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void StepperFirmata::update() +{ + if (numSteppers > 0) { + // if one or more stepper motors are used, update their position + for (byte i = 0; i < MAX_STEPPERS; i++) { + if (stepper[i]) { + bool done = stepper[i]->update(); + // send command to client application when stepping is complete + if (done) { + Firmata.write(START_SYSEX); + Firmata.write(STEPPER_DATA); + Firmata.write(i); + Firmata.write(END_SYSEX); + } + } + } + } +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.h b/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.h new file mode 100644 index 0000000..7fc9459 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/StepperFirmata.h @@ -0,0 +1,40 @@ +/* + 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. +*/ + +#ifndef StepperFirmata_h +#define StepperFirmata_h + +#include +#include "utility/FirmataStepper.h" +#include "FirmataFeature.h" + +#define MAX_STEPPERS 6 // arbitrary value... may need to adjust +#define STEPPER_CONFIG 0 +#define STEPPER_STEP 1 + +class StepperFirmata: public FirmataFeature +{ + public: + boolean handlePinMode(byte pin, int mode); + void handleCapability(byte pin); + boolean handleSysex(byte command, byte argc, byte *argv); + void update(); + void reset(); + private: + FirmataStepper *stepper[MAX_STEPPERS]; + byte numSteppers; +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.cpp b/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.cpp new file mode 100644 index 0000000..ad63007 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.cpp @@ -0,0 +1,448 @@ +/** + FirmataStepper is a simple non-blocking stepper motor library + for 2 and 4 wire bipolar and unipolar stepper motor drive circuits + as well as EasyDriver (http://schmalzhaus.com/EasyDriver/) and + other step + direction drive circuits. + + FirmataStepper (0.2) by Jeff Hoefs + + EasyDriver support based on modifications by Chris Coleman + + Acceleration / Deceleration algorithms and code based on: + app note: http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf + source code: http://www.atmel.com/dyn/resources/prod_documents/AVR446.zip + + stepMotor function based on Stepper.cpp Stepper library for + Wiring/Arduino created by Tom Igoe, Sebastian Gassner + David Mellis and Noah Shibley. + + Relevant notes from Stepper.cpp: + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +#include "FirmataStepper.h" + +/** + * Constructor. + * + * Configure a stepper for an EasyDriver or other step + direction interface or + * configure a bipolar or unipolar stepper motor for 2 wire drive mode. + * Configure a bipolar or unipolar stepper for 4 wire drive mode. + * @param interface Lower 3 bits: + * The interface type: FirmataStepper::DRIVER, + * FirmataStepper::TWO_WIRE or FirmataStepper::FOUR_WIRE + * Upper 4 bits: Any bits set = use 2 microsecond delay + * @param steps_per_rev The number of steps to make 1 revolution. + * @param first_pin The direction pin (EasyDriver) or the pin attached to the + * 1st motor coil (2 wire drive mode) + * @param second_pin The step pin (EasyDriver) or the pin attached to the 2nd + * motor coil (2 wire drive mode) + * @param motor_pin_3 The pin attached to the 3rd motor coil + * @param motor_pin_4 The pin attached to the 4th motor coil + */ +FirmataStepper::FirmataStepper(byte interface, + int steps_per_rev, + byte pin1, + byte pin2, + byte pin3, + byte pin4) +{ + this->step_number = 0; // which step the motor is on + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->steps_per_rev = steps_per_rev; // total number of steps for this motor + this->running = false; + this->interface = interface & 0x0F; // default to Easy Stepper (or other step + direction driver) + + // could update this in future to support additional delays if necessary + if (((interface & 0xF0) >> 4) > 0) + { + // high current driver + this->stepDelay = 2; // microseconds + } + else + { + this->stepDelay = 1; // microseconds + } + + this->motor_pin_1 = pin1; + this->motor_pin_2 = pin2; + this->dir_pin = pin1; + this->step_pin = pin2; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + + if (this->interface == FirmataStepper::FOUR_WIRE) + { + this->motor_pin_3 = pin3; + this->motor_pin_4 = pin4; + pinMode(this->motor_pin_3, OUTPUT); + pinMode(this->motor_pin_4, OUTPUT); + } + + + this->alpha = PI_2 / this->steps_per_rev; + this->at_x100 = (long)(this->alpha * T1_FREQ * 100); + this->ax20000 = (long)(this->alpha * 20000); + this->alpha_x2 = this->alpha * 2; + +} + +/** + * Move the stepper a given number of steps at the specified + * speed (rad/sec), acceleration (rad/sec^2) and deceleration (rad/sec^2). + * + * @param steps_to_move The number ofsteps to move the motor + * @param speed Max speed in 0.01*rad/sec + * @param accel [optional] Acceleration in 0.01*rad/sec^2 + * @param decel [optional] Deceleration in 0.01*rad/sec^2 + */ +void FirmataStepper::setStepsToMove(long steps_to_move, int speed, int accel, int decel) +{ + unsigned long maxStepLimit; + unsigned long accelerationLimit; + + this->step_number = 0; + this->lastAccelDelay = 0; + this->stepCount = 0; + this->rest = 0; + + if (steps_to_move < 0) + { + this->direction = FirmataStepper::CCW; + steps_to_move = -steps_to_move; + } + else + { + this->direction = FirmataStepper::CW; + } + + this->steps_to_move = steps_to_move; + + // set max speed limit, by calc min_delay + // min_delay = (alpha / tt)/w + this->min_delay = this->at_x100 / speed; + + // if acceleration or deceleration are not defined + // start in RUN state and do no decelerate + if (accel == 0 || decel == 0) + { + this->step_delay = this->min_delay; + + this->decel_start = steps_to_move; + this->run_state = FirmataStepper::RUN; + this->accel_count = 0; + this->running = true; + + return; + } + + // if only moving 1 step + if (steps_to_move == 1) + { + + // move one step + this->accel_count = -1; + this->run_state = FirmataStepper::DECEL; + + this->step_delay = this->min_delay; + this->running = true; + } + else if (steps_to_move != 0) + { + // set initial step delay + // step_delay = 1/tt * sqrt(2*alpha/accel) + // step_delay = ( tfreq*0.676/100 )*100 * sqrt( (2*alpha*10000000000) / (accel*100) )/10000 + this->step_delay = (long)((T1_FREQ_148 * sqrt(alpha_x2 / accel)) * 1000); + + // find out after how many steps does the speed hit the max speed limit. + // maxSpeedLimit = speed^2 / (2*alpha*accel) + maxStepLimit = (long)speed * speed / (long)(((long)this->ax20000 * accel) / 100); + + // if we hit max spped limit before 0.5 step it will round to 0. + // but in practice we need to move at least 1 step to get any speed at all. + if (maxStepLimit == 0) + { + maxStepLimit = 1; + } + + // find out after how many steps we must start deceleration. + // n1 = (n1+n2)decel / (accel + decel) + accelerationLimit = (long)((steps_to_move * decel) / (accel + decel)); + + // we must accelerate at least 1 step before we can start deceleration + if (accelerationLimit == 0) + { + accelerationLimit = 1; + } + + // use the limit we hit first to calc decel + if (accelerationLimit <= maxStepLimit) + { + this->decel_val = accelerationLimit - steps_to_move; + } + else + { + this->decel_val = -(long)(maxStepLimit * accel) / decel; + } + + // we must decelerate at least 1 step to stop + if (this->decel_val == 0) + { + this->decel_val = -1; + } + + // find step to start deceleration + this->decel_start = steps_to_move + this->decel_val; + + // if the max speed is so low that we don't need to go via acceleration state. + if (this->step_delay <= this->min_delay) + { + this->step_delay = this->min_delay; + this->run_state = FirmataStepper::RUN; + } + else + { + this->run_state = FirmataStepper::ACCEL; + } + + // reset counter + this->accel_count = 0; + this->running = true; + } +} + + +bool FirmataStepper::update() +{ + bool done = false; + unsigned long newStepDelay = this->min_delay; + + unsigned long curTimeVal = micros(); + unsigned long timeDiff = curTimeVal - this->last_step_time; + + if (this->running == true && timeDiff >= this->step_delay) + { + + this->last_step_time = curTimeVal; + + switch (this->run_state) + { + case FirmataStepper::STOP: + this->stepCount = 0; + this->rest = 0; + if (this->running) + { + done = true; + } + this->running = false; + break; + + case FirmataStepper::ACCEL: + updateStepPosition(); + this->stepCount++; + this->accel_count++; + newStepDelay = this->step_delay - (((2 * (long)this->step_delay) + this->rest) / (4 * this->accel_count + 1)); + this->rest = ((2 * (long)this->step_delay) + this->rest) % (4 * this->accel_count + 1); + + // check if we should start deceleration + if (this->stepCount >= this->decel_start) + { + this->accel_count = this->decel_val; + this->run_state = FirmataStepper::DECEL; + this->rest = 0; + } + // check if we hit max speed + else if (newStepDelay <= this->min_delay) + { + this->lastAccelDelay = newStepDelay; + newStepDelay = this->min_delay; + this->rest = 0; + this->run_state = FirmataStepper::RUN; + } + break; + + case FirmataStepper::RUN: + updateStepPosition(); + this->stepCount++; + + // if no accel or decel was specified, go directly to STOP state + if (stepCount >= this->steps_to_move) + { + this->run_state = FirmataStepper::STOP; + } + // check if we should start deceleration + else if (this->stepCount >= this->decel_start) + { + this->accel_count = this->decel_val; + // start deceleration with same delay that accel ended with + newStepDelay = this->lastAccelDelay; + this->run_state = FirmataStepper::DECEL; + } + break; + + case FirmataStepper::DECEL: + updateStepPosition(); + this->stepCount++; + this->accel_count++; + + newStepDelay = this->step_delay - (((2 * (long)this->step_delay) + this->rest) / (4 * this->accel_count + 1)); + this->rest = ((2 * (long)this->step_delay) + this->rest) % (4 * this->accel_count + 1); + + if (newStepDelay < 0) newStepDelay = -newStepDelay; + // check if we ar at the last step + if (this->accel_count >= 0) + { + this->run_state = FirmataStepper::STOP; + } + + break; + } + + this->step_delay = newStepDelay; + + } + + return done; + +} + +/** + * Update the step position. + * @private + */ +void FirmataStepper::updateStepPosition() +{ + // increment or decrement the step number, + // depending on direction: + if (this->direction == FirmataStepper::CW) + { + this->step_number++; + if (this->step_number >= this->steps_per_rev) + { + this->step_number = 0; + } + } + else + { + if (this->step_number <= 0) + { + this->step_number = this->steps_per_rev; + } + this->step_number--; + } + + // step the motor to step number 0, 1, 2, or 3: + stepMotor(this->step_number % 4, this->direction); +} + +/** + * Moves the motor forward or backwards. + * @param step_num For 2 or 4 wire configurations, this is the current step in + * the 2 or 4 step sequence. + * @param direction The direction of rotation + */ +void FirmataStepper::stepMotor(byte step_num, byte direction) +{ + if (this->interface == FirmataStepper::DRIVER) + { + digitalWrite(dir_pin, direction); + delayMicroseconds(this->stepDelay); + digitalWrite(step_pin, LOW); + delayMicroseconds(this->stepDelay); + digitalWrite(step_pin, HIGH); + } + else if (this->interface == FirmataStepper::TWO_WIRE) + { + switch (step_num) + { + case 0: /* 01 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + break; + case 1: /* 11 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + break; + case 2: /* 10 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + break; + case 3: /* 00 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + break; + } + } + else if (this->interface == FirmataStepper::FOUR_WIRE) + { + switch (step_num) + { + case 0: // 1010 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 1: // 0110 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 2: //0101 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + case 3: //1001 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + } + } +} + +/** + * @return The version number of this library. + */ +byte FirmataStepper::version(void) +{ + return 2; +} diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.h b/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.h new file mode 100644 index 0000000..52223f2 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/FirmataStepper.h @@ -0,0 +1,145 @@ +/* + FirmataStepper is a simple non-blocking stepper motor library + for 2 and 4 wire bipolar and unipolar stepper motor drive circuits + as well as EasyDriver (http://schmalzhaus.com/EasyDriver/) and + other step + direction drive circuits. + + FirmataStepper (0.2) by Jeff Hoefs + + EasyDriver support based on modifications by Chris Coleman + + Acceleration / Deceleration algorithms and code based on: + app note: http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf + source code: http://www.atmel.com/dyn/resources/prod_documents/AVR446.zip + + stepMotor function based on Stepper.cpp Stepper library for + Wiring/Arduino created by Tom Igoe, Sebastian Gassner + David Mellis and Noah Shibley. + + Relevant notes from Stepper.cpp: + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +// ensure this library description is only included once +#ifndef FirmataStepper_h +#define FirmataStepper_h + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#define PI_2 2*3.14159 +#define T1_FREQ 1000000L // provides the most accurate step delay values +#define T1_FREQ_148 ((long)((T1_FREQ*0.676)/100)) // divided by 100 and scaled by 0.676 + +// library interface description +class FirmataStepper +{ + public: + FirmataStepper(byte interface = FirmataStepper::DRIVER, + int steps_per_rev = 200, + byte pin1 = 2, + byte pin2 = 3, + byte pin3 = 4, + byte pin4 = 5); + + enum Interface + { + DRIVER = 1, + TWO_WIRE = 2, + FOUR_WIRE = 4 + }; + + enum RunState + { + STOP = 0, + ACCEL = 1, + DECEL = 2, + RUN = 3 + }; + + enum Direction + { + CCW = 0, + CW = 1 + }; + + void setStepsToMove(long steps_to_move, int speed, int accel = 0, int decel = 0); + + // update the stepper position + bool update(); + + byte version(void); + + private: + void stepMotor(byte step_num, byte direction); + void updateStepPosition(); + bool running; + byte interface; // Type of interface: DRIVER, TWO_WIRE or FOUR_WIRE + byte direction; // Direction of rotation + unsigned long step_delay; // delay between steps, in microseconds + int steps_per_rev; // number of steps to make one revolution + long step_number; // which step the motor is on + long steps_to_move; // total number of teps to move + byte stepDelay; // delay between steps (default = 1, increase for high current drivers) + + byte run_state; + int accel_count; + unsigned long min_delay; + long decel_start; + int decel_val; + + long lastAccelDelay; + long stepCount; + unsigned int rest; + + float alpha; // PI * 2 / steps_per_rev + long at_x100; // alpha * T1_FREQ * 100 + long ax20000; // alph a* 20000 + float alpha_x2; // alpha * 2 + + // motor pin numbers: + byte dir_pin; + byte step_pin; + byte motor_pin_1; + byte motor_pin_2; + byte motor_pin_3; + byte motor_pin_4; + + unsigned long last_step_time; // time stamp in microseconds of when the last step was taken +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.cpp b/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.cpp new file mode 100644 index 0000000..cf34933 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.cpp @@ -0,0 +1,567 @@ +/* +Copyright (c) 2007, Jim Studt (original old version - many contributors since) + +The latest version of this library may be found at: + http://www.pjrc.com/teensy/td_libs_OneWire.html + +OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since +January 2010. At the time, it was in need of many bug fixes, but had +been abandoned the original author (Jim Studt). None of the known +contributors were interested in maintaining OneWire. Paul typically +works on OneWire every 6 to 12 months. Patches usually wait that +long. If anyone is interested in more actively maintaining OneWire, +please contact Paul. + +Version 2.3: + Unknonw chip fallback mode, Roger Clark + Teensy-LC compatibility, Paul Stoffregen + Search bug fix, Love Nystrom + +Version 2.2: + Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com + Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030 + Fix DS18B20 example negative temperature + Fix DS18B20 example's low res modes, Ken Butcher + Improve reset timing, Mark Tillotson + Add const qualifiers, Bertrik Sikken + Add initial value input to crc16, Bertrik Sikken + Add target_search() function, Scott Roberts + +Version 2.1: + Arduino 1.0 compatibility, Paul Stoffregen + Improve temperature example, Paul Stoffregen + DS250x_PROM example, Guillermo Lovato + PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com + Improvements from Glenn Trewitt: + - crc16() now works + - check_crc16() does all of calculation/checking work. + - Added read_bytes() and write_bytes(), to reduce tedious loops. + - Added ds2408 example. + Delete very old, out-of-date readme file (info is here) + +Version 2.0: Modifications by Paul Stoffregen, January 2010: +http://www.pjrc.com/teensy/td_libs_OneWire.html + Search fix from Robin James + http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + Use direct optimized I/O in all cases + Disable interrupts during timing critical sections + (this solves many random communication errors) + Disable interrupts during read-modify-write I/O + Reduce RAM consumption by eliminating unnecessary + variables and trimming many to 8 bits + Optimize both crc8 - table version moved to flash + +Modified to work with larger numbers of devices - avoids loop. +Tested in Arduino 11 alpha with 12 sensors. +26 Sept 2008 -- Robin James +http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27 + +Updated to work with arduino-0008 and to include skip() as of +2007/07/06. --RJL20 + +Modified to calculate the 8-bit CRC directly, avoiding the need for +the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010 +-- Tom Pollard, Jan 23, 2008 + +Jim Studt's original library was modified by Josh Larios. + +Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008 + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Much of the code was inspired by Derek Yerger's code, though I don't +think much of that remains. In any event that was.. + (copyleft) 2006 by Derek Yerger - Free to distribute freely. + +The CRC code was excerpted and inspired by the Dallas Semiconductor +sample code bearing this copyright. +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//-------------------------------------------------------------------------- +*/ + +#include "OneWire.h" + + +OneWire::OneWire(uint8_t pin) +{ + pinMode(pin, INPUT); + bitmask = PIN_TO_BITMASK(pin); + baseReg = PIN_TO_BASEREG(pin); +#if ONEWIRE_SEARCH + reset_search(); +#endif +} + + +// Perform the onewire reset function. We will wait up to 250uS for +// the bus to come high, if it doesn't then it is broken or shorted +// and we return a 0; +// +// Returns 1 if a device asserted a presence pulse, 0 otherwise. +// +uint8_t OneWire::reset(void) +{ + IO_REG_TYPE mask = bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + uint8_t retries = 125; + + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); + interrupts(); + // wait until the wire is high... just in case + do { + if (--retries == 0) return 0; + delayMicroseconds(2); + } while ( !DIRECT_READ(reg, mask)); + + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + interrupts(); + delayMicroseconds(480); + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); // allow it to float + delayMicroseconds(70); + r = !DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(410); + return r; +} + +// +// Write a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +void OneWire::write_bit(uint8_t v) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + + if (v & 1) { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(10); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(55); + } else { + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(65); + DIRECT_WRITE_HIGH(reg, mask); // drive output high + interrupts(); + delayMicroseconds(5); + } +} + +// +// Read a bit. Port and bit is used to cut lookup time and provide +// more certain timing. +// +uint8_t OneWire::read_bit(void) +{ + IO_REG_TYPE mask=bitmask; + volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; + uint8_t r; + + noInterrupts(); + DIRECT_MODE_OUTPUT(reg, mask); + DIRECT_WRITE_LOW(reg, mask); + delayMicroseconds(3); + DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise + delayMicroseconds(10); + r = DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(53); + return r; +} + +// +// Write a byte. The writing code uses the active drivers to raise the +// pin high, if you need power after the write (e.g. DS18S20 in +// parasite power mode) then set 'power' to 1, otherwise the pin will +// go tri-state at the end of the write to avoid heating in a short or +// other mishap. +// +void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { + uint8_t bitMask; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + OneWire::write_bit( (bitMask & v)?1:0); + } + if ( !power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { + for (uint16_t i = 0 ; i < count ; i++) + write(buf[i]); + if (!power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } +} + +// +// Read a byte +// +uint8_t OneWire::read() { + uint8_t bitMask; + uint8_t r = 0; + + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + if ( OneWire::read_bit()) r |= bitMask; + } + return r; +} + +void OneWire::read_bytes(uint8_t *buf, uint16_t count) { + for (uint16_t i = 0 ; i < count ; i++) + buf[i] = read(); +} + +// +// Do a ROM select +// +void OneWire::select(const uint8_t rom[8]) +{ + uint8_t i; + + write(0x55); // Choose ROM + + for (i = 0; i < 8; i++) write(rom[i]); +} + +// +// Do a ROM skip +// +void OneWire::skip() +{ + write(0xCC); // Skip ROM +} + +void OneWire::depower() +{ + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + interrupts(); +} + +#if ONEWIRE_SEARCH + +// +// You need to use this function to start a search again from the beginning. +// You do not need to do it for the first search, though you could. +// +void OneWire::reset_search() +{ + // reset the search state + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + for(int i = 7; ; i--) { + ROM_NO[i] = 0; + if ( i == 0) break; + } +} + +// Setup the search to find the device type 'family_code' on the next call +// to search(*newAddr) if it is present. +// +void OneWire::target_search(uint8_t family_code) +{ + // set the search state to find SearchFamily type devices + ROM_NO[0] = family_code; + for (uint8_t i = 1; i < 8; i++) + ROM_NO[i] = 0; + LastDiscrepancy = 64; + LastFamilyDiscrepancy = 0; + LastDeviceFlag = FALSE; +} + +// +// Perform a search. If this function returns a '1' then it has +// enumerated the next device and you may retrieve the ROM from the +// OneWire::address variable. If there are no devices, no further +// devices, or something horrible happens in the middle of the +// enumeration then a 0 is returned. If a new device is found then +// its address is copied to newAddr. Use OneWire::reset_search() to +// start over. +// +// --- Replaced by the one from the Dallas Semiconductor web site --- +//-------------------------------------------------------------------------- +// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing +// search state. +// Return TRUE : device found, ROM number in ROM_NO buffer +// FALSE : device not found, end of search +// +uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) +{ + uint8_t id_bit_number; + uint8_t last_zero, rom_byte_number, search_result; + uint8_t id_bit, cmp_id_bit; + + unsigned char rom_byte_mask, search_direction; + + // initialize for search + id_bit_number = 1; + last_zero = 0; + rom_byte_number = 0; + rom_byte_mask = 1; + search_result = 0; + + // if the last call was not the last one + if (!LastDeviceFlag) + { + // 1-Wire reset + if (!reset()) + { + // reset the search + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + return FALSE; + } + + // issue the search command + if (search_mode == true) { + write(0xF0); // NORMAL SEARCH + } else { + write(0xEC); // CONDITIONAL SEARCH + } + + // loop to do the search + do + { + // read a bit and its complement + id_bit = read_bit(); + cmp_id_bit = read_bit(); + + // check for no devices on 1-wire + if ((id_bit == 1) && (cmp_id_bit == 1)) + break; + else + { + // all devices coupled have 0 or 1 + if (id_bit != cmp_id_bit) + search_direction = id_bit; // bit write value for search + else + { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (id_bit_number < LastDiscrepancy) + search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (id_bit_number == LastDiscrepancy); + + // if 0 was picked then record its position in LastZero + if (search_direction == 0) + { + last_zero = id_bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy = last_zero; + } + } + + // set or clear the bit in the ROM byte rom_byte_number + // with mask rom_byte_mask + if (search_direction == 1) + ROM_NO[rom_byte_number] |= rom_byte_mask; + else + ROM_NO[rom_byte_number] &= ~rom_byte_mask; + + // serial number search direction write bit + write_bit(search_direction); + + // increment the byte counter id_bit_number + // and shift the mask rom_byte_mask + id_bit_number++; + rom_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask + if (rom_byte_mask == 0) + { + rom_byte_number++; + rom_byte_mask = 1; + } + } + } + while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 + + // if the search was successful then + if (!(id_bit_number < 65)) + { + // search successful so set LastDiscrepancy,LastDeviceFlag,search_result + LastDiscrepancy = last_zero; + + // check for last device + if (LastDiscrepancy == 0) + LastDeviceFlag = TRUE; + + search_result = TRUE; + } + } + + // if no device found then reset counters so next 'search' will be like a first + if (!search_result || !ROM_NO[0]) + { + LastDiscrepancy = 0; + LastDeviceFlag = FALSE; + LastFamilyDiscrepancy = 0; + search_result = FALSE; + } else { + for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; + } + return search_result; + } + +#endif + +#if ONEWIRE_CRC +// The 1-Wire CRC scheme is described in Maxim Application Note 27: +// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" +// + +#if ONEWIRE_CRC8_TABLE +// This table comes from Dallas sample code where it is freely reusable, +// though Copyright (C) 2000 Dallas Semiconductor Corporation +static const uint8_t PROGMEM dscrc_table[] = { + 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, + 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, + 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, + 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, + 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, + 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, + 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, + 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, + 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, + 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, + 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, + 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, + 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, + 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, + 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, + 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; + +// +// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM +// and the registers. (note: this might better be done without to +// table, it would probably be smaller and certainly fast enough +// compared to all those delayMicrosecond() calls. But I got +// confused, so I use this table from the examples.) +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); + } + return crc; +} +#else +// +// Compute a Dallas Semiconductor 8 bit CRC directly. +// this is much slower, but much smaller, than the lookup table. +// +uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + while (len--) { + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } + } + return crc; +} +#endif + +#if ONEWIRE_CRC16 +bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc) +{ + crc = ~crc16(input, len, crc); + return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; +} + +uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) +{ + static const uint8_t oddparity[16] = + { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + + for (uint16_t i = 0 ; i < len ; i++) { + // Even though we're just copying a byte from the input, + // we'll be doing 16-bit computation with it. + uint16_t cdata = input[i]; + cdata = (cdata ^ crc) & 0xff; + crc >>= 8; + + if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) + crc ^= 0xC001; + + cdata <<= 6; + crc ^= cdata; + cdata <<= 1; + crc ^= cdata; + } + return crc; +} +#endif + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.h b/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.h new file mode 100644 index 0000000..8753e91 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/OneWire.h @@ -0,0 +1,367 @@ +#ifndef OneWire_h +#define OneWire_h + +#include + +#if ARDUINO >= 100 +#include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc +#else +#include "WProgram.h" // for delayMicroseconds +#include "pins_arduino.h" // for digitalPinToBitMask, etc +#endif + +// You can exclude certain features from OneWire. In theory, this +// might save some space. In practice, the compiler automatically +// removes unused code (technically, the linker, using -fdata-sections +// and -ffunction-sections when compiling, and Wl,--gc-sections +// when linking), so most of these will not result in any code size +// reduction. Well, unless you try to use the missing features +// and redesign your program to not need them! ONEWIRE_CRC8_TABLE +// is the exception, because it selects a fast but large algorithm +// or a small but slow algorithm. + +// you can exclude onewire_search by defining that to 0 +#ifndef ONEWIRE_SEARCH +#define ONEWIRE_SEARCH 1 +#endif + +// You can exclude CRC checks altogether by defining this to 0 +#ifndef ONEWIRE_CRC +#define ONEWIRE_CRC 1 +#endif + +// Select the table-lookup method of computing the 8-bit CRC +// by setting this to 1. The lookup table enlarges code size by +// about 250 bytes. It does NOT consume RAM (but did in very +// old versions of OneWire). If you disable this, a slower +// but very compact algorithm is used. +#ifndef ONEWIRE_CRC8_TABLE +#define ONEWIRE_CRC8_TABLE 1 +#endif + +// You can allow 16-bit CRC checks by defining this to 1 +// (Note that ONEWIRE_CRC must also be 1.) +#ifndef ONEWIRE_CRC16 +#define ONEWIRE_CRC16 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif + +// Platform specific I/O definitions + +#if defined(__AVR__) +#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM asm("r30") +#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) + +#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (1) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (*((base)+512)) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) + +#elif defined(__MKL26Z64__) +#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint8_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask)) +#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask)) +#define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask)) + +#elif defined(__SAM3X8E__) +// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. +// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268 +// If you have trouble with OneWire on Arduino Due, please check the +// status of delayMicroseconds() before reporting a bug in OneWire! +#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask)) +#ifndef PROGMEM +#define PROGMEM +#endif +#ifndef pgm_read_byte +#define pgm_read_byte(addr) (*(const uint8_t *)(addr)) +#endif + +#elif defined(__PIC32MX__) +#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 +#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08 +#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04 +#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24 +#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 + +#elif defined(ARDUINO_ARCH_ESP8266) +#define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO) +#define PIN_TO_BITMASK(pin) (1 << pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS +#define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS +#define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS +#define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS +#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS + +#elif defined(__SAMD21G18A__) +#define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) +#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) +#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask)) +#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask)) +#define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask)) +#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask)) + +#elif defined(RBL_NRF51822) +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM +#define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin) +#define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin) +#define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin) +#define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL) +#define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin) + +#elif defined(__arc__) /* Arduino101/Genuino101 specifics */ + +#include "scss_registers.h" +#include "portable.h" +#include "avr/pgmspace.h" + +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 + +/* GPIO registers base address */ +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +#define IO_REG_TYPE uint32_t +#define IO_REG_ASM + +static inline __attribute__((always_inline)) +IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + IO_REG_TYPE ret; + if (SS_GPIO == GPIO_TYPE(pin)) { + ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); + } else { + ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); + } + return ((ret >> GPIO_ID(pin)) & 0x01); +} + +static inline __attribute__((always_inline)) +void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + } else { + MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin)); + } +} + +static inline __attribute__((always_inline)) +void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) +{ + if (SS_GPIO == GPIO_TYPE(pin)) { + WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base); + } else { + MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin)); + } +} + +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) + +#else +#define PIN_TO_BASEREG(pin) (0) +#define PIN_TO_BITMASK(pin) (pin) +#define IO_REG_TYPE unsigned int +#define IO_REG_ASM +#define DIRECT_READ(base, pin) digitalRead(pin) +#define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW) +#define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH) +#define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT) +#define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT) +#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture." + +#endif + + +class OneWire +{ + private: + IO_REG_TYPE bitmask; + volatile IO_REG_TYPE *baseReg; + +#if ONEWIRE_SEARCH + // global search state + unsigned char ROM_NO[8]; + uint8_t LastDiscrepancy; + uint8_t LastFamilyDiscrepancy; + uint8_t LastDeviceFlag; +#endif + + public: + OneWire( uint8_t pin); + + // Perform a 1-Wire reset cycle. Returns 1 if a device responds + // with a presence pulse. Returns 0 if there is no device or the + // bus is shorted or otherwise held low for more than 250uS + uint8_t reset(void); + + // Issue a 1-Wire rom select command, you do the reset first. + void select(const uint8_t rom[8]); + + // Issue a 1-Wire rom skip command, to address all on bus. + void skip(void); + + // Write a byte. If 'power' is one then the wire is held high at + // the end for parasitically powered devices. You are responsible + // for eventually depowering it by calling depower() or doing + // another read or write. + void write(uint8_t v, uint8_t power = 0); + + void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); + + // Read a byte. + uint8_t read(void); + + void read_bytes(uint8_t *buf, uint16_t count); + + // Write a bit. The bus is always left powered at the end, see + // note in write() about that. + void write_bit(uint8_t v); + + // Read a bit. + uint8_t read_bit(void); + + // Stop forcing power onto the bus. You only need to do this if + // you used the 'power' flag to write() or used a write_bit() call + // and aren't about to do another read or write. You would rather + // not leave this powered if you don't have to, just in case + // someone shorts your bus. + void depower(void); + +#if ONEWIRE_SEARCH + // Clear the search state so that if will start from the beginning again. + void reset_search(); + + // Setup the search to find the device type 'family_code' on the next call + // to search(*newAddr) if it is present. + void target_search(uint8_t family_code); + + // Look for the next device. Returns 1 if a new address has been + // returned. A zero might mean that the bus is shorted, there are + // no devices, or you have already retrieved all of them. It + // might be a good idea to check the CRC to make sure you didn't + // get garbage. The order is deterministic. You will always get + // the same devices in the same order. + uint8_t search(uint8_t *newAddr, bool search_mode = true); +#endif + +#if ONEWIRE_CRC + // Compute a Dallas Semiconductor 8 bit CRC, these are used in the + // ROM and scratchpad registers. + static uint8_t crc8(const uint8_t *addr, uint8_t len); + +#if ONEWIRE_CRC16 + // Compute the 1-Wire CRC16 and compare it against the received CRC. + // Example usage (reading a DS2408): + // // Put everything in a buffer so we can compute the CRC easily. + // uint8_t buf[13]; + // buf[0] = 0xF0; // Read PIO Registers + // buf[1] = 0x88; // LSB address + // buf[2] = 0x00; // MSB address + // WriteBytes(net, buf, 3); // Write 3 cmd bytes + // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 + // if (!CheckCRC16(buf, 11, &buf[11])) { + // // Handle error. + // } + // + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param inverted_crc - The two CRC16 bytes in the received data. + // This should just point into the received data, + // *not* at a 16-bit integer. + // @param crc - The crc starting value (optional) + // @return True, iff the CRC matches. + static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); + + // Compute a Dallas Semiconductor 16 bit CRC. This is required to check + // the integrity of data received from many 1-Wire devices. Note that the + // CRC computed here is *not* what you'll get from the 1-Wire network, + // for two reasons: + // 1) The CRC is transmitted bitwise inverted. + // 2) Depending on the endian-ness of your processor, the binary + // representation of the two-byte return value may have a different + // byte order than the two bytes you get from 1-Wire. + // @param input - Array of bytes to checksum. + // @param len - How many bytes to use. + // @param crc - The crc starting value (optional) + // @return The CRC16, as defined by Dallas Semiconductor. + static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); +#endif +#endif +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.cpp b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.cpp new file mode 100644 index 0000000..c494105 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in WiFi101Stream.h to avoid linker issues. Legacy WiFi and modern WiFi101 + * both define WiFiClass which will cause linker errors whenever Firmata.h is included. + */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.h b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.h new file mode 100644 index 0000000..eb95cf5 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFi101Stream.h @@ -0,0 +1,259 @@ +/* + WiFi101Stream.h + An Arduino Stream that wraps an instance of a WiFi101 server. For use + with Arduino WiFi 101 shield, Arduino MKR1000 and other boards and + shields that are compatible with the Arduino WiFi101 library. + + Copyright (C) 2015-2016 Jesse Frush. 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. + */ + +#ifndef WIFI101_STREAM_H +#define WIFI101_STREAM_H + +#include +#include +#include + + +class WiFi101Stream : public Stream +{ +private: + WiFiServer _server = WiFiServer(23); + WiFiClient _client; + + //configuration members + IPAddress _local_ip; + uint16_t _port = 0; + uint8_t _key_idx = 0; //WEP + const char *_key = nullptr; //WEP + const char *_passphrase = nullptr; //WPA + char *_ssid = nullptr; + + inline int connect_client() + { + if( !( _client && _client.connected() ) ) + { + WiFiClient newClient = _server.available(); + if( !newClient ) + { + return 0; + } + + _client = newClient; + } + return 1; + } + + inline bool is_ready() + { + uint8_t status = WiFi.status(); + return !( status == WL_NO_SHIELD || status == WL_CONNECTED ); + } + +public: + WiFi101Stream() {}; + + // allows another way to configure a static IP before begin is called + inline void config(IPAddress local_ip) + { + _local_ip = local_ip; + WiFi.config( local_ip ); + } + + // get DCHP IP + inline IPAddress localIP() + { + return WiFi.localIP(); + } + + inline bool maintain() + { + if( connect_client() ) return true; + + stop(); + int result = 0; + if( WiFi.status() != WL_CONNECTED ) + { + if( _local_ip ) + { + WiFi.config( _local_ip ); + } + + if( _passphrase ) + { + result = WiFi.begin( _ssid, _passphrase); + } + else if( _key_idx && _key ) + { + result = WiFi.begin( _ssid, _key_idx, _key ); + } + else + { + result = WiFi.begin( _ssid ); + } + } + if( result == 0 ) return false; + + _server = WiFiServer( _port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Connection functions with DHCP + ******************************************************************************/ + + //OPEN networks + inline int begin(char *ssid, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + int result = WiFi.begin( ssid ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WEP-encrypted networks + inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _key_idx = key_idx; + _key = key; + + int result = WiFi.begin( ssid, key_idx, key ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WPA-encrypted networks + inline int begin(char *ssid, const char *passphrase, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _passphrase = passphrase; + + int result = WiFi.begin( ssid, passphrase); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Connection functions without DHCP + ******************************************************************************/ + + //OPEN networks with static IP + inline int begin(char *ssid, IPAddress local_ip, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WEP-encrypted networks with static IP + inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + _key_idx = key_idx; + _key = key; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid, key_idx, key ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WPA-encrypted networks with static IP + inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + _passphrase = passphrase; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid, passphrase); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Stream implementations + ******************************************************************************/ + + inline int available() + { + return connect_client() ? _client.available() : 0; + } + + inline void flush() + { + if( _client ) _client.flush(); + } + + inline int peek() + { + return connect_client() ? _client.peek(): 0; + } + + inline int read() + { + return connect_client() ? _client.read() : -1; + } + + inline void stop() + { + _client.stop(); + } + + inline size_t write(uint8_t byte) + { + if( connect_client() ) _client.write( byte ); + } +}; + +#endif //WIFI101_STREAM_H diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.cpp b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.cpp new file mode 100644 index 0000000..f7e0fca --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.cpp @@ -0,0 +1,4 @@ +/* + * Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both + * define WiFiClass which will cause linker errors whenever Firmata.h is included. + */ diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.h b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.h new file mode 100644 index 0000000..fdcb483 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/WiFiStream.h @@ -0,0 +1,258 @@ +/* + WiFiStream.h + An Arduino Stream that wraps an instance of a WiFi server. For use + with legacy Arduino WiFi shield and other boards and sheilds that + are compatible with the Arduino WiFi library. + + Copyright (C) 2015-2016 Jesse Frush. 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. + */ + +#ifndef WIFI_STREAM_H +#define WIFI_STREAM_H + +#include +#include +#include + +class WiFiStream : public Stream +{ +private: + WiFiServer _server = WiFiServer(23); + WiFiClient _client; + + //configuration members + IPAddress _local_ip; + uint16_t _port = 0; + uint8_t _key_idx = 0; //WEP + const char *_key = nullptr; //WEP + const char *_passphrase = nullptr; //WPA + char *_ssid = nullptr; + + inline int connect_client() + { + if( !( _client && _client.connected() ) ) + { + WiFiClient newClient = _server.available(); + if( !newClient ) + { + return 0; + } + + _client = newClient; + } + return 1; + } + + inline bool is_ready() + { + uint8_t status = WiFi.status(); + return !( status == WL_NO_SHIELD || status == WL_CONNECTED ); + } + +public: + WiFiStream() {}; + + // allows another way to configure a static IP before begin is called + inline void config(IPAddress local_ip) + { + _local_ip = local_ip; + WiFi.config( local_ip ); + } + + // get DCHP IP + inline IPAddress localIP() + { + return WiFi.localIP(); + } + + inline bool maintain() + { + if( connect_client() ) return true; + + stop(); + int result = 0; + if( WiFi.status() != WL_CONNECTED ) + { + if( _local_ip ) + { + WiFi.config( _local_ip ); + } + + if( _passphrase ) + { + result = WiFi.begin( _ssid, _passphrase); + } + else if( _key_idx && _key ) + { + result = WiFi.begin( _ssid, _key_idx, _key ); + } + else + { + result = WiFi.begin( _ssid ); + } + } + if( result == 0 ) return false; + + _server = WiFiServer( _port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Connection functions with DHCP + ******************************************************************************/ + + //OPEN networks + inline int begin(char *ssid, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + int result = WiFi.begin( ssid ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WEP-encrypted networks + inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _key_idx = key_idx; + _key = key; + + int result = WiFi.begin( ssid, key_idx, key ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WPA-encrypted networks + inline int begin(char *ssid, const char *passphrase, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _passphrase = passphrase; + + int result = WiFi.begin( ssid, passphrase); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Connection functions without DHCP + ******************************************************************************/ + + //OPEN networks with static IP + inline int begin(char *ssid, IPAddress local_ip, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WEP-encrypted networks with static IP + inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + _key_idx = key_idx; + _key = key; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid, key_idx, key ); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + + //WPA-encrypted networks with static IP + inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) + { + if( !is_ready() ) return 0; + + _ssid = ssid; + _port = port; + _local_ip = local_ip; + _passphrase = passphrase; + + WiFi.config( local_ip ); + int result = WiFi.begin( ssid, passphrase); + if( result == 0 ) return 0; + + _server = WiFiServer( port ); + _server.begin(); + return result; + } + +/****************************************************************************** + * Stream implementations + ******************************************************************************/ + + inline int available() + { + return connect_client() ? _client.available() : 0; + } + + inline void flush() + { + if( _client ) _client.flush(); + } + + inline int peek() + { + return connect_client() ? _client.peek(): 0; + } + + inline int read() + { + return connect_client() ? _client.read() : -1; + } + + inline void stop() + { + _client.stop(); + } + + inline size_t write(uint8_t byte) + { + if( connect_client() ) _client.write( byte ); + } +}; + +#endif //WIFI_STREAM_H diff --git a/libraries/FirmataWithDeviceFeature/src-features/utility/firmataDebug.h b/libraries/FirmataWithDeviceFeature/src-features/utility/firmataDebug.h new file mode 100644 index 0000000..6e364b0 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src-features/utility/firmataDebug.h @@ -0,0 +1,14 @@ +#ifndef FIRMATA_DEBUG_H +#define FIRMATA_DEBUG_H + +#ifdef SERIAL_DEBUG + #define DEBUG_BEGIN(baud) Serial.begin(baud); while(!Serial) {;} + #define DEBUG_PRINTLN(x) Serial.println (x) + #define DEBUG_PRINT(x) Serial.print (x) +#else + #define DEBUG_BEGIN(baud) + #define DEBUG_PRINTLN(x) + #define DEBUG_PRINT(x) +#endif + +#endif /* FIRMATA_DEBUG_H */ diff --git a/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.cpp b/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.cpp new file mode 100644 index 0000000..b4b43b2 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.cpp @@ -0,0 +1,709 @@ +/* + ConfigurableFirmata.pp - ConfigurableFirmata library v2.8.2 - 2016-2-16 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-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. +*/ + +//****************************************************************************** +//* Includes +//****************************************************************************** + +#include "ConfigurableFirmata.h" +#include "HardwareSerial.h" + +extern "C" { +#include +#include +} + +//****************************************************************************** +//* Support Functions +//****************************************************************************** + +/** + * Split a 16-bit byte into two 7-bit values and write each value. + * @param value The 16-bit value to be split and written separately. + */ +void FirmataClass::sendValueAsTwo7bitBytes(int value) +{ + FirmataStream->write(value & B01111111); // LSB + FirmataStream->write(value >> 7 & B01111111); // MSB +} + +/** + * A helper method to write the beginning of a Sysex message transmission. + */ +void FirmataClass::startSysex(void) +{ + FirmataStream->write(START_SYSEX); +} + +/** + * A helper method to write the end of a Sysex message transmission. + */ +void FirmataClass::endSysex(void) +{ + FirmataStream->write(END_SYSEX); +} + +//****************************************************************************** +//* Constructors +//****************************************************************************** + +/** + * The Firmata class. + * An instance named "Firmata" is created automatically for the user. + */ +FirmataClass::FirmataClass() +{ + firmwareVersionCount = 0; + firmwareVersionVector = 0; + systemReset(); +} + +//****************************************************************************** +//* Public Methods +//****************************************************************************** + +/** + * Initialize the default Serial transport at the default baud of 57600. + */ +void FirmataClass::begin(void) +{ + begin(57600); +} + +/** + * Initialize the default Serial transport and override the default baud. + * Sends the protocol version to the host application followed by the firmware version and name. + * blinkVersion is also called. To skip the call to blinkVersion, call Firmata.disableBlinkVersion() + * before calling Firmata.begin(baud). + * @param speed The baud to use. 57600 baud is the default value. + */ +void FirmataClass::begin(long speed) +{ + Serial.begin(speed); + FirmataStream = &Serial; + blinkVersion(); + printVersion(); // send the protocol version + printFirmwareVersion(); // send the firmware name and version +} + +/** + * Reassign the Firmata stream transport. + * @param s A reference to the Stream transport object. This can be any type of + * transport that implements the Stream interface. Some examples include Ethernet, WiFi + * and other UARTs on the board (Serial1, Serial2, etc). + */ +void FirmataClass::begin(Stream &s) +{ + FirmataStream = &s; + // do not call blinkVersion() here because some hardware such as the + // Ethernet shield use pin 13 + printVersion(); // send the protocol version + printFirmwareVersion(); // send the firmware name and version +} + +/** + * Send the Firmata protocol version to the Firmata host application. + */ +void FirmataClass::printVersion(void) +{ + FirmataStream->write(REPORT_VERSION); + FirmataStream->write(FIRMATA_PROTOCOL_MAJOR_VERSION); + FirmataStream->write(FIRMATA_PROTOCOL_MINOR_VERSION); +} + +/** + * Blink the Firmata protocol version to the onboard LEDs (if the board has an onboard LED). + * If VERSION_BLINK_PIN is not defined in Boards.h for a particular board, then this method + * does nothing. + * The first series of flashes indicates the firmware major version (2 flashes = 2). + * The second series of flashes indicates the firmware minor version (5 flashes = 5). + */ +void FirmataClass::blinkVersion(void) +{ +#if defined(VERSION_BLINK_PIN) + if (blinkVersionDisabled) return; + // flash the pin with the protocol version + pinMode(VERSION_BLINK_PIN, OUTPUT); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MAJOR_VERSION, 40, 210); + delay(250); + strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MINOR_VERSION, 40, 210); + delay(125); +#endif +} + +/** + * Provides a means to disable the version blink sequence on the onboard LED, trimming startup + * time by a couple of seconds. + * Call this before Firmata.begin(). It only applies when using the default Serial transport. + */ +void FirmataClass::disableBlinkVersion() +{ + blinkVersionDisabled = true; +} + +/** + * Sends the firmware name and version to the Firmata host application. The major and minor version + * numbers are the first 2 bytes in the message. The following bytes are the characters of the + * firmware name. + */ +void FirmataClass::printFirmwareVersion(void) +{ + byte i; + + if (firmwareVersionCount) { // make sure that the name has been set before reporting + startSysex(); + FirmataStream->write(REPORT_FIRMWARE); + FirmataStream->write(firmwareVersionVector[0]); // major version number + FirmataStream->write(firmwareVersionVector[1]); // minor version number + for (i = 2; i < firmwareVersionCount; ++i) { + sendValueAsTwo7bitBytes(firmwareVersionVector[i]); + } + endSysex(); + } +} + +/** + * Sets the name and version of the firmware. This is not the same version as the Firmata protocol + * (although at times the firmware version and protocol version may be the same number). + * @param name A pointer to the name char array + * @param major The major version number + * @param minor The minor version number + */ +void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte minor) +{ + const char *firmwareName; + const char *extension; + + // parse out ".cpp" and "applet/" that comes from using __FILE__ + extension = strstr(name, ".cpp"); + firmwareName = strrchr(name, '/'); + + if (!firmwareName) { + // windows + firmwareName = strrchr(name, '\\'); + } + if (!firmwareName) { + // user passed firmware name + firmwareName = name; + } else { + firmwareName ++; + } + + if (!extension) { + firmwareVersionCount = strlen(firmwareName) + 2; + } else { + firmwareVersionCount = extension - firmwareName + 2; + } + + // in case anyone calls setFirmwareNameAndVersion more than once + free(firmwareVersionVector); + + firmwareVersionVector = (byte *) malloc(firmwareVersionCount + 1); + firmwareVersionVector[firmwareVersionCount] = 0; + firmwareVersionVector[0] = major; + firmwareVersionVector[1] = minor; + strncpy((char *)firmwareVersionVector + 2, firmwareName, firmwareVersionCount - 2); +} + +//------------------------------------------------------------------------------ +// Input Stream Handling + +/** + * A wrapper for Stream::available() + * @return The number of bytes remaining in the input stream buffer. + */ +int FirmataClass::available(void) +{ + return FirmataStream->available(); +} + +/** + * Process incoming sysex messages. Handles REPORT_FIRMWARE and STRING_DATA internally. + * Calls callback function for STRING_DATA and all other sysex messages. + * @private + */ +void FirmataClass::processSysexMessage(void) +{ + switch (storedInputData[0]) { //first byte in buffer is command + case REPORT_FIRMWARE: + printFirmwareVersion(); + break; + case STRING_DATA: + if (currentStringCallback) { + byte bufferLength = (sysexBytesRead - 1) / 2; + byte i = 1; + byte j = 0; + while (j < bufferLength) { + // The string length will only be at most half the size of the + // stored input buffer so we can decode the string within the buffer. + storedInputData[j] = storedInputData[i]; + i++; + storedInputData[j] += (storedInputData[i] << 7); + i++; + j++; + } + // Make sure string is null terminated. This may be the case for data + // coming from client libraries in languages that don't null terminate + // strings. + if (storedInputData[j - 1] != '\0') { + storedInputData[j] = '\0'; + } + (*currentStringCallback)((char *)&storedInputData[0]); + } + break; + default: + if (currentSysexCallback) + (*currentSysexCallback)(storedInputData[0], sysexBytesRead - 1, storedInputData + 1); + } +} + + +/** + * Read a single int from the input stream. If the value is not = -1, pass it on to parse(byte) + */ +void FirmataClass::processInput(void) +{ + int inputData = FirmataStream->read(); // this is 'int' to handle -1 when no data + if (inputData != -1) { + parse(inputData); + } +} + +/** + * Parse data from the input stream. + * @param inputData A single byte to be added to the parser. + */ +void FirmataClass::parse(byte inputData) +{ + int command; + + // TODO make sure it handles -1 properly + + if (parsingSysex) { + if (inputData == END_SYSEX) { + //stop sysex byte + parsingSysex = false; + //fire off handler function + processSysexMessage(); + } else { + //normal data byte - add to buffer + storedInputData[sysexBytesRead] = inputData; + sysexBytesRead++; + } + } else if ( (waitForData > 0) && (inputData < 128) ) { + waitForData--; + storedInputData[waitForData] = inputData; + if ( (waitForData == 0) && executeMultiByteCommand ) { // got the whole message + switch (executeMultiByteCommand) { + case ANALOG_MESSAGE: + if (currentAnalogCallback) { + (*currentAnalogCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case DIGITAL_MESSAGE: + if (currentDigitalCallback) { + (*currentDigitalCallback)(multiByteChannel, + (storedInputData[0] << 7) + + storedInputData[1]); + } + break; + case SET_PIN_MODE: + setPinMode(storedInputData[1], storedInputData[0]); + break; + case SET_DIGITAL_PIN_VALUE: + if (currentPinValueCallback) + (*currentPinValueCallback)(storedInputData[1], storedInputData[0]); + break; + case REPORT_ANALOG: + if (currentReportAnalogCallback) + (*currentReportAnalogCallback)(multiByteChannel, storedInputData[0]); + break; + case REPORT_DIGITAL: + if (currentReportDigitalCallback) + (*currentReportDigitalCallback)(multiByteChannel, storedInputData[0]); + break; + } + executeMultiByteCommand = 0; + } + } else { + // remove channel info from command byte if less than 0xF0 + if (inputData < 0xF0) { + command = inputData & 0xF0; + multiByteChannel = inputData & 0x0F; + } else { + command = inputData; + // commands in the 0xF* range don't use channel data + } + switch (command) { + case ANALOG_MESSAGE: + case DIGITAL_MESSAGE: + case SET_PIN_MODE: + case SET_DIGITAL_PIN_VALUE: + waitForData = 2; // two data bytes needed + executeMultiByteCommand = command; + break; + case REPORT_ANALOG: + case REPORT_DIGITAL: + waitForData = 1; // one data byte needed + executeMultiByteCommand = command; + break; + case START_SYSEX: + parsingSysex = true; + sysexBytesRead = 0; + break; + case SYSTEM_RESET: + systemReset(); + break; + case REPORT_VERSION: + Firmata.printVersion(); + break; + } + } +} + +/** + * @return Returns true if the parser is actively parsing data. + */ +boolean FirmataClass::isParsingMessage(void) +{ + return (waitForData > 0 || parsingSysex); +} + +/** + * @return Returns true if the SYSTEM_RESET message is being executed + */ +boolean FirmataClass::isResetting(void) +{ + return resetting; +} + +//------------------------------------------------------------------------------ +// Output Stream Handling + +/** + * Send an analog message to the Firmata host application. The range of pins is limited to [0..15] + * when using the ANALOG_MESSAGE. The maximum value of the ANALOG_MESSAGE is limited to 14 bits + * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG + * message. + * @param pin The analog pin to send the value of (limited to pins 0 - 15). + * @param value The value of the analog pin (0 - 1024 for 10-bit analog, 0 - 4096 for 12-bit, etc). + * The maximum value is 14-bits (16384). + */ +void FirmataClass::sendAnalog(byte pin, int value) +{ + // pin can only be 0-15, so chop higher bits + FirmataStream->write(ANALOG_MESSAGE | (pin & 0xF)); + sendValueAsTwo7bitBytes(value); +} + +/* (intentionally left out asterix here) + * STUB - NOT IMPLEMENTED + * Send a single digital pin value to the Firmata host application. + * @param pin The digital pin to send the value of. + * @param value The value of the pin. + */ +void FirmataClass::sendDigital(byte pin, int value) +{ + /* TODO add single pin digital messages to the protocol, this needs to + * track the last digital data sent so that it can be sure to change just + * one bit in the packet. This is complicated by the fact that the + * numbering of the pins will probably differ on Arduino, Wiring, and + * other boards. The DIGITAL_MESSAGE sends 14 bits at a time, but it is + * probably easier to send 8 bit ports for any board with more than 14 + * digital pins. + */ + + // TODO: the digital message should not be sent on the serial port every + // time sendDigital() is called. Instead, it should add it to an int + // which will be sent on a schedule. If a pin changes more than once + // before the digital message is sent on the serial port, it should send a + // digital message for each change. + + // if(value == 0) + // sendDigitalPortPair(); +} + + +/** + * Send an 8-bit port in a single digital message (protocol v2 and later). + * Send 14-bits in a single digital message (protocol v1). + * @param portNumber The port number to send. Note that this is not the same as a "port" on the + * physical microcontroller. Ports are defined in order per every 8 pins in ascending order + * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc. + * @param portData The value of the port. The value of each pin in the port is represented by a bit. + */ +void FirmataClass::sendDigitalPort(byte portNumber, int portData) +{ + FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF)); + FirmataStream->write((byte)portData % 128); // Tx bits 0-6 + FirmataStream->write(portData >> 7); // Tx bits 7-13 +} + +/** + * Send a sysex message where all values after the command byte are packet as 2 7-bit bytes + * (this is not always the case so this function is not always used to send sysex messages). + * @param command The sysex command byte. + * @param bytec The number of data bytes in the message (excludes start, command and end bytes). + * @param bytev A pointer to the array of data bytes to send in the message. + */ +void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev) +{ + byte i; + startSysex(); + FirmataStream->write(command); + for (i = 0; i < bytec; i++) { + sendValueAsTwo7bitBytes(bytev[i]); + } + endSysex(); +} + +/** + * Send a string to the Firmata host application. + * @param command Must be STRING_DATA + * @param string A pointer to the char string + */ +void FirmataClass::sendString(byte command, const char *string) +{ + sendSysex(command, strlen(string), (byte *)string); +} + +/** + * Send a string to the Firmata host application. + * @param string A pointer to the char string + */ +void FirmataClass::sendString(const char *string) +{ + sendString(STRING_DATA, string); +} + +/** + * A wrapper for Stream::available(). + * Write a single byte to the output stream. + * @param c The byte to be written. + */ +void FirmataClass::write(byte c) +{ + FirmataStream->write(c); +} + + +/** + * Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE, + * DIGITAL_MESSAGE, REPORT_ANALOG, REPORT DIGITAL, SET_PIN_MODE and SET_DIGITAL_PIN_VALUE). + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the callback function to attach. + */ +void FirmataClass::attach(byte command, callbackFunction newFunction) +{ + switch (command) { + case ANALOG_MESSAGE: currentAnalogCallback = newFunction; break; + case DIGITAL_MESSAGE: currentDigitalCallback = newFunction; break; + case REPORT_ANALOG: currentReportAnalogCallback = newFunction; break; + case REPORT_DIGITAL: currentReportDigitalCallback = newFunction; break; + case SET_PIN_MODE: currentPinModeCallback = newFunction; break; + case SET_DIGITAL_PIN_VALUE: currentPinValueCallback = newFunction; break; + } +} + +/** + * Attach a callback function for the SYSTEM_RESET command. + * @param command Must be set to SYSTEM_RESET or it will be ignored. + * @param newFunction A reference to the system reset callback function to attach. + */ +void FirmataClass::attach(byte command, systemResetCallbackFunction newFunction) +{ + switch (command) { + case SYSTEM_RESET: currentSystemResetCallback = newFunction; break; + } +} + +/** + * Attach a callback function for the STRING_DATA command. + * @param command Must be set to STRING_DATA or it will be ignored. + * @param newFunction A reference to the string callback function to attach. + */ +void FirmataClass::attach(byte command, stringCallbackFunction newFunction) +{ + switch (command) { + case STRING_DATA: currentStringCallback = newFunction; break; + } +} + +/** + * Attach a generic sysex callback function to sysex command. + * @param command The ID of the command to attach a callback function to. + * @param newFunction A reference to the sysex callback function to attach. + */ +void FirmataClass::attach(byte command, sysexCallbackFunction newFunction) +{ + currentSysexCallback = newFunction; +} + +/** + * Detach a callback function for a specified command (such as SYSTEM_RESET, STRING_DATA, + * ANALOG_MESSAGE, DIGITAL_MESSAGE, etc). + * @param command The ID of the command to detatch the callback function from. + */ +void FirmataClass::detach(byte command) +{ + switch (command) { + case SYSTEM_RESET: currentSystemResetCallback = NULL; break; + case STRING_DATA: currentStringCallback = NULL; break; + case START_SYSEX: currentSysexCallback = NULL; break; + default: + attach(command, (callbackFunction)NULL); + } +} + +/** + * Detach a callback function for a delayed task when using FirmataScheduler + * @see FirmataScheduler + * @param newFunction A reference to the delay task callback function to attach. + */ +void FirmataClass::attachDelayTask(delayTaskCallbackFunction newFunction) +{ + delayTaskCallback = newFunction; +} + +/** + * Call the delayTask callback function when using FirmataScheduler. Must first attach a callback + * using attachDelayTask. + * @see FirmataScheduler + * @param delay The amount of time to delay in milliseconds. + */ +void FirmataClass::delayTask(long delay) +{ + if (delayTaskCallback) { + (*delayTaskCallback)(delay); + } +} + +/** + * @param pin The pin to get the configuration of. + * @return The configuration of the specified pin. + */ +byte FirmataClass::getPinMode(byte pin) +{ + return pinConfig[pin]; +} + +/** + * Set the pin mode/configuration. The pin configuration (or mode) in Firmata represents the + * current function of the pin. Examples are digital input or output, analog input, pwm, i2c, + * serial (uart), etc. + * @param pin The pin to configure. + * @param config The configuration value for the specified pin. + */ +void FirmataClass::setPinMode(byte pin, byte config) +{ + if (pinConfig[pin] == PIN_MODE_IGNORE) + return; + pinState[pin] = 0; + pinConfig[pin] = config; + if (currentPinModeCallback) + (*currentPinModeCallback)(pin, config); +} + +/** + * @param pin The pin to get the state of. + * @return The state of the specified pin. + */ +int FirmataClass::getPinState(byte pin) +{ + return pinState[pin]; +} + +/** + * Set the pin state. The pin state of an output pin is the pin value. The state of an + * input pin is 0, unless the pin has it's internal pull up resistor enabled, then the value is 1. + * @param pin The pin to set the state of + * @param state Set the state of the specified pin + */ +void FirmataClass::setPinState(byte pin, int state) +{ + pinState[pin] = state; +} + + +// sysex callbacks +/* + * this is too complicated for analogReceive, but maybe for Sysex? + void FirmataClass::attachSysex(sysexFunction newFunction) + { + byte i; + byte tmpCount = analogReceiveFunctionCount; + analogReceiveFunction* tmpArray = analogReceiveFunctionArray; + analogReceiveFunctionCount++; + analogReceiveFunctionArray = (analogReceiveFunction*) calloc(analogReceiveFunctionCount, sizeof(analogReceiveFunction)); + for(i = 0; i < tmpCount; i++) { + analogReceiveFunctionArray[i] = tmpArray[i]; + } + analogReceiveFunctionArray[tmpCount] = newFunction; + free(tmpArray); + } +*/ + +//****************************************************************************** +//* Private Methods +//****************************************************************************** + +/** + * Resets the system state upon a SYSTEM_RESET message from the host software. + * @private + */ +void FirmataClass::systemReset(void) +{ + resetting = true; + byte i; + + waitForData = 0; // this flag says the next serial input will be data + executeMultiByteCommand = 0; // execute this after getting multi-byte data + multiByteChannel = 0; // channel data for multiByteCommands + + for (i = 0; i < MAX_DATA_BYTES; i++) { + storedInputData[i] = 0; + } + + parsingSysex = false; + sysexBytesRead = 0; + + if (currentSystemResetCallback) + (*currentSystemResetCallback)(); + + resetting = false; +} + +/** + * Flashing the pin for the version number + * @private + * @param pin The pin the LED is attached to. + * @param count The number of times to flash the LED. + * @param onInterval The number of milliseconds for the LED to be ON during each interval. + * @param offInterval The number of milliseconds for the LED to be OFF during each interval. + */ +void FirmataClass::strobeBlinkPin(byte pin, int count, int onInterval, int offInterval) +{ + byte i; + for (i = 0; i < count; i++) { + delay(offInterval); + digitalWrite(pin, HIGH); + delay(onInterval); + digitalWrite(pin, LOW); + } +} + +// make one instance for the user to use +FirmataClass Firmata; diff --git a/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.h b/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.h new file mode 100644 index 0000000..48c0fe5 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/ConfigurableFirmata.h @@ -0,0 +1,245 @@ +/* + ConfigurableFirmata.h - ConfigurableFirmata library v2.8.2 - 2016-2-16 + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-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. +*/ + +#ifndef Configurable_Firmata_h +#define Configurable_Firmata_h + +#include "utility/Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */ + +/* Version numbers for the protocol. The protocol is still changing, so these + * version numbers are important. + * Query using the REPORT_VERSION message. + */ +#define FIRMATA_PROTOCOL_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_PROTOCOL_MINOR_VERSION 5 // for backwards compatible changes +#define FIRMATA_PROTOCOL_BUGFIX_VERSION 1 // for bugfix releases + +/* + * Version numbers for the Firmata library. + * ConfigurableFirmata 2.8.1 implements version 2.5.1 of the Firmata protocol. + * The firmware version will not always equal the protocol version going forward. + * Query using the REPORT_FIRMWARE message. + */ +#define FIRMATA_FIRMWARE_MAJOR_VERSION 2 // for non-compatible changes +#define FIRMATA_FIRMWARE_MINOR_VERSION 9 // for backwards compatible changes +#define FIRMATA_FIRMWARE_BUGFIX_VERSION 3 // for bugfix releases + +// DEPRECATED as of ConfigurableFirmata v2.8.1. +// Use FIRMATA_PROTOCOL_[MAJOR|MINOR|BUGFIX]_VERSION instead. +#define FIRMATA_MAJOR_VERSION 2 +#define FIRMATA_MINOR_VERSION 5 +#define FIRMATA_BUGFIX_VERSION 1 +// DEPRECATED as of ConfigurableFirmata v2.8.1. +//Use FIRMATA_FIRMWARE_[MAJOR|MINOR|BUGFIX]_VERSION instead. +#define FIRMWARE_MAJOR_VERSION 2 +#define FIRMWARE_MINOR_VERSION 8 +#define FIRMWARE_BUGFIX_VERSION 2 + +#define MAX_DATA_BYTES 64 // max number of data bytes in incoming messages + +// Arduino 101 also defines SET_PIN_MODE as a macro in scss_registers.h +#ifdef SET_PIN_MODE +#undef SET_PIN_MODE +#endif + +// message command bytes (128-255/0x80-0xFF) +#define DIGITAL_MESSAGE 0x90 // send data for a digital pin +#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM) +#define REPORT_ANALOG 0xC0 // enable analog input by pin # +#define REPORT_DIGITAL 0xD0 // enable digital input by port pair +// +#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc +#define SET_DIGITAL_PIN_VALUE 0xF5 // set value of an individual digital pin +// +#define REPORT_VERSION 0xF9 // report protocol version +#define SYSTEM_RESET 0xFF // reset from MIDI +// +#define START_SYSEX 0xF0 // start a MIDI Sysex message +#define END_SYSEX 0xF7 // end a MIDI Sysex message + +// extended command set using sysex (0-127/0x00-0x7F) +/* 0x00-0x0F reserved for user-defined commands */ +#define SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards +#define ENCODER_DATA 0x61 // reply with encoders current positions +#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq +#define STRING_DATA 0x71 // a string message with 14-bits per char +#define STEPPER_DATA 0x72 // control a stepper motor +#define ONEWIRE_DATA 0x73 // send an OneWire read/write/reset/select/skip/search request +#define SHIFT_DATA 0x75 // a bitstream to/from a shift register +#define I2C_REQUEST 0x76 // send an I2C read/write request +#define I2C_REPLY 0x77 // a reply to an I2C read request +#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins +#define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin +#define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value +#define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value +#define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins +#define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution +#define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers +#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info +#define REPORT_FIRMWARE 0x79 // report name and version of the firmware +#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop +#define SCHEDULER_DATA 0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler +#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages +#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages + +// DeviceFirmata commands + +#define DEVICE_QUERY 0x30 // message requesting action from a device driver (DeviceFirmata) +#define DEVICE_RESPONSE 0x31 // message providing the device driver response (DeviceFirmata) + +// these are DEPRECATED to make the naming more consistent +#define FIRMATA_STRING 0x71 // same as STRING_DATA +#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST +#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY +#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL + +// pin modes +//#define INPUT 0x00 // defined in Arduino.h +//#define OUTPUT 0x01 // defined in Arduino.h +#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode +#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode +#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode +#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode +#define PIN_MODE_I2C 0x06 // pin included in I2C setup +#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire +#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor +#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders +#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication +#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin +#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse +#define TOTAL_PIN_MODES 13 +// DEPRECATED as of Firmata v2.5 +#define ANALOG 0x02 // same as PIN_MODE_ANALOG +#define PWM 0x03 // same as PIN_MODE_PWM +#define SERVO 0x04 // same as PIN_MODE_SERVO +#define SHIFT 0x05 // same as PIN_MODE_SHIFT +#define I2C 0x06 // same as PIN_MODE_I2C +#define ONEWIRE 0x07 // same as PIN_MODE_ONEWIRE +#define STEPPER 0x08 // same as PIN_MODE_STEPPER +#define ENCODER 0x09 // same as PIN_MODE_ENCODER +#define IGNORE 0x7F // same as PIN_MODE_IGNORE + +extern "C" { + // callback function types + typedef void (*callbackFunction)(byte, int); + typedef void (*systemResetCallbackFunction)(void); + typedef void (*stringCallbackFunction)(char *); + typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv); + typedef void (*delayTaskCallbackFunction)(long delay); +} + + +// TODO make it a subclass of a generic Serial/Stream base class +class FirmataClass +{ + public: + FirmataClass(); + /* Arduino constructors */ + void begin(); + void begin(long); + void begin(Stream &s); + /* querying functions */ + void printVersion(void); + void blinkVersion(void); + void printFirmwareVersion(void); + //void setFirmwareVersion(byte major, byte minor); // see macro below + void setFirmwareNameAndVersion(const char *name, byte major, byte minor); + void disableBlinkVersion(); + /* serial receive handling */ + int available(void); + void processInput(void); + void parse(unsigned char value); + boolean isParsingMessage(void); + boolean isResetting(void); + /* serial send handling */ + void sendAnalog(byte pin, int value); + void sendDigital(byte pin, int value); // TODO implement this + void sendDigitalPort(byte portNumber, int portData); + void sendString(const char *string); + void sendString(byte command, const char *string); + void sendSysex(byte command, byte bytec, byte *bytev); + void write(byte c); + /* attach & detach callback functions to messages */ + void attach(byte command, callbackFunction newFunction); + void attach(byte command, systemResetCallbackFunction newFunction); + void attach(byte command, stringCallbackFunction newFunction); + void attach(byte command, sysexCallbackFunction newFunction); + void detach(byte command); + /* delegate to Scheduler (if any) */ + void attachDelayTask(delayTaskCallbackFunction newFunction); + void delayTask(long delay); + /* access pin config */ + byte getPinMode(byte pin); + void setPinMode(byte pin, byte config); + /* access pin state */ + int getPinState(byte pin); + void setPinState(byte pin, int state); + + /* utility methods */ + void sendValueAsTwo7bitBytes(int value); + void startSysex(void); + void endSysex(void); + + private: + Stream *FirmataStream; + /* firmware name and version */ + byte firmwareVersionCount; + byte *firmwareVersionVector; + /* input message handling */ + byte waitForData; // this flag says the next serial input will be data + byte executeMultiByteCommand; // execute this after getting multi-byte data + byte multiByteChannel; // channel data for multiByteCommands + byte storedInputData[MAX_DATA_BYTES]; // multi-byte data + /* sysex */ + boolean parsingSysex; + int sysexBytesRead; + /* pins configuration */ + byte pinConfig[TOTAL_PINS]; // configuration of every pin + int pinState[TOTAL_PINS]; // any value that has been written + + boolean resetting; + + /* callback functions */ + callbackFunction currentAnalogCallback; + callbackFunction currentDigitalCallback; + callbackFunction currentReportAnalogCallback; + callbackFunction currentReportDigitalCallback; + callbackFunction currentPinModeCallback; + callbackFunction currentPinValueCallback; + systemResetCallbackFunction currentSystemResetCallback; + stringCallbackFunction currentStringCallback; + sysexCallbackFunction currentSysexCallback; + delayTaskCallbackFunction delayTaskCallback; + + boolean blinkVersionDisabled = false; + + /* private methods ------------------------------ */ + void processSysexMessage(void); + void systemReset(void); + void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval); +}; + +extern FirmataClass Firmata; + +/*============================================================================== + * MACROS + *============================================================================*/ + +/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the + * firmware name. It needs to be a macro so that __FILE__ is included in the + * firmware source file rather than the library source file. + */ +#define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y) + +#endif /* Configurable_Firmata_h */ diff --git a/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.cpp b/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.cpp new file mode 100644 index 0000000..8d89bd7 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.cpp @@ -0,0 +1,264 @@ +/* + DeviceFirmata.cpp - Firmata library +*/ + +#include "DeviceFirmata.h" +#include "utility/Boards.h" +#include + +DeviceTable *gDeviceTable; +extern DeviceDriver *selectedDevices[]; + +//---------------------------------------------------------------------------- + +DeviceFirmata::DeviceFirmata() { + gDeviceTable = new DeviceTable(selectedDevices,this); +} + +//--------------------------------------------------------------------------- + +void DeviceFirmata::reset() { + gDeviceTable->reset(); +} + +// The pins that are currently being used by device drivers are already +// marked as PIN_MODE_IGNORE, so we have no additional information to +// provide to the list of capabilities. + +void DeviceFirmata::handleCapability(byte pin) {} + +// Device driver capabilities do not necessarily map directly to Firmata pin +// modes, and so none of the Firmata modes are recognized and all pin mode +// requests are disavowed. + +boolean DeviceFirmata::handlePinMode(byte pin, int mode) { + return false; +} + +void DeviceFirmata::update() { + gDeviceTable->dispatchTimers(); +} + +//--------------------------------------------------------------------------- + +// The entire body of each device driver message is encoded in base-64 +// during transmission to and from this Firmata server. The first 9 bytes +// of the decoded message body form a prologue that contains slots for all +// the common query and request parameters. Following bytes in a query +// are used by the open() and write() methods; following bytes in a response +// are used by the read() method. + +// Note that the base64 library adds a null terminator after the decoded data. +// This means that the decode targets need to be at least one byte bigger than +// the actual data size. This also makes it possible to use the decoded data +// as a null-terminated string without having to add the null (eg, open()) + +boolean DeviceFirmata::handleSysex(byte command, byte argc, byte *argv) { + if (command != DEVICE_QUERY) return false; + + if (argc < 12) { + reportError(EMSGSIZE); + return true; + } + byte parameterBlock[10]; // 9-byte beginning of every decoded DEVICE_QUERY message + byte *dataBlock = 0; // data from the client for use in open() and write() + byte *inputBuffer = 0; // data from the device in response to read() + + base64_decode((char *)parameterBlock, (char *)(argv), 12); + + int dataBlockLength = (argc == 12) ? 0 : base64_dec_len((char *)(argv + 12), argc - 12); + if (dataBlockLength > 0) { + dataBlock = new byte[dataBlockLength+1]; + if (dataBlock == 0) { + reportError(ENOMEM); + return true; + } + dataBlockLength = base64_decode((char *)dataBlock, (char *)(argv + 12), argc - 12); + } + + int action = (from8LEToHost(parameterBlock) & 0x0F); + int flags = (from8LEToHost(parameterBlock) & 0xF0) >> 4; + int handle = from16LEToHost(parameterBlock+1); + int openOpts = handle; + int reg = from16LEToHost(parameterBlock+3); + int count = from16LEToHost(parameterBlock+5); + + int status; + + switch (action) { + + case (int)DAC::OPEN: + if (dataBlockLength == 0) { + reportError(EINVAL); + } else { + status = gDeviceTable->open(openOpts, flags, (const char *)dataBlock); + reportOpen(status, openOpts, flags, dataBlock); + } + break; + + case (int)DAC::READ: + if (dataBlockLength != 0) { + reportError(EINVAL); + } else { + inputBuffer = new byte[count]; + if (inputBuffer == 0) { + reportError(ENOMEM); + } else { + status = gDeviceTable->read(handle, flags, reg, count, inputBuffer); + reportRead(status, handle, flags, reg, count, inputBuffer); + } + } + break; + + case (int)DAC::WRITE: + if (dataBlockLength != count) { + reportError(EINVAL); + } else { + status = gDeviceTable->write(handle, flags, reg, count, dataBlock); + reportWrite(status, handle, flags, reg, count); + } + break; + + case (int)DAC::CLOSE: + if (dataBlockLength != 0) { + reportError(EINVAL); + } else { + status = gDeviceTable->close(handle, flags); + reportClose(status, handle, flags); + } + break; + + default: + reportError(ENOTSUP); + break; + } + + delete dataBlock; + delete inputBuffer; + return true; +} + +//--------------------------------------------------------------------------- + +void DeviceFirmata::reportClaimPin(int pin) { + Firmata.setPinMode((byte)pin, PIN_MODE_IGNORE); +} + +void DeviceFirmata::reportReleasePin(int pin) { + Firmata.setPinMode((byte)pin, INPUT); +} + +void DeviceFirmata::reportOpen(int status, int openOpts, int flags, const byte *buf) { + sendDeviceResponse((int)DAC::OPEN, status, openOpts, flags, 0, 0,buf); +} +/** + * Translates a message from the DeviceDriver environment to a call to a Firmata-aware method. + * @param status The status code or actual byte count associated with this read. + * @param handle The handle of the unit doing the reply + * @param reg The register identifier associated with the read() being reported + * @param count The number of bytes that were requested. May be less than or + * equal to the byte count in status after a successful read. + * @param buf The byte[] result of the read(). + */ +void DeviceFirmata::reportRead(int status, int handle, int flags, int reg, int count, const byte *buf) { + sendDeviceResponse((int)DAC::READ, status, handle, flags, reg, count, buf); +} +/** + * Translates a message from the DeviceDriver environment to a call to a Firmata-aware method. + * @param status The status code or actual byte count associated with this write. + * @param handle The handle of the unit doing the reply + * @param reg The register identifier associated with the write() being reported + * @param count The number of bytes that were requested. May be less than or + * equal to the byte count in status after a successful write. + */ +void DeviceFirmata::reportWrite(int status, int handle, int flags, int reg, int count) { + sendDeviceResponse((int)DAC::WRITE, status, handle, flags, reg, count); +} + +void DeviceFirmata::reportClose(int status, int handle, int flags) { + sendDeviceResponse((int)DAC::CLOSE, status, handle, flags); +} + +void DeviceFirmata::reportError(int status) { + sendDeviceResponse((int)DAC::CLOSE, status); +} + +void DeviceFirmata::reportString(const byte *dataBytes) { + Firmata.sendString((char *)dataBytes); +} + + +//--------------------------------------------------------------------------- + +/** + * This method is called when there is a message to be sent back to the + * client. It may be in response to a DEVICE_REQUEST that was just + * processed, or it may be an asynchronous event such as a stepper motor in + * a new position or a continuous read data packet. + * + * dmB -> decoded message body + * emB -> encoded message body + * + * @param action The method identifier to use in the response. + * @param status Status value, new handle (open), or number of bytes actually read or written + * @param handle The handle identifying the device and unit number the message is coming from + * @param reg The register number associated with this message + * @param count The number of bytes specified originally by the caller + * @param dataBytes The raw data read from the device, if any + * + * Note: The base64 encoder adds a null at the end of the encoded data. Thus the encode + * target buffer needs to be one byte longer than the calculated data length. + */ +void DeviceFirmata::sendDeviceResponse(int action, int status, int handle, int flags, int reg, int count, + const byte *dataBytes) { + + byte dP[9]; // decoded (raw) message prologue + byte eP[12+1]; // encoded message prologue + byte *eD; // encoded data bytes + + Firmata.write(START_SYSEX); + Firmata.write(DEVICE_RESPONSE); + + dP[0] = (byte) ((flags & 0xF) << 4) | (action & 0xF); + dP[1] = (byte) lowByte(handle); + dP[2] = (byte) highByte(handle); + dP[3] = (byte) lowByte(reg); + dP[4] = (byte) highByte(reg); + dP[5] = (byte) lowByte(count); + dP[6] = (byte) highByte(count); + dP[7] = (byte) lowByte(status); + dP[8] = (byte) highByte(status); + + base64_encode((char *)eP, (char *)dP, 9); + + for (int idx = 0; idx < 12; idx++) { + Firmata.write(eP[idx]); + } + + int rawCount = 0; + int encCount = 0; + + if (dataBytes != 0) { + if (action == (int)DAC::OPEN) { + rawCount = strlen((const char *)dataBytes)+1; + } else if (action == (int)DAC::READ && status > 0) { + rawCount = status; + } + encCount = base64_enc_len(rawCount); + if (encCount > 0) { + eD = new byte[encCount+1]; + if (eD == 0) { + for (int idx = 0; idx < encCount; idx++) { + Firmata.write('/'); // Memory allocation error. This value will be decoded as 0x3F, ie, all 7 bits set. + } + } else { + base64_encode((char *)eD, (char *)dataBytes, rawCount); + for (int idx = 0; idx < encCount; idx++) { + Firmata.write(eD[idx]); // Success. These are the encoded data bytes. + } + } + delete eD; + } + } + Firmata.write(END_SYSEX); +} diff --git a/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.h b/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.h new file mode 100644 index 0000000..c44171b --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/DeviceFirmata.h @@ -0,0 +1,41 @@ + +#ifndef DeviceFirmata_h +#define DeviceFirmata_h + +#include +#include +#include +#include +#include + +class DeviceFirmata: public FirmataFeature, ClientReporter { +public: + DeviceFirmata(); + + // FirmataFeature + + void reset(); + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + + void update(); + + // ClientReporter + + void reportOpen(int status, int opts, int flags, const byte *buf); + void reportRead(int status, int handle, int flags, int reg, int count, const byte *dataBytes); + void reportWrite(int status, int handle, int flags, int reg, int count); + void reportClose(int status, int handle, int flags ); + void reportString(const byte *dataBytes); + void reportError(int status); + void reportClaimPin(int pin); + void reportReleasePin(int pin); + +private: + + void sendDeviceResponse(int action, int status, int handle = 0, int flags = 0, int reg = 0, int count = 0, const byte *dataBytes = 0); +}; + +#endif + diff --git a/libraries/FirmataWithDeviceFeature/src/FirmataExt.cpp b/libraries/FirmataWithDeviceFeature/src/FirmataExt.cpp new file mode 100644 index 0000000..53bcb0c --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/FirmataExt.cpp @@ -0,0 +1,117 @@ +/* + FirmataExt.h - 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-2015 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: November 15th, 2015 +*/ + +#include +#include "FirmataExt.h" + +FirmataExt *FirmataExtInstance; + +void handleSetPinModeCallback(byte pin, int mode) +{ + if (!FirmataExtInstance->handlePinMode(pin, mode) && mode != PIN_MODE_IGNORE) { + Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM + } +} + +void handleSysexCallback(byte command, byte argc, byte* argv) +{ + if (!FirmataExtInstance->handleSysex(command, argc, argv)) { + Firmata.sendString("Unhandled sysex command"); + } +} + +FirmataExt::FirmataExt() +{ + FirmataExtInstance = this; + Firmata.attach(SET_PIN_MODE, handleSetPinModeCallback); + Firmata.attach((byte)START_SYSEX, handleSysexCallback); + numFeatures = 0; +} + +void FirmataExt::handleCapability(byte pin) +{ + +} + +boolean FirmataExt::handlePinMode(byte pin, int mode) +{ + boolean known = false; + for (byte i = 0; i < numFeatures; i++) { + known |= features[i]->handlePinMode(pin, mode); + } + return known; +} + +boolean FirmataExt::handleSysex(byte command, byte argc, byte* argv) +{ + switch (command) { + + case PIN_STATE_QUERY: + if (argc > 0) { + byte pin = argv[0]; + if (pin < TOTAL_PINS) { + Firmata.write(START_SYSEX); + Firmata.write(PIN_STATE_RESPONSE); + Firmata.write(pin); + Firmata.write(Firmata.getPinMode(pin)); + int pinState = Firmata.getPinState(pin); + Firmata.write((byte)pinState & 0x7F); + if (pinState & 0xFF80) Firmata.write((byte)(pinState >> 7) & 0x7F); + if (pinState & 0xC000) Firmata.write((byte)(pinState >> 14) & 0x7F); + Firmata.write(END_SYSEX); + return true; + } + } + break; + case CAPABILITY_QUERY: + Firmata.write(START_SYSEX); + Firmata.write(CAPABILITY_RESPONSE); + for (byte pin = 0; pin < TOTAL_PINS; pin++) { + if (Firmata.getPinMode(pin) != PIN_MODE_IGNORE) { + for (byte i = 0; i < numFeatures; i++) { + features[i]->handleCapability(pin); + } + } + Firmata.write(127); + } + Firmata.write(END_SYSEX); + return true; + default: + for (byte i = 0; i < numFeatures; i++) { + if (features[i]->handleSysex(command, argc, argv)) { + return true; + } + } + break; + } + return false; +} + +void FirmataExt::addFeature(FirmataFeature &capability) +{ + if (numFeatures < MAX_FEATURES) { + features[numFeatures++] = &capability; + } +} + +void FirmataExt::reset() +{ + for (byte i = 0; i < numFeatures; i++) { + features[i]->reset(); + } +} diff --git a/libraries/FirmataWithDeviceFeature/src/FirmataExt.h b/libraries/FirmataWithDeviceFeature/src/FirmataExt.h new file mode 100644 index 0000000..04427e6 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/FirmataExt.h @@ -0,0 +1,44 @@ +/* + FirmataExt.h - 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-2015 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. +*/ + +#ifndef FirmataExt_h +#define FirmataExt_h + +#include +#include "FirmataFeature.h" + +#define MAX_FEATURES TOTAL_PIN_MODES + 1 + +void handleSetPinModeCallback(byte pin, int mode); + +void handleSysexCallback(byte command, byte argc, byte* argv); + +class FirmataExt: public FirmataFeature +{ + public: + FirmataExt(); + void handleCapability(byte pin); //empty method + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + void addFeature(FirmataFeature &capability); + void reset(); + + private: + FirmataFeature *features[MAX_FEATURES]; + byte numFeatures; +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src/FirmataFeature.h b/libraries/FirmataWithDeviceFeature/src/FirmataFeature.h new file mode 100644 index 0000000..e83653b --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/FirmataFeature.h @@ -0,0 +1,31 @@ +/* + FirmataExt.h - 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-2015 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. +*/ + +#ifndef FirmataFeature_h +#define FirmataFeature_h + +#include + +class FirmataFeature +{ + public: + virtual void handleCapability(byte pin) = 0; + virtual boolean handlePinMode(byte pin, int mode) = 0; + virtual boolean handleSysex(byte command, byte argc, byte* argv) = 0; + virtual void reset() = 0; +}; + +#endif diff --git a/libraries/FirmataWithDeviceFeature/src/utility/Boards.h b/libraries/FirmataWithDeviceFeature/src/utility/Boards.h new file mode 100644 index 0000000..3e9f4ae --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/src/utility/Boards.h @@ -0,0 +1,610 @@ +/* + Boards.h - Hardware Abstraction Layer for Firmata library + Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved. + Copyright (c) 2013 Norbert Truchsess. All rights reserved. + Copyright (c) 2013-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 January 19th, 2015 +*/ + +#ifndef Firmata_Boards_h +#define Firmata_Boards_h + +#include + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" // for digitalRead, digitalWrite, etc +#else +#include "WProgram.h" +#endif + +// Normally Servo.h must be included before Firmata.h (which then includes +// this file). If Servo.h wasn't included, this allows the code to still +// compile, but without support for any Servos. Hopefully that's what the +// user intended by not including Servo.h +#ifndef MAX_SERVOS +#define MAX_SERVOS 0 +#endif + +/* + Firmata Hardware Abstraction Layer + +Firmata is built on top of the hardware abstraction functions of Arduino, +specifically digitalWrite, digitalRead, analogWrite, analogRead, and +pinMode. While these functions offer simple integer pin numbers, Firmata +needs more information than is provided by Arduino. This file provides +all other hardware specific details. To make Firmata support a new board, +only this file should require editing. + +The key concept is every "pin" implemented by Firmata may be mapped to +any pin as implemented by Arduino. Usually a simple 1-to-1 mapping is +best, but such mapping should not be assumed. This hardware abstraction +layer allows Firmata to implement any number of pins which map onto the +Arduino implemented pins in almost any arbitrary way. + + +General Constants: + +These constants provide basic information Firmata requires. + +TOTAL_PINS: The total number of pins Firmata implemented by Firmata. + Usually this will match the number of pins the Arduino functions + implement, including any pins pins capable of analog or digital. + However, Firmata may implement any number of pins. For example, + on Arduino Mini with 8 analog inputs, 6 of these may be used + for digital functions, and 2 are analog only. On such boards, + Firmata can implement more pins than Arduino's pinMode() + function, in order to accommodate those special pins. The + Firmata protocol supports a maximum of 128 pins, so this + constant must not exceed 128. + +TOTAL_ANALOG_PINS: The total number of analog input pins implemented. + The Firmata protocol allows up to 16 analog inputs, accessed + using offsets 0 to 15. Because Firmata presents the analog + inputs using different offsets than the actual pin numbers + (a legacy of Arduino's analogRead function, and the way the + analog input capable pins are physically labeled on all + Arduino boards), the total number of analog input signals + must be specified. 16 is the maximum. + +VERSION_BLINK_PIN: When Firmata starts up, it will blink the version + number. This constant is the Arduino pin number where a + LED is connected. + + +Pin Mapping Macros: + +These macros provide the mapping between pins as implemented by +Firmata protocol and the actual pin numbers used by the Arduino +functions. Even though such mappings are often simple, pin +numbers received by Firmata protocol should always be used as +input to these macros, and the result of the macro should be +used with any Arduino function. + +When Firmata is extended to support a new pin mode or feature, +a pair of macros should be added and used for all hardware +access. For simple 1:1 mapping, these macros add no actual +overhead, yet their consistent use allows source code which +uses them consistently to be easily adapted to all other boards +with different requirements. + +IS_PIN_XXXX(pin): The IS_PIN macros resolve to true or non-zero + if a pin as implemented by Firmata corresponds to a pin + that actually implements the named feature. + +PIN_TO_XXXX(pin): The PIN_TO macros translate pin numbers as + implemented by Firmata to the pin numbers needed as inputs + to the Arduino functions. The corresponding IS_PIN macro + should always be tested before using a PIN_TO macro, so + these macros only need to handle valid Firmata pin + numbers for the named feature. + + +Port Access Inline Funtions: + +For efficiency, Firmata protocol provides access to digital +input and output pins grouped by 8 bit ports. When these +groups of 8 correspond to actual 8 bit ports as implemented +by the hardware, these inline functions can provide high +speed direct port access. Otherwise, a default implementation +using 8 calls to digitalWrite or digitalRead is used. + +When porting Firmata to a new board, it is recommended to +use the default functions first and focus only on the constants +and macros above. When those are working, if optimized port +access is desired, these inline functions may be extended. +The recommended approach defines a symbol indicating which +optimization to use, and then conditional complication is +used within these functions. + +readPort(port, bitmask): Read an 8 bit port, returning the value. + port: The port number, Firmata pins port*8 to port*8+7 + bitmask: The actual pins to read, indicated by 1 bits. + +writePort(port, value, bitmask): Write an 8 bit port. + port: The port number, Firmata pins port*8 to port*8+7 + value: The 8 bit value to write + bitmask: The actual pins to write, indicated by 1 bits. +*/ + +/*============================================================================== + * Board Specific Configuration + *============================================================================*/ + +#ifndef digitalPinHasPWM +#define digitalPinHasPWM(p) IS_PIN_DIGITAL(p) +#endif + +#if defined(digitalPinToInterrupt) && defined(NOT_AN_INTERRUPT) +#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT) +#else +#define IS_PIN_INTERRUPT(p) (0) +#endif + +// Arduino Duemilanove, Diecimila, and NG +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) +#if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6 +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#else +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 22 // 14 digital + 8 analog +#endif +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Wiring (and board) +#elif defined(WIRING) +#define VERSION_BLINK_PIN WLED +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= FIRST_ANALOG_PIN && (p) < (FIRST_ANALOG_PIN+TOTAL_ANALOG_PINS)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - FIRST_ANALOG_PIN) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// old Arduinos +#elif defined(__AVR_ATmega8__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) +#define ARDUINO_PINOUT_OPTIMIZE 1 + + +// Arduino Mega +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define TOTAL_ANALOG_PINS 16 +#define TOTAL_PINS 70 // 54 digital + 16 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino DUE +#elif defined(__SAM3X8E__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 66 // 54 digital + 12 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 19 +#define PIN_SERIAL1_TX 18 +#define PIN_SERIAL2_RX 17 +#define PIN_SERIAL2_TX 16 +#define PIN_SERIAL3_RX 15 +#define PIN_SERIAL3_TX 14 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71 +#define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 54) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Arduino/Genuino MKR1000 +#elif defined(ARDUINO_SAMD_MKR1000) +#define TOTAL_ANALOG_PINS 7 +#define TOTAL_PINS 22 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21) && !IS_PIN_SERIAL(p)) +#define IS_PIN_ANALOG(p) ((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14 +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 15) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino Zero +// Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro +// Arduino M0 Pro does not properly map pins to the board labeled pin numbers +#elif defined(_VARIANT_ARDUINO_ZERO_) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi +#define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins +#define VERSION_BLINK_PIN LED_BUILTIN +//#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h +//#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2 +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Arduino 101 +#elif defined(_VARIANT_ARDUINO_101_X_) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog +#define VERSION_BLINK_PIN LED_BUILTIN +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9 +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4 +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19 +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) // deprecated since v2.4 + + +// Teensy 1.0 +#elif defined(__AVR_AT90USB162__) +#define TOTAL_ANALOG_PINS 0 +#define TOTAL_PINS 21 // 21 digital + no analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) (0) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) (0) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (0) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 2.0 +#elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 25 // 11 digital + 12 analog +#define VERSION_BLINK_PIN 11 +#define PIN_SERIAL1_RX 7 +#define PIN_SERIAL1_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 5 || (p) == 6) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy 3.0, 3.1 and 3.2 +#elif defined(__MK20DX128__) || defined(__MK20DX256__) +#define TOTAL_ANALOG_PINS 14 +#define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33) +#define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38)) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy-LC +#elif defined(__MKL26Z64__) +#define TOTAL_ANALOG_PINS 13 +#define TOTAL_PINS 27 // 27 digital + 13 analog-digital +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define PIN_SERIAL2_RX 9 +#define PIN_SERIAL2_TX 10 +#define PIN_SERIAL3_RX 7 +#define PIN_SERIAL3_TX 8 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26) +#define IS_PIN_ANALOG(p) ((p) >= 14) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 18 || (p) == 19) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1)) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Teensy++ 1.0 and 2.0 +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 46 // 38 digital + 8 analog +#define VERSION_BLINK_PIN 6 +#define PIN_SERIAL1_RX 2 +#define PIN_SERIAL1_TX 3 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 0 || (p) == 1) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 38) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Leonardo +#elif defined(__AVR_ATmega32U4__) +#define TOTAL_ANALOG_PINS 12 +#define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header) +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 2 || (p) == 3) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) (p) - 18 +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) (p) + + +// Intel Galileo Board (gen 1 and 2) and Intel Edison +#elif defined(ARDUINO_LINUX) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 20 // 14 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define PIN_SERIAL1_RX 0 +#define PIN_SERIAL1_TX 1 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19) +#define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 14) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Pinoccio Scout +// Note: digital pins 9-16 are usable but not labeled on the board numerically. +// SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16 +#elif defined(ARDUINO_PINOCCIO) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS NUM_DIGITAL_PINS // 32 +#define VERSION_BLINK_PIN 23 +#define PIN_SERIAL1_RX 13 +#define PIN_SERIAL1_TX 14 +#define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31)) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p) +#define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Sanguino +#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) +#define TOTAL_ANALOG_PINS 8 +#define TOTAL_PINS 32 // 24 digital + 8 analog +#define VERSION_BLINK_PIN 0 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 16 || (p) == 17) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 24) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// Illuminato +#elif defined(__AVR_ATmega645__) +#define TOTAL_ANALOG_PINS 6 +#define TOTAL_PINS 42 // 36 digital + 6 analog +#define VERSION_BLINK_PIN 13 +#define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS) +#define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == 4 || (p) == 5) +#define PIN_TO_DIGITAL(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 36) +#define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) +#define PIN_TO_SERVO(p) ((p) - 2) + + +// anything else +#else +#error "Please edit Boards.h with a hardware abstraction for this board" +#endif + +// as long this is not defined for all boards: +#ifndef IS_PIN_SPI +#define IS_PIN_SPI(p) (0) +#endif + +#ifndef IS_PIN_SERIAL +#define IS_PIN_SERIAL(p) 0 +#endif + +/*============================================================================== + * readPort() - Read an 8 bit port + *============================================================================*/ + +static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char readPort(byte port, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1 + if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask; + if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask; + return 0; +#else + unsigned char out = 0, pin = port * 8; + if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01; + if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02; + if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04; + if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08; + if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10; + if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20; + if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40; + if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80; + return out; +#endif +} + +/*============================================================================== + * writePort() - Write an 8 bit port, only touch pins specified by a bitmask + *============================================================================*/ + +static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused)); +static inline unsigned char writePort(byte port, byte value, byte bitmask) +{ +#if defined(ARDUINO_PINOUT_OPTIMIZE) + if (port == 0) { + bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins + byte valD = value & bitmask; + byte maskD = ~bitmask; + cli(); + PORTD = (PORTD & maskD) | valD; + sei(); + } else if (port == 1) { + byte valB = (value & bitmask) & 0x3F; + byte valC = (value & bitmask) >> 6; + byte maskB = ~(bitmask & 0x3F); + byte maskC = ~((bitmask & 0xC0) >> 6); + cli(); + PORTB = (PORTB & maskB) | valB; + PORTC = (PORTC & maskC) | valC; + sei(); + } else if (port == 2) { + bitmask = bitmask & 0x0F; + byte valC = (value & bitmask) << 2; + byte maskC = ~(bitmask << 2); + cli(); + PORTC = (PORTC & maskC) | valC; + sei(); + } + return 1; +#else + byte pin = port * 8; + if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01)); + if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02)); + if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04)); + if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08)); + if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10)); + if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20)); + if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40)); + if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80)); + return 1; +#endif +} + + + + +#ifndef TOTAL_PORTS +#define TOTAL_PORTS ((TOTAL_PINS + 7) / 8) +#endif + + +#endif /* Firmata_Boards_h */ diff --git a/libraries/FirmataWithDeviceFeature/test/firmata_test/firmata_test.ino b/libraries/FirmataWithDeviceFeature/test/firmata_test/firmata_test.ino new file mode 100644 index 0000000..f96d119 --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/test/firmata_test/firmata_test.ino @@ -0,0 +1,136 @@ +/* + * To run this test suite, you must first install the ArduinoUnit library + * to your Arduino/libraries/ directory. + * You can get ArduinoUnit here: https://github.com/mmurdoch/arduinounit + * Download version 2.0 or greater. + */ + +#include +#include + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Test::run(); +} + +test(beginPrintsVersion) +{ + FakeStream stream; + + Firmata.begin(stream); + + char expected[] = { + REPORT_VERSION, + FIRMATA_MAJOR_VERSION, + FIRMATA_MINOR_VERSION, + 0 + }; + assertEqual(expected, stream.bytesWritten()); +} + +void processMessage(const byte *message, size_t length) +{ + FakeStream stream; + Firmata.begin(stream); + + for (size_t i = 0; i < length; i++) { + stream.nextByte(message[i]); + Firmata.processInput(); + } +} + +byte _digitalPort; +int _digitalPortValue; +void writeToDigitalPort(byte port, int value) +{ + _digitalPort = port; + _digitalPortValue = value; +} + +void setupDigitalPort() +{ + _digitalPort = 0; + _digitalPortValue = 0; +} + +test(processWriteDigital_0) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPortValue); +} + +test(processWriteDigital_127) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 127, 0 }; + processMessage(message, 3); + + assertEqual(127, _digitalPortValue); +} + +test(processWriteDigital_128) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 1 }; + processMessage(message, 3); + + assertEqual(128, _digitalPortValue); +} + +test(processWriteLargestDigitalValue) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0x7F, 0x7F }; + processMessage(message, 3); + + // Maximum of 14 bits can be set (B0011111111111111) + assertEqual(0x3FFF, _digitalPortValue); +} + +test(defaultDigitalWritePortIsZero) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE, 0, 0 }; + processMessage(message, 3); + + assertEqual(0, _digitalPort); +} + +test(specifiedDigitalWritePort) +{ + setupDigitalPort(); + Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort); + + byte message[] = { DIGITAL_MESSAGE + 1, 0, 0 }; + processMessage(message, 3); + + assertEqual(1, _digitalPort); +} + +test(setFirmwareVersionDoesNotLeakMemory) +{ + Firmata.setFirmwareVersion(1, 0); + int initialMemory = freeMemory(); + + Firmata.setFirmwareVersion(1, 0); + + assertEqual(0, initialMemory - freeMemory()); +} diff --git a/libraries/FirmataWithDeviceFeature/test/readme.md b/libraries/FirmataWithDeviceFeature/test/readme.md new file mode 100644 index 0000000..726cbcb --- /dev/null +++ b/libraries/FirmataWithDeviceFeature/test/readme.md @@ -0,0 +1,13 @@ +#Testing Firmata + +Tests tests are written using the [ArduinoUnit](https://github.com/mmurdoch/arduinounit) library (version 2.0). + +Follow the instructions in the [ArduinoUnit readme](https://github.com/mmurdoch/arduinounit/blob/master/readme.md) to install the library. + +Compile and upload the test sketch as you would any other sketch. Then open the +Serial Monitor to view the test results. + +If you make changes to Firmata.cpp, run the tests in /test/ to ensure +that your changes have not produced any unexpected errors. + +You should also perform manual tests against actual hardware. diff --git a/libraries/NeoPixelBus_by_Makuna/COPYING b/libraries/NeoPixelBus_by_Makuna/COPYING new file mode 100644 index 0000000..153d416 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/COPYING @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/ReadMe.md b/libraries/NeoPixelBus_by_Makuna/ReadMe.md new file mode 100644 index 0000000..202418d --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/ReadMe.md @@ -0,0 +1,34 @@ +# NeoPixelBus + +[![Donate](http://img.shields.io/paypal/donate.png?color=yellow)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6AA97KE54UJR4) + +Arduino NeoPixel library + +A library to control one wire protocol RGB and RGBW leds like SK6812, WS2811, WS2812 and WS2813 that are commonly refered to as NeoPixels and two wire protocol RGB like APA102 commonly refered to as DotStars. +Supports most Arduino platforms. +This is the most functional library for the Esp8266 as it provides solutions for all Esp8266 module types even when WiFi is used. + + +Please read this best practices link before connecting your NeoPixels, it will save you a lot of time and effort. +[Adafruit NeoPixel Best Practices](https://learn.adafruit.com/adafruit-neopixel-uberguide/best-practices) + +For quick questions jump on Gitter and ask away. +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Makuna/NeoPixelBus?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) + +For bugs, make sure there isn't an active issue and then create one. + +## Documentation +[See Wiki](https://github.com/Makuna/NeoPixelBus/wiki) + +## Installing This Library (prefered, you just want to use it) +Open the Library Manager and search for "NeoPixelBus by Makuna" and install + +## Installing This Library From GitHub (advanced, you want to contribute) +Create a directory in your Arduino\Library folder named "NeoPixelBus" +Clone (Git) this project into that folder. +It should now show up in the import list when you restart Arduino IDE. + + + + + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/DotStarTest/DotStarTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/DotStarTest/DotStarTest.ino new file mode 100644 index 0000000..45201ab --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/DotStarTest/DotStarTest.ino @@ -0,0 +1,88 @@ +// DotStarTest +// This example will cycle between showing four pixels as Red, Green, Blue, White +// and then showing those pixels as Black. +// +// There is serial output of the current state so you can confirm and follow along +// + +#include + +const uint16_t PixelCount = 4; // this example assumes 4 pixels, making it smaller will cause a failure + +// make sure to set this to the correct pins +const uint8_t DotClockPin = 2; +const uint8_t DotDataPin = 3; + +#define colorSaturation 128 + +// for software bit bang +NeoPixelBus strip(PixelCount, DotClockPin, DotDataPin); + +// for hardware SPI (best performance but must use hardware pins) +//NeoPixelBus strip(PixelCount); + +// DotStars that support RGB color and a overall luminance/brightness value +// NeoPixelBus strip(PixelCount, DotClockPin, DotDataPin); +// DotStars that support RGBW color with a seperate white element +//NeoPixelBus strip(PixelCount, DotClockPin, DotDataPin); + +RgbColor red(colorSaturation, 0, 0); +RgbColor green(0, colorSaturation, 0); +RgbColor blue(0, 0, colorSaturation); +RgbColor white(colorSaturation); +RgbColor black(0); + +// for use with RGB DotStars when using the luminance/brightness global value +// note that its range is only 0 - 31 (31 is full bright) and +// also note that it is not useful for POV displays as it will cause more flicker +RgbwColor redL(colorSaturation, 0, 0, 31); // use white value to store luminance +RgbwColor greenL(0, colorSaturation, 0, 31); // use white value to store luminance +RgbwColor blueL(0, 0, colorSaturation, 31); // use white value to store luminance +RgbwColor whiteL(255, 255, 255, colorSaturation / 8); // luminance is only 0-31 + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.ClearTo(black); + strip.Show(); + + Serial.println(); + Serial.println("Running..."); +} + + +void loop() +{ + delay(5000); + + Serial.println("Colors R, G, B, W..."); + + // set the colors, + strip.SetPixelColor(0, red); + strip.SetPixelColor(1, green); + strip.SetPixelColor(2, blue); + strip.SetPixelColor(3, white); + strip.Show(); + + + delay(5000); + + Serial.println("Off ..."); + + // turn off the pixels + strip.SetPixelColor(0, black); + strip.SetPixelColor(1, black); + strip.SetPixelColor(2, black); + strip.SetPixelColor(3, black); + strip.Show(); + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelBrightness/NeoPixelBrightness.ino b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelBrightness/NeoPixelBrightness.ino new file mode 100644 index 0000000..01af6cb --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelBrightness/NeoPixelBrightness.ino @@ -0,0 +1,83 @@ +// NeoPixelBrightness +// This example will cycle brightness from high to low of +// three pixels colored Red, Green, Blue. +// This demonstrates the use of the NeoPixelBrightnessBus +// with integrated brightness support +// +// There is serial output of the current state so you can +// confirm and follow along +// + +#include // instead of NeoPixelBus.h + +const uint16_t PixelCount = 3; // this example assumes 3 pixels, making it smaller will cause a failure +const uint8_t PixelPin = 14; // make sure to set this to the correct pin, ignored for Esp8266 + +#define colorSaturation 255 // saturation of color constants +RgbColor red(colorSaturation, 0, 0); +RgbColor green(0, colorSaturation, 0); +RgbColor blue(0, 0, colorSaturation); + +// Make sure to provide the correct color order feature +// for your NeoPixels +NeoPixelBrightnessBus strip(PixelCount, PixelPin); + +// you loose the original color the lower the dim value used +// here due to quantization +const uint8_t c_MinBrightness = 8; +const uint8_t c_MaxBrightness = 255; + +int8_t direction; // current direction of dimming + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.Show(); + + direction = -1; // default to dim first + + Serial.println(); + Serial.println("Running..."); + + // set our three original colors + strip.SetPixelColor(0, red); + strip.SetPixelColor(1, green); + strip.SetPixelColor(2, blue); + + strip.Show(); +} + + +void loop() +{ + uint8_t brightness = strip.GetBrightness(); + Serial.println(brightness); + + delay(100); + + // swap diection of dim when limits are reached + // + if (direction < 0 && brightness <= c_MinBrightness) + { + direction = 1; + } + else if (direction > 0 && brightness >= c_MaxBrightness) + { + direction = -1; + } + // apply dimming + brightness += direction; + strip.SetBrightness(brightness); + + // show the results + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelGamma/NeoPixelGamma.ino b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelGamma/NeoPixelGamma.ino new file mode 100644 index 0000000..3ed9152 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelGamma/NeoPixelGamma.ino @@ -0,0 +1,95 @@ +// NeoPixelGamma +// This example will display a timed series of color gradiants with gamma correction +// and then without. +// If the last pixel is on, then the colors being shown are color corrected. +// It will show Red grandiant, Green grandiant, Blue grandiant, a White grandiant, and +// then repeat. +// +// This will demonstrate the use of the NeoGamma class +// +// + +#include +#include + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +// uncomment only one of these to compare memory use or speed +// +// NeoGamma colorGamma; +NeoGamma colorGamma; + +void DrawPixels(bool corrected, HslColor startColor, HslColor stopColor) +{ + for (uint16_t index = 0; index < strip.PixelCount() - 1; index++) + { + float progress = index / static_cast(strip.PixelCount() - 2); + RgbColor color = HslColor::LinearBlend(startColor, stopColor, progress); + if (corrected) + { + color = colorGamma.Correct(color); + } + strip.SetPixelColor(index, color); + } + + // use the last pixel to indicate if we are showing corrected colors or not + if (corrected) + { + strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(64)); + } + else + { + strip.SetPixelColor(strip.PixelCount() - 1, RgbColor(0)); + } + + strip.Show(); +} + +void setup() +{ + strip.Begin(); + strip.Show(); +} + +void loop() +{ + HslColor startColor; + HslColor stopColor; + + // red color + startColor = HslColor(0.0f, 1.0f, 0.0f); + stopColor = HslColor(0.0f, 1.0f, 0.5f); + DrawPixels(true, startColor, stopColor); + delay(5000); + DrawPixels(false, startColor, stopColor); + delay(5000); + + // green color + startColor = HslColor(0.33f, 1.0f, 0.0f); + stopColor = HslColor(0.33f, 1.0f, 0.5f); + DrawPixels(true, startColor, stopColor); + delay(5000); + DrawPixels(false, startColor, stopColor); + delay(5000); + + // blue color + startColor = HslColor(0.66f, 1.0f, 0.0f); + stopColor = HslColor(0.66f, 1.0f, 0.5f); + DrawPixels(true, startColor, stopColor); + delay(5000); + DrawPixels(false, startColor, stopColor); + delay(5000); + + // white color + startColor = HslColor(0.0f, 0.0f, 0.0f); + stopColor = HslColor(0.0f, 0.0f, 0.5f); + DrawPixels(true, startColor, stopColor); + delay(5000); + DrawPixels(false, startColor, stopColor); + delay(5000); +} \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelTest/NeoPixelTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelTest/NeoPixelTest.ino new file mode 100644 index 0000000..6d323b8 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/NeoPixelTest/NeoPixelTest.ino @@ -0,0 +1,140 @@ +// NeoPixelTest +// This example will cycle between showing four pixels as Red, Green, Blue, White +// and then showing those pixels as Black. +// +// Included but commented out are examples of configuring a NeoPixelBus for +// different color order including an extra white channel, different data speeds, and +// for Esp8266 different methods to send the data. +// NOTE: You will need to make sure to pick the one for your platform +// +// +// There is serial output of the current state so you can confirm and follow along +// + +#include + +const uint16_t PixelCount = 4; // this example assumes 4 pixels, making it smaller will cause a failure +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +#define colorSaturation 128 + +// three element pixels, in different order and speeds +NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); + +// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use. +// There are other Esp8266 alternative methods that provide more pin options, but also have +// other side effects. +//NeoPixelBus strip(PixelCount); +// +// NeoEsp8266Uart800KbpsMethod uses GPI02 instead + +// You can also use one of these for Esp8266, +// each having their own restrictions +// +// These two are the same as above as the DMA method is the default +// NOTE: These will ignore the PIN and use GPI03 pin +//NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); + +// Uart method is good for the Esp-01 or other pin restricted modules +// NOTE: These will ignore the PIN and use GPI02 pin +//NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); + +// The bitbang method is really only good if you are not using WiFi features of the ESP +// It works with all but pin 16 +//NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); + +// four element pixels, RGBW +//NeoPixelBus strip(PixelCount, PixelPin); + +RgbColor red(colorSaturation, 0, 0); +RgbColor green(0, colorSaturation, 0); +RgbColor blue(0, 0, colorSaturation); +RgbColor white(colorSaturation); +RgbColor black(0); + +HslColor hslRed(red); +HslColor hslGreen(green); +HslColor hslBlue(blue); +HslColor hslWhite(white); +HslColor hslBlack(black); + + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.Show(); + + + Serial.println(); + Serial.println("Running..."); +} + + +void loop() +{ + delay(5000); + + Serial.println("Colors R, G, B, W..."); + + // set the colors, + // if they don't match in order, you need to use NeoGrbFeature feature + strip.SetPixelColor(0, red); + strip.SetPixelColor(1, green); + strip.SetPixelColor(2, blue); + strip.SetPixelColor(3, white); + // the following line demonstrates rgbw color support + // if the NeoPixels are rgbw types the following line will compile + // if the NeoPixels are anything else, the following line will give an error + //strip.SetPixelColor(3, RgbwColor(colorSaturation)); + strip.Show(); + + + delay(5000); + + Serial.println("Off ..."); + + // turn off the pixels + strip.SetPixelColor(0, black); + strip.SetPixelColor(1, black); + strip.SetPixelColor(2, black); + strip.SetPixelColor(3, black); + strip.Show(); + + delay(5000); + + Serial.println("HSL Colors R, G, B, W..."); + + // set the colors, + // if they don't match in order, you may need to use NeoGrbFeature feature + strip.SetPixelColor(0, hslRed); + strip.SetPixelColor(1, hslGreen); + strip.SetPixelColor(2, hslBlue); + strip.SetPixelColor(3, hslWhite); + strip.Show(); + + + delay(5000); + + Serial.println("Off again..."); + + // turn off the pixels + strip.SetPixelColor(0, hslBlack); + strip.SetPixelColor(1, hslBlack); + strip.SetPixelColor(2, hslBlack); + strip.SetPixelColor(3, hslBlack); + strip.Show(); + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino new file mode 100644 index 0000000..66b4d4a --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelAnimation/NeoPixelAnimation.ino @@ -0,0 +1,225 @@ +// NeoPixelAnimation +// This example will randomly pick a new color for each pixel and animate +// the current color to the new color over a random small amount of time, using +// a randomly selected animation curve. +// It will repeat this process once all pixels have finished the animation +// +// This will demonstrate the use of the NeoPixelAnimator extended time feature. +// This feature allows for different time scales to be used, allowing slow extended +// animations to be created. +// +// This will demonstrate the use of the NeoEase animation ease methods; that provide +// simulated acceleration to the animations. +// +// It also includes platform specific code for Esp8266 that demonstrates easy +// animation state and function definition inline. This is not available on AVR +// Arduinos; but the AVR compatible code is also included for comparison. +// +// The example includes some serial output that you can follow along with as it +// does the animation. +// + +#include +#include + +const uint16_t PixelCount = 4; // make sure to set this to the number of pixels in your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +NeoPixelBus strip(PixelCount, PixelPin); +// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use. +// There are other Esp8266 alternative methods that provide more pin options, but also have +// other side effects. +//NeoPixelBus strip(PixelCount); +// +// NeoEsp8266Uart800KbpsMethod uses GPI02 instead + + +// NeoPixel animation time management object +NeoPixelAnimator animations(PixelCount, NEO_CENTISECONDS); + +// create with enough animations to have one per pixel, depending on the animation +// effect, you may need more or less. +// +// since the normal animation time range is only about 65 seconds, by passing timescale value +// to the NeoPixelAnimator constructor we can increase the time range, but we also increase +// the time between the animation updates. +// NEO_CENTISECONDS will update the animations every 100th of a second rather than the default +// of a 1000th of a second, but the time range will now extend from about 65 seconds to about +// 10.9 minutes. But you must remember that the values passed to StartAnimations are now +// in centiseconds. +// +// Possible values from 1 to 32768, and there some helpful constants defined as... +// NEO_MILLISECONDS 1 // ~65 seconds max duration, ms updates +// NEO_CENTISECONDS 10 // ~10.9 minutes max duration, centisecond updates +// NEO_DECISECONDS 100 // ~1.8 hours max duration, decisecond updates +// NEO_SECONDS 1000 // ~18.2 hours max duration, second updates +// NEO_DECASECONDS 10000 // ~7.5 days, 10 second updates +// + +#ifdef ARDUINO_ARCH_AVR +// for AVR, you need to manage the state due to lack of STL/compiler support +// for Esp8266 you can define the function using a lambda and state is created for you +// see below for an example +struct MyAnimationState +{ + RgbColor StartingColor; // the color the animation starts at + RgbColor EndingColor; // the color the animation will end at + AnimEaseFunction Easeing; // the acceleration curve it will use +}; + +MyAnimationState animationState[PixelCount]; +// one entry per pixel to match the animation timing manager + +void AnimUpdate(const AnimationParam& param) +{ + // first apply an easing (curve) to the animation + // this simulates acceleration to the effect + float progress = animationState[param.index].Easeing(param.progress); + + // this gets called for each animation on every time step + // progress will start at 0.0 and end at 1.0 + // we use the blend function on the RgbColor to mix + // color based on the progress given to us in the animation + RgbColor updatedColor = RgbColor::LinearBlend( + animationState[param.index].StartingColor, + animationState[param.index].EndingColor, + progress); + // apply the color to the strip + strip.SetPixelColor(param.index, updatedColor); +} +#endif + +void SetRandomSeed() +{ + uint32_t seed; + + // random works best with a seed that can use 31 bits + // analogRead on a unconnected pin tends toward less than four bits + seed = analogRead(0); + delay(1); + + for (int shifts = 3; shifts < 31; shifts += 3) + { + seed ^= analogRead(0) << shifts; + delay(1); + } + + // Serial.println(seed); + randomSeed(seed); +} + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + strip.Begin(); + strip.Show(); + + SetRandomSeed(); + + // just pick some colors + for (uint16_t pixel = 0; pixel < PixelCount; pixel++) + { + RgbColor color = RgbColor(random(255), random(255), random(255)); + strip.SetPixelColor(pixel, color); + } + + Serial.println(); + Serial.println("Running..."); +} + + +void SetupAnimationSet() +{ + // setup some animations + for (uint16_t pixel = 0; pixel < PixelCount; pixel++) + { + const uint8_t peak = 128; + + // pick a random duration of the animation for this pixel + // since values are centiseconds, the range is 1 - 4 seconds + uint16_t time = random(100, 400); + + // each animation starts with the color that was present + RgbColor originalColor = strip.GetPixelColor(pixel); + // and ends with a random color + RgbColor targetColor = RgbColor(random(peak), random(peak), random(peak)); + // with the random ease function + AnimEaseFunction easing; + + switch (random(3)) + { + case 0: + easing = NeoEase::CubicIn; + break; + case 1: + easing = NeoEase::CubicOut; + break; + case 2: + easing = NeoEase::QuadraticInOut; + break; + } + +#ifdef ARDUINO_ARCH_AVR + // each animation starts with the color that was present + animationState[pixel].StartingColor = originalColor; + // and ends with a random color + animationState[pixel].EndingColor = targetColor; + // using the specific curve + animationState[pixel].Easeing = easing; + + // now use the animation state we just calculated and start the animation + // which will continue to run and call the update function until it completes + animations.StartAnimation(pixel, time, AnimUpdate); +#else + // we must supply a function that will define the animation, in this example + // we are using "lambda expression" to define the function inline, which gives + // us an easy way to "capture" the originalColor and targetColor for the call back. + // + // this function will get called back when ever the animation needs to change + // the state of the pixel, it will provide a animation progress value + // from 0.0 (start of animation) to 1.0 (end of animation) + // + // we use this progress value to define how we want to animate in this case + // we call RgbColor::LinearBlend which will return a color blended between + // the values given, by the amount passed, hich is also a float value from 0.0-1.0. + // then we set the color. + // + // There is no need for the MyAnimationState struct as the compiler takes care + // of those details for us + AnimUpdateCallback animUpdate = [=](const AnimationParam& param) + { + // progress will start at 0.0 and end at 1.0 + // we convert to the curve we want + float progress = easing(param.progress); + + // use the curve value to apply to the animation + RgbColor updatedColor = RgbColor::LinearBlend(originalColor, targetColor, progress); + strip.SetPixelColor(pixel, updatedColor); + }; + + // now use the animation properties we just calculated and start the animation + // which will continue to run and call the update function until it completes + animations.StartAnimation(pixel, time, animUpdate); +#endif + } +} + +void loop() +{ + if (animations.IsAnimating()) + { + // the normal loop just needs these two to run the active animations + animations.UpdateAnimations(); + strip.Show(); + } + else + { + Serial.println(); + Serial.println("Setup Next Set..."); + // example function that sets up some animations + SetupAnimationSet(); + } +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelCylon/NeoPixelCylon.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelCylon/NeoPixelCylon.ino new file mode 100644 index 0000000..4eec443 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelCylon/NeoPixelCylon.ino @@ -0,0 +1,123 @@ +// NeoPixelCylon +// This example will move a Cylon Red Eye back and forth across the +// the full collection of pixels on the strip. +// +// This will demonstrate the use of the NeoEase animation ease methods; that provide +// simulated acceleration to the animations. +// +// + +#include +#include + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 +const RgbColor CylonEyeColor(HtmlColor(0x7f0000)); + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +NeoPixelAnimator animations(2); // only ever need 2 animations + +uint16_t lastPixel = 0; // track the eye position +int8_t moveDir = 1; // track the direction of movement + +// uncomment one of the lines below to see the effects of +// changing the ease function on the movement animation +AnimEaseFunction moveEase = +// NeoEase::Linear; +// NeoEase::QuadraticInOut; +// NeoEase::CubicInOut; + NeoEase::QuarticInOut; +// NeoEase::QuinticInOut; +// NeoEase::SinusoidalInOut; +// NeoEase::ExponentialInOut; +// NeoEase::CircularInOut; + +void FadeAll(uint8_t darkenBy) +{ + RgbColor color; + for (uint16_t indexPixel = 0; indexPixel < strip.PixelCount(); indexPixel++) + { + color = strip.GetPixelColor(indexPixel); + color.Darken(darkenBy); + strip.SetPixelColor(indexPixel, color); + } +} + +void FadeAnimUpdate(const AnimationParam& param) +{ + if (param.state == AnimationState_Completed) + { + FadeAll(10); + animations.RestartAnimation(param.index); + } +} + +void MoveAnimUpdate(const AnimationParam& param) +{ + // apply the movement animation curve + float progress = moveEase(param.progress); + + // use the curved progress to calculate the pixel to effect + uint16_t nextPixel; + if (moveDir > 0) + { + nextPixel = progress * PixelCount; + } + else + { + nextPixel = (1.0f - progress) * PixelCount; + } + + // if progress moves fast enough, we may move more than + // one pixel, so we update all between the calculated and + // the last + if (lastPixel != nextPixel) + { + for (uint16_t i = lastPixel + moveDir; i != nextPixel; i += moveDir) + { + strip.SetPixelColor(i, CylonEyeColor); + } + } + strip.SetPixelColor(nextPixel, CylonEyeColor); + + lastPixel = nextPixel; + + if (param.state == AnimationState_Completed) + { + // reverse direction of movement + moveDir *= -1; + + // done, time to restart this position tracking animation/timer + animations.RestartAnimation(param.index); + } +} + +void SetupAnimations() +{ + // fade all pixels providing a tail that is longer the faster + // the pixel moves. + animations.StartAnimation(0, 5, FadeAnimUpdate); + + // take several seconds to move eye fron one side to the other + animations.StartAnimation(1, 2000, MoveAnimUpdate); +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + SetupAnimations(); +} + +void loop() +{ + // this is all that is needed to keep it running + // and avoiding using delay() is always a good thing for + // any timing related routines + animations.UpdateAnimations(); + strip.Show(); +} diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino new file mode 100644 index 0000000..45e66ec --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunFadeInOut/NeoPixelFunFadeInOut.ino @@ -0,0 +1,132 @@ +// NeoPixelFunFadeInOut +// This example will randomly pick a color and fade all pixels to that color, then +// it will fade them to black and restart over +// +// This example demonstrates the use of a single animation channel to animate all +// the pixels at once. +// +#include +#include + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 +const uint8_t AnimationChannels = 1; // we only need one as all the pixels are animated at once + +NeoPixelBus strip(PixelCount, PixelPin); +// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use. +// There are other Esp8266 alternative methods that provide more pin options, but also have +// other side effects. +//NeoPixelBus strip(PixelCount); +// +// NeoEsp8266Uart800KbpsMethod uses GPI02 instead + +NeoPixelAnimator animations(AnimationChannels); // NeoPixel animation management object + +uint16_t effectState = 0; // general purpose variable used to store effect state + + +// what is stored for state is specific to the need, in this case, the colors. +// basically what ever you need inside the animation update function +struct MyAnimationState +{ + RgbColor StartingColor; + RgbColor EndingColor; +}; + +// one entry per pixel to match the animation timing manager +MyAnimationState animationState[AnimationChannels]; + +void SetRandomSeed() +{ + uint32_t seed; + + // random works best with a seed that can use 31 bits + // analogRead on a unconnected pin tends toward less than four bits + seed = analogRead(0); + delay(1); + + for (int shifts = 3; shifts < 31; shifts += 3) + { + seed ^= analogRead(0) << shifts; + delay(1); + } + + randomSeed(seed); +} + +// simple blend function +void BlendAnimUpdate(const AnimationParam& param) +{ + // this gets called for each animation on every time step + // progress will start at 0.0 and end at 1.0 + // we use the blend function on the RgbColor to mix + // color based on the progress given to us in the animation + RgbColor updatedColor = RgbColor::LinearBlend( + animationState[param.index].StartingColor, + animationState[param.index].EndingColor, + param.progress); + + // apply the color to the strip + for (uint16_t pixel = 0; pixel < PixelCount; pixel++) + { + strip.SetPixelColor(pixel, updatedColor); + } +} + +void FadeInFadeOutRinseRepeat(float luminance) +{ + if (effectState == 0) + { + // Fade upto a random color + // we use HslColor object as it allows us to easily pick a hue + // with the same saturation and luminance so the colors picked + // will have similiar overall brightness + RgbColor target = HslColor(random(360) / 360.0f, 1.0f, luminance); + uint16_t time = random(800, 2000); + + animationState[0].StartingColor = strip.GetPixelColor(0); + animationState[0].EndingColor = target; + + animations.StartAnimation(0, time, BlendAnimUpdate); + } + else if (effectState == 1) + { + // fade to black + uint16_t time = random(600, 700); + + animationState[0].StartingColor = strip.GetPixelColor(0); + animationState[0].EndingColor = RgbColor(0); + + animations.StartAnimation(0, time, BlendAnimUpdate); + } + + // toggle to the next effect state + effectState = (effectState + 1) % 2; +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + SetRandomSeed(); +} + +void loop() +{ + if (animations.IsAnimating()) + { + // the normal loop just needs these two to run the active animations + animations.UpdateAnimations(); + strip.Show(); + } + else + { + // no animation runnning, start some + // + FadeInFadeOutRinseRepeat(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright + } +} + + + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino new file mode 100644 index 0000000..3dea4c0 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunLoop/NeoPixelFunLoop.ino @@ -0,0 +1,141 @@ +// NeoPixelFunLoop +// This example will move a trail of light around a series of pixels. +// A ring formation of pixels looks best. +// The trail will have a slowly fading tail. +// +// This will demonstrate the use of the NeoPixelAnimator. +// It shows the advanced use an animation to control the modification and +// starting of other animations. +// It also shows the normal use of animating colors. +// It also demonstrates the ability to share an animation channel rather than +// hard code them to pixels. +// + +#include +#include + + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint16_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 +const uint16_t AnimCount = PixelCount / 5 * 2 + 1; // we only need enough animations for the tail and one extra + +const uint16_t PixelFadeDuration = 300; // third of a second +// one second divide by the number of pixels = loop once a second +const uint16_t NextPixelMoveDuration = 1000 / PixelCount; // how fast we move through the pixels + +NeoGamma colorGamma; // for any fade animations, best to correct gamma + +NeoPixelBus strip(PixelCount, PixelPin); +// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use. +// There are other Esp8266 alternative methods that provide more pin options, but also have +// other side effects. +//NeoPixelBus strip(PixelCount); +// +// NeoEsp8266Uart800KbpsMethod uses GPI02 instead + +// what is stored for state is specific to the need, in this case, the colors and +// the pixel to animate; +// basically what ever you need inside the animation update function +struct MyAnimationState +{ + RgbColor StartingColor; + RgbColor EndingColor; + uint16_t IndexPixel; // which pixel this animation is effecting +}; + +NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object +MyAnimationState animationState[AnimCount]; +uint16_t frontPixel = 0; // the front of the loop +RgbColor frontColor; // the color at the front of the loop + +void SetRandomSeed() +{ + uint32_t seed; + + // random works best with a seed that can use 31 bits + // analogRead on a unconnected pin tends toward less than four bits + seed = analogRead(0); + delay(1); + + for (int shifts = 3; shifts < 31; shifts += 3) + { + seed ^= analogRead(0) << shifts; + delay(1); + } + + // Serial.println(seed); + randomSeed(seed); +} + +void FadeOutAnimUpdate(const AnimationParam& param) +{ + // this gets called for each animation on every time step + // progress will start at 0.0 and end at 1.0 + // we use the blend function on the RgbColor to mix + // color based on the progress given to us in the animation + RgbColor updatedColor = RgbColor::LinearBlend( + animationState[param.index].StartingColor, + animationState[param.index].EndingColor, + param.progress); + // apply the color to the strip + strip.SetPixelColor(animationState[param.index].IndexPixel, + colorGamma.Correct(updatedColor)); +} + +void LoopAnimUpdate(const AnimationParam& param) +{ + // wait for this animation to complete, + // we are using it as a timer of sorts + if (param.state == AnimationState_Completed) + { + // done, time to restart this position tracking animation/timer + animations.RestartAnimation(param.index); + + // pick the next pixel inline to start animating + // + frontPixel = (frontPixel + 1) % PixelCount; // increment and wrap + if (frontPixel == 0) + { + // we looped, lets pick a new front color + frontColor = HslColor(random(360) / 360.0f, 1.0f, 0.25f); + } + + uint16_t indexAnim; + // do we have an animation available to use to animate the next front pixel? + // if you see skipping, then either you are going to fast or need to increase + // the number of animation channels + if (animations.NextAvailableAnimation(&indexAnim, 1)) + { + animationState[indexAnim].StartingColor = frontColor; + animationState[indexAnim].EndingColor = RgbColor(0, 0, 0); + animationState[indexAnim].IndexPixel = frontPixel; + + animations.StartAnimation(indexAnim, PixelFadeDuration, FadeOutAnimUpdate); + } + } +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + SetRandomSeed(); + + // we use the index 0 animation to time how often we move to the next + // pixel in the strip + animations.StartAnimation(0, NextPixelMoveDuration, LoopAnimUpdate); +} + + +void loop() +{ + // this is all that is needed to keep it running + // and avoiding using delay() is always a good thing for + // any timing related routines + animations.UpdateAnimations(); + strip.Show(); +} + + + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino new file mode 100644 index 0000000..17d6b6a --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelFunRandomChange/NeoPixelFunRandomChange.ino @@ -0,0 +1,116 @@ +// NeoPixelFunRandomChange +// This example will randomly select a number pixels and then +// start an animation to blend them from their current color to +// randomly selected a color +// + +#include +#include + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +NeoPixelBus strip(PixelCount, PixelPin); +// For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use. +// There are other Esp8266 alternative methods that provide more pin options, but also have +// other side effects. +//NeoPixelBus strip(PixelCount); +// +// NeoEsp8266Uart800KbpsMethod uses GPI02 instead + +NeoPixelAnimator animations(PixelCount); // NeoPixel animation management object + +// what is stored for state is specific to the need, in this case, the colors. +// Basically what ever you need inside the animation update function +struct MyAnimationState +{ + RgbColor StartingColor; + RgbColor EndingColor; +}; + +// one entry per pixel to match the animation timing manager +MyAnimationState animationState[PixelCount]; + +void SetRandomSeed() +{ + uint32_t seed; + + // random works best with a seed that can use 31 bits + // analogRead on a unconnected pin tends toward less than four bits + seed = analogRead(0); + delay(1); + + for (int shifts = 3; shifts < 31; shifts += 3) + { + seed ^= analogRead(0) << shifts; + delay(1); + } + + // Serial.println(seed); + randomSeed(seed); +} + +// simple blend function +void BlendAnimUpdate(const AnimationParam& param) +{ + // this gets called for each animation on every time step + // progress will start at 0.0 and end at 1.0 + // we use the blend function on the RgbColor to mix + // color based on the progress given to us in the animation + RgbColor updatedColor = RgbColor::LinearBlend( + animationState[param.index].StartingColor, + animationState[param.index].EndingColor, + param.progress); + // apply the color to the strip + strip.SetPixelColor(param.index, updatedColor); +} + +void PickRandom(float luminance) +{ + // pick random count of pixels to animate + uint16_t count = random(PixelCount); + while (count > 0) + { + // pick a random pixel + uint16_t pixel = random(PixelCount); + + // pick random time and random color + // we use HslColor object as it allows us to easily pick a color + // with the same saturation and luminance + uint16_t time = random(100, 400); + animationState[pixel].StartingColor = strip.GetPixelColor(pixel); + animationState[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance); + + animations.StartAnimation(pixel, time, BlendAnimUpdate); + + count--; + } +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + SetRandomSeed(); +} + + +void loop() +{ + if (animations.IsAnimating()) + { + // the normal loop just needs these two to run the active animations + animations.UpdateAnimations(); + strip.Show(); + } + else + { + // no animations runnning, start some + // + PickRandom(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright + } +} + + + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino new file mode 100644 index 0000000..bdc9af7 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/animations/NeoPixelRotateLoop/NeoPixelRotateLoop.ino @@ -0,0 +1,97 @@ +// NeoPixelFunLoop +// This example will move a trail of light around a series of pixels. +// A ring formation of pixels looks best. +// The trail will have a slowly fading tail. +// +// This will demonstrate the use of the RotateRight method. +// + +#include +#include + + +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip +const uint16_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 +const uint16_t AnimCount = 1; // we only need one +const uint16_t TailLength = 6; // length of the tail, must be shorter than PixelCount +const float MaxLightness = 0.4f; // max lightness at the head of the tail (0.5f is full bright) + +NeoGamma colorGamma; // for any fade animations, best to correct gamma + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object + +void SetRandomSeed() +{ + uint32_t seed; + + // random works best with a seed that can use 31 bits + // analogRead on a unconnected pin tends toward less than four bits + seed = analogRead(0); + delay(1); + + for (int shifts = 3; shifts < 31; shifts += 3) + { + seed ^= analogRead(0) << shifts; + delay(1); + } + + // Serial.println(seed); + randomSeed(seed); +} + +void LoopAnimUpdate(const AnimationParam& param) +{ + // wait for this animation to complete, + // we are using it as a timer of sorts + if (param.state == AnimationState_Completed) + { + // done, time to restart this position tracking animation/timer + animations.RestartAnimation(param.index); + + // rotate the complete strip one pixel to the right on every update + strip.RotateRight(1); + } +} + +void DrawTailPixels() +{ + // using Hsl as it makes it easy to pick from similiar saturated colors + float hue = random(360) / 360.0f; + for (uint16_t index = 0; index < strip.PixelCount() && index <= TailLength; index++) + { + float lightness = index * MaxLightness / TailLength; + RgbColor color = HslColor(hue, 1.0f, lightness); + + strip.SetPixelColor(index, colorGamma.Correct(color)); + } +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + SetRandomSeed(); + + // Draw the tail that will be rotated through all the rest of the pixels + DrawTailPixels(); + + // we use the index 0 animation to time how often we rotate all the pixels + animations.StartAnimation(0, 66, LoopAnimUpdate); +} + + +void loop() +{ + // this is all that is needed to keep it running + // and avoiding using delay() is always a good thing for + // any timing related routines + animations.UpdateAnimations(); + strip.Show(); +} + + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino new file mode 100644 index 0000000..56071b2 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/NeoPixelBitmap.ino @@ -0,0 +1,98 @@ +// NeoPixelBuffer +// This example will animate pixels using a bitmap stored on a SD card +// +// +// This will demonstrate the use of the NeoBitmapFile object +// NOTE: The images provided in the example directory should be copied to +// the root of the SD card so the below code will find it. +// NOTE: This sample and the included images were built for a 144 pixel strip so +// running this with a smaller string may not look as interesting. Try providing +// your own 24 bit bitmap for better results. + +#include +#include +#include +#include + +const int chipSelect = D8; // make sure to set this to your SD carder reader CS + +//typedef NeoGrbFeature MyPixelColorFeature; +typedef NeoGrbwFeature MyPixelColorFeature; + +const uint16_t PixelCount = 144; // the sample images are meant for 144 pixels +const uint16_t PixelPin = 2; +const uint16_t AnimCount = 1; // we only need one + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); +NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object + +// our NeoBitmapFile will use the same color feature as NeoPixelBus and +// we want it to use the SD File object +NeoBitmapFile image; + +uint16_t animState; + +void LoopAnimUpdate(const AnimationParam& param) +{ + // wait for this animation to complete, + // we are using it as a timer of sorts + if (param.state == AnimationState_Completed) + { + // done, time to restart this position tracking animation/timer + animations.RestartAnimation(param.index); + + // draw the complete row at animState to the complete strip + image.Blt(strip, 0, 0, animState, image.Width()); + animState = (animState + 1) % image.Height(); // increment and wrap + } +} + +void setup() { + Serial.begin(115200); + while (!Serial); // wait for serial attach + + strip.Begin(); + strip.Show(); + + Serial.print("Initializing SD card..."); + + // see if the card is present and can be initialized: + if (!SD.begin(chipSelect)) + { + Serial.println("Card failed, or not present"); + // don't do anything more: + return; + } + Serial.println("card initialized."); + + // open the file + File bitmapFile = SD.open("strings.bmp"); + if (!bitmapFile) + { + Serial.println("File open fail, or not present"); + // don't do anything more: + return; + } + + // initialize the image with the file + if (!image.Begin(bitmapFile)) + { + Serial.println("File format fail, not a supported bitmap"); + // don't do anything more: + return; + } + + animState = 0; + // we use the index 0 animation to time how often we rotate all the pixels + animations.StartAnimation(0, 30, LoopAnimUpdate); +} + +void loop() { + // this is all that is needed to keep it running + // and avoiding using delay() is always a good thing for + // any timing related routines + animations.UpdateAnimations(); + strip.Show(); +} \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/Strings.bmp b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/Strings.bmp new file mode 100644 index 0000000000000000000000000000000000000000..0ee3be6f14647c2b65a307451aeb70aaa1260c2f GIT binary patch literal 62262 zcmZ?rHT%o}24)Nl3>pj!3=^Q3kwF414kC{5F))BIh)pEsVP&XbV`yMxP#{tRnaZpL z89YQ8BBU5H7;IP>ir^8% z%23C`kjBE007Dt93^hoyHOve_j0|#k%r!JJtZS&x$;)MAV#K426e$)~h5|N*1||j_ zQgqQwtpE#y0w;q3A1Ge@#2Mmb7;+RCYE&?zlBZowrboq~UBRP8DymK_saP;8ou@dO zJ;{;DL6t#@pClIwF*5|ga}x_g7BhnbBZCUI9K^)PAjZUC#KMpOk2n^FIOJju*o6z!L& zL6FTNHSL*d5`AiH&ES%@LYW~=jv-8v!A*q0OaPRTMOYbFu%>VhW(E&Pu3=@UVP*(m zWRM}+F9OUAPLK+Pm7$KAA%KB_3sbMQjy5EVF|!~F1WaWliLf%{(4!V3sFmP~nOw<>0kamMw#?9)q49tB5`qC{o-+KoOK7$54U0ek@W1 zRgPX_3>E?mYTS$>=18IKg*CJZIhlchfs2{J3z9ck8A`yp5?xbLN^)I8y{DHax&*m= zNG-<95JIj2IQ4O{G6-;jqCiQQ!9W@m3l6FbF6s;cdJJKP3<>5888!?BjwtmGJVuZi zRc@e&3(#Y*QUtXqIPkYYxR@BE*co(qL5&tjp;~}eIqESsgfcaxGS{Us0|OIG97GF?2v^lq*EQ5Dt0;r`RKPr}40Wsw zHKe979ySIU0Z?|ZQDSh@0L4O-2`Cl{92v@7uw)A4D8OWwxiI9}gR}?fgG{qhWYCfT z)kH)$?eGQ!69aTpjW`iAj8T~0&5QlA-gUvA-=AmJ|QWQ zm5qQQ$hvT`85z{T4f`xi1rodrPHLcn3oS>~c|zkN!~m2XAhDn!4vGaKE(RXLy%S8U zXeh$Spaf~FgKHTzd^QQNFsN~ZVlYCAAx{B41C}T;gh(<*66-(?CI%%g1}i~Od!|GQ5q)*4pzekaFE(@7SQw%(qfA&t zxTvJKuAx3FCtF=Z9bF?M69XGcDT=NDl@BQ^m>6`J7(oq#5@%>}2N@qw5E?=egzPbR z(A6Qtd|mnO%+fthxhSQx@Eql}w}8#3ro*HE8Vknio| zZSP!$T71pn_HMeGFn|jJsg4J%PRs&I{_vmgPIU~gTHoEgBPe` zQo-s30%i;%X+{PWNcW0`A&Y?lrRt}eD|whfgQ5*;pn)>%hO)6Rq=6d-ai9!=q=u1+ zQCvb?-#{ON%q`48@fBBqq=W!dNl3&=sliVxC(Lk&7JUd0GBH@eDy%4M!%7I1WU}Nq zK!ehd#$<&usF8`&AU;Tok%_?$rxJ)nQVA%^#EBYUQ4wLNac6N=Y0wg>quSUc#2IuY zIT#pNm>Gg#jdmxrR85gvm>57~n{FZ~jm8Km1_AuNcLs1HzmAom4%aXv)K!))p!jkP zf^u7rY1p|) z5i}$s#zv$S%nUBzQZWNx`r#E|NUs7#nT-b?W4TzNjdnj>2FO_bkc%%y1~C?f6mXvE zVrQ5S?qNyc@iI|TikuAIVxZXp$b=4r%u@hO0LhWi>W10H!pe{b9+AMCiX`QrQ5KuW zpkud z1A~SEgMu1^h(v%9TZbQmku1m<$g~dio4z2kkuVD*XgpSm9mgC8l1jWxMg}!VfdHP) z@Boc3;Z;wRqznf%Pt~b1#LIvxEX=MIQ3hcuVu4KD5N`i4GBK#>G5AC=WY&P93=(l* zlCPw}eqw`DR~cti3WIwHlUX~f;$#LcElkU(Bcj0#no5TEG#k`F6IBvq41sbmGX#JO zzy=nEG)4xBOFss1#o{Ic?FHv3;BE4e85oe%MM8DM%Ellf!Jw)G%3n@?jD9h;bvcgx zHFCA!te4Zkn6sWKXFo$uKSOj5gP|1z2bKvqGTkc04vHyAEl{Tlil#I<=)`f23aCOS z+FWq90?pck8xSY^YAc;%P^?wFqyS8`>th3?}BE-)Cy3>#A-cBf_l8r##MdiGY+9C}JBp1ywDO{tLK$m4ZA;fo2Rj zTt9J;)oF6%&Mc$b$;co9Ue{5?#?Z*Yu$PUYm6f54nZb{eF)^Z>5L2*^CocHiDr0cf@-b^jAe{i8k{~+WX2cqkp*$K1Uo35 z*QlT_5T)e+I|Bo#0BleLjW0pmKmp0bU;&9KR)z{@20ug@MS+C35rGFL(o10yL-P#s=Sus5iI8WKqM z5@rgqf|}Fd(us+|0y1jB!VpGw9ZlFq(DHU}kfjZ3p!PgDJwecLE;!uk7#V2NZ;dhm z#g`sN7n6}e23((#&iS3mm;hTC9DdSj14}Zd;=TPSf3ITZBHFz=P*nyTD%Ly_dCTdw2K_k5m!k|hGGKma{y9PDT zey9j3(8^&s4h9xRW(Gg-Qg)iuTnr4LCAbY1&pel@@hgG;hpMf#;UXn?L1EkA#gJsdfZ6WF zr3;rh3lnJeHq9CoP>@(NkVdN7P_;qoFD3>Pkgx~lj)waZeoTeZIN&t`&GRu}h4O<_DiNOlosCENsfMYgBP-8Me3giXIoSFfal^Ag4 z_!*FtgWg}}HDylT#h9~>fnWn(fs+BUsI^3i@IWqpr+{Qdz_WOD;B^-uJ_1Kcfhq;O ztq%Cgt}sKKSpZ=mSu724MAepH&VV*T&d8tvPHa)w&E{hPb?X|`K%>fpmc3y&fdSMw zgsk92)TeA5jM2SJIs2JhTXE@virWZ6(@Y$7W{V*E0_p`{3(X=-E)N^14-4sZ7de3{HCU4aX{#d>c+xyZ8dN?ssDTz6 z5ZdU6X%U);jwC2?MiqJpmp3q^RQogjXk#@)j>0u~sBQoa$FqXgpP+S6 zkyP=4TOD<($PHs2HqeL!q)0~@O+wN~Yo-J*1AG^ctC|isQv(YFc-&Qp6*P1JKTD)S z88muKLC!+vb0NDD((15- z_Al6&K^xB@O?T?H29fO~h7GCIs@xcA+&OFvt3=!j7&4G{wdW~-b{k<`uR@IJm}=DY z7;+jkn(DF=wF(_XL^&G3+mw*hF)@I)U_dG?2Vo=$sxWyV%|gPf8dSJIrAmns0~4s8 ziZKNh3psXF=x0&E8!{Z+Zo=m6a+=e^lqROCO|@cg051lnUX=lLq=Fi#pOw?VsAm>t z$dG5J=AzP|z#j*dWncjHdg5e20ac>{>PjFasKa7ra07Q+0*Ep$2Rxjp#p9#~imVc6 zT)WZ|mP z5NqsXr3khMzQn6UiIP?**et9B8)PRPf&DdbttR{o4QhhfvUQ#eb)KLTB;Yc1Vz4oS z4#{y70Zod5BM6j|Aqzje#6Y{7S@?LE8;qr@>O5E)yg>UAOy#Il0tkpOB$t5(osfoe znHjuP1RGK+q?Ajb|gv!f$PZrpM4 zFhDl(d4wVR3bJ5KOH89AoYi%XlKyNp_EcHwwl0vYV}h)jV`NZ65+uOX3>UUvqUKUB zg_xoupo1)FNIr5AL7(=jQw7DEiwLMEh@-IwNrcP{K|)*%k;b5foydp9K^jPQ${3S1 zxSYw&#~>jKS{0wuz~CE$Hueq~ZeR5dA>4#2SuX<=uUy~IhYtUxIr6;Qe>emVR(s0+WZI~*k4uf)j>>*prYGG z2^4pb-Y#S)GS40~D+lRoL!5wxwDDjl?`5c+!VsLqz=}Rx3~H*$RWej*Ff^!fMoT8y zGB$XD7L;P0Cc|PlDMFAX#Lxk4;xt0m`MYO=`_2hOZSN&uFb^{*jyy!s>mztRQsDx% zxCoenE)7}D4nE2fU5bqvG(#Go2bvOv#9D%hRaci!^BCgu@*u!Xn(z;!y!PdL`vR znJcLTt&IWF$;1FkW(_XPSy@bM*!QG!u`vG$h!etpX+Fg=n}80E=t!K>M~8%Ak{G!D2KbU<(_Hh&10u3FJYPq9qh1e z6Y~*cXi#G;@dPi<#JN0`i4in+0I4N|^pS#&NH>9%LFQq>E8)R>;t0s@WwfuQKk%9IDxTz z8mf`T6lB*P){Ypm&5+^i1al-Y2Om&;>7#BOfXq--C_|b_uswRjuJmJK1Wk(*S)GAT z^@JRqgzsE^Bo{-Okahkg&gkp7pwe)Pi2*cP?jr_T@0X(hs-#>*K${e(*-D37Ml6E~ zd_+9>6knWLBzQq-zQGIiMuUh9P<$cpwqs!gohRI&20C;RY#3~&63ii*(2;~zXHDLq zIdL5Ncpyux$=GWH+4N*bt@EUDxQ}MSEciQO94yc&axdg{_iP-X$jYoisk|VKs}xyS zH4Q0jOUQ7!1TRCmE5ojAhMqvsLGc(<^K7tc3;pOnf_6#pf=a>~chDgQ1QicX1&~D@ z%nVpMVoZ$CE>eaKYGWI+fh#Nx;Ugvn=+Z$2_<#k*o=Svj!YmPPhIDIazOM5G4PL=w z4H{YCYa?(k&%j{=WD*&=d<%z~K`sm%dCS9eCuFRz#F+tYBL-;YXHEkH(mWSvrWkb} z0r=t>N-yIO;sV7KWXVvSCxe>?8xJ$+@O<(jOHB-vxhh=AoioA=paBwrtaC%#cn%r6 zu5tqn-J`q9BMej?sv(Uhgh_%Lx+aLrU?BZ*7KQ|Ly`=MnxIm?1tp{7MZj!TdgRxYO zI1kA=i-!%`=OVK4NxC!Xr-PZn1Kbfqj8s4(tIiY0jtt1wej(UU8xsR`SsK#9U@FEJ zI3T!$Ig$(|L5Ip#x$!i3fkyw3&(8s?!AY<&L)WXLG;wh%Cr^TpiNT76Apv~yOdTsj z5eq{c6KIz-@(><*CZp?u><4FMD1&U3B@kH*44}-FS%WY|h69wj(3dwuw%?L4dx$U) zodsG{3Z5>>5#;3Y(1dmd8@xchY%4{K;u=*u9|ttDuoUd5D#_*XKvvK}+QXP63qt}i zg9aqo4mqQekwM8p6;#OM%vlgaa~c>Tk?w2I10S;xfp#tcMePlUJ>Y%EN{|sJ7KSwV zf&3gSpplk5dyrEh{k0S;(0v?gVu-k7VgxNVc2S4U-IO?kHb26Rr51ycK?~BrW(8du z6$CzbK?I!a`4}10m>K*ajt9?PQ8U(<8JyS|8Z71gAR`P~h|3VM`7EaabY>S^#zFv; zztCqQAn`@)crx5DWCjz137)euATx9k#?by6B;v4=5yoVUOd>l3I~(;V56rPhh%OHB z9S{}l3{zPd>Ie=cLG%zL*_h;vr5b!RimKf3^ipBQpxqY$S&NRo@)P|U4wz;lX(na{ zFG$&fwXcC}3JVixYSm2x)cb(%VQlbX$g&0P;wRg{*60wXSr-1=^Bno@}Vt_1;BI`ax(AH⪚mGI zV1O+E23(8-(x?WvD2Q0>`_KR%WQf{7XAxSR&iA-y0L z7#n~mP=e6QP}pT{M2-K04FVD1fkDvW=ipP0(dtByg28|xtBApS*hv^qWMq(nED8cw zXGvrnHfx~(9YcsQW#C{2Em;9q>x6oNkInCY3Pw$1&J>fr@%L z6$acFFhP#%0jDU;a3xZN4SZNS=nh|2h6?bZrug!yf)MBuVaRe9BHJ19T0OoC?~yO2}w4C3O(k zl?)6FLXavRTs&rB?V~~Fm>?r^Y1ZhiT*yEN;ZQix*qx#3^Ga#LSon!Ny1Fy6#$(rtZxCzV*YW^ z+Ylh;WY#bQCV=uC=vo9W@ZB7QCwXD!5}?6{F@i2=0LL22h0Dq!42`h6)IBsIWgv=a zkm)dH20|@_oCXwa3=E)Sx*;c7;XLS1go(j|nIQm%oWMI|$v=w#;Q%%c23<2y{~Z#x zISrsf&&iKLQGpnzrqcjYP| ziJ6@#$WU4+#fqWb546Fd#vSL9KVk>{Fyo7XfdO(_HDdmY0_PK{RZNP($q%}~IHv)$ z;ljxel#fU{1CtoLSlK|a?;6CAUd5QR8hnucOwjss4Fi-JKw^x-r-nn4$4O7T#!0Ec zO|>rCC?Lm9DB6TU5_h7eD8AtH%Sfa2_*_ScoT3IO*@F{2sG{+WVNljWFQO?iAD3Qc zP8P)pEUqgU(i=hc=72V6gUV0%&^IoPc*U6*L5E6Z*npaURc<^n#(rkfb<#WyY@o}{ zgYfKSqbR;0C!j)h`cw6|b%b9TnLt-aK$apvGL)qYddml)niecB7KTt3hImj5M8||7 zEDaiKISmXUDIm9zn6=rMK?9)$j-Z%=H2>T*7(_tFBnZJT0)W(O;59br9UREoOTyQY z=QMyEj5RzWz(>bmoRff6Eop*kdZ1GiA$cmP1XTMo6T7v6G>dSn26vrmP{Kz{iowAL z(Ktye0Zrx-aoY$RGiY%UeDtiq5fpJ4I}~`p2hKz5HCBc?7KS)*^Gu78K?&SftzZQm zgFtA?2Ce^v>_Gt*XkUzibB&PA1F_{)7?AUnz6Dil4Ui2)W5^^?4f-)GtZbk*4`cyt zP6Mb)b@Br>M{$^|BMI$KK{kEpNMdRGA!%h{V$cD1c0m0#NSg#s5;Y)(HXem!G>8e_ z6bxDcOZ-A~kTz^sTn153LGl!d1r9btDH4M$f(7?*v6&;M0_vDU3W1yk5F2?Qhyk>p z9kPa`!3%VJ1j>E{Z04bgaWOIIFf%x@Foc0KniH0zu~0QYc{vTBh8dKDjdBqI&9GFd zFpz!vihu}%R|K?nNUZ>!B1_tk5jIy*LyU<*2V9{bo%M}rEWZ$FaSP-M!kh+B;h?I+ z$i(EN2Azy6a{=8$jeScUrfHOmK>9fNlX?hvMMse$@&PhL+04zy;2Z!s6arMR*m&R> z8Y0R-Dl1}vjGhr5t!3k2FtP#lGIAOia~lk*3X(G|YP?x|bQlP1S*5ar8KCVVITd^c zvoJzWG9>B37jYR-9OX2C;>$UJq7E-U$5AB5%-{shV)%ADA}nBLVbM0Qtje;V*kIY) zz?)kE8Y|->dA$(AKr&dmW}uQPGK)aH&&32fkP33u9WgC@HV#lXs;mc85)wRylo)4o zF@g4?>hOY=9NP(jwrHtvf$o;#qF~euWG@?R@(XE&7)Tro=3xU(n>Khc#Dr_`M1W7k zg*Qk<;G=L@G*VNDkqMf`ocstF3qK$wM*;0v38+S10nk*MgAeqC0Z2_vc~uoD#jx6l zVJ`SuPxw{^WJZA^XqT>q0BGYXj)N4SF2qJbYB5%Z67+^5s$%%+=o~xPiefP-P-=t^ zaQVl9Mo#e8EU0FZ$rBKP7F>Y|1bVFSBVJ3C7{u5>Bd*d4px%>v2&8NWWmQNtL55Y$ z96)NwbQbu$qcBN^1^NsJEE(qMgUaLpanRw?4#J>)TM<&AJ>X@^pym)f`f9*OGwJbS z&IjRf1*D+}z9PBSzLw+14;rc(1R!|ltG7>;VJh-SV6_3pE$I{hQu8y5eGV#m;p4kifcMUMgTNw zQR5C7ufP_B%q*a*kdX&Tl1e~rEoCh%_ak%hfVz0oH#5xJ&5+jw4a4**=rAUcIh2h9 zWIjB1!(#!Hk=wv9y@z4VB!(3|3}pqNqb&6;Kv$Gv^9Pw?Y>d#rPb2X(MLrf#{75fKScQR5$nC@LY*0U22Kj|0t>Bagyy^D$@{A%^LEqHvBA!d*rxLkD)FYnD8y zIwMs(F7=?LBC4Q46{_rf&LnJBPxDCId5_iExkr%j_K%33sXXiprWX0$OFfkagG8C{gG}tPD zCLHDXev|?AfxE43^gnaY0L~hObk}w)kGpx zcN)S#$WTUd8LbBK_*g(&xgpKz2r0-$b%gCivaAF_2RFHk5~-Y0Whz{tP8`I|70RGv ztC3DWhMaC~Cdb9ZV8qN2z`~FPi8VBmm7#!zA%%q@jG4iUiNTJE!2rBIN(8ylLZ&+d z!J{b1yD7;u2)8ypUc|vZMtrax(74s3$v{?Tp>McAQ$%g91UurI14yi~Fe(Uv`dk&R zXdPq@Mg|oo1~X;`4;F?f7KSXy$q8sN2LD{Fucf$4HflT;819XIphZ@4b06O3Ta>j3i8tC8)oU7vyIzTKX@X6#+NK1}EqIARxoD2yv z{2glcOVk`^DzTa3UeZLAqgWUiM8HvL0$W%M+E9QTcgzg<*5QEeG0bTIb$*ep<6#Ch zZUe+|9t8u*EDdU)qXRrdv5)s68wg=DF@T2CA@>8;se+B|a9+R zK@D_RN{lpUWYtUn)Q&RX1GRyXW3xsDv_=u584Pj@CWc&t3qL+OLJE{mF_l3?ASaCZ zL9Z`|h|!sZ+!bEt!oX$-tBdPYK^M;vxU`o%X9~bqPq8voflEjXb4=_&wHRaq1+tVE za^5NAEKU5eAi)kga|sgoISM3Pl_JCnYNf-wt4Xq;3n(#1aUu7=&|%#pMyNmp;5WNL z_oZ?$G5CS6=Y}L>4bTELh)SxGJS+?m;2wGjmP5KA2ePGBfQCEtctK;K_~QuEWnyd$ zY2eOFi4p@z>pFOtL8nYZN=V4rXmK*2q-(?nx}Xy>vQeQ-{Q*f#FJKdaN0y&1sk~O#0WY+9CD&K_G|c<7(m0tkm9IF5tPU=453&6zMe!3-6+WE%GRhi zDxxbQ#AjjvB{sDpX!4i%raoDIp{^##mC7#a z(8Hn|yg595q41_tQV7;e2J$jESjrmN~y zX;Z3`;1Fb`0-T`pvJ9jdT+~7Pg|lp-`@7-CdSuyxq?Cm5ub@UY5|u5)3hfHzC{QcP z@WmJ8q%Tw_kk5l0nOLNVG0IK8k;Ld^VFInGP!eWHwE``;jWGosX92my93C&oj2t`A zfnx^Jh{JP;u?43Z&Wr4Kab%+KJ+%&1}{)?muC+;Nz6wF)bP^~2SqEsh6Dl2aZAIaEKY{x z!EFpvAuF}O`&BXIAjd3Y2$L_s#{wCtH`3a5A@FHkdCUwB@Pi8&8C1ZV zJ_Ja*+5_wm(g`{6xpo!GlvHF)(ES%U_ESSnf+gfMc?Ef66JvV^dwU0aeFJ@d0mM0$ z*tJ4NZ!+XS>jbc?Ko@0U0=3y6tW z7-Aqxm{6mm1iT3@kD0-RnZXHiZ~+TL91BAloh`U?$D1S zgoP$yng@KiL>>I3L3AC`3ZO1Cx+s)yYiA3Irh=kEZy#?+{tAl-uc@o8Yp74lNM~hZ zg(}6Qq}V~t`vx@z9HXO5jG%+#;G5kb_WE34H4b{DqC1NmJ`dzPB5*DvM?XRj(8sr)d zgq2WMTta+ZL%q9)JCu)&@(=K@Yp8d1bHyeJ6N5MJeL$nld>o+mHY8j5=%6oR0iWB6 z$W7p^0}UZP@w9L$Bi+Xc4_GNl6Nf*F!h zl64LBWfkS@9PD5L?1a36Jfs%GE)5q|;Dl6UithZ7Mn#D;sOEypGk|VBv4c18z|rJ{ z=TH$OjZ|Yo4sHVP{~~cMr~nIS3LY{8TcM1rvL@(I$hv*((_YA$A$uZ_MHm=D!b0mB z>YZJjQH9ZY%q+}RHPv+u_4uO|q|Ja&x?jyETaN1Sp@O1V@TsL0p$o~K_S7G zw$`SWW_-99YLHRGlLH}%JLAUF&iU;90w?kg-Jq(n;_BDpa$CFVI@e7sZ}0G zT|gjnv9dwe(hHz8jM+KZi%UzITU#uxEb;r&*~PiBxv8w8oR^Ommr8hKK^Lx=2|x-E z#vC73nJVyFAmn?Ja2Z24aW-bYI982vX1fB$FeipQRZu|(k08iVS`kv9EJl@GPGE0A z&ZuNzz}@EpD+BE?%4q=YzJzd*NM#jeNY6`L0_Tb{O)X7`8bu{VB&{e+NMu#Hp&s`q z#m<=L2)Y-zA(4MsRWî_Nl&bwX?mEo=-uh#ZYoJ0lbHKu9ektn!{-o^=iNMJ2_;BEpyoOwCLo zy)#=oTTEGq2t2aTj*Dkx5MyDhVfM&psIda2-a1v#k{c^Q(pFeO>>!d9U;)L84tR-( zml$Y~e3m?-0z%F{4Qil5FG`BRO^sPElvSddm8Bk1LV{1_qW*CWj0{>F46E1~RuE{$ zLx$z!3P6k5u=$#mjWr=D5z?yH)zf8WVFq2L!NjPjq!<_!2pOcZwz0;h6(oi$vOouy zGNgcaoBDtTu{}hghlxP~tw0g9I#G=qbfgBUOLzEKKnqZ{ctCZHn+PaJB*}sTA7V0A zvP>D2r-CFvTQ7BZL6#zIO9A-K()*Kz~`v9urgHQ957^H0L4*K z3819_JKhMecm%1{TM@xiSa zva%QXHa*;G^$qkP84XffB_t)<+Sy_)Bq3(NBMaKVfQUj#XhR4*vIG@Ep+s0gOTNP- zLCGGz`4JxMXpB4sP;m+uEKveA*rKFBITUjAjRGgAa-?d*laWD{l_3Yddk7qBTnm^P{J?n%R(<30Hy01+94g3>4yro1RFNSr!VOxa)ZhiWWCwh( zFcveAMpFs76N~O4B*etPAjHU^z{H>jx%L7~>VXF?1i+WR5$6{!9?)rN<-H7LJ)m=M zh|@$+74q(Y96R)@CE(h@=Q34+D^|j%)xmX*Tm~evavDHUCMrp-6C*{qLF<$tGg$$8 zcn;X)Vr3`*cen_h)HiZt2+8Njkwv95Fp;xWkrUcX7LwH)I1(L2vT|`5&<PcvMhhpvsY_ zNX{Xj$;bpc77TJMUPK1vWguEi<=GR+R?G};uzN)_@J%O???VPqet|6h&uIXiT;~-5 zx+_Y<05pybAx-Q+BF+JzDmyX@dKxEMY-ZMg#!)@OK<9NUYcZfsRZ!wO67+KOfl?8~ zVb*RSf03XWT`6Wv8AyZH#GtE#@>#%VE`c_@gD;zNpkXm+?gYwKNhP3T{fIm#ked&5 zFoKQ==x}o180cwQkbr`dnKhu}%H2aiF{r8oI?<8x!)&2}gF$g{fwC23ia((Ubh8hJ z65<7T*gzLdz;_em*@Fs0)OBshhKYc?{Lq^wSQs)WA6`Q?9i7d=1(!xFu68Bl9>f`v@fp!=n!2sJy7}V6#*R&gqY(M0cs8r<1h?0 zJZzw4>RGm+VWkEy&=SD_J3W*c88G-D84A%d5vr!9yTKr zs9salU@)`-#ZGV%=>9lJXp>Ebq=2r`Avd4uNHWBjA~qftI5Jo%qO4*9-?yj0#9)WE zaGjN*iiIHxyxNw&0~clvp!hO#z!S{0m15%nWf?^c(3#1WE)1?gptE^HQa}yzxB^5D zg5RwfkpVj5*u)Oh?*LuGNRjIx?eiLUP%)^4GM~%_-u;mRY3xBtJn%SJ4l{!rBZCq> wHtAsX*?`9vR#(wWFvEtyOdjhP1rub}0DMzb1~Y>PBZC&LI#VQhc4WsF0E{+0tpET3 literal 0 HcmV?d00001 diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/StringsW.bmp b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBitmap/StringsW.bmp new file mode 100644 index 0000000000000000000000000000000000000000..8e45e137e025eb263556e0af063c79421507b06e GIT binary patch literal 82998 zcmZ?rHFIHP00A=w1_liV28Ibx%*dbs5@ujX1-d_#dImT?zAv9To~=9v9>Wne)qCWu~QrJgfGA_hz}CC zx6ofgs`*AnrVF(-qSvY@)b5m*oOM)Mc>Q@$*ki-8VoOdbN>4bbp_0AX(9miTDR#}8 zGyB-O4eKxD=H+fCMISk8)s)J%f!q)B4>|g2smIXDc#)2;*$RzVi?u3wR+|-SZFb6b z+3uH|VSiL?nd2#ZX$vMNw#4C-)HM4evhB8e6)UW_!_t?&qX|mY1%kx*+JTXD~ z(3%{Pv#|66qG4$!%tdQ8IsOIZQ&2d|h%Gw>3Tp&je_l>v&M|qZX-8mKPGatHX_2+( z5NbAD0P&S%yY}m7g%M?cc6Rn=Q2JT8XwgYiQ`3dyxS3czMurv(L1`W2UQM;M4aDjj zEb8>!OqXd!n6FXIvfKnpAE0z1HP!yG_(F#hB5R$_!O{^jEwaJsg2HCIs|H)FZn~{C zzLmIC`)=txwR??Ily3KS$=#S!CwXORmeh&L5XBuqj@qlp^{bw)$8u1-BbPIB;`5KI zDpu~$(uiG)t6TwAZT;Y+Zza^wx_dsNq&YAexI@gj`>pY0qrvKD$pV12# z_StLv%yyUg1MO|rH>EZ?Tmq#x(N&ITCFa;4lj*ZNAYX5@OS!;m3#csA@itv@Or+zl!>K;a6am1H~i>1c(Jl@|5&+?J^-RPThfH9+YRWS)V(JxUph9PX)U zsT)A)2b7M?Ei7=l6(Mo@bhpq1N2v^NDY2IoZK=n^`sKU;k zSegBEQY4P8$`(bbhd^}^49`y!Ki(Q6yDvUbW2b$p;Z{)E0HqO7S^>AeRym#pr6GJ6 zREL7wUd=Xpl#8sks>fTbQ?kn0g&fbya?N}2#WN}X1(^df50rkSg*RM~5nX;7RF2^? zW9F<`M?q<*skvznE;ZB=2i4CYHz~^W?5CEQ#FztWhl1K3pfmt#vu63KZUUtPP~A}* zrm(XkT6SMwoXmk~Nm57Vrb--Nk|A~qy?p{p6WC~wT2NX7nNt#|v<+m3t+~NsV%(;0 zZ?Z%?$ZVBbn&k#i+Zh|me1WiveElv^orkW^+uM8j>a}anfznV+Z0uTed1~@O zZ9H(g7Fl}^l+GzJ8&ocYIBBf{l?i1*O55tg<#&PN57agVr2|kJSeY$)23vfSA_g*J zQHJEzU-f~j41_{&=# z@%ENK5?B#*4LyCq_@J~WJ<;wEsH_8}OKpF%l_0b5*@aCGlwMR7E4G8b|pmYc-pFn8>R91oPCB}WURs+gs z3R05~fx--gL2VLH9D>RjP`Uu64^W$4rpITljn}v_Br5?Wy3A^Q1eb-N^o*W{Ky9I=%a@-9rJ?x?7o12)Ok9u6EF)9Xg$CBPWY=@B@)$H0WNKox z5Y(1ko+Wx3mTq8UX`pgsxTh8L@I{UzQ2GI-BT#xmS4Ry#s15`52|?)wlzw382{fJn zN^jWg0J%j`rtbi#4#Ad&K;;l@Y#Nk~7A;wPs-?AcPeoxUVuRUep@B6$>+Zb0P>sP96I8N*QxC_jVR7m&DFd4`_j9H9COG(G?;mq2ZATwx3v z4+gcR!R{1Wb_&!+LsknK3j(#NVPz309U)^-`jM`ia1dD!DQr+Zt}eOiYPwy?T~OKq zwPj-6HE7!RBgK6Kp%yg80vpo-wGFYgQEBZi(0mK1y#y+UK;sDb>;sjPpm`Bc8Uod? zpmGUaJ!p*D-`{^FXnY!mLFo#Ve#H8goI+PmEI-)ZVRMlFv}Z|{O;135s*zSlp@$Qw zFAA!wz;Pix@en9((AChA51I=G%?-o)ZlE!7P~VP_o%(t%OF?xQ$laiRJ|XomdC`f> zPlM8smLKV3<)CtAb+-70P{XM&>{RF71+{-*=8hy9)CULE1CY6^4Hv+1pisRNTRx_C zxPZojK=XN^ItsmS1}c9*{WoIm1oiD9X@C8BLUU#?GeK=1Q2LRr>>_C#+1pNMIcTgF zRA*VM&b$r^Gtj&X%-oSegXS#3VGiwwg8HMN`9WIxACx9QW7MF&7idiisE&eR&>RV9 z3>&l#fgCr0){21Q64bvZCjIL4apZZ?k2`g&^RruZw?x}gU!c}r1S%t z?*^@50fn`s;Eu~;yvJ{Y%70z$AhPB>35C06l=&LD7Mnewbpo)o0;56Y4rr|ksO}f!P0w7vXS zw#KU*+*T-bIonSKyj~5q=7gSoRBZkNg*9mY5`;l)Y-%VG1NAjwWi6=A0MvsTQbQfcf1o-UoOXma zTmY>-qlUR4GeGm-ptTdA^aGj~2Z_NjC~QIbQ%gH?tww0j>}+L@dvbqHN8tRIWq|5A@gTn|b^pz%pV zbLy?9g_SAbdMVoy+})I2Y}KLXe}1Lv<51xA$=>z7$C@Bpm{8G^Ju{b|_kj8@VEt>I z&Y|xW1eGzc@*9)~={*hss;fY51C0a1+)Hg5)b0nx5eS3kc0uI~a(zK<`(Wk~Dto|V z1;UW>9;OFIgT{|Q=?B!#hKZ3wgVsrb!k65%1G5vfmk?x*Y=`YWm>6iT9MrD@g)3;x z9=!~OsiQRw@*Bu)pfxe%=7H9Of$A+>We+I4L17Q7!;sAY zwWC34M{L5fQ=oArWI0mUpfz-$a0OvdTbLBR=xRZA1E`JwVGtXVCn4j=pmkK}^*4=7woS+9W0Txy7e_QZnLTY%gL z!k{(spmoonv`()1uzDG^&W~I($khj0%LAKZ2hEvcGY8by1*LCL+Y=@aD|oH+r30lvBO$|9>ptu0-m*JUI`Akw_)g@3FM2&u#-$(B zmIm#sfYoQ9bOgenv;=Acg4WU#+dl)f>p*b>svGFl7eMwGX#FXu>;dhkLl#G7gT_Wc zZUnW*K;odf3N!`{s=E}kDz<~fNX4M`r06QgGXncuu7mbIf%;FRnt@9_sLd!)*m@7N zZ%-mC`wT91l!$}oltEz!TAKhGYlY=OP*}n+NE}qQgZ7(%_8fusm>}B;8vg{5FZ-`?Oz7D zQA0Ir6KD=!c>4PDp!A~=98K~XXizx=3L{Yd65Qc@S#p;BQP6x4HhW1G1FZ`Or5%u4 zL2YI6xsJy{^#Zl}JD@Ovjn9L^47A@3w6_(MrqJCAN*kc{cc5{6(0*Icx?g0yApM}U z21<**_Bt!j^@Gv|I6YNZZwKwC2jv5hUr_LRr}IK>IoE~a`|m1-hwcQ`i|FRinh)CJ z3-SvnY;f5HTl=GF7{4C0jvAD9h+T(P8*6Cx8qXN94WU4yimMWWe1eA6p zI_Dk-wSjP{$0iOs;{;Uxg8U4gHw-f;>f9r2W?>TpjR%6<2pZ=EjW>YQfbuZRPIBid zL1hSRuN$bm0IjP7nN1B0G8;5*i(D>&>;mo6#+Qy@c7xj3k`A3`ghMCa7Viu>joyX= z)q$YBVxW0tPBWv7Z0Mx-$C^i zXx$|!-GDHtET&d{1#>URjkx--uz4|1pB|>3SQ<2s1By>j_<+g>T>VpG&BLV*lvY6N z^gw+WkX;}QvKLe*fXXaf`at5a_7A8X4dR2!D9|_vDBXbC2%s>$3pP+CAbrr`4JZs?=?F9ig|3zoJ}lj18$VPt%iqFVapM_p+r@{Z^wmLa zKhT;Tkefj?sJ}po8*%9c^|wHE7HEtRG|q`l9cX+2NsVA-nr615fcXT;qKB(;p8gl}L11K+n@)fc?C2UYR2}6lgpS6mFn(TZGI9 z^`$^{2Piy1TTuG~<{prnK>Z!$wmWDY2FU-Q@k2sx0hLFf`CCv}klP-_ zWe2Pb0=4HrYC!E{$<`T1`8rSERVt_N3Iy?943&vKxrJ5=0R)bL47TOij#LG z<+fY`m6Q0)2F*o)+y#mw&{zd1&*M{14LQ&l4=DX$tNYQ-2ah>zaJm3GKL=e7#0RZG zSeGku{zfC`vt^lLr|H$+0ohF~2DuT`&w-6ig3dhwwT)nHXi!@S*(^}L0nJB)`YfP$ z0bx)X42mmcb<|>m#vefGr*uO6UG;g(uY=Y%OKGeDpF0d{N8_>w)HjFq9YNy{)T~=^ z*$FAP*PjQqeTdnU1*-qSb?SPj^XT;{C|*G0!XUqc>Tu-x0hj#)DGq7}A)kYi=k9k( zUUcmx*!(1Dt^<@`U}Y{S-GK5mXr7kZK=U@J4g-nP8pFm4@wI!<-2iJxgTfIe2P$V^bvLNr1LDKv1~Uz+TS4^% zsGYwuQ|6+#L;rK7*scfSbDY3q?w~XS!k{_MW~;bMN&wIoz0& zsC7|GdgV1x{fcfTsB8qynS;Ab*0!T|s-AK=bvmadnViKv;Qk*aOqD z+IMlO$xoK%i=6|FM}qXw1B2#7LF3Axv;!OC0p-nP$C%Tgb6d(ilJ9}$2|;cFwH;t_ z0LsUpJOC1>7Y3~#1ogK-=?B#RBGq1SeY?Twg7#di)1Y=J2!qZQCe<8zs0XzLKxHOq z3?0<21f>&X462(zV>+O75_OGTmMMy_z2vSv{r;M4@r$5*0Xn}R-(PtPEz4CMH}B=5 zlUAMqr5*G+QBd2$LTSRq$R5y~8mKS1qe$>FDDFV@Gia_K)JLLLdIGH*1nu1dr5(_IKy>$j+Vh~e z3bP5?H?Laaa@cnLr;fYK9x2SWJr2qn=;~?B2hB}@#>qf#0z&Sn(h*1=l&(PO3!j~^yahVr3RE9YjF&zLYTtp{ z*q}6oj6r<`&>700`73;8Vv_@%+oI(cx*E2I5VVI2G+v0V7B(lT?(DZgeYMRc&%MS^ zlh&>AUF+5x$XT)RNy>kIP5&xY(Yc&5C?_8L9wUr@UdwB8U@ zAA!;Zdieq>XF%mTXw4z0?F&j@r1%qbJ{>50KzmI<^*l^3D35^ZXwW(&PALZKmF1f-xvHU5>&!BVEV0~UtJqof9gh6fq`9{KmXT<5&y{#sr=or@K0gd;d%fa}duoRgRaS^nCL8z|i1}=Yt z+CZT3eo&l%%0AFptuQmFNyG9K=*&KH%N~$>K=}&fR#4gp@j?EA)hnQWFi4DC463VO zNL<=BxJpyaUM|F1L|{OYcGS; zg2EJ(XZRP#-sWp)e4?mUzZWEq3xoQCQxl~QgW?4=mI7*{;ZjSEI4Hfq`dq}G^@z+@fAERH*79PTH^8S4CI^Z`P*{P+ zS7Blx8g`DaTyZnby+W`#34v)Tx4?Vvl*$O++XSjZLE|VOJ3$ztssO~^E10Nf-wj7kdL3UH4O$6$X z!p53Gc?zH1_~bzCW>}dF>c?VJ3+gk&{0^EU0F}eo)Pcl6YluPXsbS^TU?_7y?n1_( z{o0^>2O0}P7AJ-c%44871oc&6>Op5fgX%8OJO#Erf1tS)P&k0X5H$7x>j#n=Z=mrg z(0BsKeo&l&_GrS~Kne{SD+Ki+Kw$t{V@Qf#bhWVj1WMEBa>#sG8Ulp@Xl?*BZw=~K zgVxJ{!UVJ~9XxIWT^|QZKcIFKDD05U84PSt-UEdlXbc9_rY6=+pfMm&TNV_5ptywT z1D(waN5>=biJUq3n=Y?!U7itwRJ%022_8c>mN*f&>Rma4dELL zLU%8y%>xQ6P+tRCPOh|d4=DX8rxk5Q76-9Gc?=X*pz;S+e}mF3$ZmY=LP2_QVNjb0 z<`$59NNE#+>TJ;33=jtGWyfU)G2)=|4`e^6Uka+nh|vS8yFhgqs4WOuX8@Wn2DNQK z^9Z1F2qaI8IRmW*)ZYNbA*dWD#x0wAFeby&WF)t#Vtq(-=c()Frr(K9z1 zIiKz<7Q6zw0~IvChi*2go(AO=P}%{Ny`VM_x*AgWAbUaK0UGB7tx+XK?{HBITN{mU zEC^Irz{X%eZAA3GGwiG$iH=l>$Vf`&o9S_>S zgsu*>CJa>mfaXn+-)eKM{1QfQ|+LE9?Iw+h#Z46vv8=yWHDE)xe zHzS7usILV|KcF>v$l{7%(jroA=1GPh7V$`NVc7W<+m_5!G)~iA76i|Hw zN<$zF8ixdxAK2<-Z0-T6gQXo@bvZVD)Di>LyRf-SkiYO@P}&0JQ)<~w$Q)Q$gW5x& z^I<^YOQ_s|nF&fi;$8EP!}uT?G`;~!KcIacAU-mtMf(}qe2^bt^AezYl0e}OlLz%9 zL3sm|c0hiFVbGcjkiS9g8c;fcsR#8PKROZ6Os6~U;4uZ;aP}qU?AA|Z`*vtl{3s4;fI+GgIwgaUL zVlYTOt=A%Db9D~ zmIVL)>!34AX_baRKcM+j5F3U;^Ms)C2Q+UA6GNs!a|a+lf$DK&aZ=fk zw$Ad?AhSSWP0KqWNp%~g>S6m}LF@WK||J0Zpmpt=UsegVZdDdB_9Y*5()DuX~~ zg2EnDHjxu%pz%^rI}ns^Ko~TS4Qfw9=EK&4&r<=pAD>&0kXF$RzxG2T;mLHCt`*r4(fl&)ar(t-x< zqXmtRg2vcE_i}*di(&g}LH@^uL2G%Xnx-BGt;qz{@u0aL(0m~wH^R>32jvSwYDtj? z#Va`6MIiAEN~0jVhz&nbeFZuf2$XI>?L<(!5!rrFT!P{R+gLQRI%GB|>_GVsSq#Jm ztxW)>A5dQD;h%okq=r+0&2g2FsS?nt?h!fwL$IzVUQk0=?MoxdO+<< zbaSb}*Y@&XsgPT}6BPHLbBaKI#EPe{KMy*y8XE?UiDA_Fz5~`&=>`{3<-{2 z3mR+3wYLq~d{Djur4f)mP#FYDkDxW0FmpjPD4j_)Pdfrq1KPU_s#8Gq3uuiLD14C3 zqc$6KcNr)@!onU@-XVuE$i5gijkO!0=k9^Z9nd%|C_RDX&@m{zgUlkOT?+~WbaOy_ z(Apo^90q6~0!R!O291M((k!Tsf?-g-1gf_{{VQDhC>00QCCVuUTR`KVAb-FxsBQw? zHxAwdN#^}OxZDQHYoPWb=$;Qyx`CZ*1Gl?k!vzu7Be!JLQP(rzGLKSmP(B2OJ*Yef z#XShC$#on^aktz78du#`D0B&QRsv}Jl+Ye3P#Ff&4;r^5)?FZf<5CN|=NNhY4=%Oz z6bH4}KMnH$nTq zL48~Fbyc)*8)&@1MyKeYlg8|OLHg64g&9qLUh0;9e`bQ_`AG@VhXNh7R}!+9+UW;W z2Eo=JqR+zW!xh5EW_P+4M zjc_-D+F+pi4^(gB(o3#5s4fDn;{=TXg6b#GJad_Q>OBwbSq}~5mfesMS$7^(AEDQ4 z*zBWr`T^Mi+T#yuM}yWsgZ5E^#OR4Z=|U|gZ3C#y3Q8{^3@U3t?N?Cygr068WEZII zqbSpRKtXESVNkiGl9aa@v@Q?iW*C-in{^cAACSKYnTJmvw3Yxgh6CH<3rahnJ{4#j z9yGrPYGZ@j!_dAOs9plKk3fAKkUO!N4H{PgsRP}qgiRis7|35BzhaYviGk)(L1_rI z*BvHC4;s|xMs5#-(haDcN{{-09RGpapCW6|p@%=Hz6I@30M&1x@CIR!UqEa3h-t?o zy9JbPKz%7tS^=dQ5C)|oP+9`XA*%(kLF3qX+eDOAR?tp9j6X8O-Sh=4Vhp0+fHjeJ=ys z#V~Og4a%qB_I}U86QFPhVNe?m)NUiDZUfa*u<;vEx&hT$ATdxK2D1Yf4H}07%{PI@ zPC)JhyAyhEHZq&kxfYNgKy5r+_QJ$L?H^G30qu(;cP}{1EL=2b{|9pW29!rZ>4ws> z9+!Qz5C`>LK=A<@*TZK%D35@~GC|=F!yrDWZ-=fAv>p>Q#{fz*APgG60gVNst0R^V zs=q*K2~;kD);WO7B8BRmptWSgngLS>ayuyf!}$1U&>kmH8Upp}LHjQ8sU<}YG(HSE zGX~@q5C*k3LH%P;ekH}6!JrltFQD`T8uKE?J)rxpK;z=bbsMPc1+|e(%uE)6>MBs* z8uy2!sS{J)5Mwtr)PUn_(#kW$=5bIP2$Y6E=a*C3o(7G#$yRjigN>np%5qRx zg2r{I;kSV>15{7I=4nWo=LGp#DYI-FsLTb&$;|Z^T<7%PY3lU9wYot30%#uuX#Lzk z_z9n#u>Jw4JjJIDCI{-%!|v?@-2seiehj7_7Y(YbKhu*m6*Q8e|Gy_&{_YU%cnn8p1$M~Xg+2zh7YJc2Z}?`7%s@|#P}C< zCIDz{G^iW`r6E$wAy8fejoAp!-+2X8Z-L5iV(cYH4alva{cE88NT5BmpgobGJ^*N6 zwsw%&Dv&%m=Hk->nzsP0M**cFP@fE+T68(k{t(cbH&A`mRO@_Ey?r7^TN^Z2gRXC& z_@H_VRt6E<4g$FqlxARQ2t?yMF90-718Sp#`mnHbVnAsYglW~rk)CLGkmsD+1HPlK zw?OF}9|nz)g6d_^x;fC=6;K%q@*^b})(!%#xd4p~6Jr*reuC{22GO9j1kwlUAA|B4 zsGJ6c83=>s;y~l6p!fr+8AuFjzryMgQ2z%M-^92Fl!jn+8ECI6=DJ)kx+NIfxj5vvAtPAO>b8t-w}I~=#&U+^7sy$y0B=u95a87!bR zxga)ZObawO3_33ZJ#B%;DnRm}wFl(Z&mi~0`eLAQJP@A{2JI~el{=t$D{Nyqpm>ol ztltIdU%(7JI zw@PC6W>DRNEsjCu1}I-Br)F;vT-|Vm`#}Fg&O?iz2rOBD6>J9590RCt0h(t3wGTn< z9$MuY&^Q&ST@OkRpz(50c|uJ61nNtI>NN1(*9#p^fb_#KC20w?b__ID2dc+FbDZd5 zj?4$`?*yeCP+to)evB-K&IYwJu#Ihl@+znu2}%#3z6rWM5FgZr0<~GVwqAbBx%0*| z;aQvD^DLlvg3Z~H6GxzS3CMorauPi)VCB!;aDivXng<-mR=wanyzHsaqS==~We=zf zqE}rFDknhoC@9`RZ8A_=0)-bj7<5hzDD8pDEs%LI3`$F&dJfc{BGz2c_%0~@fH1B! z1nQ52>MKzC0rl63H5;T3RBnLEBzWFO%D14h2~^I5FlgQcx!mGief$pJ3eX(y%%h-j zCy-j?xCM>-gUTjy(-COy0JLTX)UF1lYh<%Q=@hi)3gjkG*$GM~pfg)#+I;rG!Wy)8 z8Wg7>Khgt(>Lifepfm)k!>DB^C@q1`?gOu5XD5Tw5~yuKEwizi18VDn;u2H_ zVUwqY7%07f(haCi1KAD2AU}iJ+@SU2*z5$&kHOX$gUTY%+QCrE#G@-R%+lkCx~pg;IBL5DQK;iwMOSz(3}})%o;R?jZNP`h=KY@ zp!N!=90JX$(Ay25{0Qm`fx;EkR|cJ@O-(m|;z@Xe(|J%@rKY*0nguFzK;hh@ zE%l)O8YumM+Ss5z7PZVJ)*Mi~0d&SBC`>^ZRNsKah}Dlz9VqXE?vjviu-S!A9X;hh zbsK1G3FKE02IU`Y=?FB=18O(t_~`8bwTD1!$3c53z-b7}+6s_3sN4eWCj`x#gZ6xb z)?0weGtk;WP#QyTn^Vi*pgtTZ4T0t-sbwaq=77thm5yg%VG8QkfbuP=<{+yF<#~{K z*v@)DRyPoAP~8g3DNU{%3sBktwVlz&3PI!5puH8K`V2Jo2-*h)PFF~4 z2|;NI7Y3~Z1I_<~^nubCtPBIKB_lT+Kz%z<`T>pog2s@@HHT7tp!Nc&-3ba$5C-+p zK>3wib7AR+o^5AR-3m@e6YUOx{0qXMvHIK|^^0o?L@t8*?xdIpTDt=pmjtCRP`LwI zLkQYK1xhQRGz03};YwE^aZuU<&Fg~ZctQIFL481C?FY}H%-C=NG?zr6*$7*MsIr zKxGhU><`_IpuP*JegUNqQ2zlGC&XY-UIL{jP@f&$OhSB6+5^prgUWTVUF)6BgX{;T zTS9ta^04#+N`El1!A@5+D4&hrp!?WpyVV_#zd_^Nr1+T<|AFd8(0XUkK4{Pxa-cE^ zhC%HwLTjI4V~e1)1ab!`?vcv@kbhtpG)@M(uL-0E6wjc#0)#>35h$LK(;2A$4B9US zDz`x9l7d0~6HxmFH2w#(8`N$9m2ZU10kxq(dO&kmgwzfOdC>YnP`d}TmQXj#d<&?L z268{h@1XUa^c=GXg$L;D0#F+Zz9w2^%~?>r29m>uL2Ld&We{ww255W_)nh)oBeItMxKL1~fPxhGH=2C64PZUMOogh6=|ltw`9CT#uzrEicrP<@I`elUrF z&aeWV?*&Rfurp0Tc?XpCL4E*XP@V;~8R+e9P}>^3wqAI{1yDT)s{gRL0kj4Hlzu>C z)1WbFP@M&;mx)a;*vtfpfy!Udyd@|MKy?~uP8%dn4hGewpnHhn^&03-oAplTL3I=; zkAl(@D7}EnD6oC29M94_-;(MtPM#2rCaLO+z62L3tCDRzT?nRKA146&-`he^46;q<%1B z(A+xcoLJDh3{bh#7bkN7B!N+Y22 zbwGQxKLdGZ#Q<2-JQ7r723y918kDX#+Ga2s$qqv?mmlW z2BjZRID+ydC|p5uw8o%uB~bbS^~bQ;1)9SEr616|7&duoh=Ix-P+Jh>cThM`!_2{G z257Ay=ng|ry##9;gVGELgVGRaUojv3xO33OH$XniT@Zf{W80y;ksv_}GVo);*sz%WQ0qy}_19jH8_hC4v@ z8K_?i%8MYH)@g{C^aEO32r7f9;Xiz4fYw`q{0(Xgg4)CQ)DDCksB8e0A)xr4og#4z z)XoL%6$Z6iU}xLH(grpfwC)si*9$0ZfzDEv1oAhi4yTs6q?iLba}?Bn294c;&Tj+NHK6rc=;`BjGspAW%^WX4 zcOk;k3aCy3tt$oHw*uM&3hGON+QpzWN3HUOSic~rAyA(b6t=|bB}E-<-w0^UBrbiR z^@;Q-e?azw`be-k9Moq5iO~Xs$`Vlf6%@yyxCX6{0FCp)(gHRbH2)1MYe03(jVa<%PtWuNasz1H z7|73{u?wHo`IsZ!pv8L!V;7xLFtK_?ttxk1g&4g zXD(>JJE`SQL`1}z+`Qb)6_u4cKsYlibCa8!+cJFiAj^TqZ9x78^&`j~?*^4Ip#Bc1 zjRLA$(9;X3JpvkM0i_erdUV(xPGon$(h7K9T59TH&>S?Vz6W6;j@vJ!gg0FPv2kM1 z`atpd$3bJYpf&<1%t7laknNx~8#eY18i%K*U7&R!Ab*4QjNmgDv?ia}^pl#Fx?#@T zxyRRSSbqT>uU)_He0xX7K1WB#CHU+E$${E@pfN>I8Ul^~fy9W#p!x)KHXEqEK~6KE z^a3hZK<6l7ODmu;c~IE_I`1BQW-{y^CA9T@pgAAV{12%70i_Ah*coX21B5~6o0QEf#$J6;Rs4cb3f%qKSe zlvk8*hozY%%a)#MX>HvDDtkbmYd+au)+AO@Z2< zv@VZ8VFyY-pnVD0!kHW~PlAkl#UbSD>~vHZ`F8wL$G#Sh@kVZ9wG=sNI6D9+clfYg$0# z?y$KVP&mLas7wKsBcL`7u6bp2GfCxx(hF$4DeT-0ke#6R5~=18s~(hYKw$_PA0bu` zK6RjW9W3ndt>1u|0opT)ul@=Q3R(q9H=uNpkeIk0rVktK;o-3yq!xrhY;0Kd>~W~gUq5P2H6K%9|}sl zpmKU) z${vvVy83#e_CbKk9gzP)X$Z9D2V@Rt{0-EP0;L-e29-DHeQ417IZ&Me%PSy1gD@z+ zgTz5?V~|+`i9v3JxepXC)N%_b%wXjXs0{$>(^A81P}u_tKTtS=>T_bu1kIh|N+6NEv22G!Z1wg|G?!N3N!qd{&2waYr7eZ;0Ykh!3K zg#LExTLF8Gp7(ai-2jc_fyRqLaxe_q+l0JU7gR5R_SC`T2P+MV4^X^-`hKLk5j4I5 zTCW1?U%~PiD4&7$34qEWQq9Aq9#U39?(hSJGcI+=;;_CK=*$mfaS$8Se%ZWr%cYH* zH(w;CPY%)p!?WkkIktW0&MSo4OEC2y8r0TloT79av>(A>tJN(~-3}6mVNjg}iUSY^ zwQGp=BTWB5(x7oikh?)^i-~EsgUT7ucn&CyfW}!t=>?3TXEcE3qp9s5SonhSB(Y|L z&-elD6C{Q~G zG>`3BZvfvP1zNjmU~aky)Mo;Rzwr9=pt=jxW=7UO6xg6W$RIy}>RRM_7Bm(H8b=1@ z7tp*QY)%54UO?-0H#l7YjkALGDp0e$K@KxeTOZaQAU0ou&T;{*`vKKmpz{@w%>%JP zb+Q_qVna6OHjPGD?My9QKWfyyAznhjd} z3DidgrEOxydth#X&3l2)bb*PX(=oBJYeDT8P=DOt-+v{#JUTxqIe7z2eN=SRT6B4I zJ}m8k<{vySqj|p^s z7HAD7C`|}&aK6C5FyRi^PauCyFMYbuHq0JX1 z1$SOSE*J5c2RauBbe;auKg->l$bJHGBnpnDg`Dt%&@8!7E<`?8|0i`pLTF_WE zE_E<*<)tVOrW$?pmh(}-B32DZUVJwWXgRHO8PaN5!YREMM`|#MNr-c)oY+O z2(fWUta}Nm1C{BbJm>C<@|?L(Y@Z!g_JH=Eg7)sS{5qo8yFs*}<)(l>(o zSRg&1whpMA>Fny-50VGr(z4QRAUS-PP}%|M1)VJc3Rh6O0hFha`>n7z0j-M#jTwRV zK!eIFkUEfFaxtiV0cx9q##2D;5m5UJR4#zlErH@5W-mS(G?or3Z$RY^XiONC&OmO% zHogZ+E7-=$L1Wi);tNh_Ys3&N!^!n4Odn|O7*zg9i>y8iY6ro@anYdk0y|3zboL!C zb;#nNG}PVG15YEX)~r6eXvyMJAR3frKp4aZ)nCZ^u(4rj2h>&ut;FngOL9T=58!1I0P0Jqp?%3&Wr^2-?E{!|=4R#NiY!v(Uvs`;kC< zB0yyh$UIP4h%b%6+>Vb1^#?$CAJk6(jWpIkZ6cmgP# zL35>`xn*L^f$cj5ofS@udSo@AbsDg;2$W_(^%aN@s;}_XZ7?&C(+;ey43mS=pgaKU z3xMVwVB*+lP#p$}KTtXW<#}Su2vC}U&HsbgFbqx`a~+O@>KITO1(hG5aVgOFCaCR& zKEHy^P1F$6(ui9J9_KNyCXX%#nm-54LBRUxxY7N8omnLiJA2m=dT@M~r!(@(onJfX0_W;R*_0Fcw*J zmQWr=HXn3$H>k}_&odA~X$Q2<7?gHE{VeqP2xRwx&in)WOL+ZxP`?0KZX~l|=?GM} zLBeMPd|ex8Y#TJ@2g-NI7<4``X#N(|mO|G98VdoXOOUz5hAXVi44SVX_WV|2&Bmn; zH0KFgqXSAipgApU{VZg?Ape5=3R<^?EI!IMUH}RMP&ooxp8|?|a9AOgOQ3MWhe2!J zKy7tUd4t@h1euEugU%QO-LC;UCjnIU(Ch3>(7puF-Ud+G0nLMg`1st3JkLmq|L~bP z+~q*&2~qs7wXrA5b`8Q!^?CDTC-04zRQXDsw<-NZUJbB}j~x z_Jhh8(EbKcnFGQd(X#u{*L$Jc4_dDWDqBHm2UNbI%a8Kun{Ghi4N51VGy_XJAU;T( zRv5j^A@-aA(0Ulq`eRVq0oCW^mb29G7wG;G(0w(a`;0*I%E%aWP9$iZCa4Vw>bHXK z0|1TTQ^VfDW(Mef1JJlKJ<<*+-GKJ%g7#&C${P?Srpy6_8K^%A9^(L=b0xOy6e)cH zkbY`lP#OT;e*~M$0fiYU7<6_3=nfLl8ctC90-d7-N?X)$8@-fYKZ2{9}+lY#1a5QUfv{RGxv#HEe4f=;c>J_JHo&0p$aj zUqI(Ufx?H7K2qgzr5jKg1d_vN9w^;_`UbEv2b6w5^MwOdXMyS@P8qjzj=u8sOTqh{afG}uGhful!jX8k!K!L_ZLFomCL38S$@vDI< zb71}kg&!!)@U4AzdzgHRLCUgvnV^CcPN?V{f2bEi( zdKFYofXY5l8UtbEG7OYPKw=;a${(P4a!~yXN+YoOE0{eXF;Jg?TK)u;HK0A?p!1JF z=>>#AV>zI9G3bl}Z1#ZC18A%YG;WNjpP*yxB5Tiq)?f@|d4uj|P?-%1KTti7E;kT- zP~8b?AA!bsLFeg!(g`SS!|D}~Td9db_eFx*bfEM_E&qb{0fY7jBbPOxJ$s?}DI)@9{ z9?)DbsNV#tgFtOxSe*o;LFEoe9yC7#N?*u&2OArdZiY(w8Ej#P%RQhy<^=)DTX2mR mgX$>I{u4-gS$`hX?*_GtLF*p}V(bQ&KZp?@O+VBh(*^*kUNgr4 literal 0 HcmV?d00001 diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/Cylon.pdn b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/Cylon.pdn new file mode 100644 index 0000000000000000000000000000000000000000..e90048c37b2d3f9800d7e0e118387df1370d8838 GIT binary patch literal 3953 zcmWG=@iRWe$Y4{DlINM5n4YRoo|#gTVXI_lrlgROnwg$aVyk3iprnwKSeaT>Y^!9f zq)?n#mYNctS&|W!T2!2wpJ%INqGzdRYGGobXKY|%Y^G#qlU!O{lAmj5Q<71do1{>X zmu{<+8Rq04RBqtyo}TaM=;-Jd9Gc-8n(pZ6=mg?AhB`Y&IyyQ!yG9ln8iPe5{DOlF zJROUQO_I$5K_WqUfuV*@j*iY1S>F9XLE(_9Z@ zkipJA!J$D;VJ;Z~1ySBkF8Pk>&aNRP71~7sW+edyp}yHc{@(6J9>v}%#(s%~q5d9u zPUXqo{<&qY-pPR(ZWb;EewqHhCgJ{pZc%c+6BouZu#Lx#x9m#9yx(U8JRf-jwa!y5ncr*g$7|BAWu5F=D3Aq z2bTusIy);V=-b)o!va_z7H@V8j0}tn3=IGO|Njr785kHCc$gR%82kbfGxJJZ@=N?u zOY~e4OA>VyVDW|;Z8{3hr8y;~MX9!Vsih@Fi8(q70i{VfnaSR%l_B}rsd=_}r8zmQ zAj3sa%*;0hl$osOomv@|m{Xb>keFGNU=$qVqmWyioL`ianS>k|26_g1 z282T)$=p2AG}+Y5(9+l>)xg3sR)>~`$41A3JkP+%%)r1Pz`(#D!oa}5$pRu+85kH? zLBiO>45WswAh9SluSCGHs3@@#6p-5SnV`VtM-nK`tV$I^;+H{kH47^P1J-y3IfsJ- zL~?@C0w+5I0|UqvAm^}vEJceZkPutEZ(>${5nH@(W*&%gD$UGE;fxPTEz1OzAfUv` z!NI`+Qp(A|z;Hv1f#FFR0|PtAOmMOW$uK_|5fRSvJQc11IOw%h#OgCraVqjo!u>tWFGE;1oj15ec6mnAYAk|YzWkIT~l97@EShuZG zvb$SBQeI$bQf`=mr%Pa|ud|7%qob`YsKjLa`v3obP{6YBFfcH1!>uqhWaDLEV9-a{ zVPt6rwZlkB0b+$Q+zQ8ZM~HEsV8-#I8fU}?%A@8;##tCajkACmX9_nCRO2~1x`K*$ zh_UZs#tNYtYs@Chz`%fFtTFM%3V^aGCh0k|v&*AWFp`30#(C7G$k?8T);X^F|H0$@pPMCHK^5dha6 zJPbFK*0}IAO}()kD@>URIYfExjzq{|3O=AabI zsRl9zl-@yU2_?$aK{BAU4~lj+4Nxiw@DEc6DN4-Df!NOQpYhuN|NlWQ02>O*8el`U zL0Un11jA4rkPIllfDP3JwM1O*^@8$qASs0QN5`LUF=yLz4A$L?+fSF-? zc_kmjL{LEmR>TmFto#Q*6VH<*wzgkY9Lre#Y-fK|#bKLu?twYG;PWX)ZvS ebb}d=opnWD`(8=g6gsYq6JTSgoBY8JWC8%Od~n79 literal 0 HcmV?d00001 diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h new file mode 100644 index 0000000..852c209 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrb.h @@ -0,0 +1,30 @@ +// To recreate the data below, use the Paint.Net plugin "Arduino Progmem NeoPixel FileType" +// to save as/export the included Cylon.pdn +// Paint.Net - http://www.getpaint.net/download.html#download +// Plugin - http://forums.getpaint.net/index.php?/topic/107921-arduino-neopixel-sketch-exporter/ +// This uses Flatten, GRB, Hexadecimal +// + +const uint16_t myImageWidth = 16; +const uint16_t myImageHeight = 20; +const uint8_t PROGMEM myImage[] = { // (16 x 20) GRB in Hexadecimal + 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x3f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x3f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h new file mode 100644 index 0000000..9314f2f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/CylonGrbw.h @@ -0,0 +1,30 @@ +// To recreate the data below, use the Paint.Net plugin "Arduino Progmem NeoPixel FileType" +// to save as/export the included Cylon.pdn +// Paint.Net - http://www.getpaint.net/download.html#download +// Plugin - http://forums.getpaint.net/index.php?/topic/107921-arduino-neopixel-sketch-exporter/ +// This uses Flatten, GRBW, Hexadecimal +// + +const uint16_t myImageWidth = 16; +const uint16_t myImageHeight = 20; +const uint8_t PROGMEM myImage[] = { // (16 x 20) GRBW in Hexadecimal + 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x3f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino new file mode 100644 index 0000000..34b97f2 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferCylon/NeoPixelBufferCylon.ino @@ -0,0 +1,74 @@ +// NeoPixelBufferCylon +// This example will move a Cylon Red Eye back and forth across the +// the full collection of pixels on the strip. +// +// This will demonstrate the use of the NeoVerticalSpriteSheet +// + +#include +#include + +// The actual image is contained in the data structure in one of the Cylon*.h files +// You will need to use the one that has the same color feature as your NeoPixelBus +// There are two provided, but you can create your own easily enough using +// free versions of Paint.Net and the plugin +#include "CylonGrb.h" +typedef NeoGrbFeature MyPixelColorFeature; + +// #include "CylonGrbw.h" +// typedef NeoGrbwFeature MyPixelColorFeature; + +const uint16_t PixelCount = 16; // the sample images are meant for 16 pixels +const uint16_t PixelPin = 2; +const uint16_t AnimCount = 1; // we only need one + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); +NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object + +// sprite sheet stored in progmem using the same pixel feature as the NeoPixelBus +NeoVerticalSpriteSheet> spriteSheet( + myImageWidth, // image width and sprite width since its vertical sprite sheet + myImageHeight, // image height + 1, // sprite is only one pixel high + myImage); + +uint16_t indexSprite; + +void LoopAnimUpdate(const AnimationParam& param) +{ + // wait for this animation to complete, + // we are using it as a timer of sorts + if (param.state == AnimationState_Completed) + { + // done, time to restart this position tracking animation/timer + animations.RestartAnimation(param.index); + + // draw the next frame in the sprite + spriteSheet.Blt(strip, 0, indexSprite); + indexSprite = (indexSprite + 1) % myImageHeight; // increment and wrap + } +} + +void setup() +{ + strip.Begin(); + strip.Show(); + + indexSprite = 0; + + // we use the index 0 animation to time how often we rotate all the pixels + animations.StartAnimation(0, 60, LoopAnimUpdate); +} + + +void loop() +{ + // this is all that is needed to keep it running + // and avoiding using delay() is always a good thing for + // any timing related routines + animations.UpdateAnimations(); + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino new file mode 100644 index 0000000..37a109f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelBufferShader/NeoPixelBufferShader.ino @@ -0,0 +1,167 @@ +// NeoPixelBufferShader +// This example will provide a shader class to the NeoPixelBuffer that will dim and brighten +// the pixels that are in the buffer (a device dependent bitmap) +// + +#include + +const uint16_t PixelCount = 64; // set this to the size of your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +// three element GRB pixels, change to your needs +NeoPixelBus strip(PixelCount, PixelPin); + +// the buffer object, +// defined to use memory with the same feature as the strip +// initialized with the same number of pixels as our strip +NeoBuffer> image(8,8,NULL); + +const RgbColor BrightRed(255, 0, 0); +const RgbColor BrightGreen(0, 255, 0); +const RgbColor BrightBlue(0, 0, 255); + +const RgbColor BrightYellow(255, 255, 0); +const RgbColor BrightCyan(0, 255, 255); +const RgbColor BrightPurple(255, 0, 255); + +const RgbColor DarkRed(32, 0, 0); +const RgbColor DarkGreen(0, 32, 0); +const RgbColor DarkBlue(0, 0, 32); + +const RgbColor DarkYellow(32, 32, 0); +const RgbColor DarkCyan(0, 32, 32); +const RgbColor DarkPurple(32, 0, 32); + +const RgbColor White(255); +const RgbColor Black(0); + +// define a custom shader object that provides brightness support +// based upon the NeoBitsBase +template class BrightnessShader : public NeoBitsBase +{ +public: + BrightnessShader(): + NeoBitsBase(), + _brightness(255) // default to full bright + {} + + // required for a shader object, it will be called for + // every pixel + void Apply(uint16_t index, uint8_t* pDest, uint8_t* pSrc) + { + // we don't care what the index is so we ignore it + // + // to apply our brightness shader, + // use the source color, modify, and apply to the destination + + // for every byte in the pixel, + // scale the source value by the brightness and + // store it in the destination byte + const uint8_t* pSrcEnd = pSrc + T_COLOR_FEATURE::PixelSize; + while (pSrc != pSrcEnd) + { + *pDest++ = (*pSrc++ * (uint16_t(_brightness) + 1)) >> 8; + } + } + + // provide an accessor to set brightness + void setBrightness(uint8_t brightness) + { + _brightness = brightness; + Dirty(); // must call dirty when a property changes + } + + // provide an accessor to get brightness + uint8_t getBrightness() + { + return _brightness; + } + +private: + uint8_t _brightness; +}; + +// create an instance of our shader object with the same feature as our buffer +BrightnessShader shader; + +// some dimming tracking variables and settings +int8_t delta; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.Show(); + + // dibs do not default to any color, + // so clear it to black if you aren't going to draw + // into every pixel + image.ClearTo(Black); + + // draw a pattern into the image + uint8_t x = 0; + uint8_t y = 0; + image.SetPixelColor(x++, y, DarkRed); + image.SetPixelColor(x++, y, DarkYellow); + image.SetPixelColor(x++, y, DarkGreen); + image.SetPixelColor(x++, y, DarkCyan); + image.SetPixelColor(x++, y, DarkBlue); + image.SetPixelColor(x++, y, DarkPurple); + + x = 0; + y = 1; + image.SetPixelColor(x++, y, BrightRed); + image.SetPixelColor(x++, y, BrightYellow); + image.SetPixelColor(x++, y, BrightGreen); + image.SetPixelColor(x++, y, BrightCyan); + image.SetPixelColor(x++, y, BrightBlue); + image.SetPixelColor(x++, y, BrightPurple); + + Serial.println(); + Serial.println("Running..."); + + delta = -1; // start by dimming downward +} + +void loop() +{ + // we increment by delta every 30ms + delay(30); + + // update the brightness in shader + // + uint8_t brightness = shader.getBrightness(); + // check if we flip directions + if (brightness == 0) + { + delta = 1; // increment + } + else if (brightness == 255) + { + delta = -1; // decrement + } + // modify and apply + brightness += delta; + shader.setBrightness(brightness); + + Serial.println(brightness); + + + // render the image using the shader and then call Show() + // these two should be called together in order + // + + // need to provide the type of color feature for the strip and + // the type of our custom shader + image.Render>(strip, shader); + strip.Show(); + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino new file mode 100644 index 0000000..c421b5b --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/bitmaps/NeoPixelDibTest/NeoPixelDibTest.ino @@ -0,0 +1,158 @@ +// NeoPixelDibTest +// This example will provide a shader class to the NeoPixelDib that will dim and brighten +// the pixels that are in the Dib (Device Independant Bitmap) +// + +#include + +const uint16_t PixelCount = 64; // set this to the size of your strip +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +// three element GRB pixels, change to your needs +NeoPixelBus strip(PixelCount, PixelPin); + +// the DIB object, using RgbColor and initialized with the same number of pixels as our strip +NeoDib image(PixelCount); + +const RgbColor BrightRed(255, 0, 0); +const RgbColor BrightGreen(0, 255, 0); +const RgbColor BrightBlue(0, 0, 255); + +const RgbColor BrightYellow(255, 255, 0); +const RgbColor BrightCyan(0, 255, 255); +const RgbColor BrightPurple(255, 0, 255); + +const RgbColor DarkRed(32, 0, 0); +const RgbColor DarkGreen(0, 32, 0); +const RgbColor DarkBlue(0, 0, 32); + +const RgbColor DarkYellow(32, 32, 0); +const RgbColor DarkCyan(0, 32, 32); +const RgbColor DarkPurple(32, 0, 32); + +const RgbColor White(255); +const RgbColor Black(0); + +// define a custom shader object that provides brightness support +// based upon the NeoBitsBase +class BrightnessShader : public NeoBitsBase +{ +public: + BrightnessShader(): + NeoBitsBase(), + _brightness(255) // default to full bright + {} + + // required for a shader object, it will be called for + // every pixel + RgbColor Apply(uint16_t index, RgbColor original) + { + // we don't care what the index is so we ignore it + // + // to apply our brightness shader, modify the original color and return the color we want + // blend from black (_brightness == 0.0) to the original color (_brightness == 1.0) + + return RgbColor::LinearBlend(Black, original, (float)_brightness / 255.0f); + } + + // provide an accessor to set brightness + void setBrightness(uint8_t brightness) + { + _brightness = brightness; + Dirty(); // must call dirty when a property changes + } + + // provide an accessor to get brightness + uint8_t getBrightness() + { + return _brightness; + } + +private: + uint8_t _brightness; +}; + +// create an instance of our shader object +BrightnessShader shader; + +// some dimming tracking variables and settings +int8_t delta; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + Serial.flush(); + + // this resets all the neopixels to an off state + strip.Begin(); + strip.Show(); + + // dibs do not default to any color, + // so clear it to black if you aren't going to draw + // into every pixel + image.ClearTo(Black); + + // draw a pattern into the image + uint8_t index = 0; + image.SetPixelColor(index++, DarkRed); + image.SetPixelColor(index++, DarkYellow); + image.SetPixelColor(index++, DarkGreen); + image.SetPixelColor(index++, DarkCyan); + image.SetPixelColor(index++, DarkBlue); + image.SetPixelColor(index++, DarkPurple); + + image.SetPixelColor(index++, Black); + image.SetPixelColor(index++, Black); + + image.SetPixelColor(index++, BrightRed); + image.SetPixelColor(index++, BrightYellow); + image.SetPixelColor(index++, BrightGreen); + image.SetPixelColor(index++, BrightCyan); + image.SetPixelColor(index++, BrightBlue); + image.SetPixelColor(index++, BrightPurple); + + Serial.println(); + Serial.println("Running..."); + + delta = -1; // start by dimming downward +} + +void loop() +{ + // we increment by delta every 30ms + delay(30); + + // update the brightness in shader + // + uint8_t brightness = shader.getBrightness(); + // check if we flip directions + if (brightness == 0) + { + delta = 1; // increment + } + else if (brightness == 255) + { + delta = -1; // decrement + } + // modify and apply + brightness += delta; + shader.setBrightness(brightness); + + Serial.println(brightness); + + + // render the image using the shader and then call Show() + // these two should be called together in order + // + + // need to provide the type of color feature for the strip and + // the type of our custom shader + image.Render(strip, shader); + strip.Show(); + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino new file mode 100644 index 0000000..6b70b6f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicDump/NeoPixelMosaicDump.ino @@ -0,0 +1,98 @@ +//---------------------------------------------------------------------- +// NeoPixelMosaicDump +// This will dump to the serial output a grid map of the defined mosaic +// The output is displayed as row column labeled grid with the NeoPixelBus +// index of the pixel at the intersection of the row and column. +// +// To help with physical layout, there maybe included a symbol following the index +// < means the index is the input index for the panel, the first on the panel +// > means the index is the output index for the panel, the last on the panel +// +// This is useful in visualising the mosaic layout of your panels to +// confirm you have them correctly wired together for this mosaic pattern +// +// It does not require that you have the actual panel connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts +// rotation is ignored for mosaic as it applies a rotation for you +// that is specific to the location of the panel within the mosaic +// to reduce connection lengths + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; + +// make sure to set these panel and tile layout to match your sizes +const uint8_t PanelWidth = 8; // a 8 pixel x 8 pixel matrix of leds on the panel +const uint8_t PanelHeight = 8; +const uint8_t TileWidth = 4; // laid out in 4 panels x 2 panels mosaic +const uint8_t TileHeight = 2; + +NeoMosaic mosaic( + PanelWidth, + PanelHeight, + TileWidth, + TileHeight); + +void DumpMosaic() +{ + Serial.println(); + + Serial.print("\t\t"); + for (int x = 0; x < mosaic.getWidth(); x++) + { + Serial.print(x); + Serial.print("\t"); + } + Serial.println(); + + Serial.print("\t---"); + for (int x = 0; x < mosaic.getWidth(); x++) + { + Serial.print("--------"); + } + Serial.println(); + + for (int y = 0; y < mosaic.getHeight(); y++) + { + Serial.print(" "); + Serial.print(y); + Serial.print("\t|\t"); + + for (int x = 0; x < mosaic.getWidth(); x++) + { + NeoTopologyHint hint = mosaic.TopologyHint(x, y); + + Serial.print(mosaic.Map(x, y)); + if (hint == NeoTopologyHint_FirstOnPanel) + { + Serial.print("<"); + } + else if (hint == NeoTopologyHint_LastOnPanel) + { + Serial.print(">"); + } + Serial.print("\t"); + } + Serial.println(); + } +} + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + DumpMosaic(); +} + +void loop() +{ + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino new file mode 100644 index 0000000..2f6500c --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelMosaicTest/NeoPixelMosaicTest.ino @@ -0,0 +1,97 @@ +//---------------------------------------------------------------------- +// NeoPixelTopologyTest +// This will display specific colors in specific locations on the led panels +// +// This is useful in confirming the layout of your panels +// +// It does require that you have the actual panels connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts +// rotation is ignored for mosaic as it applies a rotation for you +// that is specific to the location of the panel within the mosaic +// to reduce connection lengths + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; + +// make sure to set these panel values to the sizes of yours +const uint8_t PanelWidth = 8; // 8 pixel x 8 pixel matrix of leds +const uint8_t PanelHeight = 8; +const uint8_t TileWidth = 4; // laid out in 4 panels x 2 panels mosaic +const uint8_t TileHeight = 2; + +const uint16_t PixelCount = PanelWidth * PanelHeight * TileWidth * TileHeight; +const uint8_t PixelPin = 2; + +NeoMosaic mosaic( + PanelWidth, + PanelHeight, + TileWidth, + TileHeight); + +NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +RgbColor red(128, 0, 0); +RgbColor green(0, 128, 0); +RgbColor blue(0, 0, 128); +RgbColor white(128); +// if using NeoRgbwFeature above, use this white instead to use +// the correct white element of the LED +//RgbwColor white(128); +RgbColor black(0); + +const uint16_t left = 0; +const uint16_t right = PanelWidth - 1; +const uint16_t top = 0; +const uint16_t bottom = PanelHeight - 1; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + + strip.Begin(); + strip.Show(); + + Serial.println(); + Serial.println("Running..."); +} + +void loop() +{ + delay(2500); + + Serial.println(); + Serial.println("If your panel is correctly defined, you should see ..."); + Serial.println("Upper left is white."); + Serial.println("Upper right is Red."); + Serial.println("Lower right is Green"); + Serial.println("Lower Left is Blue"); + + // use the topo to map the 2d cordinate to the pixel + // and use that to SetPixelColor + strip.SetPixelColor(mosaic.Map(left, top), white); + strip.SetPixelColor(mosaic.Map(right, top), red); + strip.SetPixelColor(mosaic.Map(right, bottom), green); + strip.SetPixelColor(mosaic.Map(left, bottom), blue); + strip.Show(); + + delay(5000); + + Serial.println(); + Serial.println("Cleared to black ..."); + strip.ClearTo(black); + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino new file mode 100644 index 0000000..64a1bcd --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelRingTopologyTest/NeoPixelRingTopologyTest.ino @@ -0,0 +1,100 @@ +//---------------------------------------------------------------------- +// NeoPixelRingTopologyTest +// This will display specific colors in specific locations on the led rings +// +// This is useful in confirming the layout of your rings +// +// It does require that you have the actual series of rings connected +//---------------------------------------------------------------------- + +#include + +const uint8_t PixelCount = 119; +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +// define the layout of your series of rings +// +// This example is using all of Adafruits rings and a Jewel in the center. +// The center is the input and all the rings are connected in series going outward +// +// Rings: +// 0 - 1 (virtual ring, the center of the jewel) +// 1 - 6 (virtual ring, the outer ring of the jewel) +// 2 - 12 count ring +// 3 - 16 count ring +// 4 - 24 count ring +// 5 - 60 count ring comprised of four arc segments +// +// The values below in Rings[] are the index of the first pixel in each ring. +// An extra value is appended for a virtual ring start that also +// represents the total count of pixels in the complete series and this extra +// value is required. +// +class MyRingsLayout +{ +protected: + const uint16_t Rings[7] = {0, 1, 7, 19, 35, 59, PixelCount}; +}; + +// use the MyRingsLayout to declare the topo object +// +NeoRingTopology topo; + +// declare our strip +// +NeoPixelBus strip(PixelCount, PixelPin); + +// define some handy colors +// +RgbColor red(128, 0, 0); +RgbColor green(0, 128, 0); +RgbColor blue(0, 0, 128); +RgbColor black(0); + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + + strip.Begin(); + strip.Show(); + + Serial.println(); + Serial.println("Running..."); +} + +void loop() +{ + delay(2500); + + Serial.println(); + Serial.println("If your panel is correctly defined, you should see ..."); + Serial.println("First pixel in each ring is Red."); + Serial.println("Middle pixel in each ring is Green."); + Serial.println("Last Pixel in each ring is Blue."); + + + // use the topo to map the 2d polar cordinate to the pixel + // and use that to SetPixelColor + for (uint16_t ring = 0; ring < topo.getCountOfRings(); ring++) + { + // first pixel in each ring is red + strip.SetPixelColor(topo.Map(ring, 0), red); + // last pixel in each ring is blue + strip.SetPixelColor(topo.Map(ring, topo.getPixelCountAtRing(ring) - 1), blue); + // middle pixel in each ring is green + strip.SetPixelColor(topo.Map(ring, topo.getPixelCountAtRing(ring) / 2), green); + } + strip.Show(); + + delay(5000); + + Serial.println(); + Serial.println("Cleared to black ..."); + strip.ClearTo(black); + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino new file mode 100644 index 0000000..2849c56 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesDump/NeoPixelTilesDump.ino @@ -0,0 +1,102 @@ +//---------------------------------------------------------------------- +// NeoPixelTileDump +// This will dump to the serial output a grid map of the defined tiles +// The output is displayed as row column labeled grid with the NeoPixelBus +// index of the pixel at the intersection of the row and column +// +// To help with physical layout, there maybe included a symbol following the index +// < means the index is the input index for the panel, the first on the panel +// > means the index is the output index for the panel, the last on the panel +// +// This is useful in visualising the tile layout of your panels to +// confirm you have them correctly wired together for the defined pattern +// +// It does not require that you have the actual panel connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts and +// how you want them rotated. Not all the rotations are listed here +// but you can modifiy the name to include the rotation of 90,180, or 270. + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; +// typedef RowMajor90Layout MyPanelLayout; // note rotation 90 was used + +// change this to be one of the layouts which will define the layout +// of the panels themselves +typedef ColumnMajorLayout MyTilesLayout; + +// make sure to set these panel and tile layout to match your sizes +const uint8_t PanelWidth = 8; // 8 pixel x 8 pixel matrix of leds +const uint8_t PanelHeight = 8; +const uint8_t TileWidth = 4; // laid out in 4 panels x 2 panels mosaic +const uint8_t TileHeight = 2; + +NeoTiles tiles( + PanelWidth, + PanelHeight, + TileWidth, + TileHeight); + +void DumpTopo() +{ + Serial.println(); + + Serial.print("\t\t"); + for (int x = 0; x < tiles.getWidth(); x++) + { + Serial.print(x); + Serial.print("\t"); + } + Serial.println(); + + Serial.print("\t---"); + for (int x = 0; x < tiles.getWidth(); x++) + { + Serial.print("--------"); + } + Serial.println(); + + for (int y = 0; y < tiles.getHeight(); y++) + { + Serial.print(" "); + Serial.print(y); + Serial.print("\t|\t"); + + for (int x = 0; x < tiles.getWidth(); x++) + { + NeoTopologyHint hint = tiles.TopologyHint(x, y); + + Serial.print(tiles.Map(x, y)); + if (hint == NeoTopologyHint_FirstOnPanel) + { + Serial.print("<"); + } + else if (hint == NeoTopologyHint_LastOnPanel) + { + Serial.print(">"); + } + Serial.print("\t"); + } + Serial.println(); + } +} + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + DumpTopo(); +} + +void loop() +{ + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino new file mode 100644 index 0000000..1dfc645 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTilesTest/NeoPixelTilesTest.ino @@ -0,0 +1,103 @@ +//---------------------------------------------------------------------- +// NeoPixelTilesTest +// This will display specific colors in specific locations on the led panels +// +// This is useful in confirming the layout of your panels +// +// It does require that you have the actual panels connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts and +// how you want them rotated. Not all the rotations are listed here +// but you can modifiy the name to include the rotation of 90,180, or 270. + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; +// typedef RowMajor90Layout MyPanelLayout; // note rotation 90 was used + +// change this to be one of the layouts which will define the layout +// of the panels themselves +typedef ColumnMajorLayout MyTilesLayout; + +// make sure to set these panel values to the sizes of yours +const uint8_t PanelWidth = 8; // 8 pixel x 8 pixel matrix of leds +const uint8_t PanelHeight = 8; +const uint8_t TileWidth = 4; // laid out in 4 panels x 2 panels mosaic +const uint8_t TileHeight = 2; + +const uint16_t PixelCount = PanelWidth * PanelHeight * TileWidth * TileHeight; +const uint8_t PixelPin = 2; + +NeoTiles tiles( + PanelWidth, + PanelHeight, + TileWidth, + TileHeight); + +NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +RgbColor red(128, 0, 0); +RgbColor green(0, 128, 0); +RgbColor blue(0, 0, 128); +RgbColor white(128); +// if using NeoRgbwFeature above, use this white instead to use +// the correct white element of the LED +//RgbwColor white(128); +RgbColor black(0); + +const uint16_t left = 0; +const uint16_t right = PanelWidth - 1; +const uint16_t top = 0; +const uint16_t bottom = PanelHeight - 1; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + + strip.Begin(); + strip.Show(); + + Serial.println(); + Serial.println("Running..."); +} + +void loop() +{ + delay(2500); + + Serial.println(); + Serial.println("If your panel is correctly defined, you should see ..."); + Serial.println("Upper left is white."); + Serial.println("Upper right is Red."); + Serial.println("Lower right is Green"); + Serial.println("Lower Left is Blue"); + + // use the topo to map the 2d cordinate to the pixel + // and use that to SetPixelColor + strip.SetPixelColor(tiles.Map(left, top), white); + strip.SetPixelColor(tiles.Map(right, top), red); + strip.SetPixelColor(tiles.Map(right, bottom), green); + strip.SetPixelColor(tiles.Map(left, bottom), blue); + strip.Show(); + + delay(5000); + + Serial.println(); + Serial.println("Cleared to black ..."); + strip.ClearTo(black); + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino new file mode 100644 index 0000000..aa78a25 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyDump/NeoPixelTopologyDump.ino @@ -0,0 +1,78 @@ +//---------------------------------------------------------------------- +// NeoPixelTopologyDump +// This will dump to the serial output a grid map of the defined topology +// The output is displayed as row column labeled grid with the NeoPixelBus +// index of the pixel at the intersection of the row and column +// +// This is useful in visualising the layout of your panel so you can +// confirm you have the correct pattern +// +// It does not require that you have the actual panel connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts and +// how you want them rotated. Not all the rotations are listed here +// but you can modifiy the name to include the rotation of 90,180, or 270. + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; +// typedef RowMajor90Layout MyPanelLayout; // note rotation 90 was used + +// make sure to set these panel values to the sizes of yours +const uint8_t PanelWidth = 8; // 8 pixel x 8 pixel matrix of leds +const uint8_t PanelHeight = 8; + +NeoTopology topo(PanelWidth, PanelHeight); + +void DumpTopo() +{ + Serial.println(); + + Serial.print("\t\t"); + for (int x = 0; x < topo.getWidth(); x++) + { + Serial.print(x); + Serial.print("\t"); + } + Serial.println(); + + Serial.print("\t--"); + for (int x = 0; x < topo.getWidth(); x++) + { + Serial.print("--------"); + } + Serial.println(); + + for (int y = 0; y < topo.getHeight(); y++) + { + Serial.print(" "); + Serial.print(y); + Serial.print("\t|\t"); + + for (int x = 0; x < topo.getWidth(); x++) + { + Serial.print(topo.Map(x, y)); + Serial.print("\t"); + } + Serial.println(); + } +} + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + DumpTopo(); +} + +void loop() +{ + +} + diff --git a/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino new file mode 100644 index 0000000..2071abb --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/examples/topologies/NeoPixelTopologyTest/NeoPixelTopologyTest.ino @@ -0,0 +1,92 @@ +//---------------------------------------------------------------------- +// NeoPixelTopologyTest +// This will display specific colors in specific locations on the led panel +// +// This is useful in confirming the layout of your panel +// +// It does require that you have the actual panel connected +//---------------------------------------------------------------------- + +#include +#include + +// uncomment one of these that matches your panel pixel layouts and +// how you want them rotated. Not all the rotations are listed here +// but you can modifiy the name to include the rotation of 90,180, or 270. + +typedef ColumnMajorAlternatingLayout MyPanelLayout; +// typedef ColumnMajorLayout MyPanelLayout; +// typedef RowMajorAlternatingLayout MyPanelLayout; +// typedef RowMajorLayout MyPanelLayout; +// typedef RowMajor90Layout MyPanelLayout; // note rotation 90 was used + +// make sure to set these panel values to the sizes of yours +const uint8_t PanelWidth = 8; // 8 pixel x 8 pixel matrix of leds +const uint8_t PanelHeight = 8; +const uint16_t PixelCount = PanelWidth * PanelHeight; +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 + +NeoTopology topo(PanelWidth, PanelHeight); + +NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); +//NeoPixelBus strip(PixelCount, PixelPin); +// for esp8266 omit the pin +//NeoPixelBus strip(PixelCount); + +RgbColor red(128, 0, 0); +RgbColor green(0, 128, 0); +RgbColor blue(0, 0, 128); +RgbColor white(128); +// if using NeoRgbwFeature above, use this white instead to use +// the correct white element of the LED +//RgbwColor white(128); +RgbColor black(0); + +const uint16_t left = 0; +const uint16_t right = PanelWidth - 1; +const uint16_t top = 0; +const uint16_t bottom = PanelHeight - 1; + +void setup() +{ + Serial.begin(115200); + while (!Serial); // wait for serial attach + + Serial.println(); + Serial.println("Initializing..."); + + strip.Begin(); + strip.Show(); + + Serial.println(); + Serial.println("Running..."); +} + +void loop() +{ + delay(2500); + + Serial.println(); + Serial.println("If your panel is correctly defined, you should see ..."); + Serial.println("Upper left is white."); + Serial.println("Upper right is Red."); + Serial.println("Lower right is Green"); + Serial.println("Lower Left is Blue"); + + // use the topo to map the 2d cordinate to the pixel + // and use that to SetPixelColor + strip.SetPixelColor(topo.Map(left, top), white); + strip.SetPixelColor(topo.Map(right, top), red); + strip.SetPixelColor(topo.Map(right, bottom), green); + strip.SetPixelColor(topo.Map(left, bottom), blue); + strip.Show(); + + delay(5000); + + Serial.println(); + Serial.println("Cleared to black ..."); + strip.ClearTo(black); + strip.Show(); +} + diff --git a/libraries/NeoPixelBus_by_Makuna/keywords.txt b/libraries/NeoPixelBus_by_Makuna/keywords.txt new file mode 100644 index 0000000..75771ef --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/keywords.txt @@ -0,0 +1,184 @@ +####################################### +# Syntax Coloring Map NeoPixelBus +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +NeoPixelBus KEYWORD1 +RgbwColor KEYWORD1 +RgbColor KEYWORD1 +HslColor KEYWORD1 +HsbColor KEYWORD1 +HtmlColor KEYWORD1 +NeoGrbFeature KEYWORD1 +NeoGrbwFeature KEYWORD1 +NeoRgbwFeature KEYWORD1 +NeoRgbFeature KEYWORD1 +NeoBrgFeature KEYWORD1 +NeoRbgFeature KEYWORD1 +DotStarBgrFeature KEYWORD1 +DotStarLbgrFeature KEYWORD1 +NeoWs2813Method KEYWORD1 +Neo800KbpsMethod KEYWORD1 +Neo400KbpsMethod KEYWORD1 +NeoAvrWs2813Method KEYWORD1 +NeoAvr800KbpsMethod KEYWORD1 +NeoAvr400KbpsMethod KEYWORD1 +NeoEsp8266DmaWs2813Method KEYWORD1 +NeoEsp8266Dma800KbpsMethod KEYWORD1 +NeoEsp8266Dma400KbpsMethod KEYWORD1 +NeoEsp8266UartWs2813Method KEYWORD1 +NeoEsp8266Uart800KbpsMethod KEYWORD1 +NeoEsp8266Uart400KbpsMethod KEYWORD1 +NeoEsp8266AsyncUartWs2813Method KEYWORD1 +NeoEsp8266AsyncUart800KbpsMethod KEYWORD1 +NeoEsp8266AsyncUart400KbpsMethod KEYWORD1 +NeoEsp8266BitBangWs2813Method KEYWORD1 +NeoEsp8266BitBang800KbpsMethod KEYWORD1 +NeoEsp8266BitBang400KbpsMethod KEYWORD1 +NeoEsp32BitBangWs2813Method KEYWORD1 +NeoEsp32BitBang800KbpsMethod KEYWORD1 +NeoEsp32BitBang400KbpsMethod KEYWORD1 +DotStarMethod KEYWORD1 +DotStarSpiMethod KEYWORD1 +NeoPixelAnimator KEYWORD1 +AnimUpdateCallback KEYWORD1 +AnimationParam KEYWORD1 +NeoEase KEYWORD1 +AnimEaseFunction KEYWORD1 +RowMajorLayout KEYWORD1 +RowMajor90Layout KEYWORD1 +RowMajor180Layout KEYWORD1 +RowMajor270Layout KEYWORD1 +RowMajorAlternatingLayout KEYWORD1 +RowMajorAlternating90Layout KEYWORD1 +RowMajorAlternating180Layout KEYWORD1 +RowMajorAlternating270Layout KEYWORD1 +ColumnMajorLayout KEYWORD1 +ColumnMajor90Layout KEYWORD1 +ColumnMajor180Layout KEYWORD1 +ColumnMajor270Layout KEYWORD1 +ColumnMajorAlternatingLayout KEYWORD1 +ColumnMajorAlternating90Layout KEYWORD1 +ColumnMajorAlternating180Layout KEYWORD1 +ColumnMajorAlternating270Layout KEYWORD1 +NeoTopology KEYWORD1 +NeoRingTopology KEYWORD1 +NeoTiles KEYWORD1 +NeoMosaic KEYWORD1 +NeoGammaEquationMethod KEYWORD1 +NeoGammaTableMethod KEYWORD1 +NeoGamma KEYWORD1 +NeoHueBlendShortestDistance KEYWORD1 +NeoHueBlendLongestDistance KEYWORD1 +NeoHueBlendClockwiseDirection KEYWORD1 +NeoHueBlendCounterClockwiseDirection KEYWORD1 +NeoBufferContext KEYWORD1 +LayoutMapCallback KEYWORD1 +NeoBufferMethod KEYWORD1 +NeoBufferProgmemMethod KEYWORD1 +NeoBuffer KEYWORD1 +NeoVerticalSpriteSheet KEYWORD1 +NeoBitmapFile KEYWORD1 +HtmlShortColorNames KEYWORD1 +HtmlColorNames KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +Begin KEYWORD2 +Show KEYWORD2 +CanShow KEYWORD2 +ClearTo KEYWORD2 +RotateLeft KEYWORD2 +ShiftLeft KEYWORD2 +RotateRight KEYWORD2 +ShiftRight KEYWORD2 +IsDirty KEYWORD2 +Dirty KEYWORD2 +ResetDirty KEYWORD2 +Pixels KEYWORD2 +PixelsSize KEYWORD2 +PixelCount KEYWORD2 +SetPixelColor KEYWORD2 +GetPixelColor KEYWORD2 +CalculateBrightness KEYWORD2 +Darken KEYWORD2 +Lighten KEYWORD2 +LinearBlend KEYWORD2 +BilinearBlend KEYWORD2 +IsAnimating KEYWORD2 +NextAvailableAnimation KEYWORD2 +StartAnimation KEYWORD2 +StopAnimation KEYWORD2 +RestartAnimation KEYWORD2 +IsAnimationActive KEYWORD2 +AnimationDuration KEYWORD2 +UpdateAnimations KEYWORD2 +IsPaused KEYWORD2 +Pause KEYWORD2 +Resume KEYWORD2 +getTimeScale KEYWORD2 +setTimeScale KEYWORD2 +QuadraticIn KEYWORD2 +QuadraticOut KEYWORD2 +QuadraticInOut KEYWORD2 +CubicIn KEYWORD2 +CubicOut KEYWORD2 +CubicInOut KEYWORD2 +QuarticIn KEYWORD2 +QuarticOut KEYWORD2 +QuarticInOut KEYWORD2 +QuinticIn KEYWORD2 +QuinticOut KEYWORD2 +QuinticInOut KEYWORD2 +SinusoidalIn KEYWORD2 +SinusoidalOut KEYWORD2 +SinusoidalInOut KEYWORD2 +ExponentialIn KEYWORD2 +ExponentialOut KEYWORD2 +ExponentialInOut KEYWORD2 +CircularIn KEYWORD2 +CircularOut KEYWORD2 +CircularInOut KEYWORD2 +Gamma KEYWORD2 +Map KEYWORD2 +MapProbe KEYWORD2 +getWidth KEYWORD2 +getHeight KEYWORD2 +getCountOfRings KEYWORD2 +getPixelCountAtRing KEYWORD2 +getPixelCount KEYWORD2 +TopologyHint KEYWORD2 +Correct KEYWORD2 +SpriteWidth KEYWORD2 +SpriteHeight KEYWORD2 +SpriteCount KEYWORD2 +Blt KEYWORD2 +Width KEYWORD2 +Height KEYWORD2 +Parse KEYWORD2 +ToString KEYWORD2 +ToNumericalString KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + +NEO_MILLISECONDS LITERAL1 +NEO_CENTISECONDS LITERAL1 +NEO_DECISECONDS LITERAL1 +NEO_SECONDS LITERAL1 +NEO_DECASECONDS LITERAL1 +AnimationState_Started LITERAL1 +AnimationState_Progress LITERAL1 +AnimationState_Completed LITERAL1 +NeoTopologyHint_FirstOnPanel LITERAL1 +NeoTopologyHint_InPanel LITERAL1 +NeoTopologyHint_LastOnPanel LITERAL1 +NeoTopologyHint_OutOfBounds LITERAL1 +PixelIndex_OutOfBounds LITERAL1 \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/library.json b/libraries/NeoPixelBus_by_Makuna/library.json new file mode 100644 index 0000000..6133232 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/library.json @@ -0,0 +1,15 @@ +{ + "name": "NeoPixelBus", + "keywords": "NeoPixel, WS2811, WS2812, WS2813, SK6812, DotStar, APA102, RGB, RGBW", + "description": "A library that makes controlling NeoPixels (WS2811, WS2812, WS2813 & SK6812) and DotStars (APA102) easy. Supports most Arduino platforms. Support for RGBW pixels. Includes seperate RgbColor, RgbwColor, HslColor, and HsbColor objects. Includes an animator class that helps create asyncronous animations. For Esp8266 it has three methods of sending NeoPixel data, DMA, UART, and Bit Bang; and two methods of sending DotStar data, hardware SPI and software SPI.", + "homepage": "https://github.com/Makuna/NeoPixelBus/wiki", + "repository": + { + "type": "git", + "url": "https://github.com/Makuna/NeoPixelBus" + }, + "version": "2.3.1", + "frameworks": "arduino", + "platforms": "*" +} + diff --git a/libraries/NeoPixelBus_by_Makuna/library.properties b/libraries/NeoPixelBus_by_Makuna/library.properties new file mode 100644 index 0000000..8f4a199 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/library.properties @@ -0,0 +1,9 @@ +name=NeoPixelBus by Makuna +version=2.3.1 +author=Michael C. Miller (makuna@live.com) +maintainer=Michael C. Miller (makuna@live.com) +sentence=A library that makes controlling NeoPixels (WS2811, WS2812, WS2813 & SK6812) and DotStars (APA102) easy. +paragraph=Supports most Arduino platforms, including Esp8266 and Esp32. Support for RGBW pixels. Includes seperate RgbColor, RgbwColor, HslColor, and HsbColor objects. Includes an animator class that helps create asyncronous animations. Supports Matrix layout of pixels. Includes Gamma corretion object. For Esp8266 it has three methods of sending NeoPixel data, DMA, UART, and Bit Bang; and two methods of sending DotStar data, hardware SPI and software SPI. +category=Display +url=https://github.com/Makuna/NeoPixelBus/wiki +architectures=* \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/NeoPixelAnimator.h b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelAnimator.h new file mode 100644 index 0000000..21eb2ad --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelAnimator.h @@ -0,0 +1,174 @@ +/*------------------------------------------------------------------------- +NeoPixelAnimator provides animation timing support. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#include +#include "internal/NeoEase.h" + +enum AnimationState +{ + AnimationState_Started, + AnimationState_Progress, + AnimationState_Completed +}; + +struct AnimationParam +{ + float progress; + uint16_t index; + AnimationState state; +}; + +#ifdef ARDUINO_ARCH_AVR + +typedef void(*AnimUpdateCallback)(const AnimationParam& param); + +#else + +#undef max +#undef min +#include +typedef std::function AnimUpdateCallback; + +#endif + + +#define NEO_MILLISECONDS 1 // ~65 seconds max duration, ms updates +#define NEO_CENTISECONDS 10 // ~10.9 minutes max duration, centisecond updates +#define NEO_DECISECONDS 100 // ~1.8 hours max duration, decisecond updates +#define NEO_SECONDS 1000 // ~18.2 hours max duration, second updates +#define NEO_DECASECONDS 10000 // ~7.5 days, 10 second updates + +class NeoPixelAnimator +{ +public: + NeoPixelAnimator(uint16_t countAnimations, uint16_t timeScale = NEO_MILLISECONDS); + ~NeoPixelAnimator(); + + bool IsAnimating() const + { + return _activeAnimations > 0; + } + + + bool NextAvailableAnimation(uint16_t* indexAvailable, uint16_t indexStart = 0); + + void StartAnimation(uint16_t indexAnimation, uint16_t duration, AnimUpdateCallback animUpdate); + void StopAnimation(uint16_t indexAnimation); + void StopAll(); + + void RestartAnimation(uint16_t indexAnimation) + { + if (indexAnimation >= _countAnimations || _animations[indexAnimation]._duration == 0) + { + return; + } + + StartAnimation(indexAnimation, _animations[indexAnimation]._duration, (_animations[indexAnimation]._fnCallback)); + } + + bool IsAnimationActive(uint16_t indexAnimation) const + { + if (indexAnimation >= _countAnimations) + { + return false; + } + return (IsAnimating() && _animations[indexAnimation]._remaining != 0); + } + + uint16_t AnimationDuration(uint16_t indexAnimation) + { + if (indexAnimation >= _countAnimations) + { + return 0; + } + return _animations[indexAnimation]._duration; + } + + void UpdateAnimations(); + + bool IsPaused() + { + return (!_isRunning); + } + + void Pause() + { + _isRunning = false; + } + + void Resume() + { + _isRunning = true; + _animationLastTick = millis(); + } + + uint16_t getTimeScale() + { + return _timeScale; + } + + void setTimeScale(uint16_t timeScale) + { + _timeScale = (timeScale < 1) ? (1) : (timeScale > 32768) ? 32768 : timeScale; + } + +private: + struct AnimationContext + { + AnimationContext() : + _duration(0), + _remaining(0), + _fnCallback(NULL) + {} + + void StartAnimation(uint16_t duration, AnimUpdateCallback animUpdate) + { + _duration = duration; + _remaining = duration; + _fnCallback = animUpdate; + + } + + void StopAnimation() + { + _remaining = 0; + } + + uint16_t _duration; + uint16_t _remaining; + + AnimUpdateCallback _fnCallback; + }; + + uint16_t _countAnimations; + AnimationContext* _animations; + uint32_t _animationLastTick; + uint16_t _activeAnimations; + uint16_t _timeScale; + bool _isRunning; +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBrightnessBus.h b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBrightnessBus.h new file mode 100644 index 0000000..0ff1d52 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBrightnessBus.h @@ -0,0 +1,161 @@ +/*------------------------------------------------------------------------- +NeoPixelBus library wrapper template class that provides overall brightness control + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#include "NeoPixelBus.h" + +template class NeoPixelBrightnessBus : + public NeoPixelBus +{ +private: + void ConvertColor(typename T_COLOR_FEATURE::ColorObject* color) + { + if (_brightness) + { + uint8_t* ptr = (uint8_t*) color; + uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize; + + while (ptr != ptrEnd) + { + uint16_t value = *ptr; + *ptr++ = (value * _brightness) >> 8; + } + } + } + + void RecoverColor(typename T_COLOR_FEATURE::ColorObject* color) const + { + if (_brightness) + { + uint8_t* ptr = (uint8_t*) color; + uint8_t* ptrEnd = ptr + T_COLOR_FEATURE::PixelSize; + + while (ptr != ptrEnd) + { + uint16_t value = *ptr; + *ptr++ = (value << 8) / _brightness; + } + } + } + +public: + NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pin) : + NeoPixelBus(countPixels, pin), + _brightness(0) + { + } + + NeoPixelBrightnessBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) : + NeoPixelBus(countPixels, pinClock, pinData), + _brightness(0) + { + } + + NeoPixelBrightnessBus(uint16_t countPixels) : + NeoPixelBus(countPixels), + _brightness(0) + { + } + + void SetBrightness(uint8_t brightness) + { + // Due to using fixed point math, we modifiy the brightness + // before storing making the math faster + uint8_t newBrightness = brightness + 1; + + // Only update if there is a change + if (newBrightness != _brightness) + { + // calculate a scale to modify from old brightness to new brightness + // + uint8_t oldBrightness = _brightness - 1; // unmodify brightness value + uint16_t scale; + + if (oldBrightness == 0) + { + scale = 0; // Avoid divide by 0 + } + else if (brightness == 255) + { + scale = 65535 / oldBrightness; + } + else + { + scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness; + } + + // re-scale existing pixels + // + uint8_t* ptr = this->Pixels(); + uint8_t* ptrEnd = ptr + this->PixelsSize(); + while (ptr != ptrEnd) + { + uint16_t value = *ptr; + *ptr++ = (value * scale) >> 8; + } + + _brightness = newBrightness; + this->Dirty(); + } + } + + uint8_t GetBrightness() const + { + return _brightness - 1; + } + + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + { + ConvertColor(&color); + NeoPixelBus::SetPixelColor(indexPixel, color); + } + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + { + typename T_COLOR_FEATURE::ColorObject color = NeoPixelBus::GetPixelColor(indexPixel); + RecoverColor(&color); + return color; + } + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color) + { + ConvertColor(&color); + NeoPixelBus::ClearTo(color); + }; + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color, uint16_t first, uint16_t last) + { + ConvertColor(&color); + NeoPixelBus::ClearTo(color, first, last); + } + + +protected: + uint8_t _brightness; +}; + + diff --git a/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBus.h b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBus.h new file mode 100644 index 0000000..eaf88c6 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/NeoPixelBus.h @@ -0,0 +1,401 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +// some platforms do not define this standard progmem type for some reason +// +#ifndef PGM_VOID_P +#define PGM_VOID_P const void * +#endif + +// '_state' flags for internal state +#define NEO_DIRTY 0x80 // a change was made to pixel data that requires a show + +#include "internal/NeoHueBlend.h" + +#include "internal/RgbColor.h" +#include "internal/HslColor.h" +#include "internal/HsbColor.h" +#include "internal/HtmlColor.h" +#include "internal/RgbwColor.h" + +#include "internal/NeoColorFeatures.h" +#include "internal/DotStarColorFeatures.h" + +#include "internal/Layouts.h" +#include "internal/NeoTopology.h" +#include "internal/NeoRingTopology.h" +#include "internal/NeoTiles.h" +#include "internal/NeoMosaic.h" + +#include "internal/NeoBufferContext.h" +#include "internal/NeoBufferMethods.h" +#include "internal/NeoBuffer.h" +#include "internal/NeoSpriteSheet.h" +#include "internal/NeoBitmapFile.h" +#include "internal/NeoDib.h" +#include "internal/NeoEase.h" +#include "internal/NeoGamma.h" + +#if defined(ARDUINO_ARCH_ESP8266) + +#include "internal/NeoEsp8266DmaMethod.h" +#include "internal/NeoEsp8266UartMethod.h" +#include "internal/NeoEspBitBangMethod.h" +#include "internal/DotStarGenericMethod.h" + +#elif defined(ARDUINO_ARCH_ESP32) + +#include "internal/NeoEsp32I2sMethod.h" +#include "internal/NeoEspBitBangMethod.h" +#include "internal/DotStarGenericMethod.h" + +#elif defined(__arm__) // must be before ARDUINO_ARCH_AVR due to Teensy incorrectly having it set + +#include "internal/NeoArmMethod.h" +#include "internal/DotStarGenericMethod.h" + +#elif defined(ARDUINO_ARCH_AVR) + +#include "internal/NeoAvrMethod.h" +#include "internal/DotStarAvrMethod.h" + +#else +#error "Platform Currently Not Supported, please add an Issue at Github/Makuna/NeoPixelBus" +#endif + +#if !defined(__AVR_ATtiny85__) +#include "internal/DotStarSpiMethod.h" +#endif + + +template class NeoPixelBus +{ +public: + // Constructor: number of LEDs, pin number + // NOTE: Pin Number maybe ignored due to hardware limitations of the method. + + NeoPixelBus(uint16_t countPixels, uint8_t pin) : + _countPixels(countPixels), + _state(0), + _method(pin, countPixels, T_COLOR_FEATURE::PixelSize) + { + } + + NeoPixelBus(uint16_t countPixels, uint8_t pinClock, uint8_t pinData) : + _countPixels(countPixels), + _state(0), + _method(pinClock, pinData, countPixels, T_COLOR_FEATURE::PixelSize) + { + } + + NeoPixelBus(uint16_t countPixels) : + _countPixels(countPixels), + _state(0), + _method(countPixels, T_COLOR_FEATURE::PixelSize) + { + } + + ~NeoPixelBus() + { + } + + operator NeoBufferContext() + { + Dirty(); // we assume you are playing with bits + return NeoBufferContext(_method.getPixels(), _method.getPixelsSize()); + } + + void Begin() + { + _method.Initialize(); + Dirty(); + } + + void Show() + { + if (!IsDirty()) + { + return; + } + + _method.Update(); + + ResetDirty(); + } + + inline bool CanShow() const + { + return _method.IsReadyToUpdate(); + }; + + bool IsDirty() const + { + return (_state & NEO_DIRTY); + }; + + void Dirty() + { + _state |= NEO_DIRTY; + }; + + void ResetDirty() + { + _state &= ~NEO_DIRTY; + }; + + uint8_t* Pixels() + { + return _method.getPixels(); + }; + + size_t PixelsSize() const + { + return _method.getPixelsSize(); + }; + + size_t PixelSize() const + { + return T_COLOR_FEATURE::PixelSize; + }; + + uint16_t PixelCount() const + { + return _countPixels; + }; + + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + { + if (indexPixel < _countPixels) + { + T_COLOR_FEATURE::applyPixelColor(_method.getPixels(), indexPixel, color); + Dirty(); + } + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + { + if (indexPixel < _countPixels) + { + return T_COLOR_FEATURE::retrievePixelColor(_method.getPixels(), indexPixel); + } + else + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + }; + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color) + { + uint8_t temp[T_COLOR_FEATURE::PixelSize]; + uint8_t* pixels = _method.getPixels(); + + T_COLOR_FEATURE::applyPixelColor(temp, 0, color); + + T_COLOR_FEATURE::replicatePixel(pixels, temp, _countPixels); + + Dirty(); + }; + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color, uint16_t first, uint16_t last) + { + if (first < _countPixels && + last < _countPixels && + first <= last) + { + uint8_t temp[T_COLOR_FEATURE::PixelSize]; + uint8_t* pixels = _method.getPixels(); + uint8_t* pFront = T_COLOR_FEATURE::getPixelAddress(pixels, first); + + T_COLOR_FEATURE::applyPixelColor(temp, 0, color); + + T_COLOR_FEATURE::replicatePixel(pFront, temp, last - first + 1); + + Dirty(); + } + } + + void RotateLeft(uint16_t rotationCount) + { + if ((_countPixels - 1) >= rotationCount) + { + _rotateLeft(rotationCount, 0, _countPixels - 1); + } + } + + void RotateLeft(uint16_t rotationCount, uint16_t first, uint16_t last) + { + if (first < _countPixels && + last < _countPixels && + first < last && + (last - first) >= rotationCount) + { + _rotateLeft(rotationCount, first, last); + } + } + + void ShiftLeft(uint16_t shiftCount) + { + if ((_countPixels - 1) >= shiftCount) + { + _shiftLeft(shiftCount, 0, _countPixels - 1); + Dirty(); + } + } + + void ShiftLeft(uint16_t shiftCount, uint16_t first, uint16_t last) + { + if (first < _countPixels && + last < _countPixels && + first < last && + (last - first) >= shiftCount) + { + _shiftLeft(shiftCount, first, last); + Dirty(); + } + } + + void RotateRight(uint16_t rotationCount) + { + if ((_countPixels - 1) >= rotationCount) + { + _rotateRight(rotationCount, 0, _countPixels - 1); + } + } + + void RotateRight(uint16_t rotationCount, uint16_t first, uint16_t last) + { + if (first < _countPixels && + last < _countPixels && + first < last && + (last - first) >= rotationCount) + { + _rotateRight(rotationCount, first, last); + } + } + + void ShiftRight(uint16_t shiftCount) + { + if ((_countPixels - 1) >= shiftCount) + { + _shiftRight(shiftCount, 0, _countPixels - 1); + Dirty(); + } + } + + void ShiftRight(uint16_t shiftCount, uint16_t first, uint16_t last) + { + if (first < _countPixels && + last < _countPixels && + first < last && + (last - first) >= shiftCount) + { + _shiftRight(shiftCount, first, last); + Dirty(); + } + } + + + +protected: + const uint16_t _countPixels; // Number of RGB LEDs in strip + + uint8_t _state; // internal state + T_METHOD _method; + + void _rotateLeft(uint16_t rotationCount, uint16_t first, uint16_t last) + { + // store in temp + uint8_t temp[rotationCount * T_COLOR_FEATURE::PixelSize]; + uint8_t* pixels = _method.getPixels(); + + uint8_t* pFront = T_COLOR_FEATURE::getPixelAddress(pixels, first); + + T_COLOR_FEATURE::movePixelsInc(temp, pFront, rotationCount); + + // shift data + _shiftLeft(rotationCount, first, last); + + // move temp back + pFront = T_COLOR_FEATURE::getPixelAddress(pixels, last - (rotationCount - 1)); + T_COLOR_FEATURE::movePixelsInc(pFront, temp, rotationCount); + + Dirty(); + } + + void _shiftLeft(uint16_t shiftCount, uint16_t first, uint16_t last) + { + uint16_t front = first + shiftCount; + uint16_t count = last - front + 1; + + uint8_t* pixels = _method.getPixels(); + uint8_t* pFirst = T_COLOR_FEATURE::getPixelAddress(pixels, first); + uint8_t* pFront = T_COLOR_FEATURE::getPixelAddress(pixels, front); + + T_COLOR_FEATURE::movePixelsInc(pFirst, pFront, count); + + // intentional no dirty + } + + void _rotateRight(uint16_t rotationCount, uint16_t first, uint16_t last) + { + // store in temp + uint8_t temp[rotationCount * T_COLOR_FEATURE::PixelSize]; + uint8_t* pixels = _method.getPixels(); + + uint8_t* pFront = T_COLOR_FEATURE::getPixelAddress(pixels, last - (rotationCount - 1)); + + T_COLOR_FEATURE::movePixelsDec(temp, pFront, rotationCount); + + // shift data + _shiftRight(rotationCount, first, last); + + // move temp back + pFront = T_COLOR_FEATURE::getPixelAddress(pixels, first); + T_COLOR_FEATURE::movePixelsDec(pFront, temp, rotationCount); + + Dirty(); + } + + void _shiftRight(uint16_t shiftCount, uint16_t first, uint16_t last) + { + uint16_t front = first + shiftCount; + uint16_t count = last - front + 1; + + uint8_t* pixels = _method.getPixels(); + uint8_t* pFirst = T_COLOR_FEATURE::getPixelAddress(pixels, first); + uint8_t* pFront = T_COLOR_FEATURE::getPixelAddress(pixels, front); + + T_COLOR_FEATURE::movePixelsDec(pFront, pFirst, count); + // intentional no dirty + } +}; + + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarAvrMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarAvrMethod.h new file mode 100644 index 0000000..66bd00b --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarAvrMethod.h @@ -0,0 +1,153 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for DotStars on AVR (APA102). + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +// must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set +#if defined(ARDUINO_ARCH_AVR) && !defined(__arm__) + +class DotStarAvrMethod +{ +public: + DotStarAvrMethod(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) : + _pinClock(pinClock), + _pinData(pinData), + _sizePixels(pixelCount * elementSize) + { + pinMode(pinClock, OUTPUT); + pinMode(pinData, OUTPUT); + + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + + _portClock = portOutputRegister(digitalPinToPort(_pinClock)); + _pinMaskClock = digitalPinToBitMask(_pinClock); + _portData = portOutputRegister(digitalPinToPort(_pinData)); + _pinMaskData = digitalPinToBitMask(_pinData); + } + + ~DotStarAvrMethod() + { + pinMode(_pinClock, INPUT); + pinMode(_pinData, INPUT); + + free(_pixels); + } + + bool IsReadyToUpdate() const + { + return true; // dot stars don't have a required delay + } + + void Initialize() + { + digitalWrite(_pinClock, LOW); + digitalWrite(_pinData, LOW); + } + + void Update() + { + // start frame + for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++) + { + _transmitByte(0x00); + } + + // data + uint8_t* data = _pixels; + const uint8_t* endData = _pixels + _sizePixels; + while (data < endData) + { + _transmitByte(*data++); + } + + // end frame + // one bit for every two pixels with no less than 1 byte + const uint16_t countEndFrameBytes = ((_sizePixels / 4) + 15) / 16; + for (uint16_t endFrameByte = 0; endFrameByte < countEndFrameBytes; endFrameByte++) + { + _transmitByte(0xff); + } + + // set clock and data back to low between updates + digitalWrite(_pinData, LOW); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + const uint8_t _pinClock; // output pin number for clock line + const uint8_t _pinData; // output pin number for data line + const size_t _sizePixels; // Size of '_pixels' buffer below + + uint8_t* _pixels; // Holds LED color values + + volatile uint8_t* _portData; // Output PORT register + uint8_t _pinMaskData; // Output PORT bitmask + volatile uint8_t* _portClock; // Output PORT register + uint8_t _pinMaskClock; // Output PORT bitmask + + void _transmitByte(uint8_t data) + { + for (int bit = 7; bit >= 0; bit--) + { + // set data bit on pin + // digitalWrite(_pinData, (data & 0x80) == 0x80 ? HIGH : LOW); + if (data & 0x80) + { + *_portData |= _pinMaskData; + } + else + { + *_portData &= ~_pinMaskData; + } + + + // set clock high as data is ready + // digitalWrite(_pinClock, HIGH); + *_portClock |= _pinMaskClock; + + // done between clock toggle to give a little time + data <<= 1; + + // set clock low as data pin is changed + // digitalWrite(_pinClock, LOW); + *_portClock &= ~_pinMaskClock; + } + } +}; + +typedef DotStarAvrMethod DotStarMethod; + +#endif diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarColorFeatures.h b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarColorFeatures.h new file mode 100644 index 0000000..3719d6f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarColorFeatures.h @@ -0,0 +1,326 @@ +/*------------------------------------------------------------------------- +DotStarColorFeatures provides feature classes to describe color order and +color depth for NeoPixelBus template class when used with DotStars + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +class DotStar3Elements +{ +public: + static const size_t PixelSize = 4; // still requires 4 to be sent + + static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + + static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = pPixelSrc[0]; + *pPixelDest++ = pPixelSrc[1]; + *pPixelDest++ = pPixelSrc[2]; + *pPixelDest++ = pPixelSrc[3]; + } + } + + static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + } + } + + static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + const uint8_t* pSrc = (const uint8_t*)pPixelSrc; + while (pPixelDest < pEnd) + { + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + } + } + + static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pDestBack = pPixelDest + (count * PixelSize); + const uint8_t* pSrcBack = pPixelSrc + (count * PixelSize); + while (pDestBack > pPixelDest) + { + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + } + } + + typedef RgbColor ColorObject; +}; + +class DotStar4Elements +{ +public: + static const size_t PixelSize = 4; + + static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + + static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = pPixelSrc[0]; + *pPixelDest++ = pPixelSrc[1]; + *pPixelDest++ = pPixelSrc[2]; + *pPixelDest++ = pPixelSrc[3]; + } + } + + static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + } + } + + static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + const uint8_t* pSrc = (const uint8_t*)pPixelSrc; + while (pPixelDest < pEnd) + { + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + } + } + + static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pDestBack = pPixelDest + (count * PixelSize); + const uint8_t* pSrcBack = pPixelSrc + (count * PixelSize); + while (pDestBack > pPixelDest) + { + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + } + } + + typedef RgbwColor ColorObject; +}; + +class DotStarBgrFeature : public DotStar3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = 0xff; // upper three bits are always 111 and brightness at max + *p++ = color.B; + *p++ = color.G; + *p = color.R; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + p++; // ignore the first byte + color.B = *p++; + color.G = *p++; + color.R = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + pgm_read_byte(p++); // ignore the first byte + color.B = pgm_read_byte(p++); + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p); + + return color; + } + +}; + +class DotStarLbgrFeature : public DotStar4Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = 0xE0 | (color.W < 31 ? color.W : 31); // upper three bits are always 111 + *p++ = color.B; + *p++ = color.G; + *p = color.R; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.W = (*p++) & 0x1F; // mask out upper three bits + color.B = *p++; + color.G = *p++; + color.R = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.W = pgm_read_byte(p++) & 0x1F; // mask out upper three bits + color.B = pgm_read_byte(p++); + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p); + + return color; + } + +}; + +class DotStarGrbFeature : public DotStar3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = 0xff; // upper three bits are always 111 and brightness at max + *p++ = color.G; + *p++ = color.R; + *p = color.B; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + p++; // ignore the first byte + color.G = *p++; + color.R = *p++; + color.B = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + pgm_read_byte(p++); // ignore the first byte + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p++); + color.B = pgm_read_byte(p); + + return color; + } + +}; + +class DotStarLgrbFeature : public DotStar4Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = 0xE0 | (color.W < 31 ? color.W : 31); // upper three bits are always 111 + *p++ = color.G; + *p++ = color.R; + *p = color.B; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.W = (*p++) & 0x1F; // mask out upper three bits + color.G = *p++; + color.R = *p++; + color.B = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.W = pgm_read_byte(p++) & 0x1F; // mask out upper three bits + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p++); + color.B = pgm_read_byte(p); + + return color; + } + +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarGenericMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarGenericMethod.h new file mode 100644 index 0000000..d59d0e0 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarGenericMethod.h @@ -0,0 +1,128 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for DotStars using general Pins (APA102). + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +class DotStarGenericMethod +{ +public: + DotStarGenericMethod(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) : + _pinClock(pinClock), + _pinData(pinData), + _sizePixels(pixelCount * elementSize) + { + pinMode(pinClock, OUTPUT); + pinMode(pinData, OUTPUT); + + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + } + + ~DotStarGenericMethod() + { + pinMode(_pinClock, INPUT); + pinMode(_pinData, INPUT); + + free(_pixels); + } + + bool IsReadyToUpdate() const + { + return true; // dot stars don't have a required delay + } + + void Initialize() + { + digitalWrite(_pinClock, LOW); + digitalWrite(_pinData, LOW); + } + + void Update() + { + // start frame + for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++) + { + _transmitByte(0x00); + } + + // data + uint8_t* data = _pixels; + const uint8_t* endData = _pixels + _sizePixels; + while (data < endData) + { + _transmitByte(*data++); + } + + // end frame + // one bit for every two pixels with no less than 1 byte + const uint16_t countEndFrameBytes = ((_sizePixels / 4) + 15) / 16; + for (uint16_t endFrameByte = 0; endFrameByte < countEndFrameBytes; endFrameByte++) + { + _transmitByte(0xff); + } + + // set clock and data back to low between updates + digitalWrite(_pinData, LOW); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + const uint8_t _pinClock; // output pin number for clock line + const uint8_t _pinData; // output pin number for data line + const size_t _sizePixels; // Size of '_pixels' buffer below + + uint8_t* _pixels; // Holds LED color values + + void _transmitByte(uint8_t data) + { + for (int bit = 7; bit >= 0; bit--) + { + // set data bit on pin + digitalWrite(_pinData, (data & 0x80) == 0x80 ? HIGH : LOW); + + // set clock high as data is ready + digitalWrite(_pinClock, HIGH); + + data <<= 1; + + // set clock low as data pin is changed + digitalWrite(_pinClock, LOW); + } + } +}; + +typedef DotStarGenericMethod DotStarMethod; + + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarSpiMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarSpiMethod.h new file mode 100644 index 0000000..4aabc29 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/DotStarSpiMethod.h @@ -0,0 +1,125 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for DotStars using SPI hardware (APA102). + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#include + +class DotStarSpiMethod +{ +public: + DotStarSpiMethod(uint16_t pixelCount, size_t elementSize) : + _sizeData(pixelCount * elementSize), + _sizeSendBuffer(calcBufferSize(pixelCount * elementSize)) + { + _sendBuffer = (uint8_t*)malloc(_sizeSendBuffer); + memset(_sendBuffer, 0, _sizeSendBuffer); + setEndFrameBytes(); + } + + ~DotStarSpiMethod() + { + SPI.end(); + free(_sendBuffer); + } + + bool IsReadyToUpdate() const + { + return true; // dot stars don't have a required delay + } + + void Initialize() + { + SPI.begin(); + +#if defined(ARDUINO_ARCH_ESP8266) + SPI.setFrequency(20000000L); +#elif defined(ARDUINO_ARCH_AVR) + SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (6 MHz on Pro Trinket 3V) +#else + SPI.setClockDivider((F_CPU + 4000000L) / 8000000L); // 8-ish MHz on Due +#endif + SPI.setBitOrder(MSBFIRST); + SPI.setDataMode(SPI_MODE0); + } + + void Update() + { + // due to API inconsistencies need to call different methods on SPI +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + SPI.writeBytes(_sendBuffer, _sizeSendBuffer); +#else + SPI.transfer(_sendBuffer, _sizeSendBuffer); +#endif + } + + uint8_t* getPixels() const + { + return _sendBuffer + _countStartFrame; + }; + + size_t getPixelsSize() const + { + return _sizeData; + }; + +private: + const size_t _countStartFrame = 4; + const size_t _sizeData; // size of actuall pixel data within _sendBuffer + const size_t _sizeSendBuffer; // Size of '_sendBuffer' buffer below + + uint8_t* _sendBuffer; // Holds SPI send Buffer, including LED color values + + size_t calcBufferSize(size_t sizePixels) const + { + const size_t countEndFrameBytes = calcEndFrameSize(sizePixels); + + // start frame + data + end frame + const size_t bufferSize = _countStartFrame + sizePixels + countEndFrameBytes; + return bufferSize; + } + + size_t calcEndFrameSize(size_t sizePixels) const + { + // end frame + // one bit for every two pixels with no less than 1 byte + return ((sizePixels / 4) + 15) / 16; + } + + void setEndFrameBytes() + { + uint8_t* pEndFrame = _sendBuffer + _countStartFrame + _sizeData; + uint8_t* pEndFrameStop = pEndFrame + calcEndFrameSize(_sizeData); + while (pEndFrame != pEndFrameStop) + { + *pEndFrame++ = 0xff; + } + } +}; + + + + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.c b/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.c new file mode 100644 index 0000000..a4c5acc --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.c @@ -0,0 +1,522 @@ +// WARNING: This file contains code that is more than likely already +// exposed from the Esp32 Arduino API. It will be removed once integration is complete. +// +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if defined(ARDUINO_ARCH_ESP32) + +#include +#include +#include "stdlib.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "esp_intr.h" +#include "rom/ets_sys.h" +#include "soc/gpio_reg.h" +#include "soc/gpio_sig_map.h" +#include "soc/io_mux_reg.h" +#include "soc/rtc_cntl_reg.h" +#include "soc/i2s_struct.h" +#include "soc/dport_reg.h" +#include "soc/sens_reg.h" +#include "driver/gpio.h" +#include "driver/i2s.h" +#include "driver/dac.h" +#include "Esp32_i2s.h" +#include "esp32-hal.h" + +#define I2S_BASE_CLK (160000000L) +#define ESP32_REG(addr) (*((volatile uint32_t*)(0x3FF00000+(addr)))) + +#define I2S_DMA_QUEUE_SIZE 16 +#define I2S_DMA_MAX_DATA_LEN 4092// maximum bytes in one dma item +#define I2S_DMA_SILENCE_LEN 256 // bytes + +typedef struct i2s_dma_item_s { + uint32_t blocksize: 12; // datalen + uint32_t datalen : 12; // len*(bits_per_sample/8)*2 => max 2047*8bit/1023*16bit samples + uint32_t unused : 5; // 0 + uint32_t sub_sof : 1; // 0 + uint32_t eof : 1; // 1 => last? + uint32_t owner : 1; // 1 + + void* data; // malloc(datalen) + struct i2s_dma_item_s* next; + + // if this pointer is not null, it will be freed + void* free_ptr; + + // if DMA buffers are preallocated + uint8_t* buf; +} i2s_dma_item_t; + +typedef struct { + i2s_dev_t* bus; + int8_t ws; + int8_t bck; + int8_t out; + int8_t in; + uint32_t rate; + intr_handle_t isr_handle; + xQueueHandle tx_queue; + + uint8_t* silence_buf; + size_t silence_len; + + i2s_dma_item_t* dma_items; + size_t dma_count; + uint32_t dma_buf_len :12; + uint32_t unused :20; +} i2s_bus_t; + +static uint8_t i2s_silence_buf[I2S_DMA_SILENCE_LEN]; + +static i2s_bus_t I2S[2] = { + {&I2S0, -1, -1, -1, -1, 0, NULL, NULL, i2s_silence_buf, I2S_DMA_SILENCE_LEN, NULL, I2S_DMA_QUEUE_SIZE, 0, 0}, + {&I2S1, -1, -1, -1, -1, 0, NULL, NULL, i2s_silence_buf, I2S_DMA_SILENCE_LEN, NULL, I2S_DMA_QUEUE_SIZE, 0, 0} +}; + +void IRAM_ATTR i2sDmaISR(void* arg); +bool i2sInitDmaItems(uint8_t bus_num); + +bool i2sInitDmaItems(uint8_t bus_num) { + if (bus_num > 1) { + return false; + } + if (I2S[bus_num].tx_queue) {// already set + return true; + } + + if (I2S[bus_num].dma_items == NULL) { + I2S[bus_num].dma_items = (i2s_dma_item_t*)malloc(I2S[bus_num].dma_count* sizeof(i2s_dma_item_t)); + if (I2S[bus_num].dma_items == NULL) { + log_e("MEM ERROR!"); + return false; + } + } + + int i, i2, a; + i2s_dma_item_t* item; + + for(i=0; ieof = 1; + item->owner = 1; + item->sub_sof = 0; + item->unused = 0; + item->data = I2S[bus_num].silence_buf; + item->blocksize = I2S[bus_num].silence_len; + item->datalen = I2S[bus_num].silence_len; + item->next = &I2S[bus_num].dma_items[i2]; + item->free_ptr = NULL; + if (I2S[bus_num].dma_buf_len) { + item->buf = (uint8_t*)malloc(I2S[bus_num].dma_buf_len); + if (item->buf == NULL) { + log_e("MEM ERROR!"); + for(a=0; abuf = NULL; + } + } + + I2S[bus_num].tx_queue = xQueueCreate(I2S[bus_num].dma_count, sizeof(i2s_dma_item_t*)); + if (I2S[bus_num].tx_queue == NULL) {// memory error + log_e("MEM ERROR!"); + free(I2S[bus_num].dma_items); + I2S[bus_num].dma_items = NULL; + return false; + } + return true; +} + +void i2sSetSilenceBuf(uint8_t bus_num, uint8_t* data, size_t len) { + if (bus_num > 1 || !data || !len) { + return; + } + I2S[bus_num].silence_buf = data; + I2S[bus_num].silence_len = len; +} + +esp_err_t i2sSetClock(uint8_t bus_num, uint8_t div_num, uint8_t div_b, uint8_t div_a, uint8_t bck, uint8_t bits) { + if (bus_num > 1 || div_a > 63 || div_b > 63 || bck > 63) { + return ESP_FAIL; + } + i2s_dev_t* i2s = I2S[bus_num].bus; + i2s->clkm_conf.clka_en = 0; + i2s->clkm_conf.clkm_div_a = div_a; + i2s->clkm_conf.clkm_div_b = div_b; + i2s->clkm_conf.clkm_div_num = div_num; + i2s->sample_rate_conf.tx_bck_div_num = bck; + i2s->sample_rate_conf.rx_bck_div_num = bck; + i2s->sample_rate_conf.tx_bits_mod = bits; + i2s->sample_rate_conf.rx_bits_mod = bits; + return ESP_OK; +} + +void i2sSetTxDataMode(uint8_t bus_num, i2s_tx_chan_mod_t chan_mod, i2s_tx_fifo_mod_t fifo_mod) { + if (bus_num > 1) { + return; + } + + I2S[bus_num].bus->conf_chan.tx_chan_mod = chan_mod; // 0:dual channel; 1:right channel; 2:left channel; 3:left channel constant; 4:right channel constant; (channels flipped if tx_msb_right == 1) + I2S[bus_num].bus->fifo_conf.tx_fifo_mod = fifo_mod; // 0:16-bit dual channel; 1:16-bit single channel; 2:32-bit dual channel; 3:32-bit single channel data +} + +void i2sSetDac(uint8_t bus_num, bool right, bool left) { + if (bus_num > 1) { + return; + } + + if (!right && !left) { + dac_output_disable(1); + dac_output_disable(2); + dac_i2s_disable(); + I2S[bus_num].bus->conf2.lcd_en = 0; + I2S[bus_num].bus->conf.tx_right_first = 0; + I2S[bus_num].bus->conf2.camera_en = 0; + I2S[bus_num].bus->conf.tx_msb_shift = 1;// I2S signaling + return; + } + + i2sSetPins(bus_num, -1, -1, -1, -1); + I2S[bus_num].bus->conf2.lcd_en = 1; + I2S[bus_num].bus->conf.tx_right_first = 0; + I2S[bus_num].bus->conf2.camera_en = 0; + I2S[bus_num].bus->conf.tx_msb_shift = 0; + dac_i2s_enable(); + + if (right) {// DAC1, right channel, GPIO25 + dac_output_enable(1); + } + if (left) { // DAC2, left channel, GPIO26 + dac_output_enable(2); + } +} + +void i2sSetPins(uint8_t bus_num, int8_t out, int8_t ws, int8_t bck, int8_t in) { + if (bus_num > 1) { + return; + } + + if ((ws >= 0 && I2S[bus_num].ws == -1) || (bck >= 0 && I2S[bus_num].bck == -1) || (out >= 0 && I2S[bus_num].out == -1)) { + i2sSetDac(bus_num, false, false); + } + + if (ws >= 0) { + if (I2S[bus_num].ws != ws) { + if (I2S[bus_num].ws >= 0) { + gpio_matrix_out(I2S[bus_num].ws, 0x100, false, false); + } + I2S[bus_num].ws = ws; + pinMode(ws, OUTPUT); + gpio_matrix_out(ws, bus_num?I2S1O_WS_OUT_IDX:I2S0O_WS_OUT_IDX, false, false); + } + } else if (I2S[bus_num].ws >= 0) { + gpio_matrix_out(I2S[bus_num].ws, 0x100, false, false); + I2S[bus_num].ws = -1; + } + + if (bck >= 0) { + if (I2S[bus_num].bck != bck) { + if (I2S[bus_num].bck >= 0) { + gpio_matrix_out(I2S[bus_num].bck, 0x100, false, false); + } + I2S[bus_num].bck = bck; + pinMode(bck, OUTPUT); + gpio_matrix_out(bck, bus_num?I2S1O_BCK_OUT_IDX:I2S0O_BCK_OUT_IDX, false, false); + } + } else if (I2S[bus_num].bck >= 0) { + gpio_matrix_out(I2S[bus_num].bck, 0x100, false, false); + I2S[bus_num].bck = -1; + } + + if (out >= 0) { + if (I2S[bus_num].out != out) { + if (I2S[bus_num].out >= 0) { + gpio_matrix_out(I2S[bus_num].out, 0x100, false, false); + } + I2S[bus_num].out = out; + pinMode(out, OUTPUT); + gpio_matrix_out(out, bus_num?I2S1O_DATA_OUT23_IDX:I2S0O_DATA_OUT23_IDX, false, false); + } + } else if (I2S[bus_num].out >= 0) { + gpio_matrix_out(I2S[bus_num].out, 0x100, false, false); + I2S[bus_num].out = -1; + } + +} + +bool i2sWriteDone(uint8_t bus_num) { + if (bus_num > 1) { + return false; + } + return (I2S[bus_num].dma_items[0].data == I2S[bus_num].silence_buf && I2S[bus_num].dma_items[1].data == I2S[bus_num].silence_buf); +} + +void i2sInit(uint8_t bus_num, uint32_t bits_per_sample, uint32_t sample_rate, i2s_tx_chan_mod_t chan_mod, i2s_tx_fifo_mod_t fifo_mod, size_t dma_count, size_t dma_len) { + if (bus_num > 1) { + return; + } + + I2S[bus_num].dma_count = dma_count; + I2S[bus_num].dma_buf_len = dma_len & 0xFFF; + + if (!i2sInitDmaItems(bus_num)) { + return; + } + + if (bus_num) { + periph_module_enable(PERIPH_I2S1_MODULE); + } else { + periph_module_enable(PERIPH_I2S0_MODULE); + } + + esp_intr_disable(I2S[bus_num].isr_handle); + i2s_dev_t* i2s = I2S[bus_num].bus; + i2s->out_link.stop = 1; + i2s->conf.tx_start = 0; + i2s->int_ena.val = 0; + i2s->int_clr.val = 0xFFFFFFFF; + i2s->fifo_conf.dscr_en = 0; + + // reset fifo + i2s->conf.rx_fifo_reset = 1; + i2s->conf.rx_fifo_reset = 0; + i2s->conf.tx_fifo_reset = 1; + i2s->conf.tx_fifo_reset = 0; + + // reset i2s + i2s->conf.tx_reset = 1; + i2s->conf.tx_reset = 0; + i2s->conf.rx_reset = 1; + i2s->conf.rx_reset = 0; + + // reset dma + i2s->lc_conf.in_rst = 1; + i2s->lc_conf.in_rst = 0; + i2s->lc_conf.out_rst = 1; + i2s->lc_conf.out_rst = 0; + + // Enable and configure DMA + i2s->lc_conf.check_owner = 0; + i2s->lc_conf.out_loop_test = 0; + i2s->lc_conf.out_auto_wrback = 0; + i2s->lc_conf.out_data_burst_en = 0; + i2s->lc_conf.outdscr_burst_en = 0; + i2s->lc_conf.out_no_restart_clr = 0; + i2s->lc_conf.indscr_burst_en = 0; + i2s->lc_conf.out_eof_mode = 1; + + i2s->pdm_conf.pcm2pdm_conv_en = 0; + i2s->pdm_conf.pdm2pcm_conv_en = 0; + // SET_PERI_REG_BITS(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_SOC_CLK_SEL, 0x1, RTC_CNTL_SOC_CLK_SEL_S); + + + i2s->conf_chan.tx_chan_mod = chan_mod; // 0-two channel;1-right;2-left;3-righ;4-left + i2s->conf_chan.rx_chan_mod = chan_mod; // 0-two channel;1-right;2-left;3-righ;4-left + i2s->fifo_conf.tx_fifo_mod = fifo_mod; // 0-right&left channel;1-one channel + i2s->fifo_conf.rx_fifo_mod = fifo_mod; // 0-right&left channel;1-one channel + + i2s->conf.tx_mono = 0; + i2s->conf.rx_mono = 0; + + i2s->conf.tx_start = 0; + i2s->conf.rx_start = 0; + + i2s->conf.tx_short_sync = 0; + i2s->conf.rx_short_sync = 0; + i2s->conf.tx_msb_shift = (bits_per_sample != 8);// 0:DAC/PCM, 1:I2S + i2s->conf.rx_msb_shift = 0; + + i2s->conf.tx_slave_mod = 0; // Master + + i2s->conf.tx_msb_right = 0; + i2s->conf.tx_right_first = (bits_per_sample == 8); + i2s->conf2.lcd_en = (bits_per_sample == 8); + i2s->conf2.camera_en = 0; + + i2s->fifo_conf.tx_fifo_mod_force_en = 1; + + i2s->pdm_conf.rx_pdm_en = 0; + i2s->pdm_conf.tx_pdm_en = 0; + + i2sSetSampleRate(bus_num, sample_rate, bits_per_sample); + + // enable intr in cpu // + esp_intr_alloc(bus_num?ETS_I2S1_INTR_SOURCE:ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1, &i2sDmaISR, &I2S[bus_num], &I2S[bus_num].isr_handle); + // enable send intr + i2s->int_ena.out_eof = 1; + i2s->int_ena.out_dscr_err = 1; + + i2s->fifo_conf.dscr_en = 1;// enable dma + i2s->out_link.start = 0; + i2s->out_link.addr = (uint32_t)(&I2S[bus_num].dma_items[0]); // loads dma_struct to dma + i2s->out_link.start = 1; // starts dma + i2s->conf.tx_start = 1;// Start I2s module + + esp_intr_enable(I2S[bus_num].isr_handle); +} + +esp_err_t i2sSetSampleRate(uint8_t bus_num, uint32_t rate, uint8_t bits) { + if (bus_num > 1) { + return ESP_FAIL; + } + + if (I2S[bus_num].rate == rate) { + return ESP_OK; + } + + int clkmInteger, clkmDecimals, bck = 0; + double denom = (double)1 / 63; + int channel = 2; + + double mclk, clkmdiv; + int factor; + + if (bits == 8) { + factor = 120; + } else { + factor = (256 % bits) ? 384 : 256; + } + + clkmdiv = (double)I2S_BASE_CLK / (rate* factor); + if (clkmdiv > 256) { + log_e("rate is too low"); + return ESP_FAIL; + } + I2S[bus_num].rate = rate; + + clkmInteger = clkmdiv; + clkmDecimals = ((clkmdiv - clkmInteger) / denom); + + if (bits == 8) { + mclk = rate* factor; + bck = 60; + bits = 16; + } else { + mclk = (double)clkmInteger + (denom* clkmDecimals); + bck = factor/(bits* channel); + } + + i2sSetClock(bus_num, clkmInteger, clkmDecimals, 63, bck, bits); + + return ESP_OK; +} + +void IRAM_ATTR i2sDmaISR(void* arg) +{ + i2s_dma_item_t* dummy = NULL; + i2s_bus_t* dev = (i2s_bus_t*)arg; + portBASE_TYPE hpTaskAwoken = 0; + + if (dev->bus->int_st.out_dscr_err) { + ets_printf("out_dscr_err\n"); + } + while (dev->bus->int_st.out_eof) { + i2s_dma_item_t* item = (i2s_dma_item_t*)dev->bus->out_eof_des_addr; + dev->bus->int_clr.out_eof = 1; + + if (item->data != dev->silence_buf) { + item->data = dev->silence_buf; + item->blocksize = dev->silence_len; + item->datalen = dev->silence_len; + } + if (xQueueIsQueueFullFromISR(dev->tx_queue) == pdTRUE && xQueueReceiveFromISR(dev->tx_queue, &dummy, &hpTaskAwoken) == pdTRUE && dummy->free_ptr) { + free(dummy->free_ptr); + dummy->free_ptr = NULL; + } + xQueueSendFromISR(dev->tx_queue, (void*)&item, &hpTaskAwoken); + } + dev->bus->int_clr.val = dev->bus->int_st.val; + if (hpTaskAwoken == pdTRUE) { + portYIELD_FROM_ISR(); + } +} + +size_t i2sWrite(uint8_t bus_num, uint8_t* data, size_t len, bool copy, bool free_when_sent) { + if (bus_num > 1 || !I2S[bus_num].tx_queue) { + return 0; + } + size_t index = 0; + size_t toSend = len; + size_t limit = I2S_DMA_MAX_DATA_LEN; + i2s_dma_item_t* item = NULL; + + if (I2S[bus_num].dma_buf_len) { + limit = I2S[bus_num].dma_buf_len; + } + + while (len) { + if (xQueueReceive(I2S[bus_num].tx_queue, &item, portMAX_DELAY) == pdFALSE) { + log_e("xQueueReceive failed\n"); + break; + } + if (item->free_ptr) { + free(item->free_ptr); + item->free_ptr = NULL; + } + + toSend = len; + + if (toSend > limit) { + toSend = limit; + } + + if (!copy) { + // data is constant. no need to copy + item->data = data+index; + item->blocksize = toSend; + item->datalen = toSend; + if (free_when_sent && (toSend == len)) { + // free the full data when done + item->free_ptr = data; + } + } else if (item->buf) { + // dma buffers are preallocated + memcpy(item->buf, data+index, toSend); + item->data = item->buf; + item->blocksize = toSend; + item->datalen = toSend; + } else { + // copy data to a new buffer (dma buffers are NULL) + uint8_t* buf = (uint8_t*)malloc(toSend); + if (!buf) { + log_e("buf = NULL; %u/%u (%u/%u)\n", index, len, toSend, esp_get_free_heap_size()); + break; + } + memcpy(buf, data+index, toSend); + item->free_ptr = buf; + item->data = buf; + item->blocksize = toSend; + item->datalen = toSend; + } + len -= toSend; + index += toSend; + } + return index; +} + +#endif diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.h b/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.h new file mode 100644 index 0000000..a779e8c --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/Esp32_i2s.h @@ -0,0 +1,38 @@ +#pragma once + +#if defined(ARDUINO_ARCH_ESP32) + +#ifdef __cplusplus +extern "C" { +#endif + +#include "esp_err.h" + +typedef enum { + I2S_CHAN_STEREO, I2S_CHAN_RIGHT_TO_LEFT, I2S_CHAN_LEFT_TO_RIGHT, I2S_CHAN_RIGHT_ONLY, I2S_CHAN_LEFT_ONLY +} i2s_tx_chan_mod_t; + +typedef enum { + I2S_FIFO_16BIT_DUAL, I2S_FIFO_16BIT_SINGLE, I2S_FIFO_32BIT_DUAL, I2S_FIFO_32BIT_SINGLE +} i2s_tx_fifo_mod_t; + +void i2sInit(uint8_t bus_num, uint32_t bits_per_sample, uint32_t sample_rate, i2s_tx_chan_mod_t chan_mod, i2s_tx_fifo_mod_t fifo_mod, size_t dma_count, size_t dma_len); + +void i2sSetPins(uint8_t bus_num, int8_t out, int8_t ws, int8_t bck, int8_t in); +void i2sSetDac(uint8_t bus_num, bool right, bool left); + +esp_err_t i2sSetClock(uint8_t bus_num, uint8_t div_num, uint8_t div_b, uint8_t div_a, uint8_t bck, uint8_t bits_per_sample); +esp_err_t i2sSetSampleRate(uint8_t bus_num, uint32_t sample_rate, uint8_t bits_per_sample); + +void i2sSetTxDataMode(uint8_t bus_num, i2s_tx_chan_mod_t chan_mod, i2s_tx_fifo_mod_t fifo_mod); + +void i2sSetSilenceBuf(uint8_t bus_num, uint8_t* data, size_t len); + +size_t i2sWrite(uint8_t bus_num, uint8_t* data, size_t len, bool copy, bool free_when_sent); +bool i2sWriteDone(uint8_t bus_num); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.cpp new file mode 100644 index 0000000..e47912f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.cpp @@ -0,0 +1,68 @@ +/*------------------------------------------------------------------------- +HsbColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "RgbColor.h" +#include "HsbColor.h" + + +HsbColor::HsbColor(const RgbColor& color) +{ + // convert colors to float between (0.0 - 1.0) + float r = color.R / 255.0f; + float g = color.G / 255.0f; + float b = color.B / 255.0f; + + float max = (r > g && r > b) ? r : (g > b) ? g : b; + float min = (r < g && r < b) ? r : (g < b) ? g : b; + + float d = max - min; + + float h = 0.0; + float v = max; + float s = (v == 0.0f) ? 0 : (d / v); + + if (d != 0.0f) + { + if (r == max) + { + h = (g - b) / d + (g < b ? 6.0f : 0.0f); + } + else if (g == max) + { + h = (b - r) / d + 2.0f; + } + else + { + h = (r - g) / d + 4.0f; + } + h /= 6.0f; + } + + + H = h; + S = s; + B = v; +} diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.h b/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.h new file mode 100644 index 0000000..4d0bdfa --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HsbColor.h @@ -0,0 +1,113 @@ + +/*------------------------------------------------------------------------- +HsbColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +// ------------------------------------------------------------------------ +// HsbColor represents a color object that is represented by Hue, Saturation, Brightness +// component values. It contains helpful color routines to manipulate the +// color. +// ------------------------------------------------------------------------ +struct HsbColor +{ + // ------------------------------------------------------------------------ + // Construct a HsbColor using H, S, B values (0.0 - 1.0) + // ------------------------------------------------------------------------ + HsbColor(float h, float s, float b) : + H(h), S(s), B(b) + { + }; + + // ------------------------------------------------------------------------ + // Construct a HsbColor using RgbColor + // ------------------------------------------------------------------------ + HsbColor(const RgbColor& color); + + // ------------------------------------------------------------------------ + // Construct a HsbColor that will have its values set in latter operations + // CAUTION: The H,S,B members are not initialized and may not be consistent + // ------------------------------------------------------------------------ + HsbColor() + { + }; + + // ------------------------------------------------------------------------ + // LinearBlend between two colors by the amount defined by progress variable + // left - the color to start the blend at + // right - the color to end the blend at + // progress - (0.0 - 1.0) value where 0.0 will return left and 1.0 will return right + // and a value between will blend the color weighted linearly between them + // ------------------------------------------------------------------------ + template static HsbColor LinearBlend(const HsbColor& left, + const HsbColor& right, + float progress) + { + return HsbColor(T_NEOHUEBLEND::HueBlend(left.H, right.H, progress), + left.S + ((right.S - left.S) * progress), + left.B + ((right.B - left.B) * progress)); + } + + // ------------------------------------------------------------------------ + // BilinearBlend between four colors by the amount defined by 2d variable + // c00 - upper left quadrant color + // c01 - upper right quadrant color + // c10 - lower left quadrant color + // c11 - lower right quadrant color + // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space + // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space + // ------------------------------------------------------------------------ + template static HsbColor BilinearBlend(const HsbColor& c00, + const HsbColor& c01, + const HsbColor& c10, + const HsbColor& c11, + float x, + float y) + { + float v00 = (1.0f - x) * (1.0f - y); + float v10 = x * (1.0f - y); + float v01 = (1.0f - x) * y; + float v11 = x * y; + + return HsbColor( + T_NEOHUEBLEND::HueBlend( + T_NEOHUEBLEND::HueBlend(c00.H, c10.H, x), + T_NEOHUEBLEND::HueBlend(c01.H, c11.H, x), + y), + c00.S * v00 + c10.S * v10 + c01.S * v01 + c11.S * v11, + c00.B * v00 + c10.B * v10 + c01.B * v01 + c11.B * v11); + }; + + // ------------------------------------------------------------------------ + // Hue, Saturation, Brightness color members + // ------------------------------------------------------------------------ + + float H; + float S; + float B; +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.cpp new file mode 100644 index 0000000..ce9b238 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.cpp @@ -0,0 +1,72 @@ +/*------------------------------------------------------------------------- +HslColor provides a color object that can be directly consumed by NeoPixelBus + + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "RgbColor.h" +#include "HslColor.h" + + +HslColor::HslColor(const RgbColor& color) +{ + // convert colors to float between (0.0 - 1.0) + float r = color.R / 255.0f; + float g = color.G / 255.0f; + float b = color.B / 255.0f; + + float max = (r > g && r > b) ? r : (g > b) ? g : b; + float min = (r < g && r < b) ? r : (g < b) ? g : b; + + float h, s, l; + l = (max + min) / 2.0f; + + if (max == min) + { + h = s = 0.0f; + } + else + { + float d = max - min; + s = (l > 0.5f) ? d / (2.0f - (max + min)) : d / (max + min); + + if (r > g && r > b) + { + h = (g - b) / d + (g < b ? 6.0f : 0.0f); + } + else if (g > b) + { + h = (b - r) / d + 2.0f; + } + else + { + h = (r - g) / d + 4.0f; + } + h /= 6.0f; + } + + H = h; + S = s; + L = l; +} diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.h b/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.h new file mode 100644 index 0000000..f6988bd --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HslColor.h @@ -0,0 +1,113 @@ +/*------------------------------------------------------------------------- +HslColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +// ------------------------------------------------------------------------ +// HslColor represents a color object that is represented by Hue, Saturation, Lightness +// component values. It contains helpful color routines to manipulate the +// color. +// ------------------------------------------------------------------------ +struct HslColor +{ + + // ------------------------------------------------------------------------ + // Construct a HslColor using H, S, L values (0.0 - 1.0) + // L should be limited to between (0.0 - 0.5) + // ------------------------------------------------------------------------ + HslColor(float h, float s, float l) : + H(h), S(s), L(l) + { + }; + + // ------------------------------------------------------------------------ + // Construct a HslColor using RgbColor + // ------------------------------------------------------------------------ + HslColor(const RgbColor& color); + + // ------------------------------------------------------------------------ + // Construct a HslColor that will have its values set in latter operations + // CAUTION: The H,S,L members are not initialized and may not be consistent + // ------------------------------------------------------------------------ + HslColor() + { + }; + + // ------------------------------------------------------------------------ + // LinearBlend between two colors by the amount defined by progress variable + // left - the color to start the blend at + // right - the color to end the blend at + // progress - (0.0 - 1.0) value where 0.0 will return left and 1.0 will return right + // and a value between will blend the color weighted linearly between them + // ------------------------------------------------------------------------ + template static HslColor LinearBlend(const HslColor& left, + const HslColor& right, + float progress) + { + return HslColor(T_NEOHUEBLEND::HueBlend(left.H, right.H, progress), + left.S + ((right.S - left.S) * progress), + left.L + ((right.L - left.L) * progress)); + }; + + // ------------------------------------------------------------------------ + // BilinearBlend between four colors by the amount defined by 2d variable + // c00 - upper left quadrant color + // c01 - upper right quadrant color + // c10 - lower left quadrant color + // c11 - lower right quadrant color + // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space + // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space + // ------------------------------------------------------------------------ + template static HslColor BilinearBlend(const HslColor& c00, + const HslColor& c01, + const HslColor& c10, + const HslColor& c11, + float x, + float y) + { + float v00 = (1.0f - x) * (1.0f - y); + float v10 = x * (1.0f - y); + float v01 = (1.0f - x) * y; + float v11 = x * y; + + return HslColor( + T_NEOHUEBLEND::HueBlend( + T_NEOHUEBLEND::HueBlend(c00.H, c10.H, x), + T_NEOHUEBLEND::HueBlend(c01.H, c11.H, x), + y), + c00.S * v00 + c10.S * v10 + c01.S * v01 + c11.S * v11, + c00.L * v00 + c10.L * v10 + c01.L * v01 + c11.L * v11); + }; + + // ------------------------------------------------------------------------ + // Hue, Saturation, Lightness color members + // ------------------------------------------------------------------------ + float H; + float S; + float L; +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.cpp new file mode 100644 index 0000000..018d58f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.cpp @@ -0,0 +1,58 @@ +/*------------------------------------------------------------------------- +This file contains the HtmlColor implementation + +Written by Unai Uribarri + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#include "HtmlColor.h" + +static inline char hexdigit(uint8_t v) +{ + return v + (v < 10 ? '0' : 'a' - 10); +} + + +size_t HtmlColor::ToNumericalString(char* buf, size_t bufSize) const +{ + size_t bufLen = bufSize - 1; + + if (bufLen-- > 0) + { + if (bufLen > 0) + { + buf[0] = '#'; + } + + uint32_t color = Color; + for (uint8_t indexDigit = 6; indexDigit > 0; indexDigit--) + { + if (bufLen > indexDigit) + { + buf[indexDigit] = hexdigit(color & 0x0000000f); + } + color >>= 4; + } + + buf[(bufLen < 7 ? bufLen : 7)] = 0; + } + return 7; +} diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.h b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.h new file mode 100644 index 0000000..a40d622 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColor.h @@ -0,0 +1,340 @@ +/*------------------------------------------------------------------------- +HtmlColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include "RgbColor.h" + +#define MAX_HTML_COLOR_NAME_LEN 21 + +#ifndef pgm_read_ptr +// ESP8266 doesn't define this macro +#define pgm_read_ptr(addr) (*reinterpret_cast(addr)) +#endif + +#ifndef countof +#define countof(array) (sizeof(array)/sizeof(array[0])) +#endif + +// ------------------------------------------------------------------------ +// HtmlColorPair represents an association between a name and a HTML color code +// ------------------------------------------------------------------------ +struct HtmlColorPair +{ + PGM_P Name; + uint32_t Color; +}; + +// ------------------------------------------------------------------------ +// HtmlShortColorNames is a template class used for Parse and ToString +// ------------------------------------------------------------------------ +class HtmlShortColorNames +{ +public: + static const HtmlColorPair* Pair(uint8_t index); + static uint8_t Count(); +}; + +// ------------------------------------------------------------------------ +// HtmlColorNames is a template class used for Parse and ToString +// ------------------------------------------------------------------------ +class HtmlColorNames +{ +public: + static const HtmlColorPair* Pair(uint8_t index); + static uint8_t Count(); +}; + +// ------------------------------------------------------------------------ +// HtmlColor represents a color object that is represented by a single uint32 +// value. It contains minimal routines and used primarily as a helper to +// initialize other color objects +// ------------------------------------------------------------------------ +struct HtmlColor +{ + // ------------------------------------------------------------------------ + // Construct a HtmlColor using a single value (0-0xffffff) + // This works well for hexidecimal color definitions + // 0xff0000 = red, 0x00ff00 = green, and 0x0000ff = blue + // ------------------------------------------------------------------------ + HtmlColor(uint32_t color) : + Color(color) + { + }; + + // ------------------------------------------------------------------------ + // Construct a HtmlColor using RgbColor + // ------------------------------------------------------------------------ + HtmlColor(const RgbColor& color) + { + Color = (uint32_t)color.R << 16 | (uint32_t)color.G << 8 | (uint32_t)color.B; + } + + // ------------------------------------------------------------------------ + // Construct a HtmlColor that will have its values set in latter operations + // CAUTION: The Color member is not initialized and may not be consistent + // ------------------------------------------------------------------------ + HtmlColor() + { + }; + + // ------------------------------------------------------------------------ + // Comparison operators + // ------------------------------------------------------------------------ + bool operator==(const HtmlColor& other) const + { + return (Color == other.Color); + }; + + bool operator!=(const HtmlColor& other) const + { + return !(*this == other); + }; + + // ------------------------------------------------------------------------ + // Parse a HTML4/CSS3 color name + // T_HTMLCOLORNAMES - template class that defines the collection of names + // name - the color name + // nameSize - the max size of name to check + // + // returns - zero if failed, or the number of chars parsed + // + // It will stop parsing name when a null terminator is reached, + // nameSize is reached, no match is found in the name/color pair table, or + // a non-alphanumeric is read like seperators or whitespace. + // + // It also accepts 3 or 6 digit hexadecimal notation (#rgb or #rrggbb), + // but it doesn't accept RGB, RGBA nor HSL values. + // + // See https://www.w3.org/TR/css3-color/#SRGB + // + // name must point to the first non-whitespace character to be parsed + // parsing will stop at the first non-alpha numeric + // + // Name MUST NOT be a PROGMEM pointer + // + // examples: + // Parse(buff, buffSize); + // Parse(buff, buffSize); + // ------------------------------------------------------------------------ + + template size_t Parse(const char* name, size_t nameSize) + { + if (nameSize > 0) + { + if (name[0] == '#') + { + // Parse an hexadecimal notation "#rrbbgg" or "#rgb" + // + uint8_t temp[6]; // stores preconverted chars to hex values + uint8_t tempSize = 0; + + for (uint8_t indexChar = 1; indexChar < nameSize && indexChar < 8; ++indexChar) + { + char c = name[indexChar]; + if (c >= '0' && c <= '9') + { + c -= '0'; + } + else + { + // Convert a letter to lower case (only for ASCII letters) + // It's faster & smaller than tolower() + c |= 32; + if (c >= 'a' && c <= 'f') + { + c = c - 'a' + 10; + } + else + { + // we found an non hexidecimal char + // which could be null, deliminator, or other spacing + break; + } + } + + temp[tempSize] = c; + tempSize++; + } + + if (tempSize != 3 && tempSize != 6) + { + // invalid count of numerical chars + return 0; + } + else + { + uint32_t color = 0; + for (uint8_t indexChar = 0; indexChar < tempSize; ++indexChar) + { + color = color * 16 + temp[indexChar]; + if (tempSize == 3) + { + // 3 digit hexadecimal notation can be supported easily + // duplicating digits. + color = color * 16 + temp[indexChar]; + } + } + + Color = color; + return tempSize; + } + } + else + { + // parse a standard name for the color + // + + // the normal list is small enough a binary search isn't interesting, + for (uint8_t indexName = 0; indexName < T_HTMLCOLORNAMES::Count(); ++indexName) + { + const HtmlColorPair* colorPair = T_HTMLCOLORNAMES::Pair(indexName); + PGM_P searchName = (PGM_P)pgm_read_ptr(&colorPair->Name); + size_t str1Size = nameSize; + const char* str1 = name; + const char* str2P = searchName; + + uint16_t result; + + while (str1Size > 0) + { + char ch1 = tolower(*str1++); + char ch2 = tolower(pgm_read_byte(str2P++)); + result = ch1 - ch2; + if (result != 0 || ch2 == '\0') + { + if (ch2 == '\0' && !isalnum(ch1)) + { + // the string continues but is not part of a + // valid color name, + // ends in white space, deliminator, etc + result = 0; + } + break; + } + result = -1; // have not reached the end of searchName; + str1Size--; + } + + if (result == 0) + { + Color = pgm_read_dword(&colorPair->Color); + return nameSize - str1Size; + } + } + } + } + + return 0; + } + + template size_t Parse(const char* name) + { + return Parse(name, MAX_HTML_COLOR_NAME_LEN + 1); + } + + template size_t Parse(String const &name) + { + return Parse(name.c_str(), name.length() + 1); + } + + // ------------------------------------------------------------------------ + // Converts this color code to its HTML4/CSS3 name + // T_HTMLCOLORNAMES - template class that defines the collection of names + // buf - buffer to write the string + // bufSize - actual size of buf array + // + // It returns the number of chars required not including the NUL terminator. + // + // If there is not enough space in the buffer, it will write as many + // characters as allowed and will always finish the buffer with a NUL char + // + // examples: + // ToString(buf, bufSize); + // ToString(buf, bufSize); + // ------------------------------------------------------------------------ + + template size_t ToString(char* buf, size_t bufSize) const + { + // search for a color value/name pairs first + for (uint8_t indexName = 0; indexName < T_HTMLCOLORNAMES::Count(); ++indexName) + { + const HtmlColorPair* colorPair = T_HTMLCOLORNAMES::Pair(indexName); + if (pgm_read_dword(&colorPair->Color) == Color) + { + PGM_P name = (PGM_P)pgm_read_ptr(&colorPair->Name); + strncpy_P(buf, name, bufSize); + return strlen_P(name); + } + } + + // no color name pair match, convert using numerical format + return ToNumericalString(buf, bufSize); + } + + // ------------------------------------------------------------------------ + // Converts this color code to its HTML4/CSS3 numerical name + // + // buf - buffer to write the string + // bufSize - actual size of buf array + // + // It returns the number of chars required not including the NUL terminator. + // + // If there is not enough space in the buffer, it will write as many + // characters as allowed and will always finish the buffer with a NUL char + // ------------------------------------------------------------------------ + + size_t ToNumericalString(char* buf, size_t bufSize) const; + + // ------------------------------------------------------------------------ + // BilinearBlend between four colors by the amount defined by 2d variable + // c00 - upper left quadrant color + // c01 - upper right quadrant color + // c10 - lower left quadrant color + // c11 - lower right quadrant color + // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space + // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space + // ------------------------------------------------------------------------ + + static HtmlColor BilinearBlend(const HtmlColor& c00, + const HtmlColor& c01, + const HtmlColor& c10, + const HtmlColor& c11, + float x, + float y) + { + return RgbColor::BilinearBlend(c00, c01, c10, c11, x, y); + } + + // ------------------------------------------------------------------------ + // Color member (0-0xffffff) where + // 0xff0000 is red + // 0x00ff00 is green + // 0x0000ff is blue + // ------------------------------------------------------------------------ + uint32_t Color; +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.cpp new file mode 100644 index 0000000..87dda4b --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.cpp @@ -0,0 +1,178 @@ +/*------------------------------------------------------------------------- +HtmlColorNameStrings provides the implemenation of the color string names + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "HtmlColorNameStrings.h" + +/* HTML4 color names */ +const char c_HtmlNameAqua[] PROGMEM = "aqua"; +const char c_HtmlNameBlack[] PROGMEM = "black"; +const char c_HtmlNameBlue[] PROGMEM = "blue"; +const char c_HtmlNameFuchsia[] PROGMEM = "fuchsia"; +const char c_HtmlNameGray[] PROGMEM = "gray"; +const char c_HtmlNameGreen[] PROGMEM = "green"; +const char c_HtmlNameLime[] PROGMEM = "lime"; +const char c_HtmlNameMaroon[] PROGMEM = "maroon"; +const char c_HtmlNameNavy[] PROGMEM = "navy"; +const char c_HtmlNameOlive[] PROGMEM = "olive"; +const char c_HtmlNameOrange[] PROGMEM = "orange"; +const char c_HtmlNamePurple[] PROGMEM = "purple"; +const char c_HtmlNameRed[] PROGMEM = "red"; +const char c_HtmlNameSilver[] PROGMEM = "silver"; +const char c_HtmlNameTeal[] PROGMEM = "teal"; +const char c_HtmlNameWhite[] PROGMEM = "white"; +const char c_HtmlNameYellow[] PROGMEM = "yellow"; + +/* Additional CSS3 color names */ +const char c_HtmlNameAliceBlue[] PROGMEM = "aliceblue"; +const char c_HtmlNameAntiqueWhite[] PROGMEM = "antiquewhite"; +const char c_HtmlNameAquamarine[] PROGMEM = "aquamarine"; +const char c_HtmlNameAzure[] PROGMEM = "azure"; +const char c_HtmlNameBeige[] PROGMEM = "beige"; +const char c_HtmlNameBisque[] PROGMEM = "bisque"; +const char c_HtmlNameBlanchedAlmond[] PROGMEM = "blanchedalmond"; +const char c_HtmlNameBlueViolet[] PROGMEM = "blueviolet"; +const char c_HtmlNameBrown[] PROGMEM = "brown"; +const char c_HtmlNameBurlyWood[] PROGMEM = "burlywood"; +const char c_HtmlNameCadetBlue[] PROGMEM = "cadetblue"; +const char c_HtmlNameChartreuse[] PROGMEM = "chartreuse"; +const char c_HtmlNameChocolate[] PROGMEM = "chocolate"; +const char c_HtmlNameCoral[] PROGMEM = "coral"; +const char c_HtmlNameCornflowerBlue[] PROGMEM = "cornflowerblue"; +const char c_HtmlNameCornsilk[] PROGMEM = "cornsilk"; +const char c_HtmlNameCrimson[] PROGMEM = "crimson"; +const char c_HtmlNameCyan[] PROGMEM = "cyan"; +const char c_HtmlNameDarkBlue[] PROGMEM = "darkblue"; +const char c_HtmlNameDarkCyan[] PROGMEM = "darkcyan"; +const char c_HtmlNameDarkGoldenrod[] PROGMEM = "darkgoldenrod"; +const char c_HtmlNameDarkGray[] PROGMEM = "darkgray"; +const char c_HtmlNameDarkGreen[] PROGMEM = "darkgreen"; +const char c_HtmlNameDarkGrey[] PROGMEM = "darkgrey"; +const char c_HtmlNameDarkKhaki[] PROGMEM = "darkkhaki"; +const char c_HtmlNameDarkMagenta[] PROGMEM = "darkmagenta"; +const char c_HtmlNameDarkOliveGreen[] PROGMEM = "darkolivegreen"; +const char c_HtmlNameDarkOrange[] PROGMEM = "darkorange"; +const char c_HtmlNameDarkOrchid[] PROGMEM = "darkorchid"; +const char c_HtmlNameDarkRed[] PROGMEM = "darkred"; +const char c_HtmlNameDarkSalmon[] PROGMEM = "darksalmon"; +const char c_HtmlNameDarkSeaGreen[] PROGMEM = "darkseagreen"; +const char c_HtmlNameDarkSlateBlue[] PROGMEM = "darkslateblue"; +const char c_HtmlNameDarkSlateGray[] PROGMEM = "darkslategray"; +const char c_HtmlNameDarkSlateGrey[] PROGMEM = "darkslategrey"; +const char c_HtmlNameDarkTurquoise[] PROGMEM = "darkturquoise"; +const char c_HtmlNameDarkViolet[] PROGMEM = "darkviolet"; +const char c_HtmlNameDeepPink[] PROGMEM = "deeppink"; +const char c_HtmlNameDeepSkyBlue[] PROGMEM = "deepskyblue"; +const char c_HtmlNameDimGray[] PROGMEM = "dimgray"; +const char c_HtmlNameDimGrey[] PROGMEM = "dimgrey"; +const char c_HtmlNameDodgerBlue[] PROGMEM = "dodgerblue"; +const char c_HtmlNameFirebrick[] PROGMEM = "firebrick"; +const char c_HtmlNameFloralWhite[] PROGMEM = "floralwhite"; +const char c_HtmlNameForestGreen[] PROGMEM = "forestgreen"; +const char c_HtmlNameGainsboro[] PROGMEM = "gainsboro"; +const char c_HtmlNameGhostWhite[] PROGMEM = "ghostwhite"; +const char c_HtmlNameGold[] PROGMEM = "gold"; +const char c_HtmlNameGoldenrod[] PROGMEM = "goldenrod"; +const char c_HtmlNameGreenYellow[] PROGMEM = "greenyellow"; +const char c_HtmlNameGrey[] PROGMEM = "grey"; +const char c_HtmlNameHoneydew[] PROGMEM = "honeydew"; +const char c_HtmlNameHotPink[] PROGMEM = "hotpink"; +const char c_HtmlNameIndianRed[] PROGMEM = "indianred"; +const char c_HtmlNameIndigo[] PROGMEM = "indigo"; +const char c_HtmlNameIvory[] PROGMEM = "ivory"; +const char c_HtmlNameKhaki[] PROGMEM = "khaki"; +const char c_HtmlNameLavender[] PROGMEM = "lavender"; +const char c_HtmlNameLavenderBlush[] PROGMEM = "lavenderblush"; +const char c_HtmlNameLawnGreen[] PROGMEM = "lawngreen"; +const char c_HtmlNameLemonChiffon[] PROGMEM = "lemonchiffon"; +const char c_HtmlNameLightBlue[] PROGMEM = "lightblue"; +const char c_HtmlNameLightCoral[] PROGMEM = "lightcoral"; +const char c_HtmlNameLightCyan[] PROGMEM = "lightcyan"; +const char c_HtmlNameLightGoldenrodYellow[] PROGMEM = "lightgoldenrodyellow"; +const char c_HtmlNameLightGray[] PROGMEM = "lightgray"; +const char c_HtmlNameLightGreen[] PROGMEM = "lightgreen"; +const char c_HtmlNameLightGrey[] PROGMEM = "lightgrey"; +const char c_HtmlNameLightPink[] PROGMEM = "lightpink"; +const char c_HtmlNameLightSalmon[] PROGMEM = "lightsalmon"; +const char c_HtmlNameLightSeaGreen[] PROGMEM = "lightseagreen"; +const char c_HtmlNameLightSkyBlue[] PROGMEM = "lightskyblue"; +const char c_HtmlNameLightSlateGray[] PROGMEM = "lightslategray"; +const char c_HtmlNameLightSlateGrey[] PROGMEM = "lightslategrey"; +const char c_HtmlNameLightSteelBlue[] PROGMEM = "lightsteelblue"; +const char c_HtmlNameLightYellow[] PROGMEM = "lightyellow"; +const char c_HtmlNameLimeGreen[] PROGMEM = "limegreen"; +const char c_HtmlNameLinen[] PROGMEM = "linen"; +const char c_HtmlNameMagenta[] PROGMEM = "magenta"; +const char c_HtmlNameMediumAquamarine[] PROGMEM = "mediumaquamarine"; +const char c_HtmlNameMediumBlue[] PROGMEM = "mediumblue"; +const char c_HtmlNameMediumOrchid[] PROGMEM = "mediumorchid"; +const char c_HtmlNameMediumPurple[] PROGMEM = "mediumpurple"; +const char c_HtmlNameMediumSeagreen[] PROGMEM = "mediumseagreen"; +const char c_HtmlNameMediumSlateBlue[] PROGMEM = "mediumslateblue"; +const char c_HtmlNameMediumSpringGreen[] PROGMEM = "mediumspringgreen"; +const char c_HtmlNameMediumTurquoise[] PROGMEM = "mediumturquoise"; +const char c_HtmlNameMediumVioletRed[] PROGMEM = "mediumvioletred"; +const char c_HtmlNameMidnightBlue[] PROGMEM = "midnightblue"; +const char c_HtmlNameMintCream[] PROGMEM = "mintcream"; +const char c_HtmlNameMistyRose[] PROGMEM = "mistyrose"; +const char c_HtmlNameMoccasin[] PROGMEM = "moccasin"; +const char c_HtmlNameNavajoWhite[] PROGMEM = "navajowhite"; +const char c_HtmlNameOldLace[] PROGMEM = "oldlace"; +const char c_HtmlNameOliveDrab[] PROGMEM = "olivedrab"; +const char c_HtmlNameOrangeRed[] PROGMEM = "orangered"; +const char c_HtmlNameOrchid[] PROGMEM = "orchid"; +const char c_HtmlNamePaleGoldenrod[] PROGMEM = "palegoldenrod"; +const char c_HtmlNamePaleGreen[] PROGMEM = "palegreen"; +const char c_HtmlNamePaleTurquoise[] PROGMEM = "paleturquoise"; +const char c_HtmlNamePaleVioletRed[] PROGMEM = "palevioletred"; +const char c_HtmlNamePapayaWhip[] PROGMEM = "papayawhip"; +const char c_HtmlNamePeachPuff[] PROGMEM = "peachpuff"; +const char c_HtmlNamePeru[] PROGMEM = "peru"; +const char c_HtmlNamePink[] PROGMEM = "pink"; +const char c_HtmlNamePlum[] PROGMEM = "plum"; +const char c_HtmlNamePowderBlue[] PROGMEM = "powderblue"; +const char c_HtmlNameRosyBrown[] PROGMEM = "rosybrown"; +const char c_HtmlNameRoyalBlue[] PROGMEM = "royalblue"; +const char c_HtmlNameSaddleBrown[] PROGMEM = "saddlebrown"; +const char c_HtmlNameSalmon[] PROGMEM = "salmon"; +const char c_HtmlNameSandyBrown[] PROGMEM = "sandybrown"; +const char c_HtmlNameSeaGreen[] PROGMEM = "seagreen"; +const char c_HtmlNameSeaShell[] PROGMEM = "seashell"; +const char c_HtmlNameSienna[] PROGMEM = "sienna"; +const char c_HtmlNameSkyBlue[] PROGMEM = "skyblue"; +const char c_HtmlNameSlateBlue[] PROGMEM = "slateblue"; +const char c_HtmlNameSlateGray[] PROGMEM = "slategray"; +const char c_HtmlNameSlateGrey[] PROGMEM = "slategrey"; +const char c_HtmlNameSnow[] PROGMEM = "snow"; +const char c_HtmlNameSpringGreen[] PROGMEM = "springgreen"; +const char c_HtmlNameSteelBlue[] PROGMEM = "steelblue"; +const char c_HtmlNameTan[] PROGMEM = "tan"; +const char c_HtmlNameThistle[] PROGMEM = "thistle"; +const char c_HtmlNameTomato[] PROGMEM = "tomato"; +const char c_HtmlNameTurquoise[] PROGMEM = "turquoise"; +const char c_HtmlNameViolet[] PROGMEM = "violet"; +const char c_HtmlNameWheat[] PROGMEM = "wheat"; +const char c_HtmlNameWhiteSmoke[] PROGMEM = "whitesmoke"; +const char c_HtmlNameYellowGreen[] PROGMEM = "yellowgreen"; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.h b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.h new file mode 100644 index 0000000..86ffcf4 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNameStrings.h @@ -0,0 +1,180 @@ +/*------------------------------------------------------------------------- +HtmlColorNameStrings provides the declaration of the color string names + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#include + +/* HTML4 color names */ +extern const char c_HtmlNameAqua[] PROGMEM; +extern const char c_HtmlNameBlack[] PROGMEM; +extern const char c_HtmlNameBlue[] PROGMEM; +extern const char c_HtmlNameFuchsia[] PROGMEM; +extern const char c_HtmlNameGray[] PROGMEM; +extern const char c_HtmlNameGreen[] PROGMEM; +extern const char c_HtmlNameLime[] PROGMEM; +extern const char c_HtmlNameMaroon[] PROGMEM; +extern const char c_HtmlNameNavy[] PROGMEM; +extern const char c_HtmlNameOlive[] PROGMEM; +extern const char c_HtmlNameOrange[] PROGMEM; +extern const char c_HtmlNamePurple[] PROGMEM; +extern const char c_HtmlNameRed[] PROGMEM; +extern const char c_HtmlNameSilver[] PROGMEM; +extern const char c_HtmlNameTeal[] PROGMEM; +extern const char c_HtmlNameWhite[] PROGMEM; +extern const char c_HtmlNameYellow[] PROGMEM; + +/* Additional CSS3 color names */ +extern const char c_HtmlNameAliceBlue[] PROGMEM; +extern const char c_HtmlNameAntiqueWhite[] PROGMEM; +extern const char c_HtmlNameAquamarine[] PROGMEM; +extern const char c_HtmlNameAzure[] PROGMEM; +extern const char c_HtmlNameBeige[] PROGMEM; +extern const char c_HtmlNameBisque[] PROGMEM; +extern const char c_HtmlNameBlanchedAlmond[] PROGMEM; +extern const char c_HtmlNameBlueViolet[] PROGMEM; +extern const char c_HtmlNameBrown[] PROGMEM; +extern const char c_HtmlNameBurlyWood[] PROGMEM; +extern const char c_HtmlNameCadetBlue[] PROGMEM; +extern const char c_HtmlNameChartreuse[] PROGMEM; +extern const char c_HtmlNameChocolate[] PROGMEM; +extern const char c_HtmlNameCoral[] PROGMEM; +extern const char c_HtmlNameCornflowerBlue[] PROGMEM; +extern const char c_HtmlNameCornsilk[] PROGMEM; +extern const char c_HtmlNameCrimson[] PROGMEM; +extern const char c_HtmlNameCyan[] PROGMEM; +extern const char c_HtmlNameDarkBlue[] PROGMEM; +extern const char c_HtmlNameDarkCyan[] PROGMEM; +extern const char c_HtmlNameDarkGoldenrod[] PROGMEM; +extern const char c_HtmlNameDarkGray[] PROGMEM; +extern const char c_HtmlNameDarkGreen[] PROGMEM; +extern const char c_HtmlNameDarkGrey[] PROGMEM; +extern const char c_HtmlNameDarkKhaki[] PROGMEM; +extern const char c_HtmlNameDarkMagenta[] PROGMEM; +extern const char c_HtmlNameDarkOliveGreen[] PROGMEM; +extern const char c_HtmlNameDarkOrange[] PROGMEM; +extern const char c_HtmlNameDarkOrchid[] PROGMEM; +extern const char c_HtmlNameDarkRed[] PROGMEM; +extern const char c_HtmlNameDarkSalmon[] PROGMEM; +extern const char c_HtmlNameDarkSeaGreen[] PROGMEM; +extern const char c_HtmlNameDarkSlateBlue[] PROGMEM; +extern const char c_HtmlNameDarkSlateGray[] PROGMEM; +extern const char c_HtmlNameDarkSlateGrey[] PROGMEM; +extern const char c_HtmlNameDarkTurquoise[] PROGMEM; +extern const char c_HtmlNameDarkViolet[] PROGMEM; +extern const char c_HtmlNameDeepPink[] PROGMEM; +extern const char c_HtmlNameDeepSkyBlue[] PROGMEM; +extern const char c_HtmlNameDimGray[] PROGMEM; +extern const char c_HtmlNameDimGrey[] PROGMEM; +extern const char c_HtmlNameDodgerBlue[] PROGMEM; +extern const char c_HtmlNameFirebrick[] PROGMEM; +extern const char c_HtmlNameFloralWhite[] PROGMEM; +extern const char c_HtmlNameForestGreen[] PROGMEM; +extern const char c_HtmlNameGainsboro[] PROGMEM; +extern const char c_HtmlNameGhostWhite[] PROGMEM; +extern const char c_HtmlNameGold[] PROGMEM; +extern const char c_HtmlNameGoldenrod[] PROGMEM; +extern const char c_HtmlNameGreenYellow[] PROGMEM; +extern const char c_HtmlNameGrey[] PROGMEM; +extern const char c_HtmlNameHoneydew[] PROGMEM; +extern const char c_HtmlNameHotPink[] PROGMEM; +extern const char c_HtmlNameIndianRed[] PROGMEM; +extern const char c_HtmlNameIndigo[] PROGMEM; +extern const char c_HtmlNameIvory[] PROGMEM; +extern const char c_HtmlNameKhaki[] PROGMEM; +extern const char c_HtmlNameLavender[] PROGMEM; +extern const char c_HtmlNameLavenderBlush[] PROGMEM; +extern const char c_HtmlNameLawnGreen[] PROGMEM; +extern const char c_HtmlNameLemonChiffon[] PROGMEM; +extern const char c_HtmlNameLightBlue[] PROGMEM; +extern const char c_HtmlNameLightCoral[] PROGMEM; +extern const char c_HtmlNameLightCyan[] PROGMEM; +extern const char c_HtmlNameLightGoldenrodYellow[] PROGMEM; +extern const char c_HtmlNameLightGray[] PROGMEM; +extern const char c_HtmlNameLightGreen[] PROGMEM; +extern const char c_HtmlNameLightGrey[] PROGMEM; +extern const char c_HtmlNameLightPink[] PROGMEM; +extern const char c_HtmlNameLightSalmon[] PROGMEM; +extern const char c_HtmlNameLightSeaGreen[] PROGMEM; +extern const char c_HtmlNameLightSkyBlue[] PROGMEM; +extern const char c_HtmlNameLightSlateGray[] PROGMEM; +extern const char c_HtmlNameLightSlateGrey[] PROGMEM; +extern const char c_HtmlNameLightSteelBlue[] PROGMEM; +extern const char c_HtmlNameLightYellow[] PROGMEM; +extern const char c_HtmlNameLimeGreen[] PROGMEM; +extern const char c_HtmlNameLinen[] PROGMEM; +extern const char c_HtmlNameMagenta[] PROGMEM; +extern const char c_HtmlNameMediumAquamarine[] PROGMEM; +extern const char c_HtmlNameMediumBlue[] PROGMEM; +extern const char c_HtmlNameMediumOrchid[] PROGMEM; +extern const char c_HtmlNameMediumPurple[] PROGMEM; +extern const char c_HtmlNameMediumSeagreen[] PROGMEM; +extern const char c_HtmlNameMediumSlateBlue[] PROGMEM; +extern const char c_HtmlNameMediumSpringGreen[] PROGMEM; +extern const char c_HtmlNameMediumTurquoise[] PROGMEM; +extern const char c_HtmlNameMediumVioletRed[] PROGMEM; +extern const char c_HtmlNameMidnightBlue[] PROGMEM; +extern const char c_HtmlNameMintCream[] PROGMEM; +extern const char c_HtmlNameMistyRose[] PROGMEM; +extern const char c_HtmlNameMoccasin[] PROGMEM; +extern const char c_HtmlNameNavajoWhite[] PROGMEM; +extern const char c_HtmlNameOldLace[] PROGMEM; +extern const char c_HtmlNameOliveDrab[] PROGMEM; +extern const char c_HtmlNameOrangeRed[] PROGMEM; +extern const char c_HtmlNameOrchid[] PROGMEM; +extern const char c_HtmlNamePaleGoldenrod[] PROGMEM; +extern const char c_HtmlNamePaleGreen[] PROGMEM; +extern const char c_HtmlNamePaleTurquoise[] PROGMEM; +extern const char c_HtmlNamePaleVioletRed[] PROGMEM; +extern const char c_HtmlNamePapayaWhip[] PROGMEM; +extern const char c_HtmlNamePeachPuff[] PROGMEM; +extern const char c_HtmlNamePeru[] PROGMEM; +extern const char c_HtmlNamePink[] PROGMEM; +extern const char c_HtmlNamePlum[] PROGMEM; +extern const char c_HtmlNamePowderBlue[] PROGMEM; +extern const char c_HtmlNameRosyBrown[] PROGMEM; +extern const char c_HtmlNameRoyalBlue[] PROGMEM; +extern const char c_HtmlNameSaddleBrown[] PROGMEM; +extern const char c_HtmlNameSalmon[] PROGMEM; +extern const char c_HtmlNameSandyBrown[] PROGMEM; +extern const char c_HtmlNameSeaGreen[] PROGMEM; +extern const char c_HtmlNameSeaShell[] PROGMEM; +extern const char c_HtmlNameSienna[] PROGMEM; +extern const char c_HtmlNameSkyBlue[] PROGMEM; +extern const char c_HtmlNameSlateBlue[] PROGMEM; +extern const char c_HtmlNameSlateGray[] PROGMEM; +extern const char c_HtmlNameSlateGrey[] PROGMEM; +extern const char c_HtmlNameSnow[] PROGMEM; +extern const char c_HtmlNameSpringGreen[] PROGMEM; +extern const char c_HtmlNameSteelBlue[] PROGMEM; +extern const char c_HtmlNameTan[] PROGMEM; +extern const char c_HtmlNameThistle[] PROGMEM; +extern const char c_HtmlNameTomato[] PROGMEM; +extern const char c_HtmlNameTurquoise[] PROGMEM; +extern const char c_HtmlNameViolet[] PROGMEM; +extern const char c_HtmlNameWheat[] PROGMEM; +extern const char c_HtmlNameWhiteSmoke[] PROGMEM; +extern const char c_HtmlNameYellowGreen[] PROGMEM; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNames.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNames.cpp new file mode 100644 index 0000000..b1098ea --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorNames.cpp @@ -0,0 +1,188 @@ +/*------------------------------------------------------------------------- +HtmlColorNames provides a template class for access to the full name table + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "HtmlColor.h" +#include "HtmlColorNameStrings.h" + +static const HtmlColorPair c_ColorNames[] PROGMEM = { + { c_HtmlNameAliceBlue, 0xf0f8ff }, + { c_HtmlNameAntiqueWhite, 0xfaebd7 }, + { c_HtmlNameAqua, 0xffff }, + { c_HtmlNameAquamarine, 0x7fffd4 }, + { c_HtmlNameAzure, 0xf0ffff }, + { c_HtmlNameBeige, 0xf5f5dc }, + { c_HtmlNameBisque, 0xffe4c4 }, + { c_HtmlNameBlack, 0x0 }, + { c_HtmlNameBlanchedAlmond, 0xffebcd }, + { c_HtmlNameBlue, 0xff }, + { c_HtmlNameBlueViolet, 0x8a2be2 }, + { c_HtmlNameBrown, 0xa52a2a }, + { c_HtmlNameBurlyWood, 0xdeb887 }, + { c_HtmlNameCadetBlue, 0x5f9ea0 }, + { c_HtmlNameChartreuse, 0x7fff00 }, + { c_HtmlNameChocolate, 0xd2691e }, + { c_HtmlNameCoral, 0xff7f50 }, + { c_HtmlNameCornflowerBlue, 0x6495ed }, + { c_HtmlNameCornsilk, 0xfff8dc }, + { c_HtmlNameCrimson, 0xdc143c }, + { c_HtmlNameCyan, 0xffff }, + { c_HtmlNameDarkBlue, 0x8b }, + { c_HtmlNameDarkCyan, 0x8b8b }, + { c_HtmlNameDarkGoldenrod, 0xb8860b }, + { c_HtmlNameDarkGray, 0xa9a9a9 }, + { c_HtmlNameDarkGreen, 0x6400 }, + { c_HtmlNameDarkGrey, 0xa9a9a9 }, + { c_HtmlNameDarkKhaki, 0xbdb76b }, + { c_HtmlNameDarkMagenta, 0x8b008b }, + { c_HtmlNameDarkOliveGreen, 0x556b2f }, + { c_HtmlNameDarkOrange, 0xff8c00 }, + { c_HtmlNameDarkOrchid, 0x9932cc }, + { c_HtmlNameDarkRed, 0x8b0000 }, + { c_HtmlNameDarkSalmon, 0xe9967a }, + { c_HtmlNameDarkSeaGreen, 0x8fbc8f }, + { c_HtmlNameDarkSlateBlue, 0x483d8b }, + { c_HtmlNameDarkSlateGray, 0x2f4f4f }, + { c_HtmlNameDarkSlateGrey, 0x2f4f4f }, + { c_HtmlNameDarkTurquoise, 0xced1 }, + { c_HtmlNameDarkViolet, 0x9400d3 }, + { c_HtmlNameDeepPink, 0xff1493 }, + { c_HtmlNameDeepSkyBlue, 0xbfff }, + { c_HtmlNameDimGray, 0x696969 }, + { c_HtmlNameDimGrey, 0x696969 }, + { c_HtmlNameDodgerBlue, 0x1e90ff }, + { c_HtmlNameFirebrick, 0xb22222 }, + { c_HtmlNameFloralWhite, 0xfffaf0 }, + { c_HtmlNameForestGreen, 0x228b22 }, + { c_HtmlNameFuchsia, 0xff00ff }, + { c_HtmlNameGainsboro, 0xdcdcdc }, + { c_HtmlNameGhostWhite, 0xf8f8ff }, + { c_HtmlNameGold, 0xffd700 }, + { c_HtmlNameGoldenrod, 0xdaa520 }, + { c_HtmlNameGray, 0x808080 }, + { c_HtmlNameGreen, 0x8000 }, + { c_HtmlNameGreenYellow, 0xadff2f }, + { c_HtmlNameGrey, 0x808080 }, + { c_HtmlNameHoneydew, 0xf0fff0 }, + { c_HtmlNameHotPink, 0xff69b4 }, + { c_HtmlNameIndianRed, 0xcd5c5c }, + { c_HtmlNameIndigo, 0x4b0082 }, + { c_HtmlNameIvory, 0xfffff0 }, + { c_HtmlNameKhaki, 0xf0e68c }, + { c_HtmlNameLavender, 0xe6e6fa }, + { c_HtmlNameLavenderBlush, 0xfff0f5 }, + { c_HtmlNameLawnGreen, 0x7cfc00 }, + { c_HtmlNameLemonChiffon, 0xfffacd }, + { c_HtmlNameLightBlue, 0xadd8e6 }, + { c_HtmlNameLightCoral, 0xf08080 }, + { c_HtmlNameLightCyan, 0xe0ffff }, + { c_HtmlNameLightGoldenrodYellow, 0xfafad2 }, + { c_HtmlNameLightGray, 0xd3d3d3 }, + { c_HtmlNameLightGreen, 0x90ee90 }, + { c_HtmlNameLightGrey, 0xd3d3d3 }, + { c_HtmlNameLightPink, 0xffb6c1 }, + { c_HtmlNameLightSalmon, 0xffa07a }, + { c_HtmlNameLightSeaGreen, 0x20b2aa }, + { c_HtmlNameLightSkyBlue, 0x87cefa }, + { c_HtmlNameLightSlateGray, 0x778899 }, + { c_HtmlNameLightSlateGrey, 0x778899 }, + { c_HtmlNameLightSteelBlue, 0xb0c4de }, + { c_HtmlNameLightYellow, 0xffffe0 }, + { c_HtmlNameLime, 0xff00 }, + { c_HtmlNameLimeGreen, 0x32cd32 }, + { c_HtmlNameLinen, 0xfaf0e6 }, + { c_HtmlNameMagenta, 0xff00ff }, + { c_HtmlNameMaroon, 0x800000 }, + { c_HtmlNameMediumAquamarine, 0x66cdaa }, + { c_HtmlNameMediumBlue, 0xcd }, + { c_HtmlNameMediumOrchid, 0xba55d3 }, + { c_HtmlNameMediumPurple, 0x9370d8 }, + { c_HtmlNameMediumSeagreen, 0x3cb371 }, + { c_HtmlNameMediumSlateBlue, 0x7b68ee }, + { c_HtmlNameMediumSpringGreen, 0xfa9a }, + { c_HtmlNameMediumTurquoise, 0x48d1cc }, + { c_HtmlNameMediumVioletRed, 0xc71585 }, + { c_HtmlNameMidnightBlue, 0x191970 }, + { c_HtmlNameMintCream, 0xf5fffa }, + { c_HtmlNameMistyRose, 0xffe4e1 }, + { c_HtmlNameMoccasin, 0xffe4b5 }, + { c_HtmlNameNavajoWhite, 0xffdead }, + { c_HtmlNameNavy, 0x80 }, + { c_HtmlNameOldLace, 0xfdf5e6 }, + { c_HtmlNameOlive, 0x808000 }, + { c_HtmlNameOliveDrab, 0x6b8e23 }, + { c_HtmlNameOrange, 0xffa500 }, + { c_HtmlNameOrangeRed, 0xff4500 }, + { c_HtmlNameOrchid, 0xda70d6 }, + { c_HtmlNamePaleGoldenrod, 0xeee8aa }, + { c_HtmlNamePaleGreen, 0x98fb98 }, + { c_HtmlNamePaleTurquoise, 0xafeeee }, + { c_HtmlNamePaleVioletRed, 0xd87093 }, + { c_HtmlNamePapayaWhip, 0xffefd5 }, + { c_HtmlNamePeachPuff, 0xffdab9 }, + { c_HtmlNamePeru, 0xcd853f }, + { c_HtmlNamePink, 0xffc0cb }, + { c_HtmlNamePlum, 0xdda0dd }, + { c_HtmlNamePowderBlue, 0xb0e0e6 }, + { c_HtmlNamePurple, 0x800080 }, + { c_HtmlNameRed, 0xff0000 }, + { c_HtmlNameRosyBrown, 0xbc8f8f }, + { c_HtmlNameRoyalBlue, 0x4169e1 }, + { c_HtmlNameSaddleBrown, 0x8b4513 }, + { c_HtmlNameSalmon, 0xfa8072 }, + { c_HtmlNameSandyBrown, 0xf4a460 }, + { c_HtmlNameSeaGreen, 0x2e8b57 }, + { c_HtmlNameSeaShell, 0xfff5ee }, + { c_HtmlNameSienna, 0xa0522d }, + { c_HtmlNameSilver, 0xc0c0c0 }, + { c_HtmlNameSkyBlue, 0x87ceeb }, + { c_HtmlNameSlateBlue, 0x6a5acd }, + { c_HtmlNameSlateGray, 0x708090 }, + { c_HtmlNameSlateGrey, 0x708090 }, + { c_HtmlNameSnow, 0xfffafa }, + { c_HtmlNameSpringGreen, 0xff7f }, + { c_HtmlNameSteelBlue, 0x4682b4 }, + { c_HtmlNameTan, 0xd2b48c }, + { c_HtmlNameTeal, 0x8080 }, + { c_HtmlNameThistle, 0xd8bfd8 }, + { c_HtmlNameTomato, 0xff6347 }, + { c_HtmlNameTurquoise, 0x40e0d0 }, + { c_HtmlNameViolet, 0xee82ee }, + { c_HtmlNameWheat, 0xf5deb3 }, + { c_HtmlNameWhite, 0xffffff }, + { c_HtmlNameWhiteSmoke, 0xf5f5f5 }, + { c_HtmlNameYellow, 0xffff00 }, + { c_HtmlNameYellowGreen, 0x9acd32 }, +}; + +const HtmlColorPair* HtmlColorNames::Pair(uint8_t index) +{ + return &c_ColorNames[index]; +} + +uint8_t HtmlColorNames::Count() +{ + return countof(c_ColorNames); +} diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorShortNames.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorShortNames.cpp new file mode 100644 index 0000000..3c4e27c --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/HtmlColorShortNames.cpp @@ -0,0 +1,59 @@ +/*------------------------------------------------------------------------- +HtmlShortColorNames provides a template class for access to the short name table + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "HtmlColor.h" +#include "HtmlColorNameStrings.h" + +static const HtmlColorPair c_ShortColorNames[] PROGMEM = { + { c_HtmlNameAqua, 0xffff }, + { c_HtmlNameBlack, 0x0 }, + { c_HtmlNameBlue, 0xff }, + { c_HtmlNameFuchsia, 0xff00ff }, + { c_HtmlNameGray, 0x808080 }, + { c_HtmlNameGreen, 0x8000 }, + { c_HtmlNameLime, 0xff00 }, + { c_HtmlNameMaroon, 0x800000 }, + { c_HtmlNameNavy, 0x80 }, + { c_HtmlNameOlive, 0x808000 }, + { c_HtmlNameOrange, 0xffa500 }, + { c_HtmlNamePurple, 0x800080 }, + { c_HtmlNameRed, 0xff0000 }, + { c_HtmlNameSilver, 0xc0c0c0 }, + { c_HtmlNameTeal, 0x8080 }, + { c_HtmlNameWhite, 0xffffff }, + { c_HtmlNameYellow, 0xffff00 }, +}; + + +const HtmlColorPair* HtmlShortColorNames::Pair(uint8_t index) +{ + return &c_ShortColorNames[index]; +} + +uint8_t HtmlShortColorNames::Count() +{ + return countof(c_ShortColorNames); +} \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/Layouts.h b/libraries/NeoPixelBus_by_Makuna/src/internal/Layouts.h new file mode 100644 index 0000000..0df0049 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/Layouts.h @@ -0,0 +1,426 @@ +#pragma once +/*------------------------------------------------------------------------- +Layout provides a collection of class objects that are used with NeoTopology +object. +They define the specific layout of pixels and do the math to change the 2d +cordinate space to 1d cordinate space + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +const uint16_t PixelIndex_OutOfBounds = 0xffff; + +//----------------------------------------------------------------------------- +// RowMajor +//----------------------------------------------------------------------------- + +class RowMajorLayout; +class RowMajor90Layout; +class RowMajor180Layout; +class RowMajor270Layout; + +class RowMajorTilePreference +{ +public: + typedef RowMajorLayout EvenRowEvenColumnLayout; + typedef RowMajor270Layout EvenRowOddColumnLayout; + typedef RowMajor90Layout OddRowEvenColumnLayout; + typedef RowMajor180Layout OddRowOddColumnLayout; +}; + +// layout example of 4x4 +// 00 01 02 03 +// 04 05 06 07 +// 08 09 10 11 +// 12 13 14 15 +// +class RowMajorLayout : public RowMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return x + y * width; + } +}; + +// layout example of 4x4 +// 12 08 04 00 +// 13 09 05 01 +// 14 10 06 02 +// 15 11 07 03 +// +class RowMajor90Layout : public RowMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return (width - 1 - x) * height + y; + } +}; + +// layout example of 4x4 +// 15 14 13 12 +// 11 10 09 08 +// 07 06 05 04 +// 03 02 01 00 +// +class RowMajor180Layout : public RowMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return (width - 1 - x) + (height - 1 - y) * width; + } +}; + +// layout example of 4x4 +// 03 07 11 15 +// 02 06 10 14 +// 01 05 09 13 +// 00 04 08 12 +// +class RowMajor270Layout : public RowMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return x * height + (height - 1 - y); + } +}; + + +//----------------------------------------------------------------------------- +// ColumnMajor +//----------------------------------------------------------------------------- + +class ColumnMajorLayout; +class ColumnMajor90Layout; +class ColumnMajor180Layout; +class ColumnMajor270Layout; + +class ColumnMajorTilePreference +{ +public: + typedef ColumnMajorLayout EvenRowEvenColumnLayout; + typedef ColumnMajor270Layout EvenRowOddColumnLayout; + typedef ColumnMajor90Layout OddRowEvenColumnLayout; + typedef ColumnMajor180Layout OddRowOddColumnLayout; +}; + +// layout example of 4x4 +// 00 04 08 12 +// 01 05 09 13 +// 02 06 10 14 +// 03 07 11 15 +// +class ColumnMajorLayout : public ColumnMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return x * height + y; + } +}; + +// layout example of 4x4 +// 03 02 01 00 +// 07 06 05 04 +// 11 10 09 08 +// 15 14 13 12 +// +class ColumnMajor90Layout : public ColumnMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return (width - 1 - x) + y * width; + } +}; + +// layout example of 4x4 +// 15 11 07 03 +// 14 10 06 02 +// 13 09 05 01 +// 12 08 04 00 +// +class ColumnMajor180Layout : public ColumnMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return (width - 1 - x) * height + (height - 1 - y); + } +}; + +// layout example of 4x4 +// 12 13 14 15 +// 08 09 10 11 +// 04 05 06 07 +// 00 01 02 03 +// +class ColumnMajor270Layout : public ColumnMajorTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + return x + (height - 1 - y) * width; + } +}; + + +//----------------------------------------------------------------------------- +// RowMajorAlternating +//----------------------------------------------------------------------------- + +class RowMajorAlternating270Layout; +class RowMajorAlternating90Layout; + +class RowMajorAlternatingTilePreference +{ +public: + typedef RowMajorAlternating270Layout EvenRowEvenColumnLayout; + typedef RowMajorAlternating270Layout EvenRowOddColumnLayout; + typedef RowMajorAlternating90Layout OddRowEvenColumnLayout; + typedef RowMajorAlternating90Layout OddRowOddColumnLayout; +}; + +// layout example of 4x4 +// 00 01 02 03 +// 07 06 05 04 +// 08 09 10 11 +// 15 14 13 12 +// +class RowMajorAlternatingLayout : public RowMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t index = y * width; + + if (y & 0x0001) + { + index += ((width - 1) - x); + } + else + { + index += x; + } + return index; + } +}; + +// layout example of 4x4 +// 15 08 07 00 +// 14 09 06 01 +// 13 10 05 02 +// 12 11 04 03 +// +class RowMajorAlternating90Layout : public RowMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t mx = ((width - 1) - x); + uint16_t index = mx * height; + + if (mx & 0x0001) + { + index += ((height - 1) - y); + } + else + { + index += y; + } + return index; + } +}; + +// layout example of 4x4 +// 12 13 14 15 +// 11 10 09 08 +// 04 05 06 07 +// 03 02 01 00 +// +class RowMajorAlternating180Layout : public RowMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t my = ((height - 1) - y); + uint16_t index = my * width; + + if (my & 0x0001) + { + index += x; + } + else + { + index += ((width - 1) - x); + } + return index; + } +}; + +// layout example of 4x4 +// 03 04 11 12 +// 02 05 10 13 +// 01 06 09 14 +// 00 07 08 15 +// +class RowMajorAlternating270Layout : public RowMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t index = x * height; + + if (x & 0x0001) + { + index += y; + } + else + { + index += ((height - 1) - y); + } + return index; + } +}; + + +//----------------------------------------------------------------------------- +// ColumnMajorAlternating +//----------------------------------------------------------------------------- + +class ColumnMajorAlternatingLayout; +class ColumnMajorAlternating180Layout; + +class ColumnMajorAlternatingTilePreference +{ +public: + typedef ColumnMajorAlternatingLayout EvenRowEvenColumnLayout; + typedef ColumnMajorAlternatingLayout EvenRowOddColumnLayout; + typedef ColumnMajorAlternating180Layout OddRowEvenColumnLayout; + typedef ColumnMajorAlternating180Layout OddRowOddColumnLayout; +}; + +// layout example of 4x4 +// 00 07 08 15 +// 01 06 09 14 +// 02 05 10 13 +// 03 04 11 12 +// +class ColumnMajorAlternatingLayout : public ColumnMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t index = x * height; + + if (x & 0x0001) + { + index += ((height - 1) - y); + } + else + { + index += y; + } + return index; + } +}; + +// layout example of 4x4 +// 03 02 01 00 +// 04 05 06 07 +// 11 10 09 08 +// 12 13 14 15 +// +class ColumnMajorAlternating90Layout : public ColumnMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t index = y * width; + + if (y & 0x0001) + { + index += x; + } + else + { + index += ((width - 1) - x); + } + return index; + } +}; + +// layout example of 4x4 +// 12 11 04 03 +// 13 10 05 02 +// 14 09 06 01 +// 15 08 07 00 +// +class ColumnMajorAlternating180Layout : public ColumnMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t mx = ((width - 1) - x); + uint16_t index = mx * height; + + if (mx & 0x0001) + { + index += y; + } + else + { + index += ((height - 1) - y); + } + return index; + } +}; + +// layout example of 4x4 +// 15 14 13 12 +// 08 09 10 11 +// 07 06 05 04 +// 00 01 02 03 +// +class ColumnMajorAlternating270Layout : public ColumnMajorAlternatingTilePreference +{ +public: + static uint16_t Map(uint16_t width, uint16_t height, uint16_t x, uint16_t y) + { + uint16_t my = ((height - 1) - y); + uint16_t index = my * width; + + if (my & 0x0001) + { + index += ((width - 1) - x); + } + else + { + index += x; + } + return index; + } +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoArmMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoArmMethod.h new file mode 100644 index 0000000..3ab66a7 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoArmMethod.h @@ -0,0 +1,697 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for ARM MCUs. +Teensy 3.0, 3.1, LC, Arduino Due + +Written by Michael C. Miller. +Some work taken from the Adafruit NeoPixel library. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. +The contents of this file were taken from the Adafruit NeoPixel library +and modified only to fit within individual calling functions. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#if defined(__arm__) + +template class NeoArmMethodBase +{ +public: + NeoArmMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : + _pin(pin) + { + pinMode(pin, OUTPUT); + + _sizePixels = pixelCount * elementSize; + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + } + + ~NeoArmMethodBase() + { + pinMode(_pin, INPUT); + + free(_pixels); + } + + bool IsReadyToUpdate() const + { + uint32_t delta = micros() - _endTime; + + return (delta >= T_SPEED::ResetTimeUs); + } + + void Initialize() + { + digitalWrite(_pin, LOW); + + _endTime = micros(); + } + + void Update() + { + // Data latch = 50+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while (!IsReadyToUpdate()) + { + yield(); // allows for system yield if needed + } + + noInterrupts(); // Need 100% focus on instruction timing + + T_SPEED::send_pixels(_pixels, _sizePixels, _pin); + + interrupts(); + + // save EOD time for latch on next call + _endTime = micros(); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + uint32_t _endTime; // Latch timing reference + size_t _sizePixels; // Size of '_pixels' buffer below + uint8_t* _pixels; // Holds LED color values + uint8_t _pin; // output pin number +}; + + +#if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1 + +class NeoArmMk20dxSpeedPropsWs2813 +{ +public: + static const uint32_t CyclesT0h = (F_CPU / 4000000); + static const uint32_t CyclesT1h = (F_CPU / 1250000); + static const uint32_t Cycles = (F_CPU / 800000); + static const uint32_t ResetTimeUs = 250; +}; + +class NeoArmMk20dxSpeedProps800Kbps +{ +public: + static const uint32_t CyclesT0h = (F_CPU / 4000000); + static const uint32_t CyclesT1h = (F_CPU / 1250000); + static const uint32_t Cycles = (F_CPU / 800000); + static const uint32_t ResetTimeUs = 50; +}; + +class NeoArmMk20dxSpeedProps400Kbps +{ +public: + static const uint32_t CyclesT0h = (F_CPU / 2000000); + static const uint32_t CyclesT1h = (F_CPU / 833333); + static const uint32_t Cycles = (F_CPU / 400000); + static const uint32_t ResetTimeUs = 50; +}; + +template class NeoArmMk20dxSpeedBase +{ +public: + static const uint32_t ResetTimeUs = T_SPEEDPROPS::ResetTimeUs; + + static void send_pixels(uint8_t* pixels, size_t sizePixels, uint8_t pin) + { + uint8_t* p = pixels; + uint8_t* end = p + sizePixels; + uint8_t pix; + uint8_t mask; + + volatile uint8_t* set = portSetRegister(pin); + volatile uint8_t* clr = portClearRegister(pin); + + uint32_t cyc; + + ARM_DEMCR |= ARM_DEMCR_TRCENA; + ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; + + cyc = ARM_DWT_CYCCNT + T_SPEEDPROPS::Cycles; + while (p < end) + { + pix = *p++; + for (mask = 0x80; mask; mask >>= 1) + { + while (ARM_DWT_CYCCNT - cyc < T_SPEEDPROPS::Cycles); + + cyc = ARM_DWT_CYCCNT; + *set = 1; + if (pix & mask) + { + while (ARM_DWT_CYCCNT - cyc < T_SPEEDPROPS::CyclesT1h); + } + else + { + while (ARM_DWT_CYCCNT - cyc < T_SPEEDPROPS::CyclesT0h); + } + *clr = 1; + } + } + } +}; + +typedef NeoArmMethodBase> NeoArmWs2813Method; +typedef NeoArmMethodBase> NeoArm800KbpsMethod; +typedef NeoArmMethodBase> NeoArm400KbpsMethod; + +#elif defined(__MKL26Z64__) // Teensy-LC + +#if F_CPU == 48000000 + + + +class NeoArmMk26z64Speed800KbpsBase +{ +public: + static void send_pixels(uint8_t* pixels, size_t sizePixels, uint8_t pin) + { + uint8_t* p = pixels; + uint8_t pix; + uint8_t count; + uint8_t dly; + uint8_t bitmask = digitalPinToBitMask(pin); + volatile uint8_t* reg = portSetRegister(pin); + uint32_t num = sizePixels; + + asm volatile( + "L%=_begin:" "\n\t" + "ldrb %[pix], [%[p], #0]" "\n\t" + "lsl %[pix], #24" "\n\t" + "movs %[count], #7" "\n\t" + "L%=_loop:" "\n\t" + "lsl %[pix], #1" "\n\t" + "bcs L%=_loop_one" "\n\t" + "L%=_loop_zero:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_loop_delay_T0H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T0H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_loop_delay_T0L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T0L" "\n\t" + "b L%=_next" "\n\t" + "L%=_loop_one:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_loop_delay_T1H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T1H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_loop_delay_T1L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_loop_delay_T1L" "\n\t" + "nop" "\n\t" + "L%=_next:" "\n\t" + "sub %[count], #1" "\n\t" + "bne L%=_loop" "\n\t" + "lsl %[pix], #1" "\n\t" + "bcs L%=_last_one" "\n\t" + "L%=_last_zero:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #4" "\n\t" + "L%=_last_delay_T0H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T0H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #10" "\n\t" + "L%=_last_delay_T0L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T0L" "\n\t" + "b L%=_repeat" "\n\t" + "L%=_last_one:" + "strb %[bitmask], [%[reg], #0]" "\n\t" + "movs %[dly], #13" "\n\t" + "L%=_last_delay_T1H:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T1H" "\n\t" + "strb %[bitmask], [%[reg], #4]" "\n\t" + "movs %[dly], #1" "\n\t" + "L%=_last_delay_T1L:" "\n\t" + "sub %[dly], #1" "\n\t" + "bne L%=_last_delay_T1L" "\n\t" + "nop" "\n\t" + "L%=_repeat:" "\n\t" + "add %[p], #1" "\n\t" + "sub %[num], #1" "\n\t" + "bne L%=_begin" "\n\t" + "L%=_done:" "\n\t" + : [p] "+r" (p), + [pix] "=&r" (pix), + [count] "=&r" (count), + [dly] "=&r" (dly), + [num] "+r" (num) + : [bitmask] "r" (bitmask), + [reg] "r" (reg) + ); + } +}; + +class NeoArmMk26z64SpeedWs2813 : public NeoArmMk26z64Speed800KbpsBase +{ +public: + const static uint32_t ResetTimeUs = 250; +}; + +class NeoArmMk26z64Speed800Kbps : public NeoArmMk26z64Speed800KbpsBase +{ +public: + const static uint32_t ResetTimeUs = 50; +}; + +typedef NeoArmMethodBase NeoArmWs2813Method; +typedef NeoArmMethodBase NeoArm800KbpsMethod; + +#else +#error "Teensy-LC: Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz" +#endif // F_CPU == 48000000 + +#elif defined(__SAMD21G18A__) // Arduino Zero + + +class NeoArmSamd21g18aSpeedProps800KbpsBase +{ +public: + static void BitPreWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;"); + } + static void BitT1hWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + } + static void BitT0lWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + } + static void BitPostWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;"); + } +}; + +class NeoArmSamd21g18aSpeedPropsWs2813 : public NeoArmSamd21g18aSpeedProps800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 250; +}; + +class NeoArmSamd21g18aSpeedProps800Kbps : public NeoArmSamd21g18aSpeedProps800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 50; +}; + + +class NeoArmSamd21g18aSpeedProps400Kbps +{ +public: + static void BitPreWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;"); + } + static void BitT1hWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop;"); + } + static void BitT0lWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop;"); + } + static void BitPostWait() + { + asm("nop; nop; nop; nop; nop; nop; nop;"); + } + static const uint32_t ResetTimeUs = 50; +}; + +template class NeoArmSamd21g18aSpeedBase +{ +public: + static const uint32_t ResetTimeUs = T_SPEEDPROPS::ResetTimeUs; + + static void send_pixels(uint8_t* pixels, size_t sizePixels, uint8_t pin) + { + // Tried this with a timer/counter, couldn't quite get adequate + // resolution. So yay, you get a load of goofball NOPs... + uint8_t* ptr = pixels; + uint8_t* end = ptr + sizePixels;; + uint8_t p = *ptr++; + uint8_t bitMask = 0x80; + uint8_t portNum = g_APinDescription[pin].ulPort; + uint32_t pinMask = 1ul << g_APinDescription[pin].ulPin; + + volatile uint32_t* set = &(PORT->Group[portNum].OUTSET.reg); + volatile uint32_t* clr = &(PORT->Group[portNum].OUTCLR.reg); + + for (;;) + { + *set = pinMask; + T_SPEEDPROPS::BitPreWait(); + + if (p & bitMask) + { + T_SPEEDPROPS::BitT1hWait(); + *clr = pinMask; + } + else + { + *clr = pinMask; + T_SPEEDPROPS::BitT0lWait(); + } + if (bitMask >>= 1) + { + T_SPEEDPROPS::BitPostWait(); + } + else + { + if (ptr >= end) + { + break; + } + p = *ptr++; + bitMask = 0x80; + } + } + } +}; + +typedef NeoArmMethodBase> NeoArmWs2813Method; +typedef NeoArmMethodBase> NeoArm800KbpsMethod; +typedef NeoArmMethodBase> NeoArm400KbpsMethod; + +#elif defined (ARDUINO_STM32_FEATHER) // FEATHER WICED (120MHz) + +class NeoArmStm32SpeedProps800KbpsBase +{ +public: + static void BitT1hWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop;"); + } + static void BitT1lWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop;"); + } + static void BitT0hWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop;"); + } + static void BitT0lWait() + { + asm("nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop; nop; nop; nop; nop;" + "nop; nop; nop; nop;"); + } +}; + +class NeoArmStm32SpeedProps800Kbps : public NeoArmStm32SpeedProps800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 50; +}; + +class NeoArmStm32SpeedPropsWs2813 : public NeoArmStm32SpeedProps800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 250; +}; + +/* TODO - not found in Adafruit library +class NeoArmStm32SpeedProps400Kbps +{ +static void BitT1hWait() +{ +} +static void BitT1lWait() +{ +} +static void BitT0hWait() +{ +} +static void BitT0lWait() +{ +} +}; +*/ + +template class NeoArmStm32SpeedBase +{ +public: + static const uint32_t ResetTimeUs = T_SPEEDPROPS::ResetTimeUs; + + static void send_pixels(uint8_t* pixels, size_t sizePixels, uint8_t pin) + { + // Tried this with a timer/counter, couldn't quite get adequate + // resolution. So yay, you get a load of goofball NOPs... + + uint8_t* ptr = pixels; + uint8_t* end = ptr + sizePixels; + uint8_t p = *ptr++; + uint8_t bitMask = 0x80; + uint32_t pinMask = BIT(PIN_MAP[pin].gpio_bit); + + volatile uint16_t* set = &(PIN_MAP[pin].gpio_device->regs->BSRRL); + volatile uint16_t* clr = &(PIN_MAP[pin].gpio_device->regs->BSRRH); + + for (;;) + { + if (p & bitMask) + { + // ONE + // High 800ns + *set = pinMask; + T_SPEEDPROPS::BitT1hWait(); + // Low 450ns + *clr = pinMask; + T_SPEEDPROPS::BitT1lWait(); + } + else + { + // ZERO + // High 400ns + *set = pinMask; + T_SPEEDPROPS::BitT0hWait(); + // Low 850ns + *clr = pinMask; + T_SPEEDPROPS::BitT0lWait(); + } + if (bitMask >>= 1) + { + // Move on to the next pixel + asm("nop;"); + } + else + { + if (ptr >= end) + { + break; + } + + p = *ptr++; + bitMask = 0x80; + } + } + } +}; + +typedef NeoArmMethodBase> NeoArmWs2813Method; +typedef NeoArmMethodBase> NeoArm800KbpsMethod; + +#else // Other ARM architecture -- Presumed Arduino Due + + +#define ARM_OTHER_SCALE VARIANT_MCK / 2UL / 1000000UL +#define ARM_OTHER_INST (2UL * F_CPU / VARIANT_MCK) + +class NeoArmOtherSpeedPropsWs2813 +{ +public: + static const uint32_t CyclesT0h = ((uint32_t)(0.40 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT1h = ((uint32_t)(0.80 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t Cycles = ((uint32_t)(1.25 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t ResetTimeUs = 250; +}; + +class NeoArmOtherSpeedProps800Kbps +{ +public: + static const uint32_t CyclesT0h = ((uint32_t)(0.40 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT1h = ((uint32_t)(0.80 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t Cycles = ((uint32_t)(1.25 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t ResetTimeUs = 50; +}; + +class NeoArmOtherSpeedProps400Kbps +{ +public: + static const uint32_t CyclesT0h = ((uint32_t)(0.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT1h = ((uint32_t)(1.20 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t Cycles = ((uint32_t)(2.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t ResetTimeUs = 50; +}; + +template class NeoArmOtherSpeedBase +{ +public: + static const uint32_t ResetTimeUs = T_SPEEDPROPS::ResetTimeUs; + + static void send_pixels(uint8_t* pixels, size_t sizePixels, uint8_t pin) + { + uint32_t pinMask; + uint32_t t; + Pio* port; + volatile WoReg* portSet; + volatile WoReg* portClear; + volatile WoReg* timeValue; + volatile WoReg* timeReset; + uint8_t* p; + uint8_t* end; + uint8_t pix; + uint8_t mask; + + pmc_set_writeprotect(false); + pmc_enable_periph_clk((uint32_t)TC3_IRQn); + + TC_Configure(TC1, 0, + TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1); + TC_Start(TC1, 0); + + pinMask = g_APinDescription[pin].ulPin; // Don't 'optimize' these into + port = g_APinDescription[pin].pPort; // declarations above. Want to + portSet = &(port->PIO_SODR); // burn a few cycles after + portClear = &(port->PIO_CODR); // starting timer to minimize + timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'. + timeReset = &(TC1->TC_CHANNEL[0].TC_CCR); + p = pixels; + end = p + sizePixels; + pix = *p++; + mask = 0x80; + + for (;;) + { + if (pix & mask) + { + t = T_SPEEDPROPS::CyclesT1h; + } + else + { + t = T_SPEEDPROPS::CyclesT0h; + } + + // wait for the end of the previous cycle + while (*timeValue < T_SPEEDPROPS::Cycles); + + *portSet = pinMask; + *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG; + + while (*timeValue < t); + + *portClear = pinMask; + if (!(mask >>= 1)) + { + // This 'inside-out' loop logic utilizes + if (p >= end) + { + break; // idle time to minimize inter-byte delays. + } + pix = *p++; + mask = 0x80; + } + } + + // not really needed as the wait for latch does this and + // while (*timeValue < T_SPEEDPROPS::Cycles); // Wait for last bit + + TC_Stop(TC1, 0); + } +}; + +typedef NeoArmMethodBase> NeoArmWs2813Method; +typedef NeoArmMethodBase> NeoArm800KbpsMethod; +typedef NeoArmMethodBase> NeoArm400KbpsMethod; + +#endif + + +// Arm doesn't have alternatives methods yet, so only one to make the default +typedef NeoArmWs2813Method NeoWs2813Method; +typedef NeoArm800KbpsMethod Neo800KbpsMethod; +#ifdef NeoArm400KbpsMethod // this is needed due to missing 400Kbps for some platforms +typedef NeoArm400KbpsMethod Neo400KbpsMethod; +#endif + +#endif // defined(__arm__) + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoAvrMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoAvrMethod.h new file mode 100644 index 0000000..9843816 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoAvrMethod.h @@ -0,0 +1,200 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Atmel AVR. + +Written by Michael C. Miller. +Some work taken from the Adafruit NeoPixel library. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#ifdef ARDUINO_ARCH_AVR + +extern "C" +{ + void send_pixels_8mhz_800_PortD(uint8_t* pixels, size_t sizePixels, uint8_t pinMask); + void send_pixels_8mhz_800_PortB(uint8_t* pixels, size_t sizePixels, uint8_t pinMask); + void send_pixels_8mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask); + void send_pixels_12mhz_800_PortD(uint8_t* pixels, size_t sizePixels, uint8_t pinMask); + void send_pixels_12mhz_800_PortB(uint8_t* pixels, size_t sizePixels, uint8_t pinMask); + void send_pixels_12mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask); + void send_pixels_16mhz_800(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask); + void send_pixels_16mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask); +} + +class NeoAvrSpeed800KbpsBase +{ +public: + static void send_pixels(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) + { +#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) // 8Mhz CPU +#ifdef PORTD // PORTD isn't present on ATtiny85, etc. + if (port == &PORTD) + send_pixels_8mhz_800_PortD(pixels, sizePixels, pinMask); + else if (port == &PORTB) +#endif // PORTD + send_pixels_8mhz_800_PortB(pixels, sizePixels, pinMask); + +#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) // 12Mhz CPU +#ifdef PORTD // PORTD + if (port == &PORTD) + send_pixels_12mhz_800_PortD(pixels, sizePixels, pinMask); + else if (port == &PORTB) +#endif // PORTD + send_pixels_12mhz_800_PortB(pixels, sizePixels, pinMask); + +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) // 16Mhz CPU + send_pixels_16mhz_800(pixels, sizePixels, port, pinMask); +#else +#error "CPU SPEED NOT SUPPORTED" +#endif + } + +}; + +class NeoAvrSpeedWs2813 : public NeoAvrSpeed800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 250; +}; + +class NeoAvrSpeed800Kbps: public NeoAvrSpeed800KbpsBase +{ +public: + static const uint32_t ResetTimeUs = 50; +}; + +class NeoAvrSpeed400Kbps +{ +public: + static void send_pixels(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) + { +#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) // 8Mhz CPU + send_pixels_8mhz_400(pixels, sizePixels, port, pinMask); + +#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) // 12Mhz CPU + send_pixels_12mhz_400(pixels, sizePixels, port, pinMask); + +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) // 16Mhz CPU + send_pixels_16mhz_400(pixels, sizePixels, port, pinMask); +#else +#error "CPU SPEED NOT SUPPORTED" +#endif + } + static const uint32_t ResetTimeUs = 50; +}; + +template class NeoAvrMethodBase +{ +public: + NeoAvrMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : + _pin(pin), + _port(NULL), + _pinMask(0) + { + pinMode(pin, OUTPUT); + + _sizePixels = pixelCount * elementSize; + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + + _port = portOutputRegister(digitalPinToPort(pin)); + _pinMask = digitalPinToBitMask(pin); + } + + ~NeoAvrMethodBase() + { + pinMode(_pin, INPUT); + + free(_pixels); + } + + bool IsReadyToUpdate() const + { + uint32_t delta = micros() - _endTime; + + return (delta >= T_SPEED::ResetTimeUs); + } + + void Initialize() + { + digitalWrite(_pin, LOW); + + _endTime = micros(); + } + + void Update() + { + // Data latch = 50+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while (!IsReadyToUpdate()) + { +#if !defined(ARDUINO_TEEONARDU_LEO) && !defined(ARDUINO_TEEONARDU_FLORA) + yield(); // allows for system yield if needed +#endif + } + + noInterrupts(); // Need 100% focus on instruction timing + + T_SPEED::send_pixels(_pixels, _sizePixels, _port, _pinMask); + + interrupts(); + + // save EOD time for latch on next call + _endTime = micros(); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + uint32_t _endTime; // Latch timing reference + size_t _sizePixels; // Size of '_pixels' buffer below + uint8_t* _pixels; // Holds LED color values + uint8_t _pin; // output pin number + + volatile uint8_t* _port; // Output PORT register + uint8_t _pinMask; // Output PORT bitmask +}; + +typedef NeoAvrMethodBase NeoAvrWs2813Method; +typedef NeoAvrMethodBase NeoAvr800KbpsMethod; +typedef NeoAvrMethodBase NeoAvr400KbpsMethod; + +// AVR doesn't have alternatives yet, so there is just the default +typedef NeoAvrWs2813Method NeoWs2813Method; +typedef NeoAvr800KbpsMethod Neo800KbpsMethod; +typedef NeoAvr400KbpsMethod Neo400KbpsMethod; + +#endif + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBitmapFile.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBitmapFile.h new file mode 100644 index 0000000..6979678 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBitmapFile.h @@ -0,0 +1,351 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +const uint16_t c_BitmapFileId = 0x4d42; // "BM" + +#pragma pack(push, 2) +struct BitmapFileHeader +{ + uint16_t FileId; // only c_BitmapFileId is supported + uint32_t FileSize; + uint16_t Reserved0; + uint16_t Reserved1; + uint32_t PixelAddress; +}; + +struct BitmapInfoHeader +{ + uint32_t Size; + int32_t Width; + int32_t Height; + uint16_t Planes; // only support 1 + uint16_t BitsPerPixel; // only support 24 and 32 + uint32_t Compression; // only support BI_Rgb + uint32_t RawDateSize; // can be zero + int32_t XPpm; + int32_t YPpm; + uint32_t PaletteLength; + uint32_t ImportantColorCount; +}; +#pragma pack(pop) + +enum BmpCompression +{ + BI_Rgb, + BI_Rle8, + BI_Rle4, + BI_Bitfields, + BI_Jpeg, + BI_Png, + BI_AlphaBitfields, + BI_Cmyk = 11, + BI_CmykRle8, + BI_CmykRle4 +}; + +template class NeoBitmapFile +{ +public: + NeoBitmapFile() : + _fileAddressPixels(0), + _width(0), + _height(0), + _sizeRow(0), + _bottomToTop(true), + _bytesPerPixel(0) + { + } + + + ~NeoBitmapFile() + { + _file.close(); + } + + bool Begin(T_FILE_METHOD file) + { + if (_file) + { + _file.close(); + } + + if (!file || !file.seek(0)) + { + goto error; + } + + _file = file; + + BitmapFileHeader bmpHeader; + BitmapInfoHeader bmpInfoHeader; + size_t result; + + result = _file.read((uint8_t*)(&bmpHeader), sizeof(bmpHeader)); + + if (result != sizeof(bmpHeader) || + bmpHeader.FileId != c_BitmapFileId || + bmpHeader.FileSize != _file.size()) + { + goto error; + } + + result = _file.read((uint8_t*)(&bmpInfoHeader), sizeof(bmpInfoHeader)); + + if (result != sizeof(bmpInfoHeader) || + result != bmpInfoHeader.Size || + 1 != bmpInfoHeader.Planes || + BI_Rgb != bmpInfoHeader.Compression) + { + goto error; + } + + if (!(24 == bmpInfoHeader.BitsPerPixel || + 32 == bmpInfoHeader.BitsPerPixel)) + { + goto error; + } + + // save the interesting information + _width = abs(bmpInfoHeader.Width); + _height = abs(bmpInfoHeader.Height); + _fileAddressPixels = bmpHeader.PixelAddress; + // negative height means rows are top to bottom + _bottomToTop = (bmpInfoHeader.Height > 0); + // rows are 32 bit aligned so they may have padding on each row + _sizeRow = (bmpInfoHeader.BitsPerPixel * _width + 31) / 32 * 4; + _bytesPerPixel = bmpInfoHeader.BitsPerPixel / 8; + + return true; + + error: + _fileAddressPixels = 0; + _width = 0; + _height = 0; + _sizeRow = 0; + _bytesPerPixel = 0; + + _file.close(); + return false; + }; + + size_t PixelSize() const + { + return T_COLOR_FEATURE::PixelSize; + }; + + uint16_t PixelCount() const + { + return _width * _height; + }; + + uint16_t Width() const + { + return _width; + }; + + uint16_t Height() const + { + return _height; + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(int16_t x, int16_t y) const + { + if (x < 0 || x >= _width || y < 0 || y >= _height) + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + + typename T_COLOR_FEATURE::ColorObject color; + if (!seek(x, y) || !readPixel(&color)) + { + return 0; + } + + return color; + }; + + void Blt(NeoBufferContext destBuffer, + uint16_t indexPixel, + int16_t xSrc, + int16_t ySrc, + int16_t wSrc) + { + const uint16_t destPixelCount = destBuffer.PixelCount(); + typename T_COLOR_FEATURE::ColorObject color(0); + xSrc = constrainX(xSrc); + ySrc = constrainY(ySrc); + + if (seek(xSrc, ySrc)) + { + for (int16_t x = 0; x < wSrc && indexPixel < destPixelCount; x++, indexPixel++) + { + if (xSrc < _width) + { + if (readPixel(&color)) + { + xSrc++; + } + } + + T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color); + } + } + } + + void Blt(NeoBufferContext destBuffer, + int16_t xDest, + int16_t yDest, + int16_t xSrc, + int16_t ySrc, + int16_t wSrc, + int16_t hSrc, + LayoutMapCallback layoutMap) + { + const uint16_t destPixelCount = destBuffer.PixelCount(); + typename T_COLOR_FEATURE::ColorObject color(0); + + for (int16_t y = 0; y < hSrc; y++) + { + int16_t xFile = constrainX(xSrc); + int16_t yFile = constrainY(ySrc + y); + + if (seek(xFile, yFile)) + { + for (int16_t x = 0; x < wSrc; x++) + { + if (xFile < _width) + { + if (readPixel(&color)) + { + xFile++; + } + } + + uint16_t indexDest = layoutMap(xDest + x, yDest + y); + + if (indexDest < destPixelCount) + { + T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexDest, color); + } + } + } + } + }; + + +private: + T_FILE_METHOD _file; + uint32_t _fileAddressPixels; + uint16_t _width; + uint16_t _height; + uint32_t _sizeRow; + uint8_t _bytesPerPixel; + bool _bottomToTop; + + int16_t constrainX(int16_t x) + { + if (x < 0) + { + x = 0; + } + else if (x >= _width) + { + x = _width - 1; + } + return x; + }; + + int16_t constrainY(int16_t y) + { + if (y < 0) + { + y = 0; + } + else if (y >= _height) + { + y = _height - 1; + } + return y; + }; + + bool seek(int16_t x, int16_t y) + { + if (_bottomToTop) + { + y = (_height - 1) - y; + } + + uint32_t pos = y * _sizeRow + x * _bytesPerPixel; + pos += _fileAddressPixels; + + return _file.seek(pos); + }; + + bool readPixel(RgbColor* color) + { + uint8_t bgr[4]; + int result; + + result = _file.read(bgr, _bytesPerPixel); + + if (result != _bytesPerPixel) + { + *color = 0; + return false; + } + + color->B = bgr[0]; + color->G = bgr[1]; + color->R = bgr[2]; + + return true; + }; + + bool readPixel(RgbwColor* color) + { + uint8_t bgr[4]; + int result; + + bgr[3] = 0; // init white channel as read maybe only 3 bytes + result = _file.read(bgr, _bytesPerPixel); + + if (result != _bytesPerPixel) + { + *color = 0; + return false; + } + + color->B = bgr[0]; + color->G = bgr[1]; + color->R = bgr[2]; + color->W = bgr[3]; + + return true; + }; +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBuffer.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBuffer.h new file mode 100644 index 0000000..86176c9 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBuffer.h @@ -0,0 +1,154 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +template class NeoBuffer +{ +public: + NeoBuffer(uint16_t width, + uint16_t height, + PGM_VOID_P pixels) : + _method(width, height, pixels) + { + } + + operator NeoBufferContext() + { + return _method; + } + + uint16_t PixelCount() const + { + return _method.PixelCount(); + }; + + uint16_t Width() const + { + return _method.Width(); + }; + + uint16_t Height() const + { + return _method.Height(); + }; + + void SetPixelColor( + int16_t x, + int16_t y, + typename T_BUFFER_METHOD::ColorObject color) + { + _method.SetPixelColor(pixelIndex(x, y), color); + }; + + typename T_BUFFER_METHOD::ColorObject GetPixelColor( + int16_t x, + int16_t y) const + { + return _method.GetPixelColor(pixelIndex(x, y)); + }; + + void ClearTo(typename T_BUFFER_METHOD::ColorObject color) + { + _method.ClearTo(color); + }; + + void Blt(NeoBufferContext destBuffer, + uint16_t indexPixel) + { + uint16_t destPixelCount = destBuffer.PixelCount(); + // validate indexPixel + if (indexPixel >= destPixelCount) + { + return; + } + + // calc how many we can copy + uint16_t copyCount = destPixelCount - indexPixel; + uint16_t srcPixelCount = PixelCount(); + if (copyCount > srcPixelCount) + { + copyCount = srcPixelCount; + } + + uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexPixel); + _method.CopyPixels(pDest, _method.Pixels(), copyCount); + } + + void Blt(NeoBufferContext destBuffer, + int16_t xDest, + int16_t yDest, + int16_t xSrc, + int16_t ySrc, + int16_t wSrc, + int16_t hSrc, + LayoutMapCallback layoutMap) + { + uint16_t destPixelCount = destBuffer.PixelCount(); + + for (int16_t y = 0; y < hSrc; y++) + { + for (int16_t x = 0; x < wSrc; x++) + { + uint16_t indexDest = layoutMap(xDest + x, yDest + y); + + if (indexDest < destPixelCount) + { + const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), pixelIndex(xSrc + x, ySrc + y)); + uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexDest); + + _method.CopyPixels(pDest, pSrc, 1); + } + } + } + } + + void Blt(NeoBufferContext destBuffer, + int16_t xDest, + int16_t yDest, + LayoutMapCallback layoutMap) + { + Blt(destBuffer, xDest, yDest, 0, 0, Width(), Height(), layoutMap); + } + +private: + T_BUFFER_METHOD _method; + + uint16_t pixelIndex( + int16_t x, + int16_t y) + { + uint16_t result = PixelIndex_OutOfBounds; + + if (x >= 0 && + x < Width() && + y >= 0 && + y < Height()) + { + result = x + y * Width(); + } + return result; + } +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferContext.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferContext.h new file mode 100644 index 0000000..8b57344 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferContext.h @@ -0,0 +1,48 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +// This is used to allow a template classes that share common buffer concept to +// be able to pass that common information to functions +// The template classes just need to expose a conversion operator to this type +template struct NeoBufferContext +{ + NeoBufferContext(uint8_t* pixels, + size_t sizePixels) : + Pixels(pixels), + SizePixels(sizePixels) + { + } + + uint16_t PixelCount() const + { + return SizePixels / T_COLOR_FEATURE::PixelSize; + }; + + uint8_t* Pixels; + const size_t SizePixels; + +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferMethods.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferMethods.h new file mode 100644 index 0000000..4bd531f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoBufferMethods.h @@ -0,0 +1,263 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + + +#ifdef ARDUINO_ARCH_AVR + +typedef uint16_t(*LayoutMapCallback)(int16_t x, int16_t y); + +#else + +#undef max +#undef min +#include +typedef std::function LayoutMapCallback; + +#endif + +template class NeoBufferMethod +{ +public: + NeoBufferMethod(uint16_t width, uint16_t height, PGM_VOID_P pixels = NULL) : + _width(width), + _height(height) + { + _pixels = (uint8_t*)malloc(PixelsSize()); + if (pixels) + { + // copy from progmem to initialize + T_COLOR_FEATURE::movePixelsInc_P(_pixels, pixels, PixelCount()); + } + } + + ~NeoBufferMethod() + { + free(_pixels); + } + + operator NeoBufferContext() + { + return NeoBufferContext(Pixels(), PixelsSize()); + } + + uint8_t* Pixels() const + { + return _pixels; + }; + + size_t PixelsSize() const + { + return PixelSize() * PixelCount(); + }; + + size_t PixelSize() const + { + return T_COLOR_FEATURE::PixelSize; + }; + + uint16_t PixelCount() const + { + return _width * _height; + }; + + uint16_t Width() const + { + return _width; + }; + + uint16_t Height() const + { + return _height; + }; + + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + { + if (indexPixel < PixelCount()) + { + T_COLOR_FEATURE::applyPixelColor(_pixels, indexPixel, color); + } + }; + + void SetPixelColor(int16_t x, int16_t y, typename T_COLOR_FEATURE::ColorObject color) + { + if (x < 0 || x >= _width || y < 0 || y >= _height) + { + return; + } + + uint16_t indexPixel = x + y * _width; + T_COLOR_FEATURE::applyPixelColor(_pixels, indexPixel, color); + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + { + if (indexPixel >= PixelCount()) + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + + return T_COLOR_FEATURE::retrievePixelColor(_pixels, indexPixel); + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(int16_t x, int16_t y) const + { + if (x < 0 || x >= _width || y < 0 || y >= _height) + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + + uint16_t indexPixel = x + y * _width; + return T_COLOR_FEATURE::retrievePixelColor(_pixels, indexPixel); + }; + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color) + { + uint8_t temp[T_COLOR_FEATURE::PixelSize]; + + T_COLOR_FEATURE::applyPixelColor(temp, 0, color); + + T_COLOR_FEATURE::replicatePixel(_pixels, temp, PixelCount()); + }; + + void CopyPixels(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + T_COLOR_FEATURE::movePixelsInc(pPixelDest, pPixelSrc, count); + } + + typedef typename T_COLOR_FEATURE::ColorObject ColorObject; + typedef T_COLOR_FEATURE ColorFeature; + +private: + const uint16_t _width; + const uint16_t _height; + uint8_t* _pixels; +}; + +template class NeoBufferProgmemMethod +{ +public: + NeoBufferProgmemMethod(uint16_t width, uint16_t height, PGM_VOID_P pixels) : + _width(width), + _height(height), + _pixels(pixels) + { + } + + operator NeoBufferContext() + { + return NeoBufferContext(Pixels(), PixelsSize()); + } + + uint8_t* Pixels() const + { + return (uint8_t*)_pixels; + }; + + size_t PixelsSize() const + { + return PixelSize() * PixelCount(); + }; + + size_t PixelSize() const + { + return T_COLOR_FEATURE::PixelSize; + }; + + uint16_t PixelCount() const + { + return _width * _height; + }; + + uint16_t Width() const + { + return _width; + }; + + uint16_t Height() const + { + return _height; + }; + + void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color) + { + // PROGMEM is read only, this will do nothing + }; + + void SetPixelColor(uint16_t x, uint16_t y, typename T_COLOR_FEATURE::ColorObject color) + { + // PROGMEM is read only, this will do nothing + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(uint16_t indexPixel) const + { + if (indexPixel >= PixelCount()) + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + + return T_COLOR_FEATURE::retrievePixelColor_P(_pixels, indexPixel); + }; + + typename T_COLOR_FEATURE::ColorObject GetPixelColor(int16_t x, int16_t y) const + { + if (x < 0 || x >= _width || y < 0 || y >= _height) + { + // Pixel # is out of bounds, this will get converted to a + // color object type initialized to 0 (black) + return 0; + } + + uint16_t indexPixel = x + y * _width; + return T_COLOR_FEATURE::retrievePixelColor_P(_pixels, indexPixel); + }; + + void ClearTo(typename T_COLOR_FEATURE::ColorObject color) + { + // PROGMEM is read only, this will do nothing + }; + + void CopyPixels(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + T_COLOR_FEATURE::movePixelsInc_P(pPixelDest, pPixelSrc, count); + } + + typedef typename T_COLOR_FEATURE::ColorObject ColorObject; + typedef T_COLOR_FEATURE ColorFeature; + +private: + const uint16_t _width; + const uint16_t _height; + PGM_VOID_P _pixels; +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoColorFeatures.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoColorFeatures.h new file mode 100644 index 0000000..9922363 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoColorFeatures.h @@ -0,0 +1,389 @@ +/*------------------------------------------------------------------------- +NeoPixelFeatures provides feature classes to describe color order and +color depth for NeoPixelBus template class + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +class Neo3Elements +{ +public: + static const size_t PixelSize = 3; + + static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + + static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = pPixelSrc[0]; + *pPixelDest++ = pPixelSrc[1]; + *pPixelDest++ = pPixelSrc[2]; + } + } + + static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + while (pPixelDest < pEnd) + { + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + *pPixelDest++ = *pPixelSrc++; + } + } + + static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count) + { + uint8_t* pEnd = pPixelDest + (count * PixelSize); + const uint8_t* pSrc = (const uint8_t*)pPixelSrc; + while (pPixelDest < pEnd) + { + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + *pPixelDest++ = pgm_read_byte(pSrc++); + } + } + + static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint8_t* pDestBack = pPixelDest + (count * PixelSize); + const uint8_t* pSrcBack = pPixelSrc + (count * PixelSize); + while (pDestBack > pPixelDest) + { + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + *--pDestBack = *--pSrcBack; + } + } + + typedef RgbColor ColorObject; +}; + +class Neo4Elements +{ +public: + static const size_t PixelSize = 4; + + static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel) + { + return pPixels + indexPixel * PixelSize; + } + + static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint32_t* pDest = (uint32_t*)pPixelDest; + const uint32_t* pSrc = (const uint32_t*)pPixelSrc; + + uint32_t* pEnd = pDest + count; + while (pDest < pEnd) + { + *pDest++ = *pSrc; + } + } + + static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint32_t* pDest = (uint32_t*)pPixelDest; + const uint32_t* pSrc = (uint32_t*)pPixelSrc; + uint32_t* pEnd = pDest + count; + while (pDest < pEnd) + { + *pDest++ = *pSrc++; + } + } + + static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count) + { + uint32_t* pDest = (uint32_t*)pPixelDest; + const uint32_t* pSrc = (const uint32_t*)pPixelSrc; + uint32_t* pEnd = pDest + count; + while (pDest < pEnd) + { + *pDest++ = pgm_read_dword(pSrc++); + } + } + + static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count) + { + uint32_t* pDest = (uint32_t*)pPixelDest; + const uint32_t* pSrc = (uint32_t*)pPixelSrc; + uint32_t* pDestBack = pDest + count; + const uint32_t* pSrcBack = pSrc + count; + while (pDestBack > pDest) + { + *--pDestBack = *--pSrcBack; + } + } + + typedef RgbwColor ColorObject; +}; + +class NeoGrbFeature : public Neo3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.G; + *p++ = color.R; + *p = color.B; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.G = *p++; + color.R = *p++; + color.B = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p++); + color.B = pgm_read_byte(p); + + return color; + } + +}; + +class NeoGrbwFeature : public Neo4Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.G; + *p++ = color.R; + *p++ = color.B; + *p = color.W; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.G = *p++; + color.R = *p++; + color.B = *p++; + color.W = *p; + + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.G = pgm_read_byte(p++); + color.R = pgm_read_byte(p++); + color.B = pgm_read_byte(p++); + color.W = pgm_read_byte(p); + + return color; + } + +}; + +class NeoRgbwFeature : public Neo4Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.R; + *p++ = color.G; + *p++ = color.B; + *p = color.W; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.R = *p++; + color.G = *p++; + color.B = *p++; + color.W = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.R = pgm_read_byte(p++); + color.G = pgm_read_byte(p++); + color.B = pgm_read_byte(p++); + color.W = pgm_read_byte(p); + + return color; + } + +}; + +class NeoRgbFeature : public Neo3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.R; + *p++ = color.G; + *p = color.B; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.R = *p++; + color.G = *p++; + color.B = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.R = pgm_read_byte(p++); + color.G = pgm_read_byte(p++); + color.B = pgm_read_byte(p); + + return color; + } + +}; + +class NeoBrgFeature : public Neo3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.B; + *p++ = color.R; + *p = color.G; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.B = *p++; + color.R = *p++; + color.G = *p; + + return color; + } + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.B = pgm_read_byte(p++); + color.R = pgm_read_byte(p++); + color.G = pgm_read_byte(p); + + return color; + } + +}; + +class NeoRbgFeature : public Neo3Elements +{ +public: + static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color) + { + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + *p++ = color.R; + *p++ = color.B; + *p = color.G; + } + + static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel) + { + ColorObject color; + uint8_t* p = getPixelAddress(pPixels, indexPixel); + + color.R = *p++; + color.B = *p++; + color.G = *p; + + return color; + } + + + static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel) + { + ColorObject color; + const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel); + + color.R = pgm_read_byte(p++); + color.B = pgm_read_byte(p++); + color.G = pgm_read_byte(p); + + return color; + } + +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoDib.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoDib.h new file mode 100644 index 0000000..ef69ce6 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoDib.h @@ -0,0 +1,161 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +class NeoShaderBase +{ +public: + NeoShaderBase() : + _state(0) + { + } + + bool IsDirty() const + { + return (_state & NEO_DIRTY); + }; + + void Dirty() + { + _state |= NEO_DIRTY; + }; + + void ResetDirty() + { + _state &= ~NEO_DIRTY; + }; + +protected: + uint8_t _state; // internal state +}; + +template class NeoDib +{ +public: + NeoDib(uint16_t countPixels) : + _countPixels(countPixels), + _state(0) + { + _pixels = (T_COLOR_OBJECT*)malloc(PixelsSize()); + ResetDirty(); + } + + ~NeoDib() + { + free((uint8_t*)_pixels); + } + + T_COLOR_OBJECT* Pixels() const + { + return _pixels; + }; + + uint16_t PixelCount() const + { + return _countPixels; + }; + + size_t PixelsSize() const + { + return _countPixels * PixelSize(); + }; + + size_t PixelSize() const + { + return sizeof(T_COLOR_OBJECT); + }; + + void SetPixelColor( + uint16_t indexPixel, + T_COLOR_OBJECT color) + { + if (indexPixel < PixelCount()) + { + _pixels[indexPixel] = color; + Dirty(); + } + }; + + T_COLOR_OBJECT GetPixelColor( + uint16_t indexPixel) const + { + if (indexPixel >= PixelCount()) + { + return 0; + } + return _pixels[indexPixel]; + }; + + void ClearTo(T_COLOR_OBJECT color) + { + for (uint16_t pixel = 0; pixel < PixelCount(); pixel++) + { + _pixels[pixel] = color; + } + Dirty(); + }; + + template void Render(NeoBufferContext destBuffer, T_SHADER& shader) + { + if (IsDirty() || shader.IsDirty()) + { + uint16_t countPixels = destBuffer.PixelCount(); + if (countPixels > _countPixels) + { + countPixels = _countPixels; + } + + for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++) + { + T_COLOR_OBJECT color = shader.Apply(indexPixel, _pixels[indexPixel]); + T_COLOR_FEATURE::applyPixelColor(destBuffer.Pixels, indexPixel, color); + } + + shader.ResetDirty(); + ResetDirty(); + } + } + + bool IsDirty() const + { + return (_state & NEO_DIRTY); + }; + + void Dirty() + { + _state |= NEO_DIRTY; + }; + + void ResetDirty() + { + _state &= ~NEO_DIRTY; + }; + +private: + const uint16_t _countPixels; // Number of RGB LEDs in strip + T_COLOR_OBJECT* _pixels; + uint8_t _state; // internal state +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEase.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEase.h new file mode 100644 index 0000000..eaa5023 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEase.h @@ -0,0 +1,224 @@ +/*------------------------------------------------------------------------- +NeoEase provides animation curve equations for animation support. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#ifdef ARDUINO_ARCH_AVR + +typedef float(*AnimEaseFunction)(float unitValue); + +#else + +#undef max +#undef min +#include +typedef std::function AnimEaseFunction; + +#endif + +class NeoEase +{ +public: + static float Linear(float unitValue) + { + return unitValue; + } + + static float QuadraticIn(float unitValue) + { + return unitValue * unitValue; + } + + static float QuadraticOut(float unitValue) + { + return (-unitValue * (unitValue - 2.0f)); + } + + static float QuadraticInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (0.5f * unitValue * unitValue); + } + else + { + unitValue -= 1.0f; + return (-0.5f * (unitValue * (unitValue - 2.0f) - 1.0f)); + } + } + + static float CubicIn(float unitValue) + { + return (unitValue * unitValue * unitValue); + } + + static float CubicOut(float unitValue) + { + unitValue -= 1.0f; + return (unitValue * unitValue * unitValue + 1); + } + + static float CubicInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (0.5f * unitValue * unitValue * unitValue); + } + else + { + unitValue -= 2.0f; + return (0.5f * (unitValue * unitValue * unitValue + 2.0f)); + } + } + + static float QuarticIn(float unitValue) + { + return (unitValue * unitValue * unitValue * unitValue); + } + + static float QuarticOut(float unitValue) + { + unitValue -= 1.0f; + return -(unitValue * unitValue * unitValue * unitValue - 1); + } + + static float QuarticInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (0.5f * unitValue * unitValue * unitValue * unitValue); + } + else + { + unitValue -= 2.0f; + return (-0.5f * (unitValue * unitValue * unitValue * unitValue - 2.0f)); + } + } + + static float QuinticIn(float unitValue) + { + return (unitValue * unitValue * unitValue * unitValue * unitValue); + } + + static float QuinticOut(float unitValue) + { + unitValue -= 1.0f; + return (unitValue * unitValue * unitValue * unitValue * unitValue + 1.0f); + } + + static float QuinticInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (0.5f * unitValue * unitValue * unitValue * unitValue * unitValue); + } + else + { + unitValue -= 2.0f; + return (0.5f * (unitValue * unitValue * unitValue * unitValue * unitValue + 2.0f)); + } + } + + static float SinusoidalIn(float unitValue) + { + return (-cos(unitValue * HALF_PI) + 1.0f); + } + + static float SinusoidalOut(float unitValue) + { + return (sin(unitValue * HALF_PI)); + } + + static float SinusoidalInOut(float unitValue) + { + return -0.5 * (cos(PI * unitValue) - 1.0f); + } + + static float ExponentialIn(float unitValue) + { + return (pow(2, 10.0f * (unitValue - 1.0f))); + } + + static float ExponentialOut(float unitValue) + { + return (-pow(2, -10.0f * unitValue) + 1.0f); + } + + static float ExponentialInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (0.5f * pow(2, 10.0f * (unitValue - 1.0f))); + } + else + { + unitValue -= 1.0f; + return (0.5f * (-pow(2, -10.0f * unitValue) + 2.0f)); + } + } + + static float CircularIn(float unitValue) + { + if (unitValue == 1.0f) + { + return 1.0f; + } + else + { + return (-(sqrt(1.0f - unitValue * unitValue) - 1.0f)); + } + } + + static float CircularOut(float unitValue) + { + unitValue -= 1.0f; + return (sqrt(1.0f - unitValue * unitValue)); + } + + static float CircularInOut(float unitValue) + { + unitValue *= 2.0f; + if (unitValue < 1.0f) + { + return (-0.5f * (sqrt(1.0f - unitValue * unitValue) - 1)); + } + else + { + unitValue -= 2.0f; + return (0.5f * (sqrt(1.0f - unitValue * unitValue) + 1.0f)); + } + } + + static float Gamma(float unitValue) + { + return pow(unitValue, 1.0f / 0.45f); + } +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp32I2sMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp32I2sMethod.h new file mode 100644 index 0000000..ba5a900 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp32I2sMethod.h @@ -0,0 +1,189 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp32. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#ifdef ARDUINO_ARCH_ESP32 + +extern "C" +{ +#include +#include "Esp32_i2s.h" +} + +const uint16_t c_dmaBytesPerPixelBytes = 4; +const uint16_t c_dmaBytesPer50us = 20; +const uint32_t c_dmaI2sSampleRate = 100000; + +class NeoEsp32I2sSpeedWs2813 +{ +public: + const static uint16_t I2sSampleRateDiv = 1; + const static uint16_t ResetTimeUs = 250; +}; + +class NeoEsp32I2sSpeed800Kbps +{ +public: + const static uint16_t I2sSampleRateDiv = 1; + const static uint16_t ResetTimeUs = 50; +}; + +class NeoEsp32I2sSpeed400Kbps +{ +public: + const static uint16_t I2sSampleRateDiv = 2; + const static uint16_t ResetTimeUs = 50; +}; + +class NeoEsp32I2sBusZero +{ +public: + const static uint8_t I2sBusNumber = 0; +}; + +class NeoEsp32I2sBusOne +{ +public: + const static uint8_t I2sBusNumber = 1; +}; + +template class NeoEsp32I2sMethodBase +{ +public: + NeoEsp32I2sMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : + _pin(pin) + { + uint16_t dmaPixelSize = c_dmaBytesPerPixelBytes * elementSize; + uint16_t resetSize = (c_dmaBytesPer50us * T_SPEED::ResetTimeUs / 50 / T_SPEED::I2sSampleRateDiv); + + _pixelsSize = pixelCount * elementSize; + _i2sBufferSize = pixelCount * dmaPixelSize + resetSize; + + // must have a 4 byte aligned buffer for i2s + uint32_t alignment = _i2sBufferSize % 4; + if (alignment) + { + _i2sBufferSize += 4 - alignment; + } + + _pixels = (uint8_t*)malloc(_pixelsSize); + memset(_pixels, 0x00, _pixelsSize); + + _i2sBuffer = (uint8_t*)malloc(_i2sBufferSize); + memset(_i2sBuffer, 0x00, _i2sBufferSize); + } + + ~NeoEsp32I2sMethodBase() + { + while (!IsReadyToUpdate()) + { + yield(); + } + + pinMode(_pin, INPUT); + + free(_pixels); + free(_i2sBuffer); + } + + bool IsReadyToUpdate() const + { + return (i2sWriteDone(T_BUS::I2sBusNumber)); + } + + void Initialize() + { + i2sInit(T_BUS::I2sBusNumber, 16, c_dmaI2sSampleRate / T_SPEED::I2sSampleRateDiv, I2S_CHAN_STEREO, I2S_FIFO_16BIT_DUAL, 2, 0); + i2sSetPins(T_BUS::I2sBusNumber, _pin, -1, -1, -1); + } + + void Update() + { + // wait for not actively sending data + while (!IsReadyToUpdate()) + { + yield(); + } + + FillBuffers(); + + i2sWrite(T_BUS::I2sBusNumber, _i2sBuffer, _i2sBufferSize, false, false); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _pixelsSize; + } + +private: + const uint8_t _pin; // output pin number + + size_t _pixelsSize; // Size of '_pixels' buffer + uint8_t* _pixels; // Holds LED color values + + uint32_t _i2sBufferSize; // total size of _i2sBuffer + uint8_t* _i2sBuffer; // holds the DMA buffer that is referenced by _i2sBufDesc + + void FillBuffers() + { + const uint16_t bitpatterns[16] = + { + 0b1000100010001000, 0b1000100010001110, 0b1000100011101000, 0b1000100011101110, + 0b1000111010001000, 0b1000111010001110, 0b1000111011101000, 0b1000111011101110, + 0b1110100010001000, 0b1110100010001110, 0b1110100011101000, 0b1110100011101110, + 0b1110111010001000, 0b1110111010001110, 0b1110111011101000, 0b1110111011101110, + }; + + uint16_t* pDma = (uint16_t*)_i2sBuffer; + uint8_t* pPixelsEnd = _pixels + _pixelsSize; + for (uint8_t* pPixel = _pixels; pPixel < pPixelsEnd; pPixel++) + { + *(pDma++) = bitpatterns[((*pPixel) & 0x0f)]; + *(pDma++) = bitpatterns[((*pPixel) >> 4) & 0x0f]; + } + } +}; + +typedef NeoEsp32I2sMethodBase NeoEsp32I2s0Ws2813Method; +typedef NeoEsp32I2sMethodBase NeoEsp32I2s0800KbpsMethod; +typedef NeoEsp32I2sMethodBase NeoEsp32I2s0400KbpsMethod; + +typedef NeoEsp32I2sMethodBase NeoEsp32I2s1Ws2813Method; +typedef NeoEsp32I2sMethodBase NeoEsp32I2s1800KbpsMethod; +typedef NeoEsp32I2sMethodBase NeoEsp32I2s1400KbpsMethod; + +// I2s Bus 1 method is the default method for Esp32 +typedef NeoEsp32I2s1Ws2813Method NeoWs2813Method; +typedef NeoEsp32I2s1800KbpsMethod Neo800KbpsMethod; +typedef NeoEsp32I2s1400KbpsMethod Neo400KbpsMethod; + +#endif \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266DmaMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266DmaMethod.h new file mode 100644 index 0000000..af11e1a --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266DmaMethod.h @@ -0,0 +1,382 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp8266. + + +Written by Michael C. Miller. +Thanks to g3gg0.de for porting the initial DMA support which lead to this. +Thanks to github/cnlohr for the original work on DMA support, which opend +all our minds to a better way (located at https://github.com/cnlohr/esp8266ws2812i2s). + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#ifdef ARDUINO_ARCH_ESP8266 + +extern "C" +{ +#include "Arduino.h" +#include "osapi.h" +#include "ets_sys.h" + +#include "i2s_reg.h" +#include "i2s.h" +#include "eagle_soc.h" +#include "esp8266_peri.h" +#include "slc_register.h" + +#include "osapi.h" +#include "ets_sys.h" +#include "user_interface.h" + + void rom_i2c_writeReg_Mask(uint32_t block, uint32_t host_id, uint32_t reg_add, uint32_t Msb, uint32_t Lsb, uint32_t indata); +} + +struct slc_queue_item +{ + uint32 blocksize : 12; + uint32 datalen : 12; + uint32 unused : 5; + uint32 sub_sof : 1; + uint32 eof : 1; + uint32 owner : 1; + uint32 buf_ptr; + uint32 next_link_ptr; +}; + +class NeoEsp8266DmaSpeedWs2813 +{ +public: + const static uint32_t I2sClockDivisor = 3; + const static uint32_t I2sBaseClockDivisor = 16; + const static uint32_t ResetTimeUs = 250; +}; + +class NeoEsp8266DmaSpeed800Kbps +{ +public: + const static uint32_t I2sClockDivisor = 3; + const static uint32_t I2sBaseClockDivisor = 16; + const static uint32_t ResetTimeUs = 50; +}; + +class NeoEsp8266DmaSpeed400Kbps +{ +public: + const static uint32_t I2sClockDivisor = 6; + const static uint32_t I2sBaseClockDivisor = 16; + const static uint32_t ResetTimeUs = 50; +}; + +enum NeoDmaState +{ + NeoDmaState_Idle, + NeoDmaState_Pending, + NeoDmaState_Sending, +}; +const uint16_t c_maxDmaBlockSize = 4095; +const uint16_t c_dmaBytesPerPixelBytes = 4; +const uint8_t c_I2sPin = 3; // due to I2S hardware, the pin used is restricted to this + +template class NeoEsp8266DmaMethodBase +{ +public: + NeoEsp8266DmaMethodBase(uint16_t pixelCount, size_t elementSize) + { + uint16_t dmaPixelSize = c_dmaBytesPerPixelBytes * elementSize; + + _pixelsSize = pixelCount * elementSize; + _i2sBufferSize = pixelCount * dmaPixelSize; + + _pixels = (uint8_t*)malloc(_pixelsSize); + memset(_pixels, 0x00, _pixelsSize); + + _i2sBuffer = (uint8_t*)malloc(_i2sBufferSize); + memset(_i2sBuffer, 0x00, _i2sBufferSize); + + memset(_i2sZeroes, 0x00, sizeof(_i2sZeroes)); + + _is2BufMaxBlockSize = (c_maxDmaBlockSize / dmaPixelSize) * dmaPixelSize; + + _i2sBufDescCount = (_i2sBufferSize / _is2BufMaxBlockSize) + 1 + 2; // need two more for state/latch blocks + _i2sBufDesc = (slc_queue_item*)malloc(_i2sBufDescCount * sizeof(slc_queue_item)); + + s_this = this; // store this for the ISR + } + + NeoEsp8266DmaMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : NeoEsp8266DmaMethodBase(pixelCount, elementSize) + { + } + + ~NeoEsp8266DmaMethodBase() + { + StopDma(); + + free(_pixels); + free(_i2sBuffer); + free(_i2sBufDesc); + } + + bool IsReadyToUpdate() const + { + return (_dmaState == NeoDmaState_Idle); + } + + void Initialize() + { + StopDma(); + _dmaState = NeoDmaState_Sending; // start off sending empty buffer + + uint8_t* is2Buffer = _i2sBuffer; + uint32_t is2BufferSize = _i2sBufferSize; + uint16_t indexDesc; + + // prepare main data block decriptors that point into our one static dma buffer + for (indexDesc = 0; indexDesc < (_i2sBufDescCount - 2); indexDesc++) + { + uint32_t blockSize = (is2BufferSize > _is2BufMaxBlockSize) ? _is2BufMaxBlockSize : is2BufferSize; + + _i2sBufDesc[indexDesc].owner = 1; + _i2sBufDesc[indexDesc].eof = 0; // no need to trigger interrupt generally + _i2sBufDesc[indexDesc].sub_sof = 0; + _i2sBufDesc[indexDesc].datalen = blockSize; + _i2sBufDesc[indexDesc].blocksize = blockSize; + _i2sBufDesc[indexDesc].buf_ptr = (uint32_t)is2Buffer; + _i2sBufDesc[indexDesc].unused = 0; + _i2sBufDesc[indexDesc].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc + 1]); + + is2Buffer += blockSize; + is2BufferSize -= blockSize; + } + + // prepare the two state/latch descriptors + for (; indexDesc < _i2sBufDescCount; indexDesc++) + { + _i2sBufDesc[indexDesc].owner = 1; + _i2sBufDesc[indexDesc].eof = 0; // no need to trigger interrupt generally + _i2sBufDesc[indexDesc].sub_sof = 0; + _i2sBufDesc[indexDesc].datalen = sizeof(_i2sZeroes); + _i2sBufDesc[indexDesc].blocksize = sizeof(_i2sZeroes); + _i2sBufDesc[indexDesc].buf_ptr = (uint32_t)_i2sZeroes; + _i2sBufDesc[indexDesc].unused = 0; + _i2sBufDesc[indexDesc].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc + 1]); + } + + // the first state block will trigger the interrupt + _i2sBufDesc[indexDesc - 2].eof = 1; + // the last state block will loop to the first state block by defualt + _i2sBufDesc[indexDesc - 1].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc - 2]); + + // setup the rest of i2s DMA + // + ETS_SLC_INTR_DISABLE(); + SLCC0 |= SLCRXLR | SLCTXLR; + SLCC0 &= ~(SLCRXLR | SLCTXLR); + SLCIC = 0xFFFFFFFF; + + // Configure DMA + SLCC0 &= ~(SLCMM << SLCM); // clear DMA MODE + SLCC0 |= (1 << SLCM); // set DMA MODE to 1 + SLCRXDC |= SLCBINR | SLCBTNR; // enable INFOR_NO_REPLACE and TOKEN_NO_REPLACE + SLCRXDC &= ~(SLCBRXFE | SLCBRXEM | SLCBRXFM); // disable RX_FILL, RX_EOF_MODE and RX_FILL_MODE + + // Feed DMA the 1st buffer desc addr + // To send data to the I2S subsystem, counter-intuitively we use the RXLINK part, not the TXLINK as you might + // expect. The TXLINK part still needs a valid DMA descriptor, even if it's unused: the DMA engine will throw + // an error at us otherwise. Just feed it any random descriptor. + SLCTXL &= ~(SLCTXLAM << SLCTXLA); // clear TX descriptor address + SLCTXL |= (uint32)&(_i2sBufDesc[_i2sBufDescCount-1]) << SLCTXLA; // set TX descriptor address. any random desc is OK, we don't use TX but it needs to be valid + SLCRXL &= ~(SLCRXLAM << SLCRXLA); // clear RX descriptor address + SLCRXL |= (uint32)_i2sBufDesc << SLCRXLA; // set RX descriptor address + + ETS_SLC_INTR_ATTACH(i2s_slc_isr, NULL); + SLCIE = SLCIRXEOF; // Enable only for RX EOF interrupt + + ETS_SLC_INTR_ENABLE(); + + //Start transmission + SLCTXL |= SLCTXLS; + SLCRXL |= SLCRXLS; + + pinMode(c_I2sPin, FUNCTION_1); // I2S0_DATA + + I2S_CLK_ENABLE(); + I2SIC = 0x3F; + I2SIE = 0; + + //Reset I2S + I2SC &= ~(I2SRST); + I2SC |= I2SRST; + I2SC &= ~(I2SRST); + + I2SFC &= ~(I2SDE | (I2STXFMM << I2STXFM) | (I2SRXFMM << I2SRXFM)); // Set RX/TX FIFO_MOD=0 and disable DMA (FIFO only) + I2SFC |= I2SDE; //Enable DMA + I2SCC &= ~((I2STXCMM << I2STXCM) | (I2SRXCMM << I2SRXCM)); // Set RX/TX CHAN_MOD=0 + + // set the rate + uint32_t i2s_clock_div = T_SPEED::I2sClockDivisor & I2SCDM; + uint8_t i2s_bck_div = T_SPEED::I2sBaseClockDivisor & I2SBDM; + + //!trans master, !bits mod, rece slave mod, rece msb shift, right first, msb right + I2SC &= ~(I2STSM | (I2SBMM << I2SBM) | (I2SBDM << I2SBD) | (I2SCDM << I2SCD)); + I2SC |= I2SRF | I2SMR | I2SRSM | I2SRMS | (i2s_bck_div << I2SBD) | (i2s_clock_div << I2SCD); + + I2SC |= I2STXS; // Start transmission + } + + void ICACHE_RAM_ATTR Update() + { + // wait for not actively sending data + while (_dmaState != NeoDmaState_Idle) + { + yield(); + } + FillBuffers(); + + // toggle state so the ISR reacts + _dmaState = NeoDmaState_Pending; + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _pixelsSize; + } + +private: + static NeoEsp8266DmaMethodBase* s_this; // for the ISR + + size_t _pixelsSize; // Size of '_pixels' buffer + uint8_t* _pixels; // Holds LED color values + + uint32_t _i2sBufferSize; // total size of _i2sBuffer + uint8_t* _i2sBuffer; // holds the DMA buffer that is referenced by _i2sBufDesc + + // normally 24 bytes creates the minimum 50us latch per spec, but + // with the new logic, this latch is used to space between three states + // buffer size = (24 * (speed / 50)) / 3 + uint8_t _i2sZeroes[(24L * (T_SPEED::ResetTimeUs / 50L)) / 3L]; + + slc_queue_item* _i2sBufDesc; // dma block descriptors + uint16_t _i2sBufDescCount; // count of block descriptors in _i2sBufDesc + uint16_t _is2BufMaxBlockSize; // max size based on size of a pixel of a single block + + volatile NeoDmaState _dmaState; + + // This routine is called as soon as the DMA routine has something to tell us. All we + // handle here is the RX_EOF_INT status, which indicate the DMA has sent a buffer whose + // descriptor has the 'EOF' field set to 1. + // in the case of this code, the second to last state descriptor + volatile static void ICACHE_RAM_ATTR i2s_slc_isr(void) + { + uint32_t slc_intr_status = SLCIS; + + SLCIC = 0xFFFFFFFF; + + if (slc_intr_status & SLCIRXEOF) + { + ETS_SLC_INTR_DISABLE(); + + switch (s_this->_dmaState) + { + case NeoDmaState_Idle: + break; + + case NeoDmaState_Pending: + { + slc_queue_item* finished_item = (slc_queue_item*)SLCRXEDA; + + // data block has pending data waiting to send, prepare it + // point last state block to top + (finished_item + 1)->next_link_ptr = (uint32_t)(s_this->_i2sBufDesc); + + s_this->_dmaState = NeoDmaState_Sending; + } + break; + + case NeoDmaState_Sending: + { + slc_queue_item* finished_item = (slc_queue_item*)SLCRXEDA; + + // the data block had actual data sent + // point last state block to first state block thus + // just looping and not sending the data blocks + (finished_item + 1)->next_link_ptr = (uint32_t)(finished_item); + + s_this->_dmaState = NeoDmaState_Idle; + } + break; + } + + + ETS_SLC_INTR_ENABLE(); + } + } + + void FillBuffers() + { + const uint16_t bitpatterns[16] = + { + 0b1000100010001000, 0b1000100010001110, 0b1000100011101000, 0b1000100011101110, + 0b1000111010001000, 0b1000111010001110, 0b1000111011101000, 0b1000111011101110, + 0b1110100010001000, 0b1110100010001110, 0b1110100011101000, 0b1110100011101110, + 0b1110111010001000, 0b1110111010001110, 0b1110111011101000, 0b1110111011101110, + }; + + uint16_t* pDma = (uint16_t*)_i2sBuffer; + uint8_t* pPixelsEnd = _pixels + _pixelsSize; + for (uint8_t* pPixel = _pixels; pPixel < pPixelsEnd; pPixel++) + { + *(pDma++) = bitpatterns[((*pPixel) & 0x0f)]; + *(pDma++) = bitpatterns[((*pPixel) >> 4) & 0x0f]; + } + } + + void StopDma() + { + ETS_SLC_INTR_DISABLE(); + SLCIC = 0xFFFFFFFF; + SLCIE = 0; + SLCTXL &= ~(SLCTXLAM << SLCTXLA); // clear TX descriptor address + SLCRXL &= ~(SLCRXLAM << SLCRXLA); // clear RX descriptor address + + pinMode(c_I2sPin, INPUT); + } +}; + +template +NeoEsp8266DmaMethodBase* NeoEsp8266DmaMethodBase::s_this; + +typedef NeoEsp8266DmaMethodBase NeoEsp8266DmaWs2813Method; +typedef NeoEsp8266DmaMethodBase NeoEsp8266Dma800KbpsMethod; +typedef NeoEsp8266DmaMethodBase NeoEsp8266Dma400KbpsMethod; + +// Dma method is the default method for Esp8266 +typedef NeoEsp8266DmaWs2813Method NeoWs2813Method; +typedef NeoEsp8266Dma800KbpsMethod Neo800KbpsMethod; +typedef NeoEsp8266Dma400KbpsMethod Neo400KbpsMethod; + +#endif \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.cpp new file mode 100644 index 0000000..7bfc3e0 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.cpp @@ -0,0 +1,216 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp8266 UART hardware + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#ifdef ARDUINO_ARCH_ESP8266 +#include "NeoEsp8266UartMethod.h" +#include +extern "C" +{ + #include + #include + #include + #include +} + +#define UART1 1 +#define UART1_INV_MASK (0x3f << 19) + +// Gets the number of bytes waiting in the TX FIFO of UART1 +static inline uint8_t getUartTxFifoLength() +{ + return (U1S >> USTXC) & 0xff; +} + +// Append a byte to the TX FIFO of UART1 +// You must ensure the TX FIFO isn't full +static inline void enqueue(uint8_t byte) +{ + U1F = byte; +} + +static const uint8_t* esp8266_uart1_async_buf; +static const uint8_t* esp8266_uart1_async_buf_end; + +NeoEsp8266Uart::NeoEsp8266Uart(uint16_t pixelCount, size_t elementSize) +{ + _sizePixels = pixelCount * elementSize; + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0x00, _sizePixels); +} + +NeoEsp8266Uart::~NeoEsp8266Uart() +{ + // Wait until the TX fifo is empty. This way we avoid broken frames + // when destroying & creating a NeoPixelBus to change its length. + while (getUartTxFifoLength() > 0) + { + yield(); + } + + free(_pixels); +} + +void NeoEsp8266Uart::InitializeUart(uint32_t uartBaud) +{ + // Configure the serial line with 1 start bit (0), 6 data bits and 1 stop bit (1) + Serial1.begin(uartBaud, SERIAL_6N1, SERIAL_TX_ONLY); + + // Invert the TX voltage associated with logic level so: + // - A logic level 0 will generate a Vcc signal + // - A logic level 1 will generate a Gnd signal + CLEAR_PERI_REG_MASK(UART_CONF0(UART1), UART1_INV_MASK); + SET_PERI_REG_MASK(UART_CONF0(UART1), (BIT(22))); +} + +void NeoEsp8266Uart::UpdateUart() +{ + // Since the UART can finish sending queued bytes in the FIFO in + // the background, instead of waiting for the FIFO to flush + // we annotate the start time of the frame so we can calculate + // when it will finish. + _startTime = micros(); + + // Then keep filling the FIFO until done + const uint8_t* ptr = _pixels; + const uint8_t* end = ptr + _sizePixels; + while (ptr != end) + { + ptr = FillUartFifo(ptr, end); + } +} + +const uint8_t* ICACHE_RAM_ATTR NeoEsp8266Uart::FillUartFifo(const uint8_t* pixels, const uint8_t* end) +{ + // Remember: UARTs send less significant bit (LSB) first so + // pushing ABCDEF byte will generate a 0FEDCBA1 signal, + // including a LOW(0) start & a HIGH(1) stop bits. + // Also, we have configured UART to invert logic levels, so: + const uint8_t _uartData[4] = { + 0b110111, // On wire: 1 000 100 0 [Neopixel reads 00] + 0b000111, // On wire: 1 000 111 0 [Neopixel reads 01] + 0b110100, // On wire: 1 110 100 0 [Neopixel reads 10] + 0b000100, // On wire: 1 110 111 0 [NeoPixel reads 11] + }; + uint8_t avail = (UART_TX_FIFO_SIZE - getUartTxFifoLength()) / 4; + if (end - pixels > avail) + { + end = pixels + avail; + } + while (pixels < end) + { + uint8_t subpix = *pixels++; + enqueue(_uartData[(subpix >> 6) & 0x3]); + enqueue(_uartData[(subpix >> 4) & 0x3]); + enqueue(_uartData[(subpix >> 2) & 0x3]); + enqueue(_uartData[ subpix & 0x3]); + } + return pixels; +} + +NeoEsp8266AsyncUart::NeoEsp8266AsyncUart(uint16_t pixelCount, size_t elementSize) + : NeoEsp8266Uart(pixelCount, elementSize) +{ + _asyncPixels = (uint8_t*)malloc(_sizePixels); +} + +NeoEsp8266AsyncUart::~NeoEsp8266AsyncUart() +{ + // Remember: the UART interrupt can be sending data from _asyncPixels in the background + while (esp8266_uart1_async_buf != esp8266_uart1_async_buf_end) + { + yield(); + } + free(_asyncPixels); +} + +void ICACHE_RAM_ATTR NeoEsp8266AsyncUart::InitializeUart(uint32_t uartBaud) +{ + NeoEsp8266Uart::InitializeUart(uartBaud); + + // Disable all interrupts + ETS_UART_INTR_DISABLE(); + + // Clear the RX & TX FIFOS + SET_PERI_REG_MASK(UART_CONF0(UART1), UART_RXFIFO_RST | UART_TXFIFO_RST); + CLEAR_PERI_REG_MASK(UART_CONF0(UART1), UART_RXFIFO_RST | UART_TXFIFO_RST); + + // Set the interrupt handler + ETS_UART_INTR_ATTACH(IntrHandler, NULL); + + // Set tx fifo trigger. 80 bytes gives us 200 microsecs to refill the FIFO + WRITE_PERI_REG(UART_CONF1(UART1), 80 << UART_TXFIFO_EMPTY_THRHD_S); + + // Disable RX & TX interrupts. It is enabled by uart.c in the SDK + CLEAR_PERI_REG_MASK(UART_INT_ENA(UART1), UART_RXFIFO_FULL_INT_ENA | UART_TXFIFO_EMPTY_INT_ENA); + + // Clear all pending interrupts in UART1 + WRITE_PERI_REG(UART_INT_CLR(UART1), 0xffff); + + // Reenable interrupts + ETS_UART_INTR_ENABLE(); +} + +void NeoEsp8266AsyncUart::UpdateUart() +{ + // Instruct ESP8266 hardware uart1 to send the pixels asynchronously + esp8266_uart1_async_buf = _pixels; + esp8266_uart1_async_buf_end = _pixels + _sizePixels; + SET_PERI_REG_MASK(UART_INT_ENA(1), UART_TXFIFO_EMPTY_INT_ENA); + + // Annotate when we started to send bytes, so we can calculate when we are ready to send again + _startTime = micros(); + + // Copy the pixels to the idle buffer and swap them + memcpy(_asyncPixels, _pixels, _sizePixels); + std::swap(_asyncPixels, _pixels); +} + +void ICACHE_RAM_ATTR NeoEsp8266AsyncUart::IntrHandler(void* param) +{ + // Interrupt handler is shared between UART0 & UART1 + if (READ_PERI_REG(UART_INT_ST(UART1))) //any UART1 stuff + { + // Fill the FIFO with new data + esp8266_uart1_async_buf = FillUartFifo(esp8266_uart1_async_buf, esp8266_uart1_async_buf_end); + // Disable TX interrupt when done + if (esp8266_uart1_async_buf == esp8266_uart1_async_buf_end) + { + CLEAR_PERI_REG_MASK(UART_INT_ENA(UART1), UART_TXFIFO_EMPTY_INT_ENA); + } + // Clear all interrupts flags (just in case) + WRITE_PERI_REG(UART_INT_CLR(UART1), 0xffff); + } + + if (READ_PERI_REG(UART_INT_ST(UART0))) + { + // TODO: gdbstub uses the interrupt of UART0, but there is no way to call its + // interrupt handler gdbstub_uart_hdlr since it's static. + WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff); + } +} + +#endif + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.h new file mode 100644 index 0000000..a92d566 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEsp8266UartMethod.h @@ -0,0 +1,178 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp8266 UART hardware + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#ifdef ARDUINO_ARCH_ESP8266 +#include + +// NeoEsp8266Uart contains all the low level details that doesn't +// depend on the transmission speed, and therefore, it isn't a template +class NeoEsp8266Uart +{ +protected: + NeoEsp8266Uart(uint16_t pixelCount, size_t elementSize); + + ~NeoEsp8266Uart(); + + void InitializeUart(uint32_t uartBaud); + + void UpdateUart(); + + static const uint8_t* ICACHE_RAM_ATTR FillUartFifo(const uint8_t* pixels, const uint8_t* end); + + size_t _sizePixels; // Size of '_pixels' buffer below + uint8_t* _pixels; // Holds LED color values + uint32_t _startTime; // Microsecond count when last update started +}; + +// NeoEsp8266AsyncUart handles all transmission asynchronously using interrupts +// +// This UART controller uses two buffers that are swapped in every call to +// NeoPixelBus.Show(). One buffer contains the data that is being sent +// asynchronosly and another buffer contains the data that will be send +// in the next call to NeoPixelBus.Show(). +// +// Therefore, the result of NeoPixelBus.Pixels() is invalidated after +// every call to NeoPixelBus.Show() and must not be cached. +class NeoEsp8266AsyncUart: public NeoEsp8266Uart +{ +protected: + NeoEsp8266AsyncUart(uint16_t pixelCount, size_t elementSize); + + ~NeoEsp8266AsyncUart(); + + void InitializeUart(uint32_t uartBaud); + + void UpdateUart(); + +private: + static void ICACHE_RAM_ATTR IntrHandler(void* param); + + uint8_t* _asyncPixels; // Holds a copy of LED color values taken when UpdateUart began +}; + +// NeoEsp8266UartSpeedWs2813 contains the timing constants used to get NeoPixelBus running with the Ws2813 +class NeoEsp8266UartSpeedWs2813 +{ +public: + static const uint32_t ByteSendTimeUs = 10; // us it takes to send a single pixel element at 800khz speed + static const uint32_t UartBaud = 3200000; // 800mhz, 4 serial bytes per NeoByte + static const uint32_t ResetTimeUs = 250; // us between data send bursts to reset for next update +}; + +// NeoEsp8266UartSpeed800Kbps contains the timing constant used to get NeoPixelBus running at 800Khz +class NeoEsp8266UartSpeed800Kbps +{ +public: + static const uint32_t ByteSendTimeUs = 10; // us it takes to send a single pixel element at 800khz speed + static const uint32_t UartBaud = 3200000; // 800mhz, 4 serial bytes per NeoByte + static const uint32_t ResetTimeUs = 50; // us between data send bursts to reset for next update +}; + +// NeoEsp8266UartSpeed800Kbps contains the timing constant used to get NeoPixelBus running at 400Khz +class NeoEsp8266UartSpeed400Kbps +{ +public: + static const uint32_t ByteSendTimeUs = 20; // us it takes to send a single pixel element at 400khz speed + static const uint32_t UartBaud = 1600000; // 400mhz, 4 serial bytes per NeoByte + static const uint32_t ResetTimeUs = 50; // us between data send bursts to reset for next update +}; + +// NeoEsp8266UartMethodBase is a light shell arround NeoEsp8266Uart or NeoEsp8266AsyncUart that +// implements the methods needed to operate as a NeoPixelBus method. +template +class NeoEsp8266UartMethodBase: public T_BASE +{ +public: + NeoEsp8266UartMethodBase(uint16_t pixelCount, size_t elementSize) + : T_BASE(pixelCount, elementSize) + { + } + NeoEsp8266UartMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) + : T_BASE(pixelCount, elementSize) + { + } + + bool IsReadyToUpdate() const + { + uint32_t delta = micros() - this->_startTime; + return delta >= getPixelTime() + T_SPEED::ResetTimeUs; + } + + void Initialize() + { + this->InitializeUart(T_SPEED::UartBaud); + + // Inverting logic levels can generate a phantom bit in the led strip bus + // We need to delay 50+ microseconds the output stream to force a data + // latch and discard this bit. Otherwise, that bit would be prepended to + // the first frame corrupting it. + this->_startTime = micros() - getPixelTime(); + } + + void Update() + { + // Data latch = 50+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while (!this->IsReadyToUpdate()) + { + yield(); + } + this->UpdateUart(); + } + + uint8_t* getPixels() const + { + return this->_pixels; + }; + + size_t getPixelsSize() const + { + return this->_sizePixels; + }; + +private: + uint32_t getPixelTime() const + { + return (T_SPEED::ByteSendTimeUs * this->_sizePixels); + }; +}; + +typedef NeoEsp8266UartMethodBase NeoEsp8266UartWs2813Method; +typedef NeoEsp8266UartMethodBase NeoEsp8266Uart800KbpsMethod; +typedef NeoEsp8266UartMethodBase NeoEsp8266Uart400KbpsMethod; + +typedef NeoEsp8266UartMethodBase NeoEsp8266AsyncUartWs2813Method; +typedef NeoEsp8266UartMethodBase NeoEsp8266AsyncUart800KbpsMethod; +typedef NeoEsp8266UartMethodBase NeoEsp8266AsyncUart400KbpsMethod; + +#endif + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEspBitBangMethod.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEspBitBangMethod.h new file mode 100644 index 0000000..a398f2b --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoEspBitBangMethod.h @@ -0,0 +1,173 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp8266 and Esp32 + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +// ESP32 doesn't define ICACHE_RAM_ATTR +#ifndef ICACHE_RAM_ATTR +#define ICACHE_RAM_ATTR IRAM_ATTR +#endif + +// for esp8266, due to linker overriding the ICACHE_RAM_ATTR for cpp files, these methods are +// moved into a C file so the attribute will be applied correctly +// >> this may have been fixed and is no longer a requirement << +extern "C" void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin); +extern "C" void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin); + +class NeoEspBitBangSpeedWs2813 +{ +public: + static void send_pixels(uint8_t* pixels, uint8_t* end, uint8_t pin) + { + bitbang_send_pixels_800(pixels, end, pin); + } + static const uint32_t ResetTimeUs = 250; +}; + +class NeoEspBitBangSpeed800Kbps +{ +public: + static void send_pixels(uint8_t* pixels, uint8_t* end, uint8_t pin) + { + bitbang_send_pixels_800(pixels, end, pin); + } + static const uint32_t ResetTimeUs = 50; +}; + +class NeoEspBitBangSpeed400Kbps +{ +public: + static void send_pixels(uint8_t* pixels, uint8_t* end, uint8_t pin) + { + bitbang_send_pixels_400(pixels, end, pin); + } + static const uint32_t ResetTimeUs = 50; +}; + +template class NeoEspBitBangMethodBase +{ +public: + NeoEspBitBangMethodBase(uint8_t pin, uint16_t pixelCount, size_t elementSize) : + _pin(pin) + { + pinMode(pin, OUTPUT); + + _sizePixels = pixelCount * elementSize; + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + } + + ~NeoEspBitBangMethodBase() + { + pinMode(_pin, INPUT); + + free(_pixels); + } + + bool IsReadyToUpdate() const + { + uint32_t delta = micros() - _endTime; + + return (delta >= T_SPEED::ResetTimeUs); + } + + void Initialize() + { + digitalWrite(_pin, LOW); + + _endTime = micros(); + } + + void Update() + { + // Data latch = 50+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while (!IsReadyToUpdate()) + { + yield(); // allows for system yield if needed + } + + // Need 100% focus on instruction timing +#if defined(ARDUINO_ARCH_ESP32) + delay(1); // required + portMUX_TYPE updateMux = portMUX_INITIALIZER_UNLOCKED; + + taskENTER_CRITICAL(&updateMux); +#else + noInterrupts(); +#endif + + T_SPEED::send_pixels(_pixels, _pixels + _sizePixels, _pin); + +#if defined(ARDUINO_ARCH_ESP32) + taskEXIT_CRITICAL(&updateMux); +#else + interrupts(); +#endif + + // save EOD time for latch on next call + _endTime = micros(); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + uint32_t _endTime; // Latch timing reference + size_t _sizePixels; // Size of '_pixels' buffer below + uint8_t* _pixels; // Holds LED color values + uint8_t _pin; // output pin number +}; + + +#if defined(ARDUINO_ARCH_ESP32) + +typedef NeoEspBitBangMethodBase NeoEsp32BitBangWs2813Method; +typedef NeoEspBitBangMethodBase NeoEsp32BitBang800KbpsMethod; +typedef NeoEspBitBangMethodBase NeoEsp32BitBang400KbpsMethod; + +#else + +typedef NeoEspBitBangMethodBase NeoEsp8266BitBangWs2813Method; +typedef NeoEspBitBangMethodBase NeoEsp8266BitBang800KbpsMethod; +typedef NeoEspBitBangMethodBase NeoEsp8266BitBang400KbpsMethod; + +#endif + +#endif \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.cpp new file mode 100644 index 0000000..f33cbfb --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.cpp @@ -0,0 +1,50 @@ +/*------------------------------------------------------------------------- +NeoPixelGamma class is used to correct RGB colors for human eye gamma levels + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include +#include "RgbColor.h" +#include "RgbwColor.h" +#include "NeoEase.h" +#include "NeoGamma.h" + +const uint8_t NeoGammaTableMethod::_table[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, + 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, + 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, + 19, 20, 20, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, + 29, 30, 30, 31, 32, 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, + 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, + 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 86, 87, 88, 89, + 91, 92, 93, 94, 96, 97, 98, 100, 101, 102, 104, 105, 106, 108, 109, 110, + 112, 113, 115, 116, 118, 119, 121, 122, 123, 125, 126, 128, 130, 131, 133, 134, + 136, 137, 139, 140, 142, 144, 145, 147, 149, 150, 152, 154, 155, 157, 159, 160, + 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 187, 189, + 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, + 223, 225, 227, 229, 231, 233, 235, 238, 240, 242, 244, 246, 248, 251, 253, 255 +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.h new file mode 100644 index 0000000..9b94e24 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoGamma.h @@ -0,0 +1,73 @@ +/*------------------------------------------------------------------------- +NeoPixelGamma class is used to correct RGB colors for human eye gamma levels + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +// NeoGammaEquationMethod uses no memory but is slower than NeoGammaTableMethod +class NeoGammaEquationMethod +{ +public: + static uint8_t Correct(uint8_t value) + { + return static_cast(255.0f * NeoEase::Gamma(value / 255.0f) + 0.5f); + } +}; + +// NeoGammaTableMethod uses 256 bytes of memory, but is significantly faster +class NeoGammaTableMethod +{ +public: + static uint8_t Correct(uint8_t value) + { + return _table[value]; + } + +private: + static const uint8_t _table[256]; +}; + + +// use one of the method classes above as a converter for this template class +template class NeoGamma +{ +public: + RgbColor Correct(const RgbColor& original) + { + return RgbColor(T_METHOD::Correct(original.R), + T_METHOD::Correct(original.G), + T_METHOD::Correct(original.B)); + } + + RgbwColor Correct(const RgbwColor& original) + { + return RgbwColor(T_METHOD::Correct(original.R), + T_METHOD::Correct(original.G), + T_METHOD::Correct(original.B), + T_METHOD::Correct(original.W) ); + } +}; + + + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoHueBlend.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoHueBlend.h new file mode 100644 index 0000000..d77a58f --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoHueBlend.h @@ -0,0 +1,118 @@ +/*------------------------------------------------------------------------- +NeoHueBlend provides method objects that can be directly consumed by +blend template functions in HslColor and HsbColor + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +class NeoHueBlendBase +{ +protected: + static float FixWrap(float value) + { + if (value < 0.0f) + { + value += 1.0f; + } + else if (value > 1.0f) + { + value -= 1.0f; + } + return value; + } +}; + +class NeoHueBlendShortestDistance : NeoHueBlendBase +{ +public: + static float HueBlend(float left, float right, float progress) + { + float delta = right - left; + float base = left; + if (delta > 0.5f) + { + base = right; + delta = 1.0f - delta; + progress = 1.0f - progress; + } + else if (delta < -0.5f) + { + delta = 1.0f + delta; + } + return FixWrap(base + (delta) * progress); + }; +}; + +class NeoHueBlendLongestDistance : NeoHueBlendBase +{ +public: + static float HueBlend(float left, float right, float progress) + { + float delta = right - left; + float base = left; + if (delta < 0.5f && delta >= 0.0f) + { + base = right; + delta = 1.0f - delta; + progress = 1.0f - progress; + } + else if (delta > -0.5f && delta < 0.0f) + { + delta = 1.0f + delta; + } + return FixWrap(base + delta * progress); + }; +}; + +class NeoHueBlendClockwiseDirection : NeoHueBlendBase +{ +public: + static float HueBlend(float left, float right, float progress) + { + float delta = right - left; + float base = left; + if (delta < 0.0f) + { + delta = 1.0f + delta; + } + + return FixWrap(base + delta * progress); + }; +}; + +class NeoHueBlendCounterClockwiseDirection : NeoHueBlendBase +{ +public: + static float HueBlend(float left, float right, float progress) + { + float delta = right - left; + float base = left; + if (delta > 0.0f) + { + delta = delta - 1.0f; + } + + return FixWrap(base + delta * progress); + }; +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoMosaic.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoMosaic.h new file mode 100644 index 0000000..551bd53 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoMosaic.h @@ -0,0 +1,191 @@ +#pragma once + +/*------------------------------------------------------------------------- +Mosiac provides a mapping feature of a 2d cordinate to linear 1d cordinate +It is used to map tiles of matricies of NeoPixels to a index on the NeoPixelBus +where the the matricies use a set of prefered topology and the tiles of +those matricies use the RowMajorAlternating layout + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + + +//----------------------------------------------------------------------------- +// class NeoMosaic +// Complex Tile layout class that reduces distance of the interconnects between +// the tiles by using different rotations of the layout at specific locations +// +// T_LAYOUT = the layout used for matrix panel (rotation is ignored) +// +// NOTE: The tiles in the mosaic are always laid out using RowMajorAlternating +// +//----------------------------------------------------------------------------- + +template class NeoMosaic +{ +public: + NeoMosaic(uint16_t topoWidth, uint16_t topoHeight, + uint16_t mosaicWidth, uint16_t mosaicHeight) : + _topoWidth(topoWidth), + _topoHeight(topoHeight), + _mosaicWidth(mosaicWidth), + _mosaicHeight(mosaicHeight) + { + } + + uint16_t Map(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x >= totalWidth) + { + x = totalWidth - 1; + } + else if (x < 0) + { + x = 0; + } + + if (y >= totalHeight) + { + y = totalHeight - 1; + } + else if (y < 0) + { + y = 0; + } + + uint16_t localIndex; + uint16_t tileOffset; + + calculate(x, y, &localIndex, &tileOffset); + + return localIndex + tileOffset; + } + + uint16_t MapProbe(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight) + { + return totalWidth * totalHeight; // count, out of bounds + } + + uint16_t localIndex; + uint16_t tileOffset; + + calculate(x, y, &localIndex, &tileOffset); + + return localIndex + tileOffset; + } + + NeoTopologyHint TopologyHint(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight) + { + return NeoTopologyHint_OutOfBounds; + } + + uint16_t localIndex; + uint16_t tileOffset; + NeoTopologyHint result; + + calculate(x, y, &localIndex, &tileOffset); + + if (localIndex == 0) + { + result = NeoTopologyHint_FirstOnPanel; + } + else if (localIndex == (_topoWidth * _topoHeight - 1)) + { + result = NeoTopologyHint_LastOnPanel; + } + else + { + result = NeoTopologyHint_InPanel; + } + + return result; + } + + uint16_t getWidth() const + { + return _topoWidth * _mosaicWidth; + } + + uint16_t getHeight() const + { + return _topoHeight * _mosaicHeight; + } + +private: + const uint16_t _topoWidth; + const uint16_t _topoHeight; + const uint16_t _mosaicWidth; + const uint16_t _mosaicHeight; + + void calculate(uint16_t x, uint16_t y, uint16_t* pLocalIndex, uint16_t* pTileOffset) const + { + uint16_t tileX = x / _topoWidth; + uint16_t topoX = x % _topoWidth; + + uint16_t tileY = y / _topoHeight; + uint16_t topoY = y % _topoHeight; + + *pTileOffset = RowMajorAlternatingLayout::Map(_mosaicWidth, + _mosaicHeight, + tileX, + tileY) * _topoWidth * _topoHeight; + + if (tileX & 0x0001) + { + // odd columns + if (tileY & 0x0001) + { + *pLocalIndex = T_LAYOUT::OddRowOddColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY); + } + else + { + *pLocalIndex = T_LAYOUT::EvenRowOddColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY); + } + } + else + { + // even columns + if (tileY & 0x0001) + { + *pLocalIndex = T_LAYOUT::OddRowEvenColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY); + } + else + { + *pLocalIndex = T_LAYOUT::EvenRowEvenColumnLayout::Map(_topoWidth, _topoHeight, topoX, topoY); + } + } + } +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAnimator.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAnimator.cpp new file mode 100644 index 0000000..db51a5d --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAnimator.cpp @@ -0,0 +1,166 @@ +/*------------------------------------------------------------------------- +NeoPixelAnimator provides animation timing support. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "NeoPixelAnimator.h" + +NeoPixelAnimator::NeoPixelAnimator(uint16_t countAnimations, uint16_t timeScale) : + _countAnimations(countAnimations), + _animationLastTick(0), + _activeAnimations(0), + _isRunning(true) +{ + setTimeScale(timeScale); + _animations = new AnimationContext[_countAnimations]; +} + +NeoPixelAnimator::~NeoPixelAnimator() +{ + delete[] _animations; +} + +bool NeoPixelAnimator::NextAvailableAnimation(uint16_t* indexAvailable, uint16_t indexStart) +{ + if (indexStart >= _countAnimations) + { + // last one + indexStart = _countAnimations - 1; + } + + uint16_t next = indexStart; + + do + { + if (!IsAnimationActive(next)) + { + if (indexAvailable) + { + *indexAvailable = next; + } + return true; + } + next = (next + 1) % _countAnimations; + } while (next != indexStart); + return false; +} + +void NeoPixelAnimator::StartAnimation(uint16_t indexAnimation, + uint16_t duration, + AnimUpdateCallback animUpdate) +{ + if (indexAnimation >= _countAnimations || animUpdate == NULL) + { + return; + } + + if (_activeAnimations == 0) + { + _animationLastTick = millis(); + } + + StopAnimation(indexAnimation); + + // all animations must have at least non zero duration, otherwise + // they are considered stopped + if (duration == 0) + { + duration = 1; + } + + _animations[indexAnimation].StartAnimation(duration, animUpdate); + + _activeAnimations++; +} + +void NeoPixelAnimator::StopAnimation(uint16_t indexAnimation) +{ + if (indexAnimation >= _countAnimations) + { + return; + } + + if (IsAnimationActive(indexAnimation)) + { + _activeAnimations--; + _animations[indexAnimation].StopAnimation(); + } +} + +void NeoPixelAnimator::StopAll() +{ + for (uint16_t indexAnimation = 0; indexAnimation < _countAnimations; ++indexAnimation) + { + _animations[indexAnimation].StopAnimation(); + } + _activeAnimations = 0; +} + + +void NeoPixelAnimator::UpdateAnimations() +{ + if (_isRunning) + { + uint32_t currentTick = millis(); + uint32_t delta = currentTick - _animationLastTick; + + if (delta >= _timeScale) + { + AnimationContext* pAnim; + + delta /= _timeScale; // scale delta into animation time + + for (uint16_t iAnim = 0; iAnim < _countAnimations; iAnim++) + { + pAnim = &_animations[iAnim]; + AnimUpdateCallback fnUpdate = pAnim->_fnCallback; + AnimationParam param; + + param.index = iAnim; + + if (pAnim->_remaining > delta) + { + param.state = (pAnim->_remaining == pAnim->_duration) ? AnimationState_Started : AnimationState_Progress; + param.progress = (float)(pAnim->_duration - pAnim->_remaining) / (float)pAnim->_duration; + + fnUpdate(param); + + pAnim->_remaining -= delta; + } + else if (pAnim->_remaining > 0) + { + param.state = AnimationState_Completed; + param.progress = 1.0f; + + _activeAnimations--; + pAnim->StopAnimation(); + + fnUpdate(param); + } + } + + _animationLastTick = currentTick; + } + } +} diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAvr.c b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAvr.c new file mode 100644 index 0000000..761fa7e --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelAvr.c @@ -0,0 +1,648 @@ +/*------------------------------------------------------------------------- +Arduino library to control a wide variety of WS2811- and WS2812-based RGB +LED devices such as Adafruit FLORA RGB Smart Pixels and NeoPixel strips. +Currently handles 400 and 800 KHz bitstreams on 8, 12 and 16 MHz ATmega +MCUs, with LEDs wired for various color orders. 8 MHz MCUs provide +output on PORTB and PORTD, while 16 MHz chips can handle most output pins +(possible exception with upper PORT registers on the Arduino Mega). + +Written by Phil Burgess / Paint Your Dragon for Adafruit Industries, +contributions by PJRC, Michael Miller and other members of the open +source community. + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing products +from Adafruit! + +------------------------------------------------------------------------- +The contents of this file were taken from the Adafruit NeoPixel library +and modified only to fit within individual calling functions. + +NeoPixel 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 3 of +the License, or (at your option) any later version. + +NeoPixel is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +// must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set +#if defined(ARDUINO_ARCH_AVR) && !defined(__arm__) + +#include + +// Hand-tuned assembly code issues data to the LED drivers at a specific +// rate. There's separate code for different CPU speeds (8, 12, 16 MHz) +// for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The +// datastream timing for the LED drivers allows a little wiggle room each +// way (listed in the datasheets), so the conditions for compiling each +// case are set up for a range of frequencies rather than just the exact +// 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on +// devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based +// on the datasheet figures and have not been extensively tested outside +// the canonical 8/12/16 MHz speeds; there's no guarantee these will work +// close to the extremes (or possibly they could be pushed further). +// Keep in mind only one CPU speed case actually gets compiled; the +// resulting program isn't as massive as it might look from source here. + +#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) // 8Mhz CPU + +#ifdef PORTD // PORTD isn't present on ATtiny85, etc. +void send_pixels_8mhz_800_PortD(uint8_t* pixels, size_t sizePixels, uint8_t pinMask) +{ + volatile size_t i = sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + volatile uint8_t n1; + volatile uint8_t n2 = 0; // First, next bits out + + // Squeezing an 800 KHz stream out of an 8 MHz chip requires code + // specific to each PORT register. At present this is only written + // to work with pins on PORTD or PORTB, the most likely use case -- + // this covers all the pins on the Adafruit Flora and the bulk of + // digital pins on the Arduino Pro 8 MHz (keep in mind, this code + // doesn't even get compiled for 16 MHz boards like the Uno, Mega, + // Leonardo, etc., so don't bother extending this out of hand). + // Additional PORTs could be added if you really need them, just + // duplicate the else and loop and change the PORT. Each add'l + // PORT will require about 150(ish) bytes of program space. + + // 10 instruction clocks per bit: HHxxxxxLLL + // OUT instructions: ^ ^ ^ (T=0,2,7) + + hi = PORTD | pinMask; + lo = PORTD & ~pinMask; + n1 = lo; + + if (b & 0x80) + { + n1 = hi; + } + + // Dirty trick: RJMPs proceeding to the next instruction are used + // to delay two clock cycles in one instruction word (rather than + // using two NOPs). This was necessary in order to squeeze the + // loop down to exactly 64 words -- the maximum possible for a + // relative branch. + + asm volatile( + "headD:" "\n\t" // Clk Pseudocode + // Bit 7: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 6: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 5: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 4: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 3: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 2: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 1: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "out %[port] , %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet) + // Bit 0: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "out %[port] , %[n2]" "\n\t" // 1 PORT = n2 + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo + "brne headD" "\n" // 2 while(i) (Z flag set above) + : [byte] "+r" (b), + [n1] "+r" (n1), + [n2] "+r" (n2), + [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTD)), + [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo) ); +} +#endif + +void send_pixels_8mhz_800_PortB(uint8_t* pixels, size_t sizePixels, uint8_t pinMask) +{ + volatile size_t i = sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + volatile uint8_t n1; + volatile uint8_t n2 = 0; // First, next bits out + + // Same as above, just switched to PORTB and stripped of comments. + hi = PORTB | pinMask; + lo = PORTB & ~pinMask; + n1 = lo; + if (b & 0x80) + { + n1 = hi; + } + + asm volatile( + "headB:" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 6" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 5" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 4" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 3" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 2" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 1" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "rjmp .+0" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n2] , %[lo]" "\n\t" + "out %[port] , %[n1]" "\n\t" + "rjmp .+0" "\n\t" + "sbrc %[byte] , 0" "\n\t" + "mov %[n2] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "out %[port] , %[hi]" "\n\t" + "mov %[n1] , %[lo]" "\n\t" + "out %[port] , %[n2]" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[n1] , %[hi]" "\n\t" + "out %[port] , %[lo]" "\n\t" + "brne headB" "\n" + : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); +} + +void send_pixels_8mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) +{ + volatile size_t i = sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + // Timing is more relaxed; unrolling the inner loop for each bit is + // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out + // of need but just to trim the code size down a little. + // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical + // to the 800-on-16 code later -- the hi/lo timing between WS2811 and + // WS2812 is not simply a 2:1 scale! + + // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,4,10) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "dec %[bit]" "\n\t" // 1 bit-- (T = 8) + "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) + "nextbyte20:" "\n\t" // (T = 10) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12) + "nop" "\n\t" // 1 nop (T = 13) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) + "brne head20" "\n" // 2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [hi] "r" (hi), + [lo] "r" (lo), + [ptr] "e" (ptr)); +} + +#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) // 12Mhz CPU + +#ifdef PORTD // PORTD isn't present on ATtiny85, etc. +void send_pixels_12mhz_800_PortD(uint8_t* pixels, size_t sizePixels, uint8_t pinMask) +{ + volatile size_t i = sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + // In the 12 MHz case, an optimized 800 KHz datastream (no dead time + // between bytes) requires a PORT-specific loop similar to the 8 MHz + // code (but a little more relaxed in this case). + + // 15 instruction clocks per bit: HHHHxxxxxxLLLLL + // OUT instructions: ^ ^ ^ (T=0,4,10) + + volatile uint8_t next; + + hi = PORTD | pinMask; + lo = PORTD & ~pinMask; + next = lo; + if (b & 0x80) next = hi; + + // Don't "optimize" the OUT calls into the bitTime subroutine; + // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs! + asm volatile( + "headD:" "\n\t" // (T = 0) + "out %[port], %[hi]" "\n\t" // (T = 1) + "rcall bitTimeD" "\n\t" // Bit 7 (T = 15) + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 6 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 5 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 4 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 3 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 2 + "out %[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 1 + // Bit 0: + "out %[port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "rjmp .+0" "\n\t" // 2 nop nop (T = 3) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5) + "out %[port] , %[next]" "\n\t" // 1 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "out %[port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13) + "brne headD" "\n\t" // 2 if(i != 0) -> (next byte) + "rjmp doneD" "\n\t" + "bitTimeD:" "\n\t" // nop nop nop (T = 4) + "out %[port], %[next]" "\n\t" // 1 PORT = next (T = 5) + "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7) + "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "out %[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "ret" "\n\t" // 4 nop nop nop nop (T = 15) + "doneD:" "\n" + : [byte] "+r" (b), + [next] "+r" (next), + [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTD)), + [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); +} +#endif + +void send_pixels_12mhz_800_PortB(uint8_t* pixels, size_t sizePixels, uint8_t pinMask) +{ + volatile uint16_t i = (uint16_t)sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + volatile uint8_t next; + + hi = PORTB | pinMask; + lo = PORTB & ~pinMask; + next = lo; + if (b & 0x80) + { + next = hi; + } + + // Same as above, just set for PORTB & stripped of comments + asm volatile( + "headB:" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port], %[hi]" "\n\t" + "rcall bitTimeB" "\n\t" + "out %[port] , %[hi]" "\n\t" + "rjmp .+0" "\n\t" + "ld %[byte] , %a[ptr]+" "\n\t" + "out %[port] , %[next]" "\n\t" + "mov %[next] , %[lo]" "\n\t" + "sbrc %[byte] , 7" "\n\t" + "mov %[next] , %[hi]" "\n\t" + "nop" "\n\t" + "out %[port] , %[lo]" "\n\t" + "sbiw %[count], 1" "\n\t" + "brne headB" "\n\t" + "rjmp doneB" "\n\t" + "bitTimeB:" "\n\t" + "out %[port], %[next]" "\n\t" + "mov %[next], %[lo]" "\n\t" + "rol %[byte]" "\n\t" + "sbrc %[byte], 7" "\n\t" + "mov %[next], %[hi]" "\n\t" + "nop" "\n\t" + "out %[port], %[lo]" "\n\t" + "ret" "\n\t" + "doneB:" "\n" + : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i) + : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi), + [lo] "r" (lo)); +} + +void send_pixels_12mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) +{ + volatile uint16_t i = (uint16_t)sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,6,15) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head30:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "rjmp .+0" "\n\t" // 2 nop nop (T = 6) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 8) + "rjmp .+0" "\n\t" // 2 nop nop (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "nop" "\n\t" // 1 nop (T = 15) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 17) + "rjmp .+0" "\n\t" // 2 nop nop (T = 19) + "dec %[bit]" "\n\t" // 1 bit-- (T = 20) + "breq nextbyte30" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22) + "rjmp .+0" "\n\t" // 2 nop nop (T = 24) + "rjmp .+0" "\n\t" // 2 nop nop (T = 26) + "rjmp .+0" "\n\t" // 2 nop nop (T = 28) + "rjmp head30" "\n\t" // 2 -> head30 (next bit out) + "nextbyte30:" "\n\t" // (T = 22) + "nop" "\n\t" // 1 nop (T = 23) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28) + "brne head30" "\n" // 1-2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [hi] "r" (hi), + [lo] "r" (lo), + [ptr] "e" (ptr)); +} + +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) // 16Mhz CPU + +void send_pixels_16mhz_800(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) +{ + volatile uint16_t i = (uint16_t)sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + // WS2811 and WS2812 have different hi/lo duty cycles; this is + // similar but NOT an exact copy of the prior 400-on-8 code. + + // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL + // ST instructions: ^ ^ ^ (T=0,5,13) + + volatile uint8_t next; + volatile uint8_t bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "dec %[bit]" "\n\t" // 1 bit-- (T = 5) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 7) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8) + "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "nop" "\n\t" // 1 nop (T = 13) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) + "nop" "\n\t" // 1 nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) + "nextbyte20:" "\n\t" // (T = 10) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15) + "nop" "\n\t" // 1 nop (T = 16) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) + "brne head20" "\n" // 2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); +} + +void send_pixels_16mhz_400(uint8_t* pixels, size_t sizePixels, volatile uint8_t* port, uint8_t pinMask) +{ + volatile size_t i = sizePixels; // Loop counter + volatile uint8_t* ptr = pixels; // Pointer to next byte + volatile uint8_t b = *ptr++; // Current byte value + volatile uint8_t hi; // PORT w/output bit set high + volatile uint8_t lo; // PORT w/output bit set low + + // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version. + + // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,8,20) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head40:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2) + "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "rjmp .+0" "\n\t" // 2 nop nop (T = 6) + "rjmp .+0" "\n\t" // 2 nop nop (T = 8) + "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp .+0" "\n\t" // 2 nop nop (T = 20) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 22) + "nop" "\n\t" // 1 nop (T = 23) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24) + "dec %[bit]" "\n\t" // 1 bit-- (T = 25) + "breq nextbyte40" "\n\t" // 1-2 if(bit == 0) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27) + "nop" "\n\t" // 1 nop (T = 28) + "rjmp .+0" "\n\t" // 2 nop nop (T = 30) + "rjmp .+0" "\n\t" // 2 nop nop (T = 32) + "rjmp .+0" "\n\t" // 2 nop nop (T = 34) + "rjmp .+0" "\n\t" // 2 nop nop (T = 36) + "rjmp .+0" "\n\t" // 2 nop nop (T = 38) + "rjmp head40" "\n\t" // 2 -> head40 (next bit out) + "nextbyte40:" "\n\t" // (T = 27) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30) + "rjmp .+0" "\n\t" // 2 nop nop (T = 32) + "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 34) + "rjmp .+0" "\n\t" // 2 nop nop (T = 36) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38) + "brne head40" "\n" // 1-2 if(i != 0) -> (next byte) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); +} + +#else +#error "CPU SPEED NOT SUPPORTED" +#endif + +#endif diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelEsp.c b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelEsp.c new file mode 100644 index 0000000..52415ff --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoPixelEsp.c @@ -0,0 +1,151 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for Esp8266 and Esp32. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + +#include +#if defined(ARDUINO_ARCH_ESP8266) +#include +#endif + +// ESP32 doesn't define ICACHE_RAM_ATTR +#ifndef ICACHE_RAM_ATTR +#define ICACHE_RAM_ATTR IRAM_ATTR +#endif + +inline uint32_t _getCycleCount() +{ + uint32_t ccount; + __asm__ __volatile__("rsr %0,ccount":"=a" (ccount)); + return ccount; +} + +#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us +#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us +#define CYCLES_800 (F_CPU / 800000) // 1.25us per bit +#define CYCLES_400_T0H (F_CPU / 2000000) +#define CYCLES_400_T1H (F_CPU / 833333) +#define CYCLES_400 (F_CPU / 400000) + +void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin) +{ + const uint32_t pinRegister = _BV(pin); + uint8_t mask; + uint8_t subpix; + uint32_t cyclesStart; + + // trigger emediately + cyclesStart = _getCycleCount() - CYCLES_800; + do + { + subpix = *pixels++; + for (mask = 0x80; mask != 0; mask >>= 1) + { + // do the checks here while we are waiting on time to pass + uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_800_T1H : CYCLES_800_T0H; + uint32_t cyclesNext = cyclesStart; + + // after we have done as much work as needed for this next bit + // now wait for the HIGH + do + { + // cache and use this count so we don't incur another + // instruction before we turn the bit high + cyclesStart = _getCycleCount(); + } while ((cyclesStart - cyclesNext) < CYCLES_800); + + // set high +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1ts = pinRegister; +#else + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); +#endif + + // wait for the LOW + do + { + cyclesNext = _getCycleCount(); + } while ((cyclesNext - cyclesStart) < cyclesBit); + + // set low +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1tc = pinRegister; +#else + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); +#endif + } + } while (pixels < end); +} + +void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin) +{ + const uint32_t pinRegister = _BV(pin); + uint8_t mask; + uint8_t subpix; + uint32_t cyclesStart; + + // trigger emediately + cyclesStart = _getCycleCount() - CYCLES_400; + do + { + subpix = *pixels++; + for (mask = 0x80; mask; mask >>= 1) + { + uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_400_T1H : CYCLES_400_T0H; + uint32_t cyclesNext = cyclesStart; + + // after we have done as much work as needed for this next bit + // now wait for the HIGH + do + { + // cache and use this count so we don't incur another + // instruction before we turn the bit high + cyclesStart = _getCycleCount(); + } while ((cyclesStart - cyclesNext) < CYCLES_400); + +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1ts = pinRegister; +#else + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); +#endif + + // wait for the LOW + do + { + cyclesNext = _getCycleCount(); + } while ((cyclesNext - cyclesStart) < cyclesBit); + + // set low +#if defined(ARDUINO_ARCH_ESP32) + GPIO.out_w1tc = pinRegister; +#else + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); +#endif + } + } while (pixels < end); +} + +#endif diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoRingTopology.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoRingTopology.h new file mode 100644 index 0000000..8e152f8 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoRingTopology.h @@ -0,0 +1,89 @@ +#pragma once + +/*------------------------------------------------------------------------- +NeoRingTopology provides a mapping feature of a 2d polar cordinate to a +linear 1d cordinate. +It is used to map a series of concentric rings of NeoPixels to a index on +the NeoPixelBus. + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +template class NeoRingTopology : protected T_LAYOUT +{ +public: + NeoRingTopology() + { + } + + uint16_t Map(uint8_t ring, uint16_t pixel) const + { + if (pixel >= getPixelCountAtRing(ring)) + { + return 0; // invalid ring and/or pixel argument, always return a valid value, the first one + } + + return _map(ring, pixel); + } + + uint16_t MapProbe(uint8_t ring, uint16_t pixel) const + { + if (pixel >= getPixelCountAtRing(ring)) + { + return getPixelCount(); // total count, out of bounds + } + + return _map(ring, pixel); + } + + uint8_t getCountOfRings() const + { + return _ringCount() - 1; // minus one as the Rings includes the extra value + } + + uint16_t getPixelCountAtRing(uint8_t ring) const + { + if (ring >= getCountOfRings()) + { + return 0; // invalid, no pixels + } + + return T_LAYOUT::Rings[ring + 1] - T_LAYOUT::Rings[ring]; // using the extra value for count calc + } + + uint16_t getPixelCount() const + { + return T_LAYOUT::Rings[_ringCount() - 1]; // the last entry is the total count + } + +private: + uint16_t _map(uint8_t ring, uint16_t pixel) const + { + return T_LAYOUT::Rings[ring] + pixel; + } + + uint8_t _ringCount() const + { + return sizeof(T_LAYOUT::Rings) / sizeof(T_LAYOUT::Rings[0]); + } +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoSpriteSheet.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoSpriteSheet.h new file mode 100644 index 0000000..a0f277e --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoSpriteSheet.h @@ -0,0 +1,163 @@ +/*------------------------------------------------------------------------- +NeoPixel library + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + + +template class NeoVerticalSpriteSheet +{ +public: + NeoVerticalSpriteSheet(uint16_t width, + uint16_t height, + uint16_t spriteHeight, + PGM_VOID_P pixels) : + _method(width, height, pixels), + _spriteHeight(spriteHeight), + _spriteCount(height / spriteHeight) + { + } + + operator NeoBufferContext() + { + return _method; + } + + uint16_t SpriteWidth() const + { + return _method.Width(); + }; + + uint16_t SpriteHeight() const + { + return _spriteHeight; + }; + + uint16_t SpriteCount() const + { + return _spriteCount; + } + + void SetPixelColor(uint16_t indexSprite, + int16_t x, + int16_t y, + typename T_BUFFER_METHOD::ColorObject color) + { + _method.SetPixelColor(pixelIndex(indexSprite, x, y), color); + }; + + typename T_BUFFER_METHOD::ColorObject GetPixelColor(uint16_t indexSprite, + int16_t x, + int16_t y) const + { + return _method.GetPixelColor(pixelIndex(indexSprite, x, y)); + }; + + void ClearTo(typename T_BUFFER_METHOD::ColorObject color) + { + _method.ClearTo(color); + }; + + void Blt(NeoBufferContext destBuffer, + uint16_t indexPixel, + uint16_t indexSprite) + { + uint16_t destPixelCount = destBuffer.PixelCount(); + // validate indexPixel + if (indexPixel >= destPixelCount) + { + return; + } + + // validate indexSprite + if (indexSprite >= _spriteCount) + { + return; + } + // calc how many we can copy + uint16_t copyCount = destPixelCount - indexPixel; + + if (copyCount > SpriteWidth()) + { + copyCount = SpriteWidth(); + } + + uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexPixel); + const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), pixelIndex(indexSprite, 0, 0)); + _method.CopyPixels(pDest, pSrc, copyCount); + } + + void Blt(NeoBufferContext destBuffer, + int16_t x, + int16_t y, + uint16_t indexSprite, + LayoutMapCallback layoutMap) + { + if (indexSprite >= _spriteCount) + { + return; + } + uint16_t destPixelCount = destBuffer.PixelCount(); + + for (int16_t srcY = 0; srcY < SpriteHeight(); srcY++) + { + for (int16_t srcX = 0; srcX < SpriteWidth(); srcX++) + { + uint16_t indexDest = layoutMap(srcX + x, srcY + y); + + if (indexDest < destPixelCount) + { + const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), pixelIndex(indexSprite, srcX, srcY)); + uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexDest); + + _method.CopyPixels(pDest, pSrc, 1); + } + } + } + + } + +private: + T_BUFFER_METHOD _method; + + const uint16_t _spriteHeight; + const uint16_t _spriteCount; + + uint16_t pixelIndex(uint16_t indexSprite, + int16_t x, + int16_t y) + { + uint16_t result = PixelIndex_OutOfBounds; + + if (indexSprite < _spriteCount && + x >= 0 && + x < SpriteWidth() && + y >= 0 && + y < SpriteHeight()) + { + result = x + y * SpriteWidth() + indexSprite * _spriteHeight * SpriteWidth(); + } + return result; + } +}; \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTiles.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTiles.h new file mode 100644 index 0000000..2c0ca83 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTiles.h @@ -0,0 +1,158 @@ +#pragma once + +/*------------------------------------------------------------------------- +NeoTiles provides a mapping feature of a 2d cordinate to linear 1d cordinate +It is used to map tiles of matricies of NeoPixels to a index on the NeoPixelBus +where the the matricies use one topology and the tiles of those matricies can +use another + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +//----------------------------------------------------------------------------- +// class NeoTiles +// Simple template Tile layout class +// T_MATRIX_LAYOUT = the layout used on the pixel matrix panel (a tile) +// T_TILE_LAYOUT = the layout used for the tiles. +// +//----------------------------------------------------------------------------- +template class NeoTiles +{ +public: + NeoTiles(uint16_t topoWidth, uint16_t topoHeight, + uint16_t tilesWidth, uint16_t tilesHeight) : + _topo(topoWidth, topoHeight), + _width(tilesWidth), + _height(tilesHeight) + { + } + + uint16_t Map(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x >= totalWidth) + { + x = totalWidth - 1; + } + else if (x < 0) + { + x = 0; + } + + if (y >= totalHeight) + { + y = totalHeight - 1; + } + else if (y < 0) + { + y = 0; + } + + uint16_t localIndex; + uint16_t tileOffset; + + calculate(x, y, &localIndex, &tileOffset); + + return localIndex + tileOffset; + } + + uint16_t MapProbe(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight) + { + return totalWidth * totalHeight; // count, out of bounds + } + + uint16_t localIndex; + uint16_t tileOffset; + + calculate(x, y, &localIndex, &tileOffset); + + return localIndex + tileOffset; + } + + NeoTopologyHint TopologyHint(int16_t x, int16_t y) const + { + uint16_t totalWidth = getWidth(); + uint16_t totalHeight = getHeight(); + + if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight) + { + return NeoTopologyHint_OutOfBounds; + } + + uint16_t localIndex; + uint16_t tileOffset; + NeoTopologyHint result; + + calculate(x, y, &localIndex, &tileOffset); + + if (localIndex == 0) + { + result = NeoTopologyHint_FirstOnPanel; + } + else if (localIndex == (_topo.getWidth() * _topo.getHeight() - 1)) + { + result = NeoTopologyHint_LastOnPanel; + } + else + { + result = NeoTopologyHint_InPanel; + } + + return result; + } + + uint16_t getWidth() const + { + return _width * _topo.getWidth(); + } + + uint16_t getHeight() const + { + return _height * _topo.getHeight(); + } + +private: + const NeoTopology _topo; + const uint16_t _width; + const uint16_t _height; + + void calculate(uint16_t x, uint16_t y, uint16_t* pLocalIndex, uint16_t* pTileOffset) const + { + uint16_t tileX = x / _topo.getWidth(); + uint16_t topoX = x % _topo.getWidth(); + + uint16_t tileY = y / _topo.getHeight(); + uint16_t topoY = y % _topo.getHeight(); + + *pTileOffset = T_TILE_LAYOUT::Map(_width, _height, tileX, tileY) * _topo.getWidth() * _topo.getHeight(); + *pLocalIndex = _topo.Map(topoX, topoY); + } +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTopology.h b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTopology.h new file mode 100644 index 0000000..224c1db --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/NeoTopology.h @@ -0,0 +1,91 @@ +#pragma once + +/*------------------------------------------------------------------------- +NeoTopology provides a mapping feature of a 2d cordinate to linear 1d cordinate +It is used to map a matrix of NeoPixels to a index on the NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +enum NeoTopologyHint +{ + NeoTopologyHint_FirstOnPanel, + NeoTopologyHint_InPanel, + NeoTopologyHint_LastOnPanel, + NeoTopologyHint_OutOfBounds +}; + +template class NeoTopology +{ +public: + NeoTopology(uint16_t width, uint16_t height) : + _width(width), + _height(height) + { + + } + + uint16_t Map(int16_t x, int16_t y) const + { + if (x >= _width) + { + x = _width - 1; + } + else if (x < 0) + { + x = 0; + } + if (y >= _height) + { + y = _height - 1; + } + else if (y < 0) + { + y = 0; + } + return T_LAYOUT::Map(_width, _height, x, y); + } + + uint16_t MapProbe(int16_t x, int16_t y) const + { + if (x < 0 || x >= _width || y < 0 || y >= _height) + { + return _width * _height; // count, out of bounds + } + return T_LAYOUT::Map(_width, _height, x, y); + } + + uint16_t getWidth() const + { + return _width; + } + + uint16_t getHeight() const + { + return _height; + } + +private: + const uint16_t _width; + const uint16_t _height; +}; diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.cpp new file mode 100644 index 0000000..29ae434 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.cpp @@ -0,0 +1,249 @@ +/*------------------------------------------------------------------------- +RgbColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "RgbColor.h" +#include "HslColor.h" +#include "HsbColor.h" +#include "HtmlColor.h" + +static float _CalcColor(float p, float q, float t) +{ + if (t < 0.0f) + t += 1.0f; + if (t > 1.0f) + t -= 1.0f; + + if (t < 1.0f / 6.0f) + return p + (q - p) * 6.0f * t; + + if (t < 0.5f) + return q; + + if (t < 2.0f / 3.0f) + return p + ((q - p) * (2.0f / 3.0f - t) * 6.0f); + + return p; +} + +RgbColor::RgbColor(const HtmlColor& color) +{ + uint32_t temp = color.Color; + + B = (temp & 0xff); + temp = temp >> 8; + G = (temp & 0xff); + temp = temp >> 8; + R = (temp & 0xff); +}; + +RgbColor::RgbColor(const HslColor& color) +{ + float r; + float g; + float b; + + float h = color.H; + float s = color.S; + float l = color.L; + + + if (color.S == 0.0f || color.L == 0.0f) + { + r = g = b = l; // achromatic or black + } + else + { + float q = l < 0.5f ? l * (1.0f + s) : l + s - (l * s); + float p = 2.0f * l - q; + r = _CalcColor(p, q, h + 1.0f / 3.0f); + g = _CalcColor(p, q, h); + b = _CalcColor(p, q, h - 1.0f / 3.0f); + } + + R = (uint8_t)(r * 255.0f); + G = (uint8_t)(g * 255.0f); + B = (uint8_t)(b * 255.0f); +} + +RgbColor::RgbColor(const HsbColor& color) +{ + float r; + float g; + float b; + + float h = color.H; + float s = color.S; + float v = color.B; + + if (color.S == 0.0f) + { + r = g = b = v; // achromatic or black + } + else + { + if (h < 0.0f) + { + h += 1.0f; + } + else if (h >= 1.0f) + { + h -= 1.0f; + } + h *= 6.0f; + int i = (int)h; + float f = h - i; + float q = v * (1.0f - s * f); + float p = v * (1.0f - s); + float t = v * (1.0f - s * (1.0f - f)); + switch (i) + { + case 0: + r = v; + g = t; + b = p; + break; + case 1: + r = q; + g = v; + b = p; + break; + case 2: + r = p; + g = v; + b = t; + break; + case 3: + r = p; + g = q; + b = v; + break; + case 4: + r = t; + g = p; + b = v; + break; + default: + r = v; + g = p; + b = q; + break; + } + } + + R = (uint8_t)(r * 255.0f); + G = (uint8_t)(g * 255.0f); + B = (uint8_t)(b * 255.0f); +} + +uint8_t RgbColor::CalculateBrightness() const +{ + return (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); +} + +void RgbColor::Darken(uint8_t delta) +{ + if (R > delta) + { + R -= delta; + } + else + { + R = 0; + } + + if (G > delta) + { + G -= delta; + } + else + { + G = 0; + } + + if (B > delta) + { + B -= delta; + } + else + { + B = 0; + } +} + +void RgbColor::Lighten(uint8_t delta) +{ + if (R < 255 - delta) + { + R += delta; + } + else + { + R = 255; + } + + if (G < 255 - delta) + { + G += delta; + } + else + { + G = 255; + } + + if (B < 255 - delta) + { + B += delta; + } + else + { + B = 255; + } +} + +RgbColor RgbColor::LinearBlend(const RgbColor& left, const RgbColor& right, float progress) +{ + return RgbColor( left.R + ((right.R - left.R) * progress), + left.G + ((right.G - left.G) * progress), + left.B + ((right.B - left.B) * progress)); +} + +RgbColor RgbColor::BilinearBlend(const RgbColor& c00, + const RgbColor& c01, + const RgbColor& c10, + const RgbColor& c11, + float x, + float y) +{ + float v00 = (1.0f - x) * (1.0f - y); + float v10 = x * (1.0f - y); + float v01 = (1.0f - x) * y; + float v11 = x * y; + + return RgbColor( + c00.R * v00 + c10.R * v10 + c01.R * v01 + c11.R * v11, + c00.G * v00 + c10.G * v10 + c01.G * v01 + c11.G * v11, + c00.B * v00 + c10.B * v10 + c01.B * v01 + c11.B * v11); +} \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.h b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.h new file mode 100644 index 0000000..5161123 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbColor.h @@ -0,0 +1,148 @@ +/*------------------------------------------------------------------------- +RgbColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +struct HslColor; +struct HsbColor; +struct HtmlColor; + +// ------------------------------------------------------------------------ +// RgbColor represents a color object that is represented by Red, Green, Blue +// component values. It contains helpful color routines to manipulate the +// color. +// ------------------------------------------------------------------------ +struct RgbColor +{ + // ------------------------------------------------------------------------ + // Construct a RgbColor using R, G, B values (0-255) + // ------------------------------------------------------------------------ + RgbColor(uint8_t r, uint8_t g, uint8_t b) : + R(r), G(g), B(b) + { + }; + + // ------------------------------------------------------------------------ + // Construct a RgbColor using a single brightness value (0-255) + // This works well for creating gray tone colors + // (0) = black, (255) = white, (128) = gray + // ------------------------------------------------------------------------ + RgbColor(uint8_t brightness) : + R(brightness), G(brightness), B(brightness) + { + }; + + // ------------------------------------------------------------------------ + // Construct a RgbColor using HtmlColor + // ------------------------------------------------------------------------ + RgbColor(const HtmlColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbColor using HslColor + // ------------------------------------------------------------------------ + RgbColor(const HslColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbColor using HsbColor + // ------------------------------------------------------------------------ + RgbColor(const HsbColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbColor that will have its values set in latter operations + // CAUTION: The R,G,B members are not initialized and may not be consistent + // ------------------------------------------------------------------------ + RgbColor() + { + }; + + // ------------------------------------------------------------------------ + // Comparison operators + // ------------------------------------------------------------------------ + bool operator==(const RgbColor& other) const + { + return (R == other.R && G == other.G && B == other.B); + }; + + bool operator!=(const RgbColor& other) const + { + return !(*this == other); + }; + + // ------------------------------------------------------------------------ + // CalculateBrightness will calculate the overall brightness + // NOTE: This is a simple linear brightness + // ------------------------------------------------------------------------ + uint8_t CalculateBrightness() const; + + // ------------------------------------------------------------------------ + // Darken will adjust the color by the given delta toward black + // NOTE: This is a simple linear change + // delta - (0-255) the amount to dim the color + // ------------------------------------------------------------------------ + void Darken(uint8_t delta); + + // ------------------------------------------------------------------------ + // Lighten will adjust the color by the given delta toward white + // NOTE: This is a simple linear change + // delta - (0-255) the amount to lighten the color + // ------------------------------------------------------------------------ + void Lighten(uint8_t delta); + + // ------------------------------------------------------------------------ + // LinearBlend between two colors by the amount defined by progress variable + // left - the color to start the blend at + // right - the color to end the blend at + // progress - (0.0 - 1.0) value where 0 will return left and 1.0 will return right + // and a value between will blend the color weighted linearly between them + // ------------------------------------------------------------------------ + static RgbColor LinearBlend(const RgbColor& left, const RgbColor& right, float progress); + + // ------------------------------------------------------------------------ + // BilinearBlend between four colors by the amount defined by 2d variable + // c00 - upper left quadrant color + // c01 - upper right quadrant color + // c10 - lower left quadrant color + // c11 - lower right quadrant color + // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space + // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space + // ------------------------------------------------------------------------ + static RgbColor BilinearBlend(const RgbColor& c00, + const RgbColor& c01, + const RgbColor& c10, + const RgbColor& c11, + float x, + float y); + + // ------------------------------------------------------------------------ + // Red, Green, Blue color members (0-255) where + // (0,0,0) is black and (255,255,255) is white + // ------------------------------------------------------------------------ + uint8_t R; + uint8_t G; + uint8_t B; +}; + diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.cpp b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.cpp new file mode 100644 index 0000000..062834d --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.cpp @@ -0,0 +1,178 @@ +/*------------------------------------------------------------------------- +RgbwColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#include "RgbColor.h" +#include "HslColor.h" +#include "HsbColor.h" +#include "RgbwColor.h" +#include "HtmlColor.h" + +RgbwColor::RgbwColor(const HtmlColor& color) +{ + uint32_t temp = color.Color; + B = (temp & 0xff); + temp = temp >> 8; + G = (temp & 0xff); + temp = temp >> 8; + R = (temp & 0xff); + temp = temp >> 8; + W = (temp & 0xff); +}; + +RgbwColor::RgbwColor(const HslColor& color) +{ + RgbColor rgbColor(color); + *this = rgbColor; +} + +RgbwColor::RgbwColor(const HsbColor& color) +{ + RgbColor rgbColor(color); + *this = rgbColor; +} + +uint8_t RgbwColor::CalculateBrightness() const +{ + uint8_t colorB = (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); + if (W > colorB) + { + return W; + } + else + { + return colorB; + } +} + +void RgbwColor::Darken(uint8_t delta) +{ + if (R > delta) + { + R -= delta; + } + else + { + R = 0; + } + + if (G > delta) + { + G -= delta; + } + else + { + G = 0; + } + + if (B > delta) + { + B -= delta; + } + else + { + B = 0; + } + + if (W > delta) + { + W -= delta; + } + else + { + W = 0; + } +} + +void RgbwColor::Lighten(uint8_t delta) +{ + if (IsColorLess()) + { + if (W < 255 - delta) + { + W += delta; + } + else + { + W = 255; + } + } + else + { + if (R < 255 - delta) + { + R += delta; + } + else + { + R = 255; + } + + if (G < 255 - delta) + { + G += delta; + } + else + { + G = 255; + } + + if (B < 255 - delta) + { + B += delta; + } + else + { + B = 255; + } + } +} + +RgbwColor RgbwColor::LinearBlend(const RgbwColor& left, const RgbwColor& right, float progress) +{ + return RgbwColor( left.R + ((right.R - left.R) * progress), + left.G + ((right.G - left.G) * progress), + left.B + ((right.B - left.B) * progress), + left.W + ((right.W - left.W) * progress) ); +} + +RgbwColor RgbwColor::BilinearBlend(const RgbwColor& c00, + const RgbwColor& c01, + const RgbwColor& c10, + const RgbwColor& c11, + float x, + float y) +{ + float v00 = (1.0f - x) * (1.0f - y); + float v10 = x * (1.0f - y); + float v01 = (1.0f - x) * y; + float v11 = x * y; + + return RgbwColor( + c00.R * v00 + c10.R * v10 + c01.R * v01 + c11.R * v11, + c00.G * v00 + c10.G * v10 + c01.G * v01 + c11.G * v11, + c00.B * v00 + c10.B * v10 + c01.B * v01 + c11.B * v11, + c00.W * v00 + c10.W * v10 + c01.W * v01 + c11.W * v11 ); +} \ No newline at end of file diff --git a/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.h b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.h new file mode 100644 index 0000000..38f2530 --- /dev/null +++ b/libraries/NeoPixelBus_by_Makuna/src/internal/RgbwColor.h @@ -0,0 +1,178 @@ +/*------------------------------------------------------------------------- +RgbwColor provides a color object that can be directly consumed by NeoPixelBus + +Written by Michael C. Miller. + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus 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 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +struct RgbColor; +struct HslColor; +struct HsbColor; + +// ------------------------------------------------------------------------ +// RgbwColor represents a color object that is represented by Red, Green, Blue +// component values and an extra White component. It contains helpful color +// routines to manipulate the color. +// ------------------------------------------------------------------------ +struct RgbwColor +{ + // ------------------------------------------------------------------------ + // Construct a RgbwColor using R, G, B, W values (0-255) + // ------------------------------------------------------------------------ + RgbwColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0) : + R(r), G(g), B(b), W(w) + { + }; + + // ------------------------------------------------------------------------ + // Construct a RgbColor using a single brightness value (0-255) + // This works well for creating gray tone colors + // (0) = black, (255) = white, (128) = gray + // ------------------------------------------------------------------------ + RgbwColor(uint8_t brightness) : + R(0), G(0), B(0), W(brightness) + { + }; + + // ------------------------------------------------------------------------ + // Construct a RgbwColor using RgbColor + // ------------------------------------------------------------------------ + RgbwColor(const RgbColor& color) : + R(color.R), + G(color.G), + B(color.B), + W(0) + { + }; + + // ------------------------------------------------------------------------ + // Construct a RgbwColor using HtmlColor + // ------------------------------------------------------------------------ + RgbwColor(const HtmlColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbwColor using HslColor + // ------------------------------------------------------------------------ + RgbwColor(const HslColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbwColor using HsbColor + // ------------------------------------------------------------------------ + RgbwColor(const HsbColor& color); + + // ------------------------------------------------------------------------ + // Construct a RgbwColor that will have its values set in latter operations + // CAUTION: The R,G,B, W members are not initialized and may not be consistent + // ------------------------------------------------------------------------ + RgbwColor() + { + }; + + // ------------------------------------------------------------------------ + // Comparison operators + // ------------------------------------------------------------------------ + bool operator==(const RgbwColor& other) const + { + return (R == other.R && G == other.G && B == other.B && W == other.W); + }; + + bool operator!=(const RgbwColor& other) const + { + return !(*this == other); + }; + + // ------------------------------------------------------------------------ + // Returns if the color is grey, all values are equal other than white + // ------------------------------------------------------------------------ + bool IsMonotone() const + { + return (R == B && R == G); + }; + + // ------------------------------------------------------------------------ + // Returns if the color components are all zero, the white component maybe + // anything + // ------------------------------------------------------------------------ + bool IsColorLess() const + { + return (R == 0 && B == 0 && G == 0); + }; + + // ------------------------------------------------------------------------ + // CalculateBrightness will calculate the overall brightness + // NOTE: This is a simple linear brightness + // ------------------------------------------------------------------------ + uint8_t CalculateBrightness() const; + + // ------------------------------------------------------------------------ + // Darken will adjust the color by the given delta toward black + // NOTE: This is a simple linear change + // delta - (0-255) the amount to dim the color + // ------------------------------------------------------------------------ + void Darken(uint8_t delta); + + // ------------------------------------------------------------------------ + // Lighten will adjust the color by the given delta toward white + // NOTE: This is a simple linear change + // delta - (0-255) the amount to lighten the color + // ------------------------------------------------------------------------ + void Lighten(uint8_t delta); + + // ------------------------------------------------------------------------ + // LinearBlend between two colors by the amount defined by progress variable + // left - the color to start the blend at + // right - the color to end the blend at + // progress - (0.0 - 1.0) value where 0 will return left and 1.0 will return right + // and a value between will blend the color weighted linearly between them + // ------------------------------------------------------------------------ + static RgbwColor LinearBlend(const RgbwColor& left, const RgbwColor& right, float progress); + + // ------------------------------------------------------------------------ + // BilinearBlend between four colors by the amount defined by 2d variable + // c00 - upper left quadrant color + // c01 - upper right quadrant color + // c10 - lower left quadrant color + // c11 - lower right quadrant color + // x - unit value (0.0 - 1.0) that defines the blend progress in horizontal space + // y - unit value (0.0 - 1.0) that defines the blend progress in vertical space + // ------------------------------------------------------------------------ + static RgbwColor BilinearBlend(const RgbwColor& c00, + const RgbwColor& c01, + const RgbwColor& c10, + const RgbwColor& c11, + float x, + float y); + + // ------------------------------------------------------------------------ + // Red, Green, Blue, White color members (0-255) where + // (0,0,0,0) is black and (255,255,255, 0) and (0,0,0,255) is white + // Note (255,255,255,255) is extreme bright white + // ------------------------------------------------------------------------ + uint8_t R; + uint8_t G; + uint8_t B; + uint8_t W; +}; + diff --git a/libraries/PubSubClient/CHANGES.txt b/libraries/PubSubClient/CHANGES.txt new file mode 100644 index 0000000..8c8bef6 --- /dev/null +++ b/libraries/PubSubClient/CHANGES.txt @@ -0,0 +1,68 @@ +2.4 + * Add MQTT_SOCKET_TIMEOUT to prevent it blocking indefinitely + whilst waiting for inbound data + * Fixed return code when publishing >256 bytes + +2.3 + * Add publish(topic,payload,retained) function + +2.2 + * Change code layout to match Arduino Library reqs + +2.1 + * Add MAX_TRANSFER_SIZE def to chunk messages if needed + * Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE + +2.0 + * Add (and default to) MQTT 3.1.1 support + * Fix PROGMEM handling for Intel Galileo/ESP8266 + * Add overloaded constructors for convenience + * Add chainable setters for server/callback/client/stream + * Add state function to return connack return code + +1.9 + * Do not split MQTT packets over multiple calls to _client->write() + * API change: All constructors now require an instance of Client + to be passed in. + * Fixed example to match 1.8 api changes - dpslwk + * Added username/password support - WilHall + * Added publish_P - publishes messages from PROGMEM - jobytaffey + +1.8 + * KeepAlive interval is configurable in PubSubClient.h + * Maximum packet size is configurable in PubSubClient.h + * API change: Return boolean rather than int from various functions + * API change: Length parameter in message callback changed + from int to unsigned int + * Various internal tidy-ups around types +1.7 + * Improved keepalive handling + * Updated to the Arduino-1.0 API +1.6 + * Added the ability to publish a retained message + +1.5 + * Added default constructor + * Fixed compile error when used with arduino-0021 or later + +1.4 + * Fixed connection lost handling + +1.3 + * Fixed packet reading bug in PubSubClient.readPacket + +1.2 + * Fixed compile error when used with arduino-0016 or later + + +1.1 + * Reduced size of library + * Added support for Will messages + * Clarified licensing - see LICENSE.txt + + +1.0 + * Only Quality of Service (QOS) 0 messaging is supported + * The maximum message size, including header, is 128 bytes + * The keepalive interval is set to 30 seconds + * No support for Will messages diff --git a/libraries/PubSubClient/LICENSE.txt b/libraries/PubSubClient/LICENSE.txt new file mode 100644 index 0000000..217df35 --- /dev/null +++ b/libraries/PubSubClient/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2008-2015 Nicholas O'Leary + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/libraries/PubSubClient/README.md b/libraries/PubSubClient/README.md new file mode 100644 index 0000000..8317691 --- /dev/null +++ b/libraries/PubSubClient/README.md @@ -0,0 +1,47 @@ +# Arduino Client for MQTT + +This library provides a client for doing simple publish/subscribe messaging with +a server that supports MQTT. + +## Examples + +The library comes with a number of example sketches. See File > Examples > PubSubClient +within the Arduino application. + +Full API documentation is available here: http://pubsubclient.knolleary.net + +## Limitations + + - It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1. + - The maximum message size, including header, is **128 bytes** by default. This + is configurable via `MQTT_MAX_PACKET_SIZE` in `PubSubClient.h`. + - The keepalive interval is set to 15 seconds by default. This is configurable + via `MQTT_KEEPALIVE` in `PubSubClient.h`. + - The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by + changing value of `MQTT_VERSION` in `PubSubClient.h`. + + +## Compatible Hardware + +The library uses the Arduino Ethernet Client api for interacting with the +underlying network hardware. This means it Just Works with a growing number of +boards and shields, including: + + - Arduino Ethernet + - Arduino Ethernet Shield + - Arduino YUN – use the included `YunClient` in place of `EthernetClient`, and + be sure to do a `Bridge.begin()` first + - Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield, + enable the `MQTT_MAX_TRANSFER_SIZE` define in `PubSubClient.h`. + - Sparkfun WiFly Shield – [library](https://github.com/dpslwk/WiFly) + - TI CC3000 WiFi - [library](https://github.com/sparkfun/SFE_CC3000_Library) + - Intel Galileo/Edison + - ESP8266 + +The library cannot currently be used with hardware based on the ENC28J60 chip – +such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an +[alternative library](https://github.com/njh/NanodeMQTT) available. + +## License + +This code is released under the MIT License. diff --git a/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino b/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino new file mode 100644 index 0000000..e9f7b18 --- /dev/null +++ b/libraries/PubSubClient/examples/mqtt_auth/mqtt_auth.ino @@ -0,0 +1,43 @@ +/* + Basic MQTT example with Authentication + + - connects to an MQTT server, providing username + and password + - publishes "hello world" to the topic "outTopic" + - subscribes to the topic "inTopic" +*/ + +#include +#include +#include + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +IPAddress ip(172, 16, 0, 100); +IPAddress server(172, 16, 0, 2); + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + +EthernetClient ethClient; +PubSubClient client(server, 1883, callback, ethClient); + +void setup() +{ + Ethernet.begin(mac, ip); + // Note - the default maximum packet size is 128 bytes. If the + // combined length of clientId, username and password exceed this, + // you will need to increase the value of MQTT_MAX_PACKET_SIZE in + // PubSubClient.h + + if (client.connect("arduinoClient", "testuser", "testpass")) { + client.publish("outTopic","hello world"); + client.subscribe("inTopic"); + } +} + +void loop() +{ + client.loop(); +} diff --git a/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino b/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino new file mode 100644 index 0000000..f545ade --- /dev/null +++ b/libraries/PubSubClient/examples/mqtt_basic/mqtt_basic.ino @@ -0,0 +1,77 @@ +/* + Basic MQTT example + + This sketch demonstrates the basic capabilities of the library. + It connects to an MQTT server then: + - publishes "hello world" to the topic "outTopic" + - subscribes to the topic "inTopic", printing out any messages + it receives. NB - it assumes the received payloads are strings not binary + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + +*/ + +#include +#include +#include + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +IPAddress ip(172, 16, 0, 100); +IPAddress server(172, 16, 0, 2); + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i=0;i Preferences -> Additional Boards Manager URLs": + http://arduino.esp8266.com/stable/package_esp8266com_index.json + - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" + - Select your ESP8266 in "Tools -> Board" + +*/ + +#include +#include + +// Update these with values suitable for your network. + +const char* ssid = "........"; +const char* password = "........"; +const char* mqtt_server = "broker.mqtt-dashboard.com"; + +WiFiClient espClient; +PubSubClient client(espClient); +long lastMsg = 0; +char msg[50]; +int value = 0; + +void setup() { + pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output + Serial.begin(115200); + setup_wifi(); + client.setServer(mqtt_server, 1883); + client.setCallback(callback); +} + +void setup_wifi() { + + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level + // but actually the LED is on; this is because + // it is acive low on the ESP-01) + } else { + digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH + } + +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("ESP8266Client")) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish("outTopic", "hello world"); + // ... and resubscribe + client.subscribe("inTopic"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} +void loop() { + + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 2000) { + lastMsg = now; + ++value; + snprintf (msg, 75, "hello world #%ld", value); + Serial.print("Publish message: "); + Serial.println(msg); + client.publish("outTopic", msg); + } +} diff --git a/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino b/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino new file mode 100644 index 0000000..42afb2a --- /dev/null +++ b/libraries/PubSubClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino @@ -0,0 +1,60 @@ +/* + Publishing in the callback + + - connects to an MQTT server + - subscribes to the topic "inTopic" + - when a message is received, republishes it to "outTopic" + + This example shows how to publish messages within the + callback function. The callback function header needs to + be declared before the PubSubClient constructor and the + actual callback defined afterwards. + This ensures the client reference in the callback function + is valid. + +*/ + +#include +#include +#include + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +IPAddress ip(172, 16, 0, 100); +IPAddress server(172, 16, 0, 2); + +// Callback function header +void callback(char* topic, byte* payload, unsigned int length); + +EthernetClient ethClient; +PubSubClient client(server, 1883, callback, ethClient); + +// Callback function +void callback(char* topic, byte* payload, unsigned int length) { + // In order to republish this payload, a copy must be made + // as the orignal payload buffer will be overwritten whilst + // constructing the PUBLISH packet. + + // Allocate the correct amount of memory for the payload copy + byte* p = (byte*)malloc(length); + // Copy the payload to the new buffer + memcpy(p,payload,length); + client.publish("outTopic", p, length); + // Free the memory + free(p); +} + +void setup() +{ + + Ethernet.begin(mac, ip); + if (client.connect("arduinoClient")) { + client.publish("outTopic","hello world"); + client.subscribe("inTopic"); + } +} + +void loop() +{ + client.loop(); +} diff --git a/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino b/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino new file mode 100644 index 0000000..080b739 --- /dev/null +++ b/libraries/PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.ino @@ -0,0 +1,67 @@ +/* + Reconnecting MQTT example - non-blocking + + This sketch demonstrates how to keep the client connected + using a non-blocking reconnect function. If the client loses + its connection, it attempts to reconnect every 5 seconds + without blocking the main loop. + +*/ + +#include +#include +#include + +// Update these with values suitable for your hardware/network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +IPAddress ip(172, 16, 0, 100); +IPAddress server(172, 16, 0, 2); + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + +EthernetClient ethClient; +PubSubClient client(ethClient); + +long lastReconnectAttempt = 0; + +boolean reconnect() { + if (client.connect("arduinoClient")) { + // Once connected, publish an announcement... + client.publish("outTopic","hello world"); + // ... and resubscribe + client.subscribe("inTopic"); + } + return client.connected(); +} + +void setup() +{ + client.setServer(server, 1883); + client.setCallback(callback); + + Ethernet.begin(mac, ip); + delay(1500); + lastReconnectAttempt = 0; +} + + +void loop() +{ + if (!client.connected()) { + long now = millis(); + if (now - lastReconnectAttempt > 5000) { + lastReconnectAttempt = now; + // Attempt to reconnect + if (reconnect()) { + lastReconnectAttempt = 0; + } + } + } else { + // Client connected + + client.loop(); + } + +} diff --git a/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino b/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino new file mode 100644 index 0000000..67c2287 --- /dev/null +++ b/libraries/PubSubClient/examples/mqtt_stream/mqtt_stream.ino @@ -0,0 +1,57 @@ +/* + Example of using a Stream object to store the message payload + + Uses SRAM library: https://github.com/ennui2342/arduino-sram + but could use any Stream based class such as SD + + - connects to an MQTT server + - publishes "hello world" to the topic "outTopic" + - subscribes to the topic "inTopic" +*/ + +#include +#include +#include +#include + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +IPAddress ip(172, 16, 0, 100); +IPAddress server(172, 16, 0, 2); + +SRAM sram(4, SRAM_1024); + +void callback(char* topic, byte* payload, unsigned int length) { + sram.seek(1); + + // do something with the message + for(uint8_t i=0; i +maintainer=Nick O'Leary +sentence=A client library for MQTT messaging. +paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000. +category=Communication +url=http://pubsubclient.knolleary.net +architectures=* diff --git a/libraries/PubSubClient/src/PubSubClient.cpp b/libraries/PubSubClient/src/PubSubClient.cpp new file mode 100644 index 0000000..9658c4a --- /dev/null +++ b/libraries/PubSubClient/src/PubSubClient.cpp @@ -0,0 +1,590 @@ +/* + PubSubClient.cpp - A simple client for MQTT. + Nick O'Leary + http://knolleary.net +*/ + +#include "PubSubClient.h" +#include "Arduino.h" + +PubSubClient::PubSubClient() { + this->_state = MQTT_DISCONNECTED; + this->_client = NULL; + this->stream = NULL; + setCallback(NULL); +} + +PubSubClient::PubSubClient(Client& client) { + this->_state = MQTT_DISCONNECTED; + setClient(client); + this->stream = NULL; +} + +PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(addr, port); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(addr,port); + setClient(client); + setStream(stream); +} +PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(addr, port); + setCallback(callback); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(addr,port); + setCallback(callback); + setClient(client); + setStream(stream); +} + +PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(ip, port); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(ip,port); + setClient(client); + setStream(stream); +} +PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(ip, port); + setCallback(callback); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(ip,port); + setCallback(callback); + setClient(client); + setStream(stream); +} + +PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(domain,port); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(domain,port); + setClient(client); + setStream(stream); +} +PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) { + this->_state = MQTT_DISCONNECTED; + setServer(domain,port); + setCallback(callback); + setClient(client); + this->stream = NULL; +} +PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) { + this->_state = MQTT_DISCONNECTED; + setServer(domain,port); + setCallback(callback); + setClient(client); + setStream(stream); +} + +boolean PubSubClient::connect(const char *id) { + return connect(id,NULL,NULL,0,0,0,0); +} + +boolean PubSubClient::connect(const char *id, const char *user, const char *pass) { + return connect(id,user,pass,0,0,0,0); +} + +boolean PubSubClient::connect(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) { + return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage); +} + +boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) { + if (!connected()) { + int result = 0; + + if (domain != NULL) { + result = _client->connect(this->domain, this->port); + } else { + result = _client->connect(this->ip, this->port); + } + if (result == 1) { + nextMsgId = 1; + // Leave room in the buffer for header and variable length field + uint16_t length = 5; + unsigned int j; + +#if MQTT_VERSION == MQTT_VERSION_3_1 + uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION}; +#define MQTT_HEADER_VERSION_LENGTH 9 +#elif MQTT_VERSION == MQTT_VERSION_3_1_1 + uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION}; +#define MQTT_HEADER_VERSION_LENGTH 7 +#endif + for (j = 0;j>1); + } + } + + buffer[length++] = v; + + buffer[length++] = ((MQTT_KEEPALIVE) >> 8); + buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF); + length = writeString(id,buffer,length); + if (willTopic) { + length = writeString(willTopic,buffer,length); + length = writeString(willMessage,buffer,length); + } + + if(user != NULL) { + length = writeString(user,buffer,length); + if(pass != NULL) { + length = writeString(pass,buffer,length); + } + } + + write(MQTTCONNECT,buffer,length-5); + + lastInActivity = lastOutActivity = millis(); + + while (!_client->available()) { + unsigned long t = millis(); + if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) { + _state = MQTT_CONNECTION_TIMEOUT; + _client->stop(); + return false; + } + } + uint8_t llen; + uint16_t len = readPacket(&llen); + + if (len == 4) { + if (buffer[3] == 0) { + lastInActivity = millis(); + pingOutstanding = false; + _state = MQTT_CONNECTED; + return true; + } else { + _state = buffer[3]; + } + } + _client->stop(); + } else { + _state = MQTT_CONNECT_FAILED; + } + return false; + } + return true; +} + +// reads a byte into result +boolean PubSubClient::readByte(uint8_t * result) { + uint32_t previousMillis = millis(); + while(!_client->available()) { + uint32_t currentMillis = millis(); + if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){ + return false; + } + } + *result = _client->read(); + return true; +} + +// reads a byte into result[*index] and increments index +boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){ + uint16_t current_index = *index; + uint8_t * write_address = &(result[current_index]); + if(readByte(write_address)){ + *index = current_index + 1; + return true; + } + return false; +} + +uint16_t PubSubClient::readPacket(uint8_t* lengthLength) { + uint16_t len = 0; + if(!readByte(buffer, &len)) return 0; + bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH; + uint32_t multiplier = 1; + uint16_t length = 0; + uint8_t digit = 0; + uint16_t skip = 0; + uint8_t start = 0; + + do { + if(!readByte(&digit)) return 0; + buffer[len++] = digit; + length += (digit & 127) * multiplier; + multiplier *= 128; + } while ((digit & 128) != 0); + *lengthLength = len-1; + + if (isPublish) { + // Read in topic length to calculate bytes to skip over for Stream writing + if(!readByte(buffer, &len)) return 0; + if(!readByte(buffer, &len)) return 0; + skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2]; + start = 2; + if (buffer[0]&MQTTQOS1) { + // skip message id + skip += 2; + } + } + + for (uint16_t i = start;istream) { + if (isPublish && len-*lengthLength-2>skip) { + this->stream->write(digit); + } + } + if (len < MQTT_MAX_PACKET_SIZE) { + buffer[len] = digit; + } + len++; + } + + if (!this->stream && len > MQTT_MAX_PACKET_SIZE) { + len = 0; // This will cause the packet to be ignored. + } + + return len; +} + +boolean PubSubClient::loop() { + if (connected()) { + unsigned long t = millis(); + if ((t - lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - lastOutActivity > MQTT_KEEPALIVE*1000UL)) { + if (pingOutstanding) { + this->_state = MQTT_CONNECTION_TIMEOUT; + _client->stop(); + return false; + } else { + buffer[0] = MQTTPINGREQ; + buffer[1] = 0; + _client->write(buffer,2); + lastOutActivity = t; + lastInActivity = t; + pingOutstanding = true; + } + } + if (_client->available()) { + uint8_t llen; + uint16_t len = readPacket(&llen); + uint16_t msgId = 0; + uint8_t *payload; + if (len > 0) { + lastInActivity = t; + uint8_t type = buffer[0]&0xF0; + if (type == MQTTPUBLISH) { + if (callback) { + uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; + char topic[tl+1]; + for (uint16_t i=0;i0 + if ((buffer[0]&0x06) == MQTTQOS1) { + msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1]; + payload = buffer+llen+3+tl+2; + callback(topic,payload,len-llen-3-tl-2); + + buffer[0] = MQTTPUBACK; + buffer[1] = 2; + buffer[2] = (msgId >> 8); + buffer[3] = (msgId & 0xFF); + _client->write(buffer,4); + lastOutActivity = t; + + } else { + payload = buffer+llen+3+tl; + callback(topic,payload,len-llen-3-tl); + } + } + } else if (type == MQTTPINGREQ) { + buffer[0] = MQTTPINGRESP; + buffer[1] = 0; + _client->write(buffer,2); + } else if (type == MQTTPINGRESP) { + pingOutstanding = false; + } + } + } + return true; + } + return false; +} + +boolean PubSubClient::publish(const char* topic, const char* payload) { + return publish(topic,(const uint8_t*)payload,strlen(payload),false); +} + +boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) { + return publish(topic,(const uint8_t*)payload,strlen(payload),retained); +} + +boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) { + return publish(topic, payload, plength, false); +} + +boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) { + if (connected()) { + if (MQTT_MAX_PACKET_SIZE < 5 + 2+strlen(topic) + plength) { + // Too long + return false; + } + // Leave room in the buffer for header and variable length field + uint16_t length = 5; + length = writeString(topic,buffer,length); + uint16_t i; + for (i=0;i 0) { + digit |= 0x80; + } + buffer[pos++] = digit; + llen++; + } while(len>0); + + pos = writeString(topic,buffer,pos); + + rc += _client->write(buffer,pos); + + for (i=0;iwrite((char)pgm_read_byte_near(payload + i)); + } + + lastOutActivity = millis(); + + return rc == tlen + 4 + plength; +} + +boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { + uint8_t lenBuf[4]; + uint8_t llen = 0; + uint8_t digit; + uint8_t pos = 0; + uint16_t rc; + uint16_t len = length; + do { + digit = len % 128; + len = len / 128; + if (len > 0) { + digit |= 0x80; + } + lenBuf[pos++] = digit; + llen++; + } while(len>0); + + buf[4-llen] = header; + for (int i=0;i 0) && result) { + bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining; + rc = _client->write(writeBuf,bytesToWrite); + result = (rc == bytesToWrite); + bytesRemaining -= rc; + writeBuf += rc; + } + return result; +#else + rc = _client->write(buf+(4-llen),length+1+llen); + lastOutActivity = millis(); + return (rc == 1+llen+length); +#endif +} + +boolean PubSubClient::subscribe(const char* topic) { + return subscribe(topic, 0); +} + +boolean PubSubClient::subscribe(const char* topic, uint8_t qos) { + if (qos < 0 || qos > 1) { + return false; + } + if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { + // Too long + return false; + } + if (connected()) { + // Leave room in the buffer for header and variable length field + uint16_t length = 5; + nextMsgId++; + if (nextMsgId == 0) { + nextMsgId = 1; + } + buffer[length++] = (nextMsgId >> 8); + buffer[length++] = (nextMsgId & 0xFF); + length = writeString((char*)topic, buffer,length); + buffer[length++] = qos; + return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5); + } + return false; +} + +boolean PubSubClient::unsubscribe(const char* topic) { + if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { + // Too long + return false; + } + if (connected()) { + uint16_t length = 5; + nextMsgId++; + if (nextMsgId == 0) { + nextMsgId = 1; + } + buffer[length++] = (nextMsgId >> 8); + buffer[length++] = (nextMsgId & 0xFF); + length = writeString(topic, buffer,length); + return write(MQTTUNSUBSCRIBE|MQTTQOS1,buffer,length-5); + } + return false; +} + +void PubSubClient::disconnect() { + buffer[0] = MQTTDISCONNECT; + buffer[1] = 0; + _client->write(buffer,2); + _state = MQTT_DISCONNECTED; + _client->stop(); + lastInActivity = lastOutActivity = millis(); +} + +uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) { + const char* idp = string; + uint16_t i = 0; + pos += 2; + while (*idp) { + buf[pos++] = *idp++; + i++; + } + buf[pos-i-2] = (i >> 8); + buf[pos-i-1] = (i & 0xFF); + return pos; +} + + +boolean PubSubClient::connected() { + boolean rc; + if (_client == NULL ) { + rc = false; + } else { + rc = (int)_client->connected(); + if (!rc) { + if (this->_state == MQTT_CONNECTED) { + this->_state = MQTT_CONNECTION_LOST; + _client->flush(); + _client->stop(); + } + } + } + return rc; +} + +PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) { + IPAddress addr(ip[0],ip[1],ip[2],ip[3]); + return setServer(addr,port); +} + +PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) { + this->ip = ip; + this->port = port; + this->domain = NULL; + return *this; +} + +PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) { + this->domain = domain; + this->port = port; + return *this; +} + +PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) { + this->callback = callback; + return *this; +} + +PubSubClient& PubSubClient::setClient(Client& client){ + this->_client = &client; + return *this; +} + +PubSubClient& PubSubClient::setStream(Stream& stream){ + this->stream = &stream; + return *this; +} + +int PubSubClient::state() { + return this->_state; +} diff --git a/libraries/PubSubClient/src/PubSubClient.h b/libraries/PubSubClient/src/PubSubClient.h new file mode 100644 index 0000000..be4bd67 --- /dev/null +++ b/libraries/PubSubClient/src/PubSubClient.h @@ -0,0 +1,144 @@ +/* + PubSubClient.h - A simple client for MQTT. + Nick O'Leary + http://knolleary.net +*/ + +#ifndef PubSubClient_h +#define PubSubClient_h + +#include +#include "IPAddress.h" +#include "Client.h" +#include "Stream.h" + +#define MQTT_VERSION_3_1 3 +#define MQTT_VERSION_3_1_1 4 + +// MQTT_VERSION : Pick the version +//#define MQTT_VERSION MQTT_VERSION_3_1 +#ifndef MQTT_VERSION +#define MQTT_VERSION MQTT_VERSION_3_1_1 +#endif + +// MQTT_MAX_PACKET_SIZE : Maximum packet size +#ifndef MQTT_MAX_PACKET_SIZE +#define MQTT_MAX_PACKET_SIZE 128 +#endif + +// MQTT_KEEPALIVE : keepAlive interval in Seconds +#ifndef MQTT_KEEPALIVE +#define MQTT_KEEPALIVE 15 +#endif + +// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds +#ifndef MQTT_SOCKET_TIMEOUT +#define MQTT_SOCKET_TIMEOUT 15 +#endif + +// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client +// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to +// pass the entire MQTT packet in each write call. +//#define MQTT_MAX_TRANSFER_SIZE 80 + +// Possible values for client.state() +#define MQTT_CONNECTION_TIMEOUT -4 +#define MQTT_CONNECTION_LOST -3 +#define MQTT_CONNECT_FAILED -2 +#define MQTT_DISCONNECTED -1 +#define MQTT_CONNECTED 0 +#define MQTT_CONNECT_BAD_PROTOCOL 1 +#define MQTT_CONNECT_BAD_CLIENT_ID 2 +#define MQTT_CONNECT_UNAVAILABLE 3 +#define MQTT_CONNECT_BAD_CREDENTIALS 4 +#define MQTT_CONNECT_UNAUTHORIZED 5 + +#define MQTTCONNECT 1 << 4 // Client request to connect to Server +#define MQTTCONNACK 2 << 4 // Connect Acknowledgment +#define MQTTPUBLISH 3 << 4 // Publish message +#define MQTTPUBACK 4 << 4 // Publish Acknowledgment +#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1) +#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2) +#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3) +#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request +#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment +#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request +#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment +#define MQTTPINGREQ 12 << 4 // PING Request +#define MQTTPINGRESP 13 << 4 // PING Response +#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting +#define MQTTReserved 15 << 4 // Reserved + +#define MQTTQOS0 (0 << 1) +#define MQTTQOS1 (1 << 1) +#define MQTTQOS2 (2 << 1) + +#ifdef ESP8266 +#include +#define MQTT_CALLBACK_SIGNATURE std::function callback +#else +#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int) +#endif + +class PubSubClient { +private: + Client* _client; + uint8_t buffer[MQTT_MAX_PACKET_SIZE]; + uint16_t nextMsgId; + unsigned long lastOutActivity; + unsigned long lastInActivity; + bool pingOutstanding; + MQTT_CALLBACK_SIGNATURE; + uint16_t readPacket(uint8_t*); + boolean readByte(uint8_t * result); + boolean readByte(uint8_t * result, uint16_t * index); + boolean write(uint8_t header, uint8_t* buf, uint16_t length); + uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos); + IPAddress ip; + const char* domain; + uint16_t port; + Stream* stream; + int _state; +public: + PubSubClient(); + PubSubClient(Client& client); + PubSubClient(IPAddress, uint16_t, Client& client); + PubSubClient(IPAddress, uint16_t, Client& client, Stream&); + PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); + PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); + PubSubClient(uint8_t *, uint16_t, Client& client); + PubSubClient(uint8_t *, uint16_t, Client& client, Stream&); + PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); + PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); + PubSubClient(const char*, uint16_t, Client& client); + PubSubClient(const char*, uint16_t, Client& client, Stream&); + PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); + PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&); + + PubSubClient& setServer(IPAddress ip, uint16_t port); + PubSubClient& setServer(uint8_t * ip, uint16_t port); + PubSubClient& setServer(const char * domain, uint16_t port); + PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE); + PubSubClient& setClient(Client& client); + PubSubClient& setStream(Stream& stream); + + boolean connect(const char* id); + boolean connect(const char* id, const char* user, const char* pass); + boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage); + boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage); + void disconnect(); + boolean publish(const char* topic, const char* payload); + boolean publish(const char* topic, const char* payload, boolean retained); + boolean publish(const char* topic, const uint8_t * payload, unsigned int plength); + boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained); + boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained); + boolean subscribe(const char* topic); + boolean subscribe(const char* topic, uint8_t qos); + boolean unsubscribe(const char* topic); + boolean loop(); + boolean connected(); + int state(); +}; + + +#endif diff --git a/libraries/PubSubClient/tests/.gitignore b/libraries/PubSubClient/tests/.gitignore new file mode 100644 index 0000000..215de78 --- /dev/null +++ b/libraries/PubSubClient/tests/.gitignore @@ -0,0 +1,4 @@ +.build +tmpbin +logs +*.pyc diff --git a/libraries/PubSubClient/tests/Makefile b/libraries/PubSubClient/tests/Makefile new file mode 100644 index 0000000..1f71636 --- /dev/null +++ b/libraries/PubSubClient/tests/Makefile @@ -0,0 +1,25 @@ +SRC_PATH=./src +OUT_PATH=./bin +TEST_SRC=$(wildcard ${SRC_PATH}/*_spec.cpp) +TEST_BIN= $(TEST_SRC:${SRC_PATH}/%.cpp=${OUT_PATH}/%) +VPATH=${SRC_PATH} +SHIM_FILES=${SRC_PATH}/lib/*.cpp +PSC_FILE=../src/PubSubClient.cpp +CC=g++ +CFLAGS=-I${SRC_PATH}/lib -I../src + +all: $(TEST_BIN) + +${OUT_PATH}/%: ${SRC_PATH}/%.cpp ${PSC_FILE} ${SHIM_FILES} + mkdir -p ${OUT_PATH} + ${CC} ${CFLAGS} $^ -o $@ + +clean: + @rm -rf ${OUT_PATH} + +test: + @bin/connect_spec + @bin/publish_spec + @bin/receive_spec + @bin/subscribe_spec + @bin/keepalive_spec diff --git a/libraries/PubSubClient/tests/README.md b/libraries/PubSubClient/tests/README.md new file mode 100644 index 0000000..e5700a6 --- /dev/null +++ b/libraries/PubSubClient/tests/README.md @@ -0,0 +1,93 @@ +# Arduino Client for MQTT Test Suite + +This is a regression test suite for the `PubSubClient` library. + +There are two parts: + + - Tests that can be compiled and run on any machine + - Tests that build the example sketches using the Arduino IDE + + +It is a work-in-progress and is subject to complete refactoring as the whim takes +me. + + +## Local tests + +These are a set of executables that can be run to test specific areas of functionality. +They do not require a real Arduino to be attached, nor the use of the Arduino IDE. + +The tests include a set of mock files to stub out the parts of the Arduino environment the library +depends on. + +### Dependencies + + - g++ + +### Running + +Build the tests using the provided `Makefile`: + + $ make + +This will create a set of executables in `./bin/`. Run each of these executables to test the corresponding functionality. + +*Note:* the `connect_spec` and `keepalive_spec` tests involve testing keepalive timers so naturally take a few minutes to run through. + +## Arduino tests + +*Note:* INO Tool doesn't currently play nicely with Arduino 1.5. This has broken this test suite. + +Without a suitable arduino plugged in, the test suite will only check the +example sketches compile cleanly against the library. + +With an arduino plugged in, each sketch that has a corresponding python +test case is built, uploaded and then the tests run. + +### Dependencies + + - Python 2.7+ + - [INO Tool](http://inotool.org/) - this provides command-line build/upload of Arduino sketches + +### Running + +The test suite _does not_ run an MQTT server - it is assumed to be running already. + + $ python testsuite.py + +A summary of activity is printed to the console. More comprehensive logs are written +to the `logs` directory. + +### What it does + +For each sketch in the library's `examples` directory, e.g. `mqtt_basic.ino`, the suite looks for a matching test case +`testcases/mqtt_basic.py`. + +The test case must follow these conventions: + - sub-class `unittest.TestCase` + - provide the class methods `setUpClass` and `tearDownClass` (TODO: make this optional) + - all test method names begin with `test_` + +The suite will call the `setUpClass` method _before_ uploading the sketch. This +allows any test setup to be performed before the sketch runs - such as connecting +a client and subscribing to topics. + + +### Settings + +The file `testcases/settings.py` is used to config the test environment. + + - `server_ip` - the IP address of the broker the client should connect to (the broker port is assumed to be 1883). + - `arduino_ip` - the IP address the arduino should use (when not testing DHCP). + +Before each sketch is compiled, these values are automatically substituted in. To +do this, the suite looks for lines that _start_ with the following: + + byte server[] = { + byte ip[] = { + +and replaces them with the appropriate values. + + + + diff --git a/libraries/PubSubClient/tests/src/connect_spec.cpp b/libraries/PubSubClient/tests/src/connect_spec.cpp new file mode 100644 index 0000000..69f1864 --- /dev/null +++ b/libraries/PubSubClient/tests/src/connect_spec.cpp @@ -0,0 +1,256 @@ +#include "PubSubClient.h" +#include "ShimClient.h" +#include "Buffer.h" +#include "BDDTest.h" +#include "trace.h" + + +byte server[] = { 172, 16, 0, 2 }; + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + + +int test_connect_fails_no_network() { + IT("fails to connect if underlying client doesn't connect"); + ShimClient shimClient; + shimClient.setAllowConnect(false); + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_FALSE(rc); + int state = client.state(); + IS_TRUE(state == MQTT_CONNECT_FAILED); + END_IT +} + +int test_connect_fails_on_no_response() { + IT("fails to connect if no response received after 15 seconds"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_FALSE(rc); + int state = client.state(); + IS_TRUE(state == MQTT_CONNECTION_TIMEOUT); + END_IT +} + +int test_connect_properly_formatted() { + IT("sends a properly formatted connect packet and succeeds"); + ShimClient shimClient; + + shimClient.setAllowConnect(true); + byte expectServer[] = { 172, 16, 0, 2 }; + shimClient.expectConnect(expectServer,1883); + byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + + shimClient.expect(connect,26); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int state = client.state(); + IS_TRUE(state == MQTT_DISCONNECTED); + + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + state = client.state(); + IS_TRUE(state == MQTT_CONNECTED); + + END_IT +} + +int test_connect_properly_formatted_hostname() { + IT("accepts a hostname"); + ShimClient shimClient; + + shimClient.setAllowConnect(true); + shimClient.expectConnect((char* const)"localhost",1883); + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client((char* const)"localhost", 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + + +int test_connect_fails_on_bad_rc() { + IT("fails to connect if a bad return code is received"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + byte connack[] = { 0x20, 0x02, 0x00, 0x01 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_FALSE(rc); + + int state = client.state(); + IS_TRUE(state == 0x01); + + END_IT +} + +int test_connect_accepts_username_password() { + IT("accepts a username and password"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connect[] = { 0x10,0x24,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xc2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x4,0x70,0x61,0x73,0x73}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.expect(connect,0x26); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_connect_accepts_username_no_password() { + IT("accepts a username but no password"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connect[] = { 0x10,0x1e,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x82,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x4,0x75,0x73,0x65,0x72}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.expect(connect,0x20); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1",(char*)"user",0); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_connect_ignores_password_no_username() { + IT("ignores a password but no username"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.expect(connect,26); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1",0,(char*)"pass"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_connect_with_will() { + IT("accepts a will"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connect[] = {0x10,0x30,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xe,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.expect(connect,0x32); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_connect_with_will_username_password() { + IT("accepts a will, username and password"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connect[] = {0x10,0x40,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0xce,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31,0x0,0x9,0x77,0x69,0x6c,0x6c,0x54,0x6f,0x70,0x69,0x63,0x0,0xb,0x77,0x69,0x6c,0x6c,0x4d,0x65,0x73,0x73,0x61,0x67,0x65,0x0,0x4,0x75,0x73,0x65,0x72,0x0,0x8,0x70,0x61,0x73,0x73,0x77,0x6f,0x72,0x64}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.expect(connect,0x42); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_connect_disconnect_connect() { + IT("connects, disconnects and connects again"); + ShimClient shimClient; + + shimClient.setAllowConnect(true); + byte expectServer[] = { 172, 16, 0, 2 }; + shimClient.expectConnect(expectServer,1883); + byte connect[] = {0x10,0x18,0x0,0x4,0x4d,0x51,0x54,0x54,0x4,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + + shimClient.expect(connect,26); + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + + int state = client.state(); + IS_TRUE(state == MQTT_DISCONNECTED); + + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + + state = client.state(); + IS_TRUE(state == MQTT_CONNECTED); + + byte disconnect[] = {0xE0,0x00}; + shimClient.expect(disconnect,2); + + client.disconnect(); + + IS_FALSE(client.connected()); + IS_FALSE(shimClient.connected()); + IS_FALSE(shimClient.error()); + + state = client.state(); + IS_TRUE(state == MQTT_DISCONNECTED); + + shimClient.expect(connect,28); + shimClient.respond(connack,4); + rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + state = client.state(); + IS_TRUE(state == MQTT_CONNECTED); + + END_IT +} + +int main() +{ + SUITE("Connect"); + test_connect_fails_no_network(); + test_connect_fails_on_no_response(); + + test_connect_properly_formatted(); + test_connect_accepts_username_password(); + test_connect_fails_on_bad_rc(); + test_connect_properly_formatted_hostname(); + + test_connect_accepts_username_no_password(); + test_connect_ignores_password_no_username(); + test_connect_with_will(); + test_connect_with_will_username_password(); + test_connect_disconnect_connect(); + FINISH +} diff --git a/libraries/PubSubClient/tests/src/keepalive_spec.cpp b/libraries/PubSubClient/tests/src/keepalive_spec.cpp new file mode 100644 index 0000000..ea643cf --- /dev/null +++ b/libraries/PubSubClient/tests/src/keepalive_spec.cpp @@ -0,0 +1,185 @@ +#include "PubSubClient.h" +#include "ShimClient.h" +#include "Buffer.h" +#include "BDDTest.h" +#include "trace.h" +#include + +byte server[] = { 172, 16, 0, 2 }; + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + + +int test_keepalive_pings_idle() { + IT("keeps an idle connection alive (takes 1 minute)"); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte pingreq[] = { 0xC0,0x0 }; + shimClient.expect(pingreq,2); + byte pingresp[] = { 0xD0,0x0 }; + shimClient.respond(pingresp,2); + + for (int i = 0; i < 50; i++) { + sleep(1); + if ( i == 15 || i == 31 || i == 47) { + shimClient.expect(pingreq,2); + shimClient.respond(pingresp,2); + } + rc = client.loop(); + IS_TRUE(rc); + } + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_keepalive_pings_with_outbound_qos0() { + IT("keeps a connection alive that only sends qos0 (takes 1 minute)"); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + + for (int i = 0; i < 50; i++) { + TRACE(i<<":"); + shimClient.expect(publish,16); + rc = client.publish((char*)"topic",(char*)"payload"); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + sleep(1); + if ( i == 15 || i == 31 || i == 47) { + byte pingreq[] = { 0xC0,0x0 }; + shimClient.expect(pingreq,2); + byte pingresp[] = { 0xD0,0x0 }; + shimClient.respond(pingresp,2); + } + rc = client.loop(); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + } + + END_IT +} + +int test_keepalive_pings_with_inbound_qos0() { + IT("keeps a connection alive that only receives qos0 (takes 1 minute)"); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + + for (int i = 0; i < 50; i++) { + TRACE(i<<":"); + sleep(1); + if ( i == 15 || i == 31 || i == 47) { + byte pingreq[] = { 0xC0,0x0 }; + shimClient.expect(pingreq,2); + byte pingresp[] = { 0xD0,0x0 }; + shimClient.respond(pingresp,2); + } + shimClient.respond(publish,16); + rc = client.loop(); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + } + + END_IT +} + +int test_keepalive_no_pings_inbound_qos1() { + IT("does not send pings for connections with inbound qos1 (takes 1 minute)"); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + byte puback[] = {0x40,0x2,0x12,0x34}; + + for (int i = 0; i < 50; i++) { + shimClient.respond(publish,18); + shimClient.expect(puback,4); + sleep(1); + rc = client.loop(); + IS_TRUE(rc); + IS_FALSE(shimClient.error()); + } + + END_IT +} + +int test_keepalive_disconnects_hung() { + IT("disconnects a hung connection (takes 30 seconds)"); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte pingreq[] = { 0xC0,0x0 }; + shimClient.expect(pingreq,2); + + for (int i = 0; i < 32; i++) { + sleep(1); + rc = client.loop(); + } + IS_FALSE(rc); + + int state = client.state(); + IS_TRUE(state == MQTT_CONNECTION_TIMEOUT); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int main() +{ + SUITE("Keep-alive"); + test_keepalive_pings_idle(); + test_keepalive_pings_with_outbound_qos0(); + test_keepalive_pings_with_inbound_qos0(); + test_keepalive_no_pings_inbound_qos1(); + test_keepalive_disconnects_hung(); + + FINISH +} diff --git a/libraries/PubSubClient/tests/src/lib/Arduino.h b/libraries/PubSubClient/tests/src/lib/Arduino.h new file mode 100644 index 0000000..c675280 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Arduino.h @@ -0,0 +1,23 @@ +#ifndef Arduino_h +#define Arduino_h + +#include +#include +#include +#include + + +extern "C"{ + typedef uint8_t byte ; + typedef uint8_t boolean ; + + /* sketch */ + extern void setup( void ) ; + extern void loop( void ) ; + uint32_t millis( void ); +} + +#define PROGMEM +#define pgm_read_byte_near(x) *(x) + +#endif // Arduino_h diff --git a/libraries/PubSubClient/tests/src/lib/BDDTest.cpp b/libraries/PubSubClient/tests/src/lib/BDDTest.cpp new file mode 100644 index 0000000..a72bf65 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/BDDTest.cpp @@ -0,0 +1,50 @@ +#include "BDDTest.h" +#include "trace.h" +#include +#include +#include +#include + +int testCount = 0; +int testPasses = 0; +const char* testDescription; + +std::list failureList; + +void bddtest_suite(const char* name) { + LOG(name << "\n"); +} + +int bddtest_test(const char* file, int line, const char* assertion, int result) { + if (!result) { + LOG("✗\n"); + std::ostringstream os; + os << " ! "<::iterator it = failureList.begin(); it != failureList.end(); it++) { + LOG("\n"); + LOG(*it); + LOG("\n"); + } + + LOG(std::dec << testPasses << "/" << testCount << " tests passed\n\n"); + if (testPasses == testCount) { + return 0; + } + return 1; +} diff --git a/libraries/PubSubClient/tests/src/lib/BDDTest.h b/libraries/PubSubClient/tests/src/lib/BDDTest.h new file mode 100644 index 0000000..1197fdd --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/BDDTest.h @@ -0,0 +1,23 @@ +#ifndef bddtest_h +#define bddtest_h + +void bddtest_suite(const char* name); +int bddtest_test(const char*, int, const char*, int); +void bddtest_start(const char*); +void bddtest_end(); +int bddtest_summary(); + +#define SUITE(x) { bddtest_suite(x); } +#define TEST(x) { if (!bddtest_test(__FILE__, __LINE__, #x, (x))) return false; } + +#define IT(x) { bddtest_start(x); } +#define END_IT { bddtest_end();return true;} + +#define FINISH { return bddtest_summary(); } + +#define IS_TRUE(x) TEST(x) +#define IS_FALSE(x) TEST(!(x)) +#define IS_EQUAL(x,y) TEST(x==y) +#define IS_NOT_EQUAL(x,y) TEST(x!=y) + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/Buffer.cpp b/libraries/PubSubClient/tests/src/lib/Buffer.cpp new file mode 100644 index 0000000..59a2fbb --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Buffer.cpp @@ -0,0 +1,30 @@ +#include "Buffer.h" +#include "Arduino.h" + +Buffer::Buffer() { +} + +Buffer::Buffer(uint8_t* buf, size_t size) { + this->add(buf,size); +} +bool Buffer::available() { + return this->pos < this->length; +} + +uint8_t Buffer::next() { + if (this->available()) { + return this->buffer[this->pos++]; + } + return 0; +} + +void Buffer::reset() { + this->pos = 0; +} + +void Buffer::add(uint8_t* buf, size_t size) { + uint16_t i = 0; + for (;ibuffer[this->length++] = buf[i]; + } +} diff --git a/libraries/PubSubClient/tests/src/lib/Buffer.h b/libraries/PubSubClient/tests/src/lib/Buffer.h new file mode 100644 index 0000000..f448cad --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Buffer.h @@ -0,0 +1,23 @@ +#ifndef buffer_h +#define buffer_h + +#include "Arduino.h" + +class Buffer { +private: + uint8_t buffer[1024]; + uint16_t pos; + uint16_t length; + +public: + Buffer(); + Buffer(uint8_t* buf, size_t size); + + virtual bool available(); + virtual uint8_t next(); + virtual void reset(); + + virtual void add(uint8_t* buf, size_t size); +}; + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/Client.h b/libraries/PubSubClient/tests/src/lib/Client.h new file mode 100644 index 0000000..9e18c07 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Client.h @@ -0,0 +1,21 @@ +#ifndef client_h +#define client_h +#include "IPAddress.h" + +class Client { +public: + virtual int connect(IPAddress ip, uint16_t port) =0; + virtual int connect(const char *host, uint16_t port) =0; + virtual size_t write(uint8_t) =0; + virtual size_t write(const uint8_t *buf, size_t size) =0; + virtual int available() = 0; + virtual int read() = 0; + virtual int read(uint8_t *buf, size_t size) = 0; + virtual int peek() = 0; + virtual void flush() = 0; + virtual void stop() = 0; + virtual uint8_t connected() = 0; + virtual operator bool() = 0; +}; + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/IPAddress.cpp b/libraries/PubSubClient/tests/src/lib/IPAddress.cpp new file mode 100644 index 0000000..610ff4c --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/IPAddress.cpp @@ -0,0 +1,44 @@ + +#include +#include + +IPAddress::IPAddress() +{ + memset(_address, 0, sizeof(_address)); +} + +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) +{ + _address[0] = first_octet; + _address[1] = second_octet; + _address[2] = third_octet; + _address[3] = fourth_octet; +} + +IPAddress::IPAddress(uint32_t address) +{ + memcpy(_address, &address, sizeof(_address)); +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address, address, sizeof(_address)); +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address, address, sizeof(_address)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + memcpy(_address, (const uint8_t *)&address, sizeof(_address)); + return *this; +} + +bool IPAddress::operator==(const uint8_t* addr) +{ + return memcmp(addr, _address, sizeof(_address)) == 0; +} + diff --git a/libraries/PubSubClient/tests/src/lib/IPAddress.h b/libraries/PubSubClient/tests/src/lib/IPAddress.h new file mode 100644 index 0000000..e75a8fe --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/IPAddress.h @@ -0,0 +1,72 @@ +/* + * + * MIT License: + * Copyright (c) 2011 Adrian McEwen + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * adrianm@mcqn.com 1/1/2011 + */ + +#ifndef IPAddress_h +#define IPAddress_h + + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress { +private: + uint8_t _address[4]; // IPv4 address + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() { return _address; }; + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t *address); + + // Overloaded cast operator to allow IPAddress objects to be used where a pointer + // to a four-byte uint8_t array is expected + operator uint32_t() { return *((uint32_t*)_address); }; + bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; + bool operator==(const uint8_t* addr); + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address[index]; }; + uint8_t& operator[](int index) { return _address[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/ShimClient.cpp b/libraries/PubSubClient/tests/src/lib/ShimClient.cpp new file mode 100644 index 0000000..f70115f --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/ShimClient.cpp @@ -0,0 +1,153 @@ +#include "ShimClient.h" +#include "trace.h" +#include +#include +#include + +extern "C" { + uint32_t millis(void) { + return time(0)*1000; + } +} + +ShimClient::ShimClient() { + this->responseBuffer = new Buffer(); + this->expectBuffer = new Buffer(); + this->_allowConnect = true; + this->_connected = false; + this->_error = false; + this->expectAnything = true; + this->_received = 0; + this->_expectedPort = 0; +} + +int ShimClient::connect(IPAddress ip, uint16_t port) { + if (this->_allowConnect) { + this->_connected = true; + } + if (this->_expectedPort !=0) { + // if (memcmp(ip,this->_expectedIP,4) != 0) { + // TRACE( "ip mismatch\n"); + // this->_error = true; + // } + if (port != this->_expectedPort) { + TRACE( "port mismatch\n"); + this->_error = true; + } + } + return this->_connected; +} +int ShimClient::connect(const char *host, uint16_t port) { + if (this->_allowConnect) { + this->_connected = true; + } + if (this->_expectedPort !=0) { + if (strcmp(host,this->_expectedHost) != 0) { + TRACE( "host mismatch\n"); + this->_error = true; + } + if (port != this->_expectedPort) { + TRACE( "port mismatch\n"); + this->_error = true; + } + + } + return this->_connected; +} +size_t ShimClient::write(uint8_t b) { + this->_received += 1; + TRACE(std::hex << (unsigned int)b); + if (!this->expectAnything) { + if (this->expectBuffer->available()) { + uint8_t expected = this->expectBuffer->next(); + if (expected != b) { + this->_error = true; + TRACE("!=" << (unsigned int)expected); + } + } else { + this->_error = true; + } + } + TRACE("\n"<< std::dec); + return 1; +} +size_t ShimClient::write(const uint8_t *buf, size_t size) { + this->_received += size; + TRACE( "[" << std::dec << (unsigned int)(size) << "] "); + uint16_t i=0; + for (;i0) { + TRACE(":"); + } + TRACE(std::hex << (unsigned int)(buf[i])); + + if (!this->expectAnything) { + if (this->expectBuffer->available()) { + uint8_t expected = this->expectBuffer->next(); + if (expected != buf[i]) { + this->_error = true; + TRACE("!=" << (unsigned int)expected); + } + } else { + this->_error = true; + } + } + } + TRACE("\n"<responseBuffer->available(); +} +int ShimClient::read() { return this->responseBuffer->next(); } +int ShimClient::read(uint8_t *buf, size_t size) { + uint16_t i = 0; + for (;iread(); + } + return size; +} +int ShimClient::peek() { return 0; } +void ShimClient::flush() {} +void ShimClient::stop() { + this->setConnected(false); +} +uint8_t ShimClient::connected() { return this->_connected; } +ShimClient::operator bool() { return true; } + + +ShimClient* ShimClient::respond(uint8_t *buf, size_t size) { + this->responseBuffer->add(buf,size); + return this; +} + +ShimClient* ShimClient::expect(uint8_t *buf, size_t size) { + this->expectAnything = false; + this->expectBuffer->add(buf,size); + return this; +} + +void ShimClient::setConnected(bool b) { + this->_connected = b; +} +void ShimClient::setAllowConnect(bool b) { + this->_allowConnect = b; +} + +bool ShimClient::error() { + return this->_error; +} + +uint16_t ShimClient::received() { + return this->_received; +} + +void ShimClient::expectConnect(IPAddress ip, uint16_t port) { + this->_expectedIP = ip; + this->_expectedPort = port; +} + +void ShimClient::expectConnect(const char *host, uint16_t port) { + this->_expectedHost = host; + this->_expectedPort = port; +} diff --git a/libraries/PubSubClient/tests/src/lib/ShimClient.h b/libraries/PubSubClient/tests/src/lib/ShimClient.h new file mode 100644 index 0000000..2e3f874 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/ShimClient.h @@ -0,0 +1,51 @@ +#ifndef shimclient_h +#define shimclient_h + +#include "Arduino.h" +#include "Client.h" +#include "IPAddress.h" +#include "Buffer.h" + + +class ShimClient : public Client { +private: + Buffer* responseBuffer; + Buffer* expectBuffer; + bool _allowConnect; + bool _connected; + bool expectAnything; + bool _error; + uint16_t _received; + IPAddress _expectedIP; + uint16_t _expectedPort; + const char* _expectedHost; + +public: + ShimClient(); + virtual int connect(IPAddress ip, uint16_t port); + virtual int connect(const char *host, uint16_t port); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + virtual int available(); + virtual int read(); + virtual int read(uint8_t *buf, size_t size); + virtual int peek(); + virtual void flush(); + virtual void stop(); + virtual uint8_t connected(); + virtual operator bool(); + + virtual ShimClient* respond(uint8_t *buf, size_t size); + virtual ShimClient* expect(uint8_t *buf, size_t size); + + virtual void expectConnect(IPAddress ip, uint16_t port); + virtual void expectConnect(const char *host, uint16_t port); + + virtual uint16_t received(); + virtual bool error(); + + virtual void setAllowConnect(bool b); + virtual void setConnected(bool b); +}; + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/Stream.cpp b/libraries/PubSubClient/tests/src/lib/Stream.cpp new file mode 100644 index 0000000..b0ecbb4 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Stream.cpp @@ -0,0 +1,39 @@ +#include "Stream.h" +#include "trace.h" +#include +#include + +Stream::Stream() { + this->expectBuffer = new Buffer(); + this->_error = false; + this->_written = 0; +} + +size_t Stream::write(uint8_t b) { + this->_written++; + TRACE(std::hex << (unsigned int)b); + if (this->expectBuffer->available()) { + uint8_t expected = this->expectBuffer->next(); + if (expected != b) { + this->_error = true; + TRACE("!=" << (unsigned int)expected); + } + } else { + this->_error = true; + } + TRACE("\n"<< std::dec); + return 1; +} + + +bool Stream::error() { + return this->_error; +} + +void Stream::expect(uint8_t *buf, size_t size) { + this->expectBuffer->add(buf,size); +} + +uint16_t Stream::length() { + return this->_written; +} diff --git a/libraries/PubSubClient/tests/src/lib/Stream.h b/libraries/PubSubClient/tests/src/lib/Stream.h new file mode 100644 index 0000000..4e41f86 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/Stream.h @@ -0,0 +1,22 @@ +#ifndef Stream_h +#define Stream_h + +#include "Arduino.h" +#include "Buffer.h" + +class Stream { +private: + Buffer* expectBuffer; + bool _error; + uint16_t _written; + +public: + Stream(); + virtual size_t write(uint8_t); + + virtual bool error(); + virtual void expect(uint8_t *buf, size_t size); + virtual uint16_t length(); +}; + +#endif diff --git a/libraries/PubSubClient/tests/src/lib/trace.h b/libraries/PubSubClient/tests/src/lib/trace.h new file mode 100644 index 0000000..42eb991 --- /dev/null +++ b/libraries/PubSubClient/tests/src/lib/trace.h @@ -0,0 +1,10 @@ +#ifndef trace_h +#define trace_h +#include + +#include + +#define LOG(x) {std::cout << x << std::flush; } +#define TRACE(x) {if (getenv("TRACE")) { std::cout << x << std::flush; }} + +#endif diff --git a/libraries/PubSubClient/tests/src/publish_spec.cpp b/libraries/PubSubClient/tests/src/publish_spec.cpp new file mode 100644 index 0000000..232df0d --- /dev/null +++ b/libraries/PubSubClient/tests/src/publish_spec.cpp @@ -0,0 +1,190 @@ +#include "PubSubClient.h" +#include "ShimClient.h" +#include "Buffer.h" +#include "BDDTest.h" +#include "trace.h" + + +byte server[] = { 172, 16, 0, 2 }; + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + +int test_publish() { + IT("publishes a null-terminated string"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + shimClient.expect(publish,16); + + rc = client.publish((char*)"topic",(char*)"payload"); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + + +int test_publish_bytes() { + IT("publishes a byte array"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; + int length = 5; + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; + shimClient.expect(publish,14); + + rc = client.publish((char*)"topic",payload,length); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + + +int test_publish_retained() { + IT("publishes retained - 1"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; + int length = 5; + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; + shimClient.expect(publish,14); + + rc = client.publish((char*)"topic",payload,length,true); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_publish_retained_2() { + IT("publishes retained - 2"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'}; + shimClient.expect(publish,14); + + rc = client.publish((char*)"topic",(char*)"ABCDE",true); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_publish_not_connected() { + IT("publish fails when not connected"); + ShimClient shimClient; + + PubSubClient client(server, 1883, callback, shimClient); + + int rc = client.publish((char*)"topic",(char*)"payload"); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_publish_too_long() { + IT("publish fails when topic/payload are too long"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + // 0 1 2 3 4 5 6 7 8 9 0 1 2 + rc = client.publish((char*)"topic",(char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_publish_P() { + IT("publishes using PROGMEM"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte payload[] = { 0x01,0x02,0x03,0x0,0x05 }; + int length = 5; + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1,0x2,0x3,0x0,0x5}; + shimClient.expect(publish,14); + + rc = client.publish_P((char*)"topic",payload,length,true); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + + + + +int main() +{ + SUITE("Publish"); + test_publish(); + test_publish_bytes(); + test_publish_retained(); + test_publish_retained_2(); + test_publish_not_connected(); + test_publish_too_long(); + test_publish_P(); + + FINISH +} diff --git a/libraries/PubSubClient/tests/src/receive_spec.cpp b/libraries/PubSubClient/tests/src/receive_spec.cpp new file mode 100644 index 0000000..54a62ee --- /dev/null +++ b/libraries/PubSubClient/tests/src/receive_spec.cpp @@ -0,0 +1,249 @@ +#include "PubSubClient.h" +#include "ShimClient.h" +#include "Buffer.h" +#include "BDDTest.h" +#include "trace.h" + + +byte server[] = { 172, 16, 0, 2 }; + +bool callback_called = false; +char lastTopic[1024]; +char lastPayload[1024]; +unsigned int lastLength; + +void reset_callback() { + callback_called = false; + lastTopic[0] = '\0'; + lastPayload[0] = '\0'; + lastLength = 0; +} + +void callback(char* topic, byte* payload, unsigned int length) { + callback_called = true; + strcpy(lastTopic,topic); + memcpy(lastPayload,payload,length); + lastLength = length; +} + +int test_receive_callback() { + IT("receives a callback message"); + reset_callback(); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + shimClient.respond(publish,16); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_TRUE(callback_called); + IS_TRUE(strcmp(lastTopic,"topic")==0); + IS_TRUE(memcmp(lastPayload,"payload",7)==0); + IS_TRUE(lastLength == 7); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_receive_stream() { + IT("receives a streamed callback message"); + reset_callback(); + + Stream stream; + stream.expect((uint8_t*)"payload",7); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient, stream); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + shimClient.respond(publish,16); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_TRUE(callback_called); + IS_TRUE(strcmp(lastTopic,"topic")==0); + IS_TRUE(lastLength == 7); + + IS_FALSE(stream.error()); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_receive_max_sized_message() { + IT("receives an max-sized message"); + reset_callback(); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + int length = MQTT_MAX_PACKET_SIZE; + byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + byte bigPublish[length]; + memset(bigPublish,'A',length); + bigPublish[length] = 'B'; + memcpy(bigPublish,publish,16); + shimClient.respond(bigPublish,length); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_TRUE(callback_called); + IS_TRUE(strcmp(lastTopic,"topic")==0); + IS_TRUE(lastLength == length-9); + IS_TRUE(memcmp(lastPayload,bigPublish+9,lastLength)==0); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_receive_oversized_message() { + IT("drops an oversized message"); + reset_callback(); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + int length = MQTT_MAX_PACKET_SIZE+1; + byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + byte bigPublish[length]; + memset(bigPublish,'A',length); + bigPublish[length] = 'B'; + memcpy(bigPublish,publish,16); + shimClient.respond(bigPublish,length); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_FALSE(callback_called); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_receive_oversized_stream_message() { + IT("drops an oversized message"); + reset_callback(); + + Stream stream; + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient, stream); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + int length = MQTT_MAX_PACKET_SIZE+1; + byte publish[] = {0x30,length-2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + + byte bigPublish[length]; + memset(bigPublish,'A',length); + bigPublish[length] = 'B'; + memcpy(bigPublish,publish,16); + + shimClient.respond(bigPublish,length); + stream.expect(bigPublish+9,length-9); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_TRUE(callback_called); + IS_TRUE(strcmp(lastTopic,"topic")==0); + IS_TRUE(lastLength == length-9); + + IS_FALSE(stream.error()); + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_receive_qos1() { + IT("receives a qos1 message"); + reset_callback(); + + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64}; + shimClient.respond(publish,18); + + byte puback[] = {0x40,0x2,0x12,0x34}; + shimClient.expect(puback,4); + + rc = client.loop(); + + IS_TRUE(rc); + + IS_TRUE(callback_called); + IS_TRUE(strcmp(lastTopic,"topic")==0); + IS_TRUE(memcmp(lastPayload,"payload",7)==0); + IS_TRUE(lastLength == 7); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int main() +{ + SUITE("Receive"); + test_receive_callback(); + test_receive_stream(); + test_receive_max_sized_message(); + test_receive_oversized_message(); + test_receive_oversized_stream_message(); + test_receive_qos1(); + + FINISH +} diff --git a/libraries/PubSubClient/tests/src/subscribe_spec.cpp b/libraries/PubSubClient/tests/src/subscribe_spec.cpp new file mode 100644 index 0000000..a419823 --- /dev/null +++ b/libraries/PubSubClient/tests/src/subscribe_spec.cpp @@ -0,0 +1,177 @@ +#include "PubSubClient.h" +#include "ShimClient.h" +#include "Buffer.h" +#include "BDDTest.h" +#include "trace.h" + + +byte server[] = { 172, 16, 0, 2 }; + +void callback(char* topic, byte* payload, unsigned int length) { + // handle message arrived +} + +int test_subscribe_no_qos() { + IT("subscribe without qos defaults to 0"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x0 }; + shimClient.expect(subscribe,12); + byte suback[] = { 0x90,0x3,0x0,0x2,0x0 }; + shimClient.respond(suback,5); + + rc = client.subscribe((char*)"topic"); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_subscribe_qos_1() { + IT("subscribes qos 1"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte subscribe[] = { 0x82,0xa,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x1 }; + shimClient.expect(subscribe,12); + byte suback[] = { 0x90,0x3,0x0,0x2,0x1 }; + shimClient.respond(suback,5); + + rc = client.subscribe((char*)"topic",1); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_subscribe_not_connected() { + IT("subscribe fails when not connected"); + ShimClient shimClient; + + PubSubClient client(server, 1883, callback, shimClient); + + int rc = client.subscribe((char*)"topic"); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_subscribe_invalid_qos() { + IT("subscribe fails with invalid qos values"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + rc = client.subscribe((char*)"topic",2); + IS_FALSE(rc); + rc = client.subscribe((char*)"topic",254); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_subscribe_too_long() { + IT("subscribe fails with too long topic"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + // max length should be allowed + // 0 1 2 3 4 5 6 7 8 9 0 1 2 + rc = client.subscribe((char*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); + IS_TRUE(rc); + + // 0 1 2 3 4 5 6 7 8 9 0 1 2 + rc = client.subscribe((char*)"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + + +int test_unsubscribe() { + IT("unsubscribes"); + ShimClient shimClient; + shimClient.setAllowConnect(true); + + byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; + shimClient.respond(connack,4); + + PubSubClient client(server, 1883, callback, shimClient); + int rc = client.connect((char*)"client_test1"); + IS_TRUE(rc); + + byte unsubscribe[] = { 0xA2,0x9,0x0,0x2,0x0,0x5,0x74,0x6f,0x70,0x69,0x63 }; + shimClient.expect(unsubscribe,12); + byte unsuback[] = { 0xB0,0x2,0x0,0x2 }; + shimClient.respond(unsuback,4); + + rc = client.unsubscribe((char*)"topic"); + IS_TRUE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int test_unsubscribe_not_connected() { + IT("unsubscribe fails when not connected"); + ShimClient shimClient; + + PubSubClient client(server, 1883, callback, shimClient); + + int rc = client.unsubscribe((char*)"topic"); + IS_FALSE(rc); + + IS_FALSE(shimClient.error()); + + END_IT +} + +int main() +{ + SUITE("Subscribe"); + test_subscribe_no_qos(); + test_subscribe_qos_1(); + test_subscribe_not_connected(); + test_subscribe_invalid_qos(); + test_subscribe_too_long(); + test_unsubscribe(); + test_unsubscribe_not_connected(); + FINISH +} diff --git a/libraries/PubSubClient/tests/testcases/__init__.py b/libraries/PubSubClient/tests/testcases/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/libraries/PubSubClient/tests/testcases/mqtt_basic.py b/libraries/PubSubClient/tests/testcases/mqtt_basic.py new file mode 100644 index 0000000..1b0cc65 --- /dev/null +++ b/libraries/PubSubClient/tests/testcases/mqtt_basic.py @@ -0,0 +1,43 @@ +import unittest +import settings + +import time +import mosquitto + +import serial + +def on_message(mosq, obj, msg): + obj.message_queue.append(msg) + +class mqtt_basic(unittest.TestCase): + + message_queue = [] + + @classmethod + def setUpClass(self): + self.client = mosquitto.Mosquitto("pubsubclient_ut", clean_session=True,obj=self) + self.client.connect(settings.server_ip) + self.client.on_message = on_message + self.client.subscribe("outTopic",0) + + @classmethod + def tearDownClass(self): + self.client.disconnect() + + def test_one(self): + i=30 + while len(self.message_queue) == 0 and i > 0: + self.client.loop() + time.sleep(0.5) + i -= 1 + self.assertTrue(i>0, "message receive timed-out") + self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") + msg = self.message_queue[0] + self.assertEqual(msg.mid,0,"message id not 0") + self.assertEqual(msg.topic,"outTopic","message topic incorrect") + self.assertEqual(msg.payload,"hello world") + self.assertEqual(msg.qos,0,"message qos not 0") + self.assertEqual(msg.retain,False,"message retain flag incorrect") + + + diff --git a/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py b/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py new file mode 100644 index 0000000..7989f7f --- /dev/null +++ b/libraries/PubSubClient/tests/testcases/mqtt_publish_in_callback.py @@ -0,0 +1,64 @@ +import unittest +import settings + +import time +import mosquitto + +import serial + +def on_message(mosq, obj, msg): + obj.message_queue.append(msg) + +class mqtt_publish_in_callback(unittest.TestCase): + + message_queue = [] + + @classmethod + def setUpClass(self): + self.client = mosquitto.Mosquitto("pubsubclient_ut", clean_session=True,obj=self) + self.client.connect(settings.server_ip) + self.client.on_message = on_message + self.client.subscribe("outTopic",0) + + @classmethod + def tearDownClass(self): + self.client.disconnect() + + def test_connect(self): + i=30 + while len(self.message_queue) == 0 and i > 0: + self.client.loop() + time.sleep(0.5) + i -= 1 + self.assertTrue(i>0, "message receive timed-out") + self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") + msg = self.message_queue.pop(0) + self.assertEqual(msg.mid,0,"message id not 0") + self.assertEqual(msg.topic,"outTopic","message topic incorrect") + self.assertEqual(msg.payload,"hello world") + self.assertEqual(msg.qos,0,"message qos not 0") + self.assertEqual(msg.retain,False,"message retain flag incorrect") + + + def test_publish(self): + self.assertEqual(len(self.message_queue), 0, "message queue not empty") + payload = "abcdefghij" + self.client.publish("inTopic",payload) + + i=30 + while len(self.message_queue) == 0 and i > 0: + self.client.loop() + time.sleep(0.5) + i -= 1 + + self.assertTrue(i>0, "message receive timed-out") + self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received") + msg = self.message_queue.pop(0) + self.assertEqual(msg.mid,0,"message id not 0") + self.assertEqual(msg.topic,"outTopic","message topic incorrect") + self.assertEqual(msg.payload,payload) + self.assertEqual(msg.qos,0,"message qos not 0") + self.assertEqual(msg.retain,False,"message retain flag incorrect") + + + diff --git a/libraries/PubSubClient/tests/testcases/settings.py b/libraries/PubSubClient/tests/testcases/settings.py new file mode 100644 index 0000000..4ad8719 --- /dev/null +++ b/libraries/PubSubClient/tests/testcases/settings.py @@ -0,0 +1,2 @@ +server_ip = "172.16.0.2" +arduino_ip = "172.16.0.100" diff --git a/libraries/PubSubClient/tests/testsuite.py b/libraries/PubSubClient/tests/testsuite.py new file mode 100644 index 0000000..0a8e70d --- /dev/null +++ b/libraries/PubSubClient/tests/testsuite.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python +import os +import os.path +import sys +import shutil +from subprocess import call +import importlib +import unittest +import re + +from testcases import settings + +class Workspace(object): + + def __init__(self): + self.root_dir = os.getcwd() + self.build_dir = os.path.join(self.root_dir,"tmpbin"); + self.log_dir = os.path.join(self.root_dir,"logs"); + self.tests_dir = os.path.join(self.root_dir,"testcases"); + self.examples_dir = os.path.join(self.root_dir,"../PubSubClient/examples") + self.examples = [] + self.tests = [] + if not os.path.isdir("../PubSubClient"): + raise Exception("Cannot find PubSubClient library") + try: + import ino + except: + raise Exception("ino tool not installed") + + def init(self): + if os.path.isdir(self.build_dir): + shutil.rmtree(self.build_dir) + os.mkdir(self.build_dir) + if os.path.isdir(self.log_dir): + shutil.rmtree(self.log_dir) + os.mkdir(self.log_dir) + + os.chdir(self.build_dir) + call(["ino","init"]) + + shutil.copytree("../../PubSubClient","lib/PubSubClient") + + filenames = [] + for root, dirs, files in os.walk(self.examples_dir): + filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")] + filenames.sort() + for e in filenames: + self.examples.append(Sketch(self,e)) + + filenames = [] + for root, dirs, files in os.walk(self.tests_dir): + filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")] + filenames.sort() + for e in filenames: + self.tests.append(Sketch(self,e)) + + def clean(self): + shutil.rmtree(self.build_dir) + +class Sketch(object): + def __init__(self,wksp,fn): + self.w = wksp + self.filename = fn + self.basename = os.path.basename(self.filename) + self.build_log = os.path.join(self.w.log_dir,"%s.log"%(os.path.basename(self.filename),)) + self.build_err_log = os.path.join(self.w.log_dir,"%s.err.log"%(os.path.basename(self.filename),)) + self.build_upload_log = os.path.join(self.w.log_dir,"%s.upload.log"%(os.path.basename(self.filename),)) + + def build(self): + sys.stdout.write(" Build: ") + sys.stdout.flush() + + # Copy sketch over, replacing IP addresses as necessary + fin = open(self.filename,"r") + lines = fin.readlines() + fin.close() + fout = open(os.path.join(self.w.build_dir,"src","sketch.ino"),"w") + for l in lines: + if re.match(r"^byte server\[\] = {",l): + fout.write("byte server[] = { %s };\n"%(settings.server_ip.replace(".",", "),)) + elif re.match(r"^byte ip\[\] = {",l): + fout.write("byte ip[] = { %s };\n"%(settings.arduino_ip.replace(".",", "),)) + else: + fout.write(l) + fout.flush() + fout.close() + + # Run build + fout = open(self.build_log, "w") + ferr = open(self.build_err_log, "w") + rc = call(["ino","build"],stdout=fout,stderr=ferr) + fout.close() + ferr.close() + if rc == 0: + sys.stdout.write("pass") + sys.stdout.write("\n") + return True + else: + sys.stdout.write("fail") + sys.stdout.write("\n") + with open(self.build_err_log) as f: + for line in f: + print " ",line, + return False + + def upload(self): + sys.stdout.write(" Upload: ") + sys.stdout.flush() + fout = open(self.build_upload_log, "w") + rc = call(["ino","upload"],stdout=fout,stderr=fout) + fout.close() + if rc == 0: + sys.stdout.write("pass") + sys.stdout.write("\n") + return True + else: + sys.stdout.write("fail") + sys.stdout.write("\n") + with open(self.build_upload_log) as f: + for line in f: + print " ",line, + return False + + + def test(self): + # import the matching test case, if it exists + try: + basename = os.path.basename(self.filename)[:-4] + i = importlib.import_module("testcases."+basename) + except: + sys.stdout.write(" Test: no tests found") + sys.stdout.write("\n") + return + c = getattr(i,basename) + + testmethods = [m for m in dir(c) if m.startswith("test_")] + testmethods.sort() + tests = [] + for m in testmethods: + tests.append(c(m)) + + result = unittest.TestResult() + c.setUpClass() + if self.upload(): + sys.stdout.write(" Test: ") + sys.stdout.flush() + for t in tests: + t.run(result) + print "%d/%d"%(result.testsRun-len(result.failures)-len(result.errors),result.testsRun) + if not result.wasSuccessful(): + if len(result.failures) > 0: + for f in result.failures: + print "-- %s"%(str(f[0]),) + print f[1] + if len(result.errors) > 0: + print " Errors:" + for f in result.errors: + print "-- %s"%(str(f[0]),) + print f[1] + c.tearDownClass() + +if __name__ == '__main__': + run_tests = True + + w = Workspace() + w.init() + + for e in w.examples: + print "--------------------------------------" + print "[%s]"%(e.basename,) + if e.build() and run_tests: + e.test() + for e in w.tests: + print "--------------------------------------" + print "[%s]"%(e.basename,) + if e.build() and run_tests: + e.test() + + w.clean() diff --git a/libraries/Servo/README.adoc b/libraries/Servo/README.adoc new file mode 100644 index 0000000..dd3f0ba --- /dev/null +++ b/libraries/Servo/README.adoc @@ -0,0 +1,25 @@ += Servo Library for Arduino = + +This library allows an Arduino board to control RC (hobby) servo motors. + +For more information about this library please visit us at +http://www.arduino.cc/en/Reference/Servo + +== License == + +Copyright (c) 2013 Arduino LLC. All right reserved. +Copyright (c) 2009 Michael Margolis. All right 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. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/libraries/Servo/examples/Knob/Knob.ino b/libraries/Servo/examples/Knob/Knob.ino new file mode 100644 index 0000000..0db8770 --- /dev/null +++ b/libraries/Servo/examples/Knob/Knob.ino @@ -0,0 +1,27 @@ +/* + Controlling a servo position using a potentiometer (variable resistor) + by Michal Rinott + + modified on 8 Nov 2013 + by Scott Fitzgerald + http://www.arduino.cc/en/Tutorial/Knob +*/ + +#include + +Servo myservo; // create servo object to control a servo + +int potpin = 0; // analog pin used to connect the potentiometer +int val; // variable to read the value from the analog pin + +void setup() { + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + +void loop() { + val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) + val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) + myservo.write(val); // sets the servo position according to the scaled value + delay(15); // waits for the servo to get there +} + diff --git a/libraries/Servo/examples/Sweep/Sweep.ino b/libraries/Servo/examples/Sweep/Sweep.ino new file mode 100644 index 0000000..df904af --- /dev/null +++ b/libraries/Servo/examples/Sweep/Sweep.ino @@ -0,0 +1,32 @@ +/* Sweep + by BARRAGAN + This example code is in the public domain. + + modified 8 Nov 2013 + by Scott Fitzgerald + http://www.arduino.cc/en/Tutorial/Sweep +*/ + +#include + +Servo myservo; // create servo object to control a servo +// twelve servo objects can be created on most boards + +int pos = 0; // variable to store the servo position + +void setup() { + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + +void loop() { + for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees + // in steps of 1 degree + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } + for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } +} + diff --git a/libraries/Servo/keywords.txt b/libraries/Servo/keywords.txt new file mode 100644 index 0000000..0a7ca1e --- /dev/null +++ b/libraries/Servo/keywords.txt @@ -0,0 +1,24 @@ +####################################### +# Syntax Coloring Map Servo +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Servo KEYWORD1 Servo + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +read KEYWORD2 +attached KEYWORD2 +writeMicroseconds KEYWORD2 +readMicroseconds KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/libraries/Servo/library.properties b/libraries/Servo/library.properties new file mode 100644 index 0000000..94dfc92 --- /dev/null +++ b/libraries/Servo/library.properties @@ -0,0 +1,9 @@ +name=Servo +version=1.1.2 +author=Michael Margolis, Arduino +maintainer=Arduino +sentence=Allows Arduino/Genuino boards to control a variety of servo motors. +paragraph=This library can control a great number of servos.
It makes careful use of timers: the library can control 12 servos using only 1 timer.
On the Arduino Due you can control up to 60 servos.
+category=Device Control +url=http://www.arduino.cc/en/Reference/Servo +architectures=avr,sam,samd,nrf52,stm32f4 diff --git a/libraries/Servo/src/Servo.h b/libraries/Servo/src/Servo.h new file mode 100644 index 0000000..4890a14 --- /dev/null +++ b/libraries/Servo/src/Servo.h @@ -0,0 +1,119 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + A servo is activated by creating an instance of the Servo class passing + the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently + written using the write() method. + + Note that analogWrite of PWM on pins associated with the timer are + disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two + timers, 48 servos will use four. + The sequence used to sieze timers is defined in timers.h + + The methods are: + + Servo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + */ + +#ifndef Servo_h +#define Servo_h + +#include + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +// Architecture specific include +#if defined(ARDUINO_ARCH_AVR) +#include "avr/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAM) +#include "sam/ServoTimers.h" +#elif defined(ARDUINO_ARCH_SAMD) +#include "samd/ServoTimers.h" +#elif defined(ARDUINO_ARCH_STM32F4) +#include "stm32f4/ServoTimers.h" +#elif defined(ARDUINO_ARCH_NRF52) +#include "nrf52/ServoTimers.h" +#else +#error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor." +#endif + +#define Servo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds + +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer +#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER) + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +#if !defined(ARDUINO_ARCH_STM32F4) + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t ; + +typedef struct { + ServoPin_t Pin; + volatile unsigned int ticks; +} servo_t; + +class Servo +{ +public: + Servo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds + void writeMicroseconds(int value); // Write pulse width in microseconds + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH +}; + +#endif +#endif diff --git a/libraries/Servo/src/avr/Servo.cpp b/libraries/Servo/src/avr/Servo.cpp new file mode 100644 index 0000000..ed7376e --- /dev/null +++ b/libraries/Servo/src/avr/Servo.cpp @@ -0,0 +1,318 @@ +/* + Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#if defined(ARDUINO_ARCH_AVR) + +#include +#include + +#include "Servo.h" + +#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009 +#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009 + +//#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER) + +static servo_t servos[MAX_SERVOS]; // static array of servo structures +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +uint8_t ServoCount = 0; // the total number of attached servos + + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) +{ + if( Channel[timer] < 0 ) + *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer + else{ + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( ((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL) ) // allow a few ticks to ensure the next OCR1A not missed + *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL); + else + *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform +// Interrupt handlers for Arduino +#if defined(_useTimer1) +SIGNAL (TIMER1_COMPA_vect) +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif + +#if defined(_useTimer3) +SIGNAL (TIMER3_COMPA_vect) +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif + +#if defined(_useTimer4) +SIGNAL (TIMER4_COMPA_vect) +{ + handle_interrupts(_timer4, &TCNT4, &OCR4A); +} +#endif + +#if defined(_useTimer5) +SIGNAL (TIMER5_COMPA_vect) +{ + handle_interrupts(_timer5, &TCNT5, &OCR5A); +} +#endif + +#elif defined WIRING +// Interrupt handlers for Wiring +#if defined(_useTimer1) +void Timer1Service() +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif +#if defined(_useTimer3) +void Timer3Service() +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif +#endif + + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if(timer == _timer1) { + TCCR1A = 0; // normal counting mode + TCCR1B = _BV(CS11); // set prescaler of 8 + TCNT1 = 0; // clear the timer count +#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__) + TIFR |= _BV(OCF1A); // clear any pending interrupts; + TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt +#else + // here if not ATmega8 or ATmega128 + TIFR1 |= _BV(OCF1A); // clear any pending interrupts; + TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); +#endif + } +#endif + +#if defined (_useTimer3) + if(timer == _timer3) { + TCCR3A = 0; // normal counting mode + TCCR3B = _BV(CS31); // set prescaler of 8 + TCNT3 = 0; // clear the timer count +#if defined(__AVR_ATmega128__) + TIFR |= _BV(OCF3A); // clear any pending interrupts; + ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt +#else + TIFR3 = _BV(OCF3A); // clear any pending interrupts; + TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only +#endif + } +#endif + +#if defined (_useTimer4) + if(timer == _timer4) { + TCCR4A = 0; // normal counting mode + TCCR4B = _BV(CS41); // set prescaler of 8 + TCNT4 = 0; // clear the timer count + TIFR4 = _BV(OCF4A); // clear any pending interrupts; + TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt + } +#endif + +#if defined (_useTimer5) + if(timer == _timer5) { + TCCR5A = 0; // normal counting mode + TCCR5B = _BV(CS51); // set prescaler of 8 + TCNT5 = 0; // clear the timer count + TIFR5 = _BV(OCF5A); // clear any pending interrupts; + TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt + } +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ + //disable use of the given timer +#if defined WIRING // Wiring + if(timer == _timer1) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #else + TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #endif + timerDetach(TIMER1OUTCOMPAREA_INT); + } + else if(timer == _timer3) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #else + ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #endif + timerDetach(TIMER3OUTCOMPAREA_INT); + } +#else + //For arduino - in future: call here to a currently undefined function to reset the timer + (void) timer; // squash "unused parameter 'timer' [-Wunused-parameter]" warning +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if( ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009 + } + else + this->servoIndex = INVALID_SERVO ; // too many servos +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + if(this->servoIndex < MAX_SERVOS ) { + pinMode( pin, OUTPUT) ; // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) + initISR(timer); + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex ; +} + +void Servo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + if(value < MIN_PULSE_WIDTH) + { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if(value < 0) value = 0; + if(value > 180) value = 180; + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + this->writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if( value < SERVO_MIN() ) // ensure pulse width is valid + value = SERVO_MIN(); + else if( value > SERVO_MAX() ) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + uint8_t oldSREG = SREG; + cli(); + servos[channel].ticks = value; + SREG = oldSREG; + } +} + +int Servo::read() // return the value as degrees +{ + return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if( this->servoIndex != INVALID_SERVO ) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009 + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive ; +} + +#endif // ARDUINO_ARCH_AVR + diff --git a/libraries/Servo/src/avr/ServoTimers.h b/libraries/Servo/src/avr/ServoTimers.h new file mode 100644 index 0000000..9794c8e --- /dev/null +++ b/libraries/Servo/src/avr/ServoTimers.h @@ -0,0 +1,59 @@ +/* + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +/** + * AVR Only definitions + * -------------------- + */ + +// Say which 16 bit timers can be used and in what order +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define _useTimer5 +#define _useTimer1 +#define _useTimer3 +#define _useTimer4 +typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega32U4__) +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega2561__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t; + +#else // everything else +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; +#endif + diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp new file mode 100644 index 0000000..a49f093 --- /dev/null +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -0,0 +1,134 @@ +/* + Copyright (c) 2016 Arduino. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#if defined(ARDUINO_ARCH_NRF52) + +#include +#include + + +static servo_t servos[MAX_SERVOS]; // static array of servo structures + +uint8_t ServoCount = 0; // the total number of attached servos + + + +uint32_t group_pins[3][NRF_PWM_CHANNEL_COUNT]={{NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}}; +static uint16_t seq_values[3][NRF_PWM_CHANNEL_COUNT]={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; + +Servo::Servo() +{ + if (ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + } else { + this->servoIndex = INVALID_SERVO; // too many servos + } + +} + +uint8_t Servo::attach(int pin) +{ + + return this->attach(pin, 0, 2500); +} + + +uint8_t Servo::attach(int pin, int min, int max) +{ + int servo_min, servo_max; + if (this->servoIndex < MAX_SERVOS) { + pinMode(pin, OUTPUT); // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + + if(min < servo_min) min = servo_min; + if (max > servo_max) max = servo_max; + this->min = min; + this->max = max; + + servos[this->servoIndex].Pin.isActive = true; + + } + return this->servoIndex; +} + +void Servo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; +} + + +void Servo::write(int value) +{ + if (value < 0) + value = 0; + else if (value > 180) + value = 180; + value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); + + writeMicroseconds(value); +} + + +void Servo::writeMicroseconds(int value) +{ + uint8_t channel, instance; + uint8_t pin = servos[this->servoIndex].Pin.nbr; + //instance of pwm module is MSB - look at VWariant.h + instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16; + //index of pwm channel is LSB - look at VWariant.h + channel=g_APinDescription[pin].ulPWMChannel & 0x0F; + group_pins[instance][channel]=g_APinDescription[pin].ulPin; + NRF_PWM_Type * PWMInstance = instance == 0 ? NRF_PWM0 : (instance == 1 ? NRF_PWM1 : NRF_PWM2); + //configure pwm instance and enable it + seq_values[instance][channel]= value | 0x8000; + nrf_pwm_sequence_t const seq={ + seq_values[instance], + NRF_PWM_VALUES_LENGTH(seq_values), + 0, + 0 + }; + nrf_pwm_pins_set(PWMInstance, group_pins[instance]); + nrf_pwm_enable(PWMInstance); + nrf_pwm_configure(PWMInstance, NRF_PWM_CLK_125kHz, NRF_PWM_MODE_UP, 2500); // 20ms - 50Hz + nrf_pwm_decoder_set(PWMInstance, NRF_PWM_LOAD_INDIVIDUAL, NRF_PWM_STEP_AUTO); + nrf_pwm_sequence_set(PWMInstance, 0, &seq); + nrf_pwm_loop_set(PWMInstance, 0UL); + nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0); +} + +int Servo::read() // return the value as degrees +{ + return map(readMicroseconds(), MIN_PULSE, MAX_PULSE, 0, 180); +} + +int Servo::readMicroseconds() +{ + uint8_t channel, instance; + uint8_t pin=servos[this->servoIndex].Pin.nbr; + instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16; + channel=g_APinDescription[pin].ulPWMChannel & 0x0F; + // remove the 16th bit we added before + return seq_values[instance][channel] & 0x7FFF; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive; +} + +#endif // ARDUINO_ARCH_NRF52 \ No newline at end of file diff --git a/libraries/Servo/src/nrf52/ServoTimers.h b/libraries/Servo/src/nrf52/ServoTimers.h new file mode 100644 index 0000000..51759be --- /dev/null +++ b/libraries/Servo/src/nrf52/ServoTimers.h @@ -0,0 +1,38 @@ +/* + Copyright (c) 2016 Arduino. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * NRF52 doesn't use timer, but pwm. This file include definitions to keep + * compatibility with the Servo library standards. + */ + +#ifndef __SERVO_TIMERS_H__ +#define __SERVO_TIMERS_H__ + +/** + * NRF52 Only definitions + * --------------------- + */ + +#define MIN_PULSE 55 +#define MAX_PULSE 284 + +// define one timer in order to have MAX_SERVOS = 12 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; + +#endif // __SERVO_TIMERS_H__ \ No newline at end of file diff --git a/libraries/Servo/src/sam/Servo.cpp b/libraries/Servo/src/sam/Servo.cpp new file mode 100644 index 0000000..21f901f --- /dev/null +++ b/libraries/Servo/src/sam/Servo.cpp @@ -0,0 +1,283 @@ +/* + Copyright (c) 2013 Arduino LLC. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#if defined(ARDUINO_ARCH_SAM) + +#include +#include + +#define usToTicks(_us) (( clockCyclesPerMicrosecond() * _us) / 32) // converts microseconds to tick +#define ticksToUs(_ticks) (( (unsigned)_ticks * 32)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays + +static servo_t servos[MAX_SERVOS]; // static array of servo structures + +uint8_t ServoCount = 0; // the total number of attached servos + +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +//------------------------------------------------------------------------------ +/// Interrupt handler for the TC0 channel 1. +//------------------------------------------------------------------------------ +void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel); +#if defined (_useTimer1) +void HANDLER_FOR_TIMER1(void) { + Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); +} +#endif +#if defined (_useTimer2) +void HANDLER_FOR_TIMER2(void) { + Servo_Handler(_timer2, TC_FOR_TIMER2, CHANNEL_FOR_TIMER2); +} +#endif +#if defined (_useTimer3) +void HANDLER_FOR_TIMER3(void) { + Servo_Handler(_timer3, TC_FOR_TIMER3, CHANNEL_FOR_TIMER3); +} +#endif +#if defined (_useTimer4) +void HANDLER_FOR_TIMER4(void) { + Servo_Handler(_timer4, TC_FOR_TIMER4, CHANNEL_FOR_TIMER4); +} +#endif +#if defined (_useTimer5) +void HANDLER_FOR_TIMER5(void) { + Servo_Handler(_timer5, TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); +} +#endif + +void Servo_Handler(timer16_Sequence_t timer, Tc *tc, uint8_t channel) +{ + // clear interrupt + tc->TC_CHANNEL[channel].TC_SR; + if (Channel[timer] < 0) { + tc->TC_CHANNEL[channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer + } else { + if (SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true) { + digitalWrite(SERVO(timer,Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated + } + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + tc->TC_CHANNEL[channel].TC_RA = tc->TC_CHANNEL[channel].TC_CV + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) { // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( (tc->TC_CHANNEL[channel].TC_CV) + 4 < usToTicks(REFRESH_INTERVAL) ) { // allow a few ticks to ensure the next OCR1A not missed + tc->TC_CHANNEL[channel].TC_RA = (unsigned int)usToTicks(REFRESH_INTERVAL); + } + else { + tc->TC_CHANNEL[channel].TC_RA = tc->TC_CHANNEL[channel].TC_CV + 4; // at least REFRESH_INTERVAL has elapsed + } + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +static void _initISR(Tc *tc, uint32_t channel, uint32_t id, IRQn_Type irqn) +{ + pmc_enable_periph_clk(id); + TC_Configure(tc, channel, + TC_CMR_TCCLKS_TIMER_CLOCK3 | // MCK/32 + TC_CMR_WAVE | // Waveform mode + TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when equals to RC + + /* 84MHz, MCK/32, for 1.5ms: 3937 */ + TC_SetRA(tc, channel, 2625); // 1ms + + /* Configure and enable interrupt */ + NVIC_EnableIRQ(irqn); + // TC_IER_CPAS: RA Compare + tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPAS; + + // Enables the timer clock and performs a software reset to start the counting + TC_Start(tc, channel); +} + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if (timer == _timer1) + _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1); +#endif +#if defined (_useTimer2) + if (timer == _timer2) + _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2); +#endif +#if defined (_useTimer3) + if (timer == _timer3) + _initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3); +#endif +#if defined (_useTimer4) + if (timer == _timer4) + _initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4); +#endif +#if defined (_useTimer5) + if (timer == _timer5) + _initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5); +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + TC_Stop(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); +#endif +#if defined (_useTimer2) + TC_Stop(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2); +#endif +#if defined (_useTimer3) + TC_Stop(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3); +#endif +#if defined (_useTimer4) + TC_Stop(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4); +#endif +#if defined (_useTimer5) + TC_Stop(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); +#endif +} + + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if (ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values + } else { + this->servoIndex = INVALID_SERVO; // too many servos + } +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + timer16_Sequence_t timer; + + if (this->servoIndex < MAX_SERVOS) { + pinMode(pin, OUTPUT); // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer = SERVO_INDEX_TO_TIMER(servoIndex); + if (isTimerActive(timer) == false) { + initISR(timer); + } + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex; +} + +void Servo::detach() +{ + timer16_Sequence_t timer; + + servos[this->servoIndex].Pin.isActive = false; + timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if (value < MIN_PULSE_WIDTH) + { + if (value < 0) + value = 0; + else if (value > 180) + value = 180; + + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if (value < SERVO_MIN()) // ensure pulse width is valid + value = SERVO_MIN(); + else if (value > SERVO_MAX()) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead + servos[channel].ticks = value; + } +} + +int Servo::read() // return the value as degrees +{ + return map(readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if (this->servoIndex != INVALID_SERVO) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION; + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive; +} + +#endif // ARDUINO_ARCH_SAM + diff --git a/libraries/Servo/src/sam/ServoTimers.h b/libraries/Servo/src/sam/ServoTimers.h new file mode 100644 index 0000000..13f736a --- /dev/null +++ b/libraries/Servo/src/sam/ServoTimers.h @@ -0,0 +1,88 @@ +/* + Copyright (c) 2013 Arduino LLC. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +/** + * SAM Only definitions + * -------------------- + */ + +// For SAM3X: +#define _useTimer1 +#define _useTimer2 +#define _useTimer3 +#define _useTimer4 +#define _useTimer5 + +/* + TC0, chan 0 => TC0_Handler + TC0, chan 1 => TC1_Handler + TC0, chan 2 => TC2_Handler + TC1, chan 0 => TC3_Handler + TC1, chan 1 => TC4_Handler + TC1, chan 2 => TC5_Handler + TC2, chan 0 => TC6_Handler + TC2, chan 1 => TC7_Handler + TC2, chan 2 => TC8_Handler + */ + +#if defined (_useTimer1) +#define TC_FOR_TIMER1 TC1 +#define CHANNEL_FOR_TIMER1 0 +#define ID_TC_FOR_TIMER1 ID_TC3 +#define IRQn_FOR_TIMER1 TC3_IRQn +#define HANDLER_FOR_TIMER1 TC3_Handler +#endif +#if defined (_useTimer2) +#define TC_FOR_TIMER2 TC1 +#define CHANNEL_FOR_TIMER2 1 +#define ID_TC_FOR_TIMER2 ID_TC4 +#define IRQn_FOR_TIMER2 TC4_IRQn +#define HANDLER_FOR_TIMER2 TC4_Handler +#endif +#if defined (_useTimer3) +#define TC_FOR_TIMER3 TC1 +#define CHANNEL_FOR_TIMER3 2 +#define ID_TC_FOR_TIMER3 ID_TC5 +#define IRQn_FOR_TIMER3 TC5_IRQn +#define HANDLER_FOR_TIMER3 TC5_Handler +#endif +#if defined (_useTimer4) +#define TC_FOR_TIMER4 TC0 +#define CHANNEL_FOR_TIMER4 2 +#define ID_TC_FOR_TIMER4 ID_TC2 +#define IRQn_FOR_TIMER4 TC2_IRQn +#define HANDLER_FOR_TIMER4 TC2_Handler +#endif +#if defined (_useTimer5) +#define TC_FOR_TIMER5 TC0 +#define CHANNEL_FOR_TIMER5 0 +#define ID_TC_FOR_TIMER5 ID_TC0 +#define IRQn_FOR_TIMER5 TC0_IRQn +#define HANDLER_FOR_TIMER5 TC0_Handler +#endif + +typedef enum { _timer1, _timer2, _timer3, _timer4, _timer5, _Nbr_16timers } timer16_Sequence_t ; + diff --git a/libraries/Servo/src/samd/Servo.cpp b/libraries/Servo/src/samd/Servo.cpp new file mode 100644 index 0000000..42a3877 --- /dev/null +++ b/libraries/Servo/src/samd/Servo.cpp @@ -0,0 +1,297 @@ +/* + Copyright (c) 2015 Arduino LLC. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#if defined(ARDUINO_ARCH_SAMD) + +#include +#include + +#define usToTicks(_us) ((clockCyclesPerMicrosecond() * _us) / 16) // converts microseconds to tick +#define ticksToUs(_ticks) (((unsigned) _ticks * 16) / clockCyclesPerMicrosecond()) // converts from ticks back to microseconds + +#define TRIM_DURATION 5 // compensation ticks to trim adjust for digitalWrite delays + +static servo_t servos[MAX_SERVOS]; // static array of servo structures + +uint8_t ServoCount = 0; // the total number of attached servos + +static volatile int8_t currentServoIndex[_Nbr_16timers]; // index for the servo being pulsed for each timer (or -1 if refresh interval) + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +#define WAIT_TC16_REGS_SYNC(x) while(x->COUNT16.STATUS.bit.SYNCBUSY); + +/************ static functions common to all instances ***********************/ + +void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel, uint8_t intFlag); +#if defined (_useTimer1) +void HANDLER_FOR_TIMER1(void) { + Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, INTFLAG_BIT_FOR_TIMER_1); +} +#endif +#if defined (_useTimer2) +void HANDLER_FOR_TIMER2(void) { + Servo_Handler(_timer2, TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, INTFLAG_BIT_FOR_TIMER_2); +} +#endif + +void Servo_Handler(timer16_Sequence_t timer, Tc *tc, uint8_t channel, uint8_t intFlag) +{ + if (currentServoIndex[timer] < 0) { + tc->COUNT16.COUNT.reg = (uint16_t) 0; + WAIT_TC16_REGS_SYNC(tc) + } else { + if (SERVO_INDEX(timer, currentServoIndex[timer]) < ServoCount && SERVO(timer, currentServoIndex[timer]).Pin.isActive == true) { + digitalWrite(SERVO(timer, currentServoIndex[timer]).Pin.nbr, LOW); // pulse this channel low if activated + } + } + + // Select the next servo controlled by this timer + currentServoIndex[timer]++; + + if (SERVO_INDEX(timer, currentServoIndex[timer]) < ServoCount && currentServoIndex[timer] < SERVOS_PER_TIMER) { + if (SERVO(timer, currentServoIndex[timer]).Pin.isActive == true) { // check if activated + digitalWrite(SERVO(timer, currentServoIndex[timer]).Pin.nbr, HIGH); // it's an active channel so pulse it high + } + + // Get the counter value + uint16_t tcCounterValue = tc->COUNT16.COUNT.reg; + WAIT_TC16_REGS_SYNC(tc) + + tc->COUNT16.CC[channel].reg = (uint16_t) (tcCounterValue + SERVO(timer, currentServoIndex[timer]).ticks); + WAIT_TC16_REGS_SYNC(tc) + } + else { + // finished all channels so wait for the refresh period to expire before starting over + + // Get the counter value + uint16_t tcCounterValue = tc->COUNT16.COUNT.reg; + WAIT_TC16_REGS_SYNC(tc) + + if (tcCounterValue + 4UL < usToTicks(REFRESH_INTERVAL)) { // allow a few ticks to ensure the next OCR1A not missed + tc->COUNT16.CC[channel].reg = (uint16_t) usToTicks(REFRESH_INTERVAL); + } + else { + tc->COUNT16.CC[channel].reg = (uint16_t) (tcCounterValue + 4UL); // at least REFRESH_INTERVAL has elapsed + } + WAIT_TC16_REGS_SYNC(tc) + + currentServoIndex[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } + + // Clear the interrupt + tc->COUNT16.INTFLAG.reg = intFlag; +} + +static inline void resetTC (Tc* TCx) +{ + // Disable TCx + TCx->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE; + WAIT_TC16_REGS_SYNC(TCx) + + // Reset TCx + TCx->COUNT16.CTRLA.reg = TC_CTRLA_SWRST; + WAIT_TC16_REGS_SYNC(TCx) + while (TCx->COUNT16.CTRLA.bit.SWRST); +} + +static void _initISR(Tc *tc, uint8_t channel, uint32_t id, IRQn_Type irqn, uint8_t gcmForTimer, uint8_t intEnableBit) +{ + // Enable GCLK for timer 1 (timer counter input clock) + GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(gcmForTimer)); + while (GCLK->STATUS.bit.SYNCBUSY); + + // Reset the timer + // TODO this is not the right thing to do if more than one channel per timer is used by the Servo library + resetTC(tc); + + // Set timer counter mode to 16 bits + tc->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16; + + // Set timer counter mode as normal PWM + tc->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_NPWM; + + // Set the prescaler factor to GCLK_TC/16. At nominal 48MHz GCLK_TC this is 3000 ticks per millisecond + tc->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV16; + + // Count up + tc->COUNT16.CTRLBCLR.bit.DIR = 1; + WAIT_TC16_REGS_SYNC(tc) + + // First interrupt request after 1 ms + tc->COUNT16.CC[channel].reg = (uint16_t) usToTicks(1000UL); + WAIT_TC16_REGS_SYNC(tc) + + // Configure interrupt request + // TODO this should be changed if more than one channel per timer is used by the Servo library + NVIC_DisableIRQ(irqn); + NVIC_ClearPendingIRQ(irqn); + NVIC_SetPriority(irqn, 0); + NVIC_EnableIRQ(irqn); + + // Enable the match channel interrupt request + tc->COUNT16.INTENSET.reg = intEnableBit; + + // Enable the timer and start it + tc->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; + WAIT_TC16_REGS_SYNC(tc) +} + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if (timer == _timer1) + _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1, GCM_FOR_TIMER_1, INTENSET_BIT_FOR_TIMER_1); +#endif +#if defined (_useTimer2) + if (timer == _timer2) + _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2, GCM_FOR_TIMER_2, INTENSET_BIT_FOR_TIMER_2); +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + // Disable the match channel interrupt request + TC_FOR_TIMER1->COUNT16.INTENCLR.reg = INTENCLR_BIT_FOR_TIMER_1; +#endif +#if defined (_useTimer2) + // Disable the match channel interrupt request + TC_FOR_TIMER2->COUNT16.INTENCLR.reg = INTENCLR_BIT_FOR_TIMER_2; +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + +/****************** end of static functions ******************************/ + +Servo::Servo() +{ + if (ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values + } else { + this->servoIndex = INVALID_SERVO; // too many servos + } +} + +uint8_t Servo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t Servo::attach(int pin, int min, int max) +{ + timer16_Sequence_t timer; + + if (this->servoIndex < MAX_SERVOS) { + pinMode(pin, OUTPUT); // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer = SERVO_INDEX_TO_TIMER(servoIndex); + if (isTimerActive(timer) == false) { + initISR(timer); + } + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex; +} + +void Servo::detach() +{ + timer16_Sequence_t timer; + + servos[this->servoIndex].Pin.isActive = false; + timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void Servo::write(int value) +{ + // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + if (value < MIN_PULSE_WIDTH) + { + if (value < 0) + value = 0; + else if (value > 180) + value = 180; + + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + writeMicroseconds(value); +} + +void Servo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel < MAX_SERVOS) ) // ensure channel is valid + { + if (value < SERVO_MIN()) // ensure pulse width is valid + value = SERVO_MIN(); + else if (value > SERVO_MAX()) + value = SERVO_MAX(); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead + servos[channel].ticks = value; + } +} + +int Servo::read() // return the value as degrees +{ + return map(readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int Servo::readMicroseconds() +{ + unsigned int pulsewidth; + if (this->servoIndex != INVALID_SERVO) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION; + else + pulsewidth = 0; + + return pulsewidth; +} + +bool Servo::attached() +{ + return servos[this->servoIndex].Pin.isActive; +} + +#endif // ARDUINO_ARCH_SAMD diff --git a/libraries/Servo/src/samd/ServoTimers.h b/libraries/Servo/src/samd/ServoTimers.h new file mode 100644 index 0000000..17acfb5 --- /dev/null +++ b/libraries/Servo/src/samd/ServoTimers.h @@ -0,0 +1,71 @@ +/* + Copyright (c) 2015 Arduino LLC. All right 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. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the current board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + */ + +#ifndef __SERVO_TIMERS_H__ +#define __SERVO_TIMERS_H__ + +/** + * SAMD Only definitions + * --------------------- + */ + +// For SAMD: +#define _useTimer1 +//#define _useTimer2 // <- TODO do not activate until the code in Servo.cpp has been changed in order + // to manage more than one channel per timer on the SAMD architecture + +#if defined (_useTimer1) +#define TC_FOR_TIMER1 TC4 +#define CHANNEL_FOR_TIMER1 0 +#define INTENSET_BIT_FOR_TIMER_1 TC_INTENSET_MC0 +#define INTENCLR_BIT_FOR_TIMER_1 TC_INTENCLR_MC0 +#define INTFLAG_BIT_FOR_TIMER_1 TC_INTFLAG_MC0 +#define ID_TC_FOR_TIMER1 ID_TC4 +#define IRQn_FOR_TIMER1 TC4_IRQn +#define HANDLER_FOR_TIMER1 TC4_Handler +#define GCM_FOR_TIMER_1 GCM_TC4_TC5 +#endif +#if defined (_useTimer2) +#define TC_FOR_TIMER2 TC4 +#define CHANNEL_FOR_TIMER2 1 +#define INTENSET_BIT_FOR_TIMER_2 TC_INTENSET_MC1 +#define INTENCLR_BIT_FOR_TIMER_2 TC_INTENCLR_MC1 +#define ID_TC_FOR_TIMER2 ID_TC4 +#define IRQn_FOR_TIMER2 TC4_IRQn +#define HANDLER_FOR_TIMER2 TC4_Handler +#define GCM_FOR_TIMER_2 GCM_TC4_TC5 +#endif + +typedef enum { +#if defined (_useTimer1) + _timer1, +#endif +#if defined (_useTimer2) + _timer2, +#endif + _Nbr_16timers } timer16_Sequence_t; + +#endif // __SERVO_TIMERS_H__ diff --git a/libraries/Servo/src/stm32f4/Servo.cpp b/libraries/Servo/src/stm32f4/Servo.cpp new file mode 100644 index 0000000..01d05d9 --- /dev/null +++ b/libraries/Servo/src/stm32f4/Servo.cpp @@ -0,0 +1,194 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010, LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +#if defined(ARDUINO_ARCH_STM32F4) + +#include "ServoTimers.h" + +#include "boards.h" +#include "io.h" +#include "pwm.h" +#include "math.h" + +// 20 millisecond period config. For a 1-based prescaler, +// +// (prescaler * overflow / CYC_MSEC) msec = 1 timer cycle = 20 msec +// => prescaler * overflow = 20 * CYC_MSEC +// +// This picks the smallest prescaler that allows an overflow < 2^16. +#define MAX_OVERFLOW ((1 << 16) - 1) +#define CYC_MSEC (1000 * CYCLES_PER_MICROSECOND) +#define TAU_MSEC 20 +#define TAU_USEC (TAU_MSEC * 1000) +#define TAU_CYC (TAU_MSEC * CYC_MSEC) +#define SERVO_PRESCALER (TAU_CYC / MAX_OVERFLOW + 1) +#define SERVO_OVERFLOW ((uint16)round((double)TAU_CYC / SERVO_PRESCALER)) + +// Unit conversions +#define US_TO_COMPARE(us) ((uint16)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW)) +#define COMPARE_TO_US(c) ((uint32)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC)) +#define ANGLE_TO_US(a) ((uint16)(map((a), this->minAngle, this->maxAngle, \ + this->minPW, this->maxPW))) +#define US_TO_ANGLE(us) ((int16)(map((us), this->minPW, this->maxPW, \ + this->minAngle, this->maxAngle))) + +Servo::Servo() { + this->resetFields(); +} + +bool Servo::attach(uint8 pin, uint16 minPW, uint16 maxPW, int16 minAngle, int16 maxAngle) +{ + // SerialUSB.begin(115200); + // SerialUSB.println(MAX_OVERFLOW); + + + timer_dev *tdev = PIN_MAP[pin].timer_device; + + analogWriteResolution(16); + + int prescaler = 6; + int overflow = 65400; + int minPW_correction = 300; + int maxPW_correction = 300; + + pinMode(pin, OUTPUT); + + + if (tdev == NULL) { + // don't reset any fields or ASSERT(0), to keep driving any + // previously attach()ed servo. + return false; + } + + if ( (tdev == TIMER1) || (tdev == TIMER8) || (tdev == TIMER10) || (tdev == TIMER11)) + { + prescaler = 54; + overflow = 65400; + minPW_correction = 40; + maxPW_correction = 50; + } + + if ( (tdev == TIMER2) || (tdev == TIMER3) || (tdev == TIMER4) || (tdev == TIMER5) ) + { + prescaler = 6; + overflow = 64285; + minPW_correction = 370; + maxPW_correction = 350; + } + + if ( (tdev == TIMER6) || (tdev == TIMER7) ) + { + prescaler = 6; + overflow = 65400; + minPW_correction = 0; + maxPW_correction = 0; + } + + if ( (tdev == TIMER9) || (tdev == TIMER12) || (tdev == TIMER13) || (tdev == TIMER14) ) + { + prescaler = 6; + overflow = 65400; + minPW_correction = 30; + maxPW_correction = 0; + } + + if (this->attached()) { + this->detach(); + } + + this->pin = pin; + this->minPW = (minPW + minPW_correction); + this->maxPW = (maxPW + maxPW_correction); + this->minAngle = minAngle; + this->maxAngle = maxAngle; + + timer_pause(tdev); + timer_set_prescaler(tdev, prescaler); // prescaler is 1-based + timer_set_reload(tdev, overflow); + timer_generate_update(tdev); + timer_resume(tdev); + + return true; +} + +bool Servo::detach() { + if (!this->attached()) { + return false; + } + + timer_dev *tdev = PIN_MAP[this->pin].timer_device; + uint8 tchan = PIN_MAP[this->pin].timer_channel; + timer_set_mode(tdev, tchan, TIMER_DISABLED); + + this->resetFields(); + + return true; +} + +void Servo::write(int degrees) { + degrees = constrain(degrees, this->minAngle, this->maxAngle); + this->writeMicroseconds(ANGLE_TO_US(degrees)); +} + +int Servo::read() const { + int a = US_TO_ANGLE(this->readMicroseconds()); + // map() round-trips in a weird way we mostly correct for here; + // the round-trip is still sometimes off-by-one for write(1) and + // write(179). + return a == this->minAngle || a == this->maxAngle ? a : a + 1; +} + +void Servo::writeMicroseconds(uint16 pulseWidth) { + if (!this->attached()) { + ASSERT(0); + return; + } + pulseWidth = constrain(pulseWidth, this->minPW, this->maxPW); + analogWrite(this->pin, US_TO_COMPARE(pulseWidth)); +} + +uint16 Servo::readMicroseconds() const { + if (!this->attached()) { + ASSERT(0); + return 0; + } + + stm32_pin_info pin_info = PIN_MAP[this->pin]; + uint16 compare = timer_get_compare(pin_info.timer_device, + pin_info.timer_channel); + + return COMPARE_TO_US(compare); +} + +void Servo::resetFields(void) { + this->pin = NOT_ATTACHED; + this->minAngle = MIN_ANGLE; + this->maxAngle = MAX_ANGLE; + this->minPW = MIN_PULSE_WIDTH; + this->maxPW = MAX_PULSE_WIDTH; +} + +#endif diff --git a/libraries/Servo/src/stm32f4/ServoTimers.h b/libraries/Servo/src/stm32f4/ServoTimers.h new file mode 100644 index 0000000..12d55b7 --- /dev/null +++ b/libraries/Servo/src/stm32f4/ServoTimers.h @@ -0,0 +1,207 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010, LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + + /* + * Arduino srl - www.arduino.org + * 2017 Feb 23: Edited by Francesco Alessi (alfran) - francesco@arduino.org + */ +#ifndef _SERVO_H_ +#define _SERVO_H_ + +#include "types.h" +#include "timer.h" + +#include "wiring.h" /* hack for IDE compile */ + +/* + * Note on Arduino compatibility: + * + * In the Arduino implementation, PWM is done "by hand" in the sense + * that timer channels are hijacked in groups and an ISR is set which + * toggles Servo::attach()ed pins using digitalWrite(). + * + * While this scheme allows any pin to drive a servo, it chews up + * cycles and complicates the programmer's notion of when a particular + * timer channel will be in use. + * + * This implementation only allows Servo instances to attach() to pins + * that already have a timer channel associated with them, and just + * uses pwmWrite() to drive the wave. + * + * This introduces an incompatibility: while the Arduino + * implementation of attach() returns the affected channel on success + * and 0 on failure, this one returns true on success and false on + * failure. + * + * RC Servos expect a pulse every 20ms. Since periods are set for + * entire timers, rather than individual channels, attach()ing a Servo + * to a pin can interfere with other pins associated with the same + * timer. As always, your board's pin map is your friend. + */ + +// Pin number of unattached pins +#define NOT_ATTACHED (-1) + +#define _Nbr_16timers 14 // mumber of STM32F469 Timers +#define SERVOS_PER_TIMER 4 // Number of timer channels + + +// Default min/max pulse widths (in microseconds) and angles (in +// degrees). Values chosen for Arduino compatibility. These values +// are part of the public API; DO NOT CHANGE THEM. +#define MIN_ANGLE 0 +#define MAX_ANGLE 180 + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo + +/** Class for interfacing with RC servomotors. */ +class Servo { +public: + /** + * @brief Construct a new Servo instance. + * + * The new instance will not be attached to any pin. + */ + Servo(); + + /** + * @brief Associate this instance with a servomotor whose input is + * connected to pin. + * + * If this instance is already attached to a pin, it will be + * detached before being attached to the new pin. This function + * doesn't detach any interrupt attached with the pin's timer + * channel. + * + * @param pin Pin connected to the servo pulse wave input. This + * pin must be capable of PWM output. + * + * @param minPulseWidth Minimum pulse width to write to pin, in + * microseconds. This will be associated + * with a minAngle degree angle. Defaults to + * SERVO_DEFAULT_MIN_PW = 544. + * + * @param maxPulseWidth Maximum pulse width to write to pin, in + * microseconds. This will be associated + * with a maxAngle degree angle. Defaults to + * SERVO_DEFAULT_MAX_PW = 2400. + * + * @param minAngle Target angle (in degrees) associated with + * minPulseWidth. Defaults to + * SERVO_DEFAULT_MIN_ANGLE = 0. + * + * @param maxAngle Target angle (in degrees) associated with + * maxPulseWidth. Defaults to + * SERVO_DEFAULT_MAX_ANGLE = 180. + * + * @sideeffect May set pinMode(pin, PWM). + * + * @return true if successful, false when pin doesn't support PWM. + */ + + bool attach(uint8 pin, + uint16 minPulseWidth=MIN_PULSE_WIDTH, + uint16 maxPulseWidth=MAX_PULSE_WIDTH, + int16 minAngle=MIN_ANGLE, + int16 maxAngle=MAX_ANGLE); + /** + * @brief Stop driving the servo pulse train. + * + * If not currently attached to a motor, this function has no effect. + * + * @return true if this call did anything, false otherwise. + */ + bool detach(); + + /** + * @brief Set the servomotor target angle. + * + * @param angle Target angle, in degrees. If the target angle is + * outside the range specified at attach() time, it + * will be clamped to lie in that range. + * + * @see Servo::attach() + */ + void write(int angle); + + /** + * @brief Set the pulse width, in microseconds. + * + * @param pulseWidth Pulse width to send to the servomotor, in + * microseconds. If outside of the range + * specified at attach() time, it is clamped to + * lie in that range. + * + * @see Servo::attach() + */ + void writeMicroseconds(uint16 pulseWidth); + + /** + * Get the servomotor's target angle, in degrees. This will + * lie inside the range specified at attach() time. + * + * @see Servo::attach() + */ + int read() const; + + /** + * Get the current pulse width, in microseconds. This will + * lie within the range specified at attach() time. + * + * @see Servo::attach() + */ + uint16 readMicroseconds() const; + + + /** + * @brief Check if this instance is attached to a servo. + * @return true if this instance is attached to a servo, false otherwise. + * @see Servo::attachedPin() + */ + bool attached() const { return this->pin != NOT_ATTACHED; } + + /** + * @brief Get the pin this instance is attached to. + * @return Pin number if currently attached to a pin, NOT_ATTACHED + * otherwise. + * @see Servo::attach() + */ + int attachedPin() const { return this->pin; } + +private: + int16 pin; + uint16 minPW; + uint16 maxPW; + int16 minAngle; + int16 maxAngle; + + void resetFields(void); +}; + + + +#endif /* _SERVO_H_ */ diff --git a/libraries/readme.txt b/libraries/readme.txt new file mode 100644 index 0000000..96ce674 --- /dev/null +++ b/libraries/readme.txt @@ -0,0 +1 @@ +For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries diff --git a/mqqt-neopixel/mqqt-neo/mqqt-neo.ino b/mqqt-neopixel/mqqt-neo/mqqt-neo.ino new file mode 100644 index 0000000..f4a7a87 --- /dev/null +++ b/mqqt-neopixel/mqqt-neo/mqqt-neo.ino @@ -0,0 +1,167 @@ +// Neopixel +#include + +// ESP 8266 +#include +#include + +// MQTT Subscribes +const char* SubscribeTo = "esp/1/out"; +const char* SubscribeFrom = "esp/1/in"; + +// WiFi & MQTT Server +const char* ssid = "Arubaruba"; +const char* password = "Keepitsimplestupid1"; +const char* mqtt_server = "192.168.178.50"; + +WiFiClient espClient; +PubSubClient pubClient(espClient); +long lastMsg = 0; +char msg[50]; +String message("#000000"); +String lastMessage("#000000"); + +// Neopixel Config +#define NeoPIN 13 +#define NUM_LEDS 4 +int brightness = 150; +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800); + +void setup() { + Serial.begin ( 115200 ); + + // ############## + // NeoPixel start + Serial.println(); + strip.setBrightness(brightness); + strip.begin(); + strip.show(); + delay(50); + + // WiFi + setup_wifi(); + + // MQTT + Serial.print("Connecting to MQTT..."); + // connecting to the mqtt server + pubClient.setServer(mqtt_server, 1883); + pubClient.setCallback(callback); + Serial.println("done!"); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + String color("#"); + + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + if(i > 0){ + color = color + String((char)payload[i]); + } + } + + Serial.println(); + + + // finding payload + if((char)payload[0] == '#'){ + // setting color + setNeoColor(color); + } + +} + +void setup_wifi() { + + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(100); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loop() { + if (!pubClient.connected()) { + delay(100); + reconnect(); + } + pubClient.loop(); + + long now = millis(); + if (now - lastMsg > 60000 || lastMessage.indexOf(message) < 0 ) { + + lastMsg = now; + Serial.print("Publish message: "); + Serial.println(message); + char msg[7]; + message.toCharArray(msg, message.length()); + Serial.print("Publishing..."); + pubClient.publish(SubscribeTo, msg); + Serial.println("Done!!!"); + lastMessage = message; + } + delay(10); +} + + +void setNeoColor(String value){ + Serial.println("Setting Neopixel..." + value); + message = value; + int number = (int) strtol( &value[1], NULL, 16); + + int r = number >> 16; + int g = number >> 8 & 0xFF; + int b = number & 0xFF; + + + + Serial.print("RGB: "); + Serial.print(r, DEC); + Serial.print(" "); + Serial.print(g, DEC); + Serial.print(" "); + Serial.print(b, DEC); + Serial.println(" "); + + for(int i=0; i < NUM_LEDS; i++) { + strip.setPixelColor(i, strip.Color( g, r, b ) ); + } + strip.show(); + + Serial.println("on."); +} + + +void reconnect() { + // Loop until we're reconnected + while (!pubClient.connected()) { + // Attempt to connect + if (pubClient.connect("ESP8266Client")) { + Serial.println("connected"); + // Once connected, publish an announcement... + pubClient.publish(SubscribeTo, "Restart"); + // ... and resubscribe + pubClient.subscribe(SubscribeFrom); + } else { + Serial.print("failed, rc="); + Serial.print(pubClient.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} diff --git a/mqqt-test/mqqtTest/mqqtTest.ino b/mqqt-test/mqqtTest/mqqtTest.ino new file mode 100644 index 0000000..fb95129 --- /dev/null +++ b/mqqt-test/mqqtTest/mqqtTest.ino @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +// Connect to the WiFi +const char* ssid = "Arubaruba"; +const char* password = "Keepitsimplestupid1"; +const char* mqtt_server = "192.168.178.50"; + +WiFiClient espClient; +PubSubClient client(espClient); + +const byte ledPin = 13; // Pin with LED on Adafruit Huzzah + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i=0;i + +/* +Into Robotics +*/ + +int servoPin = 9; +int ldrPin = A0; +Servo servo; + +int servoAngle = 0; // servo position in degrees +int currentAngle = 0; +int targetAngle = 0; +int ldrValue = 0; +void setup() +{ + Serial.begin(9600); + servo.attach(servoPin); + pinMode(ldrPin, INPUT); +} + + +void loop() +{ +//control the servo's direction and the position of the motor + +/* servo.write(45); // Turn SG90 servo Left to 45 degrees + delay(1000); // Wait 1 second + servo.write(90); // Turn SG90 servo back to 90 degrees (center position) + delay(1000); // Wait 1 second + servo.write(135); // Turn SG90 servo Right to 135 degrees + delay(1000); // Wait 1 second + servo.write(90); // Turn SG90 servo back to 90 degrees (center position) + delay(1000);*/ + +//end control the servo's direction and the position of the motor + ldrValue = analogRead(ldrPin); + Serial.println(ldrValue); + delay(1000); + if (ldrValue < 450){ + targetAngle = 0; + if (currentAngle >= targetAngle){ + for(servoAngle = currentAngle; servoAngle >= targetAngle; servoAngle--) { + servo.write(servoAngle); + delay(20); + } + } else { + for(servoAngle = currentAngle; servoAngle <= targetAngle; servoAngle++) { + servo.write(servoAngle); + delay(20); + } + } + currentAngle = targetAngle; + + } else if (ldrValue < 900 ) { + targetAngle = 50; + if (currentAngle >= targetAngle){ + for(servoAngle = currentAngle; servoAngle >= targetAngle; servoAngle--) { + servo.write(servoAngle); + delay(20); + } + } else { + for(servoAngle = currentAngle; servoAngle <= targetAngle; servoAngle++) { + servo.write(servoAngle); + delay(20); + } + } + currentAngle = targetAngle; + } else { + targetAngle = 140; + if (currentAngle >= targetAngle){ + for(servoAngle = currentAngle; servoAngle >= targetAngle; servoAngle--) { + servo.write(servoAngle); + delay(20); + } + } else { + for(servoAngle = currentAngle; servoAngle <= targetAngle; servoAngle++) { + servo.write(servoAngle); + delay(20); + } + } + currentAngle = targetAngle; + } +} + diff --git a/wifi/sketch_aug02a/sketch_aug02a.ino b/wifi/sketch_aug02a/sketch_aug02a.ino new file mode 100644 index 0000000..7cbf238 --- /dev/null +++ b/wifi/sketch_aug02a/sketch_aug02a.ino @@ -0,0 +1,41 @@ +#include "ESP8266WiFi.h" + +void setup() { + Serial.begin(115200); + // Set WiFi to station mode and disconnect from an AP if it was previously connected + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + delay(2000); + Serial.println("Setup done"); +} + +void loop() { + Serial.println("scan start"); + + + int n = WiFi.scanNetworks();// WiFi.scanNetworks will return the number of networks found + Serial.println("scan done"); + if (n == 0) + Serial.println("no networks found"); + else + { + Serial.print(n); + Serial.println(" networks found"); + for (int i = 0; i < n; ++i) + { + // Print SSID and RSSI for each network found + Serial.print(i + 1); + Serial.print(": "); + Serial.print(WiFi.SSID(i)); + Serial.print(" ("); + Serial.print(WiFi.RSSI(i)); + Serial.print(")"); + Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*"); + delay(10); + } + } + Serial.println(""); + + // Wait a bit before scanning again + delay(5000); +}