Skip to content

Commit

Permalink
Merge pull request #993 from pimoroni/patch-as7343-channels
Browse files Browse the repository at this point in the history
as7343: Fix channel and status order.
  • Loading branch information
Gadgetoid authored Sep 6, 2024
2 parents dfc1875 + 052dbce commit 871399a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
15 changes: 14 additions & 1 deletion drivers/as7343/as7343.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace pimoroni {
i2c->set_bits(address, reg::FIFO_MAP, 0, FIFO_MAP_CH5 | FIFO_MAP_CH4 | FIFO_MAP_CH3 | FIFO_MAP_CH2 | FIFO_MAP_CH1 | FIFO_MAP_CH0 | FIFO_MAP_ASTATUS);

// Set the PON bit
i2c->reg_write_uint8(address, reg::ENABLE, ENABLE_WEN | ENABLE_SMUXEN | ENABLE_SP_EN | ENABLE_PON);
i2c->reg_write_uint8(address, reg::ENABLE, ENABLE_WEN | ENABLE_SP_EN | ENABLE_PON);

return true;
}
Expand Down Expand Up @@ -135,10 +135,23 @@ namespace pimoroni {
}
}

void AS7343::start_measurement() {
if(running) return;
i2c->set_bits(address, reg::ENABLE, 0, ENABLE_SMUXEN);
running = true;
}

void AS7343::stop_measurement() {
i2c->set_bits(address, reg::ENABLE, 0, ENABLE_SMUXEN);
running = false;
}

void AS7343::read_fifo(uint16_t *buf) {
uint16_t expected_results = read_cycles * 7;
uint16_t result_slot = 0;

start_measurement();

while (i2c->reg_read_uint8(address, reg::FIFO_LVL) < expected_results) {
sleep_ms(1);
}
Expand Down
49 changes: 43 additions & 6 deletions drivers/as7343/as7343.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,68 @@ namespace pimoroni {
public:
struct reading {
// Cycle 1
uint16_t c1_astatus = 0; // (c1_astatus >> 8) & 0b10001111
// 0b10000000 = saturated
// 0b00001111 = gain lowest nibble
uint16_t FZ = 0; // 428-480 nm
uint16_t FY = 0; // 534-593 nm
uint16_t FXL = 0; // 593-628 nm
uint16_t NIR = 0; // 849-903 nm

uint16_t c1_vis_tl = 0; // Visible top-left
uint16_t c1_vis_br = 0; // Visible bottom-right
uint16_t c1_astatus = 0; // (c1_astatus >> 8) & 0b10001111
// 0b10000000 = saturated
// 0b00001111 = gain lowest nibble
//uint16_t c1_fd = 0; // This is where flicker detect *would* be

// Cycle 2
uint16_t c2_astatus = 0;
uint16_t F2 = 0; // 408-448 nm
uint16_t F3 = 0; // 448-500 mn
uint16_t F4 = 0; // 500-534 nm
uint16_t F6 = 0; // 618-665 nm

uint16_t c2_vis_tl = 0;
uint16_t c2_vis_br = 0;
uint16_t c2_astatus = 0;
//uint16_t c2_fd = 0;

// Cycle 3
uint16_t c3_astatus = 0;
uint16_t F1 = 0; // 396-408 nm
uint16_t F5 = 0; // 531-594 nm
uint16_t F7 = 0; // 685-715 nm
uint16_t F8 = 0; // 715-766 nm
uint16_t F5 = 0; // 531-594 nm

uint16_t c3_vis_tl = 0;
uint16_t c3_vis_br = 0;
uint16_t c3_astatus = 0;
//uint16_t c3_fd = 0;

float gain(uint8_t cycle) {
uint8_t status = 0;
switch(cycle) {
case 1:
status = c1_astatus & 0xf;
break;
case 2:
status = c2_astatus & 0xf;
break;
case 3:
status = c3_astatus & 0xf;
break;
}
return status ? 1 << (status - 1) : 0.5f;
}

bool saturated(uint8_t cycle) {
switch(cycle) {
case 1:
return c1_astatus & 0x80;
case 2:
return c2_astatus & 0x80;
case 3:
return c3_astatus & 0x80;
default:
return false;
}
}
};


Expand All @@ -95,6 +127,8 @@ namespace pimoroni {
uint8_t read_cycles = 1;
uint8_t ch_count = (uint8_t)channel_count::SIX_CHANNEL;

bool running = false;


//--------------------------------------------------
// Constructors/Destructor
Expand All @@ -120,6 +154,9 @@ namespace pimoroni {

void get_version(uint8_t &auxid, uint8_t &revid, uint8_t &hwid);

void start_measurement();
void stop_measurement();

reading read();

void read_fifo(uint16_t *buf);
Expand Down
2 changes: 2 additions & 0 deletions examples/breakout_as7343/as7343_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ int main() {
reading.F8
);

printf("Gain: C1: %fx C2: %fx C3: %fx\n", reading.gain(1), reading.gain(2), reading.gain(3));

sleep_ms(1000);
}

Expand Down

0 comments on commit 871399a

Please sign in to comment.