|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +-- |
| 4 | +-- Types.idr — Dependent-typed domain definitions for the Kaldor BBW board. |
| 5 | +-- |
| 6 | +-- Every type here carries a proof that its value is within the valid physical |
| 7 | +-- or protocol range. The Zig FFI layer (ffi/zig/src/) enforces the same |
| 8 | +-- bounds at runtime; these types make the contract explicit at compile time. |
| 9 | + |
| 10 | +module KaldorBBW.ABI.Types |
| 11 | + |
| 12 | +%default total |
| 13 | + |
| 14 | +-- -------------------------------------------------------------------------- |
| 15 | +-- GPIO |
| 16 | +-- -------------------------------------------------------------------------- |
| 17 | + |
| 18 | +||| ESP32 GPIO pin number, constrained to the Xtensa LX6 valid range 0–39. |
| 19 | +public export |
| 20 | +data GpioPin : Type where |
| 21 | + MkGpioPin : (n : Nat) -> {auto 0 prf : LTE n 39} -> GpioPin |
| 22 | + |
| 23 | +||| GPIO operating mode — mirrors the esp_idf @c gpio_mode_t enum. |
| 24 | +public export |
| 25 | +data GpioMode |
| 26 | + = GpioDisable -- 0 (no driver) |
| 27 | + | GpioInput -- 1 (input only) |
| 28 | + | GpioOutput -- 2 (push-pull output) |
| 29 | + | GpioOutputOD -- 6 (open-drain output) |
| 30 | + | GpioInputOutput -- 3 (bidirectional) |
| 31 | + |
| 32 | +-- -------------------------------------------------------------------------- |
| 33 | +-- MQTT |
| 34 | +-- -------------------------------------------------------------------------- |
| 35 | + |
| 36 | +||| MQTT QoS level. |
| 37 | +||| 0 = at most once, 1 = at least once, 2 = exactly once. |
| 38 | +public export |
| 39 | +data MqttQos = QosAtMostOnce | QosAtLeastOnce | QosExactlyOnce |
| 40 | + |
| 41 | +||| Encode QoS as the unsigned byte value expected on the wire. |
| 42 | +public export |
| 43 | +qosToU8 : MqttQos -> Bits8 |
| 44 | +qosToU8 QosAtMostOnce = 0 |
| 45 | +qosToU8 QosAtLeastOnce = 1 |
| 46 | +qosToU8 QosExactlyOnce = 2 |
| 47 | + |
| 48 | +||| A valid MQTT topic string (MQTT 3.1.1 §4.7). |
| 49 | +||| Non-empty, at most 256 bytes, no embedded NUL. |
| 50 | +public export |
| 51 | +record MqttTopic where |
| 52 | + constructor MkTopic |
| 53 | + bytes : List Bits8 |
| 54 | + nonEmpty : NonEmpty bytes |
| 55 | + bounded : LTE (length bytes) 256 |
| 56 | + |
| 57 | +-- -------------------------------------------------------------------------- |
| 58 | +-- OTA |
| 59 | +-- -------------------------------------------------------------------------- |
| 60 | + |
| 61 | +||| An HTTPS URL for firmware downloads. |
| 62 | +||| Non-empty, at most 8192 characters, prefixed with "https://". |
| 63 | +public export |
| 64 | +record OtaUrl where |
| 65 | + constructor MkOtaUrl |
| 66 | + chars : List Char |
| 67 | + nonEmpty : NonEmpty chars |
| 68 | + bounded : LTE (length chars) 8192 |
| 69 | + isHttps : isPrefixOf (unpack "https://") chars = True |
| 70 | + |
| 71 | +-- -------------------------------------------------------------------------- |
| 72 | +-- BBW sensor domain |
| 73 | +-- -------------------------------------------------------------------------- |
| 74 | + |
| 75 | +||| Back Beam Width measurement in millimetres. |
| 76 | +||| Physical operating range of the Kaldor loom: 10–500 mm (config.zig). |
| 77 | +public export |
| 78 | +record BbwMm where |
| 79 | + constructor MkBbwMm |
| 80 | + value : Double |
| 81 | + lower : value >= 10.0 = True |
| 82 | + upper : value <= 500.0 = True |
| 83 | + |
| 84 | +||| Temperature reading in degrees Celsius (DHT22 range: –40 to +80 °C). |
| 85 | +public export |
| 86 | +record TempC where |
| 87 | + constructor MkTempC |
| 88 | + value : Double |
| 89 | + lower : value >= -40.0 = True |
| 90 | + upper : value <= 80.0 = True |
| 91 | + |
| 92 | +||| Ultrasonic distance reading in millimetres (HC-SR04 range: 20–4000 mm). |
| 93 | +public export |
| 94 | +record DistanceMm where |
| 95 | + constructor MkDistanceMm |
| 96 | + value : Double |
| 97 | + lower : value >= 20.0 = True |
| 98 | + upper : value <= 4000.0 = True |
| 99 | + |
| 100 | +-- -------------------------------------------------------------------------- |
| 101 | +-- Convenience constructors with proof obligations |
| 102 | +-- -------------------------------------------------------------------------- |
| 103 | + |
| 104 | +||| Smart constructor: build a GpioPin if n <= 39, else Nothing. |
| 105 | +public export |
| 106 | +mkGpioPin : (n : Nat) -> Maybe GpioPin |
| 107 | +mkGpioPin n with (isLTE n 39) |
| 108 | + | Yes prf = Just (MkGpioPin n) |
| 109 | + | No _ = Nothing |
0 commit comments