Skip to content

Commit

Permalink
fix opencm9.04 flasher: make it python 2 & 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
aabadie committed Jan 20, 2017
1 parent 053383d commit 813c414
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions boards/opencm9-04/dist/robotis-loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
MIT License
Copyright (c) 2014 Grégoire Passault
Copyright (c) 2014 Gregoire Passault
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,11 +35,24 @@
#
# https://github.com/Gregwar/robotis-loader

import serial, sys, os, time
import serial
import sys
import os
import time

print('~~ Robotis loader ~~')
print('')


# Helper function for bytes conversion
if sys.version_info[:1][0] == 3:
def to_ord(val):
return ord(chr(val))
else:
def to_ord(val):
return ord(val)


# Reading command line
#if len(sys.argv) != 3:
# exit('! Usage: robotis-loader.py <serial-port> <binary>')
Expand All @@ -49,21 +62,26 @@
port = os.environ["PORT"]
binary = os.environ["HEXFILE"]

# Helper that prints a progress bar

def progressBar(percent, precision=65):
threshold=precision*percent/100.0
"""Prints a progress bar."""

threshold = precision*percent / 100.0
sys.stdout.write('[ ')
for x in xrange(0, precision):
if x < threshold: sys.stdout.write('#')
else: sys.stdout.write(' ')
for x in range(precision):
if x < threshold:
sys.stdout.write('#')
else:
sys.stdout.write(' ')
sys.stdout.write(' ] ')
sys.stdout.flush()


# Opening the firmware file
try:
stat = os.stat(binary)
size = stat.st_size
firmware = file(binary, 'rb')
firmware = open(binary, 'rb')
print('* Opening %s, size=%d' % (binary, size))
except:
exit('! Unable to open file %s' % binary)
Expand All @@ -79,19 +97,19 @@ def progressBar(percent, precision=65):
s.setDTR(False)
time.sleep(0.1)
s.setRTS(False)
s.write('CM9X')
s.write(b'CM9X')
s.close()
time.sleep(1.0);

print('* Connecting...')
s = serial.Serial(port, baudrate=115200)
s.write('AT&LD')
s.write(b'AT&LD')
print('* Download signal transmitted, waiting...')

# Entering bootloader sequence
while True:
line = s.readline().strip()
if line.endswith('Ready..'):
if line.endswith(b'Ready..'):
print('* Board ready, sending data')
cs = 0
pos = 0
Expand All @@ -100,23 +118,26 @@ def progressBar(percent, precision=65):
if len(c):
pos += len(c)
sys.stdout.write("\r")
progressBar(100*float(pos)/float(size))
progressBar(100 * float(pos) / float(size))
s.write(c)
for k in range(0,len(c)):
cs = (cs+ord(c[k]))%256
for k in range(0, len(c)):
cs = (cs + to_ord(c[k])) % 256
else:
firmware.close()
break
print('')
s.setDTR(True)
print('* Checksum: %d' % (cs))
s.write(chr(cs))
import struct
s.write(struct.pack('B', cs))
# s.write('{0}'.format(chr(cs)).encode('ascii'))
print('* Firmware was sent')
else:
if line == 'Success..':
if line == b'Success..':
print('* Success, running the code')
print('')
s.write('AT&RST')
s.write(b'AT&RST')
s.close()
exit()
else:
print('Board -> '+line)
print('Board -> {}'.format(line))

0 comments on commit 813c414

Please sign in to comment.