Skip to content

Commit 8a9fd91

Browse files
authored
Merge pull request #63 from sakuraio/double-to-float
Add SakuraIOUtils::double2float() helper function
2 parents 99df55c + 345b7f7 commit 8a9fd91

6 files changed

Lines changed: 68 additions & 1 deletion

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

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Please see example code.
171171

172172
* 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/).)
173173

174+
* 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))
175+
174176
# License
175177

176178
The MIT License (MIT)
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Provides functions for "sakura.io" (IoT platform of SAKURA Internet In
77
category=Communication
88
url=https://github.com/sakuraio/SakuraIOArduino
99
architectures=*
10-
includes=SakuraIO.h
10+
includes=SakuraIO.h,SakuraIOUtils.h

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_

0 commit comments

Comments
 (0)