|
39 | 39 | /* Includes ------------------------------------------------------------------*/ |
40 | 40 |
|
41 | 41 | #include "Arduino.h" |
42 | | -#include "Wire.h" |
43 | 42 | #include "LPS25HBSensor.h" |
44 | 43 |
|
45 | 44 |
|
46 | 45 | /* Class Implementation ------------------------------------------------------*/ |
47 | | - |
48 | 46 | /** Constructor |
49 | 47 | * @param i2c object of an helper class which handles the I2C peripheral |
50 | 48 | * @param address the address of the component's instance |
51 | 49 | */ |
52 | | -LPS25HBSensor::LPS25HBSensor(TwoWire *i2c) : dev_i2c(i2c) |
| 50 | +LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address) |
| 51 | +{ |
| 52 | + dev_spi = NULL; |
| 53 | +} |
| 54 | + |
| 55 | +/** Constructor |
| 56 | + * @param spi object of an helper class which handles the SPI peripheral |
| 57 | + * @param cs_pin the chip select pin |
| 58 | + * @param spi_speed the SPI speed |
| 59 | + */ |
| 60 | +LPS25HBSensor::LPS25HBSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed) |
53 | 61 | { |
54 | | - address = LPS25HB_ADDRESS_HIGH; |
| 62 | + dev_i2c = NULL; |
| 63 | + address = 0; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Configure the sensor in order to be used |
| 68 | + * @retval 0 in case of success, an error code otherwise |
| 69 | + */ |
| 70 | +LPS25HBStatusTypeDef LPS25HBSensor::begin(void) |
| 71 | +{ |
| 72 | + if(dev_spi) |
| 73 | + { |
| 74 | + // Configure CS pin |
| 75 | + pinMode(cs_pin, OUTPUT); |
| 76 | + digitalWrite(cs_pin, HIGH); |
| 77 | + } |
55 | 78 |
|
56 | 79 | /* Power down the device */ |
57 | 80 | if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR ) |
58 | 81 | { |
59 | | - return; |
| 82 | + return LPS25HB_STATUS_ERROR; |
60 | 83 | } |
61 | 84 |
|
62 | 85 | if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR ) |
63 | 86 | { |
64 | | - return; |
| 87 | + return LPS25HB_STATUS_ERROR; |
65 | 88 | } |
66 | 89 |
|
67 | 90 | /* Enable interrupt circuit */ |
68 | 91 | if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR ) |
69 | 92 | { |
70 | | - return; |
| 93 | + return LPS25HB_STATUS_ERROR; |
71 | 94 | } |
72 | 95 |
|
73 | 96 | /* Set block data update mode */ |
74 | 97 | if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR ) |
75 | 98 | { |
76 | | - return; |
| 99 | + return LPS25HB_STATUS_ERROR; |
77 | 100 | } |
78 | 101 |
|
79 | 102 | /* Set SPI mode */ |
80 | 103 | if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR ) |
81 | 104 | { |
82 | | - return; |
| 105 | + return LPS25HB_STATUS_ERROR; |
83 | 106 | } |
84 | 107 |
|
85 | 108 | /* Set internal averaging sample counts for pressure and temperature */ |
86 | 109 | if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR ) |
87 | 110 | { |
88 | | - return; |
| 111 | + return LPS25HB_STATUS_ERROR; |
89 | 112 | } |
90 | | -}; |
91 | 113 |
|
| 114 | + return LPS25HB_STATUS_OK; |
| 115 | +} |
92 | 116 |
|
93 | | -/** Constructor |
94 | | - * @param i2c object of an helper class which handles the I2C peripheral |
95 | | - * @param address the address of the component's instance |
| 117 | +/** |
| 118 | + * @brief Disable the sensor and relative resources |
| 119 | + * @retval 0 in case of success, an error code otherwise |
96 | 120 | */ |
97 | | -LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address) |
| 121 | +LPS25HBStatusTypeDef LPS25HBSensor::end(void) |
98 | 122 | { |
99 | | - /* Power down the device */ |
100 | | - if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR ) |
101 | | - { |
102 | | - return; |
103 | | - } |
104 | | - |
105 | | - if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR ) |
106 | | - { |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - /* Enable interrupt circuit */ |
111 | | - if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR ) |
112 | | - { |
113 | | - return; |
114 | | - } |
115 | | - |
116 | | - /* Set block data update mode */ |
117 | | - if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR ) |
| 123 | + /* Disable pressure and temperature sensor */ |
| 124 | + if (Disable() != LPS25HB_STATUS_OK) |
118 | 125 | { |
119 | | - return; |
120 | | - } |
121 | | - |
122 | | - /* Set SPI mode */ |
123 | | - if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR ) |
124 | | - { |
125 | | - return; |
| 126 | + return LPS25HB_STATUS_ERROR; |
126 | 127 | } |
127 | 128 |
|
128 | | - /* Set internal averaging sample counts for pressure and temperature */ |
129 | | - if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR ) |
| 129 | + /* Reset CS configuration */ |
| 130 | + if(dev_spi) |
130 | 131 | { |
131 | | - return; |
| 132 | + // Configure CS pin |
| 133 | + pinMode(cs_pin, INPUT); |
132 | 134 | } |
133 | | -}; |
134 | 135 |
|
| 136 | + return LPS25HB_STATUS_OK; |
| 137 | +} |
135 | 138 |
|
136 | 139 | /** |
137 | 140 | * @brief Enable LPS25HB |
|
0 commit comments