Skip to content

Commit 1b92651

Browse files
committed
Merge branch 'develop'
2 parents eac6909 + ed7de4c commit 1b92651

File tree

6 files changed

+106
-51
lines changed

6 files changed

+106
-51
lines changed

BrewPiUtil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def setupSerial(config, baud_rate=57600, time_out=0.1):
126126
else:
127127
port = portSetting
128128
try:
129-
ser = serial.Serial(port, baudrate=baud_rate, timeout=time_out)
129+
ser = serial.Serial(port, baudrate=baud_rate, timeout=time_out, writeTimeout=0)
130130
if ser:
131131
break
132132
except (IOError, OSError, serial.SerialException) as e:

brewpi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def printStdErr(*objs):
7777
# Settings will be read from controller, initialize with same defaults as controller
7878
# This is mainly to show what's expected. Will all be overwritten on the first update from the controller
7979

80-
compatibleHwVersion = "0.2.4"
80+
compatibleHwVersion = "0.4.0"
8181

8282
# Control Settings
8383
cs = dict(mode='b', beerSet=20.0, fridgeSet=20.0)

programController.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def program(self, hexFile, system1File, system2File, restoreWhat):
220220

221221
myDir = os.path.dirname(os.path.abspath(__file__))
222222
flashDfuPath = os.path.join(myDir, 'utils', 'flashDfu.py')
223-
command = sys.executable + ' ' + flashDfuPath + " --autodfu --file={0}".format(os.path.dirname(hexFile))
223+
command = sys.executable + ' ' + flashDfuPath + " --autodfu --noreset --file={0}".format(os.path.dirname(hexFile))
224224
if platform.system() == "Linux":
225225
command = 'sudo ' + command
226226

@@ -268,12 +268,13 @@ def program(self, hexFile, system1File, system2File, restoreWhat):
268268

269269
printStdErr("Waiting for device to reset.")
270270

271+
time.sleep(10) # give time to reboot
272+
271273
if not self.open_serial_with_retry(self.config, 57600, 0.2):
272274
printStdErr("Error opening serial port after programming. Program script will exit. Settings are not restored.")
273275
printStdErr("If your device stopped working, use flashDfu.py to restore it.")
274276
return False
275277

276-
time.sleep(1)
277278
self.fetch_new_version()
278279
self.reset_settings()
279280
if self.restoreSettings or self.restoreDevices:

utils/flashDfu.py

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@
4242
# Read in command line arguments
4343
try:
4444
opts, args = getopt.getopt(sys.argv[1:], "hf:t:ma",
45-
['help', 'file=', 'multi', 'tag=', 'testmode', 'autodfu', 'testmode'])
45+
['help', 'file=', 'system=', 'multi', 'tag=', 'testmode', 'autodfu', 'testmode', 'noreset'])
4646
except getopt.GetoptError:
47-
print "Unknown parameter, available Options: --file, --multi, --tag --autodfu --testmode"
47+
print "Unknown parameter, available Options: --file, --system, --multi, --tag --autodfu --testmode --noreset"
4848

4949
sys.exit()
5050

5151
multi = False
5252
testMode = False
5353
autoDfu = False
54+
noReset = False
5455
tag = None
5556
# binaries for system update
5657
system1 = None
@@ -64,31 +65,46 @@
6465
if o in ('-h', '--help'):
6566
print "\n Available command line options: "
6667
print "--help: print this help message"
67-
print "--file: path to .bin file to flash instead of the latest release on GitHub"
6868
print "--tag: specify which tag to download from github"
69+
print "--file: path to .bin file to flash instead of the latest release on GitHub.\n" \
70+
"If this is a directory, search for binary and system update files."
71+
print "--system: path to system binaries to update the system firmware on the photon."
6972
print "--multi: keep the script alive to flash multiple devices"
7073
print "--autodfu: automatically reboot photon in DFU mode by opening serial port at 14400 baud"
7174
print "--testmode: set controller o test mode after flashing"
75+
print "--noreset: do not reset EEPROM after flashing"
7276

7377
exit()
74-
# supply a config file
78+
# supply a binary file
7579
if o in ('-f', '--file'):
7680
print("Using local files instead of downloading a release. \n")
7781
if os.path.isdir(a):
7882
for file in os.listdir(a):
7983
if all(x in file for x in ['brewpi', '.bin']):
8084
binFile = os.path.join(os.path.abspath(a), file)
85+
else:
86+
binFile = os.path.abspath(a)
87+
if not os.path.exists(binFile):
88+
print('ERROR: Binary file "%s" was not found!' % binFile)
89+
exit(1)
90+
# supply a system update directory
91+
if o in ('-s', '--system'):
92+
print("Using local files for system update instead of downloading from GitHub.\n")
93+
if os.path.isdir(a):
94+
for file in os.listdir(a):
8195
if all(x in file for x in ['system', 'part1', '.bin']):
8296
system1 = os.path.join(os.path.abspath(a), file)
8397
if all(x in file for x in ['system', 'part2', '.bin']):
8498
system2 = os.path.join(os.path.abspath(a), file)
8599
else:
86-
binFile = os.path.abspath(a)
87-
if not os.path.exists(binFile):
88-
print('ERROR: Binary file(s) "%s" was not found!' % binFile)
100+
print('ERROR: System binaries location {0} is not a directory!' % a)
101+
if not os.path.exists(system1):
102+
print('ERROR: System binary 1 "%s" was not found!' % binFile)
89103
exit(1)
90-
if os.path.exists(system1) and os.path.exists(system2):
91-
print('System update files found, will update system part before flashing user binary. \n')
104+
if not os.path.exists(system2):
105+
print('ERROR: System binary 2 "%s" was not found!' % binFile)
106+
exit(1)
107+
92108
# send quit instruction to all running instances of BrewPi
93109
if o in ('-m', '--multi'):
94110
multi = True
@@ -102,6 +118,8 @@
102118
if o in ('-a', '--autodfu'):
103119
autoDfu = True
104120
print "Will automatically reboot newly detected photons into DFU mode"
121+
if o in ('--noreset'):
122+
noReset = True
105123

106124
dfuPath = "dfu-util"
107125
# check whether dfu-util can be found
@@ -179,10 +197,12 @@
179197

180198
# download latest binary from GitHub if file not specified
181199
if not binFile:
182-
print "Downloading latest firmware..."
183200
if tag is None:
201+
print "Downloading latest firmware..."
184202
tag = releases.getLatestTag()
185203
print "Latest stable version on GitHub: " + tag
204+
else:
205+
print "Downloading release " + tag
186206

187207
binFile = releases.getBin(tag, [type, 'brewpi', '.bin'])
188208
if binFile:
@@ -192,14 +212,22 @@
192212
exit(1)
193213

194214
if type == 'photon':
195-
system1 = releases.getBin(tag, ['photon', 'system-part1', '.bin'])
196-
system2 = releases.getBin(tag, ['photon', 'system-part2', '.bin'])
197-
215+
if LooseVersion(tag) > LooseVersion('0.2.11'): # 0.2.11 was compiled against non-forward compatible system
216+
latestSystemTag = releases.getLatestTagForSystem()
217+
else:
218+
latestSystemTag = tag
219+
print ("Updated system firmware for the photon found in release {0}".format(latestSystemTag))
220+
system1 = releases.getBin(latestSystemTag, ['photon', 'system-part1', '.bin'])
221+
system2 = releases.getBin(latestSystemTag, ['photon', 'system-part2', '.bin'])
198222
if system1:
199-
print "Release contains updated system firmware for the photon"
223+
print "Downloaded new system firmware to:\n"
224+
print "{0} and\n".format(system1)
200225
if not system2:
201226
print "Error: system firmware part2 not found in release"
202-
exit()
227+
exit(1)
228+
else:
229+
print "{0} and\n".format(system2)
230+
203231

204232
if binFile:
205233
if type == 'core':
@@ -218,26 +246,29 @@
218246
p = subprocess.Popen(dfuPath + " -d 0x2B04:0xD006 -a 0 -s 0x80A0000:leave -D {0}".format(binFile), shell=True)
219247
p.wait()
220248

221-
print "Programming done, now resetting EEPROM to defaults"
222-
# reset EEPROM to defaults
223-
configFile = util.scriptPath() + '/settings/config.cfg'
224-
config = util.readCfgWithDefaults(configFile)
225-
programmer = SerialProgrammer.create(config, "core")
249+
print "Programming done"
226250

227-
# open serial port
228-
print "Opening serial port"
229-
retries = 10
230-
while retries > 0:
231-
if programmer.open_serial(config, 57600, 0.2):
232-
break
233-
retries -= 1
234-
time.sleep(1)
235-
if retries > 0:
236-
programmer.fetch_version("Success! ")
237-
programmer.reset_settings(testMode)
238-
serialPorts = autoSerial.detect_all_ports() # update serial ports here so device will not be seen as new
239-
else:
240-
print "Could not open serial port after programming"
251+
if not noReset:
252+
print "Now resetting EEPROM to defaults"
253+
# reset EEPROM to defaults
254+
configFile = util.scriptPath() + '/settings/config.cfg'
255+
config = util.readCfgWithDefaults(configFile)
256+
programmer = SerialProgrammer.create(config, type)
257+
258+
# open serial port
259+
print "Opening serial port"
260+
retries = 10
261+
while retries > 0:
262+
if programmer.open_serial(config, 57600, 0.2):
263+
break
264+
retries -= 1
265+
time.sleep(1)
266+
if retries > 0:
267+
programmer.fetch_version("Success! ")
268+
programmer.reset_settings(testMode)
269+
serialPorts = autoSerial.detect_all_ports() # update serial ports here so device will not be seen as new
270+
else:
271+
print "Could not open serial port after programming"
241272
else:
242273
print "found DFU device, but no binary specified for flashing"
243274
if not multi:
@@ -261,8 +292,13 @@
261292
ser = serial.Serial(port)
262293
try:
263294
ser.setBaudrate(14400) # this triggers a reboot in DFU mode
295+
time.sleep(1)
296+
ser.close()
264297
except ValueError:
265298
pass # because device reboots while reconfiguring an exception is thrown, ignore
266-
ser.close()
299+
if ser.isOpen():
300+
ser.close()
301+
ser.setBaudrate(57600) # don't leave serial port at 14400, or a reboot will be triggered later
302+
267303

268304
time.sleep(1)

utils/gitHubReleases.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ def download(self, url, path):
3131
def update(self):
3232
self.releases = json.load(urllib2.urlopen(self.url + "/releases"))
3333

34-
# writes .bin in release tagged with tag to directory
35-
# defaults to ./downloads/tag_name/ as download location
36-
def getBin(self, tag, wordsInFileName, path = None):
34+
# Finds a binary for a certain tag on GitHub
35+
def getBinUrl(self, tag, wordsInFileName):
3736
try:
3837
match = (release for release in self.releases if release["tag_name"] == tag).next()
3938
except StopIteration:
@@ -48,6 +47,12 @@ def getBin(self, tag, wordsInFileName, path = None):
4847
if all(word.lower() in urlFileName.lower() for word in wordsInFileName):
4948
downloadUrl = url
5049

50+
return downloadUrl
51+
52+
# writes .bin in release tagged with tag to directory
53+
# defaults to ./downloads/tag_name/ as download location
54+
def getBin(self, tag, wordsInFileName, path=None):
55+
downloadUrl = self.getBinUrl(tag, wordsInFileName)
5156
if not downloadUrl:
5257
return None
5358

@@ -68,6 +73,16 @@ def getLatestTag(self):
6873
break
6974
return release["tag_name"]
7075

76+
def getLatestTagForSystem(self):
77+
for release in self.releases:
78+
# search for stable release
79+
tag = release["tag_name"]
80+
if release["prerelease"] == True:
81+
continue
82+
if self.getBinUrl(tag, ['photon', 'system-part1', '.bin']):
83+
return tag
84+
return None
85+
7186
def getTags(self):
7287
return self.releases[0]["tag_name"]
7388

utils/updateFirmware.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,19 @@ def updateFromGitHub(userInput = False, restoreSettings = True, restoreDevices =
126126
printStdErr("Error: Device family {0} not recognized".format(family))
127127
return -1
128128

129-
130129
if boardName == "Photon":
131-
system1 = releases.getBin(tag, ['photon', 'system-part1', '.bin'])
132-
system2 = releases.getBin(tag, ['photon', 'system-part2', '.bin'])
133-
if system1:
134-
printStdErr("Release contains updated system firmware for the photon")
135-
if not system2:
136-
printStdErr("Error: system firmware part2 not found in release")
137-
return -1
138-
130+
latestSystemTag = releases.getLatestTagForSystem()
131+
if hwVersion.isNewer(latestSystemTag):
132+
printStdErr("Updated system firmware for the photon found in release {0}".format(latestSystemTag))
133+
system1 = releases.getBin(latestSystemTag, ['photon', 'system-part1', '.bin'])
134+
system2 = releases.getBin(latestSystemTag, ['photon', 'system-part2', '.bin'])
135+
if system1:
136+
printStdErr("Downloaded new system firmware to:\n")
137+
printStdErr("{0} and\n".format(system1))
138+
if not system2:
139+
printStdErr("{0}\n".format(system2))
140+
printStdErr("Error: system firmware part2 not found in release")
141+
return -1
139142

140143
if localFileName:
141144
printStdErr("Latest firmware downloaded to " + localFileName)

0 commit comments

Comments
 (0)