Devesh36/usb-c-aqi-meter

A PETG-enclosed air-quality monitor assembly combining an AQI circuit board, Sensirion SPS30 particulate sensor, OLED display with bezel and glass, power slider, branding, and internal wiring harness.

Version
1.2.0
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone Devesh36/usb-c-aqi-meter  # download the package files into ./usb-c-aqi-meter
tsci add Devesh36/usb-c-aqi-meter    # add it to your project as @tsci/Devesh36.usb-c-aqi-meter

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/test_protocol.py

"""Host tests: python -m unittest discover -s firmware -p 'test_*.py'."""
import struct
import unittest
from protocol import crc8, shdlc_request, shdlc_response, particle_values, indicative_aqi


class ProtocolTests(unittest.TestCase):
    def test_manufacturer_start_measurement_example(self):
        self.assertEqual(shdlc_request(0, b"\x01\x03"), bytes.fromhex("7e0000020103f97e"))
        self.assertEqual(shdlc_response(bytes.fromhex("7e00000000ff7e"), 0), b"")

    def test_published_sht4x_crc_example(self):
        self.assertEqual(crc8(bytes.fromhex("beef")), 0x92)

    def test_faults_do_not_produce_measurements(self):
        for frame in ("7e00030000fb7e", "7e000380007c7e", "7e00030300f97e"):
            with self.assertRaises(ValueError):
                shdlc_response(bytes.fromhex(frame), 3)
        self.assertIsNone(particle_values(b""))
        with self.assertRaises(ValueError):
            particle_values(struct.pack(">10f", *([float("nan")] * 10)))

    def test_byte_stuffing_and_frame_length(self):
        # Valid data with all four reserved bytes, independently framed response.
        frame = bytes.fromhex("7e000300047d5e7d5d7d317d33d97e")
        self.assertEqual(shdlc_response(frame, 3), bytes.fromhex("7e7d1113"))
        with self.assertRaises(ValueError):
            shdlc_response(frame[:-1], 3)

    def test_pm_fields_follow_datasheet_order(self):
        fields = particle_values(struct.pack(">10f", *range(1, 11)))
        self.assertEqual(fields[1], 2.0)
        self.assertEqual(fields[3], 4.0)

    def test_2024_epa_boundaries_and_truncation(self):
        for c, expected in ((0,0),(9,50),(9.1,51),(35.4,100),(35.5,101),
                            (55.4,150),(55.5,151),(125.4,200),(125.5,201),
                            (225.4,300),(225.5,301),(325.4,500),(325.5,501)):
            self.assertEqual(indicative_aqi(c), expected)
        self.assertEqual(indicative_aqi(9.09), 50)
        for c in (-1, float("nan"), float("inf")):
            with self.assertRaises(ValueError):
                indicative_aqi(c)


if __name__ == "__main__":
    unittest.main()