├── .github └── workflows │ └── check.yaml ├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── cmake └── git-download.cmake ├── examples ├── mock-injection │ ├── .gitignore │ ├── platformio.ini │ ├── src │ │ ├── MyService.cpp │ │ └── MyService.h │ └── test │ │ └── test_my_service.cpp └── wiring-blink │ ├── .gitignore │ ├── README.rst │ ├── lib │ └── readme.txt │ ├── platformio.ini │ ├── src │ └── main.cpp │ └── test │ └── test_main.cpp ├── external ├── CMakeLists.txt ├── fakeit │ └── CMakeLists.txt └── unity │ └── CMakeLists.txt ├── library.json ├── platformio.ini ├── src ├── Arduino.h ├── ArduinoFake.cpp ├── ArduinoFake.h ├── CMakeLists.txt ├── Client.h ├── ClientFake.cpp ├── ClientFake.h ├── EEPROM.h ├── EEPROMFake.cpp ├── EEPROMFake.h ├── Function.h ├── FunctionFake.cpp ├── FunctionFake.h ├── Print.h ├── PrintFake.cpp ├── PrintFake.h ├── SPI.h ├── SPIFake.cpp ├── SPIFake.h ├── Serial.h ├── SerialFake.cpp ├── SerialFake.h ├── Stream.h ├── StreamFake.cpp ├── StreamFake.h ├── WString.h ├── Wire.h ├── WireFake.cpp ├── WireFake.h ├── arduino │ ├── Arduino.h │ ├── Client.h │ ├── EEPROM.h │ ├── HardwareSerial.h │ ├── IPAddress.cpp │ ├── IPAddress.h │ ├── Print.h │ ├── Printable.h │ ├── README.md │ ├── SPI.h │ ├── Server.h │ ├── Stream.h │ ├── USBAPI.h │ ├── USBCore.h │ ├── USBDesc.h │ ├── WCharacter.h │ ├── WString.cpp │ ├── WString.h │ ├── Wire.h │ ├── avr │ │ └── interrupt.h │ ├── binary.h │ ├── noniso.c │ ├── noniso.h │ ├── pgmspace.h │ └── pins_arduino.h └── fakeit.hpp └── test ├── CMakeLists.txt ├── main.cpp ├── test_arduino_string.h ├── test_client.h ├── test_context.h ├── test_eeprom.h ├── test_function.h ├── test_include.h ├── test_print.h ├── test_serial.h ├── test_spi.h ├── test_stream.h └── test_wire.h /.github/workflows/check.yaml: -------------------------------------------------------------------------------- 1 | name: Check 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | os: [ubuntu-22.04] 12 | steps: 13 | - 14 | name: Checkout 15 | uses: actions/checkout@v3 16 | - 17 | name: Tests 18 | run: make 19 | 20 | examples: 21 | runs-on: ${{ matrix.os }} 22 | strategy: 23 | fail-fast: false 24 | matrix: 25 | os: [ubuntu-22.04] 26 | example: [mock-injection, wiring-blink] 27 | steps: 28 | - 29 | name: Checkout 30 | uses: actions/checkout@v3 31 | - 32 | uses: actions/cache@v3 33 | with: 34 | path: | 35 | ~/.cache/pip 36 | ~/.platformio/.cache 37 | key: ${{ runner.os }}-pio 38 | - 39 | uses: actions/setup-python@v4 40 | with: 41 | python-version: '3.9' 42 | - 43 | name: Install PlatformIO Core 44 | run: pip install --upgrade platformio 45 | - 46 | name: Tests 47 | run: pio test -d examples/wiring-blink/ 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .pioenvs 2 | .piolibdeps 3 | /external/unity/*-repo/ 4 | /external/fakeit/*-repo/ 5 | /build/ 6 | /.cproject 7 | /.project 8 | **/CMakeFiles/* 9 | **/CMakeCache.txt 10 | **/*.cmake 11 | **/Makefile 12 | !/Makefile 13 | /Testing/* 14 | .pio/* 15 | .vscode/* 16 | /test/test_main.cpp 17 | output.txt 18 | error.txt -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | sudo: false 3 | dist: trusty 4 | group: beta 5 | language: cpp 6 | 7 | before_install: 8 | - python -m pip install --user --upgrade pip 9 | - python -m pip install --user --upgrade platformio 10 | 11 | matrix: 12 | include: 13 | # - 14 | # compiler: gcc 15 | # env: 16 | # - MAKE_TASK=pio-test 17 | # - COMPILER=g++-5 18 | # addons: 19 | # apt: 20 | # packages: 21 | # - git 22 | # - g++-5 23 | # sources: 24 | # - ubuntu-toolchain-r-test 25 | # - 26 | # compiler: clang 27 | # env: 28 | # - MAKE_TASK=pio-test 29 | # - COMPILER=clang++-3.6 30 | # addons: 31 | # apt: 32 | # packages: 33 | # - git 34 | # - clang-3.6 35 | # sources: 36 | # - ubuntu-toolchain-r-test 37 | # - llvm-toolchain-precise-3.6 38 | - 39 | compiler: gcc 40 | env: 41 | - MAKE_TASK=cmake-test 42 | - COMPILER=g++-5 43 | addons: 44 | apt: 45 | packages: 46 | - git 47 | - g++-5 48 | sources: 49 | - ubuntu-toolchain-r-test 50 | - 51 | compiler: clang 52 | env: 53 | - MAKE_TASK=cmake-test 54 | - COMPILER=clang++-3.6 55 | addons: 56 | apt: 57 | packages: 58 | - git 59 | - clang-3.6 60 | sources: 61 | - ubuntu-toolchain-r-test 62 | - llvm-toolchain-precise-3.6 63 | 64 | script: 65 | - "make $MAKE_TASK" 66 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2.2) 2 | project(ArduinoFake VERSION 0.1) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake) 8 | 9 | # Include external libs 10 | add_subdirectory(external/fakeit) 11 | add_subdirectory(external/unity) 12 | 13 | # Targets that we develop here 14 | enable_testing() 15 | 16 | add_subdirectory(src) 17 | add_subdirectory(test) 18 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | This is a step-by-step guide for contributors. 3 | 4 | ## Adding missing function 5 | I was missing `sei()`, `cli()` and `attachInterrupt()` in `ArduinoFake`, here is list of steps I did. 6 | 7 | 8 | 1. add definitions of new functions in [src/arduino/Arduino.h](/src/arduino/Arduino.h), check if your function is in [Arduino.h](/src/arduino/Arduino.h). There are two situations: 9 | * `attachInterrupt()` was already in [Arduino.h](/src/arduino/Arduino.h) so we are done. 10 | * `sei()` was not defined in [Arduino.h](/src/arduino/Arduino.h) so 11 | * create a new header file [avr/interrupt.h](/src/arduino/avr/interrupt.h) to cover interrupt related definitions with a content 12 | ```c++ 13 | /** 14 | * Fake version of avr/interrupt.h 15 | */ 16 | void cli(void); 17 | void sei(void); 18 | * add `#include "avr/interrupt.h"` in [Arduino.h](/src/arduino/Arduino.h) 19 | 1. Find approriate place for your functions, in my case I extended [src/FunctionFake.h](/src/FunctionFake.h) for new functions 20 | ```c++ 21 | struct FunctionFake 22 | { 23 | ... 24 | virtual void attachInterrupt(uint8_t, void (*)(void), int mode) = 0; 25 | virtual void cli() = 0; 26 | virtual void sei() = 0; 27 | ... 28 | } 29 | ``` 30 | 1. add default implementations into corresponding cpp file, in my case [src/FunctionFake.cpp](/src/FunctionFake.cpp). 31 | ```c++ 32 | void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { 33 | ArduinoFakeInstance(Function)->attachInterrupt(interruptNum, userFunc, mode); 34 | } 35 | 36 | void cli(void) { 37 | ArduinoFakeInstance(Function)->cli(); 38 | } 39 | 40 | void sei(void) { 41 | ArduinoFakeInstance(Function)->sei(); 42 | } 43 | ``` 44 | 1. **don't forget to add TESTs** for new functionality, at least test if a function can be executed, in my case [test/test_function.h](/test/test_function.h) 45 | ```c++ 46 | void test_attach(void) 47 | { 48 | When(Method(ArduinoFake(), attachInterrupt)).AlwaysReturn(); 49 | 50 | attachInterrupt(1, (void (*)(void))NULL, FALLING); 51 | attachInterrupt(2, (void (*)(void))NULL, CHANGE); 52 | attachInterrupt(3, (void (*)(void))NULL, RISING); 53 | 54 | Verify(Method(ArduinoFake(), attachInterrupt)).Exactly(3); 55 | } 56 | 57 | void test_cli(void) 58 | { 59 | When(Method(ArduinoFake(), cli)).AlwaysReturn(); 60 | 61 | cli(); 62 | 63 | Verify(Method(ArduinoFake(), cli)).Once(); 64 | } 65 | 66 | void test_sei(void) 67 | { 68 | When(Method(ArduinoFake(), sei)).AlwaysReturn(); 69 | 70 | sei(); 71 | 72 | Verify(Method(ArduinoFake(), sei)).Once(); 73 | } 74 | ``` 75 | and add tests to test list 76 | ```c 77 | void run_tests(void) 78 | { 79 | ... 80 | RUN_TEST(FunctionTest::test_attach); 81 | RUN_TEST(FunctionTest::test_cli); 82 | RUN_TEST(FunctionTest::test_sei); 83 | ... 84 | } 85 | 1. excersice tests from command line, there are two ways based on your Makefile 86 | * default project [Makefile](/Makefile), 87 | * execute `make` (`make` requires `cmake` - install it via `apt` / `brew` / `yum` or whatever package manager your system uses) 88 | * verify 89 | ``` 90 | Running tests... 91 | Test project /home/vlcvi01/Dropbox/git/ArduinoFake/build 92 | Start 1: main 93 | 1/1 Test #1: main ............................. Passed 0.01 sec 94 | 95 | 100% tests passed, 0 tests failed out of 1 96 | ``` 97 | * [eclipse based Makefile](https://www.mantidproject.org/Setting_up_Eclipse_projects_with_CMake) generated via `cmake -G "Eclipse CDT4 - Unix Makefiles"`. 98 | * execute `make clean all && test/main` 99 | * verify PASS of all tests 100 | ``` 101 | ... 102 | .../ArduinoFake/test/main.cpp:184:FunctionTest::test_attach:PASS 103 | .../ArduinoFake/test/main.cpp:185:FunctionTest::test_sei:PASS 104 | .../ArduinoFake/test/main.cpp:186:FunctionTest::test_cli:PASS 105 | ... 106 | 107 | ----------------------- 108 | 39 Tests 0 Failures 0 Ignored 109 | OK 110 | ``` 111 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) ArduinoFake 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default_target: all 2 | 3 | .PHONY: all 4 | all: clean build deploy test clean 5 | 6 | .PHONY: cmake 7 | cmake: 8 | @cmake $(CURDIR) -B$(CURDIR)/build 9 | 10 | .PHONY: build 11 | build: cmake 12 | @cd $(CURDIR)/build && make all 13 | 14 | .PHONY: test 15 | test: 16 | @cd $(CURDIR)/build && CTEST_OUTPUT_ON_FAILURE=TRUE make test 17 | 18 | .PHONY: pio-test 19 | pio-test: 20 | @pio test 21 | 22 | .PHONY: cmake-test 23 | cmake-test: build test 24 | 25 | .PHONY: clean 26 | clean: 27 | @rm -rf $(CURDIR)/build/* 28 | @rm -rf $(CURDIR)/.pioenvs/* 29 | @rm -rf $(CURDIR)/.pio/* 30 | 31 | .PHONY: deploy 32 | deploy: 33 | cp $(CURDIR)/external/fakeit/fakeit-repo/single_header/standalone/* $(CURDIR)/src 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArduinoFake 2 | 3 | [![Build Status](https://travis-ci.org/FabioBatSilva/ArduinoFake.svg?branch=master)](https://travis-ci.org/FabioBatSilva/ArduinoFake) 4 | 5 | `ArduinoFake` is a simple mocking framework for Arduino. 6 | `ArduinoFake` is based on [FakeIt](https://github.com/eranpeer/FakeIt) and can be used for testing your arduino project natively. No arduino required ! 7 | # 8 | 9 | ## Quickstart 10 | 11 | ### Includes 12 | 13 | You should include the following header in your test file: 14 | 15 | ```c++ 16 | #include 17 | ``` 18 | 19 | ### Stubbing 20 | 21 | Assuming we have the following arduino code: 22 | ```c++ 23 | // src/main.cpp 24 | void loop() 25 | { 26 | // turn the LED on (HIGH is the voltage level) 27 | digitalWrite(LED_BUILTIN, HIGH); 28 | // wait for a second 29 | delay(100); 30 | // turn the LED off by making the voltage LOW 31 | digitalWrite(LED_BUILTIN, LOW); 32 | // wait for a second 33 | delay(100); 34 | } 35 | ``` 36 | 37 | It can be tested using `ArduinoFake`: 38 | ```c++ 39 | #include 40 | 41 | using namespace fakeit; 42 | 43 | // test/test_main.cpp 44 | void test_loop(void) 45 | { 46 | When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn(); 47 | When(Method(ArduinoFake(), delay)).AlwaysReturn(); 48 | 49 | loop(); 50 | 51 | Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, HIGH)).Once(); 52 | Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, LOW)).Once(); 53 | Verify(Method(ArduinoFake(), delay).Using(100)).Exactly(2_Times); 54 | } 55 | ``` 56 | 57 | Checkout the [examples](./examples) for many more examples! 58 | Or take a look at the [tests](./test) 59 | 60 | ## Troubleshooting 61 | 62 | If you get a segfault while running your unit tests, eg: 63 | 64 | ``` 65 | Program errored with 3221225477 code 66 | ``` 67 | 68 | Check to make sure you have stubbed **all** the Arduino methods you are calling. 69 | 70 | # Contributing 71 | If you want to extend `ArduinoFake` library to add missing functions (for example `attachInterrupt`) see [contribution guidelines](CONTRIBUTING.md). 72 | -------------------------------------------------------------------------------- /cmake/git-download.cmake: -------------------------------------------------------------------------------- 1 | find_package(Git REQUIRED) 2 | 3 | function(execute_git) 4 | set(options "") 5 | set(oneValueArgs 6 | OUTPUT_VARIABLE 7 | ) 8 | set(multiValueArgs 9 | COMMAND 10 | ) 11 | cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 12 | 13 | execute_process( 14 | COMMAND ${GIT_EXECUTABLE} ${args_COMMAND} 15 | OUTPUT_VARIABLE GIT_RESULT 16 | ) 17 | 18 | set(${args_OUTPUT_VARIABLE} ${GIT_RESULT} PARENT_SCOPE) 19 | endfunction() 20 | 21 | function(download_repo) 22 | set(options "") 23 | set(oneValueArgs 24 | URL 25 | TAG 26 | CLONE_DIR 27 | ) 28 | set(multiValueArgs "") 29 | cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 30 | 31 | # If no tag is specified, default to master 32 | if(NOT args_TAG) 33 | set(args_TAG master) 34 | endif() 35 | 36 | execute_git( 37 | COMMAND rev-parse --show-toplevel 38 | OUTPUT_VARIABLE GIT_ROOT 39 | ) 40 | 41 | # Need to remove linebreak 42 | string(STRIP ${GIT_ROOT} GIT_ROOT) 43 | 44 | # Make clone-dir path relative to git-root 45 | string(REPLACE ${GIT_ROOT}/ "" RELATIVE_CLONE_DIR ${args_CLONE_DIR}) 46 | 47 | if(NOT EXISTS ${args_CLONE_DIR}) 48 | message("Cloning branch ${args_TAG} from ${args_URL} into directory ${args_CLONE_DIR}") 49 | execute_git( 50 | COMMAND clone --depth=50 --branch=${args_TAG} ${args_URL} ${args_CLONE_DIR} 51 | WORKING_DIRECTORY ${GIT_ROOT} 52 | ) 53 | endif() 54 | 55 | endfunction() -------------------------------------------------------------------------------- /examples/mock-injection/.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .pioenvs 3 | .piolibdeps 4 | .clang_complete 5 | .gcc-flags.json 6 | -------------------------------------------------------------------------------- /examples/mock-injection/platformio.ini: -------------------------------------------------------------------------------- 1 | [env:native] 2 | platform = native 3 | test_build_src = yes 4 | build_flags = -std=gnu++17 5 | 6 | lib_deps = file://../../ 7 | 8 | # Use this instead of local file 9 | #lib_deps = FabioBatSilva/ArduinoFake 10 | -------------------------------------------------------------------------------- /examples/mock-injection/src/MyService.cpp: -------------------------------------------------------------------------------- 1 | #include "MyService.h" 2 | 3 | MyService::MyService(Client* client) 4 | { 5 | _client = client; 6 | } 7 | 8 | String MyService::request(const char *server) { 9 | String response; 10 | 11 | if (_client->connect(server, 80)) { 12 | _client->println("STATUS"); 13 | _client->println(); 14 | } 15 | 16 | while(_client->available()) { 17 | response.concat(_client->read()); 18 | } 19 | 20 | _client->stop(); 21 | 22 | return response; 23 | } 24 | -------------------------------------------------------------------------------- /examples/mock-injection/src/MyService.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class MyService 7 | { 8 | public: 9 | MyService (Client* client); 10 | String request(const char* url); 11 | 12 | private: 13 | Client* _client; 14 | }; 15 | -------------------------------------------------------------------------------- /examples/mock-injection/test/test_my_service.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace fakeit; 5 | 6 | #include "MyService.h" 7 | 8 | void setUp(void) 9 | { 10 | ArduinoFakeReset(); 11 | } 12 | 13 | void test_connect(void) 14 | { 15 | When(Method(ArduinoFake(Client), stop)).AlwaysReturn(); 16 | When(Method(ArduinoFake(Client), available)).Return(1, 1, 1, 0); 17 | When(OverloadedMethod(ArduinoFake(Client), read, int())).Return(2, 0, 0); 18 | When(OverloadedMethod(ArduinoFake(Client), println, size_t())).AlwaysReturn(); 19 | When(OverloadedMethod(ArduinoFake(Client), println, size_t(const char *))).AlwaysReturn(); 20 | When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1); 21 | 22 | Client* clientMock = ArduinoFakeMock(Client); 23 | 24 | MyService service(clientMock); 25 | 26 | String response = service.request("myserver.com"); 27 | TEST_ASSERT_EQUAL(3, response.length()); 28 | TEST_ASSERT_TRUE(response.equals("200")); 29 | 30 | Verify(Method(ArduinoFake(Client), stop)).Once(); 31 | Verify(Method(ArduinoFake(Client), available)).Exactly(4_Times); 32 | Verify(OverloadedMethod(ArduinoFake(Client), read, int())).Exactly(3_Times); 33 | 34 | Verify(OverloadedMethod(ArduinoFake(Client), println, size_t())).Once(); 35 | Verify(OverloadedMethod(ArduinoFake(Client), println, size_t(const char [])).Using((const char *)"STATUS")).Never(); 36 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char[], uint16_t)).Using((const char *)"myserver.com", 80)).Once(); 37 | } 38 | 39 | int main(int argc, char **argv) 40 | { 41 | UNITY_BEGIN(); 42 | 43 | RUN_TEST(test_connect); 44 | 45 | return UNITY_END(); 46 | } 47 | -------------------------------------------------------------------------------- /examples/wiring-blink/.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .pioenvs 3 | .piolibdeps 4 | .clang_complete 5 | .gcc-flags.json 6 | -------------------------------------------------------------------------------- /examples/wiring-blink/README.rst: -------------------------------------------------------------------------------- 1 | # ArduinoFake example 2 | 3 | ### Testing 4 | 5 | ```bash 6 | pio test 7 | ``` -------------------------------------------------------------------------------- /examples/wiring-blink/lib/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for the project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link to executable file. 4 | 5 | The source code of each library should be placed in separate directory, like 6 | "lib/private_lib/[here are source files]". 7 | 8 | For example, see how can be organized `Foo` and `Bar` libraries: 9 | 10 | |--lib 11 | | |--Bar 12 | | | |--docs 13 | | | |--examples 14 | | | |--src 15 | | | |- Bar.c 16 | | | |- Bar.h 17 | | |--Foo 18 | | | |- Foo.c 19 | | | |- Foo.h 20 | | |- readme.txt --> THIS FILE 21 | |- platformio.ini 22 | |--src 23 | |- main.c 24 | 25 | Then in `src/main.c` you should use: 26 | 27 | #include 28 | #include 29 | 30 | // rest H/C/CPP code 31 | 32 | PlatformIO will find your libraries automatically, configure preprocessor's 33 | include paths and build them. 34 | 35 | More information about PlatformIO Library Dependency Finder 36 | - http://docs.platformio.org/page/librarymanager/ldf.html 37 | -------------------------------------------------------------------------------- /examples/wiring-blink/platformio.ini: -------------------------------------------------------------------------------- 1 | [env:native] 2 | platform = native 3 | test_build_src = yes 4 | build_flags = -std=gnu++17 5 | 6 | lib_deps = file://../../ 7 | 8 | #lib_deps = FabioBatSilva/ArduinoFake 9 | -------------------------------------------------------------------------------- /examples/wiring-blink/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Blink 3 | * Turns on an LED on for one second, 4 | * then off for one second, repeatedly. 5 | */ 6 | 7 | #ifdef UNIT_TEST 8 | #include "ArduinoFake.h" 9 | #else 10 | #include "Arduino.h" 11 | #endif 12 | 13 | void setup() 14 | { 15 | // initialize LED digital pin as an output. 16 | pinMode(LED_BUILTIN, OUTPUT); 17 | } 18 | 19 | void loop() 20 | { 21 | // turn the LED on (HIGH is the voltage level) 22 | digitalWrite(LED_BUILTIN, HIGH); 23 | // wait for a second 24 | delay(100); 25 | // turn the LED off by making the voltage LOW 26 | digitalWrite(LED_BUILTIN, LOW); 27 | // wait for a second 28 | delay(100); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /examples/wiring-blink/test/test_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace fakeit; 5 | 6 | void test_setup(void) 7 | { 8 | When(Method(ArduinoFake(), pinMode)).Return(); 9 | 10 | setup(); 11 | 12 | Verify(Method(ArduinoFake(), pinMode).Using(LED_BUILTIN, OUTPUT)).Once(); 13 | } 14 | 15 | void test_loop(void) 16 | { 17 | When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn(); 18 | When(Method(ArduinoFake(), delay)).AlwaysReturn(); 19 | 20 | loop(); 21 | 22 | Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, HIGH)).Once(); 23 | Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, LOW)).Once(); 24 | Verify(Method(ArduinoFake(), delay).Using(100)).Exactly(2_Times); 25 | } 26 | 27 | void setUp(void) 28 | { 29 | ArduinoFakeReset(); 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | UNITY_BEGIN(); 35 | 36 | RUN_TEST(test_setup); 37 | RUN_TEST(test_loop); 38 | 39 | UNITY_END(); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /external/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Include external libs 2 | add_subdirectory(unity) 3 | add_subdirectory(fakeit) -------------------------------------------------------------------------------- /external/fakeit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2.2) 2 | project(fakeit VERSION 2.4.0 LANGUAGES CXX) 3 | 4 | include(git-download) 5 | 6 | set(REPO_DIR ${PROJECT_SOURCE_DIR}/${PROJECT_NAME}-repo) 7 | 8 | download_repo( 9 | URL "https://github.com/eranpeer/FakeIt.git" 10 | TAG ${PROJECT_VERSION} 11 | CLONE_DIR ${REPO_DIR} 12 | ) 13 | 14 | add_library(${PROJECT_NAME} INTERFACE) 15 | 16 | target_include_directories(${PROJECT_NAME} INTERFACE 17 | ${REPO_DIR}/single_header/standalone/ 18 | ) 19 | -------------------------------------------------------------------------------- /external/unity/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2.2) 2 | project(unity VERSION 2.4.1 LANGUAGES C) 3 | 4 | include(git-download) 5 | 6 | set(REPO_DIR ${PROJECT_SOURCE_DIR}/${PROJECT_NAME}-repo) 7 | 8 | download_repo( 9 | URL "https://github.com/ThrowTheSwitch/Unity.git" 10 | TAG v${PROJECT_VERSION} 11 | CLONE_DIR ${REPO_DIR} 12 | ) 13 | 14 | add_library(${PROJECT_NAME} STATIC 15 | ${REPO_DIR}/src/unity.c 16 | ) 17 | 18 | target_include_directories(${PROJECT_NAME} PUBLIC 19 | ${REPO_DIR}/src 20 | ) 21 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ArduinoFake", 3 | "version": "0.4.0", 4 | "keywords": "test, mock, fake, arduino", 5 | "description": "Arduino mocking made easy.", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/FabioBatSilva/ArduinoFake.git" 9 | }, 10 | "authors": { 11 | "name": "Fabio B. Silva", 12 | "email": "fabio.bat.silva@gmail.com" 13 | }, 14 | "exclude": [ 15 | "test" 16 | ], 17 | "build": { 18 | "libCompatMode": "off" 19 | }, 20 | "frameworks": "arduino", 21 | "platforms": "native" 22 | } 23 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:native] 12 | platform = native 13 | build_flags = -std=gnu++17 14 | test_build_src = yes 15 | -------------------------------------------------------------------------------- /src/Arduino.h: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | -------------------------------------------------------------------------------- /src/ArduinoFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | 3 | ArduinoFakeContext* arduinoFakeContext; 4 | 5 | ArduinoFakeContext* getArduinoFakeContext() 6 | { 7 | if (!arduinoFakeContext) { 8 | arduinoFakeContext = new ArduinoFakeContext(); 9 | } 10 | 11 | return arduinoFakeContext; 12 | } 13 | -------------------------------------------------------------------------------- /src/ArduinoFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // clang-format off 3 | 4 | #if !defined(UBRRH) && !defined(UBRR0H) && !defined(USBCON) 5 | #define USBCON 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "fakeit.hpp" 13 | 14 | #include "arduino/Arduino.h" 15 | 16 | #include "Function.h" 17 | #include "Stream.h" 18 | #include "Serial.h" 19 | #include "Wire.h" 20 | #include "Client.h" 21 | #include "Print.h" 22 | #include "SPI.h" 23 | #include "EEPROM.h" 24 | 25 | #define ArduinoFake(mock) _ArduinoFakeGet##mock() 26 | 27 | #define ArduinoFakeReset() \ 28 | getArduinoFakeContext()->reset() 29 | 30 | #define ArduinoFakeInstance(mock, ...) \ 31 | getArduinoFakeContext()->mock(__VA_ARGS__) 32 | 33 | #define ArduinoFakeMock(mock) \ 34 | new mock##FakeProxy(ArduinoFakeInstance(mock)) 35 | 36 | #define _ArduinoFakeGetMock(mock) \ 37 | getArduinoFakeContext()->Mocks->mock 38 | 39 | #define _ArduinoFakeGetFunction() _ArduinoFakeGetMock(Function) 40 | #define _ArduinoFakeGetSerial() _ArduinoFakeGetMock(Serial) 41 | #define _ArduinoFakeGetWire() _ArduinoFakeGetMock(Wire) 42 | #define _ArduinoFakeGetSPI() _ArduinoFakeGetMock(SPI) 43 | #define _ArduinoFakeGetEEPROM() _ArduinoFakeGetMock(EEPROM) 44 | #define _ArduinoFakeGetStream() _ArduinoFakeGetMock(Stream) 45 | #define _ArduinoFakeGetClient() _ArduinoFakeGetMock(Client) 46 | #define _ArduinoFakeGetPrint() _ArduinoFakeGetMock(Print) 47 | #define _ArduinoFakeGet() _ArduinoFakeGetMock(Function) 48 | 49 | #define _ArduinoFakeInstanceGetter1(mock) \ 50 | mock##Fake* mock() \ 51 | { \ 52 | if (!this->Instances->mock){ \ 53 | this->Instances->mock = &this->Mocks->mock.get(); \ 54 | } \ 55 | return this->Instances->mock; \ 56 | } 57 | 58 | #define _ArduinoFakeInstanceGetter2(name, clazz) \ 59 | name##Fake* name(class clazz* instance) \ 60 | { \ 61 | if (Mapping[instance]) { \ 62 | return (name##Fake*) Mapping[instance]; \ 63 | } \ 64 | if (dynamic_cast(instance)) { \ 65 | return dynamic_cast(instance)->get##name##Fake(); \ 66 | } \ 67 | throw std::runtime_error("Unknown instance"); \ 68 | } 69 | 70 | struct ArduinoFakeMocks 71 | { 72 | fakeit::Mock Function; 73 | fakeit::Mock Serial; 74 | fakeit::Mock Wire; 75 | fakeit::Mock Stream; 76 | fakeit::Mock Client; 77 | fakeit::Mock Print; 78 | fakeit::Mock SPI; 79 | fakeit::Mock EEPROM; 80 | }; 81 | 82 | struct ArduinoFakeInstances 83 | { 84 | FunctionFake* Function; 85 | SerialFake* Serial; 86 | WireFake* Wire; 87 | StreamFake* Stream; 88 | ClientFake* Client; 89 | PrintFake* Print; 90 | SPIFake* SPI; 91 | EEPROMFake* EEPROM; 92 | }; 93 | 94 | class ArduinoFakeContext 95 | { 96 | public: 97 | ArduinoFakeInstances* Instances = new ArduinoFakeInstances(); 98 | ArduinoFakeMocks* Mocks = new ArduinoFakeMocks(); 99 | std::unordered_map Mapping; 100 | 101 | _ArduinoFakeInstanceGetter1(Print) 102 | _ArduinoFakeInstanceGetter1(Stream) 103 | _ArduinoFakeInstanceGetter1(Serial) 104 | _ArduinoFakeInstanceGetter1(Wire) 105 | _ArduinoFakeInstanceGetter1(Client) 106 | _ArduinoFakeInstanceGetter1(Function) 107 | _ArduinoFakeInstanceGetter1(SPI) 108 | _ArduinoFakeInstanceGetter1(EEPROM) 109 | 110 | _ArduinoFakeInstanceGetter2(Print, Print) 111 | _ArduinoFakeInstanceGetter2(Client, Client) 112 | _ArduinoFakeInstanceGetter2(Stream, Stream) 113 | _ArduinoFakeInstanceGetter2(Serial, Serial_) 114 | _ArduinoFakeInstanceGetter2(Wire, TwoWire) 115 | _ArduinoFakeInstanceGetter2(SPI, SPIClass) 116 | _ArduinoFakeInstanceGetter2(EEPROM, EEPROMClass) 117 | 118 | ArduinoFakeContext() 119 | { 120 | this->reset(); 121 | } 122 | 123 | void reset(void) 124 | { 125 | if (this->Instances) { 126 | delete this->Instances; 127 | } 128 | this->Instances = new ArduinoFakeInstances(); 129 | 130 | this->Mocks->Function.Reset(); 131 | this->Mocks->Stream.Reset(); 132 | this->Mocks->Serial.Reset(); 133 | this->Mocks->Wire.Reset(); 134 | this->Mocks->Client.Reset(); 135 | this->Mocks->Print.Reset(); 136 | this->Mocks->SPI.Reset(); 137 | this->Mocks->EEPROM.Reset(); 138 | 139 | Mapping[&::Serial] = this->Serial(); 140 | Mapping[&::Wire] = this->Wire(); 141 | Mapping[&::SPI] = this->SPI(); 142 | Mapping[&::EEPROM] = this->EEPROM(); 143 | } 144 | }; 145 | 146 | ArduinoFakeContext* getArduinoFakeContext(); 147 | 148 | // clang-format on -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. SRC_LIST) 2 | aux_source_directory(./fakeit SRC_LIST) 3 | aux_source_directory(./arduino SRC_LIST) 4 | 5 | add_library(${PROJECT_NAME} SHARED ${SRC_LIST}) 6 | 7 | target_link_libraries(${PROJECT_NAME} fakeit) -------------------------------------------------------------------------------- /src/Client.h: -------------------------------------------------------------------------------- 1 | #include "ClientFake.h" 2 | -------------------------------------------------------------------------------- /src/ClientFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "ClientFake.h" 3 | #include 4 | 5 | int Client::connect(IPAddress ip, uint16_t port) 6 | { 7 | return ArduinoFakeInstance(Client, this)->connect(ip, port); 8 | } 9 | 10 | int Client::connect(const char *host, uint16_t port) 11 | { 12 | return ArduinoFakeInstance(Client, this)->connect(host, port); 13 | } 14 | 15 | size_t Client::write(uint8_t value) 16 | { 17 | return ArduinoFakeInstance(Client, this)->write(value); 18 | } 19 | 20 | size_t Client::write(const uint8_t *buf, size_t size) 21 | { 22 | return ArduinoFakeInstance(Client, this)->write(buf, size); 23 | } 24 | 25 | int Client::available() 26 | { 27 | return ArduinoFakeInstance(Client, this)->available(); 28 | } 29 | 30 | int Client::read() 31 | { 32 | return ArduinoFakeInstance(Client, this)->read(); 33 | } 34 | 35 | int Client::read(uint8_t *buf, size_t size) 36 | { 37 | return ArduinoFakeInstance(Client, this)->read(buf, size); 38 | } 39 | 40 | int Client::peek() 41 | { 42 | return ArduinoFakeInstance(Client, this)->peek(); 43 | } 44 | 45 | void Client::flush() 46 | { 47 | return ArduinoFakeInstance(Client, this)->flush(); 48 | } 49 | 50 | void Client::stop() 51 | { 52 | return ArduinoFakeInstance(Client, this)->stop(); 53 | } 54 | 55 | uint8_t Client::connected() 56 | { 57 | return ArduinoFakeInstance(Client, this)->connected(); 58 | } 59 | 60 | Client::operator bool() 61 | { 62 | return 1 == 1; 63 | } 64 | 65 | ClientFakeProxy::operator bool() 66 | { 67 | return 1 == 1; 68 | } 69 | -------------------------------------------------------------------------------- /src/ClientFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "StreamFake.h" 5 | #include "arduino/Client.h" 6 | 7 | struct ClientFake : public StreamFake 8 | { 9 | virtual int connect(IPAddress ip, uint16_t port) = 0; 10 | 11 | virtual int connect(const char *host, uint16_t port) = 0; 12 | 13 | virtual size_t write(uint8_t) = 0; 14 | 15 | virtual size_t write(const uint8_t *buf, size_t size) = 0; 16 | 17 | virtual int available() = 0; 18 | 19 | virtual int read() = 0; 20 | 21 | virtual int read(uint8_t *buf, size_t size) = 0; 22 | 23 | virtual int peek() = 0; 24 | 25 | virtual void flush() = 0; 26 | 27 | virtual void stop() = 0; 28 | 29 | virtual uint8_t connected() = 0; 30 | }; 31 | 32 | class ClientFakeProxy : public StreamFakeProxy, public Client 33 | { 34 | private: 35 | ClientFake* clientFake; 36 | 37 | public: 38 | ClientFakeProxy(ClientFake* fake) : StreamFakeProxy(fake) 39 | { 40 | clientFake = fake; 41 | } 42 | 43 | int connect(IPAddress ip, uint16_t port) 44 | { 45 | return clientFake->connect(ip, port); 46 | } 47 | 48 | int connect(const char *host, uint16_t port) 49 | { 50 | return clientFake->connect(host, port); 51 | } 52 | 53 | size_t write(uint8_t value) 54 | { 55 | return clientFake->write(value); 56 | } 57 | 58 | size_t write(const uint8_t *buf, size_t size) 59 | { 60 | return clientFake->write(buf, size); 61 | } 62 | 63 | int available() 64 | { 65 | return clientFake->available(); 66 | } 67 | 68 | int read() 69 | { 70 | return clientFake->read(); 71 | } 72 | 73 | int read(uint8_t *buf, size_t size) 74 | { 75 | return clientFake->read(buf, size); 76 | } 77 | 78 | int peek() 79 | { 80 | return clientFake->peek(); 81 | } 82 | 83 | void flush() 84 | { 85 | clientFake->flush(); 86 | } 87 | 88 | void stop() 89 | { 90 | clientFake->stop(); 91 | } 92 | 93 | uint8_t connected() 94 | { 95 | return clientFake->connected(); 96 | } 97 | 98 | virtual operator bool(); 99 | 100 | ClientFake* getClientFake() 101 | { 102 | return clientFake; 103 | } 104 | }; 105 | -------------------------------------------------------------------------------- /src/EEPROM.h: -------------------------------------------------------------------------------- 1 | #include "EEPROMFake.h" -------------------------------------------------------------------------------- /src/EEPROMFake.cpp: -------------------------------------------------------------------------------- 1 | // clang-format off 2 | #include "ArduinoFake.h" 3 | #include "EEPROMFake.h" 4 | // clang-format on 5 | 6 | uint8_t EEPROMClass::read(int idx) { 7 | return ArduinoFakeInstance(EEPROM)->read(idx); 8 | }; 9 | void EEPROMClass::write(int idx, uint8_t val) { 10 | ArduinoFakeInstance(EEPROM)->write(idx, val); 11 | }; 12 | void EEPROMClass::update(int idx, uint8_t val) { 13 | ArduinoFakeInstance(EEPROM)->update(idx, val); 14 | }; 15 | uint16_t EEPROMClass::length() { return ArduinoFakeInstance(EEPROM)->length(); } 16 | 17 | EEPROMClass EEPROM = EEPROMFakeProxy(ArduinoFakeInstance(EEPROM)); 18 | -------------------------------------------------------------------------------- /src/EEPROMFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "arduino/EEPROM.h" 5 | 6 | struct EEPROMFake { 7 | virtual uint8_t read(int idx) = 0; 8 | virtual void write(int idx, uint8_t val) = 0; 9 | virtual void update(int idx, uint8_t val) = 0; 10 | virtual uint16_t length() = 0; 11 | }; 12 | 13 | class EEPROMFakeProxy : public EEPROMClass { 14 | private: 15 | EEPROMFake *eepromFake; 16 | 17 | public: 18 | EEPROMFakeProxy(EEPROMFake *fake) { eepromFake = fake; } 19 | 20 | EEPROMFake *getEEPROMFake() { return eepromFake; } 21 | }; 22 | -------------------------------------------------------------------------------- /src/Function.h: -------------------------------------------------------------------------------- 1 | #include "FunctionFake.h" 2 | -------------------------------------------------------------------------------- /src/FunctionFake.cpp: -------------------------------------------------------------------------------- 1 | #include "FunctionFake.h" 2 | #include "ArduinoFake.h" 3 | 4 | void pinMode(uint8_t pin, uint8_t mode) 5 | { 6 | ArduinoFakeInstance(Function)->pinMode(pin, mode); 7 | } 8 | 9 | void digitalWrite(uint8_t pin, uint8_t val) 10 | { 11 | ArduinoFakeInstance(Function)->digitalWrite(pin, val); 12 | } 13 | 14 | int digitalRead(uint8_t pin) 15 | { 16 | return ArduinoFakeInstance(Function)->digitalRead(pin); 17 | } 18 | 19 | int analogRead(uint8_t pin) 20 | { 21 | return ArduinoFakeInstance(Function)->analogRead(pin); 22 | } 23 | 24 | void analogWrite(uint8_t pin, int val) 25 | { 26 | ArduinoFakeInstance(Function)->analogWrite(pin, val); 27 | } 28 | 29 | void analogReference(uint8_t mode) 30 | { 31 | ArduinoFakeInstance(Function)->analogReference(mode); 32 | } 33 | 34 | void analogReadResolution(uint8_t mode) 35 | { 36 | ArduinoFakeInstance(Function)->analogReadResolution(mode); 37 | } 38 | 39 | unsigned long millis(void) 40 | { 41 | return ArduinoFakeInstance(Function)->millis(); 42 | } 43 | 44 | unsigned long micros(void) 45 | { 46 | return ArduinoFakeInstance(Function)->micros(); 47 | } 48 | 49 | void delay(unsigned long value) 50 | { 51 | ArduinoFakeInstance(Function)->delay(value); 52 | } 53 | 54 | void delayMicroseconds(unsigned int us) 55 | { 56 | ArduinoFakeInstance(Function)->delayMicroseconds(us); 57 | } 58 | 59 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) 60 | { 61 | return ArduinoFakeInstance(Function)->pulseIn(pin, state, timeout); 62 | } 63 | 64 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) 65 | { 66 | return ArduinoFakeInstance(Function)->pulseInLong(pin, state, timeout); 67 | } 68 | 69 | void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) 70 | { 71 | ArduinoFakeInstance(Function)->shiftOut(dataPin, clockPin, bitOrder, val); 72 | } 73 | 74 | uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { 75 | return ArduinoFakeInstance(Function)->shiftIn(dataPin, clockPin, bitOrder); 76 | } 77 | 78 | void detachInterrupt(uint8_t interruptNum) { 79 | ArduinoFakeInstance(Function)->detachInterrupt(interruptNum); 80 | } 81 | 82 | void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { 83 | ArduinoFakeInstance(Function)->attachInterrupt(interruptNum, userFunc, mode); 84 | } 85 | 86 | void cli(void) { 87 | ArduinoFakeInstance(Function)->cli(); 88 | } 89 | 90 | void sei(void) { 91 | ArduinoFakeInstance(Function)->sei(); 92 | } 93 | 94 | void tone(uint8_t pin, unsigned int frequency, unsigned long duration) 95 | { 96 | ArduinoFakeInstance(Function)->tone(pin, frequency, duration); 97 | } 98 | 99 | void noTone(uint8_t pin) 100 | { 101 | ArduinoFakeInstance(Function)->noTone(pin); 102 | } 103 | 104 | long random(long max) 105 | { 106 | return ArduinoFakeInstance(Function)->random(max); 107 | } 108 | 109 | long random(long min, long max) 110 | { 111 | return ArduinoFakeInstance(Function)->random(min, max); 112 | } 113 | 114 | void randomSeed(unsigned long seed) 115 | { 116 | return ArduinoFakeInstance(Function)->randomSeed(seed); 117 | } 118 | 119 | long map(long value, long fromLow, long fromHigh, long toLow, long toHigh) 120 | { 121 | return ArduinoFakeInstance(Function)->map(value, fromLow, fromHigh, toLow, toHigh); 122 | } 123 | 124 | void yield() 125 | { 126 | ArduinoFakeInstance(Function)->yield(); 127 | } 128 | -------------------------------------------------------------------------------- /src/FunctionFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "fakeit.hpp" 4 | 5 | struct FunctionFake 6 | { 7 | virtual void init(void) = 0; 8 | virtual void loop(void) = 0; 9 | virtual void setup(void) = 0; 10 | 11 | virtual void pinMode(uint8_t, uint8_t) = 0; 12 | virtual void digitalWrite(uint8_t, uint8_t) = 0; 13 | virtual int digitalRead(uint8_t) = 0; 14 | 15 | virtual int analogRead(uint8_t) = 0; 16 | virtual void analogReference(uint8_t) = 0; 17 | virtual void analogReadResolution(uint8_t) = 0; 18 | virtual void analogWrite(uint8_t, int) = 0; 19 | 20 | virtual unsigned long millis(void) = 0; 21 | virtual unsigned long micros(void) = 0; 22 | 23 | virtual void delay(unsigned long) = 0; 24 | virtual void delayMicroseconds(unsigned int) = 0; 25 | 26 | virtual unsigned long pulseIn(uint8_t, uint8_t, unsigned long) = 0; 27 | virtual unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) = 0; 28 | 29 | virtual void shiftOut(uint8_t, uint8_t, uint8_t, uint8_t) = 0; 30 | virtual uint8_t shiftIn(uint8_t, uint8_t, uint8_t) = 0; 31 | 32 | virtual void detachInterrupt(uint8_t) = 0; 33 | virtual void attachInterrupt(uint8_t, void (*)(void), int mode) = 0; 34 | virtual void cli() = 0; 35 | virtual void sei() = 0; 36 | 37 | virtual void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) = 0; 38 | virtual void noTone(uint8_t _pin) = 0; 39 | 40 | virtual long random(long) = 0; 41 | virtual long random(long, long) = 0; 42 | virtual void randomSeed(unsigned long) = 0; 43 | 44 | virtual long map(long, long, long, long, long) = 0; 45 | 46 | virtual void yield() = 0; 47 | }; 48 | -------------------------------------------------------------------------------- /src/Print.h: -------------------------------------------------------------------------------- 1 | #include "PrintFake.h" 2 | -------------------------------------------------------------------------------- /src/PrintFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "PrintFake.h" 3 | 4 | size_t Print::write(const uint8_t *buffer, size_t size) 5 | { 6 | return ArduinoFakeInstance(Print, this)->write(buffer, size); 7 | } 8 | 9 | size_t Print::print(const __FlashStringHelper *ifsh) 10 | { 11 | return ArduinoFakeInstance(Print, this)->print(ifsh); 12 | } 13 | 14 | size_t Print::print(const String &s) 15 | { 16 | return ArduinoFakeInstance(Print, this)->print(s); 17 | } 18 | 19 | size_t Print::print(const char str[]) 20 | { 21 | return ArduinoFakeInstance(Print, this)->print(str); 22 | } 23 | 24 | size_t Print::write(uint8_t u) 25 | { 26 | return ArduinoFakeInstance(Print, this)->write(u); 27 | } 28 | 29 | size_t Print::print(char c) 30 | { 31 | return ArduinoFakeInstance(Print, this)->print(c); 32 | } 33 | 34 | size_t Print::print(unsigned char b, int base) 35 | { 36 | return ArduinoFakeInstance(Print, this)->print(b, base); 37 | } 38 | 39 | size_t Print::print(int n, int base) 40 | { 41 | return ArduinoFakeInstance(Print, this)->print(n, base); 42 | } 43 | 44 | size_t Print::print(unsigned int n, int base) 45 | { 46 | return ArduinoFakeInstance(Print, this)->print(n, base); 47 | } 48 | 49 | size_t Print::print(long n, int base) 50 | { 51 | return ArduinoFakeInstance(Print, this)->print(n, base); 52 | } 53 | 54 | size_t Print::print(unsigned long n, int base) 55 | { 56 | return ArduinoFakeInstance(Print, this)->print(n, base); 57 | } 58 | 59 | size_t Print::print(double n, int digits) 60 | { 61 | return ArduinoFakeInstance(Print, this)->print(n, digits); 62 | } 63 | 64 | size_t Print::print(const Printable& x) 65 | { 66 | return ArduinoFakeInstance(Print, this)->print(x); 67 | } 68 | 69 | size_t Print::println(const __FlashStringHelper *ifsh) 70 | { 71 | return ArduinoFakeInstance(Print, this)->println(ifsh); 72 | } 73 | 74 | size_t Print::println(void) 75 | { 76 | return ArduinoFakeInstance(Print, this)->println(); 77 | } 78 | 79 | size_t Print::println(const String &s) 80 | { 81 | return ArduinoFakeInstance(Print, this)->println(s); 82 | } 83 | 84 | size_t Print::println(const char c[]) 85 | { 86 | return ArduinoFakeInstance(Print, this)->println(c); 87 | } 88 | 89 | size_t Print::println(char c) 90 | { 91 | return ArduinoFakeInstance(Print, this)->println(c); 92 | } 93 | 94 | size_t Print::println(unsigned char b, int base) 95 | { 96 | return ArduinoFakeInstance(Print, this)->println(b, base); 97 | } 98 | 99 | size_t Print::println(int num, int base) 100 | { 101 | return ArduinoFakeInstance(Print, this)->println(num, base); 102 | } 103 | 104 | size_t Print::println(unsigned int num, int base) 105 | { 106 | return ArduinoFakeInstance(Print, this)->println(num, base); 107 | } 108 | 109 | size_t Print::println(long num, int base) 110 | { 111 | return ArduinoFakeInstance(Print, this)->println(num, base); 112 | } 113 | 114 | size_t Print::println(unsigned long num, int base) 115 | { 116 | return ArduinoFakeInstance(Print, this)->println(num, base); 117 | } 118 | 119 | size_t Print::println(double num, int digits) 120 | { 121 | return ArduinoFakeInstance(Print, this)->println(num, digits); 122 | } 123 | 124 | size_t Print::println(const Printable& x) 125 | { 126 | return ArduinoFakeInstance(Print, this)->println(x); 127 | } 128 | -------------------------------------------------------------------------------- /src/PrintFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "arduino/Print.h" 5 | 6 | struct PrintFake 7 | { 8 | virtual size_t write(const uint8_t *buffer, size_t size) = 0; 9 | virtual size_t write(uint8_t) = 0; 10 | 11 | virtual size_t print(const __FlashStringHelper *) = 0; 12 | virtual size_t print(const String &) = 0; 13 | virtual size_t print(const char[]) = 0; 14 | virtual size_t print(char) = 0; 15 | virtual size_t print(unsigned char, int = DEC) = 0; 16 | virtual size_t print(int, int = DEC) = 0; 17 | virtual size_t print(unsigned int, int = DEC) = 0; 18 | virtual size_t print(long, int = DEC) = 0; 19 | virtual size_t print(unsigned long, int = DEC) = 0; 20 | virtual size_t print(double, int = 2) = 0; 21 | virtual size_t print(const Printable&) = 0; 22 | 23 | virtual size_t println(const __FlashStringHelper *) = 0; 24 | virtual size_t println(const String &s) = 0; 25 | virtual size_t println(const char[]) = 0; 26 | virtual size_t println(char) = 0; 27 | virtual size_t println(unsigned char, int = DEC) = 0; 28 | virtual size_t println(int, int = DEC) = 0; 29 | virtual size_t println(unsigned int, int = DEC) = 0; 30 | virtual size_t println(long, int = DEC) = 0; 31 | virtual size_t println(unsigned long, int = DEC) = 0; 32 | virtual size_t println(double, int = 2) = 0; 33 | virtual size_t println(const Printable&) = 0; 34 | virtual size_t println(void) = 0; 35 | }; 36 | 37 | class PrintFakeProxy : public Print 38 | { 39 | private: 40 | PrintFake* printFake; 41 | 42 | public: 43 | PrintFakeProxy(PrintFake* fake) 44 | { 45 | printFake = fake; 46 | } 47 | 48 | size_t write(const uint8_t *buffer, size_t size) 49 | { 50 | return printFake->write(buffer, size); 51 | } 52 | 53 | size_t write(uint8_t value) 54 | { 55 | return printFake->write(value); 56 | } 57 | 58 | PrintFake* getPrintFake() 59 | { 60 | return printFake; 61 | } 62 | }; 63 | -------------------------------------------------------------------------------- /src/SPI.h: -------------------------------------------------------------------------------- 1 | #include "SPIFake.h" -------------------------------------------------------------------------------- /src/SPIFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "SPIFake.h" 3 | 4 | void SPIClass::begin() { ArduinoFakeInstance(SPI)->begin(); }; 5 | 6 | void SPIClass::end() { ArduinoFakeInstance(SPI)->end(); }; 7 | 8 | void SPIClass::beginTransaction(SPISettings settings) { 9 | ArduinoFakeInstance(SPI)->beginTransaction(settings); 10 | }; 11 | 12 | void SPIClass::endTransaction(void) { 13 | ArduinoFakeInstance(SPI)->endTransaction(); 14 | }; 15 | 16 | uint8_t SPIClass::transfer(uint8_t data) { 17 | return ArduinoFakeInstance(SPI)->transfer(data); 18 | }; 19 | 20 | void SPIClass::transfer(void *buf, size_t count) { 21 | return ArduinoFakeInstance(SPI)->transfer(buf, count); 22 | }; 23 | 24 | SPIClass SPI = SPIFakeProxy(ArduinoFakeInstance(SPI)); 25 | -------------------------------------------------------------------------------- /src/SPIFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "arduino/SPI.h" 5 | 6 | struct SPIFake { 7 | virtual uint8_t transfer(uint8_t data) = 0; 8 | virtual void transfer(void *buf, size_t count) = 0; 9 | 10 | virtual void beginTransaction(SPISettings settings) = 0; 11 | virtual void endTransaction(void) = 0; 12 | 13 | virtual void begin() = 0; 14 | virtual void end() = 0; 15 | }; 16 | 17 | class SPIFakeProxy : public SPIClass { 18 | private: 19 | SPIFake *spiFake; 20 | 21 | public: 22 | SPIFakeProxy(SPIFake *fake) { spiFake = fake; } 23 | 24 | SPIFake *getSPIFake() { return spiFake; } 25 | }; 26 | -------------------------------------------------------------------------------- /src/Serial.h: -------------------------------------------------------------------------------- 1 | #include "SerialFake.h" 2 | -------------------------------------------------------------------------------- /src/SerialFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "SerialFake.h" 3 | 4 | #if defined(USBCON) 5 | 6 | void Serial_::begin(unsigned long baud_count) 7 | { 8 | ArduinoFakeInstance(Serial, this)->begin(baud_count); 9 | } 10 | 11 | void Serial_::begin(unsigned long baud_count, byte config) 12 | { 13 | ArduinoFakeInstance(Serial, this)->begin(baud_count, config); 14 | } 15 | 16 | void Serial_::end(void) 17 | { 18 | ArduinoFakeInstance(Serial, this)->end(); 19 | } 20 | 21 | int Serial_::available(void) 22 | { 23 | return ArduinoFakeInstance(Serial, this)->available(); 24 | } 25 | 26 | int Serial_::peek(void) 27 | { 28 | return ArduinoFakeInstance(Serial, this)->peek(); 29 | } 30 | 31 | int Serial_::read(void) 32 | { 33 | return ArduinoFakeInstance(Serial, this)->read(); 34 | } 35 | 36 | int Serial_::availableForWrite(void) 37 | { 38 | return ArduinoFakeInstance(Serial, this)->availableForWrite(); 39 | } 40 | 41 | void Serial_::flush(void) 42 | { 43 | ArduinoFakeInstance(Serial, this)->flush(); 44 | } 45 | 46 | size_t Serial_::write(uint8_t c) 47 | { 48 | return ArduinoFakeInstance(Serial, this)->write(c); 49 | } 50 | 51 | size_t Serial_::write(const uint8_t *buffer, size_t size) 52 | { 53 | return ArduinoFakeInstance(Serial, this)->write(buffer, size); 54 | } 55 | 56 | uint32_t Serial_::baud() 57 | { 58 | return ArduinoFakeInstance(Serial, this)->baud(); 59 | } 60 | 61 | uint8_t Serial_::stopbits() 62 | { 63 | return ArduinoFakeInstance(Serial, this)->stopbits(); 64 | } 65 | 66 | uint8_t Serial_::paritytype() 67 | { 68 | return ArduinoFakeInstance(Serial, this)->paritytype(); 69 | } 70 | 71 | uint8_t Serial_::numbits() 72 | { 73 | return ArduinoFakeInstance(Serial, this)->numbits(); 74 | } 75 | 76 | bool Serial_::dtr() 77 | { 78 | return ArduinoFakeInstance(Serial, this)->dtr(); 79 | } 80 | 81 | bool Serial_::rts() 82 | { 83 | return ArduinoFakeInstance(Serial, this)->rts(); 84 | } 85 | 86 | int32_t Serial_::readBreak() 87 | { 88 | return ArduinoFakeInstance(Serial, this)->readBreak(); 89 | } 90 | 91 | Serial_ Serial = SerialFakeProxy(ArduinoFakeInstance(Serial)); 92 | 93 | #endif // USBCON -------------------------------------------------------------------------------- /src/SerialFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "StreamFake.h" 5 | #include "arduino/USBAPI.h" 6 | 7 | struct SerialFake : public StreamFake 8 | { 9 | virtual void begin(unsigned long) = 0; 10 | virtual void begin(unsigned long, uint8_t) = 0; 11 | virtual void end(void) = 0; 12 | 13 | virtual int available(void) = 0; 14 | virtual int peek(void) = 0; 15 | virtual int read(void) = 0; 16 | virtual int availableForWrite(void) = 0; 17 | virtual void flush(void) = 0; 18 | virtual size_t write(uint8_t) = 0; 19 | virtual size_t write(const uint8_t*, size_t) = 0; 20 | 21 | //operator bool(); 22 | 23 | virtual int32_t readBreak() = 0; 24 | 25 | virtual uint32_t baud() = 0; 26 | virtual uint8_t stopbits() = 0; 27 | virtual uint8_t paritytype() = 0; 28 | virtual uint8_t numbits() = 0; 29 | virtual bool dtr() = 0; 30 | virtual bool rts() = 0; 31 | }; 32 | 33 | class SerialFakeProxy : public StreamFakeProxy, public Serial_ 34 | { 35 | private: 36 | SerialFake* serialFake; 37 | 38 | public: 39 | SerialFakeProxy(SerialFake* fake) : StreamFakeProxy(fake) 40 | { 41 | serialFake = fake; 42 | } 43 | 44 | SerialFake* getSerialFake() 45 | { 46 | return serialFake; 47 | } 48 | }; -------------------------------------------------------------------------------- /src/Stream.h: -------------------------------------------------------------------------------- 1 | #include "StreamFake.h" 2 | -------------------------------------------------------------------------------- /src/StreamFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "StreamFake.h" 3 | 4 | void Stream::setTimeout(unsigned long timeout) 5 | { 6 | ArduinoFakeInstance(Stream, this)->setTimeout(timeout); 7 | } 8 | 9 | bool Stream::find(char *target) 10 | { 11 | return ArduinoFakeInstance(Stream, this)->find(target); 12 | } 13 | 14 | bool Stream::find(char *target, size_t length) 15 | { 16 | return ArduinoFakeInstance(Stream, this)->find(target, length); 17 | } 18 | 19 | bool Stream::findUntil(char *target, char *terminator) 20 | { 21 | return ArduinoFakeInstance(Stream, this)->findUntil(target, terminator); 22 | } 23 | 24 | bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) 25 | { 26 | return ArduinoFakeInstance(Stream, this)->findUntil(target, targetLen, terminator, termLen); 27 | } 28 | 29 | long Stream::parseInt(LookaheadMode lookahead, char ignore) 30 | { 31 | return ArduinoFakeInstance(Stream, this)->parseInt(lookahead, ignore); 32 | } 33 | 34 | float Stream::parseFloat(LookaheadMode lookahead, char ignore) 35 | { 36 | return ArduinoFakeInstance(Stream, this)->parseFloat(lookahead, ignore); 37 | } 38 | 39 | size_t Stream::readBytes(char *buffer, size_t length) 40 | { 41 | return ArduinoFakeInstance(Stream, this)->readBytes(buffer, length); 42 | } 43 | 44 | size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) 45 | { 46 | return ArduinoFakeInstance(Stream, this)->readBytesUntil(terminator, buffer, length); 47 | } 48 | 49 | String Stream::readString() 50 | { 51 | return ArduinoFakeInstance(Stream, this)->readString(); 52 | } 53 | 54 | String Stream::readStringUntil(char terminator) 55 | { 56 | return ArduinoFakeInstance(Stream, this)->readStringUntil(terminator); 57 | } 58 | -------------------------------------------------------------------------------- /src/StreamFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ArduinoFake.h" 4 | #include "PrintFake.h" 5 | #include "arduino/Stream.h" 6 | 7 | #ifndef NO_IGNORE_CHAR 8 | #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field 9 | #endif 10 | 11 | struct StreamFake : public PrintFake 12 | { 13 | virtual int available() = 0; 14 | virtual int read() = 0; 15 | virtual int peek() = 0; 16 | virtual void flush() = 0; 17 | 18 | virtual void setTimeout(unsigned long timeout) = 0; 19 | virtual unsigned long getTimeout(void) = 0; 20 | 21 | virtual bool find(char *target) = 0; 22 | 23 | virtual bool find(char *target, size_t length) = 0; 24 | 25 | virtual bool findUntil(char *target, char *terminator) = 0; 26 | 27 | virtual bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen) = 0; 28 | 29 | virtual long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR) = 0; 30 | 31 | virtual float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR) = 0; 32 | 33 | virtual size_t readBytes(char *buffer, size_t length) = 0; 34 | 35 | virtual size_t readBytesUntil(char terminator, char *buffer, size_t length) = 0; 36 | 37 | virtual String readString() = 0; 38 | virtual String readStringUntil(char terminator) = 0; 39 | }; 40 | 41 | class StreamFakeProxy : public Stream, public PrintFakeProxy 42 | { 43 | private: 44 | StreamFake* streamFake; 45 | 46 | public: 47 | StreamFakeProxy(StreamFake* fake) : PrintFakeProxy(fake) 48 | { 49 | streamFake = fake; 50 | } 51 | 52 | size_t write(uint8_t value) 53 | { 54 | return streamFake->write(value); 55 | } 56 | 57 | int available() 58 | { 59 | return streamFake->available(); 60 | } 61 | 62 | int read() 63 | { 64 | return streamFake->read(); 65 | } 66 | 67 | int peek() 68 | { 69 | return streamFake->peek(); 70 | } 71 | 72 | void flush() 73 | { 74 | streamFake->flush(); 75 | } 76 | 77 | StreamFake* getStreamFake() 78 | { 79 | return streamFake; 80 | } 81 | }; -------------------------------------------------------------------------------- /src/WString.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "arduino/WString.h" -------------------------------------------------------------------------------- /src/Wire.h: -------------------------------------------------------------------------------- 1 | #include "WireFake.h" -------------------------------------------------------------------------------- /src/WireFake.cpp: -------------------------------------------------------------------------------- 1 | #include "ArduinoFake.h" 2 | #include "WireFake.h" 3 | 4 | void TwoWire::begin(void) { ArduinoFakeInstance(Wire, this)->begin(); } 5 | 6 | void TwoWire::begin(uint8_t address) { 7 | 8 | ArduinoFakeInstance(Wire, this)->begin(address); 9 | } 10 | 11 | void TwoWire::begin(int address) { 12 | ArduinoFakeInstance(Wire, this)->begin(address); 13 | } 14 | 15 | void TwoWire::end(void) { ArduinoFakeInstance(Wire, this)->end(); } 16 | 17 | void TwoWire::setClock(uint32_t clock) { 18 | ArduinoFakeInstance(Wire, this)->setClock(clock); 19 | } 20 | 21 | void TwoWire::setWireTimeout(uint32_t timeout, bool reset_with_timeout) { 22 | ArduinoFakeInstance(Wire, this)->setWireTimeout(timeout, reset_with_timeout); 23 | } 24 | 25 | bool TwoWire::getWireTimeoutFlag(void) { 26 | return ArduinoFakeInstance(Wire, this)->getWireTimeoutFlag(); 27 | } 28 | 29 | void TwoWire::clearWireTimeoutFlag(void) { 30 | ArduinoFakeInstance(Wire, this)->clearWireTimeoutFlag(); 31 | } 32 | 33 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, 34 | uint32_t iaddress, uint8_t isize, 35 | uint8_t sendStop) { 36 | return ArduinoFakeInstance(Wire, this) 37 | ->requestFrom(address, quantity, iaddress, isize, sendStop); 38 | } 39 | 40 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, 41 | uint8_t sendStop) { 42 | return ArduinoFakeInstance(Wire, this) 43 | ->requestFrom(address, quantity, sendStop); 44 | } 45 | 46 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { 47 | return ArduinoFakeInstance(Wire, this)->requestFrom(address, quantity); 48 | } 49 | 50 | void TwoWire::beginTransmission(uint8_t address) { 51 | ArduinoFakeInstance(Wire, this)->beginTransmission(address); 52 | } 53 | 54 | void TwoWire::beginTransmission(int address) { 55 | ArduinoFakeInstance(Wire, this)->beginTransmission(address); 56 | } 57 | 58 | uint8_t TwoWire::endTransmission(bool sendStop) { 59 | 60 | return ArduinoFakeInstance(Wire, this)->endTransmission(sendStop); 61 | } 62 | 63 | uint8_t TwoWire::endTransmission(void) { 64 | return ArduinoFakeInstance(Wire, this)->endTransmission(); 65 | } 66 | 67 | size_t TwoWire::write(uint8_t data) { 68 | return ArduinoFakeInstance(Wire, this)->write(data); 69 | } 70 | 71 | size_t TwoWire::write(const uint8_t *data, size_t quantity) { 72 | return ArduinoFakeInstance(Wire, this)->write(data, quantity); 73 | } 74 | 75 | int TwoWire::available(void) { 76 | return ArduinoFakeInstance(Wire, this)->available(); 77 | } 78 | 79 | int TwoWire::read(void) { return ArduinoFakeInstance(Wire, this)->read(); } 80 | 81 | int TwoWire::peek(void) { return ArduinoFakeInstance(Wire, this)->peek(); } 82 | 83 | void TwoWire::flush(void) { ArduinoFakeInstance(Wire, this)->flush(); } 84 | 85 | void TwoWire::onReceive(void (*function)(int)) { 86 | ArduinoFakeInstance(Wire, this)->onReceive(function); 87 | } 88 | 89 | void TwoWire::onRequest(void (*function)(void)) { 90 | ArduinoFakeInstance(Wire, this)->onRequest(function); 91 | } 92 | 93 | TwoWire Wire = WireFakeProxy(ArduinoFakeInstance(Wire)); -------------------------------------------------------------------------------- /src/WireFake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ArduinoFake.h" 3 | #include "StreamFake.h" 4 | #include "arduino/Wire.h" 5 | 6 | struct WireFake : public StreamFake { 7 | virtual void begin() = 0; 8 | virtual void begin(uint8_t) = 0; 9 | virtual void begin(int) = 0; 10 | virtual void end() = 0; 11 | virtual void setClock(uint32_t) = 0; 12 | virtual void setWireTimeout(uint32_t timeout = 25000, 13 | bool reset_with_timeout = false) = 0; 14 | virtual bool getWireTimeoutFlag(void) = 0; 15 | virtual void clearWireTimeoutFlag(void) = 0; 16 | virtual void beginTransmission(uint8_t) = 0; 17 | virtual void beginTransmission(int) = 0; 18 | virtual uint8_t endTransmission(void) = 0; 19 | virtual uint8_t endTransmission(bool) = 0; 20 | virtual uint8_t requestFrom(uint8_t, uint8_t) = 0; 21 | virtual uint8_t requestFrom(uint8_t, uint8_t, uint8_t) = 0; 22 | virtual uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t) = 0; 23 | virtual size_t write(uint8_t) = 0; 24 | virtual size_t write(const uint8_t *, size_t) = 0; 25 | virtual int available(void) = 0; 26 | virtual int read(void) = 0; 27 | virtual int peek(void) = 0; 28 | virtual void flush(void) = 0; 29 | virtual void onReceive(void (*)(int)) = 0; 30 | virtual void onRequest(void (*)(void)) = 0; 31 | }; 32 | 33 | class WireFakeProxy : public StreamFakeProxy, public TwoWire { 34 | private: 35 | WireFake *wireFake; 36 | 37 | public: 38 | WireFakeProxy(WireFake *fake) : StreamFakeProxy(fake) { wireFake = fake; } 39 | 40 | WireFake *getWireFake() { return wireFake; } 41 | }; -------------------------------------------------------------------------------- /src/arduino/Arduino.h: -------------------------------------------------------------------------------- 1 | /* 2 | Arduino.h - Main include file for the Arduino SDK 3 | Copyright (c) 2005-2013 Arduino Team. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Arduino_h 21 | #define Arduino_h 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "avr/interrupt.h" 29 | 30 | 31 | #include "binary.h" 32 | 33 | #ifdef __cplusplus 34 | extern "C"{ 35 | #endif 36 | 37 | void yield(void); 38 | 39 | #define HIGH 0x1 40 | #define LOW 0x0 41 | 42 | #define INPUT 0x0 43 | #define OUTPUT 0x1 44 | #define INPUT_PULLUP 0x2 45 | #define INPUT_PULLDOWN 0x3 46 | 47 | #define PI 3.1415926535897932384626433832795 48 | #define HALF_PI 1.5707963267948966192313216916398 49 | #define TWO_PI 6.283185307179586476925286766559 50 | #define DEG_TO_RAD 0.017453292519943295769236907684886 51 | #define RAD_TO_DEG 57.295779513082320876798154814105 52 | #define EULER 2.718281828459045235360287471352 53 | 54 | #define SERIAL 0x0 55 | #define DISPLAY 0x1 56 | 57 | #define LSBFIRST 0 58 | #define MSBFIRST 1 59 | 60 | #define CHANGE 1 61 | #define FALLING 2 62 | #define RISING 3 63 | 64 | #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) 65 | #define DEFAULT 0 66 | #define EXTERNAL 1 67 | #define INTERNAL1V1 2 68 | #define INTERNAL INTERNAL1V1 69 | #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) 70 | #define DEFAULT 0 71 | #define EXTERNAL 4 72 | #define INTERNAL1V1 8 73 | #define INTERNAL INTERNAL1V1 74 | #define INTERNAL2V56 9 75 | #define INTERNAL2V56_EXTCAP 13 76 | #else 77 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) 78 | #define INTERNAL1V1 2 79 | #define INTERNAL2V56 3 80 | #else 81 | #define INTERNAL 3 82 | #endif 83 | #define DEFAULT 1 84 | #define EXTERNAL 0 85 | #endif 86 | 87 | // undefine stdlib's abs if encountered 88 | #ifdef abs 89 | #undef abs 90 | #endif 91 | 92 | #ifndef __cplusplus 93 | #define min(a,b) ((a)<(b)?(a):(b)) 94 | #define max(a,b) ((a)>(b)?(a):(b)) 95 | #endif 96 | #define abs(x) ((x)>0?(x):-(x)) 97 | #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) 98 | #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) 99 | #define radians(deg) ((deg)*DEG_TO_RAD) 100 | #define degrees(rad) ((rad)*RAD_TO_DEG) 101 | #define sq(x) ((x)*(x)) 102 | 103 | #define interrupts() sei() 104 | #define noInterrupts() cli() 105 | 106 | #define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) 107 | #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) 108 | #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) 109 | 110 | #define lowByte(w) ((uint8_t) ((w) & 0xff)) 111 | #define highByte(w) ((uint8_t) ((w) >> 8)) 112 | 113 | #define bitRead(value, bit) (((value) >> (bit)) & 0x01) 114 | #define bitSet(value, bit) ((value) |= (1UL << (bit))) 115 | #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) 116 | #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) 117 | 118 | // avr-libc defines _NOP() since 1.6.2 119 | #ifndef _NOP 120 | #define _NOP() do { __asm__ volatile ("nop"); } while (0) 121 | #endif 122 | 123 | typedef unsigned int word; 124 | 125 | #define bit(b) (1UL << (b)) 126 | 127 | typedef bool boolean; 128 | typedef uint8_t byte; 129 | 130 | void init(void); 131 | void initVariant(void); 132 | 133 | void yield(void); 134 | 135 | int atexit(void (*func)()) __attribute__((weak)); 136 | 137 | void pinMode(uint8_t, uint8_t); 138 | void digitalWrite(uint8_t, uint8_t); 139 | int digitalRead(uint8_t); 140 | int analogRead(uint8_t); 141 | void analogReference(uint8_t mode); 142 | void analogReadResolution(uint8_t mode); 143 | void analogWrite(uint8_t, int); 144 | 145 | unsigned long millis(void); 146 | unsigned long micros(void); 147 | void delay(unsigned long); 148 | void delayMicroseconds(unsigned int us); 149 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); 150 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); 151 | 152 | void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); 153 | uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); 154 | 155 | void attachInterrupt(uint8_t, void (*)(void), int mode); 156 | void detachInterrupt(uint8_t); 157 | 158 | void setup(void); 159 | void loop(void); 160 | 161 | // Get the bit location within the hardware port of the given virtual pin. 162 | // This comes from the pins_*.c file for the active board configuration. 163 | 164 | #define analogInPinToBit(P) (P) 165 | 166 | // Get the bit location within the hardware port of the given virtual pin. 167 | // This comes from the pins_*.c file for the active board configuration. 168 | // 169 | // These perform slightly better as macros compared to inline functions 170 | // 171 | #define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) ) 172 | #define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) ) 173 | #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) ) 174 | #define analogInPinToBit(P) (P) 175 | #define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) ) 176 | #define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) ) 177 | #define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) ) 178 | 179 | #define NOT_A_PIN 0 180 | #define NOT_A_PORT 0 181 | 182 | #define NOT_AN_INTERRUPT -1 183 | 184 | #ifdef ARDUINO_MAIN 185 | #define PA 1 186 | #define PB 2 187 | #define PC 3 188 | #define PD 4 189 | #define PE 5 190 | #define PF 6 191 | #define PG 7 192 | #define PH 8 193 | #define PJ 10 194 | #define PK 11 195 | #define PL 12 196 | #endif 197 | 198 | #define NOT_ON_TIMER 0 199 | #define TIMER0A 1 200 | #define TIMER0B 2 201 | #define TIMER1A 3 202 | #define TIMER1B 4 203 | #define TIMER1C 5 204 | #define TIMER2 6 205 | #define TIMER2A 7 206 | #define TIMER2B 8 207 | 208 | #define TIMER3A 9 209 | #define TIMER3B 10 210 | #define TIMER3C 11 211 | #define TIMER4A 12 212 | #define TIMER4B 13 213 | #define TIMER4C 14 214 | #define TIMER4D 15 215 | #define TIMER5A 16 216 | #define TIMER5B 17 217 | #define TIMER5C 18 218 | 219 | #ifdef __cplusplus 220 | } // extern "C" 221 | #endif 222 | 223 | #ifdef __cplusplus 224 | #include "WCharacter.h" 225 | #include "WString.h" 226 | #include "HardwareSerial.h" 227 | #include "USBAPI.h" 228 | #if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL) 229 | #error "Targets with both UART0 and CDC serial not supported" 230 | #endif 231 | 232 | uint16_t makeWord(uint16_t w); 233 | uint16_t makeWord(byte h, byte l); 234 | 235 | #define word(...) makeWord(__VA_ARGS__) 236 | 237 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); 238 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); 239 | 240 | void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); 241 | void noTone(uint8_t _pin); 242 | 243 | // WMath prototypes 244 | long random(long); 245 | long random(long, long); 246 | void randomSeed(unsigned long); 247 | long map(long, long, long, long, long); 248 | 249 | #endif 250 | 251 | #include "pins_arduino.h" 252 | 253 | #endif 254 | -------------------------------------------------------------------------------- /src/arduino/Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Client.h - Base class that provides Client 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef client_h 21 | #define client_h 22 | #include "Print.h" 23 | #include "Stream.h" 24 | #include "IPAddress.h" 25 | 26 | class Client : public Stream { 27 | 28 | public: 29 | virtual int connect(IPAddress ip, uint16_t port) =0; 30 | virtual int connect(const char *host, uint16_t port) =0; 31 | virtual size_t write(uint8_t) =0; 32 | virtual size_t write(const uint8_t *buf, size_t size) =0; 33 | virtual int available() = 0; 34 | virtual int read() = 0; 35 | virtual int read(uint8_t *buf, size_t size) = 0; 36 | virtual int peek() = 0; 37 | virtual void flush() = 0; 38 | virtual void stop() = 0; 39 | virtual uint8_t connected() = 0; 40 | virtual operator bool() = 0; 41 | protected: 42 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/arduino/EEPROM.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | struct EEPROMClass { 5 | virtual uint8_t read(int idx); 6 | virtual void write(int idx, uint8_t val); 7 | virtual void update(int idx, uint8_t val); 8 | virtual uint16_t length(); 9 | /* 10 | EERef operator[](const int idx); 11 | 12 | // TODO: How to implement this functionality?? 13 | // https://docs.arduino.cc/learn/built-in-libraries/eeprom 14 | template T &get(int idx, T &t); 15 | 16 | template const T &put(int idx, const T &t); 17 | */ 18 | }; 19 | 20 | extern EEPROMClass EEPROM; -------------------------------------------------------------------------------- /src/arduino/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | HardwareSerial.h - Hardware serial library for Wiring 3 | Copyright (c) 2006 Nicholas Zambetti. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | Modified 28 September 2010 by Mark Sproul 20 | Modified 14 August 2012 by Alarus 21 | Modified 3 December 2013 by Matthijs Kooijman 22 | */ 23 | 24 | #ifndef HardwareSerial_h 25 | #define HardwareSerial_h 26 | 27 | #include 28 | 29 | #include "Stream.h" 30 | 31 | // Define constants and variables for buffering incoming serial data. We're 32 | // using a ring buffer (I think), in which head is the index of the location 33 | // to which to write the next incoming character and tail is the index of the 34 | // location from which to read. 35 | // NOTE: a "power of 2" buffer size is reccomended to dramatically 36 | // optimize all the modulo operations for ring buffers. 37 | // WARNING: When buffer sizes are increased to > 256, the buffer index 38 | // variables are automatically increased in size, but the extra 39 | // atomicity guards needed for that are not implemented. This will 40 | // often work, but occasionally a race condition can occur that makes 41 | // Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405 42 | #if !defined(SERIAL_TX_BUFFER_SIZE) 43 | #if ((RAMEND - RAMSTART) < 1023) 44 | #define SERIAL_TX_BUFFER_SIZE 16 45 | #else 46 | #define SERIAL_TX_BUFFER_SIZE 64 47 | #endif 48 | #endif 49 | #if !defined(SERIAL_RX_BUFFER_SIZE) 50 | #if ((RAMEND - RAMSTART) < 1023) 51 | #define SERIAL_RX_BUFFER_SIZE 16 52 | #else 53 | #define SERIAL_RX_BUFFER_SIZE 64 54 | #endif 55 | #endif 56 | #if (SERIAL_TX_BUFFER_SIZE>256) 57 | typedef uint16_t tx_buffer_index_t; 58 | #else 59 | typedef uint8_t tx_buffer_index_t; 60 | #endif 61 | #if (SERIAL_RX_BUFFER_SIZE>256) 62 | typedef uint16_t rx_buffer_index_t; 63 | #else 64 | typedef uint8_t rx_buffer_index_t; 65 | #endif 66 | 67 | // Define config for Serial.begin(baud, config); 68 | #define SERIAL_5N1 0x00 69 | #define SERIAL_6N1 0x02 70 | #define SERIAL_7N1 0x04 71 | #define SERIAL_8N1 0x06 72 | #define SERIAL_5N2 0x08 73 | #define SERIAL_6N2 0x0A 74 | #define SERIAL_7N2 0x0C 75 | #define SERIAL_8N2 0x0E 76 | #define SERIAL_5E1 0x20 77 | #define SERIAL_6E1 0x22 78 | #define SERIAL_7E1 0x24 79 | #define SERIAL_8E1 0x26 80 | #define SERIAL_5E2 0x28 81 | #define SERIAL_6E2 0x2A 82 | #define SERIAL_7E2 0x2C 83 | #define SERIAL_8E2 0x2E 84 | #define SERIAL_5O1 0x30 85 | #define SERIAL_6O1 0x32 86 | #define SERIAL_7O1 0x34 87 | #define SERIAL_8O1 0x36 88 | #define SERIAL_5O2 0x38 89 | #define SERIAL_6O2 0x3A 90 | #define SERIAL_7O2 0x3C 91 | #define SERIAL_8O2 0x3E 92 | 93 | class HardwareSerial : public Stream 94 | { 95 | protected: 96 | volatile uint8_t * const _ubrrh; 97 | volatile uint8_t * const _ubrrl; 98 | volatile uint8_t * const _ucsra; 99 | volatile uint8_t * const _ucsrb; 100 | volatile uint8_t * const _ucsrc; 101 | volatile uint8_t * const _udr; 102 | // Has any byte been written to the UART since begin() 103 | bool _written; 104 | 105 | volatile rx_buffer_index_t _rx_buffer_head; 106 | volatile rx_buffer_index_t _rx_buffer_tail; 107 | volatile tx_buffer_index_t _tx_buffer_head; 108 | volatile tx_buffer_index_t _tx_buffer_tail; 109 | 110 | // Don't put any members after these buffers, since only the first 111 | // 32 bytes of this struct can be accessed quickly using the ldd 112 | // instruction. 113 | unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; 114 | unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; 115 | 116 | public: 117 | inline HardwareSerial( 118 | volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, 119 | volatile uint8_t *ucsra, volatile uint8_t *ucsrb, 120 | volatile uint8_t *ucsrc, volatile uint8_t *udr); 121 | void begin(unsigned long baud) { begin(baud, SERIAL_8N1); } 122 | void begin(unsigned long, uint8_t); 123 | void end(); 124 | virtual int available(void); 125 | virtual int peek(void); 126 | virtual int read(void); 127 | int availableForWrite(void); 128 | virtual void flush(void); 129 | virtual size_t write(uint8_t); 130 | inline size_t write(unsigned long n) { return write((uint8_t)n); } 131 | inline size_t write(long n) { return write((uint8_t)n); } 132 | inline size_t write(unsigned int n) { return write((uint8_t)n); } 133 | inline size_t write(int n) { return write((uint8_t)n); } 134 | using Print::write; // pull in write(str) and write(buf, size) from Print 135 | operator bool() { return true; } 136 | 137 | // Interrupt handlers - Not intended to be called externally 138 | inline void _rx_complete_irq(void); 139 | void _tx_udr_empty_irq(void); 140 | }; 141 | 142 | #if defined(UBRRH) || defined(UBRR0H) 143 | extern HardwareSerial Serial; 144 | #define HAVE_HWSERIAL0 145 | #endif 146 | #if defined(UBRR1H) 147 | extern HardwareSerial Serial1; 148 | #define HAVE_HWSERIAL1 149 | #endif 150 | #if defined(UBRR2H) 151 | extern HardwareSerial Serial2; 152 | #define HAVE_HWSERIAL2 153 | #endif 154 | #if defined(UBRR3H) 155 | extern HardwareSerial Serial3; 156 | #define HAVE_HWSERIAL3 157 | #endif 158 | 159 | extern void serialEventRun(void) __attribute__((weak)); 160 | 161 | #endif 162 | -------------------------------------------------------------------------------- /src/arduino/IPAddress.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | IPAddress.cpp - Base class that provides IPAddress 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "Arduino.h" 21 | #include "IPAddress.h" 22 | 23 | IPAddress::IPAddress() 24 | { 25 | _address.dword = 0; 26 | } 27 | 28 | IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) 29 | { 30 | _address.bytes[0] = first_octet; 31 | _address.bytes[1] = second_octet; 32 | _address.bytes[2] = third_octet; 33 | _address.bytes[3] = fourth_octet; 34 | } 35 | 36 | IPAddress::IPAddress(uint32_t address) 37 | { 38 | _address.dword = address; 39 | } 40 | 41 | IPAddress::IPAddress(const uint8_t *address) 42 | { 43 | memcpy(_address.bytes, address, sizeof(_address.bytes)); 44 | } 45 | 46 | bool IPAddress::fromString(const char *address) 47 | { 48 | uint16_t acc = 0; // Accumulator 49 | uint8_t dots = 0; 50 | 51 | while (*address) 52 | { 53 | char c = *address++; 54 | if (c >= '0' && c <= '9') 55 | { 56 | acc = acc * 10 + (c - '0'); 57 | if (acc > 255) { 58 | // Value out of [0..255] range 59 | return false; 60 | } 61 | } 62 | else if (c == '.') 63 | { 64 | if (dots == 3) { 65 | // Too much dots (there must be 3 dots) 66 | return false; 67 | } 68 | _address.bytes[dots++] = acc; 69 | acc = 0; 70 | } 71 | else 72 | { 73 | // Invalid char 74 | return false; 75 | } 76 | } 77 | 78 | if (dots != 3) { 79 | // Too few dots (there must be 3 dots) 80 | return false; 81 | } 82 | _address.bytes[3] = acc; 83 | return true; 84 | } 85 | 86 | IPAddress& IPAddress::operator=(const uint8_t *address) 87 | { 88 | memcpy(_address.bytes, address, sizeof(_address.bytes)); 89 | return *this; 90 | } 91 | 92 | IPAddress& IPAddress::operator=(uint32_t address) 93 | { 94 | _address.dword = address; 95 | return *this; 96 | } 97 | 98 | bool IPAddress::operator==(const uint8_t* addr) const 99 | { 100 | return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; 101 | } 102 | 103 | size_t IPAddress::printTo(Print& p) const 104 | { 105 | size_t n = 0; 106 | for (int i =0; i < 3; i++) 107 | { 108 | n += p.print(_address.bytes[i], DEC); 109 | n += p.print('.'); 110 | } 111 | n += p.print(_address.bytes[3], DEC); 112 | return n; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /src/arduino/IPAddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | IPAddress.h - Base class that provides IPAddress 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef IPAddress_h 21 | #define IPAddress_h 22 | 23 | #include 24 | #include "Printable.h" 25 | #include "WString.h" 26 | 27 | // A class to make it easier to handle and pass around IP addresses 28 | 29 | class IPAddress : public Printable { 30 | private: 31 | union { 32 | uint8_t bytes[4]; // IPv4 address 33 | uint32_t dword; 34 | } _address; 35 | 36 | // Access the raw byte array containing the address. Because this returns a pointer 37 | // to the internal structure rather than a copy of the address this function should only 38 | // be used when you know that the usage of the returned uint8_t* will be transient and not 39 | // stored. 40 | uint8_t* raw_address() { return _address.bytes; }; 41 | 42 | public: 43 | // Constructors 44 | IPAddress(); 45 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); 46 | IPAddress(uint32_t address); 47 | IPAddress(const uint8_t *address); 48 | 49 | bool fromString(const char *address); 50 | bool fromString(const String &address) { return fromString(address.c_str()); } 51 | 52 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer 53 | // to a four-byte uint8_t array is expected 54 | operator uint32_t() const { return _address.dword; }; 55 | bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; 56 | bool operator==(const uint8_t* addr) const; 57 | 58 | // Overloaded index operator to allow getting and setting individual octets of the address 59 | uint8_t operator[](int index) const { return _address.bytes[index]; }; 60 | uint8_t& operator[](int index) { return _address.bytes[index]; }; 61 | 62 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types 63 | IPAddress& operator=(const uint8_t *address); 64 | IPAddress& operator=(uint32_t address); 65 | 66 | virtual size_t printTo(Print& p) const; 67 | 68 | friend class EthernetClass; 69 | friend class UDP; 70 | friend class Client; 71 | friend class Server; 72 | friend class DhcpClass; 73 | friend class DNSClient; 74 | }; 75 | 76 | const IPAddress INADDR_NONE(0,0,0,0); 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/arduino/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Print.h - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Print_h 21 | #define Print_h 22 | 23 | #include 24 | #include // for size_t 25 | 26 | #include "WString.h" 27 | #include "Printable.h" 28 | 29 | #define DEC 10 30 | #define HEX 16 31 | #define OCT 8 32 | #ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar 33 | #undef BIN 34 | #endif 35 | #define BIN 2 36 | 37 | class Print 38 | { 39 | private: 40 | int write_error; 41 | size_t printNumber(unsigned long, uint8_t); 42 | size_t printFloat(double, uint8_t); 43 | protected: 44 | void setWriteError(int err = 1) { write_error = err; } 45 | public: 46 | Print() : write_error(0) {} 47 | 48 | int getWriteError() { return write_error; } 49 | void clearWriteError() { setWriteError(0); } 50 | 51 | virtual size_t write(uint8_t) = 0; 52 | size_t write(const char *str) { 53 | if (str == NULL) return 0; 54 | return write((const uint8_t *)str, strlen(str)); 55 | } 56 | virtual size_t write(const uint8_t *buffer, size_t size); 57 | size_t write(const char *buffer, size_t size) { 58 | return write((const uint8_t *)buffer, size); 59 | } 60 | 61 | size_t print(const __FlashStringHelper *); 62 | size_t print(const String &); 63 | size_t print(const char[]); 64 | size_t print(char); 65 | size_t print(unsigned char, int = DEC); 66 | size_t print(int, int = DEC); 67 | size_t print(unsigned int, int = DEC); 68 | size_t print(long, int = DEC); 69 | size_t print(unsigned long, int = DEC); 70 | size_t print(double, int = 2); 71 | size_t print(const Printable&); 72 | 73 | size_t println(const __FlashStringHelper *); 74 | size_t println(const String &s); 75 | size_t println(const char[]); 76 | size_t println(char); 77 | size_t println(unsigned char, int = DEC); 78 | size_t println(int, int = DEC); 79 | size_t println(unsigned int, int = DEC); 80 | size_t println(long, int = DEC); 81 | size_t println(unsigned long, int = DEC); 82 | size_t println(double, int = 2); 83 | size_t println(const Printable&); 84 | size_t println(void); 85 | }; 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /src/arduino/Printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Printable.h - Interface class that allows printing of complex types 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Printable_h 21 | #define Printable_h 22 | 23 | #include 24 | 25 | class Print; 26 | 27 | /** The Printable class provides a way for new classes to allow themselves to be printed. 28 | By deriving from Printable and implementing the printTo method, it will then be possible 29 | for users to print out instances of this class by passing them into the usual 30 | Print::print and Print::println methods. 31 | */ 32 | 33 | class Printable 34 | { 35 | public: 36 | virtual size_t printTo(Print& p) const = 0; 37 | }; 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /src/arduino/README.md: -------------------------------------------------------------------------------- 1 | # Arduino 2 | 3 | Files present in this directory were extracted from [arduino](https://github.com/arduino) 4 | 5 | This directory should not be here, Arduino should be a dependency and the headers should be linked. -------------------------------------------------------------------------------- /src/arduino/SPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * Copyright (c) 2014 by Paul Stoffregen (Transaction API) 4 | * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) 5 | * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) 6 | * SPI Master library for arduino. 7 | * 8 | * This file is free software; you can redistribute it and/or modify 9 | * it under the terms of either the GNU General Public License version 2 10 | * or the GNU Lesser General Public License version 2.1, both as 11 | * published by the Free Software Foundation. 12 | */ 13 | 14 | #pragma once 15 | #include 16 | #include 17 | #include 18 | 19 | // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(), 20 | // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) 21 | #define SPI_HAS_TRANSACTION 1 22 | 23 | // SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method 24 | #define SPI_HAS_NOTUSINGINTERRUPT 1 25 | 26 | // SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version. 27 | // This way when there is a bug fix you can check this define to alert users 28 | // of your code if it uses better version of this library. 29 | // This also implies everything that SPI_HAS_TRANSACTION as documented above is 30 | // available too. 31 | #define SPI_ATOMIC_VERSION 1 32 | 33 | // Uncomment this line to add detection of mismatched begin/end transactions. 34 | // A mismatch occurs if other libraries fail to use SPI.endTransaction() for 35 | // each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn 36 | // on if any mismatch is ever detected. 37 | // #define SPI_TRANSACTION_MISMATCH_LED 5 38 | 39 | #ifndef LSBFIRST 40 | #define LSBFIRST 0 41 | #endif 42 | #ifndef MSBFIRST 43 | #define MSBFIRST 1 44 | #endif 45 | 46 | #define SPI_CLOCK_DIV4 0x00 47 | #define SPI_CLOCK_DIV16 0x01 48 | #define SPI_CLOCK_DIV64 0x02 49 | #define SPI_CLOCK_DIV128 0x03 50 | #define SPI_CLOCK_DIV2 0x04 51 | #define SPI_CLOCK_DIV8 0x05 52 | #define SPI_CLOCK_DIV32 0x06 53 | 54 | #define SPI_MODE0 0x00 55 | #define SPI_MODE1 0x04 56 | #define SPI_MODE2 0x08 57 | #define SPI_MODE3 0x0C 58 | 59 | #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR 60 | #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR 61 | #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR 62 | 63 | // define SPI_AVR_EIMSK for AVR boards with external interrupt pins 64 | #if defined(EIMSK) 65 | #define SPI_AVR_EIMSK EIMSK 66 | #elif defined(GICR) 67 | #define SPI_AVR_EIMSK GICR 68 | #elif defined(GIMSK) 69 | #define SPI_AVR_EIMSK GIMSK 70 | #endif 71 | 72 | class SPISettings { 73 | private: 74 | uint32_t clock; 75 | uint8_t bitOrder; 76 | uint8_t dataMode; 77 | 78 | public: 79 | SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode): clock(clock), bitOrder(bitOrder), dataMode(dataMode) {} 80 | SPISettings() { SPISettings(4000000, MSBFIRST, SPI_MODE0); } 81 | friend class SPIClass; 82 | 83 | bool operator==(const SPISettings &other) const { 84 | return (clock == other.clock) && (bitOrder == other.bitOrder) && 85 | (dataMode == other.dataMode); 86 | } 87 | }; 88 | 89 | class SPIClass { 90 | public: 91 | // Initialize the SPI library 92 | virtual void begin(); 93 | virtual void end(); 94 | 95 | // Before using SPI.transfer() or asserting chip select pins, 96 | // this function is used to gain exclusive access to the SPI bus 97 | // and configure the correct settings. 98 | virtual void beginTransaction(SPISettings settings); 99 | 100 | // Write to the SPI bus (MOSI pin) and also receive (MISO pin) 101 | virtual uint8_t transfer(uint8_t data); 102 | virtual void transfer(void *buf, size_t count); 103 | 104 | // After performing a group of transfers and releasing the chip select 105 | // signal, this function allows others to access the SPI bus 106 | virtual void endTransaction(void); 107 | 108 | // virtual ~SPIClass(); 109 | 110 | private: 111 | }; 112 | 113 | extern SPIClass SPI; -------------------------------------------------------------------------------- /src/arduino/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Server.h - Base class that provides Server 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef server_h 21 | #define server_h 22 | 23 | #include "Print.h" 24 | 25 | class Server : public Print { 26 | public: 27 | virtual void begin() =0; 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/arduino/Stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | Stream.h - base class for character-based streams. 3 | Copyright (c) 2010 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | parsing functions based on TextFinder library by Michael Margolis 20 | */ 21 | 22 | #ifndef Stream_h 23 | #define Stream_h 24 | 25 | #include 26 | #include "Print.h" 27 | 28 | // compatability macros for testing 29 | /* 30 | #define getInt() parseInt() 31 | #define getInt(ignore) parseInt(ignore) 32 | #define getFloat() parseFloat() 33 | #define getFloat(ignore) parseFloat(ignore) 34 | #define getString( pre_string, post_string, buffer, length) 35 | readBytesBetween( pre_string, terminator, buffer, length) 36 | */ 37 | 38 | // This enumeration provides the lookahead options for parseInt(), parseFloat() 39 | // The rules set out here are used until either the first valid character is found 40 | // or a time out occurs due to lack of input. 41 | enum LookaheadMode{ 42 | SKIP_ALL, // All invalid characters are ignored. 43 | SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. 44 | SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. 45 | }; 46 | 47 | #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field 48 | 49 | class Stream : public Print 50 | { 51 | protected: 52 | unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read 53 | unsigned long _startMillis; // used for timeout measurement 54 | int timedRead(); // private method to read stream with timeout 55 | int timedPeek(); // private method to peek stream with timeout 56 | int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout 57 | 58 | public: 59 | virtual int available() = 0; 60 | virtual int read() = 0; 61 | virtual int peek() = 0; 62 | virtual void flush() = 0; 63 | 64 | Stream() {_timeout=1000;} 65 | 66 | // parsing methods 67 | 68 | void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second 69 | unsigned long getTimeout(void) { return _timeout; } 70 | 71 | bool find(char *target); // reads data from the stream until the target string is found 72 | bool find(uint8_t *target) { return find ((char *)target); } 73 | // returns true if target string is found, false if timed out (see setTimeout) 74 | 75 | bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found 76 | bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } 77 | // returns true if target string is found, false if timed out 78 | 79 | bool find(char target) { return find (&target, 1); } 80 | 81 | bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found 82 | bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } 83 | 84 | bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found 85 | bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } 86 | 87 | long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); 88 | // returns the first valid (long) integer value from the current position. 89 | // lookahead determines how parseInt looks ahead in the stream. 90 | // See LookaheadMode enumeration at the top of the file. 91 | // Lookahead is terminated by the first character that is not a valid part of an integer. 92 | // Once parsing commences, 'ignore' will be skipped in the stream. 93 | 94 | float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); 95 | // float version of parseInt 96 | 97 | size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer 98 | size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } 99 | // terminates if length characters have been read or timeout (see setTimeout) 100 | // returns the number of characters placed in the buffer (0 means no valid data found) 101 | 102 | size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character 103 | size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } 104 | // terminates if length characters have been read, timeout, or if the terminator character detected 105 | // returns the number of characters placed in the buffer (0 means no valid data found) 106 | 107 | // Arduino String functions to be added here 108 | String readString(); 109 | String readStringUntil(char terminator); 110 | 111 | protected: 112 | long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } 113 | float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } 114 | // These overload exists for compatibility with any class that has derived 115 | // Stream and used parseFloat/Int with a custom ignore character. To keep 116 | // the public API simple, these overload remains protected. 117 | 118 | struct MultiTarget { 119 | const char *str; // string you're searching for 120 | size_t len; // length of string you're searching for 121 | size_t index; // index used by the search routine. 122 | }; 123 | 124 | // This allows you to search for an arbitrary number of strings. 125 | // Returns index of the target that is found first or -1 if timeout occurs. 126 | int findMulti(struct MultiTarget *targets, int tCount); 127 | }; 128 | 129 | #undef NO_IGNORE_CHAR 130 | #endif 131 | -------------------------------------------------------------------------------- /src/arduino/USBAPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | USBAPI.h 3 | Copyright (c) 2005-2014 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef __USBAPI__ 21 | #define __USBAPI__ 22 | 23 | #include 24 | 25 | typedef unsigned char u8; 26 | typedef unsigned short u16; 27 | typedef unsigned long u32; 28 | 29 | #include "Arduino.h" 30 | 31 | // This definitions is usefull if you want to reduce the EP_SIZE to 16 32 | // at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint 33 | #ifndef USB_EP_SIZE 34 | #define USB_EP_SIZE 64 35 | #endif 36 | 37 | #if defined(USBCON) 38 | 39 | #include "USBDesc.h" 40 | #include "USBCore.h" 41 | 42 | //================================================================================ 43 | //================================================================================ 44 | // USB 45 | 46 | #define EP_TYPE_CONTROL (0x00) 47 | #define EP_TYPE_BULK_IN ((1<256) 81 | #error Please lower the CDC Buffer size 82 | #endif 83 | 84 | class Serial_ : public Stream 85 | { 86 | private: 87 | int peek_buffer; 88 | public: 89 | Serial_() { peek_buffer = -1; }; 90 | void begin(unsigned long); 91 | void begin(unsigned long, uint8_t); 92 | void end(void); 93 | 94 | virtual int available(void); 95 | virtual int peek(void); 96 | virtual int read(void); 97 | int availableForWrite(void); 98 | virtual void flush(void); 99 | virtual size_t write(uint8_t); 100 | virtual size_t write(const uint8_t*, size_t); 101 | using Print::write; // pull in write(str) and write(buf, size) from Print 102 | operator bool(); 103 | 104 | volatile uint8_t _rx_buffer_head; 105 | volatile uint8_t _rx_buffer_tail; 106 | unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; 107 | 108 | // This method allows processing "SEND_BREAK" requests sent by 109 | // the USB host. Those requests indicate that the host wants to 110 | // send a BREAK signal and are accompanied by a single uint16_t 111 | // value, specifying the duration of the break. The value 0 112 | // means to end any current break, while the value 0xffff means 113 | // to start an indefinite break. 114 | // readBreak() will return the value of the most recent break 115 | // request, but will return it at most once, returning -1 when 116 | // readBreak() is called again (until another break request is 117 | // received, which is again returned once). 118 | // This also mean that if two break requests are received 119 | // without readBreak() being called in between, the value of the 120 | // first request is lost. 121 | // Note that the value returned is a long, so it can return 122 | // 0-0xffff as well as -1. 123 | int32_t readBreak(); 124 | 125 | // These return the settings specified by the USB host for the 126 | // serial port. These aren't really used, but are offered here 127 | // in case a sketch wants to act on these settings. 128 | uint32_t baud(); 129 | uint8_t stopbits(); 130 | uint8_t paritytype(); 131 | uint8_t numbits(); 132 | bool dtr(); 133 | bool rts(); 134 | enum { 135 | ONE_STOP_BIT = 0, 136 | ONE_AND_HALF_STOP_BIT = 1, 137 | TWO_STOP_BITS = 2, 138 | }; 139 | enum { 140 | NO_PARITY = 0, 141 | ODD_PARITY = 1, 142 | EVEN_PARITY = 2, 143 | MARK_PARITY = 3, 144 | SPACE_PARITY = 4, 145 | }; 146 | 147 | }; 148 | extern Serial_ Serial; 149 | 150 | #define HAVE_CDCSERIAL 151 | 152 | //================================================================================ 153 | //================================================================================ 154 | // Low level API 155 | 156 | typedef struct 157 | { 158 | uint8_t bmRequestType; 159 | uint8_t bRequest; 160 | uint8_t wValueL; 161 | uint8_t wValueH; 162 | uint16_t wIndex; 163 | uint16_t wLength; 164 | } USBSetup; 165 | 166 | //================================================================================ 167 | //================================================================================ 168 | // MSC 'Driver' 169 | 170 | int MSC_GetInterface(uint8_t* interfaceNum); 171 | int MSC_GetDescriptor(int i); 172 | bool MSC_Setup(USBSetup& setup); 173 | bool MSC_Data(uint8_t rx,uint8_t tx); 174 | 175 | //================================================================================ 176 | //================================================================================ 177 | // CSC 'Driver' 178 | 179 | int CDC_GetInterface(uint8_t* interfaceNum); 180 | int CDC_GetDescriptor(int i); 181 | bool CDC_Setup(USBSetup& setup); 182 | 183 | //================================================================================ 184 | //================================================================================ 185 | 186 | #define TRANSFER_PGM 0x80 187 | #define TRANSFER_RELEASE 0x40 188 | #define TRANSFER_ZERO 0x20 189 | 190 | int USB_SendControl(uint8_t flags, const void* d, int len); 191 | int USB_RecvControl(void* d, int len); 192 | int USB_RecvControlLong(void* d, int len); 193 | 194 | uint8_t USB_Available(uint8_t ep); 195 | uint8_t USB_SendSpace(uint8_t ep); 196 | int USB_Send(uint8_t ep, const void* data, int len); // blocking 197 | int USB_Recv(uint8_t ep, void* data, int len); // non-blocking 198 | int USB_Recv(uint8_t ep); // non-blocking 199 | void USB_Flush(uint8_t ep); 200 | 201 | #endif 202 | 203 | #endif /* if defined(USBCON) */ 204 | -------------------------------------------------------------------------------- /src/arduino/USBCore.h: -------------------------------------------------------------------------------- 1 | 2 | // Copyright (c) 2010, Peter Barrett 3 | /* 4 | ** Permission to use, copy, modify, and/or distribute this software for 5 | ** any purpose with or without fee is hereby granted, provided that the 6 | ** above copyright notice and this permission notice appear in all copies. 7 | ** 8 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 9 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 10 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 11 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 12 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 13 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 14 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 15 | ** SOFTWARE. 16 | */ 17 | 18 | #ifndef __USBCORE_H__ 19 | #define __USBCORE_H__ 20 | 21 | #include "USBAPI.h" 22 | 23 | // Standard requests 24 | #define GET_STATUS 0 25 | #define CLEAR_FEATURE 1 26 | #define SET_FEATURE 3 27 | #define SET_ADDRESS 5 28 | #define GET_DESCRIPTOR 6 29 | #define SET_DESCRIPTOR 7 30 | #define GET_CONFIGURATION 8 31 | #define SET_CONFIGURATION 9 32 | #define GET_INTERFACE 10 33 | #define SET_INTERFACE 11 34 | 35 | 36 | // bmRequestType 37 | #define REQUEST_HOSTTODEVICE 0x00 38 | #define REQUEST_DEVICETOHOST 0x80 39 | #define REQUEST_DIRECTION 0x80 40 | 41 | #define REQUEST_STANDARD 0x00 42 | #define REQUEST_CLASS 0x20 43 | #define REQUEST_VENDOR 0x40 44 | #define REQUEST_TYPE 0x60 45 | 46 | #define REQUEST_DEVICE 0x00 47 | #define REQUEST_INTERFACE 0x01 48 | #define REQUEST_ENDPOINT 0x02 49 | #define REQUEST_OTHER 0x03 50 | #define REQUEST_RECIPIENT 0x03 51 | 52 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_CLASS | REQUEST_INTERFACE) 53 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE | REQUEST_CLASS | REQUEST_INTERFACE) 54 | #define REQUEST_DEVICETOHOST_STANDARD_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_INTERFACE) 55 | 56 | // Class requests 57 | 58 | #define CDC_SET_LINE_CODING 0x20 59 | #define CDC_GET_LINE_CODING 0x21 60 | #define CDC_SET_CONTROL_LINE_STATE 0x22 61 | #define CDC_SEND_BREAK 0x23 62 | 63 | #define MSC_RESET 0xFF 64 | #define MSC_GET_MAX_LUN 0xFE 65 | 66 | // Descriptors 67 | 68 | #define USB_DEVICE_DESC_SIZE 18 69 | #define USB_CONFIGUARTION_DESC_SIZE 9 70 | #define USB_INTERFACE_DESC_SIZE 9 71 | #define USB_ENDPOINT_DESC_SIZE 7 72 | 73 | #define USB_DEVICE_DESCRIPTOR_TYPE 1 74 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 75 | #define USB_STRING_DESCRIPTOR_TYPE 3 76 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4 77 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5 78 | 79 | // usb_20.pdf Table 9.6 Standard Feature Selectors 80 | #define DEVICE_REMOTE_WAKEUP 1 81 | #define ENDPOINT_HALT 2 82 | #define TEST_MODE 3 83 | 84 | // usb_20.pdf Figure 9-4. Information Returned by a GetStatus() Request to a Device 85 | #define FEATURE_SELFPOWERED_ENABLED (1 << 0) 86 | #define FEATURE_REMOTE_WAKEUP_ENABLED (1 << 1) 87 | 88 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 89 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 90 | #define USB_DEVICE_CLASS_STORAGE 0x08 91 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF 92 | 93 | #define USB_CONFIG_POWERED_MASK 0x40 94 | #define USB_CONFIG_BUS_POWERED 0x80 95 | #define USB_CONFIG_SELF_POWERED 0xC0 96 | #define USB_CONFIG_REMOTE_WAKEUP 0x20 97 | 98 | // bMaxPower in Configuration Descriptor 99 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2) 100 | 101 | // bEndpointAddress in Endpoint Descriptor 102 | #define USB_ENDPOINT_DIRECTION_MASK 0x80 103 | #define USB_ENDPOINT_OUT(addr) (lowByte((addr) | 0x00)) 104 | #define USB_ENDPOINT_IN(addr) (lowByte((addr) | 0x80)) 105 | 106 | #define USB_ENDPOINT_TYPE_MASK 0x03 107 | #define USB_ENDPOINT_TYPE_CONTROL 0x00 108 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 109 | #define USB_ENDPOINT_TYPE_BULK 0x02 110 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03 111 | 112 | #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF) 113 | 114 | #define CDC_V1_10 0x0110 115 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 116 | 117 | #define CDC_CALL_MANAGEMENT 0x01 118 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02 119 | #define CDC_HEADER 0x00 120 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 121 | #define CDC_UNION 0x06 122 | #define CDC_CS_INTERFACE 0x24 123 | #define CDC_CS_ENDPOINT 0x25 124 | #define CDC_DATA_INTERFACE_CLASS 0x0A 125 | 126 | #define MSC_SUBCLASS_SCSI 0x06 127 | #define MSC_PROTOCOL_BULK_ONLY 0x50 128 | 129 | #ifndef USB_VERSION 130 | #define USB_VERSION 0x200 131 | #endif 132 | 133 | // Device 134 | typedef struct { 135 | u8 len; // 18 136 | u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE 137 | u16 usbVersion; // 0x200 or 0x210 138 | u8 deviceClass; 139 | u8 deviceSubClass; 140 | u8 deviceProtocol; 141 | u8 packetSize0; // Packet 0 142 | u16 idVendor; 143 | u16 idProduct; 144 | u16 deviceVersion; // 0x100 145 | u8 iManufacturer; 146 | u8 iProduct; 147 | u8 iSerialNumber; 148 | u8 bNumConfigurations; 149 | } DeviceDescriptor; 150 | 151 | // Config 152 | typedef struct { 153 | u8 len; // 9 154 | u8 dtype; // 2 155 | u16 clen; // total length 156 | u8 numInterfaces; 157 | u8 config; 158 | u8 iconfig; 159 | u8 attributes; 160 | u8 maxPower; 161 | } ConfigDescriptor; 162 | 163 | // String 164 | 165 | // Interface 166 | typedef struct 167 | { 168 | u8 len; // 9 169 | u8 dtype; // 4 170 | u8 number; 171 | u8 alternate; 172 | u8 numEndpoints; 173 | u8 interfaceClass; 174 | u8 interfaceSubClass; 175 | u8 protocol; 176 | u8 iInterface; 177 | } InterfaceDescriptor; 178 | 179 | // Endpoint 180 | typedef struct 181 | { 182 | u8 len; // 7 183 | u8 dtype; // 5 184 | u8 addr; 185 | u8 attr; 186 | u16 packetSize; 187 | u8 interval; 188 | } EndpointDescriptor; 189 | 190 | // Interface Association Descriptor 191 | // Used to bind 2 interfaces together in CDC compostite device 192 | typedef struct 193 | { 194 | u8 len; // 8 195 | u8 dtype; // 11 196 | u8 firstInterface; 197 | u8 interfaceCount; 198 | u8 functionClass; 199 | u8 funtionSubClass; 200 | u8 functionProtocol; 201 | u8 iInterface; 202 | } IADDescriptor; 203 | 204 | // CDC CS interface descriptor 205 | typedef struct 206 | { 207 | u8 len; // 5 208 | u8 dtype; // 0x24 209 | u8 subtype; 210 | u8 d0; 211 | u8 d1; 212 | } CDCCSInterfaceDescriptor; 213 | 214 | typedef struct 215 | { 216 | u8 len; // 4 217 | u8 dtype; // 0x24 218 | u8 subtype; 219 | u8 d0; 220 | } CDCCSInterfaceDescriptor4; 221 | 222 | typedef struct 223 | { 224 | u8 len; 225 | u8 dtype; // 0x24 226 | u8 subtype; // 1 227 | u8 bmCapabilities; 228 | u8 bDataInterface; 229 | } CMFunctionalDescriptor; 230 | 231 | typedef struct 232 | { 233 | u8 len; 234 | u8 dtype; // 0x24 235 | u8 subtype; // 1 236 | u8 bmCapabilities; 237 | } ACMFunctionalDescriptor; 238 | 239 | typedef struct 240 | { 241 | // IAD 242 | IADDescriptor iad; // Only needed on compound device 243 | 244 | // Control 245 | InterfaceDescriptor cif; // 246 | CDCCSInterfaceDescriptor header; 247 | CMFunctionalDescriptor callManagement; // Call Management 248 | ACMFunctionalDescriptor controlManagement; // ACM 249 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION 250 | EndpointDescriptor cifin; 251 | 252 | // Data 253 | InterfaceDescriptor dif; 254 | EndpointDescriptor in; 255 | EndpointDescriptor out; 256 | } CDCDescriptor; 257 | 258 | typedef struct 259 | { 260 | InterfaceDescriptor msc; 261 | EndpointDescriptor in; 262 | EndpointDescriptor out; 263 | } MSCDescriptor; 264 | 265 | 266 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ 267 | { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } 268 | 269 | #define D_CONFIG(_totalLength,_interfaces) \ 270 | { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(500) } 271 | 272 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ 273 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } 274 | 275 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ 276 | { 7, 5, _addr,_attr,_packetSize, _interval } 277 | 278 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ 279 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } 280 | 281 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } 282 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } 283 | 284 | // Bootloader related fields 285 | // Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten 286 | // by the running sketch before to actual reboot). 287 | // Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both 288 | // the usafe and the safe location. Check once (in USBCore.cpp) if the bootloader in new, then set the global 289 | // _updatedLUFAbootloader variable to true/false and place the magic key consequently 290 | #ifndef MAGIC_KEY 291 | #define MAGIC_KEY 0x7777 292 | #endif 293 | 294 | #ifndef MAGIC_KEY_POS 295 | #define MAGIC_KEY_POS 0x0800 296 | #endif 297 | 298 | #ifndef NEW_LUFA_SIGNATURE 299 | #define NEW_LUFA_SIGNATURE 0xDCFB 300 | #endif 301 | 302 | #endif 303 | -------------------------------------------------------------------------------- /src/arduino/USBDesc.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011, Peter Barrett 3 | Copyright (c) 2015, Arduino LLC 4 | 5 | Permission to use, copy, modify, and/or distribute this software for 6 | any purpose with or without fee is hereby granted, provided that the 7 | above copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 | WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 | WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 12 | BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 13 | OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 14 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 15 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 16 | SOFTWARE. 17 | */ 18 | 19 | #define PLUGGABLE_USB_ENABLED 20 | 21 | #if defined(EPRST6) 22 | #define USB_ENDPOINTS 7 // AtMegaxxU4 23 | #else 24 | #define USB_ENDPOINTS 5 // AtMegaxxU2 25 | #endif 26 | 27 | #define ISERIAL_MAX_LEN 20 28 | 29 | #define CDC_INTERFACE_COUNT 2 30 | #define CDC_ENPOINT_COUNT 3 31 | 32 | #define CDC_ACM_INTERFACE 0 // CDC ACM 33 | #define CDC_DATA_INTERFACE 1 // CDC Data 34 | #define CDC_FIRST_ENDPOINT 1 35 | #define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First 36 | #define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1) 37 | #define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2) 38 | 39 | #define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT) 40 | 41 | #define CDC_RX CDC_ENDPOINT_OUT 42 | #define CDC_TX CDC_ENDPOINT_IN 43 | 44 | #define IMANUFACTURER 1 45 | #define IPRODUCT 2 46 | #define ISERIAL 3 -------------------------------------------------------------------------------- /src/arduino/WCharacter.h: -------------------------------------------------------------------------------- 1 | /* 2 | WCharacter.h - Character utility functions for Wiring & Arduino 3 | Copyright (c) 2010 Hernando Barragan. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Character_h 21 | #define Character_h 22 | 23 | #include 24 | #include 25 | 26 | // WCharacter.h prototypes 27 | inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); 28 | inline boolean isAlpha(int c) __attribute__((always_inline)); 29 | inline boolean isAscii(int c) __attribute__((always_inline)); 30 | inline boolean isWhitespace(int c) __attribute__((always_inline)); 31 | inline boolean isControl(int c) __attribute__((always_inline)); 32 | inline boolean isDigit(int c) __attribute__((always_inline)); 33 | inline boolean isGraph(int c) __attribute__((always_inline)); 34 | inline boolean isLowerCase(int c) __attribute__((always_inline)); 35 | inline boolean isPrintable(int c) __attribute__((always_inline)); 36 | inline boolean isPunct(int c) __attribute__((always_inline)); 37 | inline boolean isSpace(int c) __attribute__((always_inline)); 38 | inline boolean isUpperCase(int c) __attribute__((always_inline)); 39 | inline boolean isHexadecimalDigit(int c) __attribute__((always_inline)); 40 | inline int toAscii(int c) __attribute__((always_inline)); 41 | inline int toLowerCase(int c) __attribute__((always_inline)); 42 | inline int toUpperCase(int c)__attribute__((always_inline)); 43 | 44 | 45 | // Checks for an alphanumeric character. 46 | // It is equivalent to (isalpha(c) || isdigit(c)). 47 | inline boolean isAlphaNumeric(int c) 48 | { 49 | return ( isalnum(c) == 0 ? false : true); 50 | } 51 | 52 | 53 | // Checks for an alphabetic character. 54 | // It is equivalent to (isupper(c) || islower(c)). 55 | inline boolean isAlpha(int c) 56 | { 57 | return ( isalpha(c) == 0 ? false : true); 58 | } 59 | 60 | 61 | // Checks whether c is a 7-bit unsigned char value 62 | // that fits into the ASCII character set. 63 | inline boolean isAscii(int c) 64 | { 65 | return ( isascii (c) == 0 ? false : true); 66 | } 67 | 68 | 69 | // Checks for a blank character, that is, a space or a tab. 70 | inline boolean isWhitespace(int c) 71 | { 72 | return ( isblank (c) == 0 ? false : true); 73 | } 74 | 75 | 76 | // Checks for a control character. 77 | inline boolean isControl(int c) 78 | { 79 | return ( iscntrl (c) == 0 ? false : true); 80 | } 81 | 82 | 83 | // Checks for a digit (0 through 9). 84 | inline boolean isDigit(int c) 85 | { 86 | return ( isdigit (c) == 0 ? false : true); 87 | } 88 | 89 | 90 | // Checks for any printable character except space. 91 | inline boolean isGraph(int c) 92 | { 93 | return ( isgraph (c) == 0 ? false : true); 94 | } 95 | 96 | 97 | // Checks for a lower-case character. 98 | inline boolean isLowerCase(int c) 99 | { 100 | return (islower (c) == 0 ? false : true); 101 | } 102 | 103 | 104 | // Checks for any printable character including space. 105 | inline boolean isPrintable(int c) 106 | { 107 | return ( isprint (c) == 0 ? false : true); 108 | } 109 | 110 | 111 | // Checks for any printable character which is not a space 112 | // or an alphanumeric character. 113 | inline boolean isPunct(int c) 114 | { 115 | return ( ispunct (c) == 0 ? false : true); 116 | } 117 | 118 | 119 | // Checks for white-space characters. For the avr-libc library, 120 | // these are: space, formfeed ('\f'), newline ('\n'), carriage 121 | // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). 122 | inline boolean isSpace(int c) 123 | { 124 | return ( isspace (c) == 0 ? false : true); 125 | } 126 | 127 | 128 | // Checks for an uppercase letter. 129 | inline boolean isUpperCase(int c) 130 | { 131 | return ( isupper (c) == 0 ? false : true); 132 | } 133 | 134 | 135 | // Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 136 | // 8 9 a b c d e f A B C D E F. 137 | inline boolean isHexadecimalDigit(int c) 138 | { 139 | return ( isxdigit (c) == 0 ? false : true); 140 | } 141 | 142 | 143 | // Converts c to a 7-bit unsigned char value that fits into the 144 | // ASCII character set, by clearing the high-order bits. 145 | inline int toAscii(int c) 146 | { 147 | return toascii (c); 148 | } 149 | 150 | 151 | // Warning: 152 | // Many people will be unhappy if you use this function. 153 | // This function will convert accented letters into random 154 | // characters. 155 | 156 | // Converts the letter c to lower case, if possible. 157 | inline int toLowerCase(int c) 158 | { 159 | return tolower (c); 160 | } 161 | 162 | 163 | // Converts the letter c to upper case, if possible. 164 | inline int toUpperCase(int c) 165 | { 166 | return toupper (c); 167 | } 168 | 169 | #endif -------------------------------------------------------------------------------- /src/arduino/WString.h: -------------------------------------------------------------------------------- 1 | /* 2 | WString.h - String library for Wiring & Arduino 3 | ...mostly rewritten by Paul Stoffregen... 4 | Copyright (c) 2009-10 Hernando Barragan. All right reserved. 5 | Copyright 2011, Paul Stoffregen, paul@pjrc.com 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | */ 21 | 22 | #ifndef String_class_h 23 | #define String_class_h 24 | 25 | #include "pgmspace.h" 26 | #include "noniso.h" 27 | 28 | #ifdef __cplusplus 29 | 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | // When compiling programs with this class, the following gcc parameters 36 | // dramatically increase performance and memory (RAM) efficiency, typically 37 | // with little or no increase in code size. 38 | // -felide-constructors 39 | // -std=c++0x 40 | 41 | class __FlashStringHelper; 42 | #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) 43 | 44 | // An inherited class for holding the result of a concatenation. These 45 | // result objects are assumed to be writable by subsequent concatenations. 46 | class StringSumHelper; 47 | 48 | // The string class 49 | class String 50 | { 51 | // use a function pointer to allow for "if (s)" without the 52 | // complications of an operator bool(). for more information, see: 53 | // http://www.artima.com/cppsource/safebool.html 54 | typedef void (String::*StringIfHelperType)() const; 55 | void StringIfHelper() const {} 56 | 57 | public: 58 | // constructors 59 | // creates a copy of the initial value. 60 | // if the initial value is null or invalid, or if memory allocation 61 | // fails, the string will be marked as invalid (i.e. "if (s)" will 62 | // be false). 63 | String(const char *cstr = ""); 64 | String(const String &str); 65 | String(const __FlashStringHelper *str); 66 | #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) 67 | String(String &&rval); 68 | String(StringSumHelper &&rval); 69 | #endif 70 | explicit String(char c); 71 | explicit String(unsigned char, unsigned char base=10); 72 | explicit String(int, unsigned char base=10); 73 | explicit String(unsigned int, unsigned char base=10); 74 | explicit String(long, unsigned char base=10); 75 | explicit String(unsigned long, unsigned char base=10); 76 | explicit String(float, unsigned char decimalPlaces=2); 77 | explicit String(double, unsigned char decimalPlaces=2); 78 | ~String(void); 79 | 80 | // memory management 81 | // return true on success, false on failure (in which case, the string 82 | // is left unchanged). reserve(0), if successful, will validate an 83 | // invalid string (i.e., "if (s)" will be true afterwards) 84 | unsigned char reserve(unsigned int size); 85 | inline unsigned int length(void) const {return len;} 86 | 87 | // creates a copy of the assigned value. if the value is null or 88 | // invalid, or if the memory allocation fails, the string will be 89 | // marked as invalid ("if (s)" will be false). 90 | String & operator = (const String &rhs); 91 | String & operator = (const char *cstr); 92 | String & operator = (const __FlashStringHelper *str); 93 | #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) 94 | String & operator = (String &&rval); 95 | String & operator = (StringSumHelper &&rval); 96 | #endif 97 | 98 | // concatenate (works w/ built-in types) 99 | 100 | // returns true on success, false on failure (in which case, the string 101 | // is left unchanged). if the argument is null or invalid, the 102 | // concatenation is considered unsucessful. 103 | unsigned char concat(const String &str); 104 | unsigned char concat(const char *cstr); 105 | unsigned char concat(char c); 106 | unsigned char concat(unsigned char c); 107 | unsigned char concat(int num); 108 | unsigned char concat(unsigned int num); 109 | unsigned char concat(long num); 110 | unsigned char concat(unsigned long num); 111 | unsigned char concat(float num); 112 | unsigned char concat(double num); 113 | unsigned char concat(const __FlashStringHelper * str); 114 | 115 | // if there's not enough memory for the concatenated value, the string 116 | // will be left unchanged (but this isn't signalled in any way) 117 | String & operator += (const String &rhs) {concat(rhs); return (*this);} 118 | String & operator += (const char *cstr) {concat(cstr); return (*this);} 119 | String & operator += (char c) {concat(c); return (*this);} 120 | String & operator += (unsigned char num) {concat(num); return (*this);} 121 | String & operator += (int num) {concat(num); return (*this);} 122 | String & operator += (unsigned int num) {concat(num); return (*this);} 123 | String & operator += (long num) {concat(num); return (*this);} 124 | String & operator += (unsigned long num) {concat(num); return (*this);} 125 | String & operator += (float num) {concat(num); return (*this);} 126 | String & operator += (double num) {concat(num); return (*this);} 127 | String & operator += (const __FlashStringHelper *str){concat(str); return (*this);} 128 | 129 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); 130 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); 131 | friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); 132 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); 133 | friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); 134 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); 135 | friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); 136 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); 137 | friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); 138 | friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); 139 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs); 140 | 141 | // comparison (only works w/ Strings and "strings") 142 | operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } 143 | int compareTo(const String &s) const; 144 | unsigned char equals(const String &s) const; 145 | unsigned char equals(const char *cstr) const; 146 | unsigned char operator == (const String &rhs) const {return equals(rhs);} 147 | unsigned char operator == (const char *cstr) const {return equals(cstr);} 148 | unsigned char operator != (const String &rhs) const {return !equals(rhs);} 149 | unsigned char operator != (const char *cstr) const {return !equals(cstr);} 150 | unsigned char operator < (const String &rhs) const; 151 | unsigned char operator > (const String &rhs) const; 152 | unsigned char operator <= (const String &rhs) const; 153 | unsigned char operator >= (const String &rhs) const; 154 | unsigned char equalsIgnoreCase(const String &s) const; 155 | unsigned char startsWith( const String &prefix) const; 156 | unsigned char startsWith(const String &prefix, unsigned int offset) const; 157 | unsigned char endsWith(const String &suffix) const; 158 | 159 | // character acccess 160 | char charAt(unsigned int index) const; 161 | void setCharAt(unsigned int index, char c); 162 | char operator [] (unsigned int index) const; 163 | char& operator [] (unsigned int index); 164 | void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; 165 | void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const 166 | { getBytes((unsigned char *)buf, bufsize, index); } 167 | const char* c_str() const { return buffer; } 168 | char* begin() { return buffer; } 169 | char* end() { return buffer + length(); } 170 | const char* begin() const { return c_str(); } 171 | const char* end() const { return c_str() + length(); } 172 | 173 | // search 174 | int indexOf( char ch ) const; 175 | int indexOf( char ch, unsigned int fromIndex ) const; 176 | int indexOf( const String &str ) const; 177 | int indexOf( const String &str, unsigned int fromIndex ) const; 178 | int lastIndexOf( char ch ) const; 179 | int lastIndexOf( char ch, unsigned int fromIndex ) const; 180 | int lastIndexOf( const String &str ) const; 181 | int lastIndexOf( const String &str, unsigned int fromIndex ) const; 182 | String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); }; 183 | String substring( unsigned int beginIndex, unsigned int endIndex ) const; 184 | 185 | // modification 186 | void replace(char find, char replace); 187 | void replace(const String& find, const String& replace); 188 | void remove(unsigned int index); 189 | void remove(unsigned int index, unsigned int count); 190 | void toLowerCase(void); 191 | void toUpperCase(void); 192 | void trim(void); 193 | 194 | // parsing/conversion 195 | long toInt(void) const; 196 | float toFloat(void) const; 197 | double toDouble(void) const; 198 | 199 | protected: 200 | char *buffer; // the actual char array 201 | unsigned int capacity; // the array length minus one (for the '\0') 202 | unsigned int len; // the String length (not counting the '\0') 203 | protected: 204 | void init(void); 205 | void invalidate(void); 206 | unsigned char changeBuffer(unsigned int maxStrLen); 207 | unsigned char concat(const char *cstr, unsigned int length); 208 | 209 | // copy and move 210 | String & copy(const char *cstr, unsigned int length); 211 | String & copy(const __FlashStringHelper *pstr, unsigned int length); 212 | #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) 213 | void move(String &rhs); 214 | #endif 215 | }; 216 | 217 | class StringSumHelper : public String 218 | { 219 | public: 220 | StringSumHelper(const String &s) : String(s) {} 221 | StringSumHelper(const char *p) : String(p) {} 222 | StringSumHelper(char c) : String(c) {} 223 | StringSumHelper(unsigned char num) : String(num) {} 224 | StringSumHelper(int num) : String(num) {} 225 | StringSumHelper(unsigned int num) : String(num) {} 226 | StringSumHelper(long num) : String(num) {} 227 | StringSumHelper(unsigned long num) : String(num) {} 228 | StringSumHelper(float num) : String(num) {} 229 | StringSumHelper(double num) : String(num) {} 230 | }; 231 | 232 | #endif // __cplusplus 233 | #endif // String_class_h 234 | -------------------------------------------------------------------------------- /src/arduino/Wire.h: -------------------------------------------------------------------------------- 1 | #ifndef TwoWire_h 2 | #define TwoWire_h 3 | 4 | #include "Stream.h" 5 | #include 6 | 7 | class TwoWire : public Stream { 8 | public: 9 | void begin(); 10 | void begin(uint8_t); 11 | void begin(int); 12 | void end(); 13 | void setClock(uint32_t); 14 | void setWireTimeout(uint32_t timeout = 25000, 15 | bool reset_with_timeout = false); 16 | bool getWireTimeoutFlag(void); 17 | void clearWireTimeoutFlag(void); 18 | void beginTransmission(uint8_t); 19 | void beginTransmission(int); 20 | uint8_t endTransmission(void); 21 | uint8_t endTransmission(bool); 22 | uint8_t requestFrom(uint8_t, uint8_t); 23 | uint8_t requestFrom(uint8_t, uint8_t, uint8_t); 24 | uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); 25 | virtual size_t write(uint8_t); 26 | virtual size_t write(const uint8_t *, size_t); 27 | virtual int available(void); 28 | virtual int read(void); 29 | virtual int peek(void); 30 | virtual void flush(void); 31 | void onReceive(void (*)(int)); 32 | void onRequest(void (*)(void)); 33 | 34 | inline size_t write(unsigned long n) { return write((uint8_t)n); } 35 | inline size_t write(long n) { return write((uint8_t)n); } 36 | inline size_t write(unsigned int n) { return write((uint8_t)n); } 37 | inline size_t write(int n) { return write((uint8_t)n); } 38 | using Print::write; 39 | }; 40 | 41 | extern TwoWire Wire; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/arduino/avr/interrupt.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Fake version of avr/interrupt.h 3 | */ 4 | void cli(void); 5 | void sei(void); 6 | -------------------------------------------------------------------------------- /src/arduino/binary.h: -------------------------------------------------------------------------------- 1 | /* 2 | binary.h - Definitions for binary constants 3 | Copyright (c) 2006 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Binary_h 21 | #define Binary_h 22 | 23 | #define B0 0 24 | #define B00 0 25 | #define B000 0 26 | #define B0000 0 27 | #define B00000 0 28 | #define B000000 0 29 | #define B0000000 0 30 | #define B00000000 0 31 | #define B1 1 32 | #define B01 1 33 | #define B001 1 34 | #define B0001 1 35 | #define B00001 1 36 | #define B000001 1 37 | #define B0000001 1 38 | #define B00000001 1 39 | #define B10 2 40 | #define B010 2 41 | #define B0010 2 42 | #define B00010 2 43 | #define B000010 2 44 | #define B0000010 2 45 | #define B00000010 2 46 | #define B11 3 47 | #define B011 3 48 | #define B0011 3 49 | #define B00011 3 50 | #define B000011 3 51 | #define B0000011 3 52 | #define B00000011 3 53 | #define B100 4 54 | #define B0100 4 55 | #define B00100 4 56 | #define B000100 4 57 | #define B0000100 4 58 | #define B00000100 4 59 | #define B101 5 60 | #define B0101 5 61 | #define B00101 5 62 | #define B000101 5 63 | #define B0000101 5 64 | #define B00000101 5 65 | #define B110 6 66 | #define B0110 6 67 | #define B00110 6 68 | #define B000110 6 69 | #define B0000110 6 70 | #define B00000110 6 71 | #define B111 7 72 | #define B0111 7 73 | #define B00111 7 74 | #define B000111 7 75 | #define B0000111 7 76 | #define B00000111 7 77 | #define B1000 8 78 | #define B01000 8 79 | #define B001000 8 80 | #define B0001000 8 81 | #define B00001000 8 82 | #define B1001 9 83 | #define B01001 9 84 | #define B001001 9 85 | #define B0001001 9 86 | #define B00001001 9 87 | #define B1010 10 88 | #define B01010 10 89 | #define B001010 10 90 | #define B0001010 10 91 | #define B00001010 10 92 | #define B1011 11 93 | #define B01011 11 94 | #define B001011 11 95 | #define B0001011 11 96 | #define B00001011 11 97 | #define B1100 12 98 | #define B01100 12 99 | #define B001100 12 100 | #define B0001100 12 101 | #define B00001100 12 102 | #define B1101 13 103 | #define B01101 13 104 | #define B001101 13 105 | #define B0001101 13 106 | #define B00001101 13 107 | #define B1110 14 108 | #define B01110 14 109 | #define B001110 14 110 | #define B0001110 14 111 | #define B00001110 14 112 | #define B1111 15 113 | #define B01111 15 114 | #define B001111 15 115 | #define B0001111 15 116 | #define B00001111 15 117 | #define B10000 16 118 | #define B010000 16 119 | #define B0010000 16 120 | #define B00010000 16 121 | #define B10001 17 122 | #define B010001 17 123 | #define B0010001 17 124 | #define B00010001 17 125 | #define B10010 18 126 | #define B010010 18 127 | #define B0010010 18 128 | #define B00010010 18 129 | #define B10011 19 130 | #define B010011 19 131 | #define B0010011 19 132 | #define B00010011 19 133 | #define B10100 20 134 | #define B010100 20 135 | #define B0010100 20 136 | #define B00010100 20 137 | #define B10101 21 138 | #define B010101 21 139 | #define B0010101 21 140 | #define B00010101 21 141 | #define B10110 22 142 | #define B010110 22 143 | #define B0010110 22 144 | #define B00010110 22 145 | #define B10111 23 146 | #define B010111 23 147 | #define B0010111 23 148 | #define B00010111 23 149 | #define B11000 24 150 | #define B011000 24 151 | #define B0011000 24 152 | #define B00011000 24 153 | #define B11001 25 154 | #define B011001 25 155 | #define B0011001 25 156 | #define B00011001 25 157 | #define B11010 26 158 | #define B011010 26 159 | #define B0011010 26 160 | #define B00011010 26 161 | #define B11011 27 162 | #define B011011 27 163 | #define B0011011 27 164 | #define B00011011 27 165 | #define B11100 28 166 | #define B011100 28 167 | #define B0011100 28 168 | #define B00011100 28 169 | #define B11101 29 170 | #define B011101 29 171 | #define B0011101 29 172 | #define B00011101 29 173 | #define B11110 30 174 | #define B011110 30 175 | #define B0011110 30 176 | #define B00011110 30 177 | #define B11111 31 178 | #define B011111 31 179 | #define B0011111 31 180 | #define B00011111 31 181 | #define B100000 32 182 | #define B0100000 32 183 | #define B00100000 32 184 | #define B100001 33 185 | #define B0100001 33 186 | #define B00100001 33 187 | #define B100010 34 188 | #define B0100010 34 189 | #define B00100010 34 190 | #define B100011 35 191 | #define B0100011 35 192 | #define B00100011 35 193 | #define B100100 36 194 | #define B0100100 36 195 | #define B00100100 36 196 | #define B100101 37 197 | #define B0100101 37 198 | #define B00100101 37 199 | #define B100110 38 200 | #define B0100110 38 201 | #define B00100110 38 202 | #define B100111 39 203 | #define B0100111 39 204 | #define B00100111 39 205 | #define B101000 40 206 | #define B0101000 40 207 | #define B00101000 40 208 | #define B101001 41 209 | #define B0101001 41 210 | #define B00101001 41 211 | #define B101010 42 212 | #define B0101010 42 213 | #define B00101010 42 214 | #define B101011 43 215 | #define B0101011 43 216 | #define B00101011 43 217 | #define B101100 44 218 | #define B0101100 44 219 | #define B00101100 44 220 | #define B101101 45 221 | #define B0101101 45 222 | #define B00101101 45 223 | #define B101110 46 224 | #define B0101110 46 225 | #define B00101110 46 226 | #define B101111 47 227 | #define B0101111 47 228 | #define B00101111 47 229 | #define B110000 48 230 | #define B0110000 48 231 | #define B00110000 48 232 | #define B110001 49 233 | #define B0110001 49 234 | #define B00110001 49 235 | #define B110010 50 236 | #define B0110010 50 237 | #define B00110010 50 238 | #define B110011 51 239 | #define B0110011 51 240 | #define B00110011 51 241 | #define B110100 52 242 | #define B0110100 52 243 | #define B00110100 52 244 | #define B110101 53 245 | #define B0110101 53 246 | #define B00110101 53 247 | #define B110110 54 248 | #define B0110110 54 249 | #define B00110110 54 250 | #define B110111 55 251 | #define B0110111 55 252 | #define B00110111 55 253 | #define B111000 56 254 | #define B0111000 56 255 | #define B00111000 56 256 | #define B111001 57 257 | #define B0111001 57 258 | #define B00111001 57 259 | #define B111010 58 260 | #define B0111010 58 261 | #define B00111010 58 262 | #define B111011 59 263 | #define B0111011 59 264 | #define B00111011 59 265 | #define B111100 60 266 | #define B0111100 60 267 | #define B00111100 60 268 | #define B111101 61 269 | #define B0111101 61 270 | #define B00111101 61 271 | #define B111110 62 272 | #define B0111110 62 273 | #define B00111110 62 274 | #define B111111 63 275 | #define B0111111 63 276 | #define B00111111 63 277 | #define B1000000 64 278 | #define B01000000 64 279 | #define B1000001 65 280 | #define B01000001 65 281 | #define B1000010 66 282 | #define B01000010 66 283 | #define B1000011 67 284 | #define B01000011 67 285 | #define B1000100 68 286 | #define B01000100 68 287 | #define B1000101 69 288 | #define B01000101 69 289 | #define B1000110 70 290 | #define B01000110 70 291 | #define B1000111 71 292 | #define B01000111 71 293 | #define B1001000 72 294 | #define B01001000 72 295 | #define B1001001 73 296 | #define B01001001 73 297 | #define B1001010 74 298 | #define B01001010 74 299 | #define B1001011 75 300 | #define B01001011 75 301 | #define B1001100 76 302 | #define B01001100 76 303 | #define B1001101 77 304 | #define B01001101 77 305 | #define B1001110 78 306 | #define B01001110 78 307 | #define B1001111 79 308 | #define B01001111 79 309 | #define B1010000 80 310 | #define B01010000 80 311 | #define B1010001 81 312 | #define B01010001 81 313 | #define B1010010 82 314 | #define B01010010 82 315 | #define B1010011 83 316 | #define B01010011 83 317 | #define B1010100 84 318 | #define B01010100 84 319 | #define B1010101 85 320 | #define B01010101 85 321 | #define B1010110 86 322 | #define B01010110 86 323 | #define B1010111 87 324 | #define B01010111 87 325 | #define B1011000 88 326 | #define B01011000 88 327 | #define B1011001 89 328 | #define B01011001 89 329 | #define B1011010 90 330 | #define B01011010 90 331 | #define B1011011 91 332 | #define B01011011 91 333 | #define B1011100 92 334 | #define B01011100 92 335 | #define B1011101 93 336 | #define B01011101 93 337 | #define B1011110 94 338 | #define B01011110 94 339 | #define B1011111 95 340 | #define B01011111 95 341 | #define B1100000 96 342 | #define B01100000 96 343 | #define B1100001 97 344 | #define B01100001 97 345 | #define B1100010 98 346 | #define B01100010 98 347 | #define B1100011 99 348 | #define B01100011 99 349 | #define B1100100 100 350 | #define B01100100 100 351 | #define B1100101 101 352 | #define B01100101 101 353 | #define B1100110 102 354 | #define B01100110 102 355 | #define B1100111 103 356 | #define B01100111 103 357 | #define B1101000 104 358 | #define B01101000 104 359 | #define B1101001 105 360 | #define B01101001 105 361 | #define B1101010 106 362 | #define B01101010 106 363 | #define B1101011 107 364 | #define B01101011 107 365 | #define B1101100 108 366 | #define B01101100 108 367 | #define B1101101 109 368 | #define B01101101 109 369 | #define B1101110 110 370 | #define B01101110 110 371 | #define B1101111 111 372 | #define B01101111 111 373 | #define B1110000 112 374 | #define B01110000 112 375 | #define B1110001 113 376 | #define B01110001 113 377 | #define B1110010 114 378 | #define B01110010 114 379 | #define B1110011 115 380 | #define B01110011 115 381 | #define B1110100 116 382 | #define B01110100 116 383 | #define B1110101 117 384 | #define B01110101 117 385 | #define B1110110 118 386 | #define B01110110 118 387 | #define B1110111 119 388 | #define B01110111 119 389 | #define B1111000 120 390 | #define B01111000 120 391 | #define B1111001 121 392 | #define B01111001 121 393 | #define B1111010 122 394 | #define B01111010 122 395 | #define B1111011 123 396 | #define B01111011 123 397 | #define B1111100 124 398 | #define B01111100 124 399 | #define B1111101 125 400 | #define B01111101 125 401 | #define B1111110 126 402 | #define B01111110 126 403 | #define B1111111 127 404 | #define B01111111 127 405 | #define B10000000 128 406 | #define B10000001 129 407 | #define B10000010 130 408 | #define B10000011 131 409 | #define B10000100 132 410 | #define B10000101 133 411 | #define B10000110 134 412 | #define B10000111 135 413 | #define B10001000 136 414 | #define B10001001 137 415 | #define B10001010 138 416 | #define B10001011 139 417 | #define B10001100 140 418 | #define B10001101 141 419 | #define B10001110 142 420 | #define B10001111 143 421 | #define B10010000 144 422 | #define B10010001 145 423 | #define B10010010 146 424 | #define B10010011 147 425 | #define B10010100 148 426 | #define B10010101 149 427 | #define B10010110 150 428 | #define B10010111 151 429 | #define B10011000 152 430 | #define B10011001 153 431 | #define B10011010 154 432 | #define B10011011 155 433 | #define B10011100 156 434 | #define B10011101 157 435 | #define B10011110 158 436 | #define B10011111 159 437 | #define B10100000 160 438 | #define B10100001 161 439 | #define B10100010 162 440 | #define B10100011 163 441 | #define B10100100 164 442 | #define B10100101 165 443 | #define B10100110 166 444 | #define B10100111 167 445 | #define B10101000 168 446 | #define B10101001 169 447 | #define B10101010 170 448 | #define B10101011 171 449 | #define B10101100 172 450 | #define B10101101 173 451 | #define B10101110 174 452 | #define B10101111 175 453 | #define B10110000 176 454 | #define B10110001 177 455 | #define B10110010 178 456 | #define B10110011 179 457 | #define B10110100 180 458 | #define B10110101 181 459 | #define B10110110 182 460 | #define B10110111 183 461 | #define B10111000 184 462 | #define B10111001 185 463 | #define B10111010 186 464 | #define B10111011 187 465 | #define B10111100 188 466 | #define B10111101 189 467 | #define B10111110 190 468 | #define B10111111 191 469 | #define B11000000 192 470 | #define B11000001 193 471 | #define B11000010 194 472 | #define B11000011 195 473 | #define B11000100 196 474 | #define B11000101 197 475 | #define B11000110 198 476 | #define B11000111 199 477 | #define B11001000 200 478 | #define B11001001 201 479 | #define B11001010 202 480 | #define B11001011 203 481 | #define B11001100 204 482 | #define B11001101 205 483 | #define B11001110 206 484 | #define B11001111 207 485 | #define B11010000 208 486 | #define B11010001 209 487 | #define B11010010 210 488 | #define B11010011 211 489 | #define B11010100 212 490 | #define B11010101 213 491 | #define B11010110 214 492 | #define B11010111 215 493 | #define B11011000 216 494 | #define B11011001 217 495 | #define B11011010 218 496 | #define B11011011 219 497 | #define B11011100 220 498 | #define B11011101 221 499 | #define B11011110 222 500 | #define B11011111 223 501 | #define B11100000 224 502 | #define B11100001 225 503 | #define B11100010 226 504 | #define B11100011 227 505 | #define B11100100 228 506 | #define B11100101 229 507 | #define B11100110 230 508 | #define B11100111 231 509 | #define B11101000 232 510 | #define B11101001 233 511 | #define B11101010 234 512 | #define B11101011 235 513 | #define B11101100 236 514 | #define B11101101 237 515 | #define B11101110 238 516 | #define B11101111 239 517 | #define B11110000 240 518 | #define B11110001 241 519 | #define B11110010 242 520 | #define B11110011 243 521 | #define B11110100 244 522 | #define B11110101 245 523 | #define B11110110 246 524 | #define B11110111 247 525 | #define B11111000 248 526 | #define B11111001 249 527 | #define B11111010 250 528 | #define B11111011 251 529 | #define B11111100 252 530 | #define B11111101 253 531 | #define B11111110 254 532 | #define B11111111 255 533 | 534 | #endif 535 | -------------------------------------------------------------------------------- /src/arduino/noniso.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | #include "noniso.h" 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* reverse: reverse string s in place */ 28 | /* 29 | static void reverse( char s[] ) 30 | { 31 | int i, j ; 32 | char c ; 33 | 34 | for ( i = 0, j = strlen(s)-1 ; i < j ; i++, j-- ) 35 | { 36 | c = s[i] ; 37 | s[i] = s[j] ; 38 | s[j] = c ; 39 | } 40 | } 41 | */ 42 | 43 | /* itoa: convert n to characters in s */ 44 | /* 45 | extern void itoa( int n, char s[] ) 46 | { 47 | int i, sign ; 48 | 49 | if ( (sign = n) < 0 ) // record sign 50 | { 51 | n = -n; // make n positive 52 | } 53 | 54 | i = 0; 55 | do 56 | { // generate digits in reverse order 57 | s[i++] = n % 10 + '0'; // get next digit 58 | } while ((n /= 10) > 0) ; // delete it 59 | 60 | if (sign < 0 ) 61 | { 62 | s[i++] = '-'; 63 | } 64 | 65 | s[i] = '\0'; 66 | 67 | reverse( s ) ; 68 | }*/ 69 | 70 | extern char* itoa( int value, char *string, int radix ) 71 | { 72 | return ltoa( value, string, radix ) ; 73 | } 74 | 75 | extern char* ltoa( long value, char *string, int radix ) 76 | { 77 | char tmp[33]; 78 | char *tp = tmp; 79 | long i; 80 | unsigned long v; 81 | int sign; 82 | char *sp; 83 | 84 | if ( string == NULL ) 85 | { 86 | return 0 ; 87 | } 88 | 89 | if (radix > 36 || radix <= 1) 90 | { 91 | return 0 ; 92 | } 93 | 94 | sign = (radix == 10 && value < 0); 95 | if (sign) 96 | { 97 | v = -value; 98 | } 99 | else 100 | { 101 | v = (unsigned long)value; 102 | } 103 | 104 | while (v || tp == tmp) 105 | { 106 | i = v % radix; 107 | v = v / radix; 108 | if (i < 10) 109 | *tp++ = i+'0'; 110 | else 111 | *tp++ = i + 'a' - 10; 112 | } 113 | 114 | sp = string; 115 | 116 | if (sign) 117 | *sp++ = '-'; 118 | while (tp > tmp) 119 | *sp++ = *--tp; 120 | *sp = 0; 121 | 122 | return string; 123 | } 124 | 125 | extern char* utoa( unsigned int value, char *string, int radix ) 126 | { 127 | return ultoa( value, string, radix ) ; 128 | } 129 | 130 | extern char* ultoa( unsigned long value, char *string, int radix ) 131 | { 132 | char tmp[33]; 133 | char *tp = tmp; 134 | long i; 135 | unsigned long v = value; 136 | char *sp; 137 | 138 | if ( string == NULL ) 139 | { 140 | return 0; 141 | } 142 | 143 | if (radix > 36 || radix <= 1) 144 | { 145 | return 0; 146 | } 147 | 148 | while (v || tp == tmp) 149 | { 150 | i = v % radix; 151 | v = v / radix; 152 | if (i < 10) 153 | *tp++ = i+'0'; 154 | else 155 | *tp++ = i + 'a' - 10; 156 | } 157 | 158 | sp = string; 159 | 160 | 161 | while (tp > tmp) 162 | *sp++ = *--tp; 163 | *sp = 0; 164 | 165 | return string; 166 | } 167 | 168 | char *dtostrf (double val, signed char width, unsigned char prec, char *sout) { 169 | //asm(".global _printf_float"); 170 | 171 | char fmt[20]; 172 | sprintf(fmt, "%%%d.%df", width, prec); 173 | sprintf(sout, fmt, val); 174 | return sout; 175 | } 176 | 177 | 178 | #ifdef __cplusplus 179 | } // extern "C" 180 | #endif -------------------------------------------------------------------------------- /src/arduino/noniso.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | #pragma once 20 | 21 | #ifdef __cplusplus 22 | extern "C"{ 23 | #endif 24 | 25 | //extern void itoa( int n, char s[] ) ; 26 | 27 | extern char* itoa( int value, char *string, int radix ) ; 28 | extern char* ltoa( long value, char *string, int radix ) ; 29 | extern char* utoa( unsigned int value, char *string, int radix ) ; 30 | extern char* ultoa( unsigned long value, char *string, int radix ) ; 31 | 32 | char *dtostrf(double val, signed char width, unsigned char prec, char *sout); 33 | 34 | #ifdef __cplusplus 35 | } // extern "C" 36 | #endif 37 | -------------------------------------------------------------------------------- /src/arduino/pgmspace.h: -------------------------------------------------------------------------------- 1 | /* 2 | pgmspace.h - Definitions for compatibility with AVR pgmspace macros 3 | 4 | Copyright (c) 2015 Arduino LLC 5 | 6 | Based on work of Paul Stoffregen on Teensy 3 (http://pjrc.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE 25 | */ 26 | 27 | #ifndef __PGMSPACE_H_ 28 | #define __PGMSPACE_H_ 1 29 | 30 | #include 31 | 32 | #define PROGMEM 33 | #define PGM_P const char * 34 | #define PSTR(str) (str) 35 | 36 | #define _SFR_BYTE(n) (n) 37 | 38 | typedef void prog_void; 39 | typedef char prog_char; 40 | typedef unsigned char prog_uchar; 41 | typedef int8_t prog_int8_t; 42 | typedef uint8_t prog_uint8_t; 43 | typedef int16_t prog_int16_t; 44 | typedef uint16_t prog_uint16_t; 45 | typedef int32_t prog_int32_t; 46 | typedef uint32_t prog_uint32_t; 47 | typedef int64_t prog_int64_t; 48 | typedef uint64_t prog_uint64_t; 49 | 50 | typedef const void* int_farptr_t; 51 | typedef const void* uint_farptr_t; 52 | 53 | #define memchr_P(s, c, n) memchr((s), (c), (n)) 54 | #define memcmp_P(s1, s2, n) memcmp((s1), (s2), (n)) 55 | #define memccpy_P(dest, src, c, n) memccpy((dest), (src), (c), (n)) 56 | #define memcpy_P(dest, src, n) memcpy((dest), (src), (n)) 57 | #define memmem_P(haystack, haystacklen, needle, needlelen) memmem((haystack), (haystacklen), (needle), (needlelen)) 58 | #define memrchr_P(s, c, n) memrchr((s), (c), (n)) 59 | #define strcat_P(dest, src) strcat((dest), (src)) 60 | #define strchr_P(s, c) strchr((s), (c)) 61 | #define strchrnul_P(s, c) strchrnul((s), (c)) 62 | #define strcmp_P(a, b) strcmp((a), (b)) 63 | #define strcpy_P(dest, src) strcpy((dest), (src)) 64 | #define strcasecmp_P(s1, s2) strcasecmp((s1), (s2)) 65 | #define strcasestr_P(haystack, needle) strcasestr((haystack), (needle)) 66 | #define strcspn_P(s, accept) strcspn((s), (accept)) 67 | #define strlcat_P(s1, s2, n) strlcat((s1), (s2), (n)) 68 | #define strlcpy_P(s1, s2, n) strlcpy((s1), (s2), (n)) 69 | #define strlen_P(a) strlen((a)) 70 | #define strnlen_P(s, n) strnlen((s), (n)) 71 | #define strncmp_P(s1, s2, n) strncmp((s1), (s2), (n)) 72 | #define strncasecmp_P(s1, s2, n) strncasecmp((s1), (s2), (n)) 73 | #define strncat_P(s1, s2, n) strncat((s1), (s2), (n)) 74 | #define strncpy_P(s1, s2, n) strncpy((s1), (s2), (n)) 75 | #define strpbrk_P(s, accept) strpbrk((s), (accept)) 76 | #define strrchr_P(s, c) strrchr((s), (c)) 77 | #define strsep_P(sp, delim) strsep((sp), (delim)) 78 | #define strspn_P(s, accept) strspn((s), (accept)) 79 | #define strstr_P(a, b) strstr((a), (b)) 80 | #define strtok_P(s, delim) strtok((s), (delim)) 81 | #define strtok_rP(s, delim, last) strtok((s), (delim), (last)) 82 | 83 | #define strlen_PF(a) strlen((a)) 84 | #define strnlen_PF(src, len) strnlen((src), (len)) 85 | #define memcpy_PF(dest, src, len) memcpy((dest), (src), (len)) 86 | #define strcpy_PF(dest, src) strcpy((dest), (src)) 87 | #define strncpy_PF(dest, src, len) strncpy((dest), (src), (len)) 88 | #define strcat_PF(dest, src) strcat((dest), (src)) 89 | #define strlcat_PF(dest, src, len) strlcat((dest), (src), (len)) 90 | #define strncat_PF(dest, src, len) strncat((dest), (src), (len)) 91 | #define strcmp_PF(s1, s2) strcmp((s1), (s2)) 92 | #define strncmp_PF(s1, s2, n) strncmp((s1), (s2), (n)) 93 | #define strcasecmp_PF(s1, s2) strcasecmp((s1), (s2)) 94 | #define strncasecmp_PF(s1, s2, n) strncasecmp((s1), (s2), (n)) 95 | #define strstr_PF(s1, s2) strstr((s1), (s2)) 96 | #define strlcpy_PF(dest, src, n) strlcpy((dest), (src), (n)) 97 | #define memcmp_PF(s1, s2, n) memcmp((s1), (s2), (n)) 98 | 99 | #define sprintf_P(s, f, ...) sprintf((s), (f), __VA_ARGS__) 100 | #define snprintf_P(s, f, ...) snprintf((s), (f), __VA_ARGS__) 101 | 102 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) 103 | #define pgm_read_word(addr) (*(const unsigned short *)(addr)) 104 | #define pgm_read_dword(addr) (*(const unsigned long *)(addr)) 105 | #define pgm_read_float(addr) (*(const float *)(addr)) 106 | #define pgm_read_ptr(addr) (*(const void * const *)(addr)) 107 | 108 | #define pgm_read_byte_near(addr) pgm_read_byte(addr) 109 | #define pgm_read_word_near(addr) pgm_read_word(addr) 110 | #define pgm_read_dword_near(addr) pgm_read_dword(addr) 111 | #define pgm_read_float_near(addr) pgm_read_float(addr) 112 | #define pgm_read_ptr_near(addr) pgm_read_ptr(addr) 113 | 114 | #define pgm_read_byte_far(addr) pgm_read_byte(addr) 115 | #define pgm_read_word_far(addr) pgm_read_word(addr) 116 | #define pgm_read_dword_far(addr) pgm_read_dword(addr) 117 | #define pgm_read_float_far(addr) pgm_read_float(addr) 118 | #define pgm_read_ptr_far(addr) pgm_read_ptr(addr) 119 | 120 | #define pgm_get_far_address(addr) (&(addr)) 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /src/arduino/pins_arduino.h: -------------------------------------------------------------------------------- 1 | /* 2 | pins_arduino.h - Pin definition functions for Arduino 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2007 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | */ 22 | 23 | #ifndef Pins_Arduino_h 24 | #define Pins_Arduino_h 25 | 26 | #define NUM_DIGITAL_PINS 20 27 | #define NUM_ANALOG_INPUTS 6 28 | #define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1) 29 | 30 | #define digitalPinHasPWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11) 31 | 32 | #define PIN_SPI_SS (10) 33 | #define PIN_SPI_MOSI (11) 34 | #define PIN_SPI_MISO (12) 35 | #define PIN_SPI_SCK (13) 36 | 37 | static const uint8_t SS = PIN_SPI_SS; 38 | static const uint8_t MOSI = PIN_SPI_MOSI; 39 | static const uint8_t MISO = PIN_SPI_MISO; 40 | static const uint8_t SCK = PIN_SPI_SCK; 41 | 42 | #define PIN_WIRE_SDA (18) 43 | #define PIN_WIRE_SCL (19) 44 | 45 | static const uint8_t SDA = PIN_WIRE_SDA; 46 | static const uint8_t SCL = PIN_WIRE_SCL; 47 | 48 | #define LED_BUILTIN 13 49 | 50 | #define PIN_A0 (14) 51 | #define PIN_A1 (15) 52 | #define PIN_A2 (16) 53 | #define PIN_A3 (17) 54 | #define PIN_A4 (18) 55 | #define PIN_A5 (19) 56 | #define PIN_A6 (20) 57 | #define PIN_A7 (21) 58 | 59 | static const uint8_t A0 = PIN_A0; 60 | static const uint8_t A1 = PIN_A1; 61 | static const uint8_t A2 = PIN_A2; 62 | static const uint8_t A3 = PIN_A3; 63 | static const uint8_t A4 = PIN_A4; 64 | static const uint8_t A5 = PIN_A5; 65 | static const uint8_t A6 = PIN_A6; 66 | static const uint8_t A7 = PIN_A7; 67 | 68 | #define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0)) 69 | #define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1)) 70 | #define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0)))) 71 | #define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14))) 72 | 73 | #define digitalPinToInterrupt(p) ((p) == 2 ? 0 : ((p) == 3 ? 1 : NOT_AN_INTERRUPT)) 74 | 75 | 76 | // These serial port names are intended to allow libraries and architecture-neutral 77 | // sketches to automatically default to the correct port name for a particular type 78 | // of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN, 79 | // the first hardware serial port whose RX/TX pins are not dedicated to another use. 80 | // 81 | // SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor 82 | // 83 | // SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial 84 | // 85 | // SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library 86 | // 87 | // SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins. 88 | // 89 | // SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX 90 | // pins are NOT connected to anything by default. 91 | #define SERIAL_PORT_MONITOR Serial 92 | #define SERIAL_PORT_HARDWARE Serial 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories(../src) 2 | 3 | file(GLOB cpp_test_files *.cpp) 4 | 5 | add_definitions(-DUNIT_TEST) 6 | 7 | foreach(filename ${cpp_test_files}) 8 | 9 | get_filename_component(target ${filename} NAME_WE) 10 | add_executable(${target} ${filename}) 11 | 12 | set_target_properties(${target} PROPERTIES DEBUG_POSTFIX "-d") 13 | target_link_libraries(${target} ${PROJECT_NAME} unity) 14 | add_test(NAME ${target} COMMAND ${target}) 15 | 16 | endforeach(filename) 17 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | // clang-format off 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace fakeit; 7 | 8 | #include "test_context.h" 9 | #include "test_function.h" 10 | #include "test_print.h" 11 | #include "test_stream.h" 12 | #include "test_serial.h" 13 | #include "test_wire.h" 14 | #include "test_spi.h" 15 | #include "test_eeprom.h" 16 | #include "test_client.h" 17 | #include "test_arduino_string.h" 18 | #include "test_include.h" 19 | 20 | #ifdef UNIT_TEST 21 | 22 | #define RUN_TEST_GROUP(TEST) \ 23 | if (!std::getenv("TEST_GROUP") || (strcmp(#TEST, std::getenv("TEST_GROUP")) == 0)) { \ 24 | TEST::run_tests(); \ 25 | } 26 | 27 | void setUp(void) 28 | { 29 | ArduinoFakeReset(); 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | UNITY_BEGIN(); 35 | 36 | RUN_TEST_GROUP(ArduinoStringTest); 37 | 38 | RUN_TEST_GROUP(ArduinoContextTest); 39 | RUN_TEST_GROUP(FunctionTest); 40 | RUN_TEST_GROUP(PrintTest); 41 | RUN_TEST_GROUP(StreamTest); 42 | RUN_TEST_GROUP(SerialTest); 43 | RUN_TEST_GROUP(WireTest); 44 | RUN_TEST_GROUP(SpiTest); 45 | RUN_TEST_GROUP(EEPROMTest); 46 | RUN_TEST_GROUP(ClientTest); 47 | RUN_TEST_GROUP(IncludeTest); 48 | 49 | UNITY_END(); 50 | 51 | return 0; 52 | } 53 | 54 | #endif 55 | // clang-format on -------------------------------------------------------------------------------- /test/test_arduino_string.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace ArduinoStringTest 4 | { 5 | void test_constructors(void) 6 | { 7 | String string01 = "Hello String"; 8 | String string02 = String('a'); 9 | String string03 = String("This is a string"); 10 | String string04 = String(string03 + " with more"); 11 | String string05 = String(13); 12 | String string06 = String(1000, DEC); 13 | String string07 = String(45, HEX); 14 | String string08 = String(255, BIN); 15 | String string09 = String(20000L, DEC); 16 | String string10 = String(5.698, 3); 17 | 18 | TEST_ASSERT_EQUAL_STRING("Hello String", string01.c_str()); 19 | TEST_ASSERT_EQUAL_STRING("a", string02.c_str()); 20 | TEST_ASSERT_EQUAL_STRING("This is a string", string03.c_str()); 21 | TEST_ASSERT_EQUAL_STRING("This is a string with more", string04.c_str()); 22 | 23 | TEST_ASSERT_EQUAL_STRING("13", string05.c_str()); 24 | TEST_ASSERT_EQUAL_STRING("1000", string06.c_str()); 25 | TEST_ASSERT_EQUAL_STRING("2d", string07.c_str()); 26 | TEST_ASSERT_EQUAL_STRING("11111111", string08.c_str()); 27 | TEST_ASSERT_EQUAL_STRING("20000", string09.c_str()); 28 | TEST_ASSERT_EQUAL_STRING("5.698", string10.c_str()); 29 | } 30 | 31 | void run_tests() 32 | { 33 | RUN_TEST(ArduinoStringTest::test_constructors); 34 | } 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /test/test_client.h: -------------------------------------------------------------------------------- 1 | const char * host = "localhost"; 2 | 3 | #ifdef UNIT_TEST 4 | 5 | namespace ClientTest 6 | { 7 | const char * localhost = "localhost"; 8 | 9 | class MyService 10 | { 11 | public: 12 | MyService(Client* client) 13 | { 14 | _client = client; 15 | } 16 | 17 | size_t send(uint16_t value) 18 | { 19 | return _client->write(value); 20 | } 21 | 22 | private: 23 | Client* _client; 24 | }; 25 | 26 | void test_basics(void) 27 | { 28 | When(Method(ArduinoFake(Client), stop)).Return(); 29 | When(Method(ArduinoFake(Client), peek)).Return(2); 30 | When(Method(ArduinoFake(Client), flush)).Return(); 31 | When(Method(ArduinoFake(Client), connected)).Return(0, 1); 32 | When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1); 33 | 34 | Client* client = ArduinoFakeMock(Client); 35 | 36 | TEST_ASSERT_EQUAL(0, client->connected()); 37 | TEST_ASSERT_EQUAL(1, client->connect(localhost, 8080)); 38 | TEST_ASSERT_EQUAL(1, client->connected()); 39 | TEST_ASSERT_EQUAL(2, client->peek()); 40 | 41 | client->flush(); 42 | client->stop(); 43 | 44 | Verify(Method(ArduinoFake(Client), stop)).Once(); 45 | Verify(Method(ArduinoFake(Client), peek)).Once(); 46 | Verify(Method(ArduinoFake(Client), flush)).Once(); 47 | Verify(Method(ArduinoFake(Client), connected)).Exactly(2_Times); 48 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once(); 49 | } 50 | 51 | void test_connect(void) 52 | { 53 | When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1, 0); 54 | When(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t))).Return(0, 1); 55 | 56 | IPAddress ipAddress1(62, 145, 182, 225); 57 | IPAddress ipAddress2(221, 155, 131, 19); 58 | 59 | Client* client = ArduinoFakeMock(Client); 60 | 61 | TEST_ASSERT_EQUAL(1, client->connect(localhost, 8080)); 62 | TEST_ASSERT_EQUAL(0, client->connect(localhost, 80)); 63 | 64 | TEST_ASSERT_EQUAL(0, client->connect(ipAddress1, 8080)); 65 | TEST_ASSERT_EQUAL(1, client->connect(ipAddress2, 8080)); 66 | 67 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once(); 68 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 80)).Once(); 69 | 70 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t)).Using(ipAddress1, 8080)).Once(); 71 | Verify(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t)).Using(ipAddress2, 8080)).Once(); 72 | } 73 | 74 | void test_write(void) 75 | { 76 | uint8_t val1 = 0x0; 77 | uint8_t val2 = 0x1; 78 | 79 | const uint8_t* ptr1 = &val1; 80 | const uint8_t* ptr2 = &val2; 81 | 82 | When(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t))).Return(1, 0); 83 | When(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t))).Return(0, 1); 84 | 85 | Client* client = ArduinoFakeMock(Client); 86 | 87 | TEST_ASSERT_EQUAL(1, client->write(val1)); 88 | TEST_ASSERT_EQUAL(0, client->write(val2)); 89 | 90 | TEST_ASSERT_EQUAL(0, client->write(ptr1, 2)); 91 | TEST_ASSERT_EQUAL(1, client->write(ptr2, 3)); 92 | 93 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val1)).Once(); 94 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val2)).Once(); 95 | 96 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t)).Using(ptr1, 2)).Once(); 97 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t)).Using(ptr2, 3)).Once(); 98 | } 99 | 100 | void test_read(void) 101 | { 102 | uint8_t val1 = 0x0; 103 | uint8_t val2 = 0x1; 104 | 105 | uint8_t* ptr1 = &val1; 106 | uint8_t* ptr2 = &val2; 107 | 108 | When(OverloadedMethod(ArduinoFake(Client), read, int())).Return(10, 20); 109 | When(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t))).Return(30, 400); 110 | 111 | Client* client = ArduinoFakeMock(Client); 112 | 113 | TEST_ASSERT_EQUAL(10, client->read()); 114 | TEST_ASSERT_EQUAL(20, client->read()); 115 | 116 | TEST_ASSERT_EQUAL(30, client->read(ptr1, 2)); 117 | TEST_ASSERT_EQUAL(400, client->read(ptr2, 3)); 118 | 119 | Verify(OverloadedMethod(ArduinoFake(Client), read, int())).Exactly(2_Times); 120 | 121 | Verify(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t)).Using(ptr1, 2)).Once(); 122 | Verify(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t)).Using(ptr2, 3)).Once(); 123 | } 124 | 125 | void test_inject_instance(void) 126 | { 127 | uint8_t val1 = 0x0; 128 | uint8_t val2 = 0x1; 129 | 130 | When(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t))).Return(11, 22); 131 | 132 | Client* client = ArduinoFakeMock(Client); 133 | 134 | MyService service(client); 135 | 136 | TEST_ASSERT_EQUAL(11, service.send(val1)); 137 | TEST_ASSERT_EQUAL(22, service.send(val2)); 138 | 139 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val1)).Once(); 140 | Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val2)).Once(); 141 | } 142 | 143 | void run_tests() 144 | { 145 | RUN_TEST(ClientTest::test_basics); 146 | RUN_TEST(ClientTest::test_connect); 147 | RUN_TEST(ClientTest::test_write); 148 | RUN_TEST(ClientTest::test_read); 149 | RUN_TEST(ClientTest::test_inject_instance); 150 | } 151 | } 152 | 153 | #endif 154 | 155 | -------------------------------------------------------------------------------- /test/test_context.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace ArduinoContextTest 4 | { 5 | void test_single_instance(void) 6 | { 7 | ArduinoFakeContext* context1 = getArduinoFakeContext(); 8 | ArduinoFakeContext* context2 = getArduinoFakeContext(); 9 | 10 | TEST_ASSERT_NOT_NULL(context1); 11 | TEST_ASSERT_NOT_NULL(context2); 12 | 13 | TEST_ASSERT_EQUAL(context1, context2); 14 | } 15 | 16 | void test_reset(void) 17 | { 18 | ArduinoFakeContext* context = getArduinoFakeContext(); 19 | ArduinoFakeInstances* instances = context->Instances; 20 | 21 | ArduinoFakeReset(); 22 | 23 | TEST_ASSERT_NOT_EQUAL(context->Instances, instances); 24 | } 25 | 26 | void test_function_mock(void) 27 | { 28 | Mock* m1 = &ArduinoFake(Function); 29 | Mock* m2 = &ArduinoFake(Function); 30 | Mock* m3 = &ArduinoFake(); 31 | 32 | TEST_ASSERT_NOT_NULL(m1); 33 | TEST_ASSERT_NOT_NULL(m2); 34 | TEST_ASSERT_NOT_NULL(m3); 35 | 36 | TEST_ASSERT_EQUAL(m1, m2); 37 | TEST_ASSERT_EQUAL(m1, m3); 38 | 39 | FunctionFake* i1 = ArduinoFakeInstance(Function); 40 | FunctionFake* i2 = ArduinoFakeInstance(Function); 41 | 42 | TEST_ASSERT_NOT_NULL(i1); 43 | TEST_ASSERT_NOT_NULL(i2); 44 | TEST_ASSERT_EQUAL(i1, i2); 45 | } 46 | 47 | void test_print_mock(void) 48 | { 49 | Mock* m1 = &ArduinoFake(Print); 50 | Mock* m2 = &ArduinoFake(Print); 51 | 52 | TEST_ASSERT_NOT_NULL(m1); 53 | TEST_ASSERT_NOT_NULL(m2); 54 | TEST_ASSERT_EQUAL(m1, m2); 55 | 56 | PrintFake* i1 = ArduinoFakeInstance(Print); 57 | PrintFake* i2 = ArduinoFakeInstance(Print); 58 | 59 | TEST_ASSERT_NOT_NULL(i1); 60 | TEST_ASSERT_NOT_NULL(i2); 61 | TEST_ASSERT_EQUAL(i1, i2); 62 | } 63 | 64 | void test_stream_mock(void) 65 | { 66 | Mock* m1 = &ArduinoFake(Stream); 67 | Mock* m2 = &ArduinoFake(Stream); 68 | 69 | TEST_ASSERT_NOT_NULL(m1); 70 | TEST_ASSERT_NOT_NULL(m2); 71 | TEST_ASSERT_EQUAL(m1, m2); 72 | 73 | StreamFake* i1 = ArduinoFakeInstance(Stream); 74 | StreamFake* i2 = ArduinoFakeInstance(Stream); 75 | 76 | TEST_ASSERT_NOT_NULL(i1); 77 | TEST_ASSERT_NOT_NULL(i2); 78 | TEST_ASSERT_EQUAL(i1, i2); 79 | } 80 | 81 | void test_serial_mock(void) 82 | { 83 | Mock* m1 = &ArduinoFake(Serial); 84 | Mock* m2 = &ArduinoFake(Serial); 85 | 86 | TEST_ASSERT_NOT_NULL(m1); 87 | TEST_ASSERT_NOT_NULL(m2); 88 | TEST_ASSERT_EQUAL(m1, m2); 89 | 90 | SerialFake* i1 = ArduinoFakeInstance(Serial); 91 | SerialFake* i2 = ArduinoFakeInstance(Serial); 92 | 93 | TEST_ASSERT_NOT_NULL(i1); 94 | TEST_ASSERT_NOT_NULL(i2); 95 | TEST_ASSERT_EQUAL(i1, i2); 96 | } 97 | 98 | void test_unknown_instance_exception(void) 99 | { 100 | fakeit::Mock fake; 101 | 102 | try { 103 | ArduinoFakeInstance(Print, &fake.get()); 104 | } catch (const std::runtime_error& e) { 105 | TEST_ASSERT_EQUAL_STRING("Unknown instance", e.what()); 106 | } 107 | } 108 | 109 | void test_getter_overload_with_proxy(void) 110 | { 111 | Serial_* serial = ArduinoFakeMock(Serial); 112 | PrintFake* fake = ArduinoFakeInstance(Stream, serial); 113 | 114 | TEST_ASSERT_EQUAL(getArduinoFakeContext()->Serial(), fake); 115 | } 116 | 117 | void test_getter_overload_with_mapping(void) 118 | { 119 | Serial_* serial = &::Serial; 120 | PrintFake* fake = ArduinoFakeInstance(Stream, serial); 121 | 122 | TEST_ASSERT_EQUAL(getArduinoFakeContext()->Serial(), fake); 123 | } 124 | 125 | void run_tests(void) 126 | { 127 | RUN_TEST(ArduinoContextTest::test_single_instance); 128 | RUN_TEST(ArduinoContextTest::test_reset); 129 | RUN_TEST(ArduinoContextTest::test_function_mock); 130 | RUN_TEST(ArduinoContextTest::test_print_mock); 131 | RUN_TEST(ArduinoContextTest::test_stream_mock); 132 | RUN_TEST(ArduinoContextTest::test_serial_mock); 133 | RUN_TEST(ArduinoContextTest::test_getter_overload_with_proxy); 134 | RUN_TEST(ArduinoContextTest::test_getter_overload_with_mapping); 135 | RUN_TEST(ArduinoContextTest::test_unknown_instance_exception); 136 | } 137 | } 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /test/test_eeprom.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace EEPROMTest { 4 | 5 | #include "arduino/EEPROM.h" 6 | 7 | void test_basics(void) { 8 | When(OverloadedMethod(ArduinoFake(EEPROM), read, uint8_t(int))) 9 | .AlwaysReturn(); 10 | When(OverloadedMethod(ArduinoFake(EEPROM), write, void(int, uint8_t))) 11 | .AlwaysReturn(); 12 | When(OverloadedMethod(ArduinoFake(EEPROM), update, void(int, uint8_t))) 13 | .AlwaysReturn(); 14 | When(OverloadedMethod(ArduinoFake(EEPROM), length, uint16_t(void))) 15 | .AlwaysReturn(); 16 | 17 | EEPROM.read(1); 18 | EEPROM.write(1, 1); 19 | EEPROM.update(1, 2); 20 | EEPROM.length(); 21 | 22 | Verify(OverloadedMethod(ArduinoFake(EEPROM), read, uint8_t(int))).Once(); 23 | Verify(OverloadedMethod(ArduinoFake(EEPROM), write, void(int, uint8_t))) 24 | .Once(); 25 | Verify(OverloadedMethod(ArduinoFake(EEPROM), update, void(int, uint8_t))) 26 | .Once(); 27 | Verify(OverloadedMethod(ArduinoFake(EEPROM), length, uint16_t(void))).Once(); 28 | } 29 | 30 | void run_tests() { RUN_TEST(EEPROMTest::test_basics); } 31 | } // namespace EEPROMTest 32 | 33 | #endif -------------------------------------------------------------------------------- /test/test_function.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace FunctionTest 4 | { 5 | void test_timestamps(void) 6 | { 7 | When(Method(ArduinoFake(), micros)).AlwaysReturn(100000); 8 | When(Method(ArduinoFake(), millis)).AlwaysReturn(200000); 9 | 10 | TEST_ASSERT_EQUAL(100000, micros()); 11 | TEST_ASSERT_EQUAL(200000, millis()); 12 | 13 | Verify(Method(ArduinoFake(), micros)).Once(); 14 | Verify(Method(ArduinoFake(), millis)).Once(); 15 | } 16 | 17 | void test_pin_mode(void) 18 | { 19 | When(Method(ArduinoFake(), pinMode)).AlwaysReturn(); 20 | 21 | pinMode(3, INPUT); 22 | Verify(Method(ArduinoFake(), pinMode).Using(3, INPUT)).Once(); 23 | 24 | pinMode(3, OUTPUT); 25 | Verify(Method(ArduinoFake(), pinMode).Using(3, INPUT)).Once(); 26 | } 27 | 28 | void test_digital_pin(void) 29 | { 30 | When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn(); 31 | When(Method(ArduinoFake(), digitalRead)).AlwaysReturn(LOW); 32 | 33 | digitalWrite(3, HIGH); 34 | Verify(Method(ArduinoFake(), digitalWrite).Using(3, HIGH)).Once(); 35 | 36 | TEST_ASSERT_EQUAL(LOW, digitalRead(4)); 37 | Verify(Method(ArduinoFake(), digitalRead).Using(4)).Once(); 38 | } 39 | 40 | void test_analog_pin(void) 41 | { 42 | When(Method(ArduinoFake(), analogWrite)).AlwaysReturn(); 43 | When(Method(ArduinoFake(), analogRead)).AlwaysReturn(123); 44 | 45 | analogWrite(3, 321); 46 | Verify(Method(ArduinoFake(), analogWrite).Using(3, 321)).Once(); 47 | 48 | TEST_ASSERT_EQUAL(123, analogRead(4)); 49 | Verify(Method(ArduinoFake(), analogRead).Using(4)).Once(); 50 | } 51 | 52 | void test_analog_read_resolution(void) 53 | { 54 | When(Method(ArduinoFake(), analogReadResolution)).AlwaysReturn(); 55 | 56 | analogReadResolution(12); 57 | 58 | Verify(Method(ArduinoFake(), analogReadResolution).Using(12)).Once(); 59 | } 60 | 61 | void test_yield(void) 62 | { 63 | When(Method(ArduinoFake(), yield)).AlwaysReturn(); 64 | 65 | yield(); 66 | 67 | Verify(Method(ArduinoFake(), yield)).Once(); 68 | } 69 | 70 | void test_delay(void) 71 | { 72 | When(Method(ArduinoFake(), delay)).AlwaysReturn(); 73 | When(Method(ArduinoFake(), delayMicroseconds)).AlwaysReturn(); 74 | 75 | delay(100); 76 | delayMicroseconds(200); 77 | 78 | Verify(Method(ArduinoFake(), delay).Using(100)).Once(); 79 | Verify(Method(ArduinoFake(), delayMicroseconds).Using(200)).Once(); 80 | } 81 | 82 | void test_pulsein(void) 83 | { 84 | When(Method(ArduinoFake(), pulseIn)).AlwaysReturn(1000); 85 | 86 | TEST_ASSERT_EQUAL(1000, pulseIn(10, HIGH, 1234)); 87 | 88 | Verify(Method(ArduinoFake(), pulseIn).Using(10, HIGH, 1234)).Once(); 89 | } 90 | 91 | void test_shift(void) 92 | { 93 | When(Method(ArduinoFake(), shiftIn)).AlwaysReturn(8); 94 | When(Method(ArduinoFake(), shiftOut)).AlwaysReturn(); 95 | 96 | shiftOut(10, 11, LSBFIRST, 8); 97 | Verify(Method(ArduinoFake(), shiftOut).Using(10, 11, LSBFIRST, 8)).Once(); 98 | 99 | TEST_ASSERT_EQUAL(8, shiftIn(5, 6, MSBFIRST)); 100 | Verify(Method(ArduinoFake(), shiftIn).Using(5, 6, MSBFIRST)).Once(); 101 | } 102 | 103 | void test_detach(void) 104 | { 105 | When(Method(ArduinoFake(), detachInterrupt)).AlwaysReturn(); 106 | 107 | detachInterrupt(1); 108 | 109 | Verify(Method(ArduinoFake(), detachInterrupt).Using(1)).Once(); 110 | } 111 | 112 | void test_attach(void) 113 | { 114 | When(Method(ArduinoFake(), attachInterrupt)).AlwaysReturn(); 115 | 116 | attachInterrupt(1, (void (*)(void))NULL, FALLING); 117 | attachInterrupt(2, (void (*)(void))NULL, CHANGE); 118 | attachInterrupt(3, (void (*)(void))NULL, RISING); 119 | 120 | Verify(Method(ArduinoFake(), attachInterrupt)).Exactly(3); 121 | } 122 | 123 | void test_cli(void) 124 | { 125 | When(Method(ArduinoFake(), cli)).AlwaysReturn(); 126 | 127 | cli(); 128 | 129 | Verify(Method(ArduinoFake(), cli)).Once(); 130 | } 131 | 132 | void test_sei(void) 133 | { 134 | When(Method(ArduinoFake(), sei)).AlwaysReturn(); 135 | 136 | sei(); 137 | 138 | Verify(Method(ArduinoFake(), sei)).Once(); 139 | } 140 | 141 | void test_random(void) 142 | { 143 | When(Method(ArduinoFake(), randomSeed)).AlwaysReturn(); 144 | When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(10, 11); 145 | When(OverloadedMethod(ArduinoFake(), random, long(long, long))).Return(20, 21); 146 | 147 | randomSeed(123); 148 | 149 | TEST_ASSERT_EQUAL(10, random(10)); 150 | TEST_ASSERT_EQUAL(11, random(15)); 151 | TEST_ASSERT_EQUAL(20, random(5, 20)); 152 | TEST_ASSERT_EQUAL(21, random(10, 25)); 153 | 154 | Verify(Method(ArduinoFake(), randomSeed).Using(123)).Once(); 155 | 156 | Verify(OverloadedMethod(ArduinoFake(), random, long(long)).Using(10)).Once(); 157 | Verify(OverloadedMethod(ArduinoFake(), random, long(long)).Using(15)).Once(); 158 | 159 | Verify(OverloadedMethod(ArduinoFake(), random, long(long, long)).Using(5, 20)).Once(); 160 | Verify(OverloadedMethod(ArduinoFake(), random, long(long, long)).Using(10, 25)).Once(); 161 | } 162 | 163 | void test_tone(void) 164 | { 165 | When(Method(ArduinoFake(), tone)).AlwaysReturn(); 166 | When(Method(ArduinoFake(), noTone)).AlwaysReturn(); 167 | 168 | tone(7, 1047, 8); 169 | tone(8, 1319, 4); 170 | tone(9, 1568, 2); 171 | 172 | noTone(7); 173 | noTone(8); 174 | noTone(9); 175 | 176 | Verify(Method(ArduinoFake(), tone).Using(7, 1047, 8)).Once(); 177 | Verify(Method(ArduinoFake(), tone).Using(8, 1319, 4)).Once(); 178 | Verify(Method(ArduinoFake(), tone).Using(9, 1568, 2)).Once(); 179 | 180 | Verify(Method(ArduinoFake(), noTone).Using(7)).Once(); 181 | Verify(Method(ArduinoFake(), noTone).Using(8)).Once(); 182 | Verify(Method(ArduinoFake(), noTone).Using(9)).Once(); 183 | } 184 | 185 | void test_map(void) 186 | { 187 | When(Method(ArduinoFake(), map)).Return(5); 188 | 189 | TEST_ASSERT_EQUAL(5, map(50, 0, 100, 0, 10)); 190 | 191 | Verify(Method(ArduinoFake(), map).Using(50, 0, 100, 0, 10)).Once(); 192 | } 193 | 194 | void run_tests(void) 195 | { 196 | RUN_TEST(FunctionTest::test_timestamps); 197 | RUN_TEST(FunctionTest::test_pin_mode); 198 | RUN_TEST(FunctionTest::test_digital_pin); 199 | RUN_TEST(FunctionTest::test_analog_pin); 200 | RUN_TEST(FunctionTest::test_analog_read_resolution); 201 | RUN_TEST(FunctionTest::test_delay); 202 | RUN_TEST(FunctionTest::test_detach); 203 | RUN_TEST(FunctionTest::test_attach); 204 | RUN_TEST(FunctionTest::test_cli); 205 | RUN_TEST(FunctionTest::test_sei); 206 | RUN_TEST(FunctionTest::test_pulsein); 207 | RUN_TEST(FunctionTest::test_shift); 208 | RUN_TEST(FunctionTest::test_random); 209 | RUN_TEST(FunctionTest::test_tone); 210 | RUN_TEST(FunctionTest::test_map); 211 | } 212 | } 213 | 214 | #endif 215 | -------------------------------------------------------------------------------- /test/test_include.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace IncludeTest 4 | { 5 | 6 | void test_empty(void) 7 | { 8 | int PROGMEM a = 1; 9 | } 10 | 11 | void run_tests(void) 12 | { 13 | RUN_TEST(IncludeTest::test_empty); 14 | } 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /test/test_print.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace PrintTest 4 | { 5 | void test_print_variables(void) 6 | { 7 | char char_var = 'A'; 8 | const char * char_array_var = "char_array_var"; 9 | unsigned char unsigned_char_var = 'B'; 10 | 11 | int int_var = 123; 12 | long long_var = 12345678; 13 | double double_var = 98765.12; 14 | unsigned int unsigned_int_var = 321; 15 | unsigned long unsigned_long_var = 87654321; 16 | 17 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(char))).AlwaysReturn(); 18 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(const char *))).AlwaysReturn(); 19 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned char, int))).AlwaysReturn(); 20 | 21 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(int, int))).AlwaysReturn(); 22 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(long, int))).AlwaysReturn(); 23 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(double, int))).AlwaysReturn(); 24 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned int, int))).AlwaysReturn(); 25 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned long, int))).AlwaysReturn(); 26 | 27 | Print* print = ArduinoFakeMock(Print); 28 | 29 | print->print(char_var); 30 | print->print(char_array_var); 31 | print->print(unsigned_char_var, DEC); 32 | 33 | print->print(int_var, DEC); 34 | print->print(long_var, DEC); 35 | print->print(double_var, BIN); 36 | print->print(unsigned_int_var, DEC); 37 | print->print(unsigned_long_var, DEC); 38 | 39 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(char)).Using(char_var)).Once(); 40 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(const char *)).Using(char_array_var)).Once(); 41 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned char, int)).Using(unsigned_char_var, DEC)).Once(); 42 | 43 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(int, int)).Using(int_var, DEC)).Once(); 44 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(long, int)).Using(long_var, DEC)).Once(); 45 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(double, int)).Using(double_var, BIN)).Once(); 46 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned int, int)).Using(unsigned_int_var, DEC)).Once(); 47 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(unsigned long, int)).Using(unsigned_long_var, DEC)).Once(); 48 | } 49 | 50 | void test_println_variables(void) 51 | { 52 | char char_var = 'A'; 53 | unsigned char unsigned_char_var = 'B'; 54 | const char * char_array_var = "char_array_var"; 55 | 56 | int int_var = 123; 57 | long long_var = 12345678; 58 | double double_var = 98765.12; 59 | unsigned int unsigned_int_var = 321; 60 | unsigned long unsigned_long_var = 87654321; 61 | 62 | When(OverloadedMethod(ArduinoFake(Print), println, size_t())).AlwaysReturn(); 63 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(char))).AlwaysReturn(); 64 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(const char *))).AlwaysReturn(); 65 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned char, int))).AlwaysReturn(); 66 | 67 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(int, int))).AlwaysReturn(); 68 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(long, int))).AlwaysReturn(); 69 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(double, int))).AlwaysReturn(); 70 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned int, int))).AlwaysReturn(); 71 | When(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned long, int))).AlwaysReturn(); 72 | 73 | Print* print = ArduinoFakeMock(Print); 74 | 75 | print->println(); 76 | print->println(char_var); 77 | print->println(char_array_var); 78 | print->println(unsigned_char_var, DEC); 79 | 80 | print->println(int_var, DEC); 81 | print->println(long_var, DEC); 82 | print->println(double_var, BIN); 83 | print->println(unsigned_int_var, DEC); 84 | print->println(unsigned_long_var, DEC); 85 | 86 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t())).Once(); 87 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(char)).Using(char_var)).Once(); 88 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(const char *)).Using(char_array_var)).Once(); 89 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned char, int)).Using(unsigned_char_var, DEC)).Once(); 90 | 91 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(int, int)).Using(int_var, DEC)).Once(); 92 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(long, int)).Using(long_var, DEC)).Once(); 93 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(double, int)).Using(double_var, BIN)).Once(); 94 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned int, int)).Using(unsigned_int_var, DEC)).Once(); 95 | Verify(OverloadedMethod(ArduinoFake(Print), println, size_t(unsigned long, int)).Using(unsigned_long_var, DEC)).Once(); 96 | } 97 | 98 | void run_tests() 99 | { 100 | RUN_TEST(PrintTest::test_print_variables); 101 | RUN_TEST(PrintTest::test_println_variables); 102 | } 103 | } 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /test/test_serial.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace SerialTest 4 | { 5 | void test_extends_stream(void) 6 | { 7 | TEST_ASSERT_NOT_EQUAL( 8 | ArduinoFakeInstance(Stream), 9 | ArduinoFakeInstance(Serial) 10 | ); 11 | 12 | char print_char_var = 'A'; 13 | char stream_char_var = 'B'; 14 | 15 | int print_int_var = 123; 16 | int stream_int_var = 321; 17 | 18 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(char))).AlwaysReturn(); 19 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int))).AlwaysReturn(); 20 | 21 | When(OverloadedMethod(ArduinoFake(Serial), print, size_t(char))).AlwaysReturn(); 22 | When(OverloadedMethod(ArduinoFake(Serial), print, size_t(int, int))).AlwaysReturn(); 23 | 24 | Stream* stream = ArduinoFakeMock(Stream); 25 | Serial_* serial = ArduinoFakeMock(Serial); 26 | 27 | stream->print(stream_char_var); 28 | stream->print(stream_int_var, DEC); 29 | 30 | serial->print(print_char_var); 31 | serial->print(print_int_var, DEC); 32 | 33 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(char)).Using(stream_char_var)).Once(); 34 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int)).Using(stream_int_var, DEC)).Once(); 35 | 36 | Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(char)).Using(print_char_var)).Once(); 37 | Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(int, int)).Using(print_int_var, DEC)).Once(); 38 | } 39 | 40 | void test_global_serial(void) 41 | { 42 | When(Method(ArduinoFake(Serial), available)).Return(1); 43 | When(OverloadedMethod(ArduinoFake(Serial), print, size_t(char))).Return(1); 44 | 45 | TEST_ASSERT_EQUAL(1, Serial.available()); 46 | TEST_ASSERT_EQUAL(1, Serial.print('A')); 47 | 48 | Verify(Method(ArduinoFake(Serial), available)).Once(); 49 | Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(char)).Using('A')).Once(); 50 | } 51 | 52 | void test_basics(void) 53 | { 54 | When(Method(ArduinoFake(Serial), end)).AlwaysReturn(); 55 | When(Method(ArduinoFake(Serial), flush)).AlwaysReturn(); 56 | When(Method(ArduinoFake(Serial), available)).Return(0, 1); 57 | When(OverloadedMethod(ArduinoFake(Serial), write, size_t(uint8_t))).Return(1); 58 | When(OverloadedMethod(ArduinoFake(Serial), begin, void(unsigned long))).AlwaysReturn(); 59 | 60 | Serial.begin(9600); 61 | 62 | TEST_ASSERT_EQUAL(0, Serial.available()); 63 | TEST_ASSERT_EQUAL(1, Serial.available()); 64 | TEST_ASSERT_EQUAL(1, Serial.write(5)); 65 | 66 | Serial.flush(); 67 | Serial.end(); 68 | 69 | Verify(OverloadedMethod(ArduinoFake(Serial), begin, void(unsigned long)).Using(9600)).Once(); 70 | Verify(Method(ArduinoFake(Serial), available)).Exactly(2_Times); 71 | 72 | Verify(OverloadedMethod(ArduinoFake(Serial), write, size_t(uint8_t)).Using(5)).Once(); 73 | 74 | Verify(Method(ArduinoFake(Serial), flush)).Once(); 75 | Verify(Method(ArduinoFake(Serial), end)).Once(); 76 | } 77 | 78 | void run_tests() 79 | { 80 | RUN_TEST(SerialTest::test_extends_stream); 81 | RUN_TEST(SerialTest::test_global_serial); 82 | RUN_TEST(SerialTest::test_basics); 83 | } 84 | } 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /test/test_spi.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace SpiTest { 4 | 5 | #include "arduino/SPI.h" 6 | 7 | void test_basics(void) { 8 | SPISettings settings(4000000, MSBFIRST, SPI_MODE0); 9 | uint8_t data = 0x01; 10 | uint8_t buffer[] = {0x02, 0x03, 0x04}; 11 | uint8_t *ptr = buffer; 12 | 13 | When(OverloadedMethod(ArduinoFake(SPI), begin, void(void))).AlwaysReturn(); 14 | When(OverloadedMethod(ArduinoFake(SPI), end, void(void))).AlwaysReturn(); 15 | When(OverloadedMethod(ArduinoFake(SPI), beginTransaction, void(SPISettings)).Using(settings)).AlwaysReturn(); 16 | When(OverloadedMethod(ArduinoFake(SPI), endTransaction, void(void))).AlwaysReturn(); 17 | When(OverloadedMethod(ArduinoFake(SPI), transfer, uint8_t(uint8_t)).Using(data)).AlwaysReturn(); 18 | When(OverloadedMethod(ArduinoFake(SPI), transfer, void(void*, size_t)).Using(ptr, sizeof(buffer))).AlwaysReturn(); 19 | 20 | SPI.begin(); 21 | SPI.beginTransaction(settings); 22 | SPI.transfer(data); 23 | SPI.transfer(buffer, sizeof(buffer)); 24 | SPI.endTransaction(); 25 | SPI.end(); 26 | 27 | Verify(OverloadedMethod(ArduinoFake(SPI), begin, void(void))).Once(); 28 | Verify(OverloadedMethod(ArduinoFake(SPI), end, void(void))).Once(); 29 | Verify(OverloadedMethod(ArduinoFake(SPI), beginTransaction, void(SPISettings))).Once(); 30 | Verify(OverloadedMethod(ArduinoFake(SPI), endTransaction, void(void))).Once(); 31 | Verify(OverloadedMethod(ArduinoFake(SPI), transfer, uint8_t(uint8_t))).Once(); 32 | Verify(OverloadedMethod(ArduinoFake(SPI), transfer, void(void*, size_t))).Once(); 33 | } 34 | 35 | void run_tests() { RUN_TEST(SpiTest::test_basics); } 36 | } // namespace SpiTest 37 | 38 | #endif -------------------------------------------------------------------------------- /test/test_stream.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace StreamTest 4 | { 5 | void test_extends_print(void) 6 | { 7 | TEST_ASSERT_NOT_EQUAL( 8 | ArduinoFakeInstance(Stream), 9 | ArduinoFakeInstance(Print) 10 | ); 11 | 12 | char print_char_var = 'A'; 13 | char stream_char_var = 'B'; 14 | 15 | int print_int_var = 123; 16 | int stream_int_var = 321; 17 | 18 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(char))).AlwaysReturn(); 19 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int))).AlwaysReturn(); 20 | 21 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(char))).AlwaysReturn(); 22 | When(OverloadedMethod(ArduinoFake(Print), print, size_t(int, int))).AlwaysReturn(); 23 | 24 | Stream* stream = ArduinoFakeMock(Stream); 25 | Print* print = ArduinoFakeMock(Print); 26 | 27 | stream->print(stream_char_var); 28 | stream->print(stream_int_var, DEC); 29 | 30 | print->print(print_char_var); 31 | print->print(print_int_var, DEC); 32 | 33 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(char)).Using(stream_char_var)).Once(); 34 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int)).Using(stream_int_var, DEC)).Once(); 35 | 36 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(char)).Using(print_char_var)).Once(); 37 | Verify(OverloadedMethod(ArduinoFake(Print), print, size_t(int, int)).Using(print_int_var, DEC)).Once(); 38 | } 39 | 40 | void test_stream_basics(void) 41 | { 42 | When(Method(ArduinoFake(Stream), available)).Return(0, 1, 0); 43 | When(Method(ArduinoFake(Stream), read)).Return(11, 12, 13); 44 | When(Method(ArduinoFake(Stream), peek)).Return(21, 22, 23); 45 | When(Method(ArduinoFake(Stream), flush)).AlwaysReturn(); 46 | 47 | Stream* stream = ArduinoFakeMock(Stream); 48 | 49 | TEST_ASSERT_EQUAL(0, stream->available()); 50 | TEST_ASSERT_EQUAL(1, stream->available()); 51 | TEST_ASSERT_EQUAL(0, stream->available()); 52 | 53 | TEST_ASSERT_EQUAL(11, stream->read()); 54 | TEST_ASSERT_EQUAL(12, stream->read()); 55 | TEST_ASSERT_EQUAL(13, stream->read()); 56 | 57 | TEST_ASSERT_EQUAL(21, stream->peek()); 58 | TEST_ASSERT_EQUAL(22, stream->peek()); 59 | TEST_ASSERT_EQUAL(23, stream->peek()); 60 | 61 | stream->flush(); 62 | 63 | Verify(Method(ArduinoFake(Stream), available)).Exactly(3_Times); 64 | Verify(Method(ArduinoFake(Stream), read)).Exactly(3_Times); 65 | Verify(Method(ArduinoFake(Stream), peek)).Exactly(3_Times); 66 | Verify(Method(ArduinoFake(Stream), flush)).Once(); 67 | } 68 | 69 | void test_stream_find(void) 70 | { 71 | char char_val1 = 'A'; 72 | char char_val2 = 'B'; 73 | char terminator = '*'; 74 | 75 | char* char_ptr1 = &char_val1; 76 | char* char_ptr2 = &char_val2; 77 | char* terminator_ptr = &terminator; 78 | 79 | When(OverloadedMethod(ArduinoFake(Stream), find, bool(char*))).Return(true, false); 80 | When(OverloadedMethod(ArduinoFake(Stream), find, bool(char*, size_t))).Return(true, false); 81 | 82 | When(OverloadedMethod(ArduinoFake(Stream), findUntil, bool(char*, char*))).Return(true, false); 83 | When(OverloadedMethod(ArduinoFake(Stream), findUntil, bool(char*, size_t, char*, size_t))).Return(true, false); 84 | 85 | Stream* stream = ArduinoFakeMock(Stream); 86 | 87 | TEST_ASSERT_EQUAL(true, stream->find(char_ptr1)); 88 | TEST_ASSERT_EQUAL(false, stream->find(char_ptr2)); 89 | 90 | TEST_ASSERT_EQUAL(true, stream->find(char_ptr1, 10)); 91 | TEST_ASSERT_EQUAL(false, stream->find(char_ptr2, 20)); 92 | 93 | TEST_ASSERT_EQUAL(true, stream->findUntil(char_ptr1, terminator_ptr)); 94 | TEST_ASSERT_EQUAL(false, stream->findUntil(char_ptr2, terminator_ptr)); 95 | 96 | TEST_ASSERT_EQUAL(true, stream->findUntil(char_ptr1, 10, terminator_ptr, 11)); 97 | TEST_ASSERT_EQUAL(false, stream->findUntil(char_ptr2, 20, terminator_ptr, 21)); 98 | 99 | Verify(OverloadedMethod(ArduinoFake(Stream), find, bool(char*)).Using(char_ptr1)).Once(); 100 | Verify(OverloadedMethod(ArduinoFake(Stream), find, bool(char*)).Using(char_ptr2)).Once(); 101 | 102 | Verify(OverloadedMethod(ArduinoFake(Stream), find, bool(char*, size_t)).Using(char_ptr1, 10)).Once(); 103 | Verify(OverloadedMethod(ArduinoFake(Stream), find, bool(char*, size_t)).Using(char_ptr2, 20)).Once(); 104 | 105 | Verify(OverloadedMethod(ArduinoFake(Stream), findUntil, bool(char*, char*))).Exactly(2_Times); 106 | Verify(OverloadedMethod(ArduinoFake(Stream), findUntil, bool(char*, size_t, char*, size_t))).Exactly(2_Times); 107 | } 108 | 109 | void test_stream_parse(void) 110 | { 111 | When(Method(ArduinoFake(Stream), parseInt)).Return(10, 11); 112 | When(Method(ArduinoFake(Stream), parseFloat)).Return(2.0, 2.1); 113 | 114 | Stream* stream = ArduinoFakeMock(Stream); 115 | 116 | TEST_ASSERT_INT_WITHIN(0, 10, stream->parseInt()); 117 | TEST_ASSERT_INT_WITHIN(0, 11, stream->parseInt()); 118 | 119 | TEST_ASSERT_FLOAT_WITHIN(0, 2.0, stream->parseFloat()); 120 | TEST_ASSERT_FLOAT_WITHIN(0, 2.1, stream->parseFloat()); 121 | 122 | Verify(Method(ArduinoFake(Stream), parseInt)).Exactly(2_Times); 123 | Verify(Method(ArduinoFake(Stream), parseFloat)).Exactly(2_Times); 124 | } 125 | 126 | void test_stream_read(void) 127 | { 128 | char char_val1 = 'A'; 129 | char char_val2 = 'B'; 130 | char terminator = '*'; 131 | String str1 = String('X'); 132 | String str2 = String('Z'); 133 | 134 | char* char_ptr1 = &char_val1; 135 | char* char_ptr2 = &char_val2; 136 | 137 | When(Method(ArduinoFake(Stream), readBytes)).Return(1, 2); 138 | When(Method(ArduinoFake(Stream), readBytesUntil)).Return(3, 4); 139 | When(Method(ArduinoFake(Stream), readString)).Return(str1, str2); 140 | When(Method(ArduinoFake(Stream), readStringUntil)).Return(str1, str2); 141 | 142 | Stream* stream = ArduinoFakeMock(Stream); 143 | 144 | TEST_ASSERT_EQUAL_INT(1, stream->readBytes(char_ptr1, 10)); 145 | TEST_ASSERT_EQUAL_INT(2, stream->readBytes(char_ptr2, 20)); 146 | 147 | TEST_ASSERT_EQUAL_INT(3, stream->readBytesUntil(terminator, char_ptr1, 10)); 148 | TEST_ASSERT_EQUAL_INT(4, stream->readBytesUntil(terminator, char_ptr2, 20)); 149 | 150 | TEST_ASSERT_TRUE(str1.equals(stream->readString())); 151 | TEST_ASSERT_TRUE(str2.equals(stream->readString())); 152 | 153 | TEST_ASSERT_TRUE(str1.equals(stream->readStringUntil(terminator))); 154 | TEST_ASSERT_TRUE(str2.equals(stream->readStringUntil(terminator))); 155 | 156 | Verify(Method(ArduinoFake(Stream), readBytes).Using(char_ptr1, 10)).Once(); 157 | Verify(Method(ArduinoFake(Stream), readBytes).Using(char_ptr2, 20)).Once(); 158 | 159 | Verify(Method(ArduinoFake(Stream), readBytesUntil).Using(terminator, char_ptr1, 10)).Once(); 160 | Verify(Method(ArduinoFake(Stream), readBytesUntil).Using(terminator, char_ptr2, 20)).Once(); 161 | 162 | Verify(Method(ArduinoFake(Stream), readString)).Exactly(2_Times); 163 | Verify(Method(ArduinoFake(Stream), readBytesUntil)).Exactly(2_Times); 164 | } 165 | 166 | void run_tests() 167 | { 168 | RUN_TEST(StreamTest::test_extends_print); 169 | RUN_TEST(StreamTest::test_stream_basics); 170 | RUN_TEST(StreamTest::test_stream_find); 171 | RUN_TEST(StreamTest::test_stream_parse); 172 | RUN_TEST(StreamTest::test_stream_read); 173 | } 174 | } 175 | 176 | #endif 177 | -------------------------------------------------------------------------------- /test/test_wire.h: -------------------------------------------------------------------------------- 1 | #ifdef UNIT_TEST 2 | 3 | namespace WireTest { 4 | void test_extends_stream(void) { 5 | TEST_ASSERT_NOT_EQUAL(ArduinoFakeInstance(Stream), ArduinoFakeInstance(Wire)); 6 | 7 | char print_char_var = 'A'; 8 | char stream_char_var = 'B'; 9 | 10 | int print_int_var = 123; 11 | int stream_int_var = 321; 12 | 13 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(char))) 14 | .AlwaysReturn(); 15 | When(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int))) 16 | .AlwaysReturn(); 17 | 18 | When(OverloadedMethod(ArduinoFake(Wire), print, size_t(char))).AlwaysReturn(); 19 | When(OverloadedMethod(ArduinoFake(Wire), print, size_t(int, int))) 20 | .AlwaysReturn(); 21 | 22 | Stream *stream = ArduinoFakeMock(Stream); 23 | TwoWire *wire = ArduinoFakeMock(Wire); 24 | 25 | stream->print(stream_char_var); 26 | stream->print(stream_int_var, DEC); 27 | 28 | wire->print(print_char_var); 29 | wire->print(print_int_var, DEC); 30 | 31 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(char)) 32 | .Using(stream_char_var)) 33 | .Once(); 34 | Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int)) 35 | .Using(stream_int_var, DEC)) 36 | .Once(); 37 | 38 | Verify(OverloadedMethod(ArduinoFake(Wire), print, size_t(char)) 39 | .Using(print_char_var)) 40 | .Once(); 41 | Verify(OverloadedMethod(ArduinoFake(Wire), print, size_t(int, int)) 42 | .Using(print_int_var, DEC)) 43 | .Once(); 44 | } 45 | 46 | void test_global_wire(void) { 47 | When(Method(ArduinoFake(Wire), available)).Return(1); 48 | When(OverloadedMethod(ArduinoFake(Wire), print, size_t(char))).Return(1); 49 | 50 | TEST_ASSERT_EQUAL(1, Wire.available()); 51 | TEST_ASSERT_EQUAL(1, Wire.print('A')); 52 | 53 | Verify(Method(ArduinoFake(Wire), available)).Once(); 54 | Verify(OverloadedMethod(ArduinoFake(Wire), print, size_t(char)).Using('A')) 55 | .Once(); 56 | } 57 | 58 | void test_basics(void) { 59 | 60 | uint8_t device_addr = 0xab; 61 | uint8_t register_addr = 0xcd; 62 | int num_bytes_to_read = 1; 63 | bool send_stop = false; 64 | When(OverloadedMethod(ArduinoFake(Wire), begin, void(void))).AlwaysReturn(); 65 | When(OverloadedMethod(ArduinoFake(Wire), beginTransmission, void(uint8_t))) 66 | .AlwaysReturn(); 67 | When(OverloadedMethod(ArduinoFake(Wire), write, size_t(uint8_t))) 68 | .Return(true); 69 | When(OverloadedMethod(ArduinoFake(Wire), endTransmission, uint8_t(bool))) 70 | .Return(0); 71 | When(OverloadedMethod(ArduinoFake(Wire), requestFrom, uint8_t(uint8_t, uint8_t))) 72 | .Return(0); 73 | When(OverloadedMethod(ArduinoFake(Wire), available, int(void))).Return(1); 74 | When(OverloadedMethod(ArduinoFake(Wire), read, int(void))).Return(1); 75 | 76 | Wire.begin(); 77 | Wire.beginTransmission(device_addr); 78 | Wire.write(register_addr); 79 | Wire.endTransmission(send_stop); 80 | Wire.requestFrom(device_addr, num_bytes_to_read); 81 | if (Wire.available()) { 82 | Wire.read(); 83 | } 84 | 85 | Verify(OverloadedMethod(ArduinoFake(Wire), begin, void(void))).Exactly(1); 86 | Verify(OverloadedMethod(ArduinoFake(Wire), beginTransmission, void(uint8_t)) 87 | .Using(device_addr)) 88 | .Exactly(1); 89 | Verify(OverloadedMethod(ArduinoFake(Wire), write, size_t(uint8_t)) 90 | .Using(register_addr)) 91 | .Exactly(1); 92 | Verify(OverloadedMethod(ArduinoFake(Wire), endTransmission, uint8_t(bool)) 93 | .Using(send_stop)) 94 | .Exactly(1); 95 | Verify(OverloadedMethod(ArduinoFake(Wire), requestFrom, uint8_t(uint8_t, uint8_t)) 96 | .Using(device_addr, num_bytes_to_read)) 97 | .Exactly(1); 98 | Verify(OverloadedMethod(ArduinoFake(Wire), available, int(void))).Exactly(1); 99 | Verify(OverloadedMethod(ArduinoFake(Wire), read, int(void))).Exactly(1); 100 | } 101 | 102 | void run_tests() { 103 | RUN_TEST(WireTest::test_extends_stream); 104 | RUN_TEST(WireTest::test_global_wire); 105 | RUN_TEST(WireTest::test_basics); 106 | } 107 | } // namespace WireTest 108 | 109 | #endif 110 | --------------------------------------------------------------------------------