-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHT3x.flowext.ts
More file actions
88 lines (82 loc) · 3.5 KB
/
Copy pathSHT3x.flowext.ts
File metadata and controls
88 lines (82 loc) · 3.5 KB
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
import type { BlockCategory, CodeGenContext } from '../types.js';
function SHT3xRegistor(
registerPreprocessor: CodeGenContext['registerPreprocessor'],
registerGlobal: CodeGenContext['registerGlobal'],
registerFunction: CodeGenContext['registerFunction']
) {
registerPreprocessor('#include <Wire.h>');
registerPreprocessor('#include <ArtronShop_SHT3x.h>');
registerGlobal('ArtronShop_SHT3x sht3x(0x44, &Wire); // ADDR: 0 => 0x44, ADDR: 1 => 0x45');
registerFunction(
'bool sht3x_read(float * t, float * h)',
' static bool init = false;\n' +
' if (!init) {\n' +
' Wire.begin();\n' +
' if (!sht3x.begin()) {\n' +
' return false;\n' +
' }\n' +
' delay(50);\n' +
' init = true;\n' +
' }\n' +
'\n' +
' static uint32_t last_measure = 0;\n' +
' if ((last_measure == 0) || ((millis() - last_measure) >= (100)) || (millis() < last_measure)) {\n' +
' last_measure = millis();\n' +
' if (!sht3x.measure()) {\n' +
' init = false;\n' +
' return false;\n' +
' }\n' +
' }\n' +
'\n' +
' if (t) {\n' +
' *t = sht3x.temperature();\n' +
' }\n' +
' if (h) {\n' +
' *h = sht3x.humidity();\n' +
' }\n' +
'\n' +
' return true;',
'bool sht3x_read(float * t, float * h) ;'
);
}
const sht3xExtension: BlockCategory = {
id: 'sht3x-read',
name: 'SHT3x',
blocks: [
{
id: 'sht3x-read-temp',
name: 'SHT3x Read',
color: '#3b82f6',
icon: '🌡️',
category: 'SHT3x',
description: 'อ่านค่าอุณหภูมิและความชื้นจากเซ็นเซอร์ SHT3x',
inputs: [{ id: 'in', type: 'input', label: '➜', dataType: 'any', description: 'จุดต่อสายบล็อกก่อนหน้า' }],
outputs: [
{ id: 'value', type: 'output', label: 'Value', dataType: 'float', description: 'ค่าที่ได้จากเซ็นเซอร์' },
{ id: 'error', type: 'output', label: 'Error', dataType: 'void', description: 'บล็อกที่ต้องการให้ทำงานเมื่ออ่านค่าไม่ได้' }
],
params: [
{ id: 'value_type', label: 'Value', description: 'ค่าที่ต้องการอ่าน (อุณหภูมิ / ความชื้น)', type: 'option', options: [
{ label: 'Temperature (°C)', value: 't' },
{ label: 'Humidity (%RH)', value: 'h' },
]}
],
toCode({ pad, block, registerPreprocessor, registerGlobal, registerFunction, safeId, params }) {
SHT3xRegistor(registerPreprocessor, registerGlobal, registerFunction);
const value_type = params.value_type ?? 't';
const id = safeId(block.id);
return {
parts: [
[`${pad}float ${id} = 0;`],
[`${pad}if (sht3x_read(${value_type === 't' ? `&${id}, NULL` : `NULL, &${id}`})) {`],
{ portId: 'value', depthDelta: 1 },
[`${pad}} else {`],
{ portId: 'error', depthDelta: 1 },
[`${pad}}`]
]
};
}
},
]
}
export default sht3xExtension;