Skip to content

Commit

Permalink
Adds WMath, WCharacter and more Arduino classes
Browse files Browse the repository at this point in the history
  • Loading branch information
epsilonrt committed Nov 29, 2018
1 parent 8d2a949 commit 03b1ccb
Show file tree
Hide file tree
Showing 9 changed files with 1,036 additions and 5 deletions.
24 changes: 19 additions & 5 deletions include/piduino/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#ifndef __DOXYGEN__

/* types ==================================================================== */
typedef unsigned int word;
typedef uint8_t boolean;
typedef uint8_t byte;

#ifdef __cplusplus
extern "C"{
#endif
Expand Down Expand Up @@ -81,6 +86,8 @@ extern "C"{
#include <iostream>
#include <algorithm>
#include <piduino/gpio.h>
#include "WCharacter.h"
#include "WString.h"

#define EXTERN_C extern "C"

Expand All @@ -105,6 +112,17 @@ enum ArduinoBool {

typedef Piduino::Pin::Isr Isr;

uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);

#define word(...) makeWord(__VA_ARGS__)

// WMath prototypes
long random(long);
long random(long, long);
void randomSeed(unsigned long);
long map(long, long, long, long, long);

#else /* __cplusplus not defined */
// -----------------------------------------------------------------------------
#include <stdbool.h>
Expand Down Expand Up @@ -140,12 +158,8 @@ typedef void (* Isr) (void);
// -----------------------------------------------------------------------------
#endif /* __cplusplus not defined */

#define digitalPinToInterrupt(p) (p)

/* types ==================================================================== */
typedef unsigned int word;
typedef uint8_t boolean;
typedef uint8_t byte;
#define digitalPinToInterrupt(p) (p)

#else /* __DOXYGEN__ defined */

Expand Down
47 changes: 47 additions & 0 deletions include/piduino/arduino/Client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Client.h - Base class that provides Client
Copyright (c) 2011 Adrian McEwen. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef client_h
#define client_h
#include "Print.h"
#include "Stream.h"
#include "IPAddress.h"

class Client : public Stream {

public:
virtual int connect (IPAddress ip, uint16_t port) = 0;
virtual int connect (const char *host, uint16_t port) = 0;
virtual size_t write (uint8_t) = 0;
virtual size_t write (const uint8_t *buf, size_t size) = 0;
virtual int available() = 0;
virtual int read() = 0;
virtual int read (uint8_t *buf, size_t size) = 0;
virtual int peek() = 0;
virtual void flush() = 0;
virtual void stop() = 0;
virtual uint8_t connected() = 0;
virtual operator bool() = 0;
protected:
uint8_t* rawIPAddress (IPAddress& addr) {
return addr.raw_address();
};
};

#endif
90 changes: 90 additions & 0 deletions include/piduino/arduino/IPAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
IPAddress.h - Base class that provides IPAddress
Copyright (c) 2011 Adrian McEwen. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef IPAddress_h
#define IPAddress_h

#include <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;

friend class EthernetClass;
friend class UDP;
friend class Client;
friend class Server;
friend class DhcpClass;
friend class DNSClient;
};

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

#endif
30 changes: 30 additions & 0 deletions include/piduino/arduino/Server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Server.h - Base class that provides Server
Copyright (c) 2011 Adrian McEwen. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef server_h
#define server_h

#include "Print.h"

class Server : public Print {
public:
virtual void begin() = 0;
};

#endif
152 changes: 152 additions & 0 deletions include/piduino/arduino/WCharacter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
WCharacter.h - Character utility functions for Wiring & Arduino
Copyright (c) 2010 Hernando Barragan. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef Character_h
#define Character_h

#include <ctype.h>

// WCharacter.h prototypes
inline boolean isAlphaNumeric (int c) __attribute__ ( (always_inline));
inline boolean isAlpha (int c) __attribute__ ( (always_inline));
inline boolean isAscii (int c) __attribute__ ( (always_inline));
inline boolean isWhitespace (int c) __attribute__ ( (always_inline));
inline boolean isControl (int c) __attribute__ ( (always_inline));
inline boolean isDigit (int c) __attribute__ ( (always_inline));
inline boolean isGraph (int c) __attribute__ ( (always_inline));
inline boolean isLowerCase (int c) __attribute__ ( (always_inline));
inline boolean isPrintable (int c) __attribute__ ( (always_inline));
inline boolean isPunct (int c) __attribute__ ( (always_inline));
inline boolean isSpace (int c) __attribute__ ( (always_inline));
inline boolean isUpperCase (int c) __attribute__ ( (always_inline));
inline boolean isHexadecimalDigit (int c) __attribute__ ( (always_inline));
inline int toAscii (int c) __attribute__ ( (always_inline));
inline int toLowerCase (int c) __attribute__ ( (always_inline));
inline int toUpperCase (int c) __attribute__ ( (always_inline));


// Checks for an alphanumeric character.
// It is equivalent to (isalpha(c) || isdigit(c)).
inline boolean isAlphaNumeric (int c) {
return (isalnum (c) == 0 ? false : true);
}


// Checks for an alphabetic character.
// It is equivalent to (isupper(c) || islower(c)).
inline boolean isAlpha (int c) {
return (isalpha (c) == 0 ? false : true);
}


// Checks whether c is a 7-bit unsigned char value
// that fits into the ASCII character set.
inline boolean isAscii (int c) {
return (isascii (c) == 0 ? false : true);
}


// Checks for a blank character, that is, a space or a tab.
inline boolean isWhitespace (int c) {
return (isblank (c) == 0 ? false : true);
}


// Checks for a control character.
inline boolean isControl (int c) {
return (iscntrl (c) == 0 ? false : true);
}


// Checks for a digit (0 through 9).
inline boolean isDigit (int c) {
return (isdigit (c) == 0 ? false : true);
}


// Checks for any printable character except space.
inline boolean isGraph (int c) {
return (isgraph (c) == 0 ? false : true);
}


// Checks for a lower-case character.
inline boolean isLowerCase (int c) {
return (islower (c) == 0 ? false : true);
}


// Checks for any printable character including space.
inline boolean isPrintable (int c) {
return (isprint (c) == 0 ? false : true);
}


// Checks for any printable character which is not a space
// or an alphanumeric character.
inline boolean isPunct (int c) {
return (ispunct (c) == 0 ? false : true);
}


// Checks for white-space characters. For the avr-libc library,
// these are: space, formfeed ('\f'), newline ('\n'), carriage
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
inline boolean isSpace (int c) {
return (isspace (c) == 0 ? false : true);
}


// Checks for an uppercase letter.
inline boolean isUpperCase (int c) {
return (isupper (c) == 0 ? false : true);
}


// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
// 8 9 a b c d e f A B C D E F.
inline boolean isHexadecimalDigit (int c) {
return (isxdigit (c) == 0 ? false : true);
}


// Converts c to a 7-bit unsigned char value that fits into the
// ASCII character set, by clearing the high-order bits.
inline int toAscii (int c) {
return toascii (c);
}


// Warning:
// Many people will be unhappy if you use this function.
// This function will convert accented letters into random
// characters.

// Converts the letter c to lower case, if possible.
inline int toLowerCase (int c) {
return tolower (c);
}


// Converts the letter c to upper case, if possible.
inline int toUpperCase (int c) {
return toupper (c);
}

#endif
Loading

0 comments on commit 03b1ccb

Please sign in to comment.