arduino / arduino/ArduinoCore-API

Suggest adding SPI_writeAnything to SPI library

Đang mở
#61 7 bình luận 0 reaction 1 người được giao Được @cmaglie nhận Xem trên GitHub
enhancement
Ngôn ngữ chính
C++
Star
306
Fork
150
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.