/
one_wire.py
140 lines (103 loc) · 4.92 KB
/
one_wire.py
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
# -*- coding: utf-8 -*-
"""
One Wire Plugin
Copyright (C) 2018 Olaf Lüke <olaf@tinkerforge.com>
one_wire.py: 1-Wire Plugin Implementation
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 2
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, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
"""
import time
from PyQt5.QtWidgets import QTreeWidgetItem
from brickv.plugin_system.comcu_plugin_base import COMCUPluginBase
from brickv.bindings.bricklet_one_wire import BrickletOneWire
from brickv.plugin_system.plugins.one_wire.ui_one_wire import Ui_OneWire
from brickv.async_call import async_call
class OneWire(COMCUPluginBase, Ui_OneWire):
def __init__(self, *args):
COMCUPluginBase.__init__(self, BrickletOneWire, *args)
self.setupUi(self)
self.one_wire = self.device
self.button_reset.clicked.connect(self.button_reset_clicked)
self.button_search.clicked.connect(self.button_search_clicked)
self.button_read_byte.clicked.connect(self.button_read_byte_clicked)
self.button_write_byte.clicked.connect(self.button_write_byte_clicked)
self.button_write_command.clicked.connect(self.button_write_command_clicked)
self.start_time = time.time()
self.ids = [0]
def get_status_text(self, status):
if status == 0:
return 'OK'
elif status == 1:
return 'Busy'
elif status == 2:
return 'No Presence'
elif status == 3:
return 'Timeout'
elif status == 4:
return 'Error'
else:
return 'Unknown'
def update_id_list(self):
self.combo_box_write_command.clear()
self.combo_box_write_command.addItem('Skip ID')
for i in self.ids[1:]:
self.combo_box_write_command.addItem('Match ID {0:X}'.format((i >> 8) & 0xFFFFFFFFFFFF))
def add_to_list(self, command, value, status):
self.tree_widget.insertTopLevelItem(0, QTreeWidgetItem([str(round(time.time() - self.start_time, 2)), command, value, self.get_status_text(status)]))
def reset_bus_async(self, status):
self.add_to_list('Reset Bus', '-', status)
def button_reset_clicked(self):
async_call(self.one_wire.reset_bus, None, self.reset_bus_async, self.increase_error_count)
def search_bus_async(self, data):
if not data.identifier:
self.add_to_list('Search Bus', '-', data.status)
return
self.ids = [0]
i = 0
for identifier in data.identifier:
self.ids.append(identifier)
s = 'Family: {0:X}, ID: {1:X}, CRC: {2:X}'.format(identifier & 0xFF, (identifier >> 8) & 0xFFFFFFFFFFFF, identifier >> 56)
if len(data.identifier) == 1:
self.add_to_list('Search Bus', s, data.status)
else:
self.add_to_list('Search Bus ({0})'.format(i), s, data.status)
i += 1
self.update_id_list()
def button_search_clicked(self):
async_call(self.one_wire.search_bus, None, self.search_bus_async, self.increase_error_count)
def read_byte_async(self, data):
self.add_to_list('Read Byte', '{0:X}'.format(data.data), data.status)
def button_read_byte_clicked(self):
async_call(self.one_wire.read, None, self.read_byte_async, self.increase_error_count)
def write_byte_async(self, data, status):
self.add_to_list('Write Byte', '{0:X}'.format(data), status)
def button_write_byte_clicked(self):
data = self.spinbox_write_byte.value()
async_call(self.one_wire.write, data, self.write_byte_async, self.increase_error_count,
pass_arguments_to_result_callback=True)
def write_command_async(self, id_, command, status):
self.add_to_list('Write Byte', '{0:X} to ID {1:X}'.format(command, (id_ >> 8) & 0xFFFFFFFFFFFF), status)
def button_write_command_clicked(self):
id_ = self.ids[self.combo_box_write_command.currentIndex()]
command = self.spinbox_write_command.value()
async_call(self.one_wire.write_command, (id_, command), self.write_command_async, self.increase_error_count,
pass_arguments_to_result_callback=True, expand_arguments_tuple_for_callback=True)
def start(self):
pass
def stop(self):
pass
def destroy(self):
pass
@staticmethod
def has_device_identifier(device_identifier):
return device_identifier == BrickletOneWire.DEVICE_IDENTIFIER