| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 3 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 4 | #include "sysfswrite.hpp" |
| 5 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 6 | #include <cstdint> |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 7 | #include <fstream> |
| 8 | #include <iostream> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 9 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 10 | namespace pid_control |
| 11 | { |
| 12 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 13 | void SysFsWritePercent::write(double value) |
| 14 | { |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 15 | double minimum = getMin(); |
| 16 | double maximum = getMax(); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 17 | |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 18 | double range = maximum - minimum; |
| 19 | double offset = range * value; |
| 20 | double ovalue = offset + minimum; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 21 | |
| 22 | std::ofstream ofs; |
| Patrick Venture | a1c5d37 | 2019-02-11 10:03:35 -0800 | [diff] [blame] | 23 | ofs.open(_writePath); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 24 | ofs << static_cast<int64_t>(ovalue); |
| 25 | ofs.close(); |
| 26 | |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | void SysFsWrite::write(double value) |
| 31 | { |
| 32 | std::ofstream ofs; |
| Patrick Venture | a1c5d37 | 2019-02-11 10:03:35 -0800 | [diff] [blame] | 33 | ofs.open(_writePath); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 34 | ofs << static_cast<int64_t>(value); |
| 35 | ofs.close(); |
| 36 | |
| 37 | return; |
| 38 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 39 | |
| 40 | } // namespace pid_control |