Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
299 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// StreamUtils - github.com/bblanchon/ArduinoStreamUtils | ||
// Copyright Benoit Blanchon 2019-2022 | ||
// MIT License | ||
// | ||
// This example shows how to use StringStream | ||
|
||
#include <StreamUtils.h> | ||
|
||
const char data[] PROGMEM = "This string is in program memory."; | ||
|
||
void setup() { | ||
// Initialize serial port | ||
Serial.begin(9600); | ||
while (!Serial) | ||
continue; | ||
|
||
ProgmemStream stream{data}; | ||
while (stream.available()) { | ||
Serial.write(stream.read()); | ||
} | ||
} | ||
|
||
void loop() { | ||
// not used in this example | ||
} | ||
|
||
/***************************************************** | ||
* * | ||
* Love this project? * | ||
* Star it on GitHub! * | ||
* * | ||
* .,,. * | ||
* ,,:1. * | ||
* ,.,:;1 * | ||
* .,,,::;: * | ||
* ,,,,::;;. * | ||
* .,,,:::;;; * | ||
* .....,,,,...,.,,,,,,:::,,,,,,,,,,,,, * | ||
* ,,,,,,,,,,,:,...,,,,,,:::,,,,:::;;;11l * | ||
* .;::::::::,,,,,,,,,,:::::,,::;;;1lt * | ||
* .;;;:::,,,,,,,,::::::;:::;;1t: * | ||
* :;;:,,,,,,::::::;;;;;;l1 * | ||
* ,,,,:::::::;;;;;;l * | ||
* .,,,,::::;;;;;;;:::: * | ||
* ,,,,,:::;;;;;::,:::1 * | ||
* ,,,,,::;;;t1:,,:::::;l * | ||
* .,,,,:;;ll ;::::::;;, * | ||
* ,,,:;ll. .1:::;;l * | ||
* .,:lt, .1;;l: * | ||
* * | ||
* https://github.com/bblanchon/ArduinoStreamUtils * | ||
* * | ||
*****************************************************/ |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// StreamUtils - github.com/bblanchon/ArduinoStreamUtils | ||
// Copyright Benoit Blanchon 2019-2022 | ||
// MIT License | ||
|
||
#include "StreamUtils/Streams/ProgmemStream.hpp" | ||
|
||
#include "doctest.h" | ||
|
||
using namespace StreamUtils; | ||
|
||
TEST_CASE("ProgmemStream") { | ||
GIVEN("constructor was called with {0}") { | ||
ProgmemStream s{static_cast<const char*>(0)}; | ||
|
||
THEN("available() should return 0") { | ||
CHECK(s.available() == 0); | ||
} | ||
|
||
THEN("peek() should return -1") { | ||
CHECK(s.peek() == -1); | ||
} | ||
|
||
THEN("read() should return -1") { | ||
CHECK(s.read() == -1); | ||
} | ||
|
||
THEN("readBytes() should return 0") { | ||
char buffer[8]; | ||
CHECK(s.readBytes(buffer, 8) == 0); | ||
} | ||
} | ||
|
||
GIVEN("constructor was called with {F(\"hello\"))}") { | ||
ProgmemStream s{F("hello")}; | ||
|
||
THEN("available() should return 5") { | ||
CHECK(s.available() == 5); | ||
} | ||
} | ||
|
||
GIVEN("constructor was called with {F(\"hello\"), 5)}") { | ||
ProgmemStream s(F("hello"), 5); | ||
|
||
THEN("write(char) should return 0") { | ||
CHECK(s.write('a') == 0); | ||
} | ||
|
||
THEN("print(const char*) should return 0") { | ||
CHECK(s.print("abc") == 0); | ||
} | ||
|
||
THEN("available() should return 5") { | ||
CHECK(s.available() == 5); | ||
} | ||
|
||
THEN("peek() should return 'h'") { | ||
CHECK(s.peek() == 'h'); | ||
} | ||
|
||
WHEN("read() is called") { | ||
int c = s.read(); | ||
|
||
THEN("it sould return 'h'") { | ||
CHECK(c == 'h'); | ||
} | ||
|
||
THEN("available() should return 4") { | ||
CHECK(s.available() == 4); | ||
} | ||
|
||
THEN("peek() should return 'e'") { | ||
CHECK(s.peek() == 'e'); | ||
} | ||
} | ||
|
||
WHEN("readBytes(buffer, 4) is called") { | ||
char buffer[4]; | ||
int n = s.readBytes(buffer, 4); | ||
|
||
THEN("it should return 4") { | ||
CHECK(n == 4); | ||
} | ||
|
||
THEN("available() should return 1") { | ||
CHECK(s.available() == 1); | ||
} | ||
|
||
THEN("peek() should return 'o'") { | ||
CHECK(s.peek() == 'o'); | ||
} | ||
|
||
THEN("read() should return 'o'") { | ||
CHECK(s.read() == 'o'); | ||
} | ||
} | ||
|
||
WHEN("readBytes(buffer, 8) is called") { | ||
char buffer[8]; | ||
int n = s.readBytes(buffer, 8); | ||
|
||
THEN("it should return 5") { | ||
CHECK(n == 5); | ||
} | ||
|
||
THEN("available() should return 0") { | ||
CHECK(s.available() == 0); | ||
} | ||
|
||
THEN("peek() should return -1") { | ||
CHECK(s.peek() == -1); | ||
} | ||
|
||
THEN("read() should return -1") { | ||
CHECK(s.read() == -1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
class __FlashStringHelper; | ||
|
||
inline const __FlashStringHelper* F(const char* s) { | ||
return reinterpret_cast<const __FlashStringHelper*>(s + 42); | ||
} | ||
|
||
inline size_t strlen_P(const char* s) { | ||
return strlen(s - 42); | ||
} | ||
|
||
inline uint8_t pgm_read_byte(const char* p) { | ||
return static_cast<uint8_t>(p[-42]); | ||
} | ||
|
||
inline void* memcpy_P(void* dest, const void* src, size_t size) { | ||
return memcpy(dest, reinterpret_cast<const char*>(src) - 42, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// StreamUtils - github.com/bblanchon/ArduinoStreamUtils | ||
// Copyright Benoit Blanchon 2019-2022 | ||
// MIT License | ||
|
||
#pragma once | ||
|
||
#include <Arduino.h> | ||
|
||
#include "../Configuration.hpp" | ||
|
||
class __FlashStringHelper; | ||
|
||
namespace StreamUtils { | ||
|
||
class ProgmemStream : public Stream { | ||
public: | ||
ProgmemStream(const void* ptr, size_t size) | ||
: _ptr(reinterpret_cast<const char*>(ptr)), _size(size) {} | ||
|
||
ProgmemStream(const char* ptr) : _ptr(ptr), _size(ptr ? strlen_P(ptr) : 0) {} | ||
|
||
ProgmemStream(const __FlashStringHelper* ptr) | ||
: ProgmemStream{reinterpret_cast<const char*>(ptr)} {} | ||
|
||
int available() override { | ||
return _size; | ||
} | ||
|
||
int read() override { | ||
if (_size <= 0) | ||
return -1; | ||
_size--; | ||
return pgm_read_byte(_ptr++); | ||
} | ||
|
||
#if STREAMUTILS_STREAM_READBYTES_IS_VIRTUAL | ||
size_t readBytes(char* buffer, size_t size) override { | ||
if (size > _size) | ||
size = _size; | ||
memcpy_P(buffer, _ptr, size); | ||
_ptr += size; | ||
_size -= size; | ||
return size; | ||
} | ||
#endif | ||
|
||
int peek() override { | ||
if (_size <= 0) | ||
return -1; | ||
return pgm_read_byte(_ptr); | ||
} | ||
|
||
void flush() override {} | ||
|
||
#if STREAMUTILS_PRINT_WRITE_VOID_UINT32 | ||
size_t write(const void*, uint32) override { | ||
return 0; | ||
} | ||
#else | ||
size_t write(const uint8_t*, size_t) override { | ||
return 0; | ||
} | ||
#endif | ||
|
||
size_t write(uint8_t) override { | ||
return 0; | ||
} | ||
|
||
private: | ||
const char* _ptr; | ||
size_t _size; | ||
}; | ||
|
||
} // namespace StreamUtils |