def activate(): # set Pins sclPin = machine.Pin(15) sdaPin = machine.Pin(4) rstPin = machine.Pin(16, machine.Pin.OUT) # set width and height oledWidth = 128 oledHeight = 64 # set RST to high # https://forum.micropython.org/viewtopic.php?t=4002 rstPin.value(1) # initialize Display i2c = machine.I2C(scl=sclPin, sda=sdaPin) global oled oled = ssd1306.SSD1306_I2C(oledWidth, oledHeight, i2c) global graphics graphics = gfx.GFX(oledWidth, oledHeight, oled.pixel) # OLED schwarz und wenig Kontrast # oled.contrast(0) wenig <--> oled.contrast(255) max oled.contrast(0) oled.fill(0) oled.show()
def __init__(self, i2c): ssd1306.SSD1306_I2C.__init__(self, 128, 64, i2c) self.gfx = gfx.GFX(128, 64, self.pixel, hline=self.hline, vline=self.vline)
def main(): spi = machine.SPI(1, baudrate=8000000, polarity=1, phase=1, sck=Pin(18, Pin.OUT, Pin.PULL_DOWN), mosi=Pin(23, Pin.OUT, Pin.PULL_UP), miso=Pin(19, Pin.IN, Pin.PULL_UP)) display = st7789.ST7789( spi, 240, 240, reset=Pin(2, machine.Pin.OUT, Pin.PULL_UP), dc=Pin(4, machine.Pin.OUT, Pin.PULL_UP), ) display.init() display.fill(st7789.color565(128, 128, 255)) graphics = gfx.GFX(240, 240, display.pixel) graphics.fill_rect(50, 50, 50, 50, st7789.color565(0, 0, 0)) print(mem_free()) collect() print(mem_free()) while True: display.fill( st7789.color565( random.getrandbits(8), random.getrandbits(8), random.getrandbits(8), ), ) # Pause 2 seconds. time.sleep(0.5)
def run_game(): """Init game and run game""" # init config CFG() # pre-init sound pygame.mixer.pre_init(44100, -16, 2, 2048) # init pygame and screen pygame.init() if CFG().fullscreen: screen = pygame.display.set_mode((CFG().screen_width, CFG().screen_height), pygame.FULLSCREEN) else: screen = pygame.display.set_mode((CFG().screen_width, CFG().screen_height)) pygame.display.set_caption('Space War 2027') # Internal screen int_screen = pygame.Surface((CFG().int_screen_width, CFG().int_screen_height)) show_loading(screen) # Calculate internal scaling scale = CFG().screen_width / float(CFG().int_screen_width) CFG().int_scale_width = int(CFG().int_screen_width * scale) CFG().int_scale_height = int(CFG().int_screen_height * scale) # Init sound sfx.SFX() # Init graphics gfx.GFX() # Init game clock clock = pygame.time.Clock() # Status status = GameStatus() # Init menu menu = Menu(int_screen, status) # Init game itself game = Game(int_screen, status) # Init events Events() hud = HUD(status, screen, clock) # Main loop while True: dt = clock.tick(60) # time between frames, should alter speed of everything that is not based on real time Events().get_events() if status.game_running: if game.update(dt): # If update is true level ended -> start new level game = Game(int_screen, status) game.draw() status.update() else: menu.update(game, dt) menu.draw() update_screen(screen, int_screen, hud)
def __init__(self): self.fsr = ADC(Pin(34)) self.fsr.atten(ADC.ATTN_11DB) self.encoder = RotaryEncoder() self.oled = ssd1306.SSD1306_I2C( 128, 64, I2C(-1, scl=Pin(22), sda=Pin(21))) self.graphics = gfx.GFX(128, 64, self.oled.pixel) self.encoder.rotary_encoder.set(value=random.randint(-50, 50)) self.vibration = Vibration()
def __init__(self, i2c, width=128, height=32, mqtt_topic=None): super().__init__(i2c, width, height, mqtt_topic) try: import gfx self.gfx = gfx.GFX(width, height, self.pixel, hline=self._fast_hline, vline=self._fast_vline) except Exception as e: _log.critical( "Could not import gfx module, SSD1306_complex not possible, error {!s}" .format(e))
# Optionally create faster horizontal and vertical line drawing functions using # the display's native filled rectangle function (which updates chunks of memory # instead of pixel by pixel). def fast_hline(x, y, width, color): display.fill_rectangle(x, y, width, 1, color) def fast_vline(x, y, height, color): display.fill_rectangle(x, y, 1, height, color) # Initialize the GFX library, giving it the display pixel function as its pixel # drawing primitive command. The hline and vline parameters specify optional # optimized horizontal and vertical line drawing functions. You can remove these # to see how much slower the filled shape functions perform! graphics = gfx.GFX(240, 320, display.pixel, hline=fast_hline, vline=fast_vline) # Now loop forever drawing different primitives. while True: # Clear screen and draw a red line. display.fill(0) graphics.line(0, 0, 239, 319, ili9341.color565(255, 0, 0)) time.sleep(2) # Clear screen and draw a green rectangle. display.fill(0) graphics.rect(0, 0, 120, 160, ili9341.color565(0, 255, 0)) time.sleep(2) # Clear screen and draw a filled green rectangle. display.fill(0) graphics.fill_rect(0, 0, 120, 160, ili9341.color565(0, 255, 0)) time.sleep(2)
from machine import Pin, I2C import ssd1306 import gfx from time import sleep # initialize globals WIDTH = 64 HEIGHT = 48 # I2C, oled dependencies i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) #i2c.scan() #[60] oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c) #oled.fill(0) # blank oled #oled.show() graphics = gfx.GFX(WIDTH,HEIGHT,oled.pixel) # Base object class class Thing: # Initializes the object with coordinates, size and color def __init__(self, x, y, w, h, color): self.x = x self.y = y self.w = w self.h = h self.vx = 0 self.vy = 0 self.color = color # Updates object by moving it and checking if it's in screen range
#pin16 = machine.Pin(16, machine.Pin.OUT) # NOTE: On an Heltec Wifi Kit 32 you have to set pin 16 to high to turn on the screen #pin16.value(1) # # And Configure the screen i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin( 21)) # 22 and 21 are the SCL and SDA pins the screens are connected to oled1 = ssd1306.SSD1306_I2C( 128, 64, i2c, addr=60 ) # 128,64 defines the screen width and height of the first oled, addr is the address of the screen oled2 = ssd1306.SSD1306_I2C( 128, 64, i2c, addr=61) # 128,64 defines the screen width and height of the second oled # # Setup a graphic drawing on both OLED displays, We'll use that later to draw on the screens import gfx graphics1 = gfx.GFX(128, 64, oled1.pixel) graphics2 = gfx.GFX(128, 64, oled2.pixel) # # Let's put some text on the screen as we get started oled1.fill(0) oled1.text('COVID VAC COUNT', 0, 0) oled1.text('ID: ' + device_id, 0, 20) oled1.text('By: Cas Hoefman', 0, 30) oled1.text('Micropython 1.13', 0, 40) oled1.show() # # And on the second screen oled2.fill(0) oled2.text('Starting...', 0, 0) oled2.show() #
import gfx, ssd1306, machine from time import sleep i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) graphics = gfx.GFX(128, 64, oled.pixel) oled.text('Jeroen van Oorschot', 10, 10) oled.show() adc = machine.ADC(machine.Pin(39)) raw = 0 att = [ machine.ADC.ATTN_0DB, machine.ADC.ATTN_2_5DB, machine.ADC.ATTN_6DB, machine.ADC.ATTN_11DB ] atti = 0 cal = [ # 100, 2000, 4000 [0.75, 4.28, 7.95], [0.814, 5.52, 10.37], [1.09, 7.64, 14.35], [1.65, 13.73, 25.6], # actual 24.4 but very nonlinear at the end of this regime ] while True: oled.fill(0) # raw = (raw + adc.read()) / 2.0 raw = adc.read() if raw > 4000: if atti < 3: atti += 1
cs=Pin(2)) utime.sleep(1) display.sleep(False) display.rotate(1) display2.sleep(False) display2.rotate(1) utime.sleep(1) i2c = I2C(scl=Pin(15), sda=Pin(4)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) utime.sleep(1) graphics = gfx.GFX(128, 64, oled.pixel) graphics1 = gfx.GFX(128, 64, display.pixel) graphics2 = gfx.GFX(128, 64, display2.pixel) oled.fill(0) oled.text('oled init', 0, 0, 1) graphics.circle(63, 32, 10, 1) display.fill(0) display.text('display init', 0, 0, 1) graphics1.circle(63, 32, 10, 1) display2.fill(0) display2.text('display2 init', 0, 0, 1) graphics2.circle(63, 32, 10, 1)
sleep_ms(500) oledReset.value(1) oled_width = 128 oled_height = 64 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) oled.text('Hello, World 1!', 0, 0) oled.text('Hello, World 2!', 0, 10) oled.text('Hello, World 3!', 0, 20) oled.show() sleep_ms(1000) oled.fill(0) graphics = gfx.GFX(oled_width, oled_height, oled.pixel) graphics.fill_circle(64, 16, 16, 1) oled.show() sleep_ms(1000) oled.fill(1) oled.show() sleep_ms(1000) for number in range(15): oled.fill(0) bignumber.bigNumber(oled, number) oled.show() sleep_ms(10) oled.fill(0)
import machine, ssd1306, gfx import bitmapfont i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5), freq=100000) oled = ssd1306.SSD1306_I2C(128,32, i2c) graphics = gfx.GFX(128, 32, oled.pixel) #bf = bitmapfont.BitmapFont(128, 32, oled.pixel) #bf.init() oled.fill(0) oled.show() graphics.line(0, 0, 127, 23, 1) graphics.rect(0, 0, 120, 30, 1) graphics.fill_rect(0, 0, 40, 30, 1) graphics.circle(60, 16, 15, 1) graphics.fill_circle(60, 16, 10, 1) graphics.triangle(60, 10, 100, 25, 20, 25, 1) graphics.fill_triangle(60, 10, 80, 25, 40, 25, 1) #bf.text('Ada', 0, 0, 100) oled.show()
oled.fill(0) oled_width = 128 oled_height = 64 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) oled.invert(False) # from the examples # def fast_hline(x, y, width, color): # display.fill_rectangle(x, y, width, 1, color) # # def fast_vline(x, y, height, color): # display.fill_rectangle(x, y, 1, height, color) graphics = gfx.GFX( oled_width, oled_width, oled.pixel) # reminder: oled.pixel = function that draws pixels. def demo(): oled.text('Hello main 2!', 0, 0) oled.show() sleep(2) reset() while True: demo()
rightMotor = motors.Motor(2) lcd = LCD() def fast_hline(x, y, width, color): lcd.fill(x, y, width, 1, color) def fast_vline(x, y, height, color): lcd.fill(x, y, 1, height, color) graphics = gfx.GFX(lcdWidth, lcdHeight, lcd.pixel, hline=fast_hline, vline=fast_vline) lcd.fill(0, 0, lcdWidth, lcdHeight, bgColor) graphics.rect(8, 8, lcdWidth - 16, lcdHeight - 16, textColor) lcd.text("Hello STEMBot 2!", 100, 100, textColor) time.sleep_ms(250) #intro screen delay tune = RTTTL('14:d=4,o=4,b=240:c,e,g,e,f,e,d,e,c') #buzzer.play(tune) pinUp = Pin('E5', Pin.IN, Pin.PULL_UP) pinDown = Pin('B6', Pin.IN, Pin.PULL_UP) pinSel = Pin('B2', Pin.IN, Pin.PULL_UP) #middle button below LCD
def createGFX(display): graphics = gfx.GFX(display.width, display.height, display.pixel) return graphics
scl2 = Pin(19) sda2 = Pin(18) vert = ADC(0) hors = ADC(1) clr = Pin(9, Pin.IN) print(uos.uname()) print("Freq: " + str(machine.freq()) + " Hz") print("128x64 SSD1306 I2C OLED on Raspberry Pi Pico") if i2c0_avail : i2c1 = I2C(0, scl= scl1, sda= sda1, freq= 2000000) print("Available i2c 1st BUS devices: "+ str(i2c1.scan())) oled1 = ssd1306n.SSD1306_I2C(i2c1) oled1.fill(0) graphics1 = gfx.GFX(oled1.pixel,128,64) if i2c1_avail : i2c2 = I2C(1, scl= scl2, sda= sda2, freq= 2000000) print("Available i2c 2nd BUS devices: "+ str(i2c2.scan())) oled2 = ssd1306n.SSD1306_I2C(i2c2,addr=60) oled2.fill(0) graphics2 = gfx.GFX(oled2.pixel, 128, 64) if i2c0_avail: n = 1 x1 = 1 while n==1: y = int((64/655)*ADC.read_u16(hors)/100)
from machine import I2C, Pin import ssd1306 import gfx import time import gc i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000) display = ssd1306.SSD1306_I2C(128, 64, i2c) graphics = gfx.GFX(128, 64, display.pixel) display.fill(0) while True: # Clear screen and draw a red line. display.fill(0) graphics.line(0, 0, 128, 64, 1) graphics.line(0, 64, 128, 0, 1) display.show() time.sleep(.5) # Clear screen and draw a green rectangle. display.fill(0) graphics.rect(0, 0, 128, 64, 1) graphics.circle(60, 30, 20, 1) graphics.triangle(60, 5, 45, 40, 15, 40, 1) display.show() time.sleep(.5) # Clear screen and draw a filled green rectangle. display.fill(0) graphics.fill_rect(0, 0, 128, 64, 1) display.show() time.sleep(.5) # Clear screen and draw a blue circle.