1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/* Copyright (C) 2023 Purism SPC
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
/*! Configuration management.
Fill in Select with fixed values.
Call camera::which(Select, Param) and get a list of possibilities for the chosen Param. Repeat until satisfied.
Choose one, and call camera::select(Select) to get a set of matching configs.
Choose one, and use it to configure the camera.
`which` can be used to fix config parameters one by one. `select` fills in all remaining parameters, but may not give an exhaustive list of possibilities.
*/
use std::collections::HashSet;
type Resolution = (u32, u32);
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
enum PixelFormat {
Bayer,
RGB,
}
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
struct Config {
resolution: Resolution,
pixel_format: PixelFormat,
}
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
struct Select {
resolution: Option<Resolution>,
pixel_format: Option<PixelFormat>,
}
enum Param {
Resolution,
PixelFormat,
}
#[derive(PartialEq)]
struct Response {
resolution: Vec<Resolution>,
}
#[cfg(test)]
mod test {
use super::*;
const OPTIONS: &[(Resolution, PixelFormat)] = &[
((640, 480), PixelFormat::Bayer),
((640, 480), PixelFormat::RGB),
((1024, 480), PixelFormat::Bayer),
];
fn check<T: PartialEq>(fix: &Option<T>, known: &T) -> bool {
fix.as_ref().map(|f| f == known).unwrap_or(true)
}
fn select(fixed: Select) -> HashSet<Config> {
OPTIONS.iter()
.filter(|(r, pf)| {
check(&fixed.resolution, r)
&& check(&fixed.pixel_format, pf)
})
.map(|(r, pf)| Config {
resolution: *r,
pixel_format: *pf,
})
.collect::<HashSet<_>>()
}
fn which(fixed: Select, wanted: Param) -> HashSet<Select> {
let matching = OPTIONS.iter()
.filter(|(r, pf)| {
check(&fixed.resolution, r)
&& check(&fixed.pixel_format, pf)
});
match wanted {
Param::Resolution => {
matching
.map(|(r, _)| Select {
resolution: Some(*r),
..fixed
})
.collect::<HashSet<_>>()
},
Param::PixelFormat => {
matching
.map(|(r, _)| Select {
resolution: Some(*r),
..fixed
})
.collect::<HashSet<_>>()
}
}
}
#[test]
fn q1() {
let base = Select {
resolution: None,
pixel_format: None,
};
let bases = which(base, Param::Resolution);
assert_eq!(
bases,
HashSet::from([
Select {
resolution: Some((640, 480)),
pixel_format: None,
},
Select {
resolution: Some((1024, 480)),
pixel_format: None,
},
]),
)
}
#[test]
fn q2() {
let base = Select {
resolution: None,
pixel_format: Some(PixelFormat::RGB),
};
let bases = which(base, Param::Resolution);
assert_eq!(
bases,
HashSet::from([
Select {
resolution: Some((640, 480)),
pixel_format: Some(PixelFormat::RGB),
},
]),
)
}
#[test]
fn s1() {
let base = Select {
resolution: None,
pixel_format: Some(PixelFormat::RGB),
};
let selected = select(base);
assert_eq!(
selected,
HashSet::from([
Config {
resolution: (640, 480),
pixel_format: PixelFormat::RGB,
},
]),
)
}
#[test]
fn s2() {
let base = Select {
resolution: Some((1024, 480)),
pixel_format: Some(PixelFormat::RGB),
};
let selected = select(base);
assert_eq!(
selected,
HashSet::from([]),
)
}
}