Created
September 13, 2010 20:16
-
-
Save alvesjnr/577950 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
# -*- 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 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[::-1] | |
return ret | |
def create_formated_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 __init__(self,parity=0,start=1,stop=1): | |
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): | |
sum = 0 | |
for i in c: sum += int(i) | |
return sum%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 | |
r.append(self.parity & self.__get_parity(c)) | |
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_formated_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