2 * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 pub fn new() -> Model {
45 pub fn reset(&mut self) {
46 if self.distance_cm != 0 {
48 self.is_dirty |= Model::dirty_mask(Field::Distance(0));
52 pub fn update(&mut self, data: Field) {
54 Field::UnixTime(unix_time) => {
55 if self.unix_time != unix_time {
56 self.unix_time = unix_time;
57 self.is_dirty |= Model::dirty_mask(data);
60 Field::Distance(distance_cm) => {
61 if self.distance_cm != distance_cm {
62 self.distance_cm = distance_cm;
63 self.is_dirty |= Model::dirty_mask(data);
69 pub fn check_and_reset_is_dirty(&mut self, data: Field) -> bool {
70 let mask = Model::dirty_mask(data);
72 if (self.is_dirty & mask) == 0 {
75 self.is_dirty &= !mask;
81 fn dirty_mask(data: Field) -> u32 {
83 Field::UnixTime(_) => 1,
84 Field::Distance(_) => 2,