Esempio n. 1
0
    def __init__(self, lcd_dev):
        self.logger = logging.getLogger("{}_{}".format(
            __name__,
            lcd_dev.unique_id.split('-')[0]))

        self.disp = None
        self.interface = lcd_dev.interface
        self.lcd_x_characters = lcd_dev.x_characters
        self.lcd_y_lines = lcd_dev.y_lines
        self.lcd_type = lcd_dev.lcd_type

        if self.interface == 'I2C':
            if self.lcd_type == '128x32_pioled_circuit_python':
                self.disp = adafruit_ssd1306.SSD1306_I2C(
                    128,
                    32,
                    ExtendedI2C(lcd_dev.i2c_bus),
                    addr=int(str(lcd_dev.location), 16))
            elif self.lcd_type == '128x64_pioled_circuit_python':
                self.disp = adafruit_ssd1306.SSD1306_I2C(
                    128,
                    64,
                    ExtendedI2C(lcd_dev.i2c_bus),
                    addr=int(str(lcd_dev.location), 16))

        elif self.interface == 'SPI':
            if self.lcd_type == '128x32_pioled_circuit_python':
                import Adafruit_GPIO.SPI as SPI
                self.disp = adafruit_ssd1306.SSD1306_SPI(
                    128,
                    32,
                    spi=SPI.SpiDev(lcd_dev.spi_bus, lcd_dev.spi_device),
                    dc=lcd_dev.pin_dc,
                    reset=lcd_dev.pin_reset,
                    cs=lcd_dev.pin_cs)
            elif self.lcd_type == '128x64_pioled_circuit_python':
                import Adafruit_GPIO.SPI as SPI
                self.disp = adafruit_ssd1306.SSD1306_SPI(
                    128,
                    64,
                    spi=SPI.SpiDev(lcd_dev.spi_bus, lcd_dev.spi_device),
                    dc=lcd_dev.pin_dc,
                    reset=lcd_dev.pin_reset,
                    cs=lcd_dev.pin_cs)

        if not self.disp:
            self.logger.error(
                "Unable to set up display. Check the LCD settings.")
Esempio n. 2
0
    def init_display(self):

        # Create the SPI interface.
        spi = busio.SPI(board.SCK, MOSI=board.MOSI)

        # define display connections
        reset_pin = digitalio.DigitalInOut(board.D4)
        cs_pin = digitalio.DigitalInOut(board.D5)
        dc_pin = digitalio.DigitalInOut(board.D6)

        # Create the SSD1306 OLED class.
        disp = adafruit_ssd1306.SSD1306_SPI(128, 32, spi, dc_pin, reset_pin,
                                            cs_pin)

        # Clear display.
        disp.fill(0)
        disp.show()

        # Create blank image for drawing.
        # Make sure to create image with mode '1' for 1-bit color.
        width = disp.width
        height = disp.height
        image = Image.new("1", (width, height))

        # Get drawing object to draw on image.
        draw = ImageDraw.Draw(image)

        # Alternatively load a TTF font.  Make sure the .ttf font file is in the
        # Some other nice fonts to try: http://www.dafont.com/bitmap.php
        font = ImageFont.truetype(FONT_DIRECTORY + 'bold.ttf', FONT_SIZE)

        return {'display': disp, 'canvas': image, 'draw': draw, 'font': font}
Esempio n. 3
0
def oled_factory():
    return adafruit_ssd1306.SSD1306_SPI(128,
                                        64,
                                        spi=board.SPI(),
                                        dc=digitalio.DigitalInOut(board.D7),
                                        reset=digitalio.DigitalInOut(
                                            board.D25),
                                        cs=digitalio.DigitalInOut(board.D8))
Esempio n. 4
0
def other_sounds():
	with open('tmp.wav','wb') as f:
        f.write(audio.get_wav_data())
	l_audio, _ = librosa.load('tmp.wav', mono=True, sr=None)
	oled.text(predict(l_audio, model), 1, 55, 1)

LENGTH = 6
HEIGHT = 9
MAX_CHARS = 128 // LENGTH
PATH_TO_MODEL = 'model.h5'
LABELS = ['Car Horn', 'Dog Bark', 'Gun Shot', 'Siren']

spi = busio.SPI(board.SCLK, MOSI=board.MOSI)
dc_pin = digitalio.DigitalInOut(board.D24)    # any pin!
reset_pin = digitalio.DigitalInOut(board.D23) # any pin!
cs_pin = digitalio.DigitalInOut(board.D22)    # any pin!
 
oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, dc_pin, reset_pin, cs_pin)

oled.fill(0)
oled.text('Initializing...', 0, 0, 1)
oled.show()
print('Initializing...')

r = sr.Recognizer()
r.energy_threshold = 4000
r.dynamic_energy_threshold = True
r.dynamic_energy_adjustment_ratio = 1.3 # default 1.5; lower values lead to
                                        # more false positives and less false negatives
r.pause_threshold = 0.4 # default 0.8; how long to wait before recognizing;
                        # lower values lead to quicker recognition

print('Loading model...')
model = load_model(PATH_TO_MODEL)

oled.fill(0)
oled.text('Ready!', 0, 0, 1)
oled.show()
print('Ready!')

while True:
	with sr.Microphone() as source:
		oled.text('Calibrating...', 0, 56, 1)
		oled.show()
		r.adjust_for_ambient_noise(source, duration=0.5)
		oled.text('Calibrating...', 0, 56, 0)
		oled.show()
		audio = r.listen(source)
	
	oled.fill(0)
	oled.text('Processing...', 0, 0, 1)
	oled.show()
	oled.fill(0)
	print('Processing...')

	runInParallel(speech, other_sounds)
	
	oled.show()
    def __init__(self):

        ## Arm and Hand Degrees ##
        self.armAngle = self.oldArmDegree = 0.0
        self.handAngle = self.oldHandDegree = -PI / 6

        self.maxArmAngle = PI / 2
        self.minArmAngle = 0

        self.maxHandAngle = 0
        self.minHandAngle = -PI / 2

        ## Robot Body ##
        self.robotWidth = 30
        self.robotHeight = 20
        self.robotPos = 20

        self.minRailPos = 0
        self.maxRailPos = 110

        ## Robot Arm ##
        self.armLength = 50

        ## Robot Hand ##
        self.handLength = 60

        self.positions = [20]

        i2c = busio.I2C(board.SCL, board.SDA)
        hat = adafruit_pca9685.PCA9685(i2c)
        self.kit = ServoKit(
            channels=16)  #, address=0x40, reference_clock_speed=25000000)
        self.sensor = adafruit_vl6180x.VL6180X(i2c)

        # Define the Reset Pin
        oled_reset = digitalio.DigitalInOut(board.D12)

        # Change these
        # to the right size for your display!
        WIDTH = 128
        HEIGHT = 32  # Change to 64 if needed
        BORDER = 5

        spi = busio.SPI(board.SCK, MOSI=board.MOSI)
        reset_pin = digitalio.DigitalInOut(board.D12)  # any pin!
        cs_pin = digitalio.DigitalInOut(board.D5)  # any pin!
        dc_pin = digitalio.DigitalInOut(board.D6)  # any pin!
        self.oled = adafruit_ssd1306.SSD1306_SPI(128, 32, spi, dc_pin,
                                                 reset_pin, cs_pin)

        # Load default font.
        self.font = ImageFont.load_default()
Esempio n. 6
0
def screen(): 
    loading = True
    while loading:
        try:
    # setup for the screen
    # Using for SPI
            spi = board.SPI()
            oled_reset = digitalio.DigitalInOut(board.D17)
            oled_cs = digitalio.DigitalInOut(board.D5)
            oled_dc = digitalio.DigitalInOut(board.D6)
            oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, oled_dc, oled_reset, oled_cs)
            loading = False
        except:
            loading = True
            
    try: 
    # Load font.
        font = ImageFont.truetype('/home/pi/Chemostat/arial.ttf', 12)
    except:
        font = ImageFont.load_default()
            
    # get the IP address
    IP = str(subprocess.check_output(["hostname", "-I"]).split()[0])
    IP = IP[2:-1]
    
    while True:
        if run == True:
            state = "ON"
        if run == False:
            state = "OFF"
        # Create blank image for drawing.
        
        # Make sure to create image with mode '1' for 1-bit color.
        image = Image.new('1', (oled.width, oled.height))
        # Get drawing object to draw on image.
        draw = ImageDraw.Draw(image)
        # Draw Some Text
        draw.text((1, 1), 'Temp: '+ str(temp)+' / '+ str(setpoint_T) + ' C', font=font, fill=255)
        draw.text((1, 15), 'Heat: ' + heat + ' St: ' + state , font=font, fill=255)
        draw.text((1, 28), 'Sparging: '+ str(duty_cycle)+'%', font=font, fill=255)
        draw.text((1, 41), 'pH: ' + str(pH) + ' OD: ' + str(optical_density), font=font, fill=255)
        draw.text((1, 53), 'IP: ' + str(IP), font=font, fill=255)
         
        # Display image
        oled.image(image)
        oled.show()
        #oled.dispaly()
        time.sleep(2)
Esempio n. 7
0
def setup_display():
    spi = busio.SPI(board.SCK, MOSI=board.MOSI)
    # Reset is on raspi GPIO24 / pin18
    reset_pin = digitalio.DigitalInOut(board.D24)
    # CS is on raspi GPIO8 / pin24
    cs_pin = digitalio.DigitalInOut(board.D8)
    # DC is on raspi GPIO23 / pin16
    dc_pin = digitalio.DigitalInOut(board.D23)

    oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, dc_pin, reset_pin,
                                        cs_pin)

    # Clear display.
    oled.fill(0)
    oled.show()

    return oled
def initialise_oled():
    # Define the Reset Pin
    oled_reset = digitalio.DigitalInOut(board.D4)

    # Change these
    # to the right size for your display!
    WIDTH = 128
    HEIGHT = 64  # Change to 64 if needed
    BORDER = 3

    # Use for SPI
    spi = board.SPI()
    oled_cs = digitalio.DigitalInOut(board.D5)
    oled_dc = digitalio.DigitalInOut(board.D6)
    oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc,
                                        oled_reset, oled_cs)

    return oled
Esempio n. 9
0
    def __init__(self):
        self.running = False

        # Set the oled screen height
        self.screen_width = WIDTH
        self.screen_height = HEIGHT

        # Set up buttons
        self.btn1Pin = LEFT_BTN_PIN
        self.btn2Pin = RIGHT_BTN_PIN
        self.btn3Pin = SELECT_BTN_PIN

        # Configure interrups for buttons
        GPIO.setup(self.btn1Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.btn2Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.btn3Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

        # Configure Screen
        spi = board.SPI()
        oled_reset = digitalio.DigitalInOut(RESET_PIN)
        oled_cs = digitalio.DigitalInOut(CS_PIN)
        oled_dc = digitalio.DigitalInOut(DC_PIN)
        self.oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)

        # load the pump configuration
        self.pumps = pumps
        for pump in self.pumps:
            GPIO.setup(pump["pin"], GPIO.OUT, initial=GPIO.HIGH)
        
        # Configure LED Pins
        GPIO.setup(RED_PIN, GPIO.OUT)
        GPIO.setup(GREEN_PIN, GPIO.OUT)
        GPIO.setup(BLUE_PIN, GPIO.OUT)
        
        # set RGB for lights
        self.red = GPIO.PWM(RED_PIN, 50)
        self.green = GPIO.PWM(GREEN_PIN, 50)
        self.blue = GPIO.PWM(BLUE_PIN, 50)
        
        # start LEDs
        self.red.start(0)
        self.green.start(0)
        self.blue.start(0)
Esempio n. 10
0
def write_text(text):
    # Define the Reset Pin
    oled_reset = digitalio.DigitalInOut(board.D26)

    WIDTH = 128
    HEIGHT = 32
    BORDER = 5

    # Use for SPI
    spi = board.SPI()
    oled_cs = digitalio.DigitalInOut(board.D13)
    oled_dc = digitalio.DigitalInOut(board.D19)
    oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc,
                                        oled_reset, oled_cs)

    # Clear display.
    oled.fill(0)
    oled.show()

    # Create blank image for drawing.
    # Make sure to create image with mode '1' for 1-bit color.
    image = Image.new('1', (oled.width, oled.height))

    # Get drawing object to draw on image.
    draw = ImageDraw.Draw(image)

    # Load default font.
    font = ImageFont.load_default()

    # Draw text
    (font_width, font_height) = font.getsize(text)
    draw.text((oled.width // 2 - font_width // 2,
               oled.height // 2 - font_height // 2),
              text,
              font=font,
              fill=255)

    # Display image
    oled.image(image)
    oled.show()
    return
Esempio n. 11
0
def screen():
    global temp
    global pH
    global optical_density
    global heat

    # setup for the screen
    # Using for SPI
    spi = board.SPI()
    oled_reset = digitalio.DigitalInOut(board.D17)
    oled_cs = digitalio.DigitalInOut(board.D5)
    oled_dc = digitalio.DigitalInOut(board.D6)
    oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, oled_dc, oled_reset,
                                        oled_cs)

    # Load font.
    font = ImageFont.truetype('arial.ttf', 12)

    while True:
        # Create blank image for drawing.
        # Make sure to create image with mode '1' for 1-bit color.
        image = Image.new('1', (oled.width, oled.height))
        # Get drawing object to draw on image.
        draw = ImageDraw.Draw(image)
        # Draw Some Text
        draw.text((1, 1),
                  'Temp: ' + str(temp) + 'C ' + heat,
                  font=font,
                  fill=255)
        draw.text((1, 15),
                  'Sparging: ' + str(duty_cycle) + '%',
                  font=font,
                  fill=255)
        draw.text((1, 28), 'OD: ' + str(optical_density), font=font, fill=255)
        draw.text((1, 42), 'pH: ' + str(pH), font=font, fill=255)

        # Display image
        oled.image(image)
        oled.show()
        time.sleep(2)
Esempio n. 12
0
#!/usr/bin/env python

print("initializing")

import board
import busio
import adafruit_ssd1306 as ssd1306
import time

spi = busio.SPI(clock=board.SCLK, MOSI=board.MOSI, MISO=board.MISO)

display = ssd1306.SSD1306_SPI(
    width=128,
    height=64,
    spi=spi,
    cs=digitalio.DigitalInOut(board.CE0),
    dc=digitalio.DigitalInOut(board.D27),
    rst=digitalio.DigitalInOut(board.D17),
)

print("Starting loop")

display.fill(0)
display.show()

time.sleep(1)

display.pixel(0, 0, 1)
display.pixel(64, 16, 1)
display.pixel(127, 31, 1)
display.show()
Esempio n. 13
0
import adafruit_ssd1306
import board
import busio
import digitalio
from PIL import Image, ImageDraw, ImageFont

spi = busio.SPI(board.SCK, MOSI=board.MOSI)
reset_pin = digitalio.DigitalInOut(board.D24)
cs_pin = digitalio.DigitalInOut(board.D8)
dc_pin = digitalio.DigitalInOut(board.D23)

WIDTH = 128
HEIGHT = 64
BORDER = 5

oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, dc_pin, reset_pin,
                                    cs_pin)

oled.fill(0)
oled.show()

# Create a blank image for drawing
# Make sure to create image with mode '1' for 1-bit color
image = Image.new('1', (oled.width, oled.height))

# Get drwaing object to draw on image
draw = ImageDraw.Draw(image)

# Draw a smaller inner rectangle
draw.rectangle(
    (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
    outline=0,
Esempio n. 14
0
# Change these
# to the right size for your display!
WIDTH = 128
HEIGHT = 64     # Change to 64 if needed
BORDER = 5

# Use for I2C.
#i2c = board.I2C()
#oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3c, reset=oled_reset)

# Use for SPI
spi = board.SPI()
oled_cs = digitalio.DigitalInOut(board.D5)
oled_dc = digitalio.DigitalInOut(board.D6)
oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new('1', (oled.width, oled.height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a white background
#draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
    def __init__(self, lcd_dev=None, lcd_settings_dict=None):
        if lcd_dev:
            self.logger = logging.getLogger("{}_{}".format(
                __name__,
                lcd_dev.unique_id.split('-')[0]))
            self.interface = lcd_dev.interface
            self.lcd_x_characters = lcd_dev.x_characters
            self.lcd_type = lcd_dev.lcd_type
            # I2C
            self.i2c_address = int(lcd_dev.location, 16)
            self.i2c_bus = lcd_dev.i2c_bus
            # SPI
            self.spi_bus = lcd_dev.spi_bus
            self.spi_device = lcd_dev.spi_device
            self.pin_dc = lcd_dev.pin_dc
            self.pin_reset = lcd_dev.pin_reset
            self.pin_cs = lcd_dev.pin_cs
        elif lcd_settings_dict:
            self.logger = logging.getLogger("{}_{}".format(
                __name__, lcd_settings_dict["unique_id"].split('-')[0]))
            self.interface = lcd_settings_dict["interface"]
            self.lcd_x_characters = lcd_settings_dict["x_characters"]
            self.lcd_type = lcd_settings_dict["lcd_type"]
            # I2C
            self.i2c_address = int(lcd_settings_dict["i2c_address"], 16)
            self.i2c_bus = lcd_settings_dict["i2c_bus"]
            # SPI
            self.spi_bus = lcd_settings_dict["spi_bus"]
            self.spi_device = lcd_settings_dict["spi_device"]
            self.pin_dc = lcd_settings_dict["pin_dc"]
            self.pin_reset = lcd_settings_dict["pin_reset"]
            self.pin_cs = lcd_settings_dict["pin_cs"]

        self.disp = None

        if self.interface == 'I2C':
            if self.lcd_type == '128x32_pioled_circuit_python':
                self.disp = adafruit_ssd1306.SSD1306_I2C(
                    128,
                    32,
                    ExtendedI2C(self.i2c_bus),
                    addr=int(str(self.i2c_address), 16))
            elif self.lcd_type == '128x64_pioled_circuit_python':
                self.disp = adafruit_ssd1306.SSD1306_I2C(
                    128,
                    64,
                    ExtendedI2C(self.i2c_bus),
                    addr=int(str(self.i2c_address), 16))

        elif self.interface == 'SPI':
            if self.lcd_type == '128x32_pioled_circuit_python':
                import Adafruit_GPIO.SPI as SPI
                self.disp = adafruit_ssd1306.SSD1306_SPI(128,
                                                         32,
                                                         spi=SPI.SpiDev(
                                                             self.spi_bus,
                                                             self.spi_device),
                                                         dc=self.pin_dc,
                                                         reset=self.pin_reset,
                                                         cs=self.pin_cs)
            elif self.lcd_type == '128x64_pioled_circuit_python':
                import Adafruit_GPIO.SPI as SPI
                self.disp = adafruit_ssd1306.SSD1306_SPI(128,
                                                         64,
                                                         spi=SPI.SpiDev(
                                                             self.spi_bus,
                                                             self.spi_device),
                                                         dc=self.pin_dc,
                                                         reset=self.pin_reset,
                                                         cs=self.pin_cs)

        if not self.disp:
            self.logger.error(
                "Unable to set up display. Check the LCD settings.")
Esempio n. 16
0
import adafruit_ssd1306
import board
import busio
import digitalio

spi = busio.SPI(board.SCK, MOSI=board.MOSI)
reset_pin = digitalio.DigitalInOut(board.D4) # any pin!
cs_pin = digitalio.DigitalInOut(board.D5)    # any pin!
dc_pin = digitalio.DigitalInOut(board.D6)    # any pin!

oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, dc_pin, reset_pin, cs_pin)
Esempio n. 17
0
OLED_HEIGHT  =  64

# フォントの設定
DEFAULT_FONT  =  '/usr/share/fonts/truetype/fonts-japanese-gothic.ttf'
FONT_SIZE     =  14

# TryeTypeフォントオブジェクト
jpfont = ImageFont.truetype(DEFAULT_FONT, FONT_SIZE, encoding='unic')

# SPIオブジェクト
spi = board.SPI()
oled_cs = digitalio.DigitalInOut(board.D8)
oled_reset = digitalio.DigitalInOut(board.D24)
oled_dc = digitalio.DigitalInOut(board.D23)

disp = adafruit_ssd1306.SSD1306_SPI(OLED_WIDTH, OLED_HEIGHT, spi, oled_dc, oled_reset, oled_cs)

# OLEDオブジェクトのバッファークリア
disp.fill(0)

# バッファの表示
# disp.display()
disp.show()

# 文字列の設定
gyou = [ 'こんにちは',
         'ビット・トレード・ワン',
         'ラズパイzerooneシリーズ ',
         'adrszLD 漢字ボード  ' ]

def draw_gyou():
Esempio n. 18
0
print("setup i2c & spi & digitalio")
i2c = busio.I2C(board.SCL, board.SDA)
spi = busio.SPI(board.SCLK, board.MOSI)
pin_cs = digitalio.DigitalInOut(board.P2_6)
pin_rst = digitalio.DigitalInOut(board.P1_2)
pin_dc = digitalio.DigitalInOut(board.P1_4)

print("setup APDS9960")
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)

print("init SSD1306_SPI")
width = 128
height = 64
# width, height, spi, dc, reset, cs
display = adafruit_ssd1306.SSD1306_SPI(
    width, height, spi, pin_dc, pin_rst, pin_cs)


# ------------------------------------------
gesture_name = {
    0: "No",
    1: "Up",
    2: "Down",
    3: "Left",
    4: "Right",
}

text_positions = {
    "gesture": (0, 0),
    "proximity": (0, 0),
    "color": (0, 0),
Esempio n. 19
0
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

# Setting some variables for our reset pin etc.
# RESET_PIN = digitalio.DigitalInOut(board.D4)

# # Create the I2C interface.
# i2c = busio.I2C(SCL, SDA)
# oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)

# Create the SPI interface.
spi = board.SPI()
oled_cs = digitalio.DigitalInOut(SS0)
oled_dc = digitalio.DigitalInOut(DC)
oled_reset = None
oled = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, oled_dc, oled_reset, oled_cs)

# Clear display.
oled.fill(0)
oled.show()

# Create blank image for drawing.
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)

# Load a font in 2 different sizes.
# font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
# font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 14)

# Load default font.
font = ImageFont.load_default()
Esempio n. 20
0
def on_message(client, userdata, msg):

    # Define the reset pin
    oled_reset = digitalio.DigitalInOut(board.D4)

    # These numbers vary depending on the size of the OLED screen
    WIDTH = 128
    HEIGHT = 64
    BORDER = 1
    spi = board.SPI()
    oled_cs = digitalio.DigitalInOut(board.D5)
    oled_dc = digitalio.DigitalInOut(board.D6)
    oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc,
                                        oled_reset, oled_cs)

    # Clear display
    oled.fill(0)
    oled.show()

    # Make sure to create image with mode '1' for 1-bit color
    image = Image.new("1", (oled.width, oled.height))

    # Get drawing object to draw on image
    draw = ImageDraw.Draw(image)

    # Draw a white background
    draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

    # Draw a smaller inner rectangle
    draw.rectangle(
        (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
        outline=0,
        fill=0,
    )

    # Load default font
    font = ImageFont.load_default()

    # Variable for text = emojis
    text = str(msg.payload)[2:-1]
    (font_width, font_height) = font.getsize(text)
    draw.text(
        (oled.width // 2 - font_width // 2,
         oled.height // 2 - font_height // 2),
        text,
        font=font,
        fill=255,
    )
    # Display image
    if text != " ":
        draw.text(
            (oled.width // 2 - font_width // 2,
             oled.height // 2 - font_height // 2),
            text,
            font=font,
            fill=255,
        )

        # Display image
        oled.image(image)
        oled.show()
Esempio n. 21
0
def on_message(client, userdata, msg):

    # Define the Reset Pin
    oled_reset = digitalio.DigitalInOut(board.D4)

    # Change these
    # to the right size for your display!
    WIDTH = 128
    HEIGHT = 32  # Change to 64 if needed
    BORDER = 5

    # Use for I2C.
    #i2c = board.I2C()
    #oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)

    # Use for SPI
    spi = board.SPI()
    oled_cs = digitalio.DigitalInOut(board.D5)
    oled_dc = digitalio.DigitalInOut(board.D6)
    oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc,
                                        oled_reset, oled_cs)

    # Clear display.
    oled.fill(0)
    oled.show()

    # Create blank image for drawing.
    # Make sure to create image with mode '1' for 1-bit color.
    image = Image.new("1", (oled.width, oled.height))

    # Get drawing object to draw on image.
    draw = ImageDraw.Draw(image)

    # Draw a white background
    draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

    # Draw a smaller inner rectangle
    draw.rectangle(
        (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
        outline=0,
        fill=0,
    )

    # Load default font.
    font = ImageFont.load_default()

    # Draw Some Text
    #text = "Hej guys, it's working!!!! :D"
    text = str(msg.payload)[2:-1]
    (font_width, font_height) = font.getsize(text)
    draw.text(
        (oled.width // 2 - font_width // 2,
         oled.height // 2 - font_height // 2),
        text,
        font=font,
        fill=255,
    )

    #variable to wait for new input:
    previous_message = str(msg.payload)[2:-1]

    # Display image
    if text == previous_message:
        for n in range(
                128, -128, -1
        ):  #these numbers should be calculated by the width of the text
            draw.text(
                (n, oled.height // 2 - font_height // 2),
                text,
                font=font,
                fill=255,
            )

            #Display image
            oled.image(image)
            oled.show()
            sleep(0.01)
            draw.rectangle(  #delete innermost area
                (BORDER, BORDER, oled.width - BORDER - 1,
                 oled.height - BORDER - 1),
                outline=0,
                fill=0,
            )
            previous_message = text
    else:
        # Create blank image for drawing.
        # Make sure to create image with mode '1' for 1-bit color.
        image = Image.new("1", (oled.width, oled.height))

        # Get drawing object to draw on image.
        draw = ImageDraw.Draw(image)

        # Draw a white background
        draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

        # Draw a smaller inner rectangle
        draw.rectangle(
            (BORDER, BORDER, oled.width - BORDER - 1,
             oled.height - BORDER - 1),
            outline=0,
            fill=0,
        )

        for n in range(
                128, -128, -1
        ):  #these numbers should be calculated by the width of the text
            draw.text(
                (n, oled.height // 2 - font_height // 2),
                text,
                font=font,
                fill=255,
            )

            #Display image
            oled.image(image)
            oled.show()
            sleep(0.01)
            draw.rectangle(  #delete innermost area
                (BORDER, BORDER, oled.width - BORDER - 1,
                 oled.height - BORDER - 1),
                outline=0,
                fill=0,
            )
            previous_message = text
Esempio n. 22
0
# offscreen
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

# Raspberry Pi pin configuration:
oled_reset = digitalio.DigitalInOut(board.D25)
oled_cs = digitalio.DigitalInOut(board.CE0)
oled_dc = digitalio.DigitalInOut(board.D24)

# 128x64 display with hardware SPI:
spi = board.SPI()
width = 128
height = 64
oled = adafruit_ssd1306.SSD1306_SPI(width, height, spi, oled_dc, oled_reset,
                                    oled_cs)

# buttons
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)  # SW1
GPIO.setup(17, GPIO.IN)  # SW2
GPIO.setup(18, GPIO.IN)  # SW4
GPIO.setup(27, GPIO.IN)  # SW3


# This sets TEXT equal to whatever your IP address is, or isn't
def ip_address():
    try:
        TEXT = get_ip_address(
            b'wlan0')  # WiFi address of WiFi adapter. NOT ETHERNET
    except IOError:
Esempio n. 23
0
def print_to_oled(msg):
    global message

    oled_reset = digitalio.DigitalInOut(board.D4)

    # Change these
    # to the right size for your display!
    WIDTH = 128
    HEIGHT = 32  # Change to 64 if needed
    BORDER = 0

    # Use for I2C.
    #i2c = board.I2C()
    #oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)

    # Use for SPI
    spi = board.SPI()
    oled_cs = digitalio.DigitalInOut(board.D5)
    oled_dc = digitalio.DigitalInOut(board.D6)
    oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc,
                                        oled_reset, oled_cs)

    # Clear display.
    oled.fill(0)
    oled.show()

    # Create blank image for drawing.
    # Make sure to create image with mode '1' for 1-bit color.
    image = Image.new("1", (oled.width, oled.height))

    # Get drawing object to draw on image.
    draw = ImageDraw.Draw(image)

    # Draw a white background
    draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

    # Draw a smaller inner rectangle
    draw.rectangle(
        (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
        outline=0,
        fill=0,
    )

    # Load default font.
    font = ImageFont.load_default()
    print(msg.payload.decode("utf-8"))

    # This if statement will print out anything that is published while count is less than or equal to 2
    if (count <= 2):
        text = msg.payload.decode("utf-8")
        (font_width, font_height) = font.getsize(text)
        draw.text(
            (oled.width // 2 - font_width // 2,
             oled.height // 2 - font_height // 2),
            text,
            font=font,
            fill=255,
        )

        oled.image(image)
        oled.show()

    # This if statement will print out scrolling text when both players have finished their workout
    if (
            count == 3
    ):  # When the players start another round of workouts the count variable will be reset so the text stops scrolling
        text = msg.payload
        (font_width, font_height) = font.getsize(text)

        for n in range(128, -130, -1):
            draw.text(
                (n, oled.height // 2 - font_height // 2),
                text,
                font=font,
                fill=255,
            )

            oled.image(image)
            oled.show()
            sleep(0.01)
            draw.rectangle(
                (BORDER, BORDER, oled.width - BORDER - 1,
                 oled.height - BORDER - 1),
                outline=0,
                fill=0,
            )
Esempio n. 24
0
import busio
import adafruit_ssd1306
from simpleio import map_range
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

# Create SPI bus
spi = busio.SPI(board.SCK, board.MOSI)

# Create the display
WIDTH = 128
HEIGHT = 64
DC = DigitalInOut(board.D7)
CS = DigitalInOut(board.D9)
RST = DigitalInOut(board.D10)
display = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RST, CS)
display.fill(0)
display.show()

# Create the knobs
READS = 5
x_knob = AnalogIn(board.A0)
y_knob = AnalogIn(board.A1)

# Create the clear button
clear_button = DigitalInOut(board.D12)
clear_button.direction = Direction.INPUT
clear_button.pull = Pull.UP


def read_knobs(reads):
Esempio n. 25
0
import board
import time
import busio
import digitalio
import adafruit_ssd1306
from PIL import Image, ImageFont, ImageDraw

from digitalio import DigitalInOut, Direction
spi = busio.SPI(board.SCK, board.MOSI)

DC = DigitalInOut(board.D6)
CS = DigitalInOut(board.D5)
RST = DigitalInOut(board.D4)

disp = adafruit_ssd1306.SSD1306_SPI(128, 64, spi, DC, RST, CS)
disp.fill(1)
disp.show()
time.sleep(2)
disp.fill(0)
disp.show()

#disp.begin()

# Clear display.
#disp.clear()
#disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
from adafruit_pn532.spi import PN532_SPI

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

# PN532 SPI connection:
pn_cs_pin = DigitalInOut(board.D5)
pn532 = PN532_SPI(spi, pn_cs_pin, debug=False)

# OLED ssd 1306 SPI connection:
ssd1306_cs_pin = DigitalInOut(board.D24)
ssd1306_dc_pin = DigitalInOut(board.D25)
ssd1306_reset_pin = DigitalInOut(board.D23)
WIDTH = 128
HEIGHT = 32

display = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, ssd1306_dc_pin,
                                       ssd1306_reset_pin, ssd1306_cs_pin)
"""
 OLED painting
"""
print("Lines test")
# we'll draw from corner to corner, lets define all the pair coordinates here
corners = ((0, 0), (0, display.height - 1), (display.width - 1, 0),
           (display.width - 1, display.height - 1))

display.fill(0)
for corner_from in corners:
    for corner_to in corners:
        display.line(corner_from[0], corner_from[1], corner_to[0],
                     corner_to[1], 1)
display.show()
time.sleep(1)