/
pigpio-seatalk.js
94 lines (82 loc) · 3.79 KB
/
pigpio-seatalk.js
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
/*
*
* prototype-server: An implementation of a Signal K server for boats.
* Copyright (C) 2020 Teppo Kurki <teppo.kurki@iki.fi> *et al*.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 2020-06-24 Original Python code from @Thomas-GeDaD https://github.com/Thomas-GeDaD/Seatalk1-Raspi-reader
* and finetuned by @MatsA
*
*/
const Execute = require('./execute')
const cmd = `
import pigpio, time, signal, sys
if sys.argv[1] == "undefined":
gpio = 4 #Default GPIO4 if not set
else:
#Ggpio, info as "GPIOnn", from GUI setup. Sensing the seatalk1 (yellow wire)
try:
gpio = int(filter(str.isdigit, sys.argv[1])) #python2
except:
gpio = int("".join(filter(str.isdigit, sys.argv[1]))) #python3
if __name__ == "__main__":
st1read =pigpio.pi()
try:
st1read.bb_serial_read_close(gpio) #close if already run
except:
pass
st1read.bb_serial_read_open(gpio, 4800,9)
if sys.argv[2] == "true": # Invert, inverted input from ST1, selected in the GUI
st1read.bb_serial_invert(gpio, 1)
data=""
try:
while True:
out=(st1read.bb_serial_read(gpio))
out0=out[0]
if out0>0:
out_data=out[1]
x=0
while x < out0:
if out_data[x+1] ==0:
if out_data[x] > 15:
string1=str(hex(out_data[x]))
elif out_data[x] ==0:
string1="0x00"
else:
string1="0x0"+str(hex(out_data[x]).lstrip("0x"))
data= data+string1[2:]+ ","
else:
data=data[0:-1]
data="$STALK,"+data
print (data)
string2=str(hex(out_data[x]))
string2_new=string2[2:]
if len(string2_new)==1:
string2_new="0"+string2_new
data=string2_new + ","
x+=2
time.sleep(0.01)
except:
st1read.bb_serial_read_close(gpio)
print ("exit")
`
function PigpioSeatalk(options) {
const createDebug = options.createDebug || require('debug')
Execute.call(this, { debug: createDebug('signalk:streams:pigpio-seatalk') })
this.options = options
this.options.command = `python -u -c '${cmd}' ${options.gpio} ${options.gpioInvert} `
}
require('util').inherits(PigpioSeatalk, Execute)
module.exports = PigpioSeatalk