Arrays

Introduction

Supports arrays of simple types, such as char, int, double, or POD structures (i.e. basic C structures).

FSTR::Array is a class template, so requires an additional ElementType parameter:

#include <FlashString/Array.hpp>

DEFINE_FSTR_ARRAY(myDoubleArray, double,
   PI, 53.0, 100, 1e8, 47
);
Serial.print("My double array: ");
myDoubleArray.printTo(Serial);
Serial.println();

Note

Objects do not inherit from Printable because it is a virtual base class.

Therefore, statements like Serial.println(myDoubleArray) are not supported.

This also avoids ambiguity between implicit WString conversions.

There are some Print helper functions in the library you can use:

FSTR::println(Serial, myDoubleArray);

These are templated so will handle both simple data types and Objects.

You can share Arrays between translation units by declaring it in a header:

DECLARE_FSTR_ARRAY(table);

Macros

DECLARE_FSTR_ARRAY(name, ElementType)

Declare a global Array& reference.

Note
Use DEFINE_FSTR_ARRAY to instantiate the global Object
Parameters
  • name:
  • ElementType:

DEFINE_FSTR_ARRAY(name, ElementType, ...)

Define an Array Object with global reference.

Note
Unlike String, array is not NUL-terminated
Parameters
  • name: Name of Array& reference to define
  • ElementType:
  • ...: List of ElementType items

DEFINE_FSTR_ARRAY_LOCAL(name, ElementType, ...)

Like DEFINE_FSTR_ARRAY except reference is declared static constexpr.

DEFINE_FSTR_ARRAY_DATA(name, ElementType, ...)

Define an Array data structure.

Parameters
  • name: Name of data structure
  • ElementType:
  • ...: List of ElementType items

LOAD_FSTR_ARRAY(name, array)

Load an Array object into a named local (stack) buffer.

Note
Example:
DEFINE_FSTR_ARRAY(fsArray, double, 5.33, PI)
...
LOAD_FSTR_ARRAY(arr, fsArray)
printf("arr[0] = %f, %u elements, buffer is %u bytes\n", arr[0], fsArray.length(), sizeof(arr));

FSTR_ARRAY_ARRAY(name, ElementType, ...)

Define an Array and load it into a named buffer on the stack.

Note
Equivalent to ElementType name[] = {a, b, c} except the buffer is word-aligned

IMPORT_FSTR_ARRAY(name, ElementType, file)

Define an Array containing data from an external file.

See
See also IMPORT_FSTR_DATA
Parameters
  • name: Name for the Array object
  • ElementType: Array element type
  • file: Absolute path to the file containing the content

IMPORT_FSTR_ARRAY_LOCAL(name, ElementType, file)

Like IMPORT_FSTR_ARRAY except reference is declared static constexpr.

Classes

template <typename ElementType>
class Array : public FSTR::Object<Array<ElementType>, ElementType>

Class to access an array of integral values stored in flash.

Template Parameters
  • ElementType:

Public Types

template<>
using Iterator = ObjectIterator<Array<ElementType>, ElementType>

Public Functions

ArrayPrinter<Array> printer() const

Returns a printer object for this array.

Note
ElementType must be supported by Print

size_t printTo(Print &p) const
Iterator begin() const
Iterator end() const
size_t length() const

Get the length of the content in elements.

int indexOf(const ValueType &value) const
ElementType valueAt(unsigned index) const
ElementType operator[](unsigned index) const

Array operator[].

size_t elementSize() const
const ElementType *data() const
size_t read(size_t index, ElementType *buffer, size_t count) const

Read content into RAM.

Parameters
  • index: First element to read
  • buffer: Where to store data
  • count: How many elements to read
Return Value
  • size_t: Number of elements actually read

size_t read(size_t offset, void *buffer, size_t count) const

Read contents of a String into RAM.

Parameters
  • offset: Zero-based offset from start of flash data to start reading
  • buffer: Where to store data
  • count: How many bytes to read
Return Value
  • size_t: Number of bytes actually read

size_t readFlash(size_t index, ElementType *buffer, size_t count) const

Read content into RAM,using flashmem_read()

Parameters
  • index: First element to read
  • buffer: Where to store data
  • count: How many elements to read
Return Value
  • size_t: Number of elements actually read

size_t readFlash(size_t offset, void *buffer, size_t count) const

Read contents of a String into RAM, using flashread()

PROGMEM data is accessed via the CPU data cache, so to avoid degrading performance you can use this method to read data directly from flash memory. This is appropriate for infrequently accessed data, especially if it is large. For example, if storing content using

IMPORT_FSTR instead of SPIFFS then it is generally better to avoid contaminating the cache.
See
See also FlashMemoryStream class.
Parameters
  • offset: Zero-based offset from start of flash data to start reading
  • buffer: Where to store data
  • count: How many bytes to read
Return Value
  • size_t: Number of bytes actually read

size_t size() const

Get the object data size in bytes.

Note
Always an integer multiple of 4 bytes

template <class ObjectType>
constexpr const ObjectType &as() const

Cast to a different object type.

Note
example:
    fstr.as<Array<int>>();

bool isCopy() const
bool isNull() const

Indicates an invalid String, used for return value from lookups, etc.

Note
A real String can be zero-length, but it cannot be null

Public Members

uint32_t flashLength_

Public Static Functions

static const Array<ElementType> &empty()

Return an empty object which evaluates to null.