kopia lustrzana https://github.com/pimoroni/pimoroni-pico
				
				
				
			Merge pull request #655 from pimoroni/patch/i75-userbutton
i75/i75w: Auto-detect board and set up user button.patch/inventor_docs
						commit
						d0e2425e07
					
				|  | @ -13,8 +13,11 @@ PicoGraphics_PenRGB888 graphics(hub75.width, hub75.height, nullptr); | |||
| 
 | ||||
| // And each button
 | ||||
| Button button_a(Interstate75::A); | ||||
| Button button_b(Interstate75::B); | ||||
| // For the Interstate75
 | ||||
| // Button button_b(Interstate75::BOOT); // Using this button definition on the Interstate75W will most likely disable the wifi make sure it is commented out if using Interstate75W
 | ||||
| 
 | ||||
| // Or for the Interstate75W
 | ||||
| Button button_b(Interstate75::B); // This button is not present on the Interstate75 (non W version)
 | ||||
| // RGB LED
 | ||||
| RGBLED led(Interstate75::LED_R, Interstate75::LED_G, Interstate75::LED_B, ACTIVE_LOW); | ||||
| 
 | ||||
|  |  | |||
|  | @ -27,6 +27,9 @@ PicoGraphics_PenRGB888 graphics(hub75.width, hub75.height, nullptr); | |||
| 
 | ||||
| // And each button | ||||
| Button button_a(Interstate75::A); | ||||
| // For the Interstate75 | ||||
| Button button_b(Interstate75::BOOT); | ||||
| // Or for the Interstate75W | ||||
| Button button_b(Interstate75::B); | ||||
| 
 | ||||
| // RGB LED | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ namespace pimoroni { | |||
|   namespace Interstate75{ | ||||
|     static const uint8_t A = 14; | ||||
|     static const uint8_t B = 15; | ||||
|     static const uint8_t BOOT = 23; | ||||
|     static const uint8_t LED_R = 16; | ||||
|     static const uint8_t LED_G = 17; | ||||
|     static const uint8_t LED_B = 18; | ||||
|  |  | |||
|  | @ -0,0 +1,49 @@ | |||
| ''' | ||||
| buttons.py | ||||
| Push either switch A, switch B or the BOOT switch (in the case of the non-w version) to change the display | ||||
| ''' | ||||
| import interstate75 | ||||
| 
 | ||||
| i75 = interstate75.Interstate75(display=interstate75.DISPLAY_INTERSTATE75_32X32) | ||||
| graphics = i75.display | ||||
| 
 | ||||
| width = i75.width | ||||
| height = i75.height | ||||
| 
 | ||||
| A_COLOR = graphics.create_pen(0x31, 0x81, 0xCE) | ||||
| A_TEXT = graphics.create_pen(0xCE, 0x7E, 0x31) | ||||
| 
 | ||||
| B_COLOR = graphics.create_pen(0xC3, 0x3C, 0xBD) | ||||
| B_TEXT = graphics.create_pen(0x3C, 0xC3, 0x42) | ||||
| 
 | ||||
| BOOT_COLOR = graphics.create_pen(0xC3, 0x3C, 0xBD) | ||||
| BOOT_TEXT = graphics.create_pen(0x3C, 0xC3, 0x42) | ||||
| 
 | ||||
| BG = graphics.create_pen(0xC1, 0x99, 0x3E) | ||||
| 
 | ||||
| 
 | ||||
| def display_a(): | ||||
|     graphics.set_pen(A_COLOR) | ||||
|     graphics.clear() | ||||
|     graphics.set_pen(A_TEXT) | ||||
|     graphics.text("A", 8, 6, False, 3) | ||||
|     i75.update() | ||||
| 
 | ||||
| 
 | ||||
| def display_b(): | ||||
|     graphics.set_pen(B_COLOR) | ||||
|     graphics.clear() | ||||
|     graphics.set_pen(B_TEXT) | ||||
|     graphics.text("B", 8, 6, False, 3) | ||||
|     i75.update() | ||||
| 
 | ||||
| 
 | ||||
| graphics.set_pen(BG) | ||||
| graphics.clear() | ||||
| i75.update() | ||||
| 
 | ||||
| while 1: | ||||
|     if i75.switch_pressed(interstate75.SWITCH_A): | ||||
|         display_a() | ||||
|     if i75.switch_pressed(interstate75.SWITCH_B): | ||||
|         display_b() | ||||
|  | @ -1,6 +1,6 @@ | |||
| ''' | ||||
| buttons.py | ||||
| Push either switch A or switch B to change the display | ||||
| Push either switch A, switch B or the BOOT switch (in the case of the non-w version) to change the display | ||||
| ''' | ||||
| import interstate75 | ||||
| 
 | ||||
|  | @ -16,6 +16,9 @@ A_TEXT = graphics.create_pen(0xCE, 0x7E, 0x31) | |||
| B_COLOR = graphics.create_pen(0xC3, 0x3C, 0xBD) | ||||
| B_TEXT = graphics.create_pen(0x3C, 0xC3, 0x42) | ||||
| 
 | ||||
| BOOT_COLOR = graphics.create_pen(0xC3, 0x3C, 0xBD) | ||||
| BOOT_TEXT = graphics.create_pen(0x3C, 0xC3, 0x42) | ||||
| 
 | ||||
| BG = graphics.create_pen(0xC1, 0x99, 0x3E) | ||||
| 
 | ||||
| 
 | ||||
|  | @ -27,11 +30,11 @@ def display_a(): | |||
|     i75.update() | ||||
| 
 | ||||
| 
 | ||||
| def display_b(): | ||||
|     graphics.set_pen(B_COLOR) | ||||
| def display_boot(): | ||||
|     graphics.set_pen(BOOT_COLOR) | ||||
|     graphics.clear() | ||||
|     graphics.set_pen(B_TEXT) | ||||
|     graphics.text("B", 8, 6, False, 3) | ||||
|     graphics.set_pen(BOOT_TEXT) | ||||
|     graphics.text("BOOT", 5, 11, False, 1) | ||||
|     i75.update() | ||||
| 
 | ||||
| 
 | ||||
|  | @ -42,5 +45,5 @@ i75.update() | |||
| while 1: | ||||
|     if i75.switch_pressed(interstate75.SWITCH_A): | ||||
|         display_a() | ||||
|     if i75.switch_pressed(interstate75.SWITCH_B): | ||||
|         display_b() | ||||
|     if i75.switch_pressed(interstate75.SWITCH_BOOT): | ||||
|         display_boot() | ||||
|  |  | |||
|  | @ -4,9 +4,10 @@ This library offers convenient functions for interacting with [Interstate75](htt | |||
| 
 | ||||
| ## Table of Content | ||||
| - [Table of Content](#table-of-content) | ||||
| - [Interstate75 Module](#interstate75-class) | ||||
| - [Interstate75 Class](#interstate75-class) | ||||
|   - [Switches](#switches) | ||||
|   - [RGB LED](#rgb-led) | ||||
| - [Display](#display) | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | @ -14,7 +15,18 @@ This library offers convenient functions for interacting with [Interstate75](htt | |||
| 
 | ||||
| The `Interstate75` class deals with RGB LED and buttons on the Interstate75 and 75W. To create one, import the `interstate75` module, then define a new `board` variable. | ||||
| 
 | ||||
| This is where you define the HUB75 matrix display size that you wish to use by defining `display=` | ||||
| 
 | ||||
| ```python | ||||
| import interstate75 | ||||
| 
 | ||||
| display = interstate75.DISPLAY_INTERSTATE75_32X32 | ||||
| 
 | ||||
| board = interstate75.Interstate75(display=display) | ||||
| ``` | ||||
| 
 | ||||
| The version of Intersate75 you're using should be automatically detected. Check `board.interstate75w` to verify this. It should be `True` on a W and `False` on a non-W. | ||||
| 
 | ||||
| You can choose the HUB75 matrix display size that you wish to use by defining `display=` as one of the following: | ||||
| 
 | ||||
| ```python | ||||
| DISPLAY_INTERSTATE75_32X32 | ||||
|  | @ -27,17 +39,6 @@ DISPLAY_INTERSTATE75_192X64 | |||
| DISPLAY_INTERSTATE75_256X64 | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
| ```python | ||||
| import interstate75 | ||||
| 
 | ||||
| display = interstate75.DISPLAY_INTERSTATE75_32X32 | ||||
| 
 | ||||
| board = interstate75.Interstate75(display=display) | ||||
| ``` | ||||
| 
 | ||||
| From here, all features can be accessed by calling functions on `board`. In addition, when using Qwiic / Stemma QT devices, the I2C channel to use can be accessed with `board.i2c`. | ||||
| 
 | ||||
| ### Switches | ||||
| 
 | ||||
| Interstate75 and 75W have two switches in the front of the board. To read one of the switches, call `.switch_pressed(switch)`, where `switch` is a value from `0` to `.NUM_SWITCHES - 1`. This returns `True` when the specified switch is pressed, and `False` otherwise. | ||||
|  | @ -47,12 +48,22 @@ To read a specific input, the `interstate75` module contains these handy constan | |||
| * `SWITCH_A` = `0` | ||||
| * `SWITCH_B` = `1` | ||||
| 
 | ||||
| The Interstate75 (non W) uses the boot button instead of `SWITCH_B` | ||||
| 
 | ||||
| * `SWITCH_A` = `0` | ||||
| * `SWITCH_BOOT` = `1` | ||||
| 
 | ||||
| ```python | ||||
| if board.switch_pressed(SWITCH_A): | ||||
|   # Do something interesting here! | ||||
| 
 | ||||
| # Either for Interstate 75W | ||||
| if board.switch_pressed(SWITCH_B): | ||||
|   # Do something else even more interesting here! | ||||
| 
 | ||||
| # Or for Interstate 75 | ||||
| if board.switch_pressed(SWITCH_BOOT): | ||||
|   # Do something else even more interesting here! | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -2,16 +2,19 @@ from pimoroni import RGBLED, Button | |||
| from picographics import PicoGraphics, DISPLAY_INTERSTATE75_32X32, DISPLAY_INTERSTATE75_64X32, DISPLAY_INTERSTATE75_96X32, DISPLAY_INTERSTATE75_128X32, DISPLAY_INTERSTATE75_64X64, DISPLAY_INTERSTATE75_128X64, DISPLAY_INTERSTATE75_192X64, DISPLAY_INTERSTATE75_256X64 | ||||
| from pimoroni_i2c import PimoroniI2C | ||||
| import hub75 | ||||
| import sys | ||||
| 
 | ||||
| # Index Constants | ||||
| SWITCH_A = 0 | ||||
| SWITCH_B = 1 | ||||
| SWITCH_BOOT = 1 | ||||
| 
 | ||||
| 
 | ||||
| class Interstate75: | ||||
|     I2C_SDA_PIN = 20 | ||||
|     I2C_SCL_PIN = 21 | ||||
|     SWITCH_PINS = (14, 15) | ||||
|     SWITCH_PINS = (14, 23) | ||||
|     SWITCH_PINS_W = (14, 15) | ||||
|     LED_R_PIN = 16 | ||||
|     LED_G_PIN = 17 | ||||
|     LED_B_PIN = 18 | ||||
|  | @ -39,15 +42,20 @@ class Interstate75: | |||
|     NUM_SWITCHES = 2 | ||||
| 
 | ||||
|     def __init__(self, display, panel_type=hub75.PANEL_GENERIC, stb_invert=False, color_order=hub75.COLOR_ORDER_RGB): | ||||
|         self.interstate75w = "Pico W" in sys.implementation._machine | ||||
|         self.display = PicoGraphics(display=display) | ||||
|         self.width, self.height = self.display.get_bounds() | ||||
|         self.hub75 = hub75.Hub75(self.width, self.height, panel_type=panel_type, stb_invert=stb_invert, color_order=color_order) | ||||
|         self.hub75.start() | ||||
|         if self.interstate75w: | ||||
|             self._switch_pins = self.SWITCH_PINS_W | ||||
|         else: | ||||
|             self._switch_pins = self.SWITCH_PINS | ||||
| 
 | ||||
|         # Set up the user switches | ||||
|         self.__switches = [] | ||||
|         for i in range(self.NUM_SWITCHES): | ||||
|             self.__switches.append(Button(self.SWITCH_PINS[i])) | ||||
|             self.__switches.append(Button(self._switch_pins[i])) | ||||
| 
 | ||||
|         self.__rgb = RGBLED(Interstate75.LED_R_PIN, Interstate75.LED_G_PIN, Interstate75.LED_B_PIN, invert=True) | ||||
| 
 | ||||
|  | @ -61,7 +69,7 @@ class Interstate75: | |||
| 
 | ||||
|     def switch_pressed(self, switch): | ||||
|         if switch < 0 or switch >= self.NUM_SWITCHES: | ||||
|             raise ValueError("switch out of range. Expected SWITCH_A (0), SWITCH_B (1)") | ||||
|             raise ValueError("switch out of range. Expected SWITCH_A (0), SWITCH_B/BOOT (1)") | ||||
|         return self.__switches[switch].is_pressed | ||||
| 
 | ||||
|     def set_led(self, r, g, b): | ||||
|  |  | |||
		Ładowanie…
	
		Reference in New Issue
	
	 Hel Gibbons
						Hel Gibbons