From 6ecfecac011f420a320173a89bea5383de3cb237 Mon Sep 17 00:00:00 2001 From: Carl Peto Date: Mon, 15 Aug 2022 22:31:32 +0300 Subject: [PATCH 1/3] Initial arduino library support for C++ files. Containing standard Wiring functions, UART and I2C master. --- v4.0/contributed libraries/Arduino.swift | 1 + v4.0/contributed libraries/catalogNew.txt | 68 ++++ .../unsafe_modules/Arduino/AdapterSerial.h | 64 ++++ .../unsafe_modules/Arduino/Arduino.h | 19 ++ .../unsafe_modules/Arduino/ArduinoSwift.h | 1 + .../unsafe_modules/Arduino/Client.h | 45 +++ .../unsafe_modules/Arduino/DebugPrinter.h | 12 + .../unsafe_modules/Arduino/IPAddress.h | 78 +++++ .../unsafe_modules/Arduino/Print.h | 97 ++++++ .../unsafe_modules/Arduino/Printable.h | 40 +++ .../unsafe_modules/Arduino/Server.h | 30 ++ .../Arduino/SimpleDebugPrinter.h | 13 + .../unsafe_modules/Arduino/Stream.h | 134 ++++++++ .../unsafe_modules/Arduino/Udp.h | 89 +++++ .../unsafe_modules/Arduino/Wire.h | 89 +++++ .../unsafe_modules/Arduino/avrTypes.h | 98 ++++++ .../unsafe_modules/Arduino/libArduino.a | Bin 0 -> 34380 bytes .../unsafe_modules/Arduino/module.modulemap | 4 + .../unsafe_modules/Arduino/shims.h | 138 ++++++++ .../ArduinoAdapter/AdapterSerial.cpp | 91 +++++ .../ArduinoAdapter/AdapterSerial.h | 64 ++++ .../ArduinoAdapter/Arduino.h | 19 ++ .../ArduinoAdapter/Arduino.swift | 1 + .../ArduinoAdapter/Client.h | 45 +++ .../ArduinoAdapter/DebugPrinter.cpp | 13 + .../ArduinoAdapter/DebugPrinter.h | 12 + .../ArduinoAdapter/IPAddress.cpp | 114 +++++++ .../ArduinoAdapter/IPAddress.h | 78 +++++ .../ArduinoAdapter/Makefile | 94 ++++++ .../ArduinoAdapter/Print.cpp | 266 +++++++++++++++ .../ArduinoAdapter/Print.h | 97 ++++++ .../ArduinoAdapter/Printable.h | 40 +++ .../ArduinoAdapter/Server.h | 30 ++ .../ArduinoAdapter/SimpleDebugPrinter.cpp | 12 + .../ArduinoAdapter/SimpleDebugPrinter.h | 13 + .../ArduinoAdapter/Stream.cpp | 319 ++++++++++++++++++ .../ArduinoAdapter/Stream.h | 134 ++++++++ .../ArduinoAdapter/Udp.h | 89 +++++ .../ArduinoAdapter/Wire.cpp | 278 +++++++++++++++ .../ArduinoAdapter/Wire.h | 89 +++++ .../ArduinoAdapter/abi.cpp | 45 +++ .../ArduinoAdapter/avrTypes.h | 98 ++++++ .../ArduinoAdapter/libArduino.a | Bin 0 -> 34380 bytes .../ArduinoAdapter/module.modulemap | 4 + .../ArduinoAdapter/shims.h | 138 ++++++++ 45 files changed, 3203 insertions(+) create mode 100644 v4.0/contributed libraries/Arduino.swift create mode 100644 v4.0/contributed libraries/catalogNew.txt create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/AdapterSerial.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/ArduinoSwift.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Client.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/DebugPrinter.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/IPAddress.h create mode 100755 v4.0/contributed libraries/unsafe_modules/Arduino/Print.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Printable.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Server.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/SimpleDebugPrinter.h create mode 100755 v4.0/contributed libraries/unsafe_modules/Arduino/Stream.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Udp.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/Wire.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/avrTypes.h create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/libArduino.a create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/module.modulemap create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/shims.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.swift create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Client.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Makefile create mode 100755 v4.0/contributed unsafe modules/ArduinoAdapter/Print.cpp create mode 100755 v4.0/contributed unsafe modules/ArduinoAdapter/Print.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Printable.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Server.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.h create mode 100755 v4.0/contributed unsafe modules/ArduinoAdapter/Stream.cpp create mode 100755 v4.0/contributed unsafe modules/ArduinoAdapter/Stream.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Udp.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/Wire.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/abi.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/avrTypes.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/libArduino.a create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/module.modulemap create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/shims.h diff --git a/v4.0/contributed libraries/Arduino.swift b/v4.0/contributed libraries/Arduino.swift new file mode 100644 index 0000000..298cfa8 --- /dev/null +++ b/v4.0/contributed libraries/Arduino.swift @@ -0,0 +1 @@ +import Arduino diff --git a/v4.0/contributed libraries/catalogNew.txt b/v4.0/contributed libraries/catalogNew.txt new file mode 100644 index 0000000..8219b9d --- /dev/null +++ b/v4.0/contributed libraries/catalogNew.txt @@ -0,0 +1,68 @@ +# This file lists all libraries and documentation the S4A IDE should automatically download +# standard libraries +MPL31155A2.swift +rotaryEncoder.swift +servo.swift +stepper-28BYJ-48.swift +iLEDHelpers.swift +DHT11.swift +Grove2DigitAlnum.swift +fade.swift +pulseIn.swift +unitTests.swift + +# standard library documentation +MPL31155A2.txt +rotaryEncoder.txt +servo.txt +stepper-28BYJ-48.txt +iLEDHelpers.txt +DHT11.txt +Grove2DigitAlnum.txt +fade.txt +pulseIn.txt +unitTests.txt + +# unsafe libraries and documentation +unsafeSeeed_RGB_LED_Matrix.swift +unsafeSeeed_RGB_LED_Matrix.txt +unsafe_modules/Seeed_RGB_LED_Matrix/libSeeed_RGB_LED_Matrix.a +unsafe_modules/Seeed_RGB_LED_Matrix/module.modulemap +unsafe_modules/Seeed_RGB_LED_Matrix/Seeed_RGB_LED_Matrix.h + +unsafeAdafruit_Bluefruit_LE.swift +unsafeAdafruit_Bluefruit_LE.txt +unsafe_modules/Adafruit_Bluefruit_LE/Adafruit_Bluefruit_LE.h +unsafe_modules/Adafruit_Bluefruit_LE/module.modulemap +unsafe_modules/Adafruit_Bluefruit_LE/libAdafruit_Bluefruit_LE.a + +unsafeMAX30105.swift +unsafeMAX30105.txt +unsafe_modules/MAX30105/MAX30105_.h +unsafe_modules/MAX30105/module.modulemap +unsafe_modules/MAX30105/libMAX30105.a + +unsafeTimerInterrupts.swift +unsafeTimerInterrupts.txt +unsafe_modules/TimerInterrupts/module.modulemap +unsafe_modules/TimerInterrupts/libTimerInterrupts.a +unsafe_modules/TimerInterrupts/TimerInterrupts.h + +unsafe_modules/Arduino/libArduino.a +unsafe_modules/Arduino/module.modulemap +unsafe_modules/Arduino/Arduino.h +Arduino.swift +unsafe_modules/Arduino/ArduinoSwift.h +unsafe_modules/Arduino/SimpleDebugPrinter.h +unsafe_modules/Arduino/DebugPrinter.h +unsafe_modules/Arduino/Print.h +unsafe_modules/Arduino/Stream.h +unsafe_modules/Arduino/shims.h +unsafe_modules/Arduino/avrTypes.h +unsafe_modules/Arduino/AdapterSerial.h +unsafe_modules/Arduino/IPAddress.h +unsafe_modules/Arduino/Printable.h +unsafe_modules/Arduino/Udp.h +unsafe_modules/Arduino/Client.h +unsafe_modules/Arduino/Server.h +unsafe_modules/Arduino/Wire.h \ No newline at end of file diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/AdapterSerial.h b/v4.0/contributed libraries/unsafe_modules/Arduino/AdapterSerial.h new file mode 100644 index 0000000..e2d28c0 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/AdapterSerial.h @@ -0,0 +1,64 @@ +/* + +ORIGINAL FILE AND COPYRIGHT: + HardwareSerial.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman + + + +NEW FILE: AdapterSerial.h +Changes are copyright (c) 2022 Carl Peto. + +Rewritten to work as stubs with the AVR library native to the Swift for Arduino platform. + +*/ + +#ifndef Serial_h +#define Serial_h + +#include + +#include "Stream.h" + +class AdapterSerial : public Stream +{ + public: + inline AdapterSerial(); + void begin(unsigned long baud) { } // noop + void begin(unsigned long, uint8_t); + void end(); + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool() { return true; } +}; + +extern AdapterSerial Serial; + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h new file mode 100644 index 0000000..83a04d9 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h @@ -0,0 +1,19 @@ +#define ARDUINO_SHIM_DEFINED (unsigned char)1 + +#ifdef __cplusplus + +#include "DebugPrinter.h" +#include "Print.h" +#include "SimpleDebugPrinter.h" +#include "Stream.h" +#include "AdapterSerial.h" +#include "Wire.h" + +// these seem to cause program crashes +// #include "IPAddress.h" +// #include "Printable.h" +// #include "Udp.h" +// #include "Client.h" +// #include "Server.h" + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/ArduinoSwift.h b/v4.0/contributed libraries/unsafe_modules/Arduino/ArduinoSwift.h new file mode 100644 index 0000000..b39abff --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/ArduinoSwift.h @@ -0,0 +1 @@ +#define ARDUINO_SHIM_DEFINED (unsigned char)1 diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Client.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Client.h new file mode 100644 index 0000000..b8e5d93 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Client.h @@ -0,0 +1,45 @@ +/* + Client.h - Base class that provides Client + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef client_h +#define client_h +#include "Print.h" +#include "Stream.h" +#include "IPAddress.h" + +class Client : public Stream { + +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; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/DebugPrinter.h b/v4.0/contributed libraries/unsafe_modules/Arduino/DebugPrinter.h new file mode 100644 index 0000000..3c813de --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/DebugPrinter.h @@ -0,0 +1,12 @@ +#include +#include +#include "Print.h" + +class DebugPrinter : public Print +{ +protected: + size_t(*_printCallback)(const uint8_t *buf, size_t size) = 0; +public: + DebugPrinter(size_t(*callback)(const uint8_t *buf, size_t size)) { _printCallback = callback; } + virtual size_t write(const uint8_t *buffer, size_t size); +}; \ No newline at end of file diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/IPAddress.h b/v4.0/contributed libraries/unsafe_modules/Arduino/IPAddress.h new file mode 100644 index 0000000..a147a88 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/IPAddress.h @@ -0,0 +1,78 @@ +/* + IPAddress.h - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef IPAddress_h +#define IPAddress_h + +#include +#include "Printable.h" +// #include "WString.h" + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _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.bytes; }; + +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); + + bool fromString(const char *address); + // bool fromString(const String &address) { return fromString(address.c_str()); } + + // 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() const { return _address.dword; }; + bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address.bytes[index]; }; + uint8_t& operator[](int index) { return _address.bytes[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Print.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Print.h new file mode 100755 index 0000000..192d1e1 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Print.h @@ -0,0 +1,97 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. 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 +*/ + +#ifndef Print_h +#define Print_h + +#include +#include // for size_t +#include +#include + +class __FlashStringHelper; + +// #include "WString.h" +// #include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar +#undef BIN +#endif +#define BIN 2 + +class Print +{ + private: + int write_error; + size_t printNumber(unsigned long, uint8_t); + size_t printFloat(double, uint8_t); + protected: + void setWriteError(int err = 1) { write_error = err; } + public: + Print() : write_error(0) {} + + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + + virtual size_t write(uint8_t) = 0; + size_t write(const char *str) { + if (str == NULL) return 0; + return write((const uint8_t *)str, strlen(str)); + } + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + // default to zero, meaning "a single write may block" + // should be overriden by subclasses with buffering + virtual int availableForWrite() { return 0; } + + size_t print(const __FlashStringHelper *); + // size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + // size_t print(const Printable&); + + size_t println(const __FlashStringHelper *); + // size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + // size_t println(const Printable&); + size_t println(void); + + virtual void flush() { /* Empty implementation for backward compatibility */ } +}; + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Printable.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Printable.h new file mode 100644 index 0000000..2a1b2e9 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Printable.h @@ -0,0 +1,40 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef Printable_h +#define Printable_h + +#include + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ + +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Server.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Server.h new file mode 100644 index 0000000..69e3e39 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Server.h @@ -0,0 +1,30 @@ +/* + Server.h - Base class that provides Server + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef server_h +#define server_h + +#include "Print.h" + +class Server : public Print { +public: + virtual void begin() =0; +}; + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/SimpleDebugPrinter.h b/v4.0/contributed libraries/unsafe_modules/Arduino/SimpleDebugPrinter.h new file mode 100644 index 0000000..b4cbaa3 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/SimpleDebugPrinter.h @@ -0,0 +1,13 @@ +#include +#include "Print.h" + +#define _Bool bool +#include "shims.h" + +class SimpleDebugPrinter : public Print +{ +public: + SimpleDebugPrinter() { } + virtual size_t write(const uint8_t *buffer, size_t size); + virtual size_t write(uint8_t); +}; \ No newline at end of file diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Stream.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Stream.h new file mode 100755 index 0000000..102a284 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Stream.h @@ -0,0 +1,134 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 David A. Mellis. 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 + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef Stream_h +#define Stream_h + +// compatibility layer +#define _Bool bool + +#include + +#include "shims.h" +#include "Print.h" + +// compatability macros for testing +/* +#define getInt() parseInt() +#define getInt(ignore) parseInt(ignore) +#define getFloat() parseFloat() +#define getFloat(ignore) parseFloat(ignore) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +// This enumeration provides the lookahead options for parseInt(), parseFloat() +// The rules set out here are used until either the first valid character is found +// or a time out occurs due to lack of input. +enum LookaheadMode{ + SKIP_ALL, // All invalid characters are ignored. + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. +}; + +#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field + +class Stream : public Print +{ + protected: + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read + unsigned long _startMillis; // used for timeout measurement + int timedRead(); // read stream with timeout + int timedPeek(); // peek stream with timeout + int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + unsigned long getTimeout(void) { return _timeout; } + + bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } + // returns true if target string is found, false if timed out + + bool find(char target) { return find (&target, 1); } + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } + + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // returns the first valid (long) integer value from the current position. + // lookahead determines how parseInt looks ahead in the stream. + // See LookaheadMode enumeration at the top of the file. + // Lookahead is terminated by the first character that is not a valid part of an integer. + // Once parsing commences, 'ignore' will be skipped in the stream. + + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // float version of parseInt + + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } + // terminates if length characters have been read or timeout (see setTimeout) + // returns the number of characters placed in the buffer (0 means no valid data found) + + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } + // terminates if length characters have been read, timeout, or if the terminator character detected + // returns the number of characters placed in the buffer (0 means no valid data found) + + // // Arduino String functions to be added here + // String readString(); + // String readStringUntil(char terminator); + + protected: + long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } + float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } + // These overload exists for compatibility with any class that has derived + // Stream and used parseFloat/Int with a custom ignore character. To keep + // the public API simple, these overload remains protected. + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); +}; + +#undef NO_IGNORE_CHAR +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Udp.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Udp.h new file mode 100644 index 0000000..25fa15d --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Udp.h @@ -0,0 +1,89 @@ +/* + * Udp.cpp: Library to send/receive UDP packets. + * + * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) + * 1) UDP does not guarantee the order in which assembled UDP packets are received. This + * might not happen often in practice, but in larger network topologies, a UDP + * packet can be received out of sequence. + * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being + * aware of it. Again, this may not be a concern in practice on small local networks. + * For more information, see http://www.cafeaulait.org/course/week12/35.html + * + * MIT License: + * Copyright (c) 2008 Bjoern Hartmann + * 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. + * + * bjoern@cs.stanford.edu 12/30/2008 + */ + +#ifndef udp_h +#define udp_h + +#include "Stream.h" +#include "IPAddress.h" + +class UDP : public Stream { + +public: + virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure + virtual void stop() =0; // Finish with the UDP socket + + // Sending UDP packets + + // Start building up a packet to send to the remote host specific in ip and port + // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port + virtual int beginPacket(IPAddress ip, uint16_t port) =0; + // Start building up a packet to send to the remote host specific in host and port + // Returns 1 if successful, 0 if there was a problem resolving the hostname or port + virtual int beginPacket(const char *host, uint16_t port) =0; + // Finish off this packet and send it + // Returns 1 if the packet was sent successfully, 0 if there was an error + virtual int endPacket() =0; + // Write a single byte into the packet + virtual size_t write(uint8_t) =0; + // Write size bytes from buffer into the packet + virtual size_t write(const uint8_t *buffer, size_t size) =0; + + // Start processing the next available incoming packet + // Returns the size of the packet in bytes, or 0 if no packets are available + virtual int parsePacket() =0; + // Number of bytes remaining in the current packet + virtual int available() =0; + // Read a single byte from the current packet + virtual int read() =0; + // Read up to len bytes from the current packet and place them into buffer + // Returns the number of bytes read, or 0 if none are available + virtual int read(unsigned char* buffer, size_t len) =0; + // Read up to len characters from the current packet and place them into buffer + // Returns the number of characters read, or 0 if none are available + virtual int read(char* buffer, size_t len) =0; + // Return the next byte from the current packet without moving on to the next byte + virtual int peek() =0; + virtual void flush() =0; // Finish reading the current packet + + // Return the IP address of the host who sent the current incoming packet + virtual IPAddress remoteIP() =0; + // Return the port of the host who sent the current incoming packet + virtual uint16_t remotePort() =0; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Wire.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Wire.h new file mode 100644 index 0000000..fc2d877 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Wire.h @@ -0,0 +1,89 @@ +/* + TwoWire.h - TWI/I2C library for Arduino & Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts + Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts +*/ + +#ifndef TwoWire_h +#define TwoWire_h + +#include +#include "Stream.h" + +#define BUFFER_LENGTH 32 + +// WIRE_HAS_END means Wire has end() +#define WIRE_HAS_END 1 + +class I2CAdapter : public Stream +{ + private: + static uint8_t rxBuffer[]; + static uint8_t rxBufferIndex; + static uint8_t rxBufferLength; + + bool activatePullups; + uint8_t premultiplier; + uint8_t slaveAddress; + + // static to mirror Arduino standard semantics + // for most people it will never matter + static uint8_t txAddress; + static uint8_t txBuffer[]; + static uint8_t txBufferIndex; + static uint8_t txBufferLength; + public: + I2CAdapter(uint8_t premultiplier = 0, bool activatePullups = false); + void begin(); + void begin(uint8_t); + void begin(int); + void end(); + void setClock(uint32_t); + void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false); + bool getWireTimeoutFlag(void); + void clearWireTimeoutFlag(void); + void beginTransmission(uint8_t); + void beginTransmission(int); + uint8_t endTransmission(void); + uint8_t endTransmission(uint8_t); + uint8_t requestFrom(uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); + uint8_t requestFrom(int, int); + uint8_t requestFrom(int, int, int); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *, size_t); + virtual int available(void); + virtual int read(void); + virtual int peek(void); + virtual void flush(void); + void onReceive( void (*)(int) ); + void onRequest( void (*)(void) ); + + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; +}; + +extern I2CAdapter Wire; + +#endif + diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/avrTypes.h b/v4.0/contributed libraries/unsafe_modules/Arduino/avrTypes.h new file mode 100644 index 0000000..4fa65de --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/avrTypes.h @@ -0,0 +1,98 @@ +#define false 0 +#define true 1 +#define int32 long +#define uint32 unsigned long +#define iLEDColor uint32 +#define iLEDLevel unsigned char + +// note, use short, short int, unsigned short, unsigned short int for 16 bit ints, they are the same on both platforms +// use int32 for 32 bit int +// use long long int or unsigned long long int for 64 bit ints + + +#ifdef __clang_version__ +// Clang (we are parsing header files as bridging header) +#define __nonnull _Nonnull +#define __nullable _Nullable +#else +// GCC +#define __nonnull +#define __nullable +#endif + +typedef void (* __nonnull adcCallback)(unsigned short value); +typedef void (* __nonnull temperatureCallback)(short centigrade); +typedef void (* __nonnull externalInterruptCallback)(); +typedef void (* __nullable optionalInterruptCallback)(); +typedef void (* __nonnull asyncCallback)(); +typedef void (* __nonnull asyncCallbackWithContext)(unsigned short context); +typedef void (* __nonnull slaveBufferCallback)(const unsigned char * __nonnull buffer, unsigned char length); +typedef const unsigned char * __nonnull (* __nonnull slaveGetBufferToSendCallback)(unsigned char * __nonnull bufferLength); + +typedef void (* __nonnull slaveRegisterReceiveCallback)(unsigned char register, unsigned char value); +typedef unsigned char (* __nonnull slaveRegisterSendCallback)(unsigned char register); + +// define the most used constants using #defines, so that they will be imported +// as true constants into swift and lowered into MC as constants +#define HIGH (_Bool)1 +#define LOW (_Bool)0 +#define OUTPUT (_Bool)1 +#define INPUT (_Bool)0 +#define WHILE_LOW (unsigned char)0 +#define CHANGING_EDGE (unsigned char)1 +#define FALLING_EDGE (unsigned char)2 +#define RISING_EDGE (unsigned char)3 + +#define D0 (unsigned char)0 +#define D1 (unsigned char)1 +#define D2 (unsigned char)2 +#define D3 (unsigned char)3 +#define D4 (unsigned char)4 +#define D5 (unsigned char)5 +#define D6 (unsigned char)6 +#define D7 (unsigned char)7 +#define D8 (unsigned char)8 +#define D9 (unsigned char)9 +#define D10 (unsigned char)10 +#define D11 (unsigned char)11 +#define D12 (unsigned char)12 +#define D13 (unsigned char)13 + +#define D14 (unsigned char)14 // this is pin A0 on an UNO, used as a digital input/output +#define D15 (unsigned char)15 // this is pin A1 on an UNO, used as a digital input/output +#define D16 (unsigned char)16 // this is pin A2 on an UNO, used as a digital input/output +#define D17 (unsigned char)17 // this is pin A3 on an UNO, used as a digital input/output +#define D18 (unsigned char)18 // this is pin A4 on an UNO, used as a digital input/output +#define D19 (unsigned char)19 // this is pin A5 on an UNO, used as a digital input/output + +#define A0 (unsigned char)128 // this is pin A0 for use in ADC functions (=128+0) +#define A1 (unsigned char)129 // this is pin A0 for use in ADC functions (=128+1) +#define A2 (unsigned char)130 // this is pin A0 for use in ADC functions (=128+2) +#define A3 (unsigned char)131 // this is pin A0 for use in ADC functions (=128+3) +#define A4 (unsigned char)132 // this is pin A0 for use in ADC functions (=128+4) +#define A5 (unsigned char)133 // this is pin A0 for use in ADC functions (=128+5) + +// for less frequently used constants, use defined globals + +#define minAnalogPin (unsigned char)0 +#define maxAnalogPin (unsigned char)5 + +#define minAnalogValue (unsigned short)0 +#define maxAnalogValue (unsigned short)1023 + +extern iLEDColor iLEDWhite; +extern iLEDColor iLEDRed; +extern iLEDColor iLEDGreen; +extern iLEDColor iLEDBlue; +extern iLEDColor iLEDOff; + +extern const unsigned char WATCHDOG_TIMEOUT_15MS; +extern const unsigned char WATCHDOG_TIMEOUT_30MS; +extern const unsigned char WATCHDOG_TIMEOUT_60MS; +extern const unsigned char WATCHDOG_TIMEOUT_120MS; +extern const unsigned char WATCHDOG_TIMEOUT_250MS; +extern const unsigned char WATCHDOG_TIMEOUT_500MS; +extern const unsigned char WATCHDOG_TIMEOUT_1S; +extern const unsigned char WATCHDOG_TIMEOUT_2S; +extern const unsigned char WATCHDOG_TIMEOUT_4S; +extern const unsigned char WATCHDOG_TIMEOUT_8S; diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/libArduino.a b/v4.0/contributed libraries/unsafe_modules/Arduino/libArduino.a new file mode 100644 index 0000000000000000000000000000000000000000..3bd408e9c33e829ab190906b32b61074b45c6c07 GIT binary patch literal 34380 zcmd^o3w%`7wf;Uc1crw^1`rg4fl@>alS~MZSgJt@qM}B@M=dr4GJ(;&Ou|FkY9;|e z1PPD`S}WGuT3esBy=twu#J0EUt+m?Qhqu}~MQ=-6DxqSlw^aV$w;wZm_RJ(m+uQ!` zf5D!!*4k@-`~5g)pM5fEZdKEYRc8(=2&Ge@B`V(j4TXwIN{W=pvOz+KN+AZn^1c=+dW1L@7hNaBKbNb#>Y~|;TbiO(b)_w_x@h&K(W>h5HNsU_ zM5EWHsUtHRp}Hu#u4R5~WvrzgaL{AyU{JZCL{FhSe?Qb(Tj- zV^vdg^t}3v&?_vXNTIY^P}@+|t6eGbP<{6GEz#!kiWS$`0L4k4%j#QVwdE_){Yz_N z_0<=yu5F2xS6sBXG7^#elB%Ya(U$U<1tbj>1P~S%R@$+odBV>MSIh{UL>V2;W>qxB z>RTdns@7D+YO9vlMi(?RUC|V4iK+ru-m_|IS2wRp)2x*?6&I|M>Ck3IDT`3+8i`+C zaY3ZCvT{LfRr4xTc&vWq!f0({v?-N>S;j6itfiSFt!nG-q$G8H#$j1z!?bjfh>Yl> z)pg6GP33i~GSXQq>n1IWF-w(&<;xm;yiR93E7S5?`#x+z+@Cf3xlx~f*Is-v~hmT0x3UAeldsk*Xi#dWJ= zO;K&BbWO0YYKHDOnq#D7ajdSfHab7LeD%r-W+~b<%d|t)ID0W+zL#8{&47uC%$!$U z)d;}F(WY2c?aU~8k{q2J54(FfUd0@!vIff8e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unsafe_modules/Arduino/module.modulemap new file mode 100644 index 0000000..5a532ac --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/module.modulemap @@ -0,0 +1,4 @@ +module Arduino { + header "Arduino.h" + link "Arduino" +} \ No newline at end of file diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h b/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h new file mode 100644 index 0000000..b6b6e96 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h @@ -0,0 +1,138 @@ +// AVR library primitives/internals, not for use from Swift, use the Swift wrapper +// functions in the main AVR module instead + +#ifndef _SHIMS_H_ +#define _SHIMS_H_ +extern "C" { + +#ifndef uint32 +#define uint32 uint32_t +#endif + +#ifndef millis +#define millis _ticks +#endif + +// nullable annotations are meaningless in GCC +#ifdef __clang_version__ +// Clang (we are parsing header files as bridging header) +#define __nonnull _Nonnull +#define __nullable _Nullable +#else +// GCC +#define __nonnull +#define __nullable +#endif + +#ifndef digitalRead +#define digitalRead _digitalRead +#endif + +#ifndef digitalWrite +#define digitalWrite _digitalWrite +#endif + +#ifndef pinMode +#define pinMode _pinMode +#endif + +#ifndef delayMicroseconds +#define delayMicroseconds _delayUs +#endif + + void _i2cinit(unsigned char speed, unsigned char premultiplier, _Bool activatePullups); + _Bool _i2cstart(); + void _i2cstop(); + _Bool _i2cwrite(unsigned char byte); + unsigned char _i2cread(_Bool sendAck); + + // higher level I2C + void _i2cWriteByte(unsigned char address, unsigned char byte); + void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * buffer, _Bool isString); + const unsigned char * __nullable _i2cReadBuffer(unsigned char address, unsigned char size, unsigned char * __nullable buffer); + void _i2cshutdown(); + + // SPI + // enable hardware SPI and set parameters, note: this takes over pins 11,12,13 and sets pin 10 as output (for use as SS) + // speed is 0 - 3, 0 is fastest, clock speed/4, 1 = clock/16, 2 = clock/64, 3 = clock/128 + void _setupSPIAsMaster(unsigned char speed, unsigned char mode, _Bool lsb); + // not recommended when already running on maximum speed (speed == 0, clock/4) + void _setupSPIDoubleSpeed(_Bool doubleSpeed); + // blocks until transmission and reception complete + unsigned char _sendSPIByteBlocking(unsigned char byte); + // blocks until transmission and reception complete + const char * __nonnull _sendReceiveSPIBufferBlocking(unsigned short maxMsgLen, const char * __nonnull message, _Bool sendMessage, _Bool sendString, _Bool receiveString); + // blocks until reception complete + const char * __nonnull _receiveSPIBufferBlocking(unsigned short maxMsgLen, _Bool receiveString); + // disable hardware SPI, release pins 11,12,13, you should now reset their input/output modes if you want to use them + void __attribute__((weak)) _stopSPI(); + + // utils + uint32 _ticks(); + void _delayMs(unsigned short delay); + void _delayUs(unsigned short delayUs); + + // digital IO + _Bool _digitalRead(unsigned char pin); + void _digitalWrite(unsigned char pin, _Bool value); + void _pinMode(unsigned char pin, _Bool write); + + /* Raw read/write. */ + unsigned char _getPortB(); + unsigned char _getPortC(); + unsigned char _getPortD(); + + void _setPortB(unsigned char value); + void _setPortC(unsigned char value); + void _setPortD(unsigned char value); + + unsigned char _getDDRB(); + unsigned char _getDDRC(); + unsigned char _getDDRD(); + + void _setDDRB(unsigned char value); + void _setDDRC(unsigned char value); + void _setDDRD(unsigned char value); + + //USART + void _sendByte(unsigned char byte); + // send a hard coded string from the PROGMEM (does not use RAM) + void _sendString(unsigned short maxMsgLen, const char * __nonnull message, _Bool addNewline); // must be NULL terminated + // send a buffer from RAM (do not mix these two up!) + void _sendBuffer(unsigned short maxMsgLen, const char * __nonnull buffer, _Bool addNewline); // must be NULL terminated + _Bool _available(); + unsigned char _receiveByte(); + + // SPI + // blocks until transmission and reception complete + unsigned char _sendSPIByteBlocking(unsigned char byte); + // blocks until transmission and reception complete + const char * __nonnull _sendReceiveSPIBufferBlocking( + unsigned short maxMsgLen, + const char * __nonnull message, + _Bool sendMessage, + _Bool sendString, + _Bool receiveString); + + // Memory efficient string buffer handling/building. + // note, only one string buffer can be used at a time + _Bool _stringAddSingleCharacter(unsigned char byte); + _Bool _stringAddWord(unsigned short word, _Bool bigEndian); + void _stringStartNew(); + const char * __nonnull _stringCurrentValue(); + const char _stringCurrentLength(); + const char _stringRemainingLength(); + const char * __nonnull _stringAdd(const char * __nullable message); + const char * __nonnull _stringAddFromProgmem(const char * __nullable message); + const char * __nonnull _emptyStringBuffer(); + + extern const _Bool HIGH; + extern const _Bool LOW; + extern const _Bool OUTPUT; + extern const _Bool INPUT; + extern const unsigned char WHILE_LOW; + extern const unsigned char CHANGING_EDGE; + extern const unsigned char FALLING_EDGE; + extern const unsigned char RISING_EDGE; +} +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.cpp new file mode 100644 index 0000000..4f4d3a0 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.cpp @@ -0,0 +1,91 @@ +/* + +ORIGINAL FILE AND COPYRIGHT: + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman + + + +NEW FILE: AdapterSerial.cpp +Changes are copyright (c) 2022 Carl Peto. + +Rewritten to work as stubs with the AVR library native to the Swift for Arduino platform. + +*/ + +#include +#include +#include +#include +#include +#include "Arduino.h" + +#include "AdapterSerial.h" + +AdapterSerial::AdapterSerial() +{ +} + +void AdapterSerial::begin(unsigned long baud, uint8_t config) +{ + // no-op, the hardware is started by standard AVR calls +} + +void AdapterSerial::end() +{ + // no-op, the hardware is stopped by standard AVR calls +} + +int AdapterSerial::available(void) +{ + return _available() ? 1 : 0; +} + +int AdapterSerial::peek(void) +{ + // non functional on this platform + return -1; +} + +int AdapterSerial::read(void) +{ + return _receiveByte(); +} + +int AdapterSerial::availableForWrite(void) +{ + return 0; +} + +void AdapterSerial::flush() +{ + // no-op on this platform +} + +size_t AdapterSerial::write(uint8_t c) +{ + _sendByte(c); + return 1; +} + + +AdapterSerial Serial; diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.h b/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.h new file mode 100644 index 0000000..e2d28c0 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/AdapterSerial.h @@ -0,0 +1,64 @@ +/* + +ORIGINAL FILE AND COPYRIGHT: + HardwareSerial.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman + + + +NEW FILE: AdapterSerial.h +Changes are copyright (c) 2022 Carl Peto. + +Rewritten to work as stubs with the AVR library native to the Swift for Arduino platform. + +*/ + +#ifndef Serial_h +#define Serial_h + +#include + +#include "Stream.h" + +class AdapterSerial : public Stream +{ + public: + inline AdapterSerial(); + void begin(unsigned long baud) { } // noop + void begin(unsigned long, uint8_t); + void end(); + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool() { return true; } +}; + +extern AdapterSerial Serial; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h new file mode 100644 index 0000000..83a04d9 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h @@ -0,0 +1,19 @@ +#define ARDUINO_SHIM_DEFINED (unsigned char)1 + +#ifdef __cplusplus + +#include "DebugPrinter.h" +#include "Print.h" +#include "SimpleDebugPrinter.h" +#include "Stream.h" +#include "AdapterSerial.h" +#include "Wire.h" + +// these seem to cause program crashes +// #include "IPAddress.h" +// #include "Printable.h" +// #include "Udp.h" +// #include "Client.h" +// #include "Server.h" + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.swift b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.swift new file mode 100644 index 0000000..298cfa8 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.swift @@ -0,0 +1 @@ +import Arduino diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Client.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Client.h new file mode 100644 index 0000000..b8e5d93 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Client.h @@ -0,0 +1,45 @@ +/* + Client.h - Base class that provides Client + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef client_h +#define client_h +#include "Print.h" +#include "Stream.h" +#include "IPAddress.h" + +class Client : public Stream { + +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; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.cpp new file mode 100644 index 0000000..ccf2bd0 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.cpp @@ -0,0 +1,13 @@ +#include "DebugPrinter.h" + +size_t DebugPrinter::write(const uint8_t *buf, size_t size) +{ + if ( _printCallback ) + { + return _printCallback(buf, size); + } + else + { + return 0; + } +} \ No newline at end of file diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.h b/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.h new file mode 100644 index 0000000..3c813de --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/DebugPrinter.h @@ -0,0 +1,12 @@ +#include +#include +#include "Print.h" + +class DebugPrinter : public Print +{ +protected: + size_t(*_printCallback)(const uint8_t *buf, size_t size) = 0; +public: + DebugPrinter(size_t(*callback)(const uint8_t *buf, size_t size)) { _printCallback = callback; } + virtual size_t write(const uint8_t *buffer, size_t size); +}; \ No newline at end of file diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.cpp new file mode 100644 index 0000000..d9fe5be --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.cpp @@ -0,0 +1,114 @@ +/* + IPAddress.cpp - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#include +#include + +IPAddress::IPAddress() +{ + _address.dword = 0; +} + +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) +{ + _address.bytes[0] = first_octet; + _address.bytes[1] = second_octet; + _address.bytes[2] = third_octet; + _address.bytes[3] = fourth_octet; +} + +IPAddress::IPAddress(uint32_t address) +{ + _address.dword = address; +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +bool IPAddress::fromString(const char *address) +{ + uint16_t acc = 0; // Accumulator + uint8_t dots = 0; + + while (*address) + { + char c = *address++; + if (c >= '0' && c <= '9') + { + acc = acc * 10 + (c - '0'); + if (acc > 255) { + // Value out of [0..255] range + return false; + } + } + else if (c == '.') + { + if (dots == 3) { + // Too much dots (there must be 3 dots) + return false; + } + _address.bytes[dots++] = acc; + acc = 0; + } + else + { + // Invalid char + return false; + } + } + + if (dots != 3) { + // Too few dots (there must be 3 dots) + return false; + } + _address.bytes[3] = acc; + return true; +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + _address.dword = address; + return *this; +} + +bool IPAddress::operator==(const uint8_t* addr) const +{ + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; +} + +size_t IPAddress::printTo(Print& p) const +{ + size_t n = 0; + for (int i =0; i < 3; i++) + { + n += p.print(_address.bytes[i], DEC); + n += p.print('.'); + } + n += p.print(_address.bytes[3], DEC); + return n; +} + diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.h b/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.h new file mode 100644 index 0000000..a147a88 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/IPAddress.h @@ -0,0 +1,78 @@ +/* + IPAddress.h - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef IPAddress_h +#define IPAddress_h + +#include +#include "Printable.h" +// #include "WString.h" + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _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.bytes; }; + +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); + + bool fromString(const char *address); + // bool fromString(const String &address) { return fromString(address.c_str()); } + + // 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() const { return _address.dword; }; + bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address.bytes[index]; }; + uint8_t& operator[](int index) { return _address.bytes[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile b/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile new file mode 100644 index 0000000..8a36996 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile @@ -0,0 +1,94 @@ +# outputs +COPIED_FILES_U=module.modulemap Arduino.h SimpleDebugPrinter.h DebugPrinter.h Print.h Stream.h shims.h AdapterSerial.h\ +Wire.h + +# these seem to crash programs +# IPAddress.h Printable.h +# IPAddress.h Printable.h +# Udp.h Client.h Server.h +COPIED_FILES_S=Arduino.swift +MODULE_NAME=Arduino + +# program location and settings +# reasonable defaults, change to suit your build +AVR_BINUTILS_DIR=/usr/local/bin +AVR_GCC_BIN_DIR=/usr/local/bin +# this relies on the current S4A Community folder +# structure so it could very easily break in future +UNSAFE_MODULES_INSTALL_DIR=../../contributed\ libraries/unsafe_modules +SAFE_FILES_INSTALL_DIR=../../contributed\ libraries +CATALOG_FILE=../../contributed\ libraries/catalogNew.txt +LEGACY_CATALOG_FILE=../../contributed\ libraries/catalog.txt + + +# source files and settings +CPP_FILES=Stream.cpp Print.cpp abi.cpp SimpleDebugPrinter.cpp AdapterSerial.cpp\ +Wire.cpp + +# IPAddress.cpp + +MCU=atmega328p +# note, for this bluetooth library, it is too bulky, probably partly because it +# uses an unnecessarily large amount of underlying Arduino furniture like Stream +# and Print, as a result we have to compile with -Os or it won't fit +CPP_OPTS=-std=c++11 -ffunction-sections -Os +AR_OPTS=rcs + + +# derived variables +CPP_OBJS=$(CPP_FILES:.cpp=.o) +GCC_PLUS_OPTS=-mmcu=$(MCU) $(CPP_OPTS) -B"$(AVR_BINUTILS_DIR)" -iquote . +AR="$(AVR_BINUTILS_DIR)/avr-ar" $(AR_OPTS) +GCC_PLUS="$(AVR_GCC_BIN_DIR)/avr-gcc" $(GCC_PLUS_OPTS) +BUILT_PRODUCT=lib$(MODULE_NAME).a +MODULE_NAME_DIR=$(UNSAFE_MODULES_INSTALL_DIR)/$(MODULE_NAME) + +BUILT_PRODUCT_INSTALL=$(BUILT_PRODUCT:=-install) +COPIED_FILES_U_INSTALL=$(COPIED_FILES_U:=-install) +COPIED_FILES_S_INSTALL=$(COPIED_FILES_S:=-install) + +# subroutine + +define includeFileReference + if ! grep -s $(1) $(2); then \ + echo "\n$(1)\c" >> $(2); \ + fi +endef + + +#targets + +.PHONY: all clean install $(BUILT_PRODUCT_INSTALL) $(COPIED_FILES_U_INSTALL) $(COPIED_FILES_S_INSTALL) + +all: $(BUILT_PRODUCT) + +$(BUILT_PRODUCT): $(CPP_OBJS) + $(AR) -o $@ $^ + +%.o: %.cpp + $(GCC_PLUS) -I . -DF_CPU=16000000UL -c -o $@ $(@:.o=.cpp) + +clean: + rm -rf *.o *.a + +# install section + +$(CATALOG_FILE): + if [ ! -e "$@" ]; then cp $(LEGACY_CATALOG_FILE) "$@";fi + +$(MODULE_NAME_DIR): + if [ ! -d "$@" ]; then mkdir -p "$@";fi + +$(BUILT_PRODUCT_INSTALL): $(BUILT_PRODUCT) $(MODULE_NAME_DIR) $(CATALOG_FILE) + cp -a $(@:-install=) $(UNSAFE_MODULES_INSTALL_DIR)/$(MODULE_NAME)/ + $(call includeFileReference,unsafe_modules/$(MODULE_NAME)/$(@:-install=),$(CATALOG_FILE)) + +$(COPIED_FILES_U_INSTALL): $(COPIED_FILES_U) $(MODULE_NAME_DIR) $(CATALOG_FILE) + cp -a $(@:-install=) $(UNSAFE_MODULES_INSTALL_DIR)/$(MODULE_NAME)/ + $(call includeFileReference,unsafe_modules/$(MODULE_NAME)/$(@:-install=),$(CATALOG_FILE)) + +$(COPIED_FILES_S_INSTALL): $(COPIED_FILES_S) $(CATALOG_FILE) + cp -a $(@:-install=) $(SAFE_FILES_INSTALL_DIR)/ + $(call includeFileReference,$(@:-install=),$(CATALOG_FILE)) + +install: $(BUILT_PRODUCT_INSTALL) $(COPIED_FILES_U_INSTALL) $(COPIED_FILES_S_INSTALL) diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Print.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/Print.cpp new file mode 100755 index 0000000..86029c5 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Print.cpp @@ -0,0 +1,266 @@ +/* + Print.cpp - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. 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 + + Modified 23 November 2006 by David A. Mellis + Modified 03 August 2015 by Chuck Todd + */ + +#include +#include +#include +#include +// #include "Arduino.h" + +#include "Print.h" + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + while (size--) { + if (write(*buffer++)) n++; + else break; + } + return n; +} + +size_t Print::print(const __FlashStringHelper *ifsh) +{ + PGM_P p = reinterpret_cast(ifsh); + size_t n = 0; + while (1) { + unsigned char c = pgm_read_byte(p++); + if (c == 0) break; + if (write(c)) n++; + else break; + } + return n; +} + +// size_t Print::print(const String &s) +// { +// return write(s.c_str(), s.length()); +// } + +size_t Print::print(const char str[]) +{ + return write(str); +} + +size_t Print::print(char c) +{ + return write(c); +} + +size_t Print::print(unsigned char b, int base) +{ + return print((unsigned long) b, base); +} + +size_t Print::print(int n, int base) +{ + return print((long) n, base); +} + +size_t Print::print(unsigned int n, int base) +{ + return print((unsigned long) n, base); +} + +size_t Print::print(long n, int base) +{ + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); + } +} + +size_t Print::print(unsigned long n, int base) +{ + if (base == 0) return write(n); + else return printNumber(n, base); +} + +size_t Print::print(double n, int digits) +{ + return printFloat(n, digits); +} + +size_t Print::println(const __FlashStringHelper *ifsh) +{ + size_t n = print(ifsh); + n += println(); + return n; +} + +// size_t Print::print(const Printable& x) +// { +// return x.printTo(*this); +// } + +size_t Print::println(void) +{ + return write("\r\n"); +} + +// size_t Print::println(const String &s) +// { +// size_t n = print(s); +// n += println(); +// return n; +// } + +size_t Print::println(const char c[]) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(char c) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(unsigned char b, int base) +{ + size_t n = print(b, base); + n += println(); + return n; +} + +size_t Print::println(int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(double num, int digits) +{ + size_t n = print(num, digits); + n += println(); + return n; +} + +// size_t Print::println(const Printable& x) +// { +// size_t n = print(x); +// n += println(); +// return n; +// } + +// Private Methods ///////////////////////////////////////////////////////////// + +size_t Print::printNumber(unsigned long n, uint8_t base) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + char c = n % base; + n /= base; + + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); +} + +size_t Print::printFloat(double number, uint8_t digits) +{ + size_t n = 0; + + if (isnan(number)) return print("nan"); + if (isinf(number)) return print("inf"); + if (number > 4294967040.0) return print ("ovf"); // constant determined empirically + if (number <-4294967040.0) return print ("ovf"); // constant determined empirically + + // Handle negative numbers + if (number < 0.0) + { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + n += print('.'); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + unsigned int toPrint = (unsigned int)(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; +} diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Print.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Print.h new file mode 100755 index 0000000..192d1e1 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Print.h @@ -0,0 +1,97 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. 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 +*/ + +#ifndef Print_h +#define Print_h + +#include +#include // for size_t +#include +#include + +class __FlashStringHelper; + +// #include "WString.h" +// #include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar +#undef BIN +#endif +#define BIN 2 + +class Print +{ + private: + int write_error; + size_t printNumber(unsigned long, uint8_t); + size_t printFloat(double, uint8_t); + protected: + void setWriteError(int err = 1) { write_error = err; } + public: + Print() : write_error(0) {} + + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + + virtual size_t write(uint8_t) = 0; + size_t write(const char *str) { + if (str == NULL) return 0; + return write((const uint8_t *)str, strlen(str)); + } + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + // default to zero, meaning "a single write may block" + // should be overriden by subclasses with buffering + virtual int availableForWrite() { return 0; } + + size_t print(const __FlashStringHelper *); + // size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + // size_t print(const Printable&); + + size_t println(const __FlashStringHelper *); + // size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + // size_t println(const Printable&); + size_t println(void); + + virtual void flush() { /* Empty implementation for backward compatibility */ } +}; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Printable.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Printable.h new file mode 100644 index 0000000..2a1b2e9 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Printable.h @@ -0,0 +1,40 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef Printable_h +#define Printable_h + +#include + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ + +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Server.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Server.h new file mode 100644 index 0000000..69e3e39 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Server.h @@ -0,0 +1,30 @@ +/* + Server.h - Base class that provides Server + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef server_h +#define server_h + +#include "Print.h" + +class Server : public Print { +public: + virtual void begin() =0; +}; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.cpp new file mode 100644 index 0000000..7dc98ba --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.cpp @@ -0,0 +1,12 @@ +#include "SimpleDebugPrinter.h" + +size_t SimpleDebugPrinter::write(const uint8_t *buf, size_t size) +{ + _sendBuffer(size, (const char *)buf, false); + return size; +} + +size_t SimpleDebugPrinter::write(uint8_t byte) { + _sendByte(byte); + return 1; +} \ No newline at end of file diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.h b/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.h new file mode 100644 index 0000000..b4cbaa3 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/SimpleDebugPrinter.h @@ -0,0 +1,13 @@ +#include +#include "Print.h" + +#define _Bool bool +#include "shims.h" + +class SimpleDebugPrinter : public Print +{ +public: + SimpleDebugPrinter() { } + virtual size_t write(const uint8_t *buffer, size_t size); + virtual size_t write(uint8_t); +}; \ No newline at end of file diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.cpp new file mode 100755 index 0000000..db92956 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.cpp @@ -0,0 +1,319 @@ +/* + Stream.cpp - adds parsing methods to Stream class + Copyright (c) 2008 David A. Mellis. 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 + + Created July 2011 + parsing functions based on TextFinder library by Michael Margolis + + findMulti/findUntil routines written by Jim Leonard/Xuth + */ + +// #include "Arduino.h" +#include "Stream.h" +#include "shims.h" + +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait + +// protected method to read stream with timeout +int Stream::timedRead() +{ + int c; + _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// protected method to peek stream with timeout +int Stream::timedPeek() +{ + int c; + _startMillis = millis(); + do { + c = peek(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// returns peek of the next digit in the stream or -1 if timeout +// discards non-numeric characters +int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal) +{ + int c; + while (1) { + c = timedPeek(); + + if( c < 0 || + c == '-' || + (c >= '0' && c <= '9') || + (detectDecimal && c == '.')) return c; + + switch( lookahead ){ + case SKIP_NONE: return -1; // Fail code. + case SKIP_WHITESPACE: + switch( c ){ + case ' ': + case '\t': + case '\r': + case '\n': break; + default: return -1; // Fail code. + } + case SKIP_ALL: + break; + } + read(); // discard non-numeric + } +} + +// Public Methods +////////////////////////////////////////////////////////////// + +void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait +{ + _timeout = timeout; +} + + // find returns true if the target string is found +bool Stream::find(char *target) +{ + return findUntil(target, strlen(target), NULL, 0); +} + +// reads data from the stream until the target string of given length is found +// returns true if target string is found, false if timed out +bool Stream::find(char *target, size_t length) +{ + return findUntil(target, length, NULL, 0); +} + +// as find but search ends if the terminator string is found +bool Stream::findUntil(char *target, char *terminator) +{ + return findUntil(target, strlen(target), terminator, strlen(terminator)); +} + +// reads data from the stream until the target string of the given length is found +// search terminated if the terminator string is found +// returns true if target string is found, false if terminated or timed out +bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) +{ + if (terminator == NULL) { + MultiTarget t[1] = {{target, targetLen, 0}}; + return findMulti(t, 1) == 0 ? true : false; + } else { + MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; + return findMulti(t, 2) == 0 ? true : false; + } +} + +// returns the first valid (long) integer value from the current position. +// lookahead determines how parseInt looks ahead in the stream. +// See LookaheadMode enumeration at the top of the file. +// Lookahead is terminated by the first character that is not a valid part of an integer. +// Once parsing commences, 'ignore' will be skipped in the stream. +long Stream::parseInt(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + long value = 0; + int c; + + c = peekNextDigit(lookahead, false); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore this character + else if(c == '-') + isNegative = true; + else if(c >= '0' && c <= '9') // is c a digit? + value = value * 10 + c - '0'; + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == ignore ); + + if(isNegative) + value = -value; + return value; +} + +// as parseInt but returns a floating point value +float Stream::parseFloat(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + bool isFraction = false; + long value = 0; + int c; + float fraction = 1.0; + + c = peekNextDigit(lookahead, true); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore + else if(c == '-') + isNegative = true; + else if (c == '.') + isFraction = true; + else if(c >= '0' && c <= '9') { // is c a digit? + value = value * 10 + c - '0'; + if(isFraction) + fraction *= 0.1; + } + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore ); + + if(isNegative) + value = -value; + if(isFraction) + return value * fraction; + else + return value; +} + +// read characters from stream into buffer +// terminates if length characters have been read, or timeout (see setTimeout) +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +// +size_t Stream::readBytes(char *buffer, size_t length) +{ + size_t count = 0; + while (count < length) { + int c = timedRead(); + if (c < 0) break; + *buffer++ = (char)c; + count++; + } + return count; +} + + +// as readBytes with terminator character +// terminates if length characters have been read, timeout, or if the terminator character detected +// returns the number of characters placed in the buffer (0 means no valid data found) + +size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) +{ + size_t index = 0; + while (index < length) { + int c = timedRead(); + if (c < 0 || c == terminator) break; + *buffer++ = (char)c; + index++; + } + return index; // return number of characters, not including null terminator +} + +// String Stream::readString() +// { +// String ret; +// int c = timedRead(); +// while (c >= 0) +// { +// ret += (char)c; +// c = timedRead(); +// } +// return ret; +// } + +// String Stream::readStringUntil(char terminator) +// { +// String ret; +// int c = timedRead(); +// while (c >= 0 && c != terminator) +// { +// ret += (char)c; +// c = timedRead(); +// } +// return ret; +// } + +int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { + // any zero length target string automatically matches and would make + // a mess of the rest of the algorithm. + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + if (t->len <= 0) + return t - targets; + } + + while (1) { + int c = timedRead(); + if (c < 0) + return -1; + + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + // the simple case is if we match, deal with that first. + if (c == t->str[t->index]) { + if (++t->index == t->len) + return t - targets; + else + continue; + } + + // if not we need to walk back and see if we could have matched further + // down the stream (ie '1112' doesn't match the first position in '11112' + // but it will match the second position so we can't just reset the current + // index to 0 when we find a mismatch. + if (t->index == 0) + continue; + + int origIndex = t->index; + do { + --t->index; + // first check if current char works against the new current index + if (c != t->str[t->index]) + continue; + + // if it's the only char then we're good, nothing more to check + if (t->index == 0) { + t->index++; + break; + } + + // otherwise we need to check the rest of the found string + int diff = origIndex - t->index; + size_t i; + for (i = 0; i < t->index; ++i) { + if (t->str[i] != t->str[i + diff]) + break; + } + + // if we successfully got through the previous loop then our current + // index is good. + if (i == t->index) { + t->index++; + break; + } + + // otherwise we just try the next index + } while (t->index); + } + } + // unreachable + return -1; +} diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.h new file mode 100755 index 0000000..102a284 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Stream.h @@ -0,0 +1,134 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 David A. Mellis. 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 + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef Stream_h +#define Stream_h + +// compatibility layer +#define _Bool bool + +#include + +#include "shims.h" +#include "Print.h" + +// compatability macros for testing +/* +#define getInt() parseInt() +#define getInt(ignore) parseInt(ignore) +#define getFloat() parseFloat() +#define getFloat(ignore) parseFloat(ignore) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +// This enumeration provides the lookahead options for parseInt(), parseFloat() +// The rules set out here are used until either the first valid character is found +// or a time out occurs due to lack of input. +enum LookaheadMode{ + SKIP_ALL, // All invalid characters are ignored. + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. +}; + +#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field + +class Stream : public Print +{ + protected: + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read + unsigned long _startMillis; // used for timeout measurement + int timedRead(); // read stream with timeout + int timedPeek(); // peek stream with timeout + int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + unsigned long getTimeout(void) { return _timeout; } + + bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } + // returns true if target string is found, false if timed out + + bool find(char target) { return find (&target, 1); } + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } + + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // returns the first valid (long) integer value from the current position. + // lookahead determines how parseInt looks ahead in the stream. + // See LookaheadMode enumeration at the top of the file. + // Lookahead is terminated by the first character that is not a valid part of an integer. + // Once parsing commences, 'ignore' will be skipped in the stream. + + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // float version of parseInt + + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } + // terminates if length characters have been read or timeout (see setTimeout) + // returns the number of characters placed in the buffer (0 means no valid data found) + + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } + // terminates if length characters have been read, timeout, or if the terminator character detected + // returns the number of characters placed in the buffer (0 means no valid data found) + + // // Arduino String functions to be added here + // String readString(); + // String readStringUntil(char terminator); + + protected: + long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } + float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } + // These overload exists for compatibility with any class that has derived + // Stream and used parseFloat/Int with a custom ignore character. To keep + // the public API simple, these overload remains protected. + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); +}; + +#undef NO_IGNORE_CHAR +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Udp.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Udp.h new file mode 100644 index 0000000..25fa15d --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Udp.h @@ -0,0 +1,89 @@ +/* + * Udp.cpp: Library to send/receive UDP packets. + * + * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) + * 1) UDP does not guarantee the order in which assembled UDP packets are received. This + * might not happen often in practice, but in larger network topologies, a UDP + * packet can be received out of sequence. + * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being + * aware of it. Again, this may not be a concern in practice on small local networks. + * For more information, see http://www.cafeaulait.org/course/week12/35.html + * + * MIT License: + * Copyright (c) 2008 Bjoern Hartmann + * 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. + * + * bjoern@cs.stanford.edu 12/30/2008 + */ + +#ifndef udp_h +#define udp_h + +#include "Stream.h" +#include "IPAddress.h" + +class UDP : public Stream { + +public: + virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure + virtual void stop() =0; // Finish with the UDP socket + + // Sending UDP packets + + // Start building up a packet to send to the remote host specific in ip and port + // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port + virtual int beginPacket(IPAddress ip, uint16_t port) =0; + // Start building up a packet to send to the remote host specific in host and port + // Returns 1 if successful, 0 if there was a problem resolving the hostname or port + virtual int beginPacket(const char *host, uint16_t port) =0; + // Finish off this packet and send it + // Returns 1 if the packet was sent successfully, 0 if there was an error + virtual int endPacket() =0; + // Write a single byte into the packet + virtual size_t write(uint8_t) =0; + // Write size bytes from buffer into the packet + virtual size_t write(const uint8_t *buffer, size_t size) =0; + + // Start processing the next available incoming packet + // Returns the size of the packet in bytes, or 0 if no packets are available + virtual int parsePacket() =0; + // Number of bytes remaining in the current packet + virtual int available() =0; + // Read a single byte from the current packet + virtual int read() =0; + // Read up to len bytes from the current packet and place them into buffer + // Returns the number of bytes read, or 0 if none are available + virtual int read(unsigned char* buffer, size_t len) =0; + // Read up to len characters from the current packet and place them into buffer + // Returns the number of characters read, or 0 if none are available + virtual int read(char* buffer, size_t len) =0; + // Return the next byte from the current packet without moving on to the next byte + virtual int peek() =0; + virtual void flush() =0; // Finish reading the current packet + + // Return the IP address of the host who sent the current incoming packet + virtual IPAddress remoteIP() =0; + // Return the port of the host who sent the current incoming packet + virtual uint16_t remotePort() =0; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp new file mode 100644 index 0000000..1f93c56 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp @@ -0,0 +1,278 @@ +/* + TwoWire.cpp - TWI/I2C library for Wiring & Arduino + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts + Modified 2017 by Chuck Todd (ctodd@cableone.net) to correct Unconfigured Slave Mode reboot + Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts +*/ + +extern "C" { + #include + #include + #include + #include "utility/twi.h" +} + +#include "Wire.h" +#include "shims.h" + +#ifndef TWI_FREQ +#define TWI_FREQ 100000L +#endif + +#ifndef TWI_BUFFER_LENGTH +#define TWI_BUFFER_LENGTH 32 +#endif + +uint8_t I2CAdapter::rxBuffer[BUFFER_LENGTH]; +uint8_t I2CAdapter::rxBufferIndex = 0; +uint8_t I2CAdapter::rxBufferLength = 0; + +uint8_t I2CAdapter::txAddress = 0; +uint8_t I2CAdapter::txBuffer[BUFFER_LENGTH]; +uint8_t I2CAdapter::txBufferIndex = 0; +uint8_t I2CAdapter::txBufferLength = 0; + +I2CAdapter::I2CAdapter(uint8_t premultiplier, bool activatePullups) : premultiplier(premultiplier), activatePullups(activatePullups) +{ +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void I2CAdapter::begin(void) +{ + rxBufferIndex = 0; + rxBufferLength = 0; + _i2cinit(((F_CPU / TWI_FREQ) - 16) / 2, premultiplier & 3, activatePullups); +} + +void I2CAdapter::begin(uint8_t address) +{ + begin(); + slaveAddress = address; +} + +void I2CAdapter::begin(int address) +{ + begin((uint8_t)address); +} + +void I2CAdapter::end(void) +{ + _i2cshutdown(); +} + +void I2CAdapter::setClock(uint32_t clock) +{ + _i2cinit(((F_CPU / clock) - 16) / 2, premultiplier & 3, activatePullups); +} + + +void I2CAdapter::setWireTimeout(uint32_t timeout, bool reset_with_timeout){ + // noop - not supported +} + +bool I2CAdapter::getWireTimeoutFlag(void){ + return false; // not supported +} + +void I2CAdapter::clearWireTimeoutFlag(void){ + // noop - not supported +} + +uint8_t I2CAdapter::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop) +{ + if (isize > 0) { + // send internal address; this mode allows sending a repeated start to access + // some devices' internal registers. This function is executed by the hardware + // TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers) + + beginTransmission(address); + + // the maximum size of internal address is 3 bytes + if (isize > 3){ + isize = 3; + } + + _i2cwrite((address&0x7f)<<1); + // write internal register address - most significant byte first + while (isize-- > 0) + _i2cwrite((uint8_t)(iaddress >> (isize*8))); + } + + // clamp to buffer length + if(quantity > BUFFER_LENGTH){ + quantity = BUFFER_LENGTH; + } + // perform blocking read into buffer + uint8_t read = 0; + _i2cReadBuffer(address, quantity, rxBuffer); + + // set rx buffer iterator vars + rxBufferIndex = 0; + rxBufferLength = read; + + return read; +} + +uint8_t I2CAdapter::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop); +} + +uint8_t I2CAdapter::requestFrom(uint8_t address, uint8_t quantity) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true); +} + +uint8_t I2CAdapter::requestFrom(int address, int quantity) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true); +} + +uint8_t I2CAdapter::requestFrom(int address, int quantity, int sendStop) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop); +} + +void I2CAdapter::beginTransmission(uint8_t address) +{ + // set address of targeted slave + txAddress = address; +} + +void I2CAdapter::beginTransmission(int address) +{ + beginTransmission((uint8_t)address); +} + +// Arduino lib buffers then transmits, recreating this with AVR. +// Always sends stop, limitation of AVR library +uint8_t I2CAdapter::endTransmission(uint8_t sendStop) +{ + _i2cWriteBuffer(txAddress, txBufferLength, (const char*)txBuffer, false); + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; + + return 0; // return codes not supported +} + +uint8_t I2CAdapter::endTransmission(void) +{ + return endTransmission(true); +} + +size_t I2CAdapter::write(uint8_t data) +{ + // only support master transmitter mode + // don't bother if buffer is full + if(txBufferLength >= BUFFER_LENGTH){ + setWriteError(); + return 0; + } + // put byte in tx buffer + txBuffer[txBufferIndex] = data; + ++txBufferIndex; + // update amount in buffer + txBufferLength = txBufferIndex; + return 1; +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +size_t I2CAdapter::write(const uint8_t *data, size_t quantity) +{ + // in master transmitter mode + for(size_t i = 0; i < quantity; ++i){ + write(data[i]); + } + return quantity; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int I2CAdapter::available(void) +{ + return rxBufferLength - rxBufferIndex; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int I2CAdapter::read(void) +{ + int value = -1; + + // get each successive byte on each call + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + ++rxBufferIndex; + } + + return value; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int I2CAdapter::peek(void) +{ + int value = -1; + + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + } + + return value; +} + +void I2CAdapter::flush(void) +{ + // XXX: to be implemented. +} + +// // behind the scenes function that is called when data is received +// void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) +// { +// // noop - slave not yet supported +// } + +// // behind the scenes function that is called when data is requested +// void TwoWire::onRequestService(void) +// { +// // noop - slave not yet supported +// } + +// sets function called on slave write +void I2CAdapter::onReceive( void (*function)(int) ) +{ + // noop - slave not yet supported +} + +// sets function called on slave read +void I2CAdapter::onRequest( void (*function)(void) ) +{ + // noop - slave not yet supported +} + +// Preinstantiate Objects ////////////////////////////////////////////////////// + +I2CAdapter Wire = I2CAdapter(); + diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.h new file mode 100644 index 0000000..fc2d877 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.h @@ -0,0 +1,89 @@ +/* + TwoWire.h - TWI/I2C library for Arduino & Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + + Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts + Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts +*/ + +#ifndef TwoWire_h +#define TwoWire_h + +#include +#include "Stream.h" + +#define BUFFER_LENGTH 32 + +// WIRE_HAS_END means Wire has end() +#define WIRE_HAS_END 1 + +class I2CAdapter : public Stream +{ + private: + static uint8_t rxBuffer[]; + static uint8_t rxBufferIndex; + static uint8_t rxBufferLength; + + bool activatePullups; + uint8_t premultiplier; + uint8_t slaveAddress; + + // static to mirror Arduino standard semantics + // for most people it will never matter + static uint8_t txAddress; + static uint8_t txBuffer[]; + static uint8_t txBufferIndex; + static uint8_t txBufferLength; + public: + I2CAdapter(uint8_t premultiplier = 0, bool activatePullups = false); + void begin(); + void begin(uint8_t); + void begin(int); + void end(); + void setClock(uint32_t); + void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false); + bool getWireTimeoutFlag(void); + void clearWireTimeoutFlag(void); + void beginTransmission(uint8_t); + void beginTransmission(int); + uint8_t endTransmission(void); + uint8_t endTransmission(uint8_t); + uint8_t requestFrom(uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); + uint8_t requestFrom(int, int); + uint8_t requestFrom(int, int, int); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *, size_t); + virtual int available(void); + virtual int read(void); + virtual int peek(void); + virtual void flush(void); + void onReceive( void (*)(int) ); + void onRequest( void (*)(void) ); + + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; +}; + +extern I2CAdapter Wire; + +#endif + diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/abi.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/abi.cpp new file mode 100644 index 0000000..b018abc --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/abi.cpp @@ -0,0 +1,45 @@ +/* + Copyright (c) 2014 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 +*/ + +#include + +// these errors are a noop, it seems less destructive than a hard to trace hang +extern "C" void __cxa_pure_virtual(void); +extern "C" void __cxa_deleted_virtual(void); + +extern "C" int __cxa_guard_acquire(void); +extern "C" void __cxa_guard_release(void); + +void __cxa_pure_virtual(void) { + // We might want to write some diagnostics to uart in this case + //std::terminate(); +} + +void __cxa_deleted_virtual(void) { + // We might want to write some diagnostics to uart in this case + //std::terminate(); +} + +//int __cxxabiv1::__cxa_guard_acquire(uint64_t* guard_object) +int __cxa_guard_acquire(void) { + return 1; +} + +void __cxa_guard_release(void) { + +} diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/avrTypes.h b/v4.0/contributed unsafe modules/ArduinoAdapter/avrTypes.h new file mode 100644 index 0000000..4fa65de --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/avrTypes.h @@ -0,0 +1,98 @@ +#define false 0 +#define true 1 +#define int32 long +#define uint32 unsigned long +#define iLEDColor uint32 +#define iLEDLevel unsigned char + +// note, use short, short int, unsigned short, unsigned short int for 16 bit ints, they are the same on both platforms +// use int32 for 32 bit int +// use long long int or unsigned long long int for 64 bit ints + + +#ifdef __clang_version__ +// Clang (we are parsing header files as bridging header) +#define __nonnull _Nonnull +#define __nullable _Nullable +#else +// GCC +#define __nonnull +#define __nullable +#endif + +typedef void (* __nonnull adcCallback)(unsigned short value); +typedef void (* __nonnull temperatureCallback)(short centigrade); +typedef void (* __nonnull externalInterruptCallback)(); +typedef void (* __nullable optionalInterruptCallback)(); +typedef void (* __nonnull asyncCallback)(); +typedef void (* __nonnull asyncCallbackWithContext)(unsigned short context); +typedef void (* __nonnull slaveBufferCallback)(const unsigned char * __nonnull buffer, unsigned char length); +typedef const unsigned char * __nonnull (* __nonnull slaveGetBufferToSendCallback)(unsigned char * __nonnull bufferLength); + +typedef void (* __nonnull slaveRegisterReceiveCallback)(unsigned char register, unsigned char value); +typedef unsigned char (* __nonnull slaveRegisterSendCallback)(unsigned char register); + +// define the most used constants using #defines, so that they will be imported +// as true constants into swift and lowered into MC as constants +#define HIGH (_Bool)1 +#define LOW (_Bool)0 +#define OUTPUT (_Bool)1 +#define INPUT (_Bool)0 +#define WHILE_LOW (unsigned char)0 +#define CHANGING_EDGE (unsigned char)1 +#define FALLING_EDGE (unsigned char)2 +#define RISING_EDGE (unsigned char)3 + +#define D0 (unsigned char)0 +#define D1 (unsigned char)1 +#define D2 (unsigned char)2 +#define D3 (unsigned char)3 +#define D4 (unsigned char)4 +#define D5 (unsigned char)5 +#define D6 (unsigned char)6 +#define D7 (unsigned char)7 +#define D8 (unsigned char)8 +#define D9 (unsigned char)9 +#define D10 (unsigned char)10 +#define D11 (unsigned char)11 +#define D12 (unsigned char)12 +#define D13 (unsigned char)13 + +#define D14 (unsigned char)14 // this is pin A0 on an UNO, used as a digital input/output +#define D15 (unsigned char)15 // this is pin A1 on an UNO, used as a digital input/output +#define D16 (unsigned char)16 // this is pin A2 on an UNO, used as a digital input/output +#define D17 (unsigned char)17 // this is pin A3 on an UNO, used as a digital input/output +#define D18 (unsigned char)18 // this is pin A4 on an UNO, used as a digital input/output +#define D19 (unsigned char)19 // this is pin A5 on an UNO, used as a digital input/output + +#define A0 (unsigned char)128 // this is pin A0 for use in ADC functions (=128+0) +#define A1 (unsigned char)129 // this is pin A0 for use in ADC functions (=128+1) +#define A2 (unsigned char)130 // this is pin A0 for use in ADC functions (=128+2) +#define A3 (unsigned char)131 // this is pin A0 for use in ADC functions (=128+3) +#define A4 (unsigned char)132 // this is pin A0 for use in ADC functions (=128+4) +#define A5 (unsigned char)133 // this is pin A0 for use in ADC functions (=128+5) + +// for less frequently used constants, use defined globals + +#define minAnalogPin (unsigned char)0 +#define maxAnalogPin (unsigned char)5 + +#define minAnalogValue (unsigned short)0 +#define maxAnalogValue (unsigned short)1023 + +extern iLEDColor iLEDWhite; +extern iLEDColor iLEDRed; +extern iLEDColor iLEDGreen; +extern iLEDColor iLEDBlue; +extern iLEDColor iLEDOff; + +extern const unsigned char WATCHDOG_TIMEOUT_15MS; +extern const unsigned char WATCHDOG_TIMEOUT_30MS; +extern const unsigned char WATCHDOG_TIMEOUT_60MS; +extern const unsigned char WATCHDOG_TIMEOUT_120MS; +extern const unsigned char WATCHDOG_TIMEOUT_250MS; +extern const unsigned char WATCHDOG_TIMEOUT_500MS; +extern const unsigned char WATCHDOG_TIMEOUT_1S; +extern const unsigned char WATCHDOG_TIMEOUT_2S; +extern const unsigned char WATCHDOG_TIMEOUT_4S; +extern const unsigned char WATCHDOG_TIMEOUT_8S; diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/libArduino.a b/v4.0/contributed unsafe modules/ArduinoAdapter/libArduino.a new file mode 100644 index 0000000000000000000000000000000000000000..3bd408e9c33e829ab190906b32b61074b45c6c07 GIT binary patch literal 34380 zcmd^o3w%`7wf;Uc1crw^1`rg4fl@>alS~MZSgJt@qM}B@M=dr4GJ(;&Ou|FkY9;|e z1PPD`S}WGuT3esBy=twu#J0EUt+m?Qhqu}~MQ=-6DxqSlw^aV$w;wZm_RJ(m+uQ!` zf5D!!*4k@-`~5g)pM5fEZdKEYRc8(=2&Ge@B`V(j4TXwIN{W=pvOz+KN+AZn^1c=+dW1L@7hNaBKbNb#>Y~|;TbiO(b)_w_x@h&K(W>h5HNsU_ zM5EWHsUtHRp}Hu#u4R5~WvrzgaL{AyU{JZCL{FhSe?Qb(Tj- zV^vdg^t}3v&?_vXNTIY^P}@+|t6eGbP<{6GEz#!kiWS$`0L4k4%j#QVwdE_){Yz_N z_0<=yu5F2xS6sBXG7^#elB%Ya(U$U<1tbj>1P~S%R@$+odBV>MSIh{UL>V2;W>qxB z>RTdns@7D+YO9vlMi(?RUC|V4iK+ru-m_|IS2wRp)2x*?6&I|M>Ck3IDT`3+8i`+C zaY3ZCvT{LfRr4xTc&vWq!f0({v?-N>S;j6itfiSFt!nG-q$G8H#$j1z!?bjfh>Yl> z)pg6GP33i~GSXQq>n1IWF-w(&<;xm;yi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N?>g8?$vlqJLz56b8TUWc*bcc=@mk`SD-LCwwd)1 ix^%`n4UKrof$w`5GU-Edl4}?G6(bXmF;SX~-TwpBKFPBH literal 0 HcmV?d00001 diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/module.modulemap b/v4.0/contributed unsafe modules/ArduinoAdapter/module.modulemap new file mode 100644 index 0000000..5a532ac --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/module.modulemap @@ -0,0 +1,4 @@ +module Arduino { + header "Arduino.h" + link "Arduino" +} \ No newline at end of file diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h b/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h new file mode 100644 index 0000000..b6b6e96 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h @@ -0,0 +1,138 @@ +// AVR library primitives/internals, not for use from Swift, use the Swift wrapper +// functions in the main AVR module instead + +#ifndef _SHIMS_H_ +#define _SHIMS_H_ +extern "C" { + +#ifndef uint32 +#define uint32 uint32_t +#endif + +#ifndef millis +#define millis _ticks +#endif + +// nullable annotations are meaningless in GCC +#ifdef __clang_version__ +// Clang (we are parsing header files as bridging header) +#define __nonnull _Nonnull +#define __nullable _Nullable +#else +// GCC +#define __nonnull +#define __nullable +#endif + +#ifndef digitalRead +#define digitalRead _digitalRead +#endif + +#ifndef digitalWrite +#define digitalWrite _digitalWrite +#endif + +#ifndef pinMode +#define pinMode _pinMode +#endif + +#ifndef delayMicroseconds +#define delayMicroseconds _delayUs +#endif + + void _i2cinit(unsigned char speed, unsigned char premultiplier, _Bool activatePullups); + _Bool _i2cstart(); + void _i2cstop(); + _Bool _i2cwrite(unsigned char byte); + unsigned char _i2cread(_Bool sendAck); + + // higher level I2C + void _i2cWriteByte(unsigned char address, unsigned char byte); + void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * buffer, _Bool isString); + const unsigned char * __nullable _i2cReadBuffer(unsigned char address, unsigned char size, unsigned char * __nullable buffer); + void _i2cshutdown(); + + // SPI + // enable hardware SPI and set parameters, note: this takes over pins 11,12,13 and sets pin 10 as output (for use as SS) + // speed is 0 - 3, 0 is fastest, clock speed/4, 1 = clock/16, 2 = clock/64, 3 = clock/128 + void _setupSPIAsMaster(unsigned char speed, unsigned char mode, _Bool lsb); + // not recommended when already running on maximum speed (speed == 0, clock/4) + void _setupSPIDoubleSpeed(_Bool doubleSpeed); + // blocks until transmission and reception complete + unsigned char _sendSPIByteBlocking(unsigned char byte); + // blocks until transmission and reception complete + const char * __nonnull _sendReceiveSPIBufferBlocking(unsigned short maxMsgLen, const char * __nonnull message, _Bool sendMessage, _Bool sendString, _Bool receiveString); + // blocks until reception complete + const char * __nonnull _receiveSPIBufferBlocking(unsigned short maxMsgLen, _Bool receiveString); + // disable hardware SPI, release pins 11,12,13, you should now reset their input/output modes if you want to use them + void __attribute__((weak)) _stopSPI(); + + // utils + uint32 _ticks(); + void _delayMs(unsigned short delay); + void _delayUs(unsigned short delayUs); + + // digital IO + _Bool _digitalRead(unsigned char pin); + void _digitalWrite(unsigned char pin, _Bool value); + void _pinMode(unsigned char pin, _Bool write); + + /* Raw read/write. */ + unsigned char _getPortB(); + unsigned char _getPortC(); + unsigned char _getPortD(); + + void _setPortB(unsigned char value); + void _setPortC(unsigned char value); + void _setPortD(unsigned char value); + + unsigned char _getDDRB(); + unsigned char _getDDRC(); + unsigned char _getDDRD(); + + void _setDDRB(unsigned char value); + void _setDDRC(unsigned char value); + void _setDDRD(unsigned char value); + + //USART + void _sendByte(unsigned char byte); + // send a hard coded string from the PROGMEM (does not use RAM) + void _sendString(unsigned short maxMsgLen, const char * __nonnull message, _Bool addNewline); // must be NULL terminated + // send a buffer from RAM (do not mix these two up!) + void _sendBuffer(unsigned short maxMsgLen, const char * __nonnull buffer, _Bool addNewline); // must be NULL terminated + _Bool _available(); + unsigned char _receiveByte(); + + // SPI + // blocks until transmission and reception complete + unsigned char _sendSPIByteBlocking(unsigned char byte); + // blocks until transmission and reception complete + const char * __nonnull _sendReceiveSPIBufferBlocking( + unsigned short maxMsgLen, + const char * __nonnull message, + _Bool sendMessage, + _Bool sendString, + _Bool receiveString); + + // Memory efficient string buffer handling/building. + // note, only one string buffer can be used at a time + _Bool _stringAddSingleCharacter(unsigned char byte); + _Bool _stringAddWord(unsigned short word, _Bool bigEndian); + void _stringStartNew(); + const char * __nonnull _stringCurrentValue(); + const char _stringCurrentLength(); + const char _stringRemainingLength(); + const char * __nonnull _stringAdd(const char * __nullable message); + const char * __nonnull _stringAddFromProgmem(const char * __nullable message); + const char * __nonnull _emptyStringBuffer(); + + extern const _Bool HIGH; + extern const _Bool LOW; + extern const _Bool OUTPUT; + extern const _Bool INPUT; + extern const unsigned char WHILE_LOW; + extern const unsigned char CHANGING_EDGE; + extern const unsigned char FALLING_EDGE; + extern const unsigned char RISING_EDGE; +} +#endif From ed8a161d3546ce9c9fea26a5350323083d88549c Mon Sep 17 00:00:00 2001 From: Carl Peto Date: Thu, 18 Aug 2022 00:03:48 +0300 Subject: [PATCH 2/3] arduino adapter fixes --- v4.0/contributed libraries/catalogNew.txt | 3 +- .../unsafe_modules/Arduino/Arduino.h | 1 + .../unsafe_modules/Arduino/binary.h | 534 ++++++++++++++++++ .../unsafe_modules/Arduino/shims.h | 9 +- .../ArduinoAdapter/Arduino.h | 1 + .../ArduinoAdapter/Makefile | 2 +- .../ArduinoAdapter/SPI.cpp | 201 +++++++ .../ArduinoAdapter/SPI.h | 324 +++++++++++ .../ArduinoAdapter/Wire.cpp | 2 +- .../ArduinoAdapter/binary.h | 534 ++++++++++++++++++ .../ArduinoAdapter/shims.h | 9 +- 11 files changed, 1613 insertions(+), 7 deletions(-) create mode 100644 v4.0/contributed libraries/unsafe_modules/Arduino/binary.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/SPI.cpp create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/SPI.h create mode 100644 v4.0/contributed unsafe modules/ArduinoAdapter/binary.h diff --git a/v4.0/contributed libraries/catalogNew.txt b/v4.0/contributed libraries/catalogNew.txt index 8219b9d..20fbf75 100644 --- a/v4.0/contributed libraries/catalogNew.txt +++ b/v4.0/contributed libraries/catalogNew.txt @@ -65,4 +65,5 @@ unsafe_modules/Arduino/Printable.h unsafe_modules/Arduino/Udp.h unsafe_modules/Arduino/Client.h unsafe_modules/Arduino/Server.h -unsafe_modules/Arduino/Wire.h \ No newline at end of file +unsafe_modules/Arduino/Wire.h +unsafe_modules/Arduino/binary.h \ No newline at end of file diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h index 83a04d9..19ff7ff 100644 --- a/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h @@ -8,6 +8,7 @@ #include "Stream.h" #include "AdapterSerial.h" #include "Wire.h" +#include "binary.h" // these seem to cause program crashes // #include "IPAddress.h" diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/binary.h b/v4.0/contributed libraries/unsafe_modules/Arduino/binary.h new file mode 100644 index 0000000..aec4c73 --- /dev/null +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/binary.h @@ -0,0 +1,534 @@ +/* + binary.h - Definitions for binary constants + Copyright (c) 2006 David A. Mellis. 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 +*/ + +#ifndef Binary_h +#define Binary_h + +#define B0 0 +#define B00 0 +#define B000 0 +#define B0000 0 +#define B00000 0 +#define B000000 0 +#define B0000000 0 +#define B00000000 0 +#define B1 1 +#define B01 1 +#define B001 1 +#define B0001 1 +#define B00001 1 +#define B000001 1 +#define B0000001 1 +#define B00000001 1 +#define B10 2 +#define B010 2 +#define B0010 2 +#define B00010 2 +#define B000010 2 +#define B0000010 2 +#define B00000010 2 +#define B11 3 +#define B011 3 +#define B0011 3 +#define B00011 3 +#define B000011 3 +#define B0000011 3 +#define B00000011 3 +#define B100 4 +#define B0100 4 +#define B00100 4 +#define B000100 4 +#define B0000100 4 +#define B00000100 4 +#define B101 5 +#define B0101 5 +#define B00101 5 +#define B000101 5 +#define B0000101 5 +#define B00000101 5 +#define B110 6 +#define B0110 6 +#define B00110 6 +#define B000110 6 +#define B0000110 6 +#define B00000110 6 +#define B111 7 +#define B0111 7 +#define B00111 7 +#define B000111 7 +#define B0000111 7 +#define B00000111 7 +#define B1000 8 +#define B01000 8 +#define B001000 8 +#define B0001000 8 +#define B00001000 8 +#define B1001 9 +#define B01001 9 +#define B001001 9 +#define B0001001 9 +#define B00001001 9 +#define B1010 10 +#define B01010 10 +#define B001010 10 +#define B0001010 10 +#define B00001010 10 +#define B1011 11 +#define B01011 11 +#define B001011 11 +#define B0001011 11 +#define B00001011 11 +#define B1100 12 +#define B01100 12 +#define B001100 12 +#define B0001100 12 +#define B00001100 12 +#define B1101 13 +#define B01101 13 +#define B001101 13 +#define B0001101 13 +#define B00001101 13 +#define B1110 14 +#define B01110 14 +#define B001110 14 +#define B0001110 14 +#define B00001110 14 +#define B1111 15 +#define B01111 15 +#define B001111 15 +#define B0001111 15 +#define B00001111 15 +#define B10000 16 +#define B010000 16 +#define B0010000 16 +#define B00010000 16 +#define B10001 17 +#define B010001 17 +#define B0010001 17 +#define B00010001 17 +#define B10010 18 +#define B010010 18 +#define B0010010 18 +#define B00010010 18 +#define B10011 19 +#define B010011 19 +#define B0010011 19 +#define B00010011 19 +#define B10100 20 +#define B010100 20 +#define B0010100 20 +#define B00010100 20 +#define B10101 21 +#define B010101 21 +#define B0010101 21 +#define B00010101 21 +#define B10110 22 +#define B010110 22 +#define B0010110 22 +#define B00010110 22 +#define B10111 23 +#define B010111 23 +#define B0010111 23 +#define B00010111 23 +#define B11000 24 +#define B011000 24 +#define B0011000 24 +#define B00011000 24 +#define B11001 25 +#define B011001 25 +#define B0011001 25 +#define B00011001 25 +#define B11010 26 +#define B011010 26 +#define B0011010 26 +#define B00011010 26 +#define B11011 27 +#define B011011 27 +#define B0011011 27 +#define B00011011 27 +#define B11100 28 +#define B011100 28 +#define B0011100 28 +#define B00011100 28 +#define B11101 29 +#define B011101 29 +#define B0011101 29 +#define B00011101 29 +#define B11110 30 +#define B011110 30 +#define B0011110 30 +#define B00011110 30 +#define B11111 31 +#define B011111 31 +#define B0011111 31 +#define B00011111 31 +#define B100000 32 +#define B0100000 32 +#define B00100000 32 +#define B100001 33 +#define B0100001 33 +#define B00100001 33 +#define B100010 34 +#define B0100010 34 +#define B00100010 34 +#define B100011 35 +#define B0100011 35 +#define B00100011 35 +#define B100100 36 +#define B0100100 36 +#define B00100100 36 +#define B100101 37 +#define B0100101 37 +#define B00100101 37 +#define B100110 38 +#define B0100110 38 +#define B00100110 38 +#define B100111 39 +#define B0100111 39 +#define B00100111 39 +#define B101000 40 +#define B0101000 40 +#define B00101000 40 +#define B101001 41 +#define B0101001 41 +#define B00101001 41 +#define B101010 42 +#define B0101010 42 +#define B00101010 42 +#define B101011 43 +#define B0101011 43 +#define B00101011 43 +#define B101100 44 +#define B0101100 44 +#define B00101100 44 +#define B101101 45 +#define B0101101 45 +#define B00101101 45 +#define B101110 46 +#define B0101110 46 +#define B00101110 46 +#define B101111 47 +#define B0101111 47 +#define B00101111 47 +#define B110000 48 +#define B0110000 48 +#define B00110000 48 +#define B110001 49 +#define B0110001 49 +#define B00110001 49 +#define B110010 50 +#define B0110010 50 +#define B00110010 50 +#define B110011 51 +#define B0110011 51 +#define B00110011 51 +#define B110100 52 +#define B0110100 52 +#define B00110100 52 +#define B110101 53 +#define B0110101 53 +#define B00110101 53 +#define B110110 54 +#define B0110110 54 +#define B00110110 54 +#define B110111 55 +#define B0110111 55 +#define B00110111 55 +#define B111000 56 +#define B0111000 56 +#define B00111000 56 +#define B111001 57 +#define B0111001 57 +#define B00111001 57 +#define B111010 58 +#define B0111010 58 +#define B00111010 58 +#define B111011 59 +#define B0111011 59 +#define B00111011 59 +#define B111100 60 +#define B0111100 60 +#define B00111100 60 +#define B111101 61 +#define B0111101 61 +#define B00111101 61 +#define B111110 62 +#define B0111110 62 +#define B00111110 62 +#define B111111 63 +#define B0111111 63 +#define B00111111 63 +#define B1000000 64 +#define B01000000 64 +#define B1000001 65 +#define B01000001 65 +#define B1000010 66 +#define B01000010 66 +#define B1000011 67 +#define B01000011 67 +#define B1000100 68 +#define B01000100 68 +#define B1000101 69 +#define B01000101 69 +#define B1000110 70 +#define B01000110 70 +#define B1000111 71 +#define B01000111 71 +#define B1001000 72 +#define B01001000 72 +#define B1001001 73 +#define B01001001 73 +#define B1001010 74 +#define B01001010 74 +#define B1001011 75 +#define B01001011 75 +#define B1001100 76 +#define B01001100 76 +#define B1001101 77 +#define B01001101 77 +#define B1001110 78 +#define B01001110 78 +#define B1001111 79 +#define B01001111 79 +#define B1010000 80 +#define B01010000 80 +#define B1010001 81 +#define B01010001 81 +#define B1010010 82 +#define B01010010 82 +#define B1010011 83 +#define B01010011 83 +#define B1010100 84 +#define B01010100 84 +#define B1010101 85 +#define B01010101 85 +#define B1010110 86 +#define B01010110 86 +#define B1010111 87 +#define B01010111 87 +#define B1011000 88 +#define B01011000 88 +#define B1011001 89 +#define B01011001 89 +#define B1011010 90 +#define B01011010 90 +#define B1011011 91 +#define B01011011 91 +#define B1011100 92 +#define B01011100 92 +#define B1011101 93 +#define B01011101 93 +#define B1011110 94 +#define B01011110 94 +#define B1011111 95 +#define B01011111 95 +#define B1100000 96 +#define B01100000 96 +#define B1100001 97 +#define B01100001 97 +#define B1100010 98 +#define B01100010 98 +#define B1100011 99 +#define B01100011 99 +#define B1100100 100 +#define B01100100 100 +#define B1100101 101 +#define B01100101 101 +#define B1100110 102 +#define B01100110 102 +#define B1100111 103 +#define B01100111 103 +#define B1101000 104 +#define B01101000 104 +#define B1101001 105 +#define B01101001 105 +#define B1101010 106 +#define B01101010 106 +#define B1101011 107 +#define B01101011 107 +#define B1101100 108 +#define B01101100 108 +#define B1101101 109 +#define B01101101 109 +#define B1101110 110 +#define B01101110 110 +#define B1101111 111 +#define B01101111 111 +#define B1110000 112 +#define B01110000 112 +#define B1110001 113 +#define B01110001 113 +#define B1110010 114 +#define B01110010 114 +#define B1110011 115 +#define B01110011 115 +#define B1110100 116 +#define B01110100 116 +#define B1110101 117 +#define B01110101 117 +#define B1110110 118 +#define B01110110 118 +#define B1110111 119 +#define B01110111 119 +#define B1111000 120 +#define B01111000 120 +#define B1111001 121 +#define B01111001 121 +#define B1111010 122 +#define B01111010 122 +#define B1111011 123 +#define B01111011 123 +#define B1111100 124 +#define B01111100 124 +#define B1111101 125 +#define B01111101 125 +#define B1111110 126 +#define B01111110 126 +#define B1111111 127 +#define B01111111 127 +#define B10000000 128 +#define B10000001 129 +#define B10000010 130 +#define B10000011 131 +#define B10000100 132 +#define B10000101 133 +#define B10000110 134 +#define B10000111 135 +#define B10001000 136 +#define B10001001 137 +#define B10001010 138 +#define B10001011 139 +#define B10001100 140 +#define B10001101 141 +#define B10001110 142 +#define B10001111 143 +#define B10010000 144 +#define B10010001 145 +#define B10010010 146 +#define B10010011 147 +#define B10010100 148 +#define B10010101 149 +#define B10010110 150 +#define B10010111 151 +#define B10011000 152 +#define B10011001 153 +#define B10011010 154 +#define B10011011 155 +#define B10011100 156 +#define B10011101 157 +#define B10011110 158 +#define B10011111 159 +#define B10100000 160 +#define B10100001 161 +#define B10100010 162 +#define B10100011 163 +#define B10100100 164 +#define B10100101 165 +#define B10100110 166 +#define B10100111 167 +#define B10101000 168 +#define B10101001 169 +#define B10101010 170 +#define B10101011 171 +#define B10101100 172 +#define B10101101 173 +#define B10101110 174 +#define B10101111 175 +#define B10110000 176 +#define B10110001 177 +#define B10110010 178 +#define B10110011 179 +#define B10110100 180 +#define B10110101 181 +#define B10110110 182 +#define B10110111 183 +#define B10111000 184 +#define B10111001 185 +#define B10111010 186 +#define B10111011 187 +#define B10111100 188 +#define B10111101 189 +#define B10111110 190 +#define B10111111 191 +#define B11000000 192 +#define B11000001 193 +#define B11000010 194 +#define B11000011 195 +#define B11000100 196 +#define B11000101 197 +#define B11000110 198 +#define B11000111 199 +#define B11001000 200 +#define B11001001 201 +#define B11001010 202 +#define B11001011 203 +#define B11001100 204 +#define B11001101 205 +#define B11001110 206 +#define B11001111 207 +#define B11010000 208 +#define B11010001 209 +#define B11010010 210 +#define B11010011 211 +#define B11010100 212 +#define B11010101 213 +#define B11010110 214 +#define B11010111 215 +#define B11011000 216 +#define B11011001 217 +#define B11011010 218 +#define B11011011 219 +#define B11011100 220 +#define B11011101 221 +#define B11011110 222 +#define B11011111 223 +#define B11100000 224 +#define B11100001 225 +#define B11100010 226 +#define B11100011 227 +#define B11100100 228 +#define B11100101 229 +#define B11100110 230 +#define B11100111 231 +#define B11101000 232 +#define B11101001 233 +#define B11101010 234 +#define B11101011 235 +#define B11101100 236 +#define B11101101 237 +#define B11101110 238 +#define B11101111 239 +#define B11110000 240 +#define B11110001 241 +#define B11110010 242 +#define B11110011 243 +#define B11110100 244 +#define B11110101 245 +#define B11110110 246 +#define B11110111 247 +#define B11111000 248 +#define B11111001 249 +#define B11111010 250 +#define B11111011 251 +#define B11111100 252 +#define B11111101 253 +#define B11111110 254 +#define B11111111 255 + +#endif diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h b/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h index b6b6e96..9b76a53 100644 --- a/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/shims.h @@ -3,7 +3,6 @@ #ifndef _SHIMS_H_ #define _SHIMS_H_ -extern "C" { #ifndef uint32 #define uint32 uint32_t @@ -40,6 +39,12 @@ extern "C" { #define delayMicroseconds _delayUs #endif +#ifndef delay +#define delay _delayMs +#endif + +extern "C" { + void _i2cinit(unsigned char speed, unsigned char premultiplier, _Bool activatePullups); _Bool _i2cstart(); void _i2cstop(); @@ -48,7 +53,7 @@ extern "C" { // higher level I2C void _i2cWriteByte(unsigned char address, unsigned char byte); - void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * buffer, _Bool isString); + void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * __nonnull buffer, _Bool isString); const unsigned char * __nullable _i2cReadBuffer(unsigned char address, unsigned char size, unsigned char * __nullable buffer); void _i2cshutdown(); diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h index 83a04d9..19ff7ff 100644 --- a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h @@ -8,6 +8,7 @@ #include "Stream.h" #include "AdapterSerial.h" #include "Wire.h" +#include "binary.h" // these seem to cause program crashes // #include "IPAddress.h" diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile b/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile index 8a36996..9d22e59 100644 --- a/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Makefile @@ -1,6 +1,6 @@ # outputs COPIED_FILES_U=module.modulemap Arduino.h SimpleDebugPrinter.h DebugPrinter.h Print.h Stream.h shims.h AdapterSerial.h\ -Wire.h +Wire.h binary.h # these seem to crash programs # IPAddress.h Printable.h diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/SPI.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/SPI.cpp new file mode 100644 index 0000000..af14e07 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/SPI.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2010 by Cristian Maglie + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) + * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + */ + +#include "SPI.h" + +SPIClass SPI; + +uint8_t SPIClass::initialized = 0; +uint8_t SPIClass::interruptMode = 0; +uint8_t SPIClass::interruptMask = 0; +uint8_t SPIClass::interruptSave = 0; +#ifdef SPI_TRANSACTION_MISMATCH_LED +uint8_t SPIClass::inTransactionFlag = 0; +#endif + +void SPIClass::begin() +{ + uint8_t sreg = SREG; + noInterrupts(); // Protect from a scheduler and prevent transactionBegin + if (!initialized) { + // Set SS to high so a connected chip will be "deselected" by default + uint8_t port = digitalPinToPort(SS); + uint8_t bit = digitalPinToBitMask(SS); + volatile uint8_t *reg = portModeRegister(port); + + // if the SS pin is not already configured as an output + // then set it high (to enable the internal pull-up resistor) + if(!(*reg & bit)){ + digitalWrite(SS, HIGH); + } + + // When the SS pin is set as OUTPUT, it can be used as + // a general purpose output port (it doesn't influence + // SPI operations). + pinMode(SS, OUTPUT); + + // Warning: if the SS pin ever becomes a LOW INPUT then SPI + // automatically switches to Slave, so the data direction of + // the SS pin MUST be kept as OUTPUT. + SPCR |= _BV(MSTR); + SPCR |= _BV(SPE); + + // Set direction register for SCK and MOSI pin. + // MISO pin automatically overrides to INPUT. + // By doing this AFTER enabling SPI, we avoid accidentally + // clocking in a single bit since the lines go directly + // from "input" to SPI control. + // http://code.google.com/p/arduino/issues/detail?id=888 + pinMode(SCK, OUTPUT); + pinMode(MOSI, OUTPUT); + } + initialized++; // reference count + SREG = sreg; +} + +void SPIClass::end() { + uint8_t sreg = SREG; + noInterrupts(); // Protect from a scheduler and prevent transactionBegin + // Decrease the reference counter + if (initialized) + initialized--; + // If there are no more references disable SPI + if (!initialized) { + SPCR &= ~_BV(SPE); + interruptMode = 0; + #ifdef SPI_TRANSACTION_MISMATCH_LED + inTransactionFlag = 0; + #endif + } + SREG = sreg; +} + +// mapping of interrupt numbers to bits within SPI_AVR_EIMSK +#if defined(__AVR_ATmega32U4__) + #define SPI_INT0_MASK (1< + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) + * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + */ + +#ifndef _SPI_H_INCLUDED +#define _SPI_H_INCLUDED + +#include + +// SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(), +// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) +#define SPI_HAS_TRANSACTION 1 + +// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method +#define SPI_HAS_NOTUSINGINTERRUPT 1 + +// SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version. +// This way when there is a bug fix you can check this define to alert users +// of your code if it uses better version of this library. +// This also implies everything that SPI_HAS_TRANSACTION as documented above is +// available too. +#define SPI_ATOMIC_VERSION 1 + +// Uncomment this line to add detection of mismatched begin/end transactions. +// A mismatch occurs if other libraries fail to use SPI.endTransaction() for +// each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn +// on if any mismatch is ever detected. +//#define SPI_TRANSACTION_MISMATCH_LED 5 + +#ifndef LSBFIRST +#define LSBFIRST 0 +#endif +#ifndef MSBFIRST +#define MSBFIRST 1 +#endif + +#define SPI_CLOCK_DIV4 0x00 +#define SPI_CLOCK_DIV16 0x01 +#define SPI_CLOCK_DIV64 0x02 +#define SPI_CLOCK_DIV128 0x03 +#define SPI_CLOCK_DIV2 0x04 +#define SPI_CLOCK_DIV8 0x05 +#define SPI_CLOCK_DIV32 0x06 + +#define SPI_MODE0 0x00 +#define SPI_MODE1 0x04 +#define SPI_MODE2 0x08 +#define SPI_MODE3 0x0C + +#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR +#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR +#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR + +// define SPI_AVR_EIMSK for AVR boards with external interrupt pins +#if defined(EIMSK) + #define SPI_AVR_EIMSK EIMSK +#elif defined(GICR) + #define SPI_AVR_EIMSK GICR +#elif defined(GIMSK) + #define SPI_AVR_EIMSK GIMSK +#endif + +class SPISettings { +public: + SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) { + if (__builtin_constant_p(clock)) { + init_AlwaysInline(clock, bitOrder, dataMode); + } else { + init_MightInline(clock, bitOrder, dataMode); + } + } + SPISettings() { + init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); + } +private: + void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) { + init_AlwaysInline(clock, bitOrder, dataMode); + } + void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) + __attribute__((__always_inline__)) { + // Clock settings are defined as follows. Note that this shows SPI2X + // inverted, so the bits form increasing numbers. Also note that + // fosc/64 appears twice + // SPR1 SPR0 ~SPI2X Freq + // 0 0 0 fosc/2 + // 0 0 1 fosc/4 + // 0 1 0 fosc/8 + // 0 1 1 fosc/16 + // 1 0 0 fosc/32 + // 1 0 1 fosc/64 + // 1 1 0 fosc/64 + // 1 1 1 fosc/128 + + // We find the fastest clock that is less than or equal to the + // given clock rate. The clock divider that results in clock_setting + // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the + // slowest (128 == 2 ^^ 7, so clock_div = 6). + uint8_t clockDiv; + + // When the clock is known at compile time, use this if-then-else + // cascade, which the compiler knows how to completely optimize + // away. When clock is not known, use a loop instead, which generates + // shorter code. + if (__builtin_constant_p(clock)) { + if (clock >= F_CPU / 2) { + clockDiv = 0; + } else if (clock >= F_CPU / 4) { + clockDiv = 1; + } else if (clock >= F_CPU / 8) { + clockDiv = 2; + } else if (clock >= F_CPU / 16) { + clockDiv = 3; + } else if (clock >= F_CPU / 32) { + clockDiv = 4; + } else if (clock >= F_CPU / 64) { + clockDiv = 5; + } else { + clockDiv = 6; + } + } else { + uint32_t clockSetting = F_CPU / 2; + clockDiv = 0; + while (clockDiv < 6 && clock < clockSetting) { + clockSetting /= 2; + clockDiv++; + } + } + + // Compensate for the duplicate fosc/64 + if (clockDiv == 6) + clockDiv = 7; + + // Invert the SPI2X bit + clockDiv ^= 0x1; + + // Pack into the SPISettings class + spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) | + (dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK); + spsr = clockDiv & SPI_2XCLOCK_MASK; + } + uint8_t spcr; + uint8_t spsr; + friend class SPIClass; +}; + + +class SPIClass { +public: + // Initialize the SPI library + static void begin(); + + // If SPI is used from within an interrupt, this function registers + // that interrupt with the SPI library, so beginTransaction() can + // prevent conflicts. The input interruptNumber is the number used + // with attachInterrupt. If SPI is used from a different interrupt + // (eg, a timer), interruptNumber should be 255. + static void usingInterrupt(uint8_t interruptNumber); + // And this does the opposite. + static void notUsingInterrupt(uint8_t interruptNumber); + // Note: the usingInterrupt and notUsingInterrupt functions should + // not to be called from ISR context or inside a transaction. + // For details see: + // https://github.com/arduino/Arduino/pull/2381 + // https://github.com/arduino/Arduino/pull/2449 + + // Before using SPI.transfer() or asserting chip select pins, + // this function is used to gain exclusive access to the SPI bus + // and configure the correct settings. + inline static void beginTransaction(SPISettings settings) { + if (interruptMode > 0) { + uint8_t sreg = SREG; + noInterrupts(); + + #ifdef SPI_AVR_EIMSK + if (interruptMode == 1) { + interruptSave = SPI_AVR_EIMSK; + SPI_AVR_EIMSK &= ~interruptMask; + SREG = sreg; + } else + #endif + { + interruptSave = sreg; + } + } + + #ifdef SPI_TRANSACTION_MISMATCH_LED + if (inTransactionFlag) { + pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT); + digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH); + } + inTransactionFlag = 1; + #endif + + SPCR = settings.spcr; + SPSR = settings.spsr; + } + + // Write to the SPI bus (MOSI pin) and also receive (MISO pin) + inline static uint8_t transfer(uint8_t data) { + SPDR = data; + /* + * The following NOP introduces a small delay that can prevent the wait + * loop form iterating when running at the maximum speed. This gives + * about 10% more speed, even if it seems counter-intuitive. At lower + * speeds it is unnoticed. + */ + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; // wait + return SPDR; + } + inline static uint16_t transfer16(uint16_t data) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out; + in.val = data; + if (!(SPCR & _BV(DORD))) { + SPDR = in.msb; + asm volatile("nop"); // See transfer(uint8_t) function + while (!(SPSR & _BV(SPIF))) ; + out.msb = SPDR; + SPDR = in.lsb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.lsb = SPDR; + } else { + SPDR = in.lsb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.lsb = SPDR; + SPDR = in.msb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.msb = SPDR; + } + return out.val; + } + inline static void transfer(void *buf, size_t count) { + if (count == 0) return; + uint8_t *p = (uint8_t *)buf; + SPDR = *p; + while (--count > 0) { + uint8_t out = *(p + 1); + while (!(SPSR & _BV(SPIF))) ; + uint8_t in = SPDR; + SPDR = out; + *p++ = in; + } + while (!(SPSR & _BV(SPIF))) ; + *p = SPDR; + } + // After performing a group of transfers and releasing the chip select + // signal, this function allows others to access the SPI bus + inline static void endTransaction(void) { + #ifdef SPI_TRANSACTION_MISMATCH_LED + if (!inTransactionFlag) { + pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT); + digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH); + } + inTransactionFlag = 0; + #endif + + if (interruptMode > 0) { + #ifdef SPI_AVR_EIMSK + uint8_t sreg = SREG; + #endif + noInterrupts(); + #ifdef SPI_AVR_EIMSK + if (interruptMode == 1) { + SPI_AVR_EIMSK = interruptSave; + SREG = sreg; + } else + #endif + { + SREG = interruptSave; + } + } + } + + // Disable the SPI bus + static void end(); + + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setBitOrder(uint8_t bitOrder) { + if (bitOrder == LSBFIRST) SPCR |= _BV(DORD); + else SPCR &= ~(_BV(DORD)); + } + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setDataMode(uint8_t dataMode) { + SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode; + } + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setClockDivider(uint8_t clockDiv) { + SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK); + SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK); + } + // These undocumented functions should not be used. SPI.transfer() + // polls the hardware flag which is automatically cleared as the + // AVR responds to SPI's interrupt + inline static void attachInterrupt() { SPCR |= _BV(SPIE); } + inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); } + +private: + static uint8_t initialized; + static uint8_t interruptMode; // 0=none, 1=mask, 2=global + static uint8_t interruptMask; // which interrupts to mask + static uint8_t interruptSave; // temp storage, to restore state + #ifdef SPI_TRANSACTION_MISMATCH_LED + static uint8_t inTransactionFlag; + #endif +}; + +extern SPIClass SPI; + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp index 1f93c56..d939cf1 100644 --- a/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Wire.cpp @@ -25,7 +25,7 @@ extern "C" { #include #include #include - #include "utility/twi.h" + // #include "utility/twi.h" } #include "Wire.h" diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/binary.h b/v4.0/contributed unsafe modules/ArduinoAdapter/binary.h new file mode 100644 index 0000000..aec4c73 --- /dev/null +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/binary.h @@ -0,0 +1,534 @@ +/* + binary.h - Definitions for binary constants + Copyright (c) 2006 David A. Mellis. 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 +*/ + +#ifndef Binary_h +#define Binary_h + +#define B0 0 +#define B00 0 +#define B000 0 +#define B0000 0 +#define B00000 0 +#define B000000 0 +#define B0000000 0 +#define B00000000 0 +#define B1 1 +#define B01 1 +#define B001 1 +#define B0001 1 +#define B00001 1 +#define B000001 1 +#define B0000001 1 +#define B00000001 1 +#define B10 2 +#define B010 2 +#define B0010 2 +#define B00010 2 +#define B000010 2 +#define B0000010 2 +#define B00000010 2 +#define B11 3 +#define B011 3 +#define B0011 3 +#define B00011 3 +#define B000011 3 +#define B0000011 3 +#define B00000011 3 +#define B100 4 +#define B0100 4 +#define B00100 4 +#define B000100 4 +#define B0000100 4 +#define B00000100 4 +#define B101 5 +#define B0101 5 +#define B00101 5 +#define B000101 5 +#define B0000101 5 +#define B00000101 5 +#define B110 6 +#define B0110 6 +#define B00110 6 +#define B000110 6 +#define B0000110 6 +#define B00000110 6 +#define B111 7 +#define B0111 7 +#define B00111 7 +#define B000111 7 +#define B0000111 7 +#define B00000111 7 +#define B1000 8 +#define B01000 8 +#define B001000 8 +#define B0001000 8 +#define B00001000 8 +#define B1001 9 +#define B01001 9 +#define B001001 9 +#define B0001001 9 +#define B00001001 9 +#define B1010 10 +#define B01010 10 +#define B001010 10 +#define B0001010 10 +#define B00001010 10 +#define B1011 11 +#define B01011 11 +#define B001011 11 +#define B0001011 11 +#define B00001011 11 +#define B1100 12 +#define B01100 12 +#define B001100 12 +#define B0001100 12 +#define B00001100 12 +#define B1101 13 +#define B01101 13 +#define B001101 13 +#define B0001101 13 +#define B00001101 13 +#define B1110 14 +#define B01110 14 +#define B001110 14 +#define B0001110 14 +#define B00001110 14 +#define B1111 15 +#define B01111 15 +#define B001111 15 +#define B0001111 15 +#define B00001111 15 +#define B10000 16 +#define B010000 16 +#define B0010000 16 +#define B00010000 16 +#define B10001 17 +#define B010001 17 +#define B0010001 17 +#define B00010001 17 +#define B10010 18 +#define B010010 18 +#define B0010010 18 +#define B00010010 18 +#define B10011 19 +#define B010011 19 +#define B0010011 19 +#define B00010011 19 +#define B10100 20 +#define B010100 20 +#define B0010100 20 +#define B00010100 20 +#define B10101 21 +#define B010101 21 +#define B0010101 21 +#define B00010101 21 +#define B10110 22 +#define B010110 22 +#define B0010110 22 +#define B00010110 22 +#define B10111 23 +#define B010111 23 +#define B0010111 23 +#define B00010111 23 +#define B11000 24 +#define B011000 24 +#define B0011000 24 +#define B00011000 24 +#define B11001 25 +#define B011001 25 +#define B0011001 25 +#define B00011001 25 +#define B11010 26 +#define B011010 26 +#define B0011010 26 +#define B00011010 26 +#define B11011 27 +#define B011011 27 +#define B0011011 27 +#define B00011011 27 +#define B11100 28 +#define B011100 28 +#define B0011100 28 +#define B00011100 28 +#define B11101 29 +#define B011101 29 +#define B0011101 29 +#define B00011101 29 +#define B11110 30 +#define B011110 30 +#define B0011110 30 +#define B00011110 30 +#define B11111 31 +#define B011111 31 +#define B0011111 31 +#define B00011111 31 +#define B100000 32 +#define B0100000 32 +#define B00100000 32 +#define B100001 33 +#define B0100001 33 +#define B00100001 33 +#define B100010 34 +#define B0100010 34 +#define B00100010 34 +#define B100011 35 +#define B0100011 35 +#define B00100011 35 +#define B100100 36 +#define B0100100 36 +#define B00100100 36 +#define B100101 37 +#define B0100101 37 +#define B00100101 37 +#define B100110 38 +#define B0100110 38 +#define B00100110 38 +#define B100111 39 +#define B0100111 39 +#define B00100111 39 +#define B101000 40 +#define B0101000 40 +#define B00101000 40 +#define B101001 41 +#define B0101001 41 +#define B00101001 41 +#define B101010 42 +#define B0101010 42 +#define B00101010 42 +#define B101011 43 +#define B0101011 43 +#define B00101011 43 +#define B101100 44 +#define B0101100 44 +#define B00101100 44 +#define B101101 45 +#define B0101101 45 +#define B00101101 45 +#define B101110 46 +#define B0101110 46 +#define B00101110 46 +#define B101111 47 +#define B0101111 47 +#define B00101111 47 +#define B110000 48 +#define B0110000 48 +#define B00110000 48 +#define B110001 49 +#define B0110001 49 +#define B00110001 49 +#define B110010 50 +#define B0110010 50 +#define B00110010 50 +#define B110011 51 +#define B0110011 51 +#define B00110011 51 +#define B110100 52 +#define B0110100 52 +#define B00110100 52 +#define B110101 53 +#define B0110101 53 +#define B00110101 53 +#define B110110 54 +#define B0110110 54 +#define B00110110 54 +#define B110111 55 +#define B0110111 55 +#define B00110111 55 +#define B111000 56 +#define B0111000 56 +#define B00111000 56 +#define B111001 57 +#define B0111001 57 +#define B00111001 57 +#define B111010 58 +#define B0111010 58 +#define B00111010 58 +#define B111011 59 +#define B0111011 59 +#define B00111011 59 +#define B111100 60 +#define B0111100 60 +#define B00111100 60 +#define B111101 61 +#define B0111101 61 +#define B00111101 61 +#define B111110 62 +#define B0111110 62 +#define B00111110 62 +#define B111111 63 +#define B0111111 63 +#define B00111111 63 +#define B1000000 64 +#define B01000000 64 +#define B1000001 65 +#define B01000001 65 +#define B1000010 66 +#define B01000010 66 +#define B1000011 67 +#define B01000011 67 +#define B1000100 68 +#define B01000100 68 +#define B1000101 69 +#define B01000101 69 +#define B1000110 70 +#define B01000110 70 +#define B1000111 71 +#define B01000111 71 +#define B1001000 72 +#define B01001000 72 +#define B1001001 73 +#define B01001001 73 +#define B1001010 74 +#define B01001010 74 +#define B1001011 75 +#define B01001011 75 +#define B1001100 76 +#define B01001100 76 +#define B1001101 77 +#define B01001101 77 +#define B1001110 78 +#define B01001110 78 +#define B1001111 79 +#define B01001111 79 +#define B1010000 80 +#define B01010000 80 +#define B1010001 81 +#define B01010001 81 +#define B1010010 82 +#define B01010010 82 +#define B1010011 83 +#define B01010011 83 +#define B1010100 84 +#define B01010100 84 +#define B1010101 85 +#define B01010101 85 +#define B1010110 86 +#define B01010110 86 +#define B1010111 87 +#define B01010111 87 +#define B1011000 88 +#define B01011000 88 +#define B1011001 89 +#define B01011001 89 +#define B1011010 90 +#define B01011010 90 +#define B1011011 91 +#define B01011011 91 +#define B1011100 92 +#define B01011100 92 +#define B1011101 93 +#define B01011101 93 +#define B1011110 94 +#define B01011110 94 +#define B1011111 95 +#define B01011111 95 +#define B1100000 96 +#define B01100000 96 +#define B1100001 97 +#define B01100001 97 +#define B1100010 98 +#define B01100010 98 +#define B1100011 99 +#define B01100011 99 +#define B1100100 100 +#define B01100100 100 +#define B1100101 101 +#define B01100101 101 +#define B1100110 102 +#define B01100110 102 +#define B1100111 103 +#define B01100111 103 +#define B1101000 104 +#define B01101000 104 +#define B1101001 105 +#define B01101001 105 +#define B1101010 106 +#define B01101010 106 +#define B1101011 107 +#define B01101011 107 +#define B1101100 108 +#define B01101100 108 +#define B1101101 109 +#define B01101101 109 +#define B1101110 110 +#define B01101110 110 +#define B1101111 111 +#define B01101111 111 +#define B1110000 112 +#define B01110000 112 +#define B1110001 113 +#define B01110001 113 +#define B1110010 114 +#define B01110010 114 +#define B1110011 115 +#define B01110011 115 +#define B1110100 116 +#define B01110100 116 +#define B1110101 117 +#define B01110101 117 +#define B1110110 118 +#define B01110110 118 +#define B1110111 119 +#define B01110111 119 +#define B1111000 120 +#define B01111000 120 +#define B1111001 121 +#define B01111001 121 +#define B1111010 122 +#define B01111010 122 +#define B1111011 123 +#define B01111011 123 +#define B1111100 124 +#define B01111100 124 +#define B1111101 125 +#define B01111101 125 +#define B1111110 126 +#define B01111110 126 +#define B1111111 127 +#define B01111111 127 +#define B10000000 128 +#define B10000001 129 +#define B10000010 130 +#define B10000011 131 +#define B10000100 132 +#define B10000101 133 +#define B10000110 134 +#define B10000111 135 +#define B10001000 136 +#define B10001001 137 +#define B10001010 138 +#define B10001011 139 +#define B10001100 140 +#define B10001101 141 +#define B10001110 142 +#define B10001111 143 +#define B10010000 144 +#define B10010001 145 +#define B10010010 146 +#define B10010011 147 +#define B10010100 148 +#define B10010101 149 +#define B10010110 150 +#define B10010111 151 +#define B10011000 152 +#define B10011001 153 +#define B10011010 154 +#define B10011011 155 +#define B10011100 156 +#define B10011101 157 +#define B10011110 158 +#define B10011111 159 +#define B10100000 160 +#define B10100001 161 +#define B10100010 162 +#define B10100011 163 +#define B10100100 164 +#define B10100101 165 +#define B10100110 166 +#define B10100111 167 +#define B10101000 168 +#define B10101001 169 +#define B10101010 170 +#define B10101011 171 +#define B10101100 172 +#define B10101101 173 +#define B10101110 174 +#define B10101111 175 +#define B10110000 176 +#define B10110001 177 +#define B10110010 178 +#define B10110011 179 +#define B10110100 180 +#define B10110101 181 +#define B10110110 182 +#define B10110111 183 +#define B10111000 184 +#define B10111001 185 +#define B10111010 186 +#define B10111011 187 +#define B10111100 188 +#define B10111101 189 +#define B10111110 190 +#define B10111111 191 +#define B11000000 192 +#define B11000001 193 +#define B11000010 194 +#define B11000011 195 +#define B11000100 196 +#define B11000101 197 +#define B11000110 198 +#define B11000111 199 +#define B11001000 200 +#define B11001001 201 +#define B11001010 202 +#define B11001011 203 +#define B11001100 204 +#define B11001101 205 +#define B11001110 206 +#define B11001111 207 +#define B11010000 208 +#define B11010001 209 +#define B11010010 210 +#define B11010011 211 +#define B11010100 212 +#define B11010101 213 +#define B11010110 214 +#define B11010111 215 +#define B11011000 216 +#define B11011001 217 +#define B11011010 218 +#define B11011011 219 +#define B11011100 220 +#define B11011101 221 +#define B11011110 222 +#define B11011111 223 +#define B11100000 224 +#define B11100001 225 +#define B11100010 226 +#define B11100011 227 +#define B11100100 228 +#define B11100101 229 +#define B11100110 230 +#define B11100111 231 +#define B11101000 232 +#define B11101001 233 +#define B11101010 234 +#define B11101011 235 +#define B11101100 236 +#define B11101101 237 +#define B11101110 238 +#define B11101111 239 +#define B11110000 240 +#define B11110001 241 +#define B11110010 242 +#define B11110011 243 +#define B11110100 244 +#define B11110101 245 +#define B11110110 246 +#define B11110111 247 +#define B11111000 248 +#define B11111001 249 +#define B11111010 250 +#define B11111011 251 +#define B11111100 252 +#define B11111101 253 +#define B11111110 254 +#define B11111111 255 + +#endif diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h b/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h index b6b6e96..9b76a53 100644 --- a/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/shims.h @@ -3,7 +3,6 @@ #ifndef _SHIMS_H_ #define _SHIMS_H_ -extern "C" { #ifndef uint32 #define uint32 uint32_t @@ -40,6 +39,12 @@ extern "C" { #define delayMicroseconds _delayUs #endif +#ifndef delay +#define delay _delayMs +#endif + +extern "C" { + void _i2cinit(unsigned char speed, unsigned char premultiplier, _Bool activatePullups); _Bool _i2cstart(); void _i2cstop(); @@ -48,7 +53,7 @@ extern "C" { // higher level I2C void _i2cWriteByte(unsigned char address, unsigned char byte); - void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * buffer, _Bool isString); + void _i2cWriteBuffer(unsigned char address, unsigned short maxMsgLen, const char * __nonnull buffer, _Bool isString); const unsigned char * __nullable _i2cReadBuffer(unsigned char address, unsigned char size, unsigned char * __nullable buffer); void _i2cshutdown(); From d5582a7ab0d9c5b58002915d780b6d14669905ff Mon Sep 17 00:00:00 2001 From: Carl Peto Date: Wed, 19 Oct 2022 23:29:44 +0100 Subject: [PATCH 3/3] add in a default for the arduino version --- v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h | 2 ++ v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h index 19ff7ff..9f39a03 100644 --- a/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h +++ b/v4.0/contributed libraries/unsafe_modules/Arduino/Arduino.h @@ -1,5 +1,7 @@ #define ARDUINO_SHIM_DEFINED (unsigned char)1 +#define ARDUINO 100 + #ifdef __cplusplus #include "DebugPrinter.h" diff --git a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h index 19ff7ff..9f39a03 100644 --- a/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h +++ b/v4.0/contributed unsafe modules/ArduinoAdapter/Arduino.h @@ -1,5 +1,7 @@ #define ARDUINO_SHIM_DEFINED (unsigned char)1 +#define ARDUINO 100 + #ifdef __cplusplus #include "DebugPrinter.h"