Skip to content

Commit

Permalink
Fixes UnitPAJ7620U2
Browse files Browse the repository at this point in the history
  • Loading branch information
GOB52 committed Jun 19, 2024
1 parent 1f0fbbd commit c98f45c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 52 deletions.
23 changes: 21 additions & 2 deletions examples/UnitGesture/gesture/gesture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,28 @@ m5::unit::UnitPAJ7620U2 unit;
m5::unit::UnitPaHub unitPaHub;
#endif

constexpr const char* gstr[] = {
"None", "Up ", "Down",
"Left", "Right", "Forward",
"Backward", "Clockwise", "CounterClockwise",
"Wave", "Approach", "HasObject",
"WakeupTrigger", "Confirm", "Abort",
"Reserve", "NoObject",
};

const char* gesture_to_string(const m5::unit::paj7620u2::Gesture g) {
unsigned int ui{m5::stl::to_underlying(g)};
ui = (ui == 0) ? 0 : __builtin_ctz(ui) + 1;

return ui < m5::stl::size(gstr) ? gstr[ui] : "ERROR";
}

} // namespace

void setup() {
m5::utility::delay(1500);


M5.begin();

auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
Expand Down Expand Up @@ -110,7 +129,7 @@ void loop() {
M5.update();
Units.update();
if (unit.updated()) {
M5_LOGI("gesture:%x", unit.gesture());

M5_LOGI("%lu:gesture:%s", unit.updatedMillis(),
gesture_to_string(unit.gesture()));
}
}
59 changes: 9 additions & 50 deletions lib/M5UnitGesture/src/unit/unit_PAJ7620U2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ bool UnitPAJ7620U2::begin() {
uint16_t id{};
uint8_t ver{};

m5::utility::delay(1); // Wait 700us for PAJ7620U2 to stabilize

if (!get_chip_id(id) || !get_version(ver)) {
M5_LIB_LOGE("Failed to get id/version %x:%x", id, ver);
return false;
Expand All @@ -81,46 +79,18 @@ bool UnitPAJ7620U2::begin() {
}
}
return select_bank(0, true);


// return setMode(_cfg.mode);
}

void UnitPAJ7620U2::update() {
#if 1
uint16_t detection{};
if (read_detection(detection) && detection) {
M5_LIB_LOGE(">>>>>[%x]", detection);

// paj7620u2::Gesture _gesture{paj7620u2::Gesture::None};
}
#endif

#if 0
if (_periodic) {
unsigned long at{m5::utility::millis()};
if (!_latest || at >= _latest + _interval) {
_interval = 1000; // 1sec
_updated = readMeasurement(_CO2eq, _TVOC);
if (_updated) {
_latest = at;
}
} else {
_updated = false;
}
_updated = false;

// Store baseline values every hour
if (_interval == 1000 &&
(!_latestBaseline || at >= _latestBaseline + BASELINE_INTERVAL)) {
_updatedBaseline = getIaqBaseline(_baselineCO2eq, _baselineTVOC);
if (_updatedBaseline) {
_latestBaseline = at;
}
} else {
_updatedBaseline = false;
}
if (read_detection(detection) && detection &&
__builtin_popcount((unsigned int)detection) == 1) {
_updated = true;
_latest = m5::utility::millis();
_gesture = static_cast<Gesture>(detection);
}
#endif
}

bool UnitPAJ7620U2::setMode(const paj7620u2::Mode m) {
Expand Down Expand Up @@ -170,18 +140,18 @@ bool UnitPAJ7620U2::select_bank(const uint8_t bank, const bool force) {
bool UnitPAJ7620U2::read_banked_register(const uint16_t reg, uint8_t* buf,
const size_t len) {
return select_bank((reg >> 8) & 1) &&
readRegister((uint8_t)(reg & 0xFF), buf, len, 0);
readRegister((uint8_t)(reg & 0xFF), buf, len, 1);
}

bool UnitPAJ7620U2::read_banked_register8(const uint16_t reg, uint8_t& value) {
return select_bank((reg >> 8) & 1) &&
readRegister8((uint8_t)(reg & 0xFF), value, 0);
readRegister8((uint8_t)(reg & 0xFF), value, 1);
}

bool UnitPAJ7620U2::read_banked_register16(const uint16_t reg,
uint16_t& value) {
return select_bank((reg >> 8) & 1) &&
readRegister16((uint8_t)(reg & 0xFF), value, 0);
readRegister16((uint8_t)(reg & 0xFF), value, 1);
}

bool UnitPAJ7620U2::write_banked_register(const uint16_t reg,
Expand Down Expand Up @@ -212,19 +182,8 @@ bool UnitPAJ7620U2::get_version(uint8_t& version) {
}

bool UnitPAJ7620U2::read_detection(uint16_t& detection) {
if (select_bank(0)) {
uint8_t high{}, low{};
if (readRegister8((uint8_t)0x44, high, 0) &&
readRegister8((uint8_t)0x43, low, 0)) {
detection = ((uint16_t)high) << 8 | low;
return true;
}
}
return false;
#if 0
return read_banked_register(GESTURE_DETECTION_INTERRUPT_FLAG_LOW,
(uint8_t*)&detection, 2);
#endif
}

} // namespace unit
Expand Down
7 changes: 7 additions & 0 deletions lib/M5UnitGesture/src/unit/unit_PAJ7620U2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ enum class Gesture : uint16_t {
Clockwise = 1U << 6, //!< Clockwise
CounterClockwise = 1U << 7, //!< Counter clock wise
Wave = 1U << 8, //!< Wave
Approach = 1U << 9, //!< Approach (Proximity mode only)
HasObject = 1U << 10,
WakeupTrigger = 1U << 11,
Confirm = 1U << 12,
Abort = 1U << 13,
Reserve = 1U << 14,
NoObject = 1U << 15,
};

/*!
Expand Down

0 comments on commit c98f45c

Please sign in to comment.