arduino / arduino/ArduinoCore-API

Suggest adding SPI_writeAnything to SPI library

未关闭
#61 7 条评论 0 个 reaction 已指派 1 人 已被 @cmaglie 认领 在 GitHub 查看
enhancement
主要语言
C++
星标
306
派生
150
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。