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"
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 __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 _initialize(self, platform, urn): self.v = GPIOWidget(count = 32, gpio_actions = self.gpio_actions) self.gpio = GPIO(platform, urn, debug = False) #Initialize the thread with a 40mS timeout self.v.add_register(0, "Value", initial_value = self.gpio.get_port_raw()) self.v.add_register(1, "Direction", initial_value = self.gpio.get_port_direction()) self.v.add_register(2, "Interrupts", initial_value = self.gpio.get_interrupts()) self.v.add_register(3, "Interrupt Enable", initial_value = self.gpio.get_interrupt_enable()) self.v.add_register(4, "Interrupt Edge", initial_value = self.gpio.get_interrupt_edge()) self.v.add_register(5, "Interrupt Both Edge", initial_value = self.gpio.get_interrupt_both_edge()) self.v.add_register(6, "Interrupt Timeout", initial_value = self.gpio.get_interrupt_timeout()) self.v.add_register(7, "Read Clock Rate", initial_value = self.gpio.get_clock_rate()) self.gpio.register_interrupt_callback(self.interrupt_callback)
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())
class Controller(NysaBaseController): @staticmethod def get_name(): return APP_NAME @staticmethod def get_driver(): return DRIVER def __init__(self): super (Controller, self).__init__() self.gpio_actions = GPIOActions() self.gpio_actions.get_pressed.connect(self.register_get_pressed) self.gpio_actions.set_pressed.connect(self.register_set_pressed) self.gpio_actions.gpio_out_changed.connect(self.gpio_out_changed) self.gpio_actions.direction_changed.connect(self.direction_changed) self.gpio_actions.interrupt_en_changed.connect(self.interrupt_en_changed) self.gpio_actions.interrupt_edge_changed.connect(self.interrupt_edge_changed) self.gpio_actions.interrupt_both_edge_changed.connect(self.interrupt_both_edge_changed) self.gpio_actions.read_start_stop.connect(self.read_start_stop) self.gpio_actions.gpio_input_changed.connect(self.gpio_input_changed) self.gpio_actions.gpio_interrupt.connect(self.process_interrupts) def _initialize(self, platform, urn): self.v = GPIOWidget(count = 32, gpio_actions = self.gpio_actions) self.gpio = GPIO(platform, urn, debug = False) #Initialize the thread with a 40mS timeout self.v.add_register(0, "Value", initial_value = self.gpio.get_port_raw()) self.v.add_register(1, "Direction", initial_value = self.gpio.get_port_direction()) self.v.add_register(2, "Interrupts", initial_value = self.gpio.get_interrupts()) self.v.add_register(3, "Interrupt Enable", initial_value = self.gpio.get_interrupt_enable()) self.v.add_register(4, "Interrupt Edge", initial_value = self.gpio.get_interrupt_edge()) self.v.add_register(5, "Interrupt Both Edge", initial_value = self.gpio.get_interrupt_both_edge()) self.v.add_register(6, "Interrupt Timeout", initial_value = self.gpio.get_interrupt_timeout()) self.v.add_register(7, "Read Clock Rate", initial_value = self.gpio.get_clock_rate()) self.gpio.register_interrupt_callback(self.interrupt_callback) def start_tab_view(self, platform, urn, status): self.status = status self.status.Verbose("Thread name: %s" % QThread.currentThread().objectName()) self._initialize(platform, urn) def get_view(self): return self.v def read_start_stop(self, start_stop, rate): self.status.Verbose("Enter Read/startstop") def gpio_input_changed(self, value): self.status.Verbose("Input Changed") #Input Changed self.v.set_register(0, value) def register_get_pressed(self, index): self.status.Verbose("Register Get Pressed: %d" % index) value = self.gpio.read_register(index) self.v.set_register(index, value) def register_set_pressed(self, index, value): self.status.Verbose("Register Set Pressed: %d: %d" % (index, value)) self.gpio.write_register(index, value) def gpio_out_changed(self, index, val): self.status.Verbose( "GPIO Out: %d : %s" % (index, str(val))) self.gpio.set_bit_value(index, val) def direction_changed(self, index, value): self.status.Verbose( "GPIO Direction: %d : %s" % (index, str(value))) self.gpio.enable_register_bit(1, index, value) def interrupt_en_changed(self, index, value): self.status.Verbose("Interrupt En Changed: %d : %s" % (index, str(value))) self.gpio.enable_register_bit(3, index, value) def interrupt_edge_changed(self, index, value): self.status.Verbose("Interrupt Edge Changed %d : %s" % (index, str(value))) self.gpio.enable_register_bit(4, index, value) def interrupt_both_edge_changed(self, index, value): self.status.Verbose("Interrupt Both Edges Changed %d: %s" % (index, str(value))) self.gpio.enable_register_bit(5, index, value) def process_interrupts(self): self.status.Verbose("Process interrupts") value = self.gpio.get_port_raw() interrupts = self.gpio.get_interrupts() self.v.set_register(2, interrupts) self.gpio_actions.gpio_input_changed.emit(value) def interrupt_callback(self): self.status.Verbose("Received interrupt callback") self.gpio_actions.gpio_interrupt.emit()