Created
September 16, 2010 17:54
-
-
Save alvesjnr/582846 to your computer and use it in GitHub Desktop.
This file contains 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/python | |
# -*- coding: utf-8 -*- | |
__author__ = "Antonio Ribeiro Alves Júnior" | |
__date__ = "Sept/2010" | |
__license__ = "Creative Commons" | |
__contact__ = "alvesjunior.antonio [at] gmail [dot] com" | |
class Serial: | |
def __init__(self,parity=0,start=1,stop=1): | |
self.parity = parity | |
self.start = start | |
self.stop = stop | |
def create_stream(self,frase): | |
"""Cria o stream serial binário para uma determinada cadeia de caracteres de entrada""" | |
ret = [] | |
for i in frase: | |
piece = self.__create_slice(i) | |
ret += piece[::] | |
return ret | |
def create_formatted_stream(self,frase, m=10, high=1, low=0): | |
"""Cria o stream de saida formatado para uma determinada cadeia de caracteres de entrada""" | |
ret = [] | |
v = self.create_stream(frase) | |
for i in v: | |
if i: ret += m*[high] | |
else: ret += m*[low] | |
return ret | |
def tuning(self,parity=0,start=1, stop=1): | |
"""Método invocado para alterar configurações básicas""" | |
self.parity = parity | |
self.start = start | |
self.stop = stop | |
def __to8(self,v): | |
if len(v) > 8: | |
raise 'Too Large Input' | |
return | |
return '0'+v | |
def __get_vector_bin(self,c): | |
try: | |
c = c[0] | |
except: | |
print 'You must provide a single character for this function' | |
c = bin(ord(c))[2::] | |
while len(c)<8: | |
c = self.__to8(c) | |
return c[::-1] | |
def __get_parity(self,c): | |
c = map(lambda i:int(i),c) | |
return sum(c)%2 | |
def __create_slice(self,c): | |
r = [] | |
v = [] | |
c = self.__get_vector_bin(c) | |
for i in c: v.append(int(i)) | |
r.append(self.start) | |
r += v | |
if self.parity: | |
par = not self.__get_parity(c) | |
else: | |
par = self.__get_parity(c) | |
r.append(par) | |
r.append(self.stop) | |
return r | |
if __name__ == "__main__": | |
""" | |
Este programa de exemplo cria o gráfico de saída para a frase 'O rato roeu a roupa do rei de roma' | |
As configurações para gerar o gráfico neste exemplo foram: | |
Tensão de saída para bit 1: +5v | |
Tensão de saída para bit 0: -5v | |
Fator multiplicador: 10x (quantas vezes o bit será repetido para formar a onde) | |
Paridade par | |
Start bit: 1 | |
Stop bit: 1 | |
""" | |
try: | |
import pylab | |
except: | |
print "Você deve instalar o módulo pylab para realizar as impressões dos gráficos" | |
print "http://www.scipy.org/PyLab" | |
frase = "O rato roeu a roupa do rei de roma" | |
s = Serial() | |
out = s.create_formatted_stream(frase,10,+5,-5) | |
pylab.ylim(-7,7) | |
pylab.plot(out) | |
pylab.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment