Skip to content

Commit

Permalink
Rename UNIX_HOST_* and UnixHost* macros and variables to EPOXY_* and …
Browse files Browse the repository at this point in the history
…Epoxy* (see #15)
  • Loading branch information
bxparks committed Jan 22, 2021
1 parent a1323fc commit 8aa4839
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 182 deletions.
8 changes: 4 additions & 4 deletions Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* Copyright (c) 2005-2013 Arduino Team
*/

#ifndef UNIX_HOST_DUINO_ARDUINO_H
#define UNIX_HOST_DUINO_ARDUINO_H
#ifndef EPOXY_DUINO_ARDUINO_H
#define EPOXY_DUINO_ARDUINO_H

#include "Print.h"
#include "StdioSerial.h"

// xx.yy.zz => xxyyzz (without leading 0)
#define UNIX_HOST_DUINO_VERSION 400
#define UNIX_HOST_DUINO_VERSION_STRING "0.4"
#define EPOXY_DUINO_VERSION 400
#define EPOXY_DUINO_VERSION_STRING "0.4"

// Used by digitalRead() and digitalWrite()
#define HIGH 0x1
Expand Down
4 changes: 2 additions & 2 deletions EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef UNIX_HOST_DUINO_EEPROM_H
#define UNIX_HOST_DUINO_EEPROM_H
#ifndef EPOXY_DUINO_EEPROM_H
#define EPOXY_DUINO_EEPROM_H

#include <inttypes.h>
/*
Expand Down
127 changes: 127 additions & 0 deletions EpoxyDuino.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Include this Makefile to compile an Arduino *.ino file on Linux or MacOS.
#
# Create a 'Makefile' in the sketch folder. For example, for the
# Blink/Blink.ino program, the makefile will be 'Blink/Makefile'.
# The content will look like this:
#
# APP_NAME := {name of *.ino file}
# ARDUINO_LIBS := AUnit AceTime {... additional Arduino libraries}
# include ../../../EpoxyDuino/EpoxyDuino.mk
#
# The 2 required parameters are:
#
# * APP_NAME: base name of the Arduino sketch file,
# e.g. 'Blink' not 'Blink.ino'
# * ARDUINO_LIBS: list of dependent Arduino libraries in sibling directories
# to EpoxyDuino (e.g. AUnit). The EpoxyDuino directory is
# automatically included.
#
# Optional parameters are:
#
# * ARDUINO_LIB_DIRS: List of additional locations of Arduino libs, for
# example, $(ARDUINO_IDE_DIR)/libraries,
# $(ARDUINO_IDE_DIR)/hardware/arduino/avr/libraries,
# $(ARDUINO_IDE_DIR)/portable/packages/arduino/hardware/avr/1.8.2/libraries.
# (The $(ARDUINO_IDE_DIR) is an example temporary variable containing the
# install location of the Arduino IDE. It is not used by EpoxyDuino.mk.)
# * OBJS: Additional object (*.o) files needed by the binary
# * GENERATED: A list of files which are generated by a script, and therefore
# can be deleted by 'make clean'
# * MORE_CLEAN: Optional user-supplied make-target that performs
# additional cleanup (i.e. removing generated directories).
#
# Type 'make -n' to verify.
#
# Type 'make' to create the $(APP_NAME).out program.
#
# Type 'make clean' to remove intermediate files.

# Detect Linux or MacOS
UNAME := $(shell uname)

# EpoxyDuino module directory.
EPOXY_DUINO_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
# Assume that there are other libs are siblings to EpoxyDuino
EPOXY_DUINO_LIB_DIR := $(abspath $(EPOXY_DUINO_DIR)/..)

# List of Arduino IDE library folders, both built-in to the Arduino IDE
# and those downloaded later, e.g. in the portable/ directory or .arduino15/
# directory.
ARDUINO_LIB_DIRS ?=

# Default modules which are automatically linked in: EpoxyDuino/
DEFAULT_MODULES := $(EPOXY_DUINO_DIR)

# Look for the ARDUINO_LIBS modules under each of the ARDUINO_LIB_DIRS and
# EPOXY_DUINO_LIB_DIR.
APP_MODULES := $(foreach lib,$(ARDUINO_LIBS),${EPOXY_DUINO_LIB_DIR}/${lib})
APP_MODULES += \
$(foreach lib_dir,$(ARDUINO_LIB_DIRS),\
$(foreach lib,$(ARDUINO_LIBS),\
${lib_dir}/${lib}\
)\
)

# All dependent modules.
ALL_MODULES := $(DEFAULT_MODULES) $(APP_MODULES)

# Compiler and settings
ifeq ($(UNAME), Linux)
CXX ?= g++
CXXFLAGS ?= -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -flto
else ifeq ($(UNAME), Darwin)
CXX ?= clang++
CXXFLAGS ?= -std=c++11 -stdlib=libc++ # -Weverything
endif

# pre-processor (-I, -D, etc)
CPPFLAGS_EXPANSION = -I$(module) -I$(module)/src
CPPFLAGS ?=
CPPFLAGS += $(foreach module,$(ALL_MODULES),$(CPPFLAGS_EXPANSION))

# Define a macro to indicate that EpoxyDuino is being used. Defined here
# instead of Arduino.h so that files like 'compat.h' can determine the
# compile-time environment without having to include <Arduino.h>.
# Also define UNIX_HOST_DUINO for backwards compatibility.
CPPFLAGS += -DUNIX_HOST_DUINO -DEPOXY_DUINO

# linker settings (e.g. -lm)
LDFLAGS ?=

# C++ srcs. Old Arduino libraries place the source files at the top level.
# Later Arduino libraries put the source files under the src/ directory.
# Support subdirectory expansions up to 3 levels below 'src/'.
# (There might be a better way to do this using GNU Make but I can't find a
# mechanism that doesn't barf when the 'src/' directory doesn't exist.)
SRCS_EXPANSION = $(wildcard $(module)/*.cpp) \
$(wildcard $(module)/src/*.cpp) \
$(wildcard $(module)/src/*/*.cpp) \
$(wildcard $(module)/src/*/*/*.cpp) \
$(wildcard $(module)/src/*/*/*/*.cpp)
SRCS := $(foreach module,$(ALL_MODULES),$(SRCS_EXPANSION))
SRCS := ${SRCS} $(wildcard *.cpp) $(wildcard */*.cpp)

# Objects including *.o from *.ino
OBJS += $(SRCS:%.cpp=%.o) $(APP_NAME).o

$(APP_NAME).out: $(OBJS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

$(APP_NAME).o: $(APP_NAME).ino
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -x c++ -c $<

%.o: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

# This simple rule does not capture all header dependencies of a given cpp
# file. Maybe it's better to make each cpp to depend on all headers of a given
# module, and force a recompilation of all cpp files. As far as I understand,
# this is what the Arduino IDE does upon each compile iteration.
%.cpp: %.h

.PHONY: all clean $(MORE_CLEAN)

all: $(APP_NAME).out

clean: $(MORE_CLEAN)
rm -f $(OBJS) $(APP_NAME).out $(GENERATED)
4 changes: 2 additions & 2 deletions Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef UNIX_HOST_DUINO_PRINT_H
#define UNIX_HOST_DUINO_PRINT_H
#ifndef EPOXY_DUINO_PRINT_H
#define EPOXY_DUINO_PRINT_H

#include <inttypes.h>
#include <string.h> // strlen()
Expand Down
4 changes: 2 additions & 2 deletions Printable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef UNIX_HOST_DUINO_PRINTABLE_H
#define UNIX_HOST_DUINO_PRINTABLE_H
#ifndef EPOXY_DUINO_PRINTABLE_H
#define EPOXY_DUINO_PRINTABLE_H

#include <stdlib.h>

Expand Down
58 changes: 30 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# UnixHostDuino
# EpoxyDuino

This project contains a small (but often effective) implementation of the
Arduino programming framework for Linux and MacOS. Originally, it was created to
Arduino programming framework for Linux, MacOS and potentially other POSIX-like
systems. Originally, it was created to
allow [AUnit](https://github.com/bxparks/AUnit) unit tests to be compiled and
run on a Linux or MacOS machine, instead of running on the embedded
microcontroller. As more Arduino functionality was added, it became useful for
Expand All @@ -15,9 +16,9 @@ sketch is `SampleTest/SampleTest.ino`, then the makefile should be
produces an executable with a `.out` extension, for example, `SampleTest.out`.

To be clear, most Arduino programs have hardware dependencies which will *not*
be supported by UnixHostDuino. However, if your program has limited hardware
be supported by EpoxyDuino. However, if your program has limited hardware
dependencies so that it is conceptually portable to a vanilla Unix environment,
UnixHostDuino may work.
EpoxyDuino may work.

Running an Arduino program natively on Linux or MacOS has some advantages:

Expand All @@ -44,33 +45,33 @@ You need to grab the sources directly from GitHub. This project is *not* an
Arduino library so it is not available through the [Arduino Library
Manager](https://www.arduino.cc/en/guide/libraries) in the Arduino IDE.

The location of the UnixHostDuino directory can be arbitrary, but a convenient
The location of the EpoxyDuino directory can be arbitrary, but a convenient
location might be the same `./libraries/` directory used by the Arduino IDE to
store other Arduino libraries:

```
$ cd {sketchbook_directory}/libraries
$ git clone https://github.com/bxparks/UnixHostDuino.git
$ git clone https://github.com/bxparks/EpoxyDuino.git
```

This will create a directory called
`{sketchbook_directory}/libraries/UnixHostDuino`.
`{sketchbook_directory}/libraries/EpoxyDuino`.

## Usage

The minimal `Makefile` has 3 lines:
```
APP_NAME := {name of project}
ARDUINO_LIBS := {list of dependent Arduino libraries}
include {path/to/UnixHostDuino.mk}
include {path/to/EpoxyDuino.mk}
```

For example, the [examples/BlinkSOS](examples/BlinkSOS) project contains this
Makefile:
```
APP_NAME := BlinkSOS
ARDUINO_LIBS :=
include ../../../UnixHostDuino/UnixHostDuino.mk
include ../../../EpoxyDuino/EpoxyDuino.mk
```

To build the program, just run `make`:
Expand Down Expand Up @@ -99,30 +100,30 @@ $ CXX=clang++ make
```
(This sets the `CXX` shell environment variable temporarily, for the duration of
the `make` command, which causes `make` to set its internal `CXX` variable,
which causes `UnixHostDuino.mk` to use `clang++` over the default `g++`.)
which causes `EpoxyDuino.mk` to use `clang++` over the default `g++`.)

### Additional Arduino Libraries

If the Arduino program depends on additional Arduino libraries, they must be
specified in the `Makefile` using the `ARDUINO_LIBS` parameter. For example,
this includes the [AUnit](https://github.com/bxparks/AUnit) library if it is at
the same level as UnixHostDuino:
the same level as EpoxyDuino:

```
APP_NAME := SampleTest
ARDUINO_LIBS := AUnit AceButton AceTime
include ../../UnixHostDuino/UnixHostDuino.mk
include ../../EpoxyDuino/EpoxyDuino.mk
```

The libraries are referred to by their base directory name (e.g. `AceButton`,
or `AceTime`) not the full path. The `UnixHostDuino.mk` file will look for
these additional libraries at the same level as the `UnixHostDuino` directory
or `AceTime`) not the full path. The `EpoxyDuino.mk` file will look for
these additional libraries at the same level as the `EpoxyDuino` directory
itself.

### Additional Arduino Library Locations

By default, UnixHostDuino assumes that the additional libraries are siblings to
the`UnixHostDuino/` library. Unfortunately, Arduino libraries tend to be
By default, EpoxyDuino assumes that the additional libraries are siblings to
the`EpoxyDuino/` library. Unfortunately, Arduino libraries tend to be
scattered among many locations. These additional locations can be specified
using the `ARDUINO_LIB_DIRS` variable. For example,

Expand All @@ -134,13 +135,13 @@ ARDUINO_LIB_DIRS := \
$(ARDUINO_IDE_DIR)/portable/packages/arduino/hardware/avr/1.8.2/libraries \
$(ARDUINO_IDE_DIR)/libraries \
$(ARDUINO_IDE_DIR)/hardware/arduino/avr/libraries
include ../../UnixHostDuino/UnixHostDuino.mk
include ../../EpoxyDuino/EpoxyDuino.mk
```

Each of the `AUnit`, `AceButton` and `AceTime` libraries will be searched in
each of the 3 directories given in the `ARDUINO_LIB_DIRS`. (The
`ARDUINO_IDE_DIR` is a convenience temporary variable. It has no significance to
`UnixHostDuino.mk`)
`EpoxyDuino.mk`)

### Difference from Arduino IDE

Expand All @@ -151,7 +152,7 @@ provided by the Arduino IDE:
an `#include <Arduino.h>` include line at the top of the file. This is
compatible with the Arduino IDE which automatically includes `<Arduino.h>`.
* The Arduion IDE supports multiple `ino` files in the same directory. (I
believe it simply concontenates them all into a single file.) UnixHostDuino
believe it simply concontenates them all into a single file.) EpoxyDuino
supports only one `ino` file in a given directory.
* The Arduino IDE automatically generates forward declarations for functions
that appear *after* the global `setup()` and `loop()` methods. In a normal
Expand All @@ -160,25 +161,25 @@ provided by the Arduino IDE:
`ino` file.

Fortunately, the changes required to make an `ino` file compatible with
UnixHostDuino are backwards compatible with the Arduino IDE. In other words, a
program that compiles with UnixHostDuino will also compile under Ardunio IDE.
EpoxyDuino are backwards compatible with the Arduino IDE. In other words, a
program that compiles with EpoxyDuino will also compile under Ardunio IDE.

There are other substantial differences. The Arduino IDE supports multiple
microcontroller board types, each using its own set of compiler tools and
library locations. There is a complicated set of files and rules that determine
how to find and use those tools and libraries. The UnixHostDuino tool does *not*
how to find and use those tools and libraries. The EpoxyDuino tool does *not*
use any of the configuration files used by the Arduino IDE. Sometimes, you can
use the `ARDUINO_LIB_DIRS` to get around this limitations. However, when you
start using `ARDUINO_LIB_DIRS`, you will often run into third party libraries
using features which are not supported by the UnixHostDuino framework emulation
using features which are not supported by the EpoxyDuino framework emulation
layer.

### Conditional Code

If you want to add code that takes effect only on UnixHostDuino, you can use
If you want to add code that takes effect only on EpoxyDuino, you can use
the following macro:
```C++
#if defined(UNIX_HOST_DUINO)
#if defined(EPOXY_DUINO)
...
#endif
```
Expand Down Expand Up @@ -230,7 +231,7 @@ The following functions and features of the Arduino framework are implemented:
* `Wire.h`
* compile only

See [Arduino.h](https://github.com/bxparks/UnixHostDuino/blob/develop/Arduino.h)
See [Arduino.h](https://github.com/bxparks/EpoxyDuino/blob/develop/Arduino.h)
for the latest list.

The `Print.printf()` function is an extension to the `Print` class that is
Expand All @@ -249,7 +250,7 @@ The interaction with the Unix `tty` device is complicated, and I am not entirely
sure that I have implemented things properly. See [Entering raw
mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html) for
in-depth details. The following is a quick summary of how this is implemented
under `UnixHostDuino`.
under `EpoxyDuino`.

The `STDOUT` remains mostly in normal mode. In particular, `ONLCR` mode is
enabled, which translates `\n` (NL) to `\r\n` (CR-NL). This allows the program
Expand All @@ -271,7 +272,7 @@ behavior.
The `ISIG` option on the `tty` device is *enabled*. This allows the usual Unix
signals to be active, such as Ctrl-C to quit the program, or Ctrl-Z to suspend
the program. But this convenience means that the Arduino program running under
`UnixHostDuino` will never receive a control character through the
`EpoxyDuino` will never receive a control character through the
`Serial.read()` function. The advantages of having normal Unix signals seemed
worth the trade-off.

Expand All @@ -287,6 +288,7 @@ This library has been tested on:
* GNU Make 4.1
* Ubuntu 20.04
* g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
* clang++ version 10.0.0-4ubuntu1
* GNU Make 4.2.1
* MacOS 10.14.5
* clang++ Apple LLVM version 10.0.1
Expand Down
4 changes: 2 additions & 2 deletions StdioSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* MIT License
*/

#ifndef UNIX_HOST_DUINO_STDIO_SERIAL_H
#define UNIX_HOST_DUINO_STDIO_SERIAL_H
#ifndef EPOXY_DUINO_STDIO_SERIAL_H
#define EPOXY_DUINO_STDIO_SERIAL_H

#include "Print.h"
#include "Stream.h"
Expand Down
4 changes: 2 additions & 2 deletions Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
parsing functions based on TextFinder library by Michael Margolis
*/

#ifndef UNIX_HOST_DUINO_STREAM_H
#define UNIX_HOST_DUINO_STREAM_H
#ifndef EPOXY_DUINO_STREAM_H
#define EPOXY_DUINO_STREAM_H

#include <inttypes.h>
#include "Print.h"
Expand Down
Loading

0 comments on commit 8aa4839

Please sign in to comment.