Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new EPOXY_CORE_PATH override to select ESP8266 core provided by this project #3

Merged
merged 4 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Root directory of EpoxyFS
epoxyfsdata

# Root file of EpoxyEepromAvr and EpoxyEepromEsp
epoxyeepromdata
22 changes: 0 additions & 22 deletions EspMock.mk

This file was deleted.

44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,48 @@ for any other project that needs mocks for ESP8266 and ESP32 libraries.

## Installation

This project *cannot* be installed through the Arduino Library Manager because
it is compatible only with EpoxyDuino. You need to run the `git clone` command
manually. It is probably best to install at the same location as a normal
Arduino library. In other words, locate the `./libraries/` directory of your
sketchbook directory used by the Arduino IDE:
You must install both of the following projects:

* https://github.com/bxparks/EpoxyDuino
* https://github.com/hsaturn/EspMock

These projects *cannot* be installed through the Arduino Library Manager because
they are not normal Arduino libraries. It is probably most convenient to install
them in the same location as other Arduino library directory. In other words,
locate the `./libraries/` directory of your sketchbook directory used by the
Arduino IDE, and run the following commands:

```bash
$ cd {sketchbook_location}/libraries
$ cd {SketchBookDirectory}/libraries
$ git clone https://github.com/bxparks/EpoxyDuino
$ git clone https://github.com/hsaturn/EspMock
```

Then add `EspMock` to the `ARDUINO_LIBS` variable of the EpoxyDuino `Makefile`
located in directory of the Arduino sketch that you want to compile, like this:
## Usage

For each Arduino program, we need to create a `Makefile`
as described in [EpoxyDuino](https://github.com/bxparks/EpoxyDuino), but with a
few extra parameters:

* Add the `EPOXY_CORE_PATH` variable (new for EpoxyDuino v0.7), pointing to the
`{EspMock}/cores/esp8266/` directory.
* Add `EspMock` to the `ARDUINO_LIBS` variable.
* Add a `ARDUINO_LIB_DIRS` variable that points to
`{EspMockDirectory}/libraries` directory to pickup additional ESP8266 or ESP32
mock libraries provided by EspMock.

The result is a `Makefile` that looks like this:

```
APP_NAME := network-tests
APP_NAME := {NameOfSketch}
ARDUINO_LIBS := {Lib1} {Lib2} ... EspMock
include ../../../EpoxyDuino/EpoxyDuino.mk
ARDUINO_LIB_DIRS := {EspMockDirectory}/libraries
EPOXY_CORE_PATH := {EspMockDirectory}/cores/esp8266
include {EpoxyDuinoDirectory}/EpoxyDuino.mk
```

See [EpoxyDuino](https://github.com/bxparks/EpoxyDuino) for information on how
to configure the `Makefile`.
See [tests/network-tests/Makefile](tests/network-tests/Makefile) for a concrete
example.

## License

Expand Down
76 changes: 76 additions & 0 deletions cores/esp8266/Arduino.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2019 Brian T. Park
*
* Parts derived from the Arduino SDK
* Copyright (c) 2005-2013 Arduino Team
*
* Parts inspired by [Entering raw
* mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html).
*
* Parts inspired by [ESP8266 Host
* Emulation](https://github.com/esp8266/Arduino/tree/master/tests/host).
*
*/

#include <inttypes.h>
#include <unistd.h> // usleep()
#include <time.h> // clock_gettime()
#include "Arduino.h"

// -----------------------------------------------------------------------
// Arduino methods emulated in Unix
// -----------------------------------------------------------------------

void yield() {
usleep(1000); // prevents program from consuming 100% CPU
}

void pinMode(uint8_t /*pin*/, uint8_t /*mode*/) {}

void digitalWrite(uint8_t /*pin*/, uint8_t /*val*/) {}

int digitalRead(uint8_t /*pin*/) { return 0; }

int analogRead(uint8_t /*pin*/) { return 0; }

void analogWrite(uint8_t /*pin*/, int /*val*/) {}

unsigned long millis() {
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
unsigned long ms = spec.tv_sec * 1000U + spec.tv_nsec / 1000000UL;
return ms;
}

unsigned long micros() {
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
unsigned long us = spec.tv_sec * 1000000UL + spec.tv_nsec / 1000U;
return us;
}

void delay(unsigned long ms) {
usleep(ms * 1000);
}

void delayMicroseconds(unsigned int us) {
usleep(us);
}

unsigned long pulseIn(
uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) {
return 0;
}

unsigned long pulseInLong(
uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) {
return 0;
}

void shiftOut(uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/,
uint8_t /*val*/) {}

uint8_t shiftIn(
uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/) {
return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
116 changes: 116 additions & 0 deletions cores/esp8266/IPAddress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
IPAddress.cpp - Base class that provides IPAddress
Copyright (c) 2011 Adrian McEwen. All right reserved.

Copied from Arduino AVR core 1.8.3 by Erik Tideman. Removed
unnecessary 'friend' declarations.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <IPAddress.h>

IPAddress::IPAddress()
{
_address.dword = 0;
}

IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
{
_address.bytes[0] = first_octet;
_address.bytes[1] = second_octet;
_address.bytes[2] = third_octet;
_address.bytes[3] = fourth_octet;
}

IPAddress::IPAddress(uint32_t address)
{
_address.dword = address;
}

IPAddress::IPAddress(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
}

bool IPAddress::fromString(const char *address)
{
uint16_t acc = 0; // Accumulator
uint8_t dots = 0;

while (*address)
{
char c = *address++;
if (c >= '0' && c <= '9')
{
acc = acc * 10 + (c - '0');
if (acc > 255) {
// Value out of [0..255] range
return false;
}
}
else if (c == '.')
{
if (dots == 3) {
// Too much dots (there must be 3 dots)
return false;
}
_address.bytes[dots++] = acc;
acc = 0;
}
else
{
// Invalid char
return false;
}
}

if (dots != 3) {
// Too few dots (there must be 3 dots)
return false;
}
_address.bytes[3] = acc;
return true;
}

IPAddress& IPAddress::operator=(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
return *this;
}

IPAddress& IPAddress::operator=(uint32_t address)
{
_address.dword = address;
return *this;
}

bool IPAddress::operator==(const uint8_t* addr) const
{
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
}

size_t IPAddress::printTo(Print& p) const
{
size_t n = 0;
for (int i =0; i < 3; i++)
{
n += p.print(_address.bytes[i], DEC);
n += p.print('.');
}
n += p.print(_address.bytes[3], DEC);
return n;
}
74 changes: 74 additions & 0 deletions cores/esp8266/IPAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
IPAddress.h - Base class that provides IPAddress
Copyright (c) 2011 Adrian McEwen. All right reserved.

Copied from Arduino AVR core 1.8.3 by Erik Tideman. Removed
unnecessary 'friend' declarations.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef IPAddress_h
#define IPAddress_h

#include <stdint.h>
#include "Printable.h"
#include "WString.h"

// A class to make it easier to handle and pass around IP addresses

class IPAddress : public Printable {
private:
union {
uint8_t bytes[4]; // IPv4 address
uint32_t dword;
} _address;

// Access the raw byte array containing the address. Because this returns a pointer
// to the internal structure rather than a copy of the address this function should only
// be used when you know that the usage of the returned uint8_t* will be transient and not
// stored.
uint8_t* raw_address() { return _address.bytes; };

public:
// Constructors
IPAddress();
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
IPAddress(uint32_t address);
IPAddress(const uint8_t *address);

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }

// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; };
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
bool operator==(const uint8_t* addr) const;

// Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const { return _address.bytes[index]; };
uint8_t& operator[](int index) { return _address.bytes[index]; };

// Overloaded copy operators to allow initialisation of IPAddress objects from other types
IPAddress& operator=(const uint8_t *address);
IPAddress& operator=(uint32_t address);

virtual size_t printTo(Print& p) const;
};

const IPAddress INADDR_NONE(0,0,0,0);

#endif
Loading