Skip to content

Commit 7e99ef5

Browse files
committed
Update to use new architecture for Arduino abstraction
1 parent 28be1f3 commit 7e99ef5

File tree

10 files changed

+203
-38
lines changed

10 files changed

+203
-38
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "SparkFun_STHS34PF80_Arduino_Library.h"
2+
#include <Wire.h>
3+
4+
STHS34PF80_I2C mySensor;
5+
// STHS34PF80_SPI mySensor;
6+
7+
void setup()
8+
{
9+
Serial.begin(115200);
10+
Serial.println("Exmaple 1! yay");
11+
12+
Wire.begin()
13+
14+
mySensor.begin();
15+
}
16+
17+
void loop()
18+
{
19+
Serial.println(mySensor.getData());
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef __SparkFun_STHS34PF80_Library_H__
2+
#define __SparkFun_STHS34PF80_Library_H__
3+
4+
#include "SparkFun_STHS34PF80_Arduino_Library.h"
5+
6+
bool STHS34PF80_I2C::begin(uint8_t devAddr)
7+
{
8+
bus.init(devAddr, Wire);
9+
sensor.read_reg = read;
10+
sensor.write_reg = write;
11+
sensor.mdelay = delayMS;
12+
sensor.handle = &bus;
13+
14+
STHS34PF80::begin();
15+
}
16+
17+
int32_t STHS34PF80_I2C::read(void* bus, uint8_t addr, uint8_t* data, uint16_t numData)
18+
{
19+
((SFE_BusI2C*) bus)->readRegs(addr, data, numData);
20+
}
21+
22+
int32_t STHS34PF80_I2C::write(void* bus, uint8_t addr, const uint8_t* data, uint16_t numData)
23+
{
24+
((SFE_BusI2C*) bus)->writeRegs(addr, data, numData);
25+
}
26+
27+
void STHS34PF80_I2C::delayMS(uint32_t millisec)
28+
{
29+
delay(millisec);
30+
}
31+
32+
#endif

src/SparkFun_STHS34PF80_Arduino_Library.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,32 @@ Development environment specifics:
1515
Distributed as-is; no warranty is given.
1616
******************************************************************************/
1717

18-
19-
#ifndef __SparkFun_STHS34PF80_Arduino_Library_H__
20-
#define __SparkFun_STHS34PF80_Arduino_Library_H__
21-
22-
#include sths34pf80_reg.h
18+
#include "sths34pf80_class.h"
19+
#include "SFE_Bus.h"
2320
#include <Arduino.h>
2421
#include <Wire.h>
2522

2623

27-
class STHS34PF80
24+
class STHS34PF80_I2C : public STHS34PF80
2825
{
2926
public:
30-
31-
/*
32-
begin()
33-
read()
34-
write()
35-
*/
36-
37-
38-
39-
private:
40-
41-
42-
/*
43-
Bus12C bus;
44-
*/
27+
bool begin(uint8_t devAddr);
28+
static int32_t read(void *, uint8_t, uint8_t *, uint16_t);
29+
static int32_t write(void *, uint8_t, const uint8_t *, uint16_t);
30+
static void delayMS(uint32_t millisec);
31+
private:
32+
SFE_BusI2C bus;
4533
};
4634

35+
// class STHS34PF80_SPI
36+
// {
37+
// public:
38+
// bool begin();
39+
40+
// private:
41+
4742

48-
#endif
43+
// /*
44+
// Bus12C bus;
45+
// */
46+
// };

src/sfe_bus.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,57 @@
1-
// For general SFE bus needs (I2C, SPI, etc)
1+
#ifndef __SparkFun_Bus_Library_H__
2+
#define __SparkFun_Bus_Library_H__
23

4+
#include "SFE_Bus.h"
5+
6+
bool SFE_BusI2C::init(uint8_t addr, TwoWire& port)
7+
{
8+
devAddr == adrr;
9+
devPort = port
10+
}
11+
12+
bool SFE_BusI2C::readRegs(uint8_t regAddress, uint8_t* dataBuffer, uint8_t numBytes)
13+
{
14+
// Jump to desired register address
15+
devPort->beginTransmission(devAddr);
16+
devPort->write(regAddress);
17+
if(devPort->endTransmission())
18+
{
19+
return false;
20+
}
21+
22+
// Read bytes from these registers
23+
devPort->requestFrom(devAddr, numBytes);
24+
25+
// Store all requested bytes
26+
for(uint32_t i = 0; i < numBytes && devPort->available(); i++)
27+
{
28+
dataBuffer[i] = devPort->read();
29+
}
30+
31+
return true;
32+
}
33+
34+
bool SFE_BusI2C::writeRegs(uint8_t regAddress, const uint8_t* dataBuffer, uint8_t numBytes)
35+
{
36+
// Begin transmission
37+
devPort->beginTransmission(devAddr);
38+
39+
// Write the address
40+
devPort->write(regAddress);
41+
42+
// Write all the data
43+
for(uint32_t i = 0; i < numBytes; i++)
44+
{
45+
devPort->write(dataBuffer[i]);
46+
}
47+
48+
// End transmission
49+
if(devPort->endTransmission())
50+
{
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
#endif

src/sfe_bus.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <Arduino.h>
2+
#include <Wire.h>
3+
4+
class SFE_BusI2C
5+
{
6+
public:
7+
bool init(uint8_t addr, TwoWire& port);
8+
bool readRegs(uint8_t regAddr, uint8_t* data, uint8_t numData);
9+
bool writeRegs(uint8_t regAddr, const uint8_t* data, uint8_t numData);
10+
11+
private:
12+
uint8_t devAddr;
13+
TwoWire* devPort;
14+
};

src/sths34pf80_api/LICENSE.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2022, STMicroelectronics
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
File renamed without changes.
File renamed without changes.

src/sths34pf80_class.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef __SparkFun_STHS34PF80_Library_H__
2+
#define __SparkFun_STHS34PF80_Library_H__
3+
4+
#include "sths34pf80_class.h"
5+
6+
bool STHS34PF80::begin()
7+
{
8+
return false;
9+
}
10+
11+
bool STHS34PF80::getData()
12+
{
13+
return false;
14+
}
15+
16+
bool STHS34PF80::isConencted()
17+
{
18+
uint8_t devId = 0;
19+
int32_t err = sths34pf80_device_id_get(&sensor, &devId);
20+
if(err)
21+
return false;
22+
return devId == STHS34PF80_ID;
23+
}
24+
25+
#endif

src/sths34pf80_class.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
#ifndef __sths34pf80_class_H__
2-
#define __sths34pf80_class_H__
1+
#include "sths34pf80_api/sths34pf80_reg.h"
32

4-
#include SparkFun_STHS34PF80_Arduino_Library.h
5-
6-
7-
8-
class STHS34PF80_Class
3+
class STHS34PF80
94
{
105
public:
11-
6+
bool begin();
7+
bool getData();
8+
bool isConencted();
129

1310
protected:
14-
15-
};
16-
17-
18-
19-
20-
#endif
11+
stmdev_ctx_t sensor;
12+
};

0 commit comments

Comments
 (0)