Skip to content

Commit

Permalink
Merge pull request #12 from bxparks/develop
Browse files Browse the repository at this point in the history
merge v0.3 into master
  • Loading branch information
bxparks authored Aug 20, 2020
2 parents 64d0090 + e21c8ea commit 554cc70
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 23 deletions.
1 change: 1 addition & 0 deletions Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ int digitalRead(uint8_t pin) { return 0; }

void pinMode(uint8_t pin, uint8_t mode) {}

int analogRead(uint8_t pin) { return 0; }
38 changes: 36 additions & 2 deletions Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "StdioSerial.h"

// xx.yy.zz => xxyyzz (without leading 0)
#define UNIX_HOST_DUINO_VERSION 201
#define UNIX_HOST_DUINO_VERSION_STRING "0.2.1"
#define UNIX_HOST_DUINO_VERSION 300
#define UNIX_HOST_DUINO_VERSION_STRING "0.3.0"

// Used by digitalRead() and digitalWrite()
#define HIGH 0x1
Expand Down Expand Up @@ -46,6 +46,39 @@
// Arbitrarily define the pin for the LED_BUILTIN
#define LED_BUILTIN 1

// Define a bunch of digital and analog pins
#define D0 0
#define D1 1
#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#define D8 8
#define D9 9
#define D10 10
#define D11 11
#define D12 12
#define D13 13
#define D14 14
#define D15 15
#define D16 16
#define D17 17
#define D18 18
#define D19 19

#define A0 0
#define A1 1
#define A2 2
#define A3 3
#define A4 4
#define A5 5
#define A6 6
#define A7 7
#define A8 8
#define A9 9

extern "C" {

void delay(unsigned long ms);
Expand All @@ -55,6 +88,7 @@ unsigned long micros();
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
void pinMode(uint8_t pin, uint8_t mode);
int analogRead(uint8_t pin);

/** Provided in the client code's *.ino file. */
void setup();
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

* Unreleased
* 0.3 (2020-08-20)
* Add `#define` for various digital and analog pins: `D0`-`D19`, `A0`-`A9`.
* Add stub for `analogRead()`.
* Add `Print.printf()` provided by a significant number of Arduino boards
(ESP8266, ESP32, Teensy, etc).
* 0.2.1 (2020-05-02)
* Add `MORE_CLEAN`, an optional user-provided make-target to do additional
cleaning, such as removing directories created by code-generators.
Expand Down
48 changes: 34 additions & 14 deletions Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@
Print.cpp - Base class that provides print() and println()
Copyright (c) 2008 David A. Mellis. All right reserved.
Modified by Brian T. Park 2019.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 23 November 2006 by David A. Mellis
Modified 03 August 2015 by Chuck Todd
*/
Modified 2018 by Brian T. Park
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include "pgmspace.h"
#include "Print.h"

// Size of the internal printf() buffer
#define PRINTF_BUFFER_SIZE 250

// Public Methods //////////////////////////////////////////////////////////////

/* default implementation: may be overridden */
Expand Down Expand Up @@ -198,6 +203,21 @@ size_t Print::println(const Printable& x)
return n;
}

size_t Print::printf(const char* fmt, ...) {
char buf[PRINTF_BUFFER_SIZE];
va_list args;
va_start(args, fmt);
int status = vsnprintf(buf, PRINTF_BUFFER_SIZE, fmt, args);
va_end(args);
if (status >= 0) {
buf[PRINTF_BUFFER_SIZE - 1] = '\0';
size_t n = print(buf);
return n;
} else {
return 0;
}
}

// Private Methods /////////////////////////////////////////////////////////////

size_t Print::printNumber(unsigned long n, uint8_t base)
Expand All @@ -220,15 +240,15 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
return write(str);
}

size_t Print::printFloat(double number, uint8_t digits)
{
size_t Print::printFloat(double number, uint8_t digits)
{
size_t n = 0;

if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically

// Handle negative numbers
if (number < 0.0)
{
Expand All @@ -240,7 +260,7 @@ size_t Print::printFloat(double number, uint8_t digits)
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;

number += rounding;

// Extract the integer part of the number and print it
Expand All @@ -250,7 +270,7 @@ size_t Print::printFloat(double number, uint8_t digits)

// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
n += print('.');
n += print('.');
}

// Extract digits from the remainder one at a time
Expand All @@ -259,8 +279,8 @@ size_t Print::printFloat(double number, uint8_t digits)
remainder *= 10.0;
unsigned int toPrint = (unsigned int)(remainder);
n += print(toPrint);
remainder -= toPrint;
}
remainder -= toPrint;
}

return n;
}
8 changes: 6 additions & 2 deletions Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class Print
void setWriteError(int err = 1) { write_error = err; }
public:
Print() : write_error(0) {}

int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }

virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
Expand Down Expand Up @@ -89,6 +89,10 @@ class Print
size_t println(const Printable&);
size_t println(void);

// printf() extension supported by many microcontrollers including
// Teensy, ESP8266 and ESP32 (but not AVR).
size_t printf(const char* format, ...);

virtual void flush() { /* Empty implementation for backward compatibility */ }
};

Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The disadvantages are:
* There may be compiler differences between the desktop and the embedded
environments (e.g. 16-bit `int` versus 64-bit `int`).

Version: 0.2.1 (2020-05-02)
Version: 0.3 (2020-08-20)

## Installation

Expand Down Expand Up @@ -200,16 +200,19 @@ The following functions and features of the Arduino framework are implemented:
* `delay()`, `yield()`
* `millis()`, `micros()`
* `digitalWrite()`, `digitalRead()`, `pinMode()` (empty stubs)
* `analogRead()` (empty stub)
* `HIGH`, `LOW`, `INPUT`, `OUTPUT`, `INPUT_PULLUP`
* `StdioSerial.h`
* `Serial.print()`, `Serial.println()`, `Serial.write()`
* `Serial.read()`, `Serial.peek()`, `Serial.available()`
* `SERIAL_PORT_MONITOR`
* `WString.h`
* `class String`
* `class __FlashStringHelper`, `F()`
* `class __FlashStringHelper`, `F()`, `FPSTR()`
* `Print.h`
* `class Print`, `class Printable`
* `Print.printf()` - extended function supported by some Arduino compatible
microcontrollers
* `pgmspace.h`
* `pgm_read_byte()`, `pgm_read_word()`, `pgm_read_dword()`,
`pgm_read_float()`, `pgm_read_ptr()`
Expand All @@ -224,6 +227,12 @@ The following functions and features of the Arduino framework are implemented:
See [Arduino.h](https://github.com/bxparks/UnixHostDuino/blob/develop/Arduino.h)
for the latest list.

The `Print.printf()` function is an extension to the `Print` class that is
provided by many Arduino-compatible microcontrollers (but not the AVR
controllers). It is implemented here for convenience. The size of the internal
buffer is `250` characters, which can be changed by changing the
`PRINTF_BUFFER_SIZE` parameter if needed.

### Serial Port Emulation

The `Serial` object is an instance of the `StdioSerial` class which emulates the
Expand Down Expand Up @@ -270,6 +279,9 @@ This library has been tested on:
* clang++ 8.0.0-3~ubuntu18.04.2
* clang++ 6.0.0-1ubuntu2
* GNU Make 4.1
* Ubuntu 20.04
* g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
* GNU Make 4.2.1
* MacOS 10.14.5
* clang++ Apple LLVM version 10.0.1
* GNU Make 3.81
Expand Down
4 changes: 2 additions & 2 deletions UnixHostDuino.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
UNAME := $(shell uname)

# UnixHostDuino module directory.
UNIX_HOST_DUINO_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
UNIX_HOST_DUINO_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
# Assume that there are other libs are siblings to UnixHostDuino
UNIX_HOST_DUINO_LIB_DIR := $(realpath $(UNIX_HOST_DUINO_DIR)/..)
UNIX_HOST_DUINO_LIB_DIR := $(abspath $(UNIX_HOST_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/
Expand Down
2 changes: 1 addition & 1 deletion WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
Copyright 2011, Paul Stoffregen, [email protected]
Modified by Brian T. Park 2019.
Modified 2019, Brian T. Park
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down

0 comments on commit 554cc70

Please sign in to comment.