-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhaleTunesPython.py
More file actions
154 lines (116 loc) · 5.12 KB
/
WhaleTunesPython.py
File metadata and controls
154 lines (116 loc) · 5.12 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File Name: WhaleTunesPython.py
# Author: Seamus Finlayson
# Created: 2023-04-25
#default libraries
from datetime import datetime
import time
import os
import sys
#additional libraries
import pyrebase
import serial
#my files
from waveFileClass import waveFileClass
from firebaseApiConfig import firebaseConfig
#startup dialog
print("Running...")
#setup serial
ser = serial.Serial("/dev/serial0", 430000)
print("Using serial: ", ser.name)
#firebase setup
firebase = pyrebase.initialize_app(firebaseConfig)
storage = firebase.storage()
#state machine setup
WAITING_STATE = 0
COLLECTING_DATA_STATE = 1
UPLOADING_DATA_STATE = 2
state = WAITING_STATE
#uart code symbols
ESCAPE_CHAR = "ffff"
SOF_CHAR = "ffee" #start of file character
EOF_CHAR = "fffe" #end of file character
#default file name
fileName = "error getting name"
while True:
#detect exit key -- not implemented
if False:
pass
else:
#select state
if state == WAITING_STATE:
#indicate state change
print("\nWaiting for hydrophone...")
#check for escape character
byte_array = ser.read(2)
if byte_array.hex() == ESCAPE_CHAR:
#check for start of file character
byte_array = ser.read(2)
if byte_array.hex() == SOF_CHAR:
#go to next state
print("Start character received.")
state = COLLECTING_DATA_STATE
else:
print("ERROR unexpected uart code word: ", byte_array.hex())
print("Expected ", SOF_CHAR)
sys.exit(1)
else:
print("ERROR unexpected uart code word: ", byte_array.hex())
print("Expected ", ESCAPE_CHAR)
sys.exit(1)
elif state == COLLECTING_DATA_STATE:
#indicate state change
print("\nCollecting data from hydrophone...")
#get date and time for file name
now = datetime.now()
dateTimeString = now.strftime("%Y-%m-%d at %H:%M:%S")
#set file name
fileName = "Recording from " + dateTimeString
print("File name is: ", fileName)
audioDataFileName = fileName + ".dat"
audioDataPath = os.path.join("./Hydrophone_Data", audioDataFileName)
#write samples to audio file until escape character
with open(audioDataPath, 'wb') as f:
#get samples in loop
quit = False
while not quit:
#get data
byte_array = ser.read(2)
#binary_data = bytes([data[0], data[1]) #data is already bytes data type
#check for escape character
if byte_array.hex() == ESCAPE_CHAR:
byte_array = ser.read(2)
if byte_array.hex() == ESCAPE_CHAR:
#escape char value was sent, write it to file
intData = int.from_bytes(byte_array, "big", signed="True")
bytesData = bytearray(intData.to_bytes(2, "little", signed=True))
f.write(bytesData)
elif byte_array.hex() == EOF_CHAR:
#end of file was sent, go to next state
quit = True
print("End of data character received.")
#next state
state = UPLOADING_DATA_STATE
else:
print("ERROR bad data received")
sys.exit(1)
else:
#write data to file
#print(time.time(), " data is: ", data) #debug only
intData = int.from_bytes(byte_array, "big", signed="True")
bytesData = bytearray(intData.to_bytes(2, "little", signed=True))
f.write(bytesData)
elif state == UPLOADING_DATA_STATE:
#indicate state change
print("\nUploading data to firebase...")
#audioDataFileName = "WhaleData.dat" #testing only
waveFileName = fileName + ".wav"
waveFilePath = os.path.join("./Recordings", waveFileName)
waveFileClass(8000, audioDataPath, waveFilePath).createFile()
print("Recording saved as: ", waveFilePath)
# upload file
storage.child(waveFileName).put(waveFilePath) #disable while debugging other components
print("File uploaded.")
#remove file
# os.remove(name)
# print("File Removed")
state = WAITING_STATE