forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
Initial commit: PI_mikrokontroler changes
This commit is contained in:
87
firmware_adxl345_spi/include/ADXL345FreshSPI.h
Normal file
87
firmware_adxl345_spi/include/ADXL345FreshSPI.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include "ADXL345Registers.h"
|
||||
#include <Logger.h>
|
||||
|
||||
class ADXL345FreshSPI {
|
||||
public:
|
||||
enum class Range { G2=0, G4=1, G8=2, G16=3 };
|
||||
enum class FIFOmode { BYPASS, FIFO, STREAM, TRIGGER };
|
||||
|
||||
struct SampleI16 {
|
||||
int16_t x, y, z;
|
||||
uint32_t ts_us;
|
||||
};
|
||||
struct SampleSI {
|
||||
float ax_g, ay_g, az_g;
|
||||
float ax_ms2, ay_ms2, az_ms2;
|
||||
uint32_t ts_us;
|
||||
};
|
||||
|
||||
ADXL345FreshSPI() = default;
|
||||
|
||||
// --- Init (SPI) ---
|
||||
// Uwaga: ADXL345 wymaga SPI MODE3, zegar ≤ ~5 MHz.
|
||||
bool begin(SPIClass *spi, uint8_t csPin, uint32_t clockHz = 5000000);
|
||||
|
||||
// --- Konfiguracja ---
|
||||
bool setODR_Hz(float odr_hz);
|
||||
bool setRange(Range r, bool fullRes=true);
|
||||
bool enableFIFO(FIFOmode mode, uint8_t triggerLevel=16);
|
||||
bool enableDataReadyInterrupt(bool enable=true); // przy pollingu pozostaw false
|
||||
|
||||
// --- Status ---
|
||||
bool ping(); // DEVID == 0xE5 ?
|
||||
bool available(); // DATA_READY z INT_SOURCE
|
||||
|
||||
// --- Odczyty „świeże” (blokujące do timeout_ms) ---
|
||||
bool readFresh(SampleI16& out, uint32_t timeout_ms = 100);
|
||||
bool readFresh(SampleSI& out, uint32_t timeout_ms = 100);
|
||||
|
||||
// --- Zrzut FIFO (do n elementów) ---
|
||||
size_t readFIFOBurst(SampleI16* buf, size_t maxCount);
|
||||
size_t readFIFOBurst(SampleSI* buf, size_t maxCount);
|
||||
|
||||
// --- Parametry pomocnicze ---
|
||||
void setGConstant(float g = 9.80665f) { g_ms2 = g; }
|
||||
|
||||
float lsb_per_g() const { return 1.0f / scale_g_per_lsb; }
|
||||
//bool write8(uint8_t reg, uint8_t val);
|
||||
//bool read8(uint8_t reg, uint8_t& val);
|
||||
|
||||
/*
|
||||
Zwraca tryb pracy akcelerometru
|
||||
*/
|
||||
uint8_t getADXLRange();
|
||||
|
||||
/* Zwraca czy jest FULL_RES w akcelerometrze */
|
||||
bool getADXLFullRes();
|
||||
|
||||
void showRangeFull(String txt="");
|
||||
|
||||
// niskopoziomowe
|
||||
bool write8(uint8_t reg, uint8_t val);
|
||||
bool read8(uint8_t reg, uint8_t& val);
|
||||
|
||||
private:
|
||||
SPIClass* spi = nullptr;
|
||||
uint8_t cs = 255;
|
||||
uint32_t spiHz = 5000000;
|
||||
|
||||
float scale_g_per_lsb = 0.0039f; // full-res ~3.9 mg/LSB
|
||||
float g_ms2 = 9.80665f;
|
||||
bool fullRes = true;
|
||||
|
||||
// niskopoziomowe
|
||||
//bool write8(uint8_t reg, uint8_t val);
|
||||
//bool read8(uint8_t reg, uint8_t& val);
|
||||
bool readMulti(uint8_t reg, uint8_t* dst, size_t n);
|
||||
|
||||
void spiSelect();
|
||||
void spiDeselect();
|
||||
|
||||
bool configurePowerMeasure();
|
||||
uint8_t odrCodeFromHz(float hz);
|
||||
void countsToSI(const SampleI16& in, SampleSI& out);
|
||||
};
|
||||
Reference in New Issue
Block a user