-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd2.py
88 lines (77 loc) · 3.19 KB
/
lcd2.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
from binascii import a2b_hex
from struct import unpack
import socket as python_socket
from pathlib import Path
import datetime
import calendar
import socket
import dateutil.parser as dparser
import numpy as np
from signal import signal, SIGTERM, SIGHUP, pause
# set up the display
from rpi_lcd import LCD
lcd = LCD()
#set up the data types although Packets now not used
mtype = [('Call','S12'), ('Packets',int),('Date', 'S19')]
ta = np.zeros((0), dtype=mtype)
def from_hex(hexstr):
return a2b_hex(hexstr.replace(' ', ''))
def to_hex(bytestr):
return ' '.join([
'%02x' % byte
for byte in bytestr
])
# strip the callsign from the data
def decode(data):
if isinstance(data, (bytes, bytearray)):
# Ensure the data is at least 7 bytes!
if len(data) < 7:
raise ValueError('AX.25 addresses must be 7 bytes!')
# This is a binary representation in the AX.25 frame header
callsign = bytes([
b >> 1
for b in data[0:6]
]).decode('US-ASCII').strip()
ssid = (data[6] & 0b00011110) >> 1
ch = bool(data[6] & 0b10000000)
res1 = bool(data[6] & 0b01000000)
res0 = bool(data[6] & 0b00100000)
extension = bool(data[6] & 0b00000001)
if ssid > 0:
callsign = callsign + "-" + str(ssid)
return callsign
#Location of the mheard.dat file
file_name = '/var/ax25/mheard/mheard.dat'
#Calculate the number of bytes so you dont go off the end of the file.
num_bytes = Path(file_name).stat().st_size * 2
#read the file in
with open(file_name, 'rb') as f:
hex_addr = f.read().hex()
n = 0
#Data offsets for the items I wanted
packet_offset = 72
offset =232
#Move the bytes around to get the correct order. Must be an easier way....
while n < num_bytes:
hexlist = hex_addr[n] + hex_addr[n+1] + " " + hex_addr[n+2] + hex_addr[n+3] + " " + hex_addr[n+4] + hex_addr[n+5] + " " + hex_addr[n+6] + \
hex_addr[n+7] + " " + hex_addr[n+8] + hex_addr[n+9] + " " + \
hex_addr[n+10] + hex_addr[n+11] + " " + hex_addr[n+12] + hex_addr[n+13]
addr = decode(from_hex(hexlist))
addr = (addr.ljust(12)).ljust(15)
packets = hex_addr[n+packet_offset+2] + hex_addr[n+packet_offset +
3] + hex_addr[n+packet_offset] + hex_addr[n+packet_offset+1]
last_heard_hex = hex_addr[n+offset+6] + hex_addr[n+offset + 7] + hex_addr[n+offset+4] + hex_addr[n+offset+5]+hex_addr[n+offset+2] + hex_addr[n+offset + 3] + hex_addr[n+offset+0] + hex_addr[n+offset+1]
last_heard_hex = "0x"+last_heard_hex
recovernow = datetime.datetime.fromtimestamp(int(last_heard_hex,16))
ta = np.append(ta, np.array([(str(addr), '{0:06}'.format(int(packets, 16)), recovernow)], dtype=mtype))
n = n+512
#Sort them on the "Date" column
ta.sort(order='Date')
#Find the time in the time date string for the display
for line in range(1,4):
temp = str(ta[0-line])
x=temp.find(":")
call_txt = temp[3:12] + " " + temp[(x-2):(x+6)]
lcd.text(call_txt, line)
#Hostname is fixed as it never changes but the IP Could change as set for DHCP
lcd.text("TNC2 " + socket.gethostbyname(socket.gethostname()),4)