@@ -35,60 +35,60 @@ LIS3DHTR<T>::LIS3DHTR()
3535
3636}
3737
38- // template <class T>
39- // void LIS3DHTR<T>::begin(SPIClass &comm, uint8_t sspin)
40- // {
41- // chipSelectPin = sspin;
42- // _spi_com = &comm;
43- // _wire_com = NULL;
44- // pinMode(chipSelectPin, OUTPUT);
45- // digitalWrite(chipSelectPin, HIGH);
38+ template <class T >
39+ void LIS3DHTR<T>::begin(SPIClass &comm, uint8_t sspin)
40+ {
41+ chipSelectPin = sspin;
42+ _spi_com = &comm;
43+ _wire_com = NULL ;
44+ pinMode (chipSelectPin, OUTPUT);
45+ digitalWrite (chipSelectPin, HIGH);
4646
47- // // Maximum SPI frequency is 10MHz, Data is read and written MSb first,
48- // // Data is captured on rising edge of clock (CPHA = 0)
49- // // Base value of the clock is HIGH (CPOL = 1)
50- // // MODE3 for 328p operation
51- // SPISettings _settings(10000000, MSBFIRST, SPI_MODE3);
47+ // Maximum SPI frequency is 10MHz, Data is read and written MSb first,
48+ // Data is captured on rising edge of clock (CPHA = 0)
49+ // Base value of the clock is HIGH (CPOL = 1)
50+ // MODE3 for 328p operation
51+ SPISettings _settings (10000000 , MSBFIRST, SPI_MODE3);
5252
53- // // start the SPI library:
54- // _spi_com->begin();
53+ // start the SPI library:
54+ _spi_com->begin ();
5555
56- // delay(200);
56+ delay (200 );
5757
58- // uint8_t config5 = LIS3DHTR_REG_TEMP_ADC_PD_ENABLED |
59- // LIS3DHTR_REG_TEMP_TEMP_EN_DISABLED;
58+ uint8_t config5 = LIS3DHTR_REG_TEMP_ADC_PD_ENABLED |
59+ LIS3DHTR_REG_TEMP_TEMP_EN_DISABLED;
6060
61- // writeRegister(LIS3DHTR_REG_TEMP_CFG, config5);
62- // delay(LIS3DHTR_CONVERSIONDELAY);
61+ writeRegister (LIS3DHTR_REG_TEMP_CFG, config5);
62+ delay (LIS3DHTR_CONVERSIONDELAY);
6363
64- // uint8_t config1 = LIS3DHTR_REG_ACCEL_CTRL_REG1_LPEN_NORMAL | // Normal Mode
65- // LIS3DHTR_REG_ACCEL_CTRL_REG1_AZEN_ENABLE | // Acceleration Z-Axis Enabled
66- // LIS3DHTR_REG_ACCEL_CTRL_REG1_AYEN_ENABLE | // Acceleration Y-Axis Enabled
67- // LIS3DHTR_REG_ACCEL_CTRL_REG1_AXEN_ENABLE;
64+ uint8_t config1 = LIS3DHTR_REG_ACCEL_CTRL_REG1_LPEN_NORMAL | // Normal Mode
65+ LIS3DHTR_REG_ACCEL_CTRL_REG1_AZEN_ENABLE | // Acceleration Z-Axis Enabled
66+ LIS3DHTR_REG_ACCEL_CTRL_REG1_AYEN_ENABLE | // Acceleration Y-Axis Enabled
67+ LIS3DHTR_REG_ACCEL_CTRL_REG1_AXEN_ENABLE;
6868
69- // writeRegister(LIS3DHTR_REG_ACCEL_CTRL_REG1, config1);
70- // delay(LIS3DHTR_CONVERSIONDELAY);
69+ writeRegister (LIS3DHTR_REG_ACCEL_CTRL_REG1, config1);
70+ delay (LIS3DHTR_CONVERSIONDELAY);
7171
72- // uint8_t config4 = LIS3DHTR_REG_ACCEL_CTRL_REG4_BDU_NOTUPDATED | // Continuous Update
73- // LIS3DHTR_REG_ACCEL_CTRL_REG4_BLE_LSB | // Data LSB @ lower address
74- // LIS3DHTR_REG_ACCEL_CTRL_REG4_HS_DISABLE | // High Resolution Disable
75- // LIS3DHTR_REG_ACCEL_CTRL_REG4_ST_NORMAL | // Normal Mode
76- // LIS3DHTR_REG_ACCEL_CTRL_REG4_SIM_4WIRE; // 4-Wire Interface
72+ uint8_t config4 = LIS3DHTR_REG_ACCEL_CTRL_REG4_BDU_NOTUPDATED | // Continuous Update
73+ LIS3DHTR_REG_ACCEL_CTRL_REG4_BLE_LSB | // Data LSB @ lower address
74+ LIS3DHTR_REG_ACCEL_CTRL_REG4_HS_DISABLE | // High Resolution Disable
75+ LIS3DHTR_REG_ACCEL_CTRL_REG4_ST_NORMAL | // Normal Mode
76+ LIS3DHTR_REG_ACCEL_CTRL_REG4_SIM_4WIRE; // 4-Wire Interface
7777
78- // writeRegister(LIS3DHTR_REG_ACCEL_CTRL_REG4, config4);
78+ writeRegister (LIS3DHTR_REG_ACCEL_CTRL_REG4, config4);
7979
80- // delay(LIS3DHTR_CONVERSIONDELAY);
80+ delay (LIS3DHTR_CONVERSIONDELAY);
8181
82- // setFullScaleRange(LIS3DHTR_RANGE_16G);
83- // setOutputDataRate(LIS3DHTR_DATARATE_400HZ);
84- // }
82+ setFullScaleRange (LIS3DHTR_RANGE_16G);
83+ setOutputDataRate (LIS3DHTR_DATARATE_400HZ);
84+ }
8585
8686template <class T >
8787void LIS3DHTR<T>::begin(TwoWire &wire, uint8_t address)
8888{
8989 _wire_com = &wire;
9090 _wire_com->begin ();
91- // _spi_com = NULL;
91+ _spi_com = NULL ;
9292 devAddr = address;
9393
9494 uint8_t config5 = LIS3DHTR_REG_TEMP_ADC_PD_ENABLED |
@@ -360,22 +360,22 @@ uint16_t LIS3DHTR<T>::readbitADC3(void)
360360template <class T >
361361void LIS3DHTR<T>::writeRegister(uint8_t reg, uint8_t val)
362362{
363- // if (_spi_com != NULL)
364- // {
365- // _spi_com->beginTransaction(_settings);
366- // digitalWrite(chipSelectPin, LOW);
367- // _spi_com->transfer(reg);
368- // _spi_com->transfer(val);
369- // digitalWrite(chipSelectPin, HIGH);
370- // _spi_com->endTransaction();
371- // }
372- // else
373- // {
363+ if (_spi_com != NULL )
364+ {
365+ _spi_com->beginTransaction (_settings);
366+ digitalWrite (chipSelectPin, LOW);
367+ _spi_com->transfer (reg);
368+ _spi_com->transfer (val);
369+ digitalWrite (chipSelectPin, HIGH);
370+ _spi_com->endTransaction ();
371+ }
372+ else
373+ {
374374 _wire_com->beginTransmission (devAddr);
375375 _wire_com->write (reg);
376376 _wire_com->write (val);
377377 _wire_com->endTransmission ();
378- // }
378+ }
379379}
380380
381381template <class T >
@@ -386,23 +386,23 @@ void LIS3DHTR<T>::readRegisterRegion(uint8_t *outputPointer, uint8_t reg, uint8_
386386 uint8_t i = 0 ;
387387 uint8_t c = 0 ;
388388
389- // if (_spi_com != NULL)
390- // {
391- // _spi_com->beginTransaction(_settings);
392- // digitalWrite(chipSelectPin, LOW);
393- // _spi_com->transfer(reg | 0x80 | 0x40); //Ored with "read request" bit and "auto increment" bit
394- // while (i < length) // slave may send less than requested
395- // {
396- // c = _spi_com->transfer(0x00); // receive a byte as character
397- // *outputPointer = c;
398- // outputPointer++;
399- // i++;
400- // }
401- // digitalWrite(chipSelectPin, HIGH);
402- // _spi_com->endTransaction();
403- // }
404- // else
405- // {
389+ if (_spi_com != NULL )
390+ {
391+ _spi_com->beginTransaction (_settings);
392+ digitalWrite (chipSelectPin, LOW);
393+ _spi_com->transfer (reg | 0x80 | 0x40 ); // Ored with "read request" bit and "auto increment" bit
394+ while (i < length) // slave may send less than requested
395+ {
396+ c = _spi_com->transfer (0x00 ); // receive a byte as character
397+ *outputPointer = c;
398+ outputPointer++;
399+ i++;
400+ }
401+ digitalWrite (chipSelectPin, HIGH);
402+ _spi_com->endTransaction ();
403+ }
404+ else
405+ {
406406
407407 _wire_com->beginTransmission (devAddr);
408408 reg |= 0x80 ; // turn auto-increment bit on, bit 7 for I2C
@@ -417,7 +417,7 @@ void LIS3DHTR<T>::readRegisterRegion(uint8_t *outputPointer, uint8_t reg, uint8_
417417 outputPointer++;
418418 i++;
419419 }
420- // }
420+ }
421421}
422422
423423template <class T >
@@ -473,5 +473,5 @@ void LIS3DHTR<T>::click(uint8_t c, uint8_t click_thresh, uint8_t limit, uint8_t
473473template <class T >
474474LIS3DHTR<T>::operator bool () { return isConnection (); }
475475
476- // template class LIS3DHTR<SPIClass>;
476+ template class LIS3DHTR <SPIClass>;
477477template class LIS3DHTR <TwoWire>;
0 commit comments