arduino / arduino/ArduinoCore-API

Suggest adding SPI_writeAnything to SPI library

Offen
#61 7 Kommentare 0 Reaktionen 1 zugewiesene Person Beansprucht von @cmaglie Auf GitHub ansehen
enhancement
Vorherrschende Sprache
C++
Sterne
306
Forks
150
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I have the following small template library on my page about SPI ( http://www.gammon.com.au/spi ):

```cpp
template unsigned int SPI_writeAnything (const T& value)
{
const byte * p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
SPI.transfer(*p++);
return i;
} // end of SPI_writeAnything

template unsigned int SPI_readAnything(T& value)
{
byte * p = (byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
*p++ = SPI.transfer (0);
return i;
} // end of SPI_readAnything

template unsigned int SPI_readAnything_ISR(T& value)
{
byte * p = (byte*) &value;
unsigned int i;
*p++ = SPDR; // get first byte
for (i = 1; i < sizeof value; i++)
*p++ = SPI.transfer (0);
return i;
} // end of SPI_readAnything_ISR
```

The function `SPI_readAnything_ISR` is for using inside an ISR, as it directly accesses `SPDR`.

It has been suggested to me that I request that you include this in the standard SPI library (SPI.h). This lets you more easily send things like `float`s or `struct`s via SPI. For example:

```cpp
float fnum = 42.666;
digitalWrite(SS, LOW);
SPI_writeAnything (fnum);
digitalWrite(SS, HIGH);
```

Being a template function it won't add any bloat unless you actually use it.

### Additional context

#### Related

- https://github.com/arduino/Arduino/issues/3691

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.