Created
June 28, 2017 14:24
-
-
Save aSmig/00e101b4bf3e95a9de64c4e8c4745c3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Copyright 2011 Kenny McElroy <[email protected]> | |
Based on the I2C testscript from Peter Huewe | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>. | |
For a compatible copy of pyBusPirateLite, try cloning from | |
https://github.com/jevinskie/bus-pirate-old | |
""" | |
import sys, re | |
from time import sleep | |
from pyBusPirateLite.I2C import * | |
characters = { | |
' ': [0x00, 0x00, 0x00, 0x00, 0x00], # (space) | |
'!': [0x00, 0x00, 0x5F, 0x00, 0x00], # ! | |
'"': [0x00, 0x07, 0x00, 0x07, 0x00], # " | |
'#': [0x14, 0x7F, 0x14, 0x7F, 0x14], # # | |
'$': [0x24, 0x2A, 0x7F, 0x2A, 0x12], # $ | |
'%': [0x23, 0x13, 0x08, 0x64, 0x62], # % | |
'&': [0x36, 0x49, 0x55, 0x22, 0x50], # & | |
'\'': [0x00, 0x05, 0x03, 0x00, 0x00], # ' | |
'(': [0x00, 0x1C, 0x22, 0x41, 0x00], # ( | |
')': [0x00, 0x41, 0x22, 0x1C, 0x00], # ) | |
'*': [0x08, 0x2A, 0x1C, 0x2A, 0x08], # * | |
'+': [0x08, 0x08, 0x3E, 0x08, 0x08], # + | |
',': [0x00, 0x50, 0x30, 0x00, 0x00], # , | |
'-': [0x08, 0x08, 0x08, 0x08, 0x08], # - | |
'.': [0x00, 0x60, 0x60, 0x00, 0x00], # . | |
'/': [0x20, 0x10, 0x08, 0x04, 0x02], # / | |
'0': [0x3E, 0x51, 0x49, 0x45, 0x3E], # 0 | |
'1': [0x00, 0x42, 0x7F, 0x40, 0x00], # 1 | |
'2': [0x42, 0x61, 0x51, 0x49, 0x46], # 2 | |
'3': [0x21, 0x41, 0x45, 0x4B, 0x31], # 3 | |
'4': [0x18, 0x14, 0x12, 0x7F, 0x10], # 4 | |
'5': [0x27, 0x45, 0x45, 0x45, 0x39], # 5 | |
'6': [0x3C, 0x4A, 0x49, 0x49, 0x30], # 6 | |
'7': [0x01, 0x71, 0x09, 0x05, 0x03], # 7 | |
'8': [0x36, 0x49, 0x49, 0x49, 0x36], # 8 | |
'9': [0x06, 0x49, 0x49, 0x29, 0x1E], # 9 | |
':': [0x00, 0x36, 0x36, 0x00, 0x00], # : | |
';': [0x00, 0x56, 0x36, 0x00, 0x00], # ; | |
'<': [0x00, 0x08, 0x14, 0x22, 0x41], # < | |
'=': [0x14, 0x14, 0x14, 0x14, 0x14], # = | |
'>': [0x41, 0x22, 0x14, 0x08, 0x00], # > | |
'?': [0x02, 0x01, 0x51, 0x09, 0x06], # ? | |
'@': [0x32, 0x49, 0x79, 0x41, 0x3E], # @ | |
'A': [0x7E, 0x11, 0x11, 0x11, 0x7E], # A | |
'B': [0x7F, 0x49, 0x49, 0x49, 0x36], # B | |
'C': [0x3E, 0x41, 0x41, 0x41, 0x22], # C | |
'D': [0x7F, 0x41, 0x41, 0x22, 0x1C], # D | |
'E': [0x7F, 0x49, 0x49, 0x49, 0x41], # E | |
'F': [0x7F, 0x09, 0x09, 0x01, 0x01], # F | |
'G': [0x3E, 0x41, 0x41, 0x51, 0x32], # G | |
'H': [0x7F, 0x08, 0x08, 0x08, 0x7F], # H | |
'I': [0x00, 0x41, 0x7F, 0x41, 0x00], # I | |
'J': [0x20, 0x40, 0x41, 0x3F, 0x01], # J | |
'K': [0x7F, 0x08, 0x14, 0x22, 0x41], # K | |
'L': [0x7F, 0x40, 0x40, 0x40, 0x40], # L | |
'M': [0x7F, 0x02, 0x04, 0x02, 0x7F], # M | |
'N': [0x7F, 0x04, 0x08, 0x10, 0x7F], # N | |
'O': [0x3E, 0x41, 0x41, 0x41, 0x3E], # O | |
'P': [0x7F, 0x09, 0x09, 0x09, 0x06], # P | |
'Q': [0x3E, 0x41, 0x51, 0x21, 0x5E], # Q | |
'R': [0x7F, 0x09, 0x19, 0x29, 0x46], # R | |
'S': [0x46, 0x49, 0x49, 0x49, 0x31], # S | |
'T': [0x01, 0x01, 0x7F, 0x01, 0x01], # T | |
'U': [0x3F, 0x40, 0x40, 0x40, 0x3F], # U | |
'V': [0x1F, 0x20, 0x40, 0x20, 0x1F], # V | |
'W': [0x7F, 0x20, 0x18, 0x20, 0x7F], # W | |
'X': [0x63, 0x14, 0x08, 0x14, 0x63], # X | |
'Y': [0x03, 0x04, 0x78, 0x04, 0x03], # Y | |
'Z': [0x61, 0x51, 0x49, 0x45, 0x43], # Z | |
'[': [0x00, 0x00, 0x7F, 0x41, 0x41], # [ | |
'\\': [0x02, 0x04, 0x08, 0x10, 0x20], # \ | |
']': [0x41, 0x41, 0x7F, 0x00, 0x00], # ] | |
'^': [0x04, 0x02, 0x01, 0x02, 0x04], # ^ | |
'_': [0x40, 0x40, 0x40, 0x40, 0x40], # _ | |
'`': [0x00, 0x01, 0x02, 0x04, 0x00], # ` | |
'a': [0x20, 0x54, 0x54, 0x54, 0x78], # a | |
'b': [0x7F, 0x48, 0x44, 0x44, 0x38], # b | |
'c': [0x38, 0x44, 0x44, 0x44, 0x20], # c | |
'd': [0x38, 0x44, 0x44, 0x48, 0x7F], # d | |
'e': [0x38, 0x54, 0x54, 0x54, 0x18], # e | |
'f': [0x08, 0x7E, 0x09, 0x01, 0x02], # f | |
'g': [0x08, 0x14, 0x54, 0x54, 0x3C], # g | |
'h': [0x7F, 0x08, 0x04, 0x04, 0x78], # h | |
'i': [0x00, 0x44, 0x7D, 0x40, 0x00], # i | |
'j': [0x20, 0x40, 0x44, 0x3D, 0x00], # j | |
'k': [0x00, 0x7F, 0x10, 0x28, 0x44], # k | |
'l': [0x00, 0x41, 0x7F, 0x40, 0x00], # l | |
'm': [0x7C, 0x04, 0x18, 0x04, 0x78], # m | |
'n': [0x7C, 0x08, 0x04, 0x04, 0x78], # n | |
'o': [0x38, 0x44, 0x44, 0x44, 0x38], # o | |
'p': [0x7C, 0x14, 0x14, 0x14, 0x08], # p | |
'q': [0x08, 0x14, 0x14, 0x18, 0x7C], # q | |
'r': [0x7C, 0x08, 0x04, 0x04, 0x08], # r | |
's': [0x48, 0x54, 0x54, 0x54, 0x20], # s | |
't': [0x04, 0x3F, 0x44, 0x40, 0x20], # t | |
'u': [0x3C, 0x40, 0x40, 0x20, 0x7C], # u | |
'v': [0x1C, 0x20, 0x40, 0x20, 0x1C], # v | |
'w': [0x3C, 0x40, 0x30, 0x40, 0x3C], # w | |
'x': [0x44, 0x28, 0x10, 0x28, 0x44], # x | |
'y': [0x0C, 0x50, 0x50, 0x50, 0x3C], # y | |
'z': [0x44, 0x64, 0x54, 0x4C, 0x44], # z | |
'{': [0x00, 0x08, 0x36, 0x41, 0x00], # { | |
'|': [0x00, 0x00, 0x7F, 0x00, 0x00], # | | |
'}': [0x00, 0x41, 0x36, 0x08, 0x00], # } | |
#'->': [0x08, 0x08, 0x2A, 0x1C, 0x08], # -> | |
#'<-': [0x08, 0x1C, 0x2A, 0x08, 0x08], # <- | |
} | |
def i2c_write_data(data): | |
i2c.send_start_bit() | |
i2c.bulk_trans(len(data),data) | |
i2c.send_stop_bit() | |
def i2c_read_bytes(address, numbytes, ret=False): | |
data_out=[] | |
i2c.send_start_bit() | |
i2c.bulk_trans(len(address),address) | |
print "+-----------------------------------------------------+" | |
while numbytes > 0: | |
if not ret: | |
sys.stdout.write(" %02x"%ord(i2c.read_byte())) | |
else: | |
data_out.append(ord(i2c.read_byte())) | |
if numbytes > 1: | |
i2c.send_ack() | |
numbytes-=1 | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
print "+-----------------------------------------------------+" | |
i2c.send_nack() | |
i2c.send_stop_bit() | |
if ret: | |
return data_out | |
def build_char(char): | |
return (characters[char]) | |
def build_phrase(string): | |
length = len(string) | |
if length > 20: | |
print "Too many characters in string: %s" % string | |
print "Remove at least %i characters" % (length - 20) | |
sys.exit() | |
data = [length] | |
for char in re.findall(".", string): | |
data.insert(1, build_char(char)) | |
return data | |
def build_load(phrases): | |
data = [len(phrases)] | |
for phrase in phrases: | |
data.extend(build_phrase(phrase)) | |
return flatten(data) | |
def write_load(phrases): | |
load = build_load(phrases) | |
print "size: ", len(load), "data: ", load | |
if len(load) > 512: | |
print "That is too many bytes for this pathetic EEPROM." | |
print "Remove at least %i characters" % ((len(load) - 512) / 5) | |
sys.exit() | |
addr=0 | |
eeaddr=0xA0 | |
while len(load) > addr: | |
if addr > 0xff: | |
eeaddr=0xA2 | |
chunk = flatten([eeaddr, addr&0xff, load[addr:addr+8]]) | |
print chunk | |
i2c_write_data(chunk) | |
addr+=8 | |
def flatten(data): | |
if type(data) != list: | |
return data | |
out=[] | |
dive=False | |
for chunk in data: | |
if type(chunk) != list: | |
out.append(chunk) | |
else: | |
for subchunk in chunk: | |
out.append(subchunk) | |
dive=True | |
if dive: | |
return flatten(out) | |
return out | |
def bitswap(b): | |
b<<=1 | |
r = 0 | |
mask = 0x80 | |
while mask: | |
r >>= 1 | |
if b & mask: | |
r |= 0x80 | |
mask >>= 1 | |
return r ^ 0x7f | |
def invert_chars(): | |
for c in characters.keys(): | |
new=[] | |
for v in characters[c]: | |
new.insert(0, bitswap(v)) | |
characters[c] = new | |
def BP_binmode(i2c): | |
print "Entering binmode: ", | |
if i2c.BBmode(): | |
print "OK." | |
else: | |
print "failed." | |
sys.exit() | |
def BP_I2C(i2c): | |
print "Entering raw I2C mode: ", | |
if i2c.enter_I2C(): | |
print "OK." | |
else: | |
print "failed." | |
sys.exit() | |
print "Configuring I2C." | |
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS): | |
print "Failed to set I2C peripherals." | |
sys.exit() | |
if not i2c.set_speed(I2CSpeed._50KHZ): | |
print "Failed to set I2C Speed." | |
sys.exit() | |
i2c.timeout(0.2) | |
def BP_reset(i2c): | |
print "Reset Bus Pirate to user terminal: " | |
if i2c.resetBP(): | |
print "OK." | |
else: | |
print "failed." | |
sys.exit() | |
if __name__ == '__main__': | |
if len (sys.argv) < 3: | |
sys.stderr.write ('usage: %s <port> <text> ["more text"...]\n' % (sys.argv[0])) | |
sys.exit (1) | |
invert_chars() | |
print "Using port: %s" % sys.argv[1] | |
i2c = I2C(sys.argv[1], 115200) | |
BP_binmode(i2c) | |
BP_I2C(i2c) | |
#write_load(['Hack this fan', 'Hack all the things!']) | |
write_load(sys.argv[2:]) | |
#sleep(1) | |
print "Reading EEPROM." | |
i2c_write_data([0xa0, 0]) | |
print i2c_read_bytes([0xa1],12) | |
BP_reset(i2c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment