arduino / arduino/ArduinoCore-API

Suggest adding SPI_writeAnything to SPI library

Abierto
#61 7 comentarios 0 reacciones 1 asignado Reclamado por @cmaglie Ver en GitHub
enhancement
Lenguaje dominante
C++
Estrellas
306
Forks
150
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.