Skip to content

Commit 2ef4aff

Browse files
authored
Merge branch 'master' into feat/detailed_comment
2 parents 04fe289 + 5ca93f0 commit 2ef4aff

10 files changed

Lines changed: 110 additions & 7 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ script:
2323
- arduino --verify --board arduino:avr:uno $PWD/examples/Shell/Shell.ino
2424
- arduino --verify --board arduino:avr:uno $PWD/examples/Standard/Standard.ino
2525
- arduino --verify --board arduino:avr:uno $PWD/examples/CdS/CdS.ino
26+
- arduino --verify --board arduino:avr:uno $PWD/examples/double2float/double2float.ino
2627
notifications:
2728
email:
2829
on_success: change

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 SAKURA Internet Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ Please see example code.
169169
* [Shell](./examples/Shell/Shell.ino)
170170
* [Send illminance (CdS)](./examples/CdS/CdS.ino)
171171

172+
# Notes
172173

174+
* On the Uno and other ATMEGA based boards, Values ​​of type double are sent as float type. (Please see [Arduino Reference]( https://www.arduino.cc/reference/en/language/variables/data-types/double/).)
175+
176+
* Please use SakuraIOUtils::double2float() when you want to handle the value of double type received as float. DO NOT USE C type casting. (Please see [the example](./examples/double2float/double2float.ino))
173177

174178
# License
175179

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <SakuraIO.h>
2+
#include <SakuraIOUtils.h>
3+
4+
//SakuraIO_SPI sakuraio(10);
5+
SakuraIO_I2C sakuraio;
6+
7+
void setup(){
8+
9+
}
10+
11+
void loop(){
12+
uint8_t ret;
13+
14+
uint8_t channel;
15+
uint8_t type;
16+
uint8_t values[8];
17+
int64_t offset;
18+
ret = sakuraio.dequeueRx(&channel, &type, values, &offset);
19+
20+
if (ret != 0x01) {
21+
return;
22+
}
23+
24+
if (type == 'd') {
25+
float f = SakuraIOUtils::double2float(*((uint64_t *)values));
26+
27+
// On the Uno and other ATMEGA based boards, this occupies 4 bytes.
28+
// That is, the double implementation is exactly the same as the float.
29+
// DO NOT USE C type casting.
30+
// double d = *((double *)values); // bad
31+
// float f = (float)*((double *)values); // bad
32+
}
33+
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=SakuraIO
2-
version=1.1.4
2+
version=1.1.5
33
author=SAKURA Internet Inc. <support@sakura.ad.jp>
44
maintainer=chibiegg <y-egusa@sakura.ad.jp>
55
sentence=Library for Sakura Communication Module (https://sakura.io/)
66
paragraph=Provides functions for "sakura.io" (IoT platform of SAKURA Internet Inc.) (https://sakura.io/) with Sakura Communication Module. This library supports SCM-LTE-Beta and SCM-LTE-01.
77
category=Communication
88
url=https://github.com/sakuraio/SakuraIOArduino
99
architectures=*
10-
includes=SakuraIO.h
10+
includes=SakuraIO.h,SakuraIOUtils.h

src/SakuraIO.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ uint8_t SakuraIO::enqueueTx(uint8_t ch, float value, uint64_t offset){
164164
}
165165

166166
uint8_t SakuraIO::enqueueTx(uint8_t ch, double value, uint64_t offset){
167+
168+
// check sizeof(double)
169+
//
170+
// On the Uno and other ATMEGA based boards,
171+
// this occupies 4 bytes. That is, the double
172+
// implementation is exactly the same as the float,
173+
// with no gain in precision.
174+
// https://www.arduino.cc/reference/en/language/variables/data-types/double/
175+
if (sizeof(double) == 4){
176+
return enqueueTx(ch, (float)value, offset);
177+
}
178+
167179
return enqueueTxRaw(ch, 'd', 8, (uint8_t *)&value, offset);
168180
}
169181

src/SakuraIOUtils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "SakuraIOUtils.h"
2+
3+
namespace SakuraIOUtils {
4+
5+
float double2float(uint64_t d){
6+
7+
uint8_t sign = d>>63;
8+
uint32_t exponent = (d>>52) & 0x7ff;
9+
uint64_t fraction = d & 0xfffffffffffff;
10+
11+
12+
uint32_t fv = 0;
13+
fv |= (uint32_t)sign << 31;
14+
fv |= (uint32_t)(exponent>>3)<<23;
15+
fv |= (uint32_t)(fraction>>29);
16+
17+
float *pf = (float *)&fv;
18+
return *pf;
19+
}
20+
21+
}

src/SakuraIOUtils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _SAKURAIOUTILS_H_
2+
#define _SAKURAIOUTILS_H_
3+
4+
#include <stdint.h>
5+
6+
namespace SakuraIOUtils {
7+
float double2float(uint64_t d);
8+
}
9+
10+
#endif // _SAKURAIOUTILS_H_

src/SakuraIO_I2C.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void SakuraIO_I2C::end(){
1919
Wire.endTransmission();
2020
break;
2121
case MODE_READ:
22-
Wire.requestFrom(SAKURAIO_SLAVE_ADDR, 1, true);
22+
Wire.requestFrom((uint8_t)SAKURAIO_SLAVE_ADDR, (uint8_t)1, (uint8_t)true);
2323
if( Wire.available() ) Wire.read();
2424
break;
2525
}

src/SakuraIO_SPI.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
#include <Arduino.h>
12
#include <SPI.h>
23
#include "SakuraIO.h"
34
#include "SakuraIO/debug.h"
45

5-
#if SPI_HAS_TRANSACTION
6+
#ifdef SPI_HAS_TRANSACTION
67
static SPISettings settings;
78
#endif
89

910
void SakuraIO_SPI::begin(){
10-
#if SPI_HAS_TRANSACTION
11+
#ifdef SPI_HAS_TRANSACTION
1112
SPI.beginTransaction(settings);
1213
#endif
1314
dbgln("CS=0");
@@ -17,7 +18,7 @@ void SakuraIO_SPI::begin(){
1718
void SakuraIO_SPI::end(){
1819
dbgln("CS=1");
1920
digitalWrite(cs, HIGH);
20-
#if SPI_HAS_TRANSACTION
21+
#ifdef SPI_HAS_TRANSACTION
2122
SPI.endTransaction();
2223
#endif
2324
delayMicroseconds(20);
@@ -47,7 +48,7 @@ uint8_t SakuraIO_SPI::receiveByte(){
4748
SakuraIO_SPI::SakuraIO_SPI(int _cs){
4849
cs = _cs;
4950
SPI.begin();
50-
#if SPI_HAS_TRANSACTION
51+
#ifdef SPI_HAS_TRANSACTION
5152
settings = SPISettings(350000, MSBFIRST, SPI_MODE0); // 350kHz, MSB First, SPI mode 0
5253
#endif
5354
pinMode(cs, OUTPUT);

0 commit comments

Comments
 (0)