X-Git-Url: http://git.code-monkey.de/?p=gps-watch.git;a=blobdiff_plain;f=src%2Fapplication%2Fmodel.rs;fp=src%2Fapplication%2Fmodel.rs;h=a056af5b1fcf41f1a06b18a5eeab0e2fabc837f5;hp=0000000000000000000000000000000000000000;hb=683135c35c05cd547f8a77b3602c2ddc78c1e887;hpb=70fb2fadd8688fdc11ba1d398661ac99aaa1b02a diff --git a/src/application/model.rs b/src/application/model.rs new file mode 100644 index 0000000..a056af5 --- /dev/null +++ b/src/application/model.rs @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +pub enum Field { + UnixTime(u32), + Distance(u32), +} + +pub struct Model { + pub unix_time: u32, + pub distance_cm: u32, + + is_dirty: u32, +} + +impl Model { + pub fn new() -> Model { + Model { + unix_time: 0, + distance_cm: 0, + is_dirty: 0, + } + } + + pub fn reset(&mut self) { + if self.distance_cm != 0 { + self.distance_cm = 0; + self.is_dirty |= Model::dirty_mask(Field::Distance(0)); + } + } + + pub fn update(&mut self, data: Field) { + match data { + Field::UnixTime(unix_time) => { + if self.unix_time != unix_time { + self.unix_time = unix_time; + self.is_dirty |= Model::dirty_mask(data); + } + }, + Field::Distance(distance_cm) => { + if self.distance_cm != distance_cm { + self.distance_cm = distance_cm; + self.is_dirty |= Model::dirty_mask(data); + } + }, + } + } + + pub fn check_and_reset_is_dirty(&mut self, data: Field) -> bool { + let mask = Model::dirty_mask(data); + + if (self.is_dirty & mask) == 0 { + false + } else { + self.is_dirty &= !mask; + + true + } + } + + fn dirty_mask(data: Field) -> u32 { + match data { + Field::UnixTime(_) => 1, + Field::Distance(_) => 2, + } + } +}