summaryrefslogtreecommitdiff
path: root/tmp117-cli/src/main.rs
blob: 0be782adff6040033f1903d09eca2cb90499dbb8 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use clap::{Parser, Subcommand};
use linux_embedded_hal::{Delay, I2cdev};
use log::{info, trace};
use tmp117::client::TMP117;
use tmp117::conf::{
    AlertPinSelect, Configuration, ConversionAveragingMode, ConversionMode, Mode, PinPolarity,
};

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
    /// I2C device path
    #[arg(short('p'), long, default_value = "/dev/i2c-0")]
    i2c_device_path: String,

    //// Temperature sensor address on I2C bus
    #[arg(short('a'), long, default_value_t = 0x57)]
    slave_address: u8,

    //// The command to run
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum TemperatureCommands {
    /// Get the temperature from the sensor as raw value or in celsius
    Get {
        /// Get the temperature in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },
}

#[derive(Subcommand)]
enum ConfigureCommands {
    /// Set the configuration register using raw value
    Set {
        /// Configuration value
        #[arg(short, long)]
        configuration: u16,
    },

    /// Set the configuration register using named fields
    SetTyped {
        /// If true then the ALERT pin reflects the status of the data ready flag otherwise it reflects the status of the alert flags
        #[arg(short('d'), long, default_value_t = false)]
        alert_pin_reflects_data_ready: bool,

        /// If true then the ALERT pin is active high otherwise it is active low
        #[arg(short('p'), long, default_value_t = false)]
        alert_pin_active_high: bool,

        /// If true then the sensor will operate in thermal mode otherwise it is in alert mode
        #[arg(short, long, default_value_t = false)]
        thermal_mode: bool,

        /// The number of conversion results that are collected and averaged (accumulative, not running) before updating temperature register
        #[arg(short('c'), long, default_value_t = 8)]
        averaged_conversions: u8,
    },

    /// Get the configuration from the sensor as raw value
    Get {},

    /// Get the configuration from the sensor as named fields
    GetFields {},
}

#[derive(Subcommand)]
enum HighLimitCommands {
    /// Set the high limit as raw value or in celsius
    Set {
        /// 16-bit high limit for comparison with the temperature result. One LSB equals 7.8125 m°C. The range of the register is ±256 °C. Negative numbers are represented in binary two's complement format. Following power-up or a general-call reset, the high-limit register is loaded with the stored value from the EEPROM. The factory default reset value is 6000h.
        #[arg(short, long)]
        limit: f32,

        /// The provided limit is temperature in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },

    /// Get the high limit temperature as raw value or in celsius
    Get {
        /// Get the high limit in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },
}

#[derive(Subcommand)]
enum LowLimitCommands {
    /// Set the low limit as raw value or in celsius
    Set {
        /// 16-bit low limit for comparison with the temperature result. One LSB equals 7.8125 m°C. The range of the register is ±256 °C. Negative numbers are represented in binary two's complement format. Following power-up or a general-call reset, the high-limit register is loaded with the stored value from the EEPROM. The factory default reset value is 8000h.
        #[arg(short, long)]
        limit: f32,

        /// The provided limit is temperature in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },

    /// Get the low limit temperature as raw value or in celsius
    Get {
        /// Get the low limit in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },
}

#[derive(Subcommand)]
enum OffsetCommands {
    /// Set the temperature offset as raw value or in celsius
    Set {
        /// Offset will be added to temperature result after linearization. It has a same resolution of 7.8125 m°C and same range of ±256 °C as the temperature result register. The data format is the same as the temperature register. If the added result is out of boundary, then the temperature result will show as the maximum or minimum value.
        #[arg(short, long)]
        offset: f32,

        /// The provided offset is temperature in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },

    /// Get the temperature offset as raw value or in celsius
    Get {
        /// Get the temperature offset in celsius
        #[arg(short, long, default_value_t = false)]
        celsius: bool,
    },
}

#[derive(Subcommand)]
enum Commands {
    /// Interact with temperature register
    Temperature {
        #[command(subcommand)]
        command: TemperatureCommands,
    },

    /// Interact with configuration register
    Configure {
        #[command(subcommand)]
        command: ConfigureCommands,
    },

    /// Interact with high limit register
    HighLimit {
        #[command(subcommand)]
        command: HighLimitCommands,
    },

    /// Interact with low limit register
    LowLimit {
        #[command(subcommand)]
        command: LowLimitCommands,
    },

    /// Interact with temperature offset register
    Offset {
        #[command(subcommand)]
        command: OffsetCommands,
    },

    /// Reset the sensor
    Reset {},
}

fn main() {
    env_logger::init();

    let cli = Args::parse();

    trace!("I2C device path: {}", &cli.i2c_device_path);
    trace!("I2C slave address: {:#04x}", cli.slave_address);

    match &cli.command {
        Some(Commands::Temperature { command }) => match command {
            TemperatureCommands::Get { celsius } => {
                info!("Getting temperature...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    let temperature = tmp117.get_temperature_celsius().unwrap();
                    println!("{:.4} °C", temperature);
                } else {
                    let temperature = tmp117.get_temperature().unwrap();
                    println!("{:#06x}", temperature);
                }
            }
        },
        Some(Commands::Configure { command }) => match command {
            ConfigureCommands::Set { configuration } => {
                info!("Setting configuration...");
                trace!("Configuration: {:#06x}", configuration);

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                tmp117.configure(*configuration).unwrap();
            }
            ConfigureCommands::SetTyped {
                alert_pin_reflects_data_ready,
                alert_pin_active_high,
                thermal_mode,
                averaged_conversions,
            } => {
                info!("Setting configuration...");
                trace!(
                    "Alert pin reflects data ready? {}",
                    alert_pin_reflects_data_ready
                );
                trace!("Alert pin active high? {}", alert_pin_active_high);
                trace!("Thermal mode enabled? {}", thermal_mode);
                trace!("Averaged conversions: {}", averaged_conversions);

                let configuration = Configuration {
                    alert_pin_select: match alert_pin_reflects_data_ready {
                        false => AlertPinSelect::Alert,
                        true => AlertPinSelect::DataReady,
                    },
                    alert_pin_polarity: match alert_pin_active_high {
                        false => PinPolarity::ActiveLow,
                        true => PinPolarity::ActiveHigh,
                    },
                    mode: match thermal_mode {
                        false => Mode::Alert,
                        true => Mode::Thermal,
                    },
                    conversion_averaging_mode: match averaged_conversions {
                        0 => ConversionAveragingMode::None,
                        8 => ConversionAveragingMode::Avg8,
                        32 => ConversionAveragingMode::Avg32,
                        64 => ConversionAveragingMode::Avg64,
                        _ => todo!("Handle invalid averaging value"),
                    },
                    conversion_mode: ConversionMode::Continuous,
                };

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                tmp117.configure_typed(configuration).unwrap();
            }
            ConfigureCommands::Get {} => {
                info!("Getting configuration...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                let configuration = tmp117.get_configuration().unwrap();
                println!("{:#06x}", configuration)
            }
            ConfigureCommands::GetFields {} => {
                info!("Getting configuration...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                let configuration = tmp117.get_configuration_typed().unwrap();
                println!("{}", configuration);
            }
        },
        Some(Commands::HighLimit { command }) => match command {
            HighLimitCommands::Set { limit, celsius } => {
                info!("Setting high limit...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    trace!("Limit (°C): {:.4}", *limit);
                    tmp117.configure_high_limit_celsius(*limit).unwrap();
                } else {
                    trace!("Limit: {:#06x}", (*limit as i16));
                    tmp117.configure_high_limit(*limit as i16).unwrap();
                }
            }
            HighLimitCommands::Get { celsius } => {
                info!("Getting high limit...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    let limit = tmp117.get_high_limit_celsius().unwrap();
                    println!("{:.4} °C", limit);
                } else {
                    let limit = tmp117.get_high_limit().unwrap();
                    println!("{:#06x}", limit);
                }
            }
        },
        Some(Commands::LowLimit { command }) => match command {
            LowLimitCommands::Set { limit, celsius } => {
                info!("Setting low limit...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    trace!("Limit (°C): {:.4}", *limit);
                    tmp117.configure_low_limit_celsius(*limit).unwrap();
                } else {
                    trace!("Limit: {:#06x}", (*limit as i16));
                    tmp117.configure_low_limit(*limit as i16).unwrap();
                }
            }
            LowLimitCommands::Get { celsius } => {
                info!("Getting low limit...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    let limit = tmp117.get_low_limit_celsius().unwrap();
                    println!("{:.4} °C", limit);
                } else {
                    let limit = tmp117.get_low_limit().unwrap();
                    println!("{:#06x}", limit);
                }
            }
        },
        Some(Commands::Offset { command }) => match command {
            OffsetCommands::Set { offset, celsius } => {
                info!("Setting temperature offset...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    trace!("Offset (°C): {:.4}", *offset);
                    tmp117
                        .configure_offset_temperature_celsius(*offset)
                        .unwrap();
                } else {
                    trace!("Offset: {:#06x}", (*offset as i16));
                    tmp117.configure_offset_temperature(*offset as i16).unwrap();
                }
            }
            OffsetCommands::Get { celsius } => {
                info!("Getting temperature offset...");

                let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
                let delay = Delay;
                let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);

                if *celsius {
                    let offset = tmp117.get_offset_temperature_celsius().unwrap();
                    println!("{:.4} °C", offset);
                } else {
                    let offset = tmp117.get_offset_temperature().unwrap();
                    println!("{:#06x}", offset);
                }
            }
        },
        Some(Commands::Reset {}) => {
            info!("Resetting TI TMP117...");

            let i2c = I2cdev::new(&cli.i2c_device_path).unwrap();
            let delay = Delay;
            let mut tmp117 = TMP117::new(cli.slave_address, i2c, delay);
            tmp117.software_reset().unwrap();
        }
        None => {}
    }
}