-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.temperature.tmp3x.spin2
121 lines (86 loc) · 3.57 KB
/
sensor.temperature.tmp3x.spin2
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
{
----------------------------------------------------------------------------------------------------
Filename: sensor.temperature.tmp3x.spin2
Description: Driver for the Analog Devices TMP3x-series temperature sensors
Author: Jesse Burt
Started: Jun 30, 2023
Updated: Sep 19, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
Requirements:
* an ADC driver with the following interfaces:
voltage() (ADC word scaled to microvolts)
Usage:
start() _must_ first be called from the parent object/application, with a pointer
to the chosen ADC driver's OBJ symbol. That is how this driver is "attached"
to the ADC.
Example 'application.spin':
#define TMP3X_ADC "signal.adc.mcp320x"
#pragma exportdef(TMP3X_ADC)
OBJ
adc: TMP3X_ADC | CS=0, SCK=1, MOSI=2, MISO=3
temp: "sensor.temperature.tmp3x"
PUB main()
adc.start()
temp.init(@adc) ' point to the adc object
The preprocessor symbol TMP3X_ADC must be set to the filename of your
chosen ADC driver, _inside of escaped quotes_.
}
#ifndef TMP3X_ADC
# define TMP3X_ADC "signal.adc.mcp320x"
#endif
obj
adc= TMP3X_ADC ' "virtual" instance of ADC object
var
long _instance
byte _averages
PUB start(adc_driver)
' Initialize the driver
' adc_driver: address of the chosen ADC driver
attach_adc_driver(adc_driver)
_averages := 1
pub stop()
' Stop the driver
' Clear out variable space
_instance := _averages := 0
pub bind = attach_adc_driver
pub attach = attach_adc_driver
pub attach_adc_driver(optr)
' Attach to an ADC driver
_instance := optr
pub set_sample_averages(c)
' Set number of samples to average
_averages := c
pub temp_data(): w | tmp
' Temperature data ADC word
' Returns: ADC word (voltage)
tmp := 0
repeat _averages
tmp += adc[_instance].voltage()
w := (tmp / _averages)
pub temp_word2deg(w): temp
' Convert ADC word to hundredths of a degree Celsius
' Returns: temperature, in hundredths of a degree, in chosen scale
temp := (w - 50_0000) / 100
case _temp_scale
C:
return temp
F:
return ((temp * 9) / 5) + 32_00
#include "sensor.temp.common.spin2h" ' use code common to all temp sensors
DAT
{
Copyright 2024 Jesse Burt
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.
}