class NysaGPIOServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer, ServerBase): @staticmethod def name(): return "gpio" @staticmethod def get_request_handler(): return gpio_request_handler def get_name(self): return "gpio" def setup(self, nysa): self.n = nysa self.n.read_sdb() self.urn = self.n.find_device(GPIO)[0] print "Found a GPIO device: %s" % self.urn self.gpio = GPIO(self.n, self.urn) self.gpio.set_port_direction(0x00000003) self.gpio.set_port_raw(0x00000003) time.sleep(0.100) self.gpio.set_port_raw(0x00000000) print "setup gpios"
class NysaGPIOServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer, ServerBase): @staticmethod def name(): return "gpio" @staticmethod def get_request_handler(): return gpio_request_handler def get_name(self): return "gpio" def setup(self, nysa): self.n = nysa self.n.read_sdb() self.urn = self.n.find_device(GPIO)[0] print "Found a GPIO device: %s" % self.urn self.gpio = GPIO(self.n, self.urn) self.gpio.set_port_direction(0x00000003) self.gpio.set_port_raw(0x00000003) time.sleep(0.100) self.gpio.set_port_raw(0x00000000) print "setup gpios"
class OLED(object): #OLED Functions def __init__(self, platform, urn, status): self.status = status spi_urn = urn gpio_urn = platform.get_integration_references(spi_urn)[0] self.status.Debug("GPIO URN: %s" % gpio_urn) self.status.Debug("SPI URN: %s" % spi_urn) self.gpio = GPIO(platform, gpio_urn, status) self.spi = SPI(platform, spi_urn, status) self.status = status #Setup the controller self.platform = platform self.status = status self.spi.set_spi_clock_rate(1000000) self.status.Important("Clock Rate: %d" % self.spi.get_spi_clock_rate()) #self.spi.set_tx_polarity(False) #self.spi.set_rx_polarity(True) self.spi.set_spi_mode(3) self.spi.set_spi_slave_select(0, True) self.spi.auto_ss_control_enable(True) self.spi.set_character_length(8) self.buffer_len = self.spi.get_max_character_length() / 8 self.gpio.set_port_direction(0xFFFFFFFF) #Setup the GPIOs self.set_command_mode() self.enable_vdd(False) self.enable_vbat(False) time.sleep(0.001) self.setup() def reset(self): self.status.Important("Resetting OLED") self.gpio.set_bit_value(RESET, 0) time.sleep(0.01) self.gpio.set_bit_value(RESET, 1) def set_data_mode(self): self.gpio.set_bit_value(DATA_COMMAND_MODE, 1) def set_command_mode(self): self.gpio.set_bit_value(DATA_COMMAND_MODE, 0) def enable_vbat(self, enable): if enable: self.gpio.set_bit_value(VBAT_ENABLE, 0) else: self.gpio.set_bit_value(VBAT_ENABLE, 1) def enable_vdd(self, enable): if enable: self.gpio.set_bit_value(VDD_ENABLE, 0) else: self.gpio.set_bit_value(VDD_ENABLE, 1) def send_command(self, command): #if isinstance(command, list): # command = Array('B', command) #elif isinstance(command, int): # command = Array('B', [command]) command = Array('B', [command]) #print "command: %s" % str(command) self.spi.set_write_data(command) self.spi.start_transaction() #XXX: This should be pushed to a background thread! while self.spi.is_busy(): time.sleep(0.01) def write_buffer(self, buf): column_index = 0 for i in range(4): #Set the Page command self.set_command_mode() self.send_command(0x22) self.send_command(i) #Start from the left column self.send_command(0x00) self.send_command(0x10) self.set_data_mode() self.put_column_buffer(i, buf) def put_column_buffer(self, column_index, buf): pos = 0 column_width = 128 buf_pos = column_index * column_width last_buf_pos = buf_pos + column_width #self.buffer_len for i in range ((column_index * 128), ((column_index * 128) + 128)): #self.send_command(Array('B', [buf[i]])) self.spi.set_write_data([buf[i]]) self.spi.start_transaction() while self.spi.is_busy(): time.sleep(0.01) def setup(self): self.status.Important("Power Up") self.set_command_mode() self.status.Important("Enable VDD") self.enable_vdd(True) time.sleep(0.001) #Disable the Display self.status.Important("Disable display") self.send_command(0xAE) #Reset the display self.status.Important("Reset display") self.reset() #Set the charge pump and set pre-charge period command self.status.Important("Setup charge pump and pre-charge pump") self.send_command(0x8D) self.send_command(0x14) self.send_command(0xD9) self.send_command(0xF1) #Turn on VBAT self.status.Important("Turn on VBAT") self.enable_vbat(True) time.sleep(0.1) #Invert the display self.status.Important("Invert Display") self.send_command(0xA1) #Remap Columns self.send_command(0xC8) #Remap Rows self.status.Important("Set the command to select sequence COMM configuration") self.send_command(0xDA) self.send_command(0x20) self.status.Important("Enable display") self.send_command(0xAF)
class Test (unittest.TestCase): def setUp(self): self.s = Status() self.s.set_level("verbose") plat = ["", None, None] pscanner = PlatformScanner() platform_dict = pscanner.get_platforms() platform_names = platform_dict.keys() if "sim" in platform_names: #If sim is in the platforms, move it to the end platform_names.remove("sim") platform_names.append("sim") urn = None for platform_name in platform_names: if plat[1] is not None: break self.s.Debug("Platform: %s" % str(platform_name)) platform_instance = platform_dict[platform_name](self.s) #self.s.Verbose("Platform Instance: %s" % str(platform_instance)) instances_dict = platform_instance.scan() for name in instances_dict: #s.Verbose("Found Platform Item: %s" % str(platform_item)) n = instances_dict[name] plat = ["", None, None] if n is not None: self.s.Important("Found a nysa instance: %s" % name) n.read_sdb() #import pdb; pdb.set_trace() if n.is_device_in_platform(GPIO): plat = [platform_name, name, n] break continue #self.s.Verbose("\t%s" % psi) if plat[1] is None: self.gpio = None return n = plat[2] urn = n.find_device(GPIO)[0] self.gpio = GPIO(n, urn) self.s.Important("Using Platform: %s" % plat[0]) self.s.Important("Instantiated a GPIO Device: %s" % urn) def test_gpio(self): if self.gpio is None: self.s.Fatal("Cannot Run Test when no device is found!") return self.s.Info ("Testing output ports (like LEDs)") self.s.Info ("Flashing all the outputs for one second") self.s.Info ("Set all the ports to outputs") self.gpio.set_port_direction(0xFFFFFFFF) self.s.Info ("Set all the values to 1s") self.gpio.set_port_raw(0xFFFFFFFF) time.sleep(1) self.s.Info ("Set all the values to 0s") self.gpio.set_port_raw(0x00000000) self.s.Info ("Reading inputs (Like buttons) in 2 second") self.gpio.set_port_direction(0x00000000) time.sleep(2) self.s.Info ("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info ("Reading inputs (Like buttons) in 2 second") time.sleep(2) self.s.Info ("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info ("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.s.Info ("Testing Interrupts, setting interrupts up for positive edge detect") self.s.Info ("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.gpio.set_interrupt_edge(0xFFFFFFFF) self.gpio.set_interrupt_enable(0xFFFFFFFF) self.s.Info ("Waiting for 5 seconds for the interrupts to fire") if self.gpio.wait_for_interrupts(5): self.s.Info ("Interrupt detected!\n") #if self.gpio.is_interrupt_for_slave(): self.s.Info ("Interrupt for GPIO detected!") self.s.Info ("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.s.Info ("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info ("Interrupts: 0x%08X" % self.gpio.get_interrupts())
class Test(unittest.TestCase): def setUp(self): self.s = Status() self.s.set_level("verbose") plat = ["", None, None] pscanner = PlatformScanner() platform_dict = pscanner.get_platforms() platform_names = platform_dict.keys() if "sim" in platform_names: #If sim is in the platforms, move it to the end platform_names.remove("sim") platform_names.append("sim") urn = None for platform_name in platform_names: if plat[1] is not None: break self.s.Debug("Platform: %s" % str(platform_name)) platform_instance = platform_dict[platform_name](self.s) #self.s.Verbose("Platform Instance: %s" % str(platform_instance)) instances_dict = platform_instance.scan() for name in instances_dict: #s.Verbose("Found Platform Item: %s" % str(platform_item)) n = instances_dict[name] plat = ["", None, None] if n is not None: self.s.Important("Found a nysa instance: %s" % name) n.read_sdb() #import pdb; pdb.set_trace() if n.is_device_in_platform(GPIO): plat = [platform_name, name, n] break continue #self.s.Verbose("\t%s" % psi) if plat[1] is None: self.gpio = None return n = plat[2] urn = n.find_device(GPIO)[0] self.gpio = GPIO(n, urn) self.s.Important("Using Platform: %s" % plat[0]) self.s.Important("Instantiated a GPIO Device: %s" % urn) def test_gpio(self): if self.gpio is None: self.s.Fatal("Cannot Run Test when no device is found!") return self.s.Info("Testing output ports (like LEDs)") self.s.Info("Flashing all the outputs for one second") self.s.Info("Set all the ports to outputs") self.gpio.set_port_direction(0xFFFFFFFF) self.s.Info("Set all the values to 1s") self.gpio.set_port_raw(0xFFFFFFFF) time.sleep(1) self.s.Info("Set all the values to 0s") self.gpio.set_port_raw(0x00000000) self.s.Info("Reading inputs (Like buttons) in 2 second") self.gpio.set_port_direction(0x00000000) time.sleep(2) self.s.Info("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info("Reading inputs (Like buttons) in 2 second") time.sleep(2) self.s.Info("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.s.Info( "Testing Interrupts, setting interrupts up for positive edge detect" ) self.s.Info("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.gpio.set_interrupt_edge(0xFFFFFFFF) self.gpio.set_interrupt_enable(0xFFFFFFFF) self.s.Info("Waiting for 5 seconds for the interrupts to fire") if self.gpio.wait_for_interrupts(5): self.s.Info("Interrupt detected!\n") #if self.gpio.is_interrupt_for_slave(): self.s.Info("Interrupt for GPIO detected!") self.s.Info("Interrupts: 0x%08X" % self.gpio.get_interrupts()) self.s.Info("Read value: 0x%08X" % self.gpio.get_port_raw()) self.s.Info("Interrupts: 0x%08X" % self.gpio.get_interrupts())