arduino / arduino/ArduinoCore-API

Suggest adding SPI_writeAnything to SPI library

Ouverte
#61 7 commentaires 0 réactions 1 personne assignée Réclamée par @cmaglie Voir sur GitHub
enhancement
Langage dominant
C++
Étoiles
306
Forks
150
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.