Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as7343: Fix channel and status order. #993

Merged
merged 1 commit into from
Sep 6, 2024
Merged

Conversation

Gadgetoid
Copy link
Member

The astatus value was being interpreted as FZ, F2 and F1 causing bizarre readings and all other channels to be shifted one place.

Additionally the datasheet has been updated to re-order F5, F7 and F8 to F7, F8, F5, so these channels have been re-ordered accordingly.

Note: This will turn sensor output from confusing abject nonsense into what looks like pretty reasonable colour data and fixes #989.

The astatus value was being interpreted as FZ, F2 and F1 causing bizarre
readings and all other channels to be shifted one place.

Additionally the datasheet has been updated to re-order F5, F7 and F8 to
F7, F8, F5, so these channels have been re-ordered accordingly.

Note: This will turn sensor output from confusing abject nonsense into
what looks like pretty reasonable colour data and fixes #989.
@Gadgetoid
Copy link
Member Author

Tested with the code from the issue:

from breakout_as7343 import BreakoutAS7343
from pimoroni_i2c import PimoroniI2C
from picographics import PicoGraphics, DISPLAY_PICO_W_EXPLORER, PEN_P4
from pimoroni import Button
import time

i2c = PimoroniI2C(sda=20, scl=21)

as7343 = BreakoutAS7343(i2c)

as7343.set_gain(1024)
as7343.set_measurement_time(33)  # Roughly 30fps at 16ms/measurement
as7343.set_integration_time(27800)
as7343.set_channels(18)

button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)

display = PicoGraphics(display=DISPLAY_PICO_W_EXPLORER, pen_type=PEN_P4, rotate=270)
display.set_font("bitmap8")
display.set_backlight(0.5)


WIDTH, HEIGHT = display.get_bounds()
#WIDTH = 240
#HEIGHT = 240
#the above call doesn't get this for the display_pico_explorer

print (WIDTH, HEIGHT)

BLACK = display.create_pen(0, 0, 0)

FZ = display.create_pen(0, 70, 255)
FY = display.create_pen(179, 255, 0)
FXL = display.create_pen(255, 190, 0)
NIR = display.create_pen(97, 0, 0)
F2 = display.create_pen(84, 0, 255)
F3 = display.create_pen(0, 192, 255)
F4 = display.create_pen(31, 255, 0)
F6 = display.create_pen(255, 33, 0)
F1 = display.create_pen(130, 0, 200)
F5 = display.create_pen(163, 255, 0)
F7 = display.create_pen(255, 0, 0)
F8 = display.create_pen(171, 0, 0)

WHITE = display.create_pen(255, 255, 255)


BAR_WIDTH = 16
BAR_SPACING = 4
MARGIN = display.measure_text("NIR") + 2
BAR_HEIGHT = WIDTH - MARGIN

OFFSET_LEFT = int((HEIGHT - 110 - ((BAR_WIDTH + BAR_SPACING) * 12)) / 2)

# Starting max values for auto-ranging
# From figure 8 of the datasheet
# F3 in pos 6 was 962 but gave overrange
# F5 in pos 10 was 1967 but also overrange
MAX_VALUES = [
    2711,
    4684,
    5970,
    13226,
    2371,
    5000,
    3926,
    4170,
    7760,
    5000,
    6774,
    1166
]

LABELS = [
    "FZ",
    "FY",
    "FXL",
    "NIR",
    "F2",
    "F3",
    "F4",
    "F6",
    "F1",
    "F5",
    "F7",
    "F8"
]

PLACE = [
    3,
    6,
    8,
    12,
    2,
    4,
    5,
    9,
    1,
    7,
    10,
    11
]


while True:
    display.set_pen(0)
    display.clear()
    readings = as7343.read()
    for i, reading in enumerate(readings):
        MAX_VALUES[i] = max(reading, MAX_VALUES[i])
        scaled = int(reading / MAX_VALUES[i] * BAR_HEIGHT)
#       y = i * (BAR_WIDTH + BAR_SPACING)
        y = PLACE[i] * (BAR_WIDTH + BAR_SPACING)
        y += OFFSET_LEFT

        display.set_pen(i + 1)
        display.rectangle(MARGIN, y, scaled, BAR_WIDTH)

        display.set_pen(WHITE)
        display.text(LABELS[i], 0, y + 1)

    display.update()
    if button_b.is_pressed:
        as7343.set_illumination_led(False)
    elif button_a.is_pressed:       
        as7343.set_illumination_current(4)
        as7343.set_illumination_led(True)
    elif button_x.is_pressed:
        as7343.set_illumination_current(10)
        as7343.set_illumination_led(True)
    time.sleep(0.01)

Used a 1 hour RGB cycle video on my phone and observed a regular pattern of red, green, blue cycling with interim colours comprising a mix of those. Seems to work well!

@Gadgetoid Gadgetoid merged commit 871399a into main Sep 6, 2024
30 checks passed
@Gadgetoid Gadgetoid deleted the patch-as7343-channels branch October 31, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[-breakout-] AS7343
1 participant