open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

commit dea82f7c001e59fc33aaaab032ada2212a14ec2e
parent 89af9c0ce8e13b1df10b7bb5bd49c40757a7c9c9
Author: Jean Schurger <jean@schurger.org>
Date:   Fri, 13 Nov 2026 14:05:46 -0500

0.2: pump control with automatic stops, deliberate disconnection

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYGSNhtY1AmBH7DQGNs

Diffstat:
MOpenMerou/Housing/HousingClient.swift | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
MOpenMerou/UI/HousingView.swift | 27+++++++++++++++++++++++++++
MREADME.md | 4++--
Mdocs/PROTOCOLE.md | 4+++-
Mfastlane/Fastfile | 4++--
Mproject.yml | 2+-
6 files changed, 111 insertions(+), 10 deletions(-)

diff --git a/OpenMerou/Housing/HousingClient.swift b/OpenMerou/Housing/HousingClient.swift @@ -1,5 +1,5 @@ -// Connexion Bluetooth au boîtier, en LECTURE SEULE pour ce prototype : aucune écriture, -// à part l'abonnement aux notifications. La commande de la pompe viendra après. +// Connexion Bluetooth au boîtier. Seule écriture : la pompe (1624), avec arrêt automatique +// au seuil de vide ou après un délai, comme le fait l'app officielle (docs/PROTOCOLE.md). @preconcurrency import CoreBluetooth import Foundation import Observation @@ -39,6 +39,63 @@ final class HousingClient: NSObject { @ObservationIgnored private var central: CBCentralManager! @ObservationIgnored private var peripheral: CBPeripheral? + @ObservationIgnored private var pumpCharacteristic: CBCharacteristic? + @ObservationIgnored private var pumpTimeout: Task<Void, Never>? + + /// Vrai entre notre ordre de marche et notre ordre d'arrêt. + private(set) var pumpRequested = false + var canPump: Bool { pumpCharacteristic != nil } + + /// Seuil d'arrêt observé avec l'app officielle : premier échantillon sous 80 kPa. + static let vacuumTargetPa = 80_000.0 + /// Garde-fou : l'app officielle pompe environ 10 s. + static let pumpMaxSeconds = 15.0 + + /// Vrai quand l'usager a demandé la déconnexion : on ne se reconnecte pas tout seul. + private(set) var released = false + + /// Dans la capture, l'app officielle n'envoie aucun ordre d'extinction : elle coupe la connexion + /// (HCI 0x16) et le boîtier s'éteint. On fait pareil. + func release() { + guard let peripheral else { return } + if pumpRequested { stopPump(reason: "déconnexion") } + released = true + central.stopScan() + central.cancelPeripheralConnection(peripheral) + note("Déconnexion demandée") + } + + func reconnect() { + released = false + peripheral = nil + startScan() + } + + func startPump() { + guard let peripheral, let pumpCharacteristic, !pumpRequested else { return } + if let p = readings.internalPressurePa, p < Self.vacuumTargetPa { + note("Pompe : vide déjà atteint (\(Int(p)) Pa)") + return + } + pumpRequested = true + peripheral.writeValue(Data([0x01]), for: pumpCharacteristic, type: .withResponse) + note("Pompe : marche demandée") + pumpTimeout = Task { [weak self] in + try? await Task.sleep(for: .seconds(Self.pumpMaxSeconds)) + guard !Task.isCancelled else { return } + self?.stopPump(reason: "délai de \(Int(Self.pumpMaxSeconds)) s") + } + } + + func stopPump(reason: String) { + pumpTimeout?.cancel() + pumpTimeout = nil + guard let peripheral, let pumpCharacteristic else { pumpRequested = false; return } + // Toujours envoyer l'arrêt, même si on ne pense pas l'avoir démarrée. + peripheral.writeValue(Data([0x00]), for: pumpCharacteristic, type: .withResponse) + pumpRequested = false + note("Pompe : arrêt demandé (\(reason))") + } override init() { super.init() @@ -90,6 +147,7 @@ extension HousingClient: CBCentralManagerDelegate { let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? peripheral.name guard name == HousingUUID.advertisedName else { return } MainActor.assumeIsolated { + guard !released else { return } note("Boîtier trouvé (\(RSSI) dBm)") connect(peripheral) } @@ -115,10 +173,13 @@ extension HousingClient: CBCentralManagerDelegate { error: Error?) { MainActor.assumeIsolated { let why = error?.localizedDescription ?? "fin de connexion" + pumpCharacteristic = nil + pumpTimeout?.cancel() + pumpRequested = false status = .disconnected(why) note("Déconnecté : \(why)") // Une demande de connexion en attente n'expire pas : le boîtier sera repris dès son retour. - central.connect(peripheral) + if !released { central.connect(peripheral) } } } } @@ -136,7 +197,7 @@ extension HousingClient: CBPeripheralDelegate { error: Error?) { MainActor.assumeIsolated { for characteristic in service.characteristics ?? [] { - // Lecture seule : on ne fait que lire et s'abonner. + if characteristic.uuid == HousingUUID.pump { pumpCharacteristic = characteristic } if characteristic.properties.contains(.notify) { peripheral.setNotifyValue(true, for: characteristic) } @@ -147,6 +208,13 @@ extension HousingClient: CBPeripheralDelegate { } } + nonisolated func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, + error: Error?) { + guard let error else { return } + let text = error.localizedDescription + MainActor.assumeIsolated { note("Écriture refusée : \(text)") } + } + nonisolated func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { guard let data = characteristic.value else { return } @@ -157,6 +225,10 @@ extension HousingClient: CBPeripheralDelegate { return } if let line = readings.apply(uuid, data) { note(line) } + if uuid == HousingUUID.internalPressure, pumpRequested, + let p = readings.internalPressurePa, p < Self.vacuumTargetPa { + stopPump(reason: "vide atteint, \(Int(p)) Pa") + } if [HousingUUID.internalPressure, HousingUUID.externalPressure, HousingUUID.temperature].contains(uuid) { lastSensorUpdate = .now } diff --git a/OpenMerou/UI/HousingView.swift b/OpenMerou/UI/HousingView.swift @@ -3,6 +3,7 @@ import SwiftUI /// Écran de diagnostic du prototype : tout ce que le boîtier envoie, en direct. struct HousingView: View { let housing: HousingClient + @Environment(\.scenePhase) private var scenePhase var body: some View { NavigationStack { @@ -28,6 +29,29 @@ struct HousingView: View { row("1628 (alarme ?)", housing.readings.alarmRaw.map { String(format: "%02X", $0) }) } + Section { + if housing.pumpRequested { + Button("Arrêter la pompe", role: .destructive) { housing.stopPump(reason: "bouton") } + } else { + Button("Démarrer la pompe") { housing.startPump() } + .disabled(!housing.canPump) + } + } header: { + Text("Pompe à vide") + } footer: { + Text("Arrêt automatique sous 800 hPa ou après 15 s. Boîtier ouvert, la pompe tourne sans faire le vide.") + } + + Section { + if housing.released { + Button("Se reconnecter") { housing.reconnect() } + } else { + Button("Se déconnecter du boîtier", role: .destructive) { housing.release() } + } + } footer: { + Text("L'app officielle n'envoie pas d'ordre d'extinction : elle se déconnecte. À vérifier : le boîtier s'éteint-il alors tout seul ?") + } + if !housing.deviceInfo.isEmpty { Section("Appareil") { ForEach(housing.deviceInfo.keys.sorted(), id: \.self) { key in @@ -49,6 +73,9 @@ struct HousingView: View { } } .navigationTitle("Open Mérou") + .onChange(of: scenePhase) { _, phase in + if phase != .active, housing.pumpRequested { housing.stopPump(reason: "app en arrière-plan") } + } } } diff --git a/README.md b/README.md @@ -9,8 +9,8 @@ Ce projet n'est ni affilié à Huish Outdoors ni approuvé par eux ; « Oceanic ## État -Prototype 0.1, **lecture seule** : connexion automatique au boîtier (« HUISH »), boutons, batterie, -pressions, température, état de la pompe, journal. Aucune écriture vers le boîtier. +Prototype 0.2 : connexion automatique au boîtier (« HUISH »), boutons, batterie, pressions, température, +état de la pompe, journal ; commande de la pompe (arrêt automatique sous 80 kPa ou après 15 s) et déconnexion. ## Développement diff --git a/docs/PROTOCOLE.md b/docs/PROTOCOLE.md @@ -64,7 +64,9 @@ Le boîtier ne fait qu'annoncer les appuis : tout le comportement (photo, menus, 5. Ouverture (côté app, guidée) : appui long droite (0x31) = lancer la séquence d'ouverture, puis appuis droite (0x30) = confirmations d'étapes (ouvrir le bouchon, purge, fermer le bouchon). 12:59:40 retour à 99,3 kPa quand le bouchon est ouvert : mise à l'air mécanique, aucune écriture BLE. -6. Appui long droite hors séquence = extinction du boîtier. +6. Extinction : aucun ordre BLE. Après la dernière confirmation (12:59:53), plus aucun bouton notifié ; + à 13:00:27 l'iPhone coupe la connexion (HCI 0x16, hôte local). Hypothèse : le boîtier s'éteint seul + une fois déconnecté (à vérifier avec le bouton « Se déconnecter » d'Open Mérou 0.2). ## À faire diff --git a/fastlane/Fastfile b/fastlane/Fastfile @@ -122,9 +122,9 @@ platform :ios do end texts = { "fr-CA" => ["App libre et gratuite pour boîtier de plongée compatible : boutons, pompe à vide, capteurs, caméra.", - "Prototype 0.1, lecture seule : fermez l'app officielle, allumez le boîtier, ouvrez Open Mérou. Vérifiez la connexion, chaque bouton, la batterie, les pressions et la température, puis la baisse de pression pendant un cycle de vide lancé depuis l'app officielle."], + "0.2 : bouton de pompe (arrêt automatique sous 800 hPa ou après 15 s) et déconnexion du boîtier. Vérifiez les boutons, la batterie, les pressions pendant le pompage, et si le boîtier s'éteint après « Se déconnecter »."], "en-CA" => ["Free, open-source app for compatible dive housings: buttons, vacuum pump, sensors, camera.", - "Prototype 0.1, read-only: close the official app, turn the housing on, open Open Mérou. Check the connection, each button, battery, pressures and temperature, then the pressure drop during a vacuum cycle started from the official app."], + "0.2: pump button (auto stop below 800 hPa or after 15 s) and housing disconnect. Check the buttons, battery, pressures while pumping, and whether the housing turns off after "Disconnect"."], } _, locs = asc(:get, "/v1/apps/#{app.id}/betaAppLocalizations") texts.each do |locale, (description, _)| diff --git a/project.yml b/project.yml @@ -9,7 +9,7 @@ settings: base: DEVELOPMENT_TEAM: CM5RQKJH56 CODE_SIGN_STYLE: Automatic - MARKETING_VERSION: "0.1" + MARKETING_VERSION: "0.2" CURRENT_PROJECT_VERSION: "1" SWIFT_VERSION: "6.0" targets: