-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf022.py
executable file
·115 lines (94 loc) · 2.85 KB
/
rdf022.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
# -*- coding: utf-8 -*-
import re
import subprocess
import threading
def get_ip():
try:
import netifaces
except ImportError:
__NETIFACES_IMPORTED = False
else:
__NETIFACES_IMPORTED = True
if __NETIFACES_IMPORTED:
for iface in netifaces.interfaces():
if iface.startswith('eth'):
return netifaces.ifaddresses(iface)[netifaces.AF_INET][0] \
['addr']
else:
p = subprocess.Popen(['ip', '-4', '-o', 'addr', 'show'],
stdout=subprocess.PIPE)
for l in p.communicate()[0].split('\n'):
if not ': eth' in l:
continue
m = re.search(r'inet (([0-9]{1,3}\.){3}[0-9]{1,3})', l)
try:
return m.group(1)
except:
return '0.0.0.0'
else:
return '0.0.0.0'
class Lcm(object):
def __init__(self):
self.__lock = threading.RLock()
self._BUS = '2'
self._ADDR = '0x20'
self._WIDTH = 0x10
self._HEIGHT = 0x2
self._HIDDEN_WIDTH_BOUNDARY = 40
self._messages = ('', '')
self._BRIGHTNESS_DEPTH = 8
try:
import smbus
except ImportError:
self.__SMBUS_IMPORTED = False
else:
self.__SMBUS_IMPORTED = True
self._bus = smbus.SMbus(self._BUS)
self.init()
def refresh(self):
with self.__lock:
self.clear()
for line in self.messages:
for i in [str(ord(c)) for c in line \
+ ' ' * (self._HIDDEN_WIDTH_BOUNDARY - len(line))]:
self.write(i)
def increase_brightness(self):
with self.__lock:
subprocess.call(['i2cset', '-y', self._BUS, self._ADDR, '0x07', '0x01'])
def decrease_brightness(self):
with self.__lock:
subprocess.call(['i2cset', '-y', self._BUS, self._ADDR, '0x07', '0x00'])
def init(self):
with self.__lock:
self.write('0x1B')
self.write('0x40')
def clear(self):
with self.__lock:
self.write('0x0C')
def write(self, value):
if self.__SMBUS_IMPORTED:
self._bus.write_byte(self._ADDR, value)
else:
subprocess.call(['i2cset', '-y', self._BUS, self._ADDR, value])
@property
def messages(self):
return self._messages
@messages.setter
def messages(self, msg):
if not hasattr(msg, '__iter__'):
raise TypeError('messages is not iterable')
if len(msg) > self._HEIGHT:
raise IndexError('too many messages')
self._messages = msg
@property
def brightness_depth(self):
return self._BRIGHTNESS_DEPTH
def get_lcm():
return lcm
lcm = Lcm()
__version__ = '1.0'
__all__ = (
'get_lcm',
'get_ip',
'Lcm',
)