Compare commits

..
25 Commits
Author SHA1 Message Date
Khoi Hoang dc88bb078f v1.13.0 for LwIP W6100 and fix bug
### Releases v1.13.0

1. Add support to ESP32 boards using `LwIP W6100 Ethernet`
2. Fix bug of `_parseURL()`. Check [Bug with _parseURL() #21](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/issues/21)
3. Improve `README.md` so that links can be used in other sites, such as `PIO`
2023-02-01 01:00:41 -05:00
Khoi Hoang e1ae41ed24 v1.13.0 for LwIP W6100 and fix bug
### Releases v1.13.0

1. Add support to ESP32 boards using `LwIP W6100 Ethernet`
2. Fix bug of `_parseURL()`. Check [Bug with _parseURL() #21](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/issues/21)
3. Improve `README.md` so that links can be used in other sites, such as `PIO`
2023-02-01 00:58:15 -05:00
Khoi Hoang c9a376e2fc v1.13.0 for LwIP W6100 and fix bug
### Releases v1.13.0

1. Add support to ESP32 boards using `LwIP W6100 Ethernet`
2. Fix bug of `_parseURL()`. Check [Bug with _parseURL() #21](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/issues/21)
3. Improve `README.md` so that links can be used in other sites, such as `PIO`
2023-02-01 00:54:32 -05:00
Khoi Hoang cad009d718 v1.13.0 for LwIP W6100 and fix bug
### Releases v1.13.0

1. Add support to ESP32 boards using `LwIP W6100 Ethernet`
2. Fix bug of `_parseURL()`. Check [Bug with _parseURL() #21](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/issues/21)
3. Improve `README.md` so that links can be used in other sites, such as `PIO`
2023-02-01 00:53:15 -05:00
Khoi Hoang 08d950eb13 Update .github 2023-02-01 00:52:12 -05:00
Khoi Hoang 8b03589b02 Update README.md 2022-12-04 23:15:57 -05:00
Khoi Hoang b93352c940 Update changelog.md 2022-12-04 23:15:43 -05:00
Khoi Hoang 06c722029d v1.12.0 for ESP32 and LwIP W5500 Ethernet
### Releases v1.12.0

1. Add support to ESP32 boards using `LwIP W5500 Ethernet`
2. Fix bug. Check [Fix "blank new line in chunk" bug #50](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/50)
2022-11-30 17:13:18 -05:00
Khoi Hoang c84d355d1b v1.12.0 for ESP32 and LwIP W5500 Ethernet
### Releases v1.12.0

1. Add support to ESP32 boards using `LwIP W5500 Ethernet`
2. Fix bug. Check [Fix "blank new line in chunk" bug #50](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/50)
2022-11-30 17:10:20 -05:00
Khoi Hoang 3949066843 v1.12.0 for ESP32 and LwIP W5500 Ethernet
### Releases v1.12.0

1. Add support to ESP32 boards using `LwIP W5500 Ethernet`
2. Fix bug. Check [Fix "blank new line in chunk" bug #50](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/pull/50)
2022-11-30 17:09:38 -05:00
Khoi Hoang ce452fb60f Merge pull request #50 from 1618033/patch-1
Fix "blank new line in chunk" bug
2022-11-30 03:24:55 -05:00
1618033 887bdef1cd Fix "blank new line in chunk" bug
If a chunk contains an empty line ("\r\n") chunkLength will be 0 and _setReadyState(readyStateDone) will be called, resulting in truncated payloads. We actually need to wait for "0\r\n" to set state as done.
2022-11-29 19:19:46 -06:00
Khoi Hoang 6fdfafd0c7 v1.11.0 for ESP32 and LwIP ENC28J60 Ethernet
### Releases v1.11.0

1. Add support to ESP32 boards using LwIP ENC28J60 Ethernet
2. Use `allman astyle` and add `utils`. Restyle the library
2022-11-28 23:24:09 -05:00
Khoi Hoang ce09b7d741 v1.11.0 for ESP32 and LwIP ENC28J60 Ethernet
### Releases v1.11.0

1. Add support to ESP32 boards using LwIP ENC28J60 Ethernet
2. Use `allman astyle` and add `utils`. Restyle the library
2022-11-28 23:20:44 -05:00
Khoi Hoang 9647523b2a v1.11.0 for ESP32 and LwIP ENC28J60 Ethernet
### Releases v1.11.0

1. Add support to ESP32 boards using LwIP ENC28J60 Ethernet
2. Use `allman astyle` and add `utils`. Restyle the library
2022-11-28 23:19:15 -05:00
Khoi Hoang 4b56b009c7 Update README.md and use allman astyle 2022-11-23 17:06:13 -05:00
Khoi Hoang f48c4a7ff2 Update Packages' Patches 2022-11-23 17:05:04 -05:00
Khoi Hoang a94b6658b5 Update Packages' Patches 2022-11-23 17:04:14 -05:00
Khoi Hoang ffb8322b5e Update Packages' Patches 2022-11-23 17:03:12 -05:00
Khoi Hoang 9dd39f4771 Update Packages' Patches 2022-11-23 17:02:20 -05:00
Khoi Hoang 8e1db58b7d Update .github 2022-11-23 16:59:34 -05:00
Khoi Hoang c693b74eb1 Fix Codespell complaints 2022-11-17 23:19:34 -05:00
Khoi Hoang d0eec9da72 Delete check-arduino.yml 2022-11-17 23:19:02 -05:00
Khoi Hoang c81b3c70de Update .github 2022-11-17 20:04:13 -05:00
Khoi Hoang 0de65ee0c3 Add files via upload 2022-11-17 20:03:57 -05:00
172 changed files with 11300 additions and 6756 deletions
+7
View File
@@ -0,0 +1,7 @@
# See: https://github.com/codespell-project/codespell#using-a-config-file
[codespell]
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
ignore-words-list = ,
check-filenames =
check-hidden =
skip = ./.git,./src,./examples,./Packages_Patches,./LibraryPatches
+9 -3
View File
@@ -31,12 +31,14 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.
---
### Information
Please ensure to specify the following:
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `ESP8266`,`ESP32` or `STM32` Core Version (e.g. ESP8266 core v3.0.2, ESP32 v2.0.5 or STM32 v2.3.0)
* `ESP8266` or`ESP32` Core Version (e.g. ESP8266 core v3.1.1, ESP32 v2.0.6)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
@@ -44,13 +46,17 @@ Please ensure to specify the following:
* Network configuration
Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted.
---
### Example
```
Arduino IDE version: 1.8.19
ESP32 Core Version 2.0.5
ESP32 Core Version 2.0.6
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using this library
+10
View File
@@ -0,0 +1,10 @@
# See: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates#about-the-dependabotyml-file
version: 2
updates:
# Configure check for outdated GitHub Actions actions in workflows.
# See: https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-dependabot
- package-ecosystem: github-actions
directory: / # Check the repository's workflows under /.github/workflows/
schedule:
interval: daily
+1 -1
View File
@@ -24,7 +24,7 @@ markComment: >
for your contributions.
unmarkComment: >
[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.
[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it opening the future.
closeComment: >
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.
+2 -2
View File
@@ -4,8 +4,8 @@ jobs:
check-bats-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '14'
- run: npm install -g bats
+16
View File
@@ -0,0 +1,16 @@
name: Report Size Deltas
on:
schedule:
- cron: '*/5 * * * *'
jobs:
report:
runs-on: ubuntu-latest
steps:
- name: Comment size deltas reports to PRs
uses: arduino/report-size-deltas@v1
with:
# The name of the workflow artifact created by the "Compile Examples" workflow
sketches-reports-source: sketches-reports
+22
View File
@@ -0,0 +1,22 @@
name: Spell Check
on:
pull_request:
push:
schedule:
# run every Tuesday at 3 AM UTC
- cron: "0 3 * * 2"
workflow_dispatch:
repository_dispatch:
jobs:
spellcheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
# See: https://github.com/codespell-project/actions-codespell/blob/master/README.md
- name: Spell check
uses: codespell-project/actions-codespell@master
+9 -3
View File
@@ -10,12 +10,14 @@ However, before reporting a bug please check through the following:
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/new).
---
### How to submit a bug report
Please ensure to specify the following:
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `ESP8266`,`ESP32` or `STM32` Core Version (e.g. ESP8266 core v3.0.2, ESP32 v2.0.5 or STM32 v2.3.0)
* `ESP8266` or`ESP32` Core Version (e.g. ESP8266 core v3.1.1, ESP32 v2.0.6)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
@@ -23,13 +25,17 @@ Please ensure to specify the following:
* Network configuration
Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted.
---
### Example
```
Arduino IDE version: 1.8.19
ESP32 Core Version 2.0.5
ESP32 Core Version 2.0.6
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using this library
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.project_flags=
build.cache_flags=
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.project_flags=
build.cache_flags=
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.project_flags=
build.cache_flags=
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.project_flags=
build.cache_flags=
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -1,49 +1,49 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
@@ -1,152 +1,152 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
* Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
* SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
* QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -1,49 +1,49 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
@@ -1,152 +1,152 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
* Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
* SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
* QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -1,49 +1,49 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
@@ -1,152 +1,152 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
* Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
* SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
* QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -1,36 +1,36 @@
/*
* Udp.cpp: Library to send/receive UDP packets.
*
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
* might not happen often in practice, but in larger network topologies, a UDP
* packet can be received out of sequence.
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
* aware of it. Again, this may not be a concern in practice on small local networks.
* For more information, see http://www.cafeaulait.org/course/week12/35.html
*
* MIT License:
* Copyright (c) 2008 Bjoern Hartmann
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* bjoern@cs.stanford.edu 12/30/2008
*/
Udp.cpp: Library to send/receive UDP packets.
NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
1) UDP does not guarantee the order in which assembled UDP packets are received. This
might not happen often in practice, but in larger network topologies, a UDP
packet can be received out of sequence.
2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
aware of it. Again, this may not be a concern in practice on small local networks.
For more information, see http://www.cafeaulait.org/course/week12/35.html
MIT License:
Copyright (c) 2008 Bjoern Hartmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
bjoern@cs.stanford.edu 12/30/2008
*/
#ifndef udp_h
#define udp_h
@@ -38,55 +38,63 @@
#include <Stream.h>
#include <IPAddress.h>
class UDP : public Stream {
class UDP : public Stream
{
public:
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
virtual void stop() =0; // Finish with the UDP socket
public:
virtual uint8_t begin(uint16_t) =
0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
// Sending UDP packets
// KH, add virtual function to support Multicast, necessary for many services (MDNS, UPnP, etc.)
virtual uint8_t beginMulticast(IPAddress, uint16_t)
{
return 0; // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
}
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) =0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() =0;
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
virtual void stop() = 0; // Finish with the UDP socket
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() =0;
// Number of bytes remaining in the current packet
virtual int available() =0;
// Read a single byte from the current packet
virtual int read() =0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) =0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) =0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() =0;
virtual void flush() =0; // Finish reading the current packet
// Sending UDP packets
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() =0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port) = 0;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port) = 0;
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket() = 0;
// Write a single byte into the packet
virtual size_t write(uint8_t) = 0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket() = 0;
// Number of bytes remaining in the current packet
virtual int available() = 0;
// Read a single byte from the current packet
virtual int read() = 0;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len) = 0;
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) = 0;
// Return the next byte from the current packet without moving on to the next byte
virtual int peek() = 0;
virtual void flush() = 0; // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP() = 0;
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr)
{
return addr.raw_address();
};
};
#endif
@@ -27,12 +27,13 @@
//https://www.u-blox.com/sites/default/files/NINA-B1_DataSheet_UBX-15019243.pdf
//https://www.u-blox.com/sites/default/files/EVK-NINA-B1_UserGuide_%28UBX-15028120%29_C1-Public.pdf
const uint32_t g_ADigitalPinMap[] = {
// D0 .. D13
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
5, // D0 is P0.05 (UART RX)
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
6, // D1 is P0.06 (UART TX)
7, // D2 is P0.07
31, // D3 is P0.31
18, // D4 is P0.18 (LED Blue)
99, // D5 (NC)
9, // D6 is P0.09 NFC1
@@ -34,8 +34,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -66,12 +66,12 @@ extern "C"
#define SW2 (19)
// NFC
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
#define PIN_NFC_1 (6) // P0.9
#define PIN_NFC_2 (7) // P0.10
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (16) // P0.03
#define PIN_A1 (17) // P0.02
#define PIN_A2 (18) // P0.04
@@ -125,8 +125,8 @@ static const uint8_t D15 = PIN_D15 ;
//#define PIN_VBAT PIN_A7
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL_RX (0) // P0.05
#define PIN_SERIAL_TX (1) // P0.06
#define PIN_SERIAL_CTS (2) // P0.07
@@ -135,8 +135,8 @@ static const uint8_t D15 = PIN_D15 ;
#define PIN_SERIAL_DSR (29) // P0.29
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (12) // P0.12
@@ -149,8 +149,8 @@ static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (14) // P0.02
@@ -165,8 +165,8 @@ static const uint8_t SCL = PIN_WIRE_SCL;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B112_UBLOX_
@@ -17,7 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#include "variant.h"
@@ -27,16 +27,16 @@
const uint32_t g_ADigitalPinMap[] =
{
// D0 .. D13
// D0 .. D13
29, // D0 is P0.29 (UART RX)
45, // D1 is P1.13 (UART TX)
45, // D1 is P1.13 (UART TX)
44, // D2 is P1.12 (NFC2)
31, // D3 is P0.31 (LED1)
13, // D4 is P0.13 (LED2)
11, // D5 is P0.11
9, // D6 is P0.09
10, // D7 is P0.10 (Button)
41, // D8 is P1.09
41, // D8 is P1.09
12, // D9 is P0.12
14, // D10 is P0.14
15, // D11 is P0.15
@@ -54,7 +54,7 @@ const uint32_t g_ADigitalPinMap[] =
// D20 .. D21 (aka I2C pins)
16, // D20 is P0.16 (SDA)
24, // D21 is P0.24 (SCL)
// QSPI pins (not exposed via any header / test point)
19, // D22 is P0.19 (QSPI CLK)
17, // D23 is P0.17 (QSPI CS)
@@ -62,7 +62,7 @@ const uint32_t g_ADigitalPinMap[] =
21, // D25 is P0.21 (QSPI Data 1)
22, // D26 is P0.22 (QSPI Data 2)
26, // D27 is P0.23 (QSPI Data 3)
40, // D28 is P1.08 - IO34
41, // D29 is P1.01 - IO35
44, // D30 is P1.02 - IO36
@@ -70,10 +70,10 @@ const uint32_t g_ADigitalPinMap[] =
42, // D32 is P1.10 - IO38
43, // D33 is P1.11 - IO39
47, // D34 is P1.15 - IO40
46, // D35 is P1.14 - IO41
46, // D35 is P1.14 - IO41
26, // D36 is P0.26 - IO42
6, // D37 is P0.6 - IO43
27, // D38 is P0.27 - IO44
27, // D38 is P0.27 - IO44
};
void initVariant()
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// Thanks to great work of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip).
// See [u-blox nina b](https://github.com/khoih-prog/WiFiNINA_Generic/issues/1)
#ifndef _VARIANT_NINA_B302_UBLOX_
@@ -29,8 +29,8 @@
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
@@ -41,7 +41,7 @@ extern "C"
// Number of pins defined in PinDescription array
#define PINS_COUNT (40)
#define NUM_DIGITAL_PINS (34)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_INPUTS (6)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
@@ -57,13 +57,13 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
/*
* Buttons
*/
Buttons
*/
#define PIN_BUTTON1 (7)
/*
* Analog pins
*/
Analog pins
*/
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
@@ -99,14 +99,14 @@ static const uint8_t A5 = PIN_A5 ;
#define PIN_NFC2 (2)
/*
* Serial interfaces
*/
Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
/*
* SPI Interfaces
*/
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (22) //24 original
@@ -119,8 +119,8 @@ static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
/*
* Wire Interfaces
*/
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (20)
@@ -143,7 +143,7 @@ static const uint8_t SCK = PIN_SPI_SCK;
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif //_VARIANT_NINA_B302_UBLOX_
@@ -1,49 +1,49 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 "variant.h"
#include "wiring_constants.h"
#include "wiring_digital.h"
#include "nrf.h"
const uint32_t g_ADigitalPinMap[] =
{
// P0
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
// P1
32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63
};
void initVariant()
{
// LED1 & LED2
pinMode(PIN_LED1, OUTPUT);
ledOff(PIN_LED1);
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
}
@@ -1,152 +1,152 @@
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
* Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
* SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
* Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
* QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
* Arduino objects - C++ only
*----------------------------------------------------------------------------*/
/*
Copyright (c) 2014-2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
Copyright (c) 2018, Adafruit Industries (adafruit.com)
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 _VARIANT_SPARKFUN52840MINI_
#define _VARIANT_SPARKFUN52840MINI_
/** Master clock frequency */
#define VARIANT_MCK (64000000ul)
#define USE_LFXO // Board uses 32khz crystal for LF
// define USE_LFRC // Board uses RC for LF
/*----------------------------------------------------------------------------
Headers
----------------------------------------------------------------------------*/
#include "WVariant.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
// Number of pins defined in PinDescription array
#define PINS_COUNT (64)
#define NUM_DIGITAL_PINS (64)
#define NUM_ANALOG_INPUTS (8)
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (7)
#define PIN_LED2 (14)
#define LED_BUILTIN PIN_LED1
#define LED_CONN PIN_LED2
#define LED_BLUE PIN_LED1
#define LED_RED PIN_LED2
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
/*
#define PIN_BUTTON1 (2)
#define PIN_BUTTON2 (3)
#define PIN_BUTTON3 (4)
#define PIN_BUTTON4 (5)
*/
/*
Analog pins
*/
#define PIN_A0 (2)
#define PIN_A1 (3)
#define PIN_A2 (4)
#define PIN_A3 (5)
#define PIN_A4 (28)
#define PIN_A5 (29)
#define PIN_A6 (30)
#define PIN_A7 (31)
static const uint8_t A0 = PIN_A0 ;
static const uint8_t A1 = PIN_A1 ;
static const uint8_t A2 = PIN_A2 ;
static const uint8_t A3 = PIN_A3 ;
static const uint8_t A4 = PIN_A4 ;
static const uint8_t A5 = PIN_A5 ;
static const uint8_t A6 = PIN_A6 ;
static const uint8_t A7 = PIN_A7 ;
#define ADC_RESOLUTION 14
// Other pins
#define PIN_AREF (2)
#define PIN_DFU (13)
#define PIN_NFC1 (9)
#define PIN_NFC2 (10)
static const uint8_t AREF = PIN_AREF;
/*
Serial interfaces
*/
// Serial
//Previous Hardware UART definition for nRF52 Arduino Core, below 0.16.0
//Feel free to comment out these two lines below if there are conflicts with latest release
#define PIN_SERIAL_RX (15)
#define PIN_SERIAL_TX (17)
//Hardware UART definition for nRF52 Arduino Core, 0.17.0 and above
#define PIN_SERIAL1_RX (15)
#define PIN_SERIAL1_TX (17)
/*
SPI Interfaces
*/
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MISO (31)
#define PIN_SPI_MOSI (3)
#define PIN_SPI_SCK (30)
static const uint8_t SS = 2 ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK = PIN_SPI_SCK ;
/*
Wire Interfaces
*/
#define WIRE_INTERFACES_COUNT 1
#define PIN_WIRE_SDA (8)
#define PIN_WIRE_SCL (11)
/*
QSPI interface for external flash
*/
#define PIN_QSPI_SCK 32
#define PIN_QSPI_CS 33
#define PIN_QSPI_DATA0 34
#define PIN_QSPI_DATA1 35
#define PIN_QSPI_DATA2 36
#define PIN_QSPI_DATA3 37
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#ifdef __cplusplus
}
#endif
/*----------------------------------------------------------------------------
Arduino objects - C++ only
----------------------------------------------------------------------------*/
#endif
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.cache_flags=
build.flags.optimize=
@@ -0,0 +1,466 @@
/*
Copyright (c) 2014 Arduino. 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
*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Arduino.h"
#include "Print.h"
//using namespace arduino;
// Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
size_t Print::print(const __FlashStringHelper *ifsh)
{
return print(reinterpret_cast<const char *>(ifsh));
}
size_t Print::print(const String &s)
{
return write(s.c_str(), s.length());
}
size_t Print::print(const char str[])
{
return write(str);
}
size_t Print::print(char c)
{
return write(c);
}
size_t Print::print(unsigned char b, int base)
{
return print((unsigned long) b, base);
}
size_t Print::print(int n, int base)
{
return print((long) n, base);
}
size_t Print::print(unsigned int n, int base)
{
return print((unsigned long) n, base);
}
size_t Print::print(long n, int base)
{
if (base == 0)
{
return write(n);
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0)
{
return write(n);
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
{
return printFloat(n, digits);
}
size_t Print::println(const __FlashStringHelper *ifsh)
{
size_t n = print(ifsh);
n += println();
return n;
}
size_t Print::print(const Printable& x)
{
return x.printTo(*this);
}
size_t Print::println(void)
{
return write("\r\n");
}
size_t Print::println(const String &s)
{
size_t n = print(s);
n += println();
return n;
}
size_t Print::println(const char c[])
{
size_t n = print(c);
n += println();
return n;
}
size_t Print::println(char c)
{
size_t n = print(c);
n += println();
return n;
}
size_t Print::println(unsigned char b, int base)
{
size_t n = print(b, base);
n += println();
return n;
}
size_t Print::println(int num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(unsigned int num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(unsigned long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(long long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(unsigned long long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(double num, int digits)
{
size_t n = print(num, digits);
n += println();
return n;
}
size_t Print::println(const Printable& x)
{
size_t n = print(x);
n += println();
return n;
}
size_t Print::printf(const char * format, ...)
{
char buf[256];
int len;
va_list ap;
va_start(ap, format);
len = vsnprintf(buf, 256, format, ap);
this->write(buf, len);
va_end(ap);
return len;
}
// Private Methods /////////////////////////////////////////////////////////////
size_t Print::printNumber(unsigned long n, uint8_t base)
{
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
*str = '\0';
// prevent crash if called with base == 1
if (base < 2)
base = 10;
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while (n);
return write(str);
}
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// if limited to base 10 and 16 the bufsize can be 20
char buf[64];
uint8_t i = 0;
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
innerLoops++;
}
while (n64 > th16)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
size_t Print::printFloat(double number, int digits)
{
if (digits < 0)
digits = 2;
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)
{
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
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
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
{
n += print(".");
}
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
unsigned int toPrint = (unsigned int)remainder;
n += print(toPrint);
remainder -= toPrint;
}
return n;
}
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0)
return 0;
for (int i = 0; i < len; i++)
{
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0)
return 0;
for (int i = 0; i < len; i++)
{
if (i != 0)
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len * 3 - 1);
}
@@ -0,0 +1,123 @@
/*
Copyright (c) 2016 Arduino LLC. 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
*/
#pragma once
#include <inttypes.h>
#include <stdio.h> // for size_t
#include "WString.h"
#include "Printable.h"
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
class Print
{
private:
int write_error;
size_t printNumber(unsigned long, uint8_t);
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
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;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(long long, int = DEC);
size_t print(unsigned long long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);
size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(long long, int = DEC);
size_t println(unsigned long long, int = DEC);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
virtual void flush() { /* Empty implementation for backward compatibility */ }
};
@@ -0,0 +1,227 @@
# Copyright (c) 2014-2015 Arduino LLC. 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
# Arduino SAMD Core and platform.
#
# For more info:
# https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification
name=Adafruit SAMD (32-bits ARM Cortex-M0+ and Cortex-M4) Boards
version=1.7.11
# Compile variables
# -----------------
compiler.warning_flags=-Werror=return-type
compiler.warning_flags.none=-Werror=return-type
compiler.warning_flags.default=-Werror=return-type
compiler.warning_flags.more=-Wall -Werror=return-type -Wno-expansion-to-defined
compiler.warning_flags.all=-Wall -Wextra -Werror=return-type -Wno-expansion-to-defined
compiler.path={runtime.tools.arm-none-eabi-gcc.path}/bin/
compiler.c.cmd=arm-none-eabi-gcc
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -MMD -D__SKETCH_NAME__="""{build.project_name}"""
compiler.c.elf.cmd=arm-none-eabi-g++
compiler.c.elf.flags=-Os -Wl,--gc-sections -save-temps
compiler.S.cmd=arm-none-eabi-gcc
compiler.S.flags=-c -g -x assembler-with-cpp -MMD
compiler.cpp.cmd=arm-none-eabi-g++
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -D__SKETCH_NAME__="""{build.project_name}"""
compiler.ar.cmd=arm-none-eabi-ar
compiler.ar.flags=rcs
compiler.objcopy.cmd=arm-none-eabi-objcopy
compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
compiler.elf2hex.bin.flags=-O binary
compiler.elf2hex.hex.flags=-O ihex -R .eeprom
compiler.elf2hex.cmd=arm-none-eabi-objcopy
compiler.ldflags=-mcpu={build.mcu} -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align
compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overridden in boards.txt
build.extra_flags=
build.cache_flags=
build.flags.optimize=
build.flags.maxspi=
build.flags.maxqspi=
build.flags.usbstack=
build.flags.debug=
# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
#compiler.c.elf.extra_flags=-v
compiler.cpp.extra_flags=
compiler.S.extra_flags=
compiler.ar.extra_flags=
compiler.elf2hex.extra_flags=
compiler.arm.cmsis.c.flags="-I{runtime.tools.CMSIS-5.4.0.path}/CMSIS/Core/Include/" "-I{runtime.tools.CMSIS-5.4.0.path}/CMSIS/DSP/Include/" "-I{runtime.tools.CMSIS-Atmel-1.2.2.path}/CMSIS/Device/ATMEL/"
compiler.arm.cmsis.ldflags="-L{runtime.tools.CMSIS-5.4.0.path}/CMSIS/Lib/GCC/" -larm_cortexM0l_math
compiler.libraries.ldflags=
# USB Flags
# ---------
build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_CONFIG_POWER={build.usb_power} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {build.flags.usbstack} {build.flags.debug} "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
# Default advertised device power setting in mA
build.usb_power=100
# Default usb manufacturer will be replaced at compile time using
# numeric vendor ID if available or by board's specific value.
build.usb_manufacturer="Unknown"
# Compile patterns
# ----------------
## Compile c files
## KH Add -DBOARD_NAME="{build.board}"
recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" -DARDUINO_ARCH_{build.arch} -DARDUINO_SAMD_ADAFRUIT {compiler.c.extra_flags} {build.extra_flags} {build.cache_flags} {build.flags.debug} {build.flags.optimize} {build.flags.maxspi} {build.flags.maxqspi} {compiler.arm.cmsis.c.flags} {includes} "{source_file}" -o "{object_file}"
## Compile c++ files
## KH Add -DBOARD_NAME="{build.board}"
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" -DARDUINO_ARCH_{build.arch} -DARDUINO_SAMD_ADAFRUIT {compiler.cpp.extra_flags} {build.extra_flags} {build.cache_flags} {build.flags.debug} {build.flags.optimize} {build.flags.maxspi} {build.flags.maxqspi} {build.extra_flags} {compiler.arm.cmsis.c.flags} {includes} "{source_file}" -o "{object_file}"
## Compile S files
## KH Add -DBOARD_NAME="{build.board}"
recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" -DARDUINO_ARCH_{build.arch} -DARDUINO_SAMD_ADAFRUIT {compiler.S.extra_flags} {build.extra_flags} {build.cache_flags} {compiler.arm.cmsis.c.flags} {includes} "{source_file}" -o "{object_file}"
## Create archives
# archive_file_path is needed for backwards compatibility with IDE 1.6.5 or older, IDE 1.6.6 or newer overrides this value
archive_file_path={build.path}/{archive_file}
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
## Combine gc-sections, archives, and objects
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} {compiler.libraries.ldflags} -Wl,--start-group {compiler.arm.cmsis.ldflags} "-L{build.variant.path}" -lm "{build.path}/{archive_file}" -Wl,--end-group
## Create output (bin file)
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.bin.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"
## Create output (hex file)
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
build.preferred_out_format=bin
## Save hex
recipe.output.tmp_file={build.project_name}.{build.preferred_out_format}
recipe.output.save_file={build.project_name}.{build.variant}.{build.preferred_out_format}
## Compute size
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
recipe.size.regex=\.text\s+([0-9]+).*
#
# BOSSA
#
tools.bossac.path={runtime.tools.bossac-1.7.0-arduino3.path}
tools.bossac.cmd=bossac
tools.bossac.cmd.windows=bossac.exe
tools.bossac.upload.params.verbose=-i -d
tools.bossac.upload.params.quiet=
tools.bossac.upload.pattern="{path}/{cmd}" {upload.verbose} --port={serial.port.file} -U {upload.native_usb} -i -e -w -v "{build.path}/{build.project_name}.bin" -R
tools.bossac_remote.upload.pattern=/usr/bin/run-bossac {upload.verbose} --port=ttyATH0 -U {upload.native_usb} -e -w -v /tmp/sketch.bin -R
tools.bossac.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
tools.bossac.upload.network_pattern="{network_cmd}" -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
# v1.8.0
tools.bossac18.path={runtime.tools.bossac-1.8.0-48-gb176eee.path}
tools.bossac18.cmd=bossac
tools.bossac18.cmd.windows=bossac.exe
tools.bossac18.upload.params.verbose=-i -d
tools.bossac18.upload.params.quiet=
tools.bossac18.upload.pattern="{path}/{cmd}" {upload.verbose} --port={serial.port.file} -U -i --offset={upload.offset} -w -v "{build.path}/{build.project_name}.bin" -R
tools.bossac18.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
tools.bossac18.upload.network_pattern="{network_cmd}" -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
#
# BOSSA (ignore binary size)
#
tools.bossacI.path={runtime.tools.bossac-1.7.0-arduino3.path}
tools.bossacI.cmd=bossac
tools.bossacI.cmd.windows=bossac.exe
tools.bossacI.upload.params.verbose=-i -d
tools.bossacI.upload.params.quiet=
tools.bossacI.upload.pattern="{path}/{cmd}" {upload.verbose} --port={serial.port.file} -I -U {upload.native_usb} -i -e -w "{build.path}/{build.project_name}.bin" -R
tools.bossacI_remote.upload.pattern=/usr/bin/run-bossac {upload.verbose} --port=ttyATH0 -U {upload.native_usb} -e -w -v /tmp/sketch.bin -R
tools.bossacI.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
tools.bossacI.upload.network_pattern="{network_cmd}" -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
#
# OpenOCD sketch upload
#
tools.openocd.path={runtime.tools.openocd-0.10.0-arduino7.path}
tools.openocd.cmd=bin/openocd
tools.openocd.cmd.windows=bin/openocd.exe
tools.openocd.upload.params.verbose=-d2
tools.openocd.upload.params.quiet=-d0
tools.openocd.upload.pattern="{path}/{cmd}" {upload.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; program {{build.path}/{build.project_name}.bin} verify reset 0x2000; shutdown"
tools.openocd.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
tools.openocd.upload.network_pattern={network_cmd} -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
# Program flashes the binary at 0x0000, so use the linker script without_bootloader
tools.openocd.program.params.verbose=-d2
tools.openocd.program.params.quiet=-d0
tools.openocd.program.pattern="{path}/{cmd}" {program.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; program {{build.path}/{build.project_name}.elf} verify reset; shutdown"
tools.openocd.erase.params.verbose=-d3
tools.openocd.erase.params.quiet=-d0
tools.openocd.erase.pattern=
tools.openocd.bootloader.params.verbose=-d2
tools.openocd.bootloader.params.quiet=-d0
tools.openocd.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; init; halt; at91samd bootloader 0; program {{runtime.platform.path}/bootloaders/{bootloader.file}} verify reset; shutdown"
#
# OpenOCD sketch upload - version with configurable bootloader size
# FIXME: this programmer is a workaround for default options being overwritten by uploadUsingPreferences
#
tools.openocd-withbootsize.path={runtime.tools.openocd-0.10.0-arduino7.path}
tools.openocd-withbootsize.cmd=bin/openocd
tools.openocd-withbootsize.cmd.windows=bin/openocd.exe
tools.openocd-withbootsize.upload.params.verbose=-d2
tools.openocd-withbootsize.upload.params.quiet=-d0
tools.openocd-withbootsize.upload.pattern="{path}/{cmd}" {upload.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; program {{build.path}/{build.project_name}.bin} verify reset {bootloader.size}; shutdown"
# Program flashes the binary at 0x0000, so use the linker script without_bootloader
tools.openocd-withbootsize.program.params.verbose=-d2
tools.openocd-withbootsize.program.params.quiet=-d0
tools.openocd-withbootsize.program.pattern="{path}/{cmd}" {program.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; program {{build.path}/{build.project_name}.elf} verify reset; shutdown"
tools.openocd-withbootsize.erase.params.verbose=-d3
tools.openocd-withbootsize.erase.params.quiet=-d0
tools.openocd-withbootsize.erase.pattern=
tools.openocd-withbootsize.bootloader.params.verbose=-d2
tools.openocd-withbootsize.bootloader.params.quiet=-d0
tools.openocd-withbootsize.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -s "{path}/share/openocd/scripts/" -f "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}" -c "telnet_port disabled; init; halt; at91samd bootloader 0; program {{runtime.platform.path}/bootloaders/{bootloader.file}} verify reset; shutdown"
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.cache_flags=
build.flags.optimize=
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}
@@ -52,7 +52,7 @@ compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=
compiler.readelf.cmd=arm-none-eabi-readelf
# this can be overriden in boards.txt
# this can be overridden in boards.txt
build.extra_flags=
build.cache_flags=
build.flags.optimize=
@@ -36,10 +36,15 @@
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
if (write(*buffer++)) n++;
else break;
while (size--)
{
if (write(*buffer++))
n++;
else
break;
}
return n;
}
@@ -80,46 +85,64 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
}
else
{
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
if (base == 0)
return write(n);
else
return printNumber(n, base);
}
size_t Print::print(long long n, int base)
{
if (base == 0) {
if (base == 0)
{
return write(n);
} else if (base == 10) {
if (n < 0) {
}
else if (base == 10)
{
if (n < 0)
{
int t = print('-');
n = -n;
return printULLNumber(n, 10) + t;
}
return printULLNumber(n, 10);
} else {
}
else
{
return printULLNumber(n, base);
}
}
size_t Print::print(unsigned long long n, int base)
{
if (base == 0) return write(n);
else return printULLNumber(n, base);
if (base == 0)
return write(n);
else
return printULLNumber(n, base);
}
size_t Print::print(double n, int digits)
@@ -253,14 +276,16 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
do {
do
{
char c = n % base;
n /= base;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
} while (n);
return write(str);
}
@@ -268,23 +293,23 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
// REFERENCE IMPLEMENTATION FOR ULL
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
// {
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// // if limited to base 10 and 16 the bufsize can be smaller
// char buf[65];
// char *str = &buf[64];
// *str = '\0';
// *str = '\0';
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// // prevent crash if called with base == 1
// if (base < 2) base = 10;
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// do {
// unsigned long long t = n / base;
// char c = n - t * base; // faster than c = n%base;
// n = t;
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
// } while(n);
// return write(str);
// return write(str);
// }
// FAST IMPLEMENTATION FOR ULL
@@ -296,11 +321,13 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
uint8_t innerLoops = 0;
// prevent crash if called with base == 1
if (base < 2) base = 10;
if (base < 2)
base = 10;
// process chunks that fit in "16 bit math".
uint16_t top = 0xFFFF / base;
uint16_t th16 = 1;
while (th16 < top)
{
th16 *= base;
@@ -311,31 +338,33 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
{
// 64 bit math part
uint64_t q = n64 / th16;
uint16_t r = n64 - q*th16;
uint16_t r = n64 - q * th16;
n64 = q;
// 16 bit math loop to do remainder. (note buffer is filled reverse)
for (uint8_t j=0; j < innerLoops; j++)
for (uint8_t j = 0; j < innerLoops; j++)
{
uint16_t qq = r/base;
buf[i++] = r - qq*base;
uint16_t qq = r / base;
buf[i++] = r - qq * base;
r = qq;
}
}
uint16_t n16 = n64;
while (n16 > 0)
{
uint16_t qq = n16/base;
buf[i++] = n16 - qq*base;
uint16_t qq = n16 / base;
buf[i++] = n16 - qq * base;
n16 = qq;
}
size_t bytes = i;
for (; i > 0; i--)
write((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
return bytes;
}
@@ -347,21 +376,29 @@ size_t Print::printFloat(double number, int 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
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)
{
n += print('-');
number = -number;
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
@@ -372,7 +409,8 @@ size_t Print::printFloat(double number, int digits)
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
if (digits > 0)
{
n += print(".");
}
@@ -390,31 +428,39 @@ size_t Print::printFloat(double number, int digits)
size_t Print::printBuffer(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if ( i != 0 ) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if ( i != 0 )
print(delim);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
size_t Print::printBufferReverse(uint8_t const buffer[], int len, char delim, int byteline)
{
if (buffer == NULL || len == 0) return 0;
if (buffer == NULL || len == 0)
return 0;
for(int i=0; i<len; i++)
for (int i = 0; i < len; i++)
{
if (i != 0) print(delim);
if ( byteline && (i%byteline == 0) ) println();
if (i != 0)
print(delim);
this->printf("%02X", buffer[len-1-i]);
if ( byteline && (i % byteline == 0) )
println();
this->printf("%02X", buffer[len - 1 - i]);
}
return (len*3 - 1);
return (len * 3 - 1);
}
@@ -37,26 +37,42 @@ class Print
size_t printULLNumber(unsigned long long, uint8_t);
size_t printFloat(double, int);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1)
{
write_error = err;
}
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(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;
size_t write(const char *str)
{
if (str == NULL)
return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
size_t write(const char *buffer, size_t size)
{
return write((const uint8_t *)buffer, size);
}
// default to zero, meaning "a single write may block"
// should be overridden by subclasses with buffering
virtual int availableForWrite() { return 0; }
virtual int availableForWrite()
{
return 0;
}
size_t print(const __FlashStringHelper *);
size_t print(const String &);
@@ -86,17 +102,17 @@ class Print
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
size_t printf(const char * format, ...);
size_t printBuffer(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBuffer(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBuffer(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBuffer((uint8_t const*) buffer, size, delim, byteline);
}
size_t printBufferReverse(uint8_t const buffer[], int len, char delim=' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim=' ', int byteline = 0)
size_t printBufferReverse(uint8_t const buffer[], int len, char delim = ' ', int byteline = 0);
size_t printBufferReverse(char const buffer[], int size, char delim = ' ', int byteline = 0)
{
return printBufferReverse((uint8_t const*) buffer, size, delim, byteline);
}

Some files were not shown because too many files have changed in this diff Show More