def read(ch): buf = "00" # any 2 bytes wiringpi2.wiringPiSPIDataRW(ch, buf) # this returns [D0:D7] [D8:D15] (i.e. D0 is the highest order bit if the 16 bit word bytes = map(ord, buf) word = (bytes[0]<<8) + bytes[1] return word
def send_code(self, bits): # increment message counter self.counter += 1 print 'Sending code ' + bits + ':', sys.stdout.flush() # repeat 5 times buf = '' for x in range(0, 5): # first two bits: message counter buf += self.counterValues[self.counter & 3] # add data bits for bit in bits: if bit == '0': buf += self.ZERO else: buf += self.ONE # add padding for pause between packets buf += '\x00\x00\x00\x00\x00\x00\x00\x00\x00' # send the packet wiringpi.wiringPiSPIDataRW(self.SPI_CHANNEL, buf) # print 'OK - WARNING: DEBUG MODE, DID NOT ACTUALLY TRANSMIT DATA' print 'OK'
def sendCmd(self, register, data): buffer=(register<<8)+data buffer=buffer.to_bytes(2, byteorder='big') if self.DEBUG:print("Send byte: 0x%04x"% int.from_bytes(buffer,'big')) wiringpi2.wiringPiSPIDataRW(SPI_CS,buffer) if self.DEBUG:print("Response: 0x%04x"% int.from_bytes(buffer,'big')) return buffer
def __send_data(self, tx_bytes): u'''Converts the tx_byte list to a string and sends the string via the SPI interface.''' data = "" rx_data = [] for x in tx_bytes: data = data + chr(x) wiringpi.wiringPiSPIDataRW(0,data); for x in data: rx_data.append(ord(x)) return rx_data
def __send_data(self, tx_bytes): u'''Converts the tx_byte list to a string and sends the string via the SPI interface.''' data = "" rx_data = [] for x in tx_bytes: data = data + chr(x) wiringpi.wiringPiSPIDataRW(0, data) for x in data: rx_data.append(ord(x)) return rx_data
def writeToDisplay(data): """ Write data to the display """ length = len(data) if length > DISPLAY_BUF_SZ: print "bsp_writeToDisplay: ERROR len=%d > %d" % (length, DISPLAY_BUF_SZ) return #TODO: can hang here... wiringpi2.wiringPiSPIDataRW(0, data)
def send(self, address, data): #msg = address << 8 | data msg = chr(self.NOOP) * (self.__display_count - self.__display) * 2 msg += chr(address % 256) + chr(data % 256) # Avoids unicode #print "{0:0>{1}}".format(bin(address)[2:], 16) + " " + "{0:0>{1}}".format(bin(data)[2:], 16) msg += chr(self.NOOP) * (self.__display) * 2 return wiringpi2.wiringPiSPIDataRW(self.__channel, msg)
def ReadByte(self): """ Reads a byte from the SPI bus :returns: byte read from the bus """ byte = wp.wiringPiSPIDataRW(self.SPI_CHANNEL, chr(0x00)) return byte
def task(): # Doesn't matter WHAT this is, it's just dummy data to poll the slave MCU myData = 'A' # 0x41 in hex # Because SPI recevies a byte when it sends, for some reason I found you # need to send TWO calls oherwise you get into a "ping pong" scenario wiringpi2.wiringPiSPIDataRW(1,myData) wiringpi2.wiringPiSPIDataRW(1,myData) # Turn the data into something we can use # http://stackoverflow.com/questions/1916928/ hexdata = ''.join('%02x' % ord(byte) for byte in myData) doActions(hexdata) ## How do you run your own code alongside Tkinter's event loop? ## http://stackoverflow.com/questions/459083/ root.after(10,task) # reschedule event 100 times per second
def getByte(self, out=0x00): """ return uint8_t """ # Using file handles doesn't work too well #os.write(self.fd, struct.pack('B', out)) #c = os.read(self.fd, 1) # Requires a fixed version of wiringpi2 SPI function _, c = wiringpi2.wiringPiSPIDataRW(0, struct.pack('B', out)) ret = struct.unpack('B', c)[0] return ret
def SendByte(self, byte): """ Sends a byte to the SPI bus """ debug_print("Entered SendByte") debug_print("Sending: " + str(byte)) data = chr(byte) result = wp.wiringPiSPIDataRW(self.SPI_CHANNEL, data) debug_print("Read " + str(data))
def _adc_read_channels(self): chdq = [ chr(0x01) + chr(0xA0) + chr(0x00), chr(0x01) + chr(0xE0) + chr(0x00) ] val = [0, 0] for i in range(2): dq = chdq[i] self._adc_select() # wiringpi2.delayMicroseconds(1) wiringpi2.wiringPiSPIDataRW(0, dq) # wiringpi2.delayMicroseconds(1) self._adc_deselect() val[i] = (ord(dq[1]) << 8) | (ord(dq[2]) << 0) return val
def send(self, read_write, address_char, string_data): if (read_write): address_bin = fill_bits_to_byte(str2bits(address_char)) address_char = bits2str('1' + address_bin[1:]) string_data_cpy = address_char + string_data[:] error = wiringpi.wiringPiSPIDataRW(self.ce_channel, string_data_cpy) if (error == -1): # couldn't make SendingEcteption take more than one argument in contructor... #error_msg = SendingException.get_msg(read_write, address_char, string_data) error_msg = ( 'Error while SPI %s address:%s data:%s' % ( 'read' if read_write else 'write', str2bits(address_char), '(not important)' if read_write else str2bits(string_data) ) ) raise SendingException(error_msg) return string_data_cpy[1:] if read_write else None
def write_str(self, s): wiringpi2.wiringPiSPIDataRW(self.spi_name, s)
def max7219WriteReg(addr, data): io.digitalWrite(SPI_CS_Pin, io.LOW) wpi.wiringPiSPIDataRW(0, chr(addr) + chr(data)) io.digitalWrite(SPI_CS_Pin, io.HIGH)
def write_str(self, s): # os.write(self.port, s) wiringpi2.wiringPiSPIDataRW(self.spi_name, s)
#!/usr/bin/env python import wiringpi2 setupres = wiringpi2.wiringPiSPISetup(0, 5000) while True: command = raw_input("Data to write?") written = wiringpi2.wiringPiSPIDataRW(0, command+'\n')
# -*- coding: utf-8 -*- import wiringpi2 as w import time def MCP3008(channel) register = 0x80 buff=(1 << 16) + (register<<8)+(channel<<12) buff=buff.to_bytes(3,byteorder='big') w.wiringPiSPIDataRW(0,buff) return (((buf[1]&3)*256)+buff[2]) def telmin(): REF=3.3 # 5.p or 3.3 mode=1 w.wiringPiSetup() w.wiringPiSPISetup(0,1000000) w.pinMode(0,1) w.pinMode(2,1) w.pinMode(1,2) #w.softPwmCreate(1,0,100) w.softPwmCreate(2,0,100) w.digitalWrite(0,1) while 1: data1 = MCP3008(1) data7 = MCP3008(7) time.sleep(0.01) data11 = int(data1)-860; w.softPwmWrite(2,data11)
# if count%1000==1 : # print ("%.4f" % time.time()) theta_1 = theta_1 - 1 #minus one because way I defined angles (CCW = negative) if theta_1 < 0 : #changes -1degree to 359degrees theta_1 = theta_1 + 360 timeUpdate = timeUpdate + SPDeg #Update Buffer with appropriate integer values degreeColors = Degree(theta_1,frame) #Assigning LEDs the colors from degreeColors list i = 0 while i < 3 : #LED Stick 1 buff_1 = ''.join(chr(x) for x in [ICaddr[i], GPIOA, degreeColors[0][2*i], degreeColors[0][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_1) #LED Stick 2 buff_2 = ''.join(chr(x) for x in [ICaddr[i+3], GPIOA, degreeColors[1][2*i], degreeColors[1][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_2) #LED Stick 3 buff_3 = ''.join(chr(x) for x in [ICaddr[i], GPIOA, degreeColors[2][2*i], degreeColors[2][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(1, buff_3) #LED Stick 4 buff_4 = ''.join(chr(x) for x in [ICaddr[i+3], GPIOA, degreeColors[3][2*i], degreeColors[3][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(1, buff_4) i = i+1 except KeyboardInterrupt : pass #---- Cleanup ----#
#print degreeColors[0:6] theta_1 = theta_1 - 1 #minus one because way i defined angles (CCW = negative) if theta_1 < 0 : #changes -1degree to 359degrees theta_1 = theta_1 + 360 timeUpdate = time.time() + SPDeg #Update Buffer with appropriate integer values degreeColors = Degree(theta_1) one = degreeColors[0] #print one #Assigning LEDs the colors from allColors list i = 0 while i < 3 : #LED Stick 1 buff_1 = ''.join(chr(x) for x in [ICaddr[i], GPIOA, degreeColors[0][2*i], degreeColors[0][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_1) #LED Stick 2 buff_2 = ''.join(chr(x) for x in [ICaddr[i+3], GPIOA, degreeColors[1][2*i], degreeColors[1][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_2) #LED Stick 3 buff_3 = ''.join(chr(x) for x in [ICaddr[i], GPIOA, degreeColors[2][2*i], degreeColors[2][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_3) #LED Stick 4 buff_4 = ''.join(chr(x) for x in [ICaddr[i+3], GPIOA, degreeColors[3][2*i], degreeColors[3][2*i+1]]) result = wiringpi.wiringPiSPIDataRW(0, buff_4) i = i+1 except KeyboardInterrupt : pass #---- Cleanup ----#
#!/usr/bin/python3 # spiTest.py import wiringpi2 print("Add SPI Loopback - connect P1-Pin19 and P1-Pin21") print("[Press Enter to continue]") input() wiringpi2.wiringPiSPISetup(1,500000) buffer=str.encode("HELLO") print("Buffer sent %s" % buffer) wiringpi2.wiringPiSPIDataRW(1,buffer) print("Buffer received %s" % buffer) print("Remove the SPI Loopback") print("[Press Enter to continue]") input() buffer=str.encode("HELLO") print("Buffer sent %s" % buffer) wiringpi2.wiringPiSPIDataRW(1,buffer) print("Buffer received %s" % buffer) #End