Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This library supports the complete API exposed by the Nicla Sense Env sensor board over I2C.

- 🌈 RGB LED control
- ⚪️ White LED control
- ⚪️ Orange LED control
- 💤 Board control (sleep, reset, factory reset)
- 🔧 Board configuration (e.g. changing the I2C address)
- 🏠 Indoor Air Quality Sensor control
Expand Down Expand Up @@ -46,7 +46,7 @@ if device.connected:
# ...
rgb_led = device.rgb_led
# ...
white_led = device.white_led
orange_led = device.orange_led
# ...
```
Once the desired object is obtained you can call functions and query properties on these objects such as `temperature_sensor.temperature`.
Expand All @@ -65,4 +65,4 @@ The following scripts are examples of how to use the Nicla Sense Env board with
- [rgb_led.py](../examples/rgb_led.py): Demonstrates how to control the board's RGB LED.
- [temperature_humidity.py](../examples/temperature_humidity.py): Demonstrates how to read the temperature and humidity data from the board's sensors.
- [uart_read.py](../examples/uart_read.py): Shows how to read data from the UART port on the board when not connecting to it over I2C.
- [white_led.py](../examples/white_led.py): Demonstrates how to control the board's white LED.
- [orange_led.py](../examples/orange_led.py): Demonstrates how to control the board's orange LED.
34 changes: 34 additions & 0 deletions examples/orange_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
This example shows how to control the orange LED on the Nicla Sense Env board.
"""

from arduino_nicla_sense_env import NiclaSenseEnv, IndoorAirQualitySensorMode
from time import sleep_ms

def pulse_led(led):
"""
Pulses the LED.
"""
# Fade in
for i in range(0, 255):
led.brightness = i
sleep_ms(4)

# Fade out
for i in range(255, -1, -1):
led.brightness = i
sleep_ms(4)

device = NiclaSenseEnv()

if device.connected:
orange_led = device.orange_led

print(f"🔢 Orange LED error status enabled: {orange_led.error_status_enabled}")
print(f"💡 Orange LED brightness: {orange_led.brightness}")
pulse_led(orange_led)

# Enable sensor error indication on orange LED (LED should turn off if sensors are okay)
orange_led.error_status_enabled = True
else:
print("🤷 Device could not be found. Please double check the wiring.")
10 changes: 5 additions & 5 deletions examples/rgb_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def pulse_led(led):
"""
Pulses the LED from off to full brightness and back to off.
"""
print(f"🌈 RGB values: {rgb_led.rgb}")
print(f"🌈 RGB values: {rgb_led.color}")

for i in range(0, 256):
led.brightness = i
Expand All @@ -23,13 +23,13 @@ def pulse_colors(led):
Pulses the LED through the colors red, green, blue and white.
"""
# Brightness can also be set via 4th tuple element
led.rgb = (255, 0, 0, 255)
led.color = (255, 0, 0, 255)
pulse_led(led)
led.rgb = (0, 255, 0)
led.color = (0, 255, 0)
pulse_led(led)
led.rgb = (0, 0, 255)
led.color = (0, 0, 255)
pulse_led(led)
led.rgb = (255, 255, 255)
led.color = (255, 255, 255)
pulse_led(led)

device = NiclaSenseEnv()
Expand Down
34 changes: 0 additions & 34 deletions examples/white_led.py

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
["arduino_nicla_sense_env/outdoor_air_quality_sensor.py", "github:arduino/arduino-nicla-sense-env-mpy/src/arduino_nicla_sense_env/outdoor_air_quality_sensor.py"],
["arduino_nicla_sense_env/rgb_led.py", "github:arduino/arduino-nicla-sense-env-mpy/src/arduino_nicla_sense_env/rgb_led.py"],
["arduino_nicla_sense_env/temperature_humidity_sensor.py", "github:arduino/arduino-nicla-sense-env-mpy/src/arduino_nicla_sense_env/temperature_humidity_sensor.py"],
["arduino_nicla_sense_env/white_led.py", "github:arduino/arduino-nicla-sense-env-mpy/src/arduino_nicla_sense_env/white_led.py"]
["arduino_nicla_sense_env/orange_led.py", "github:arduino/arduino-nicla-sense-env-mpy/src/arduino_nicla_sense_env/orange_led.py"]
],
"deps": [
],
Expand Down
2 changes: 1 addition & 1 deletion src/arduino_nicla_sense_env/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"status": {"address": 0x00, "type": "uint8", "bytes": 1}, # Board Status Register
"slave_address": {"address": 0x01, "type": "uint8", "bytes": 1}, # Board Slave Address Register (valid immediately after writing)
"control": {"address": 0x02, "type": "uint8", "bytes": 1}, # Board Control Register
"white_led": {"address": 0x03, "type": "uint8", "bytes": 1}, # White LED Control Register
"orange_led": {"address": 0x03, "type": "uint8", "bytes": 1}, # Orange LED Control Register
"rgb_led_red": {"address": 0x04, "type": "uint8", "bytes": 1}, # RGB LED Control Register RED (4x uint8)
"rgb_led_green": {"address": 0x05, "type": "uint8", "bytes": 1}, # GREEN
"rgb_led_blue": {"address": 0x06, "type": "uint8", "bytes": 1}, # BLUE
Expand Down
14 changes: 7 additions & 7 deletions src/arduino_nicla_sense_env/nicla_sense_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .indoor_air_quality_sensor import IndoorAirQualitySensor
from .outdoor_air_quality_sensor import OutdoorAirQualitySensor
from .rgb_led import RGBLED
from .white_led import WhiteLED
from .orange_led import OrangeLED
from .i2c_device import I2CDevice
from .constants import REGISTERS

Expand All @@ -29,8 +29,8 @@ def store_settings_in_flash(self):
- Indoor air quality sensor mode
- Outdoor air quality sensor mode
- Temperature sensor enabled
- White LED brightness
- White LED error status enabled
- Orange LED brightness
- Orange LED error status enabled
- RGB LED brightness
- RGB LED color

Expand Down Expand Up @@ -138,14 +138,14 @@ def rgb_led(self):
return RGBLED(self.bus, self.device_address)

@property
def white_led(self):
def orange_led(self):
"""
Gets the white LED control interface.
Gets the orange LED control interface.

Returns:
WhiteLED: The white LED.
OrangeLED: The orange LED.
"""
return WhiteLED(self.bus, self.device_address)
return OrangeLED(self.bus, self.device_address)

def reset(self):
"""
Expand Down
67 changes: 67 additions & 0 deletions src/arduino_nicla_sense_env/orange_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from .i2c_device import I2CDevice
from .constants import REGISTERS

class OrangeLED(I2CDevice):
"""
This class allows to control the orange LED on the Nicla Sense Env board.
"""

@property
def brightness(self) -> int:
"""
Gets the brightness of the orange LED.

Returns
----
int: The brightness of the orange LED. Range is 0 to 255.
"""
# Read bits 0 - 5 from orange_led register
data = self._read_from_register(REGISTERS["orange_led"])
brightness = data & 63
# Map brightness from 0-63 to 0-255
return round((brightness * 255) / 63)

@brightness.setter
def brightness(self, led_brightness = 63) -> None:
"""
Sets the brightness of the orange LED.
Call store_settings_in_flash() on NiclaSenseEnv instance after changing the orange LED brightness to make the change persistent.

Parameters
----
led_brightness (int):
The brightness of the orange LED. Range is 0 to 63.
"""
if led_brightness < 0 or led_brightness > 255:
raise ValueError("Brightness must be between 0 and 255")

# Map brightness from 0-255 to 0-63
mapped_brightness = round((led_brightness * 63) / 255)
current_register_data = self._read_from_register(REGISTERS["orange_led"])
# Overwrite bits 0 - 5 with the new value by clearing the bits and then setting them
self._write_to_register(REGISTERS["orange_led"], (current_register_data & 0b11000000) | mapped_brightness)

@property
def error_status_enabled(self) -> bool:
"""
Determines whether the orange LED is used to indicate an error status of one of the sensors.
"""
# Read bit 7 from orange_led register
data = self._read_from_register(REGISTERS["orange_led"])
return bool(data & (1 << 7))

@error_status_enabled.setter
def error_status_enabled(self, enabled: bool) -> None:
"""
Enables or disables the orange LED to indicate an error status of one of the sensors.
Call store_settings_in_flash() on NiclaSenseEnv instance after enabling/disabling the orange LED error status to make the change persistent.

Parameters
----
enabled (bool):
Whether to enable or disable the orange LED error status.
"""
current_register_data = self._read_from_register(REGISTERS["orange_led"])
# Set bit 7 to 1 if enabled or 0 if disabled while keeping the other bits unchanged
# by clearing bit 7 and then setting it to the desired value
self._write_to_register(REGISTERS["orange_led"], current_register_data & 0b01111111 | (int(enabled) << 7))
62 changes: 0 additions & 62 deletions src/arduino_nicla_sense_env/white_led.py

This file was deleted.