コード例 #1
0
#!/usr/bin/python3

from sense_emu import SenseHat

import sys

sense = SenseHat()

for x in range(8):
    for y in range(8):
        sense.set_pixel(x, y, 0, 0, 0) #set pixel to white
                
コード例 #2
0
sense = SenseHat()

keydown = 0
keyup = 0
p = True
bool = False

while True:
    for event in sense.stick.get_events():
        print(event)
        if event.action == 'pressed' & event.direction == 'left':
            if bool:
                if p:
                    for i in range(7):
                        sense.set_pixel(i, 3, 0, 255, 255)
                    for i in range(7):
                        sense.set_pixel(i, 7, 0, 255, 255)
                else:
                    for i in range(7):
                        sense.set_pixel(i, 0, 0, 255, 0)
                    for i in range(7):
                        sense.set_pixel(i, 3, 0, 255, 0)
                    for i in range(8):
                        sense.set_pixel(7, i, 0, 255, 0)
                    for i in range(7):
                        sense.set_pixel(i, 7, 0, 255, 0)
                p = not p
            else:
                bool = not bool
コード例 #3
0
from sense_emu import SenseHat
from mcpi.minecraft import Minecraft

mc = Minecraft.create()

sense = SenseHat()
sense.clear()
sense.set_pixel(7, 7, (255, 255, 255))


def rwmf():
    while true:
        x, y, z = mc.player.getPos()  # player position (x, y, z)
        block_beneath = mc.getBlock(x, y - 1, z)  # block ID

        if block_beneath == grass:
            mc.setBlock(x, y, z, flower)


rwmf()
コード例 #4
0
                ledcolor = parsedjson["LED"]

                # The Flow program will only send back the following LED colors:
                #     OFF, RED, GREEN, YELLOW, BLUE, MAGENTA, TURQUOISE, WHITE
                # Convert these colors to the Pi HAT LED colors
                if ledcolor == "OFF":
                    rgbLED = [0, 0, 0]
                elif ledcolor == "RED":
                    rgbLED = [255, 0, 0]
                elif ledcolor == "GREEN":
                    rgbLED = [0, 255, 0]
                elif ledcolor == "YELLOW":
                    rgbLED = [255, 255, 0]
                elif ledcolor == "BLUE":
                    rgbLED = [0, 0, 255]
                elif ledcolor == "MAGENTA":
                    rgbLED = [255, 0, 255]
                elif ledcolor == "TURQUOISE":
                    rgbLED = [0, 255, 255]
                elif ledcolor == "WHITE":
                    rgbLED = [255, 255, 255]
                else:
                    rgbLED = [0, 0, 0]

                #Echo out parsed color
                print "LED color:", ledcolor, rgbLED
                sense.set_pixel(oldcol, oldrow, [0, 0, 0])
                sense.set_pixel(col, row, rgbLED)
                oldrow = row
                oldcol = col
コード例 #5
0
#!/usr/bin/python
import json
from sense_emu import SenseHat

sense = SenseHat()

filename = "luigi.json"

if filename:
    with open(filename, 'r') as f:
        ledDisplayArray = json.load(f)

#DEBUG: print results
for led in ledDisplayArray:
    sense.set_pixel(led[0], led[1], led[2], led[3], led[4])
    #print("(x:"+str(led[0])+ "y:"+str(led[1])+r:"+str(led[2])+"g:"+str(led[3])+"b:"+str(led[4]))
コード例 #6
0
ファイル: sensehat.py プロジェクト: andrewquamme/RaspberryPi
from sense_emu import SenseHat
##from sense_hat import SenseHat

sense = SenseHat()

r = (255, 0, 0)
g = (0, 255, 0)
b = (0, 0, 255)
y = (255, 255, 0)

message = "Hi!"

##sense.set_rotation(180)
sense.show_letter("Z")
##sense.show_message(message, text_colour=blue)
##sense.clear()

sense.set_pixel(0, 0, r)
pixels = [
    g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, b, b, g, g, b, b, g, g,
    b, b, g, g, b, b, g, g, g, g, b, b, g, g, g, g, g, b, b, b, b, g, g, g, g,
    b, b, b, b, g, g, g, g, b, g, g, b, g, g
]

sense.set_pixels(pixels)
コード例 #7
0
def down():
    global ry
    if ry + 2 < 7:
        ry = ry + 1


def up():
    global ry
    if ry > 0:
        ry = ry - 1


sense.stick.direction_down = down
sense.stick.direction_up = up

sense.set_pixel(x, y, purple)

while True:
    sense.clear(0, 0, 0)
    pongRaquette()
    sense.set_pixel(x, y, purple)
    time.sleep(0.2)

    x = x + x_sens
    y = y + y_sens

    if x == 7:
        x_sens = -1

    if y == 0:
        y_sens = 1
コード例 #8
0
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)


def random_colour():
    return (randint(0, 255), randint(0, 255), randint(0, 255))


sense.clear(random_colour())
sleep(1)

# light corners
maxc = 7
sense.clear()
sense.set_pixel(0, 0, white)
sense.set_pixel(0, maxc, white)
sense.set_pixel(maxc, 0, white)
sense.set_pixel(maxc, maxc, white)
sleep(1)

# draw smiley
sense.clear()
sense.set_pixel(2, 2, (0, 0, 255))
sense.set_pixel(4, 2, (0, 0, 255))
sense.set_pixel(3, 4, (100, 0, 0))
sense.set_pixel(1, 5, (255, 0, 0))
sense.set_pixel(2, 6, (255, 0, 0))
sense.set_pixel(3, 6, (255, 0, 0))
sense.set_pixel(4, 6, (255, 0, 0))
sense.set_pixel(5, 5, (255, 0, 0))
コード例 #9
0
#Spencer Organ - August 2019

from sense_emu import SenseHat
from time import sleep
sense = SenseHat()
r = [255, 0, 0]
wait = 10
sense.clear()

while True:
    for c in range(0, 8):
        pressure = sense.pressure
        print(pressure)
        graph_pressure = int(pressure / 150)
        print(graph_pressure)
        for i in range(graph_pressure):
            sense.set_pixel(c, i, r)
        print(c)
        sleep(wait)
    sense.clear()
コード例 #10
0
    y = int((pi + tang) * 8 / (2 * pi))

    print(x, y)
    # affiche les valeurs de roulis/tanguage en radians
    print("Roulis:", roul, "Tangage:", tang)

    tang_deg = tang * 360 / (2 * pi)
    roul_deg = roul * 360 / (2 * pi)
    # affiche les valeurs de roulis/tanguage en degres
    print('Roulis: %5.2f ' % roul_deg, 'Tangage: %5.2f ' % tang_deg)

    return x, y


try:

    while True:  # Boucle principale

        lect_capt()  # appelle la routine de lecture des capteurs
        sense.clear()
        # allume la led sur le sense-hat
        # la couleur est indexee dans la matrice de couleur
        sense.set_pixel(x, y, couleur[x][y])

        # sleep(0.04)
        sleep(0.1)

except KeyboardInterrupt:  # sortie de prgm si ctrl C
    print("Termine")
    sense.clear()
コード例 #11
0
import json

sense = SenseHat()
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
orange = (255, 165, 0)
white = (255, 255, 255)
colorPicked = ()


data_file = open("singleLedColor.json", "r")
data_led = data_file.readline()
data_file.close()
data_led_json = json.loads(data_led)

if data_led_json["color"] == "red":
    colorPicked = red
elif data_led_json["color"] == "green":
    colorPicked = green
elif data_led_json["color"] == "blue":
    colorPicked = blue
elif data_led_json["color"] == "orange":
    colorPicked = orange
elif data_led_json["color"] == "white":
    colorPicked = white
else:
    colorPicked = (0, 0, 0)

sense.set_pixel(data_led_json["column"], data_led_json["row"], colorPicked)
コード例 #12
0
# descomente a linha abaixo print final no terminal para o programados entender o que aconteceu
#print(pixels)

# Aqui vamos fazer as inserções de modo individualmente
# perceba que o método é set_pixel,
# antes era set_pixels
# a diferença é que o primeiro (set_pixels) atualiza a lista inteira
# e se basei em posições de 0 à 63

# Já o segundo (set_pixel) pracisa de duas informações de posição
# um valor para coluna
# outro para linha
# só depois os valores das cores em RGB

#setamos o pixel da coluna 0 e linha 0 com os valores RGB 255, 255, 255
hat.set_pixel(0, 0, 255, 255, 255)
time.sleep(2.5)
hat.set_pixel(7, 0, 255, 255, 255)
time.sleep(2.5)
# Os valores em RGB também funcionam para os valores predefinidos nas linhas 8 à 11
hat.set_pixel(0, 7, white)
time.sleep(2.5)
hat.set_pixel(7, 7, white)
time.sleep(2.5)
# Os mesmos que setamos de branco com a lista
# agora pintaremos de vermelho de forma individual
hat.set_pixel(3, 3, red)
time.sleep(2.5)
hat.set_pixel(4, 4, red)
time.sleep(2.5)
コード例 #13
0
from random import shuffle
from random import *

random_list = [0, 0, 0, 0, 0, 0, 0, 0]  #Set up the array
sorted = False
pointer = 0
passes = (len(random_list) - 1)
temp = 0
swap_made = True
my_pass = 0

sense = SenseHat()
sense.low_light = True  # This makes it easier to film the results
sense.clear()
colour = (0, 0, 0)
sense.set_pixel(0, 1, colour)


def populate_list():
    count = 0
    while count <= 7:  # This number can change if you are not using the SenseHat
        random_list[count] = randint(
            0, 10)  # This method will probaby generate repeated
        count += 1
    shuffle(random_list)
    shuffle(random_list)
    shuffle(random_list)
    check_pixel()


def check_pixel():
コード例 #14
0
# Bind the socket to the port
server_address = ('localhost', 10001)
print('starting up on {} port {}'. format(*server_address))
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    connection, client_address = sock.accept()
    try:
        msg = connection.recv(4096)
        JSONstring = msg.decode()
        data = json.loads(JSONstring)
        if data:
            sense.clear()
            for key, value in data.items():
                x = int(key[3])
                y = int(key[4])
                r = int(value[0])
                g = int(value[1])
                b = int(value[2])
                sense.set_pixel( x, y, r, g, b )
        else:
            sense.clear()
        msg = "Success"
        msg = msg.encode('utf-8')
        connection.sendall(msg)
    finally:
        connection.close()
コード例 #15
0
from sense_emu import SenseHat
from time import sleep
sense = SenseHat()

sense.clear()
sense.set_pixel(2, 2, (0, 0, 255))
sense.set_pixel(4, 2, (0, 0, 255))
sense.set_pixel(3, 4, (100, 0, 0))
sense.set_pixel(1, 5, (255, 0, 0))
sense.set_pixel(2, 6, (255, 0, 0))
sense.set_pixel(3, 6, (255, 0, 0))
sense.set_pixel(4, 6, (255, 0, 0))
sense.set_pixel(5, 5, (255, 0, 0))

sleep(3)
sense.clear()
sleep(1)

g = (0, 255, 0)
b = (0, 0, 0)

creeper_pixels = [
    g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, g, b, b, g, g,
    g, g, g, g, b, b, g, g, g, g, b, b, g, g, g, g, g, b, b, b, b, g, g, g, g,
    b, b, b, b, g, g, g, g, b, g, g, b, g, g
]

sense.set_pixels(creeper_pixels)

while True:
    sleep(1)
コード例 #16
0
ファイル: mines.py プロジェクト: Terence-Chevroulet/oc-2018
#            Start of the game              #
#############################################

# We use the function of game start:
new_game(nb_bombs)

# Part that manages the deplacement with joystick:
while game:
    while running:
        for event in sense.stick.get_events():
            if event.action == 'held' and event.direction == 'middle':

                xy_flag = x * 10 + y
                if xy_flag not in list_flag:
                    list_flag.append(xy_flag)
                    sense.set_pixel(x, y, green)
                else:
                    list_flag.remove(xy_flag)

            if event.action == 'pressed':

                sense.set_pixel(x, y, old_color)
                if event.direction == 'down' and y < 7:

                    y = y + 1
                    old_color = sense.get_pixel(x, y)

                elif event.direction == 'up' and y > 0:

                    y = y - 1
                    old_color = sense.get_pixel(x, y)
コード例 #17
0
    #     else: IO_rx_color = red
    #     if loglevel_deep: print(ticker(), "-- IO_rx_coloer is: ", IO_rx_color)
    #     s.set_pixel(IO_pixel_rx[0], IO_pixel_rx[1], IO_rx_color)

    return ()


#End of System Pixel Block------------

#---------------------------------------------------------------------------------
#------------- and execute... ----------------------------------------------------

while True:
    for i in range(8):  #clear the entire display
        for ii in range(8):
            s.set_pixel(i, ii, nothing)
    time.sleep(sleeptime_m)
    hour_stripe_color = night_or_day()

    #    hour_wipe_stripe()     #initialize the hour stripe
    #    if loglevel_deep: print(ticker(),"display cleared and hour ring initialized before going into 24-hour loop")

    localtime = time.localtime(time.time())
    hour_true = localtime.tm_hour

    while hour_true < 24:
        hour_wipe_stripe()  #initialize the hour stripe
        if loglevel_deep:
            print(
                ticker(),
                "display cleared and hour ring initialized from inside 24hr loop"
コード例 #18
0
ファイル: hat_test1-1.py プロジェクト: cgdtaylor/sense_hat
#sense hat test 1.1
from sense_emu import SenseHat
from random import randint
sense = SenseHat()

r=randint(0,255)
g=randint(0,255)
b=randint(0,255)

sense.set_pixel(0,0,r,g,b)

#display color


# define scoll speed
sense.scroll_speed = 15

#send message
sense.show_message("this is colourful", text_colour=(r,g,b)) 
print r,g,b
コード例 #19
0
from sense_emu import SenseHat
from time import sleep
sense = SenseHat()
y = [255, 255, 255]
e = [0, 0, 0]
O = [0, 0, 255]
A = 0
B = 0
path = [
    y, y, y, e, e, e, e, e, e, e, y, e, e, e, e, e, e, e, y, e, e, e, e, e, e,
    e, y, y, y, y, y, e, e, e, e, e, e, e, y, e, e, e, e, e, e, e, y, e, e, e,
    e, e, y, y, y, e, e, e, e, e, y, e, e, e
]
while 1:
    sense.set_pixels(path)
    sense.set_pixel(A, B, O)
    pitch = sense.get_orientation()['pitch']
    roll = sense.get_orientation()['roll']
    print('Pitch:', pitch)
    print('Roll:', roll)
    if 270 < pitch < 315 and A < 7:
        A += 1
    if 45 < pitch < 90 and A > 0:
        A -= 1
    if 45 < roll < 90 and B < 7:
        B += 1
    if 270 < roll < 315 and B > 0:
        B -= 1
    current = sense.get_pixel(A, B)
    if current == e:
        A = 0
コード例 #20
0
from random import *

random_list = [0, 0, 0, 0, 0, 0, 0, 0]

print(random_list)
sorted = False
pointer = 0
passes = (len(random_list) - 1)
temp = 0
swap_made = True
my_pass = 0

hat = SenseHat()
hat.clear()
colour = (0, 0, 0)
hat.set_pixel(0, 1, colour)


def populate_list():
    count = 0
    while count <= 7:  # This number can change if you are not using the SenseHat
        random_list[count] = randint(0, 10)
        count += 1
    print(random_list)
    check_pixel()


def check_pixel():
    count = 0
    while count <= passes:
        if random_list[count] == 0:
コード例 #21
0
class Game:
    def __init__(self):
        self.sense = SenseHat()
        self.sense.low_light = True

        self.intro_msg()

        self.grid = Grid(colors["white"])
        self.sense.set_pixels(self.grid)

        self.cursor = Cursor(colors["pink"])
        self.tiles = [GameTile(i) for i in range(9)]

        self.players = [Player(colors["blue"]), Player(colors["red"])]

    @property
    def current_player(self):
        nb = self.turn % 2
        return self.players[nb]

    @property
    def other_player(self):
        nb = self.turn % 2
        return self.players[nb ^ 1]

    @property
    def turn(self):
        nb_used_tiles = len(self.players[0].tiles) + len(self.players[1].tiles)
        return nb_used_tiles

    def refresh(self):
        for tile in self.tiles:
            self._draw_tile(tile)

    def _draw_tile(self, tile):
        for led in tile.coords:
            self.sense.set_pixel(led[0], led[1], tile.color)

    def play(self, socket):
        self.cursor.place(4)
        self._draw_tile(self.cursor)
        while True:
            event = self.sense.stick.wait_for_event()
            if event.action == "pressed":
                if not event.direction == "middle":
                    self.cursor.move(event.direction)
                    self.refresh()
                    self._draw_tile(self.cursor)
                else:
                    selected_tile = self.tiles[self.cursor.nb]
                    if selected_tile.color == colors["blank"]:
                        self.current_player.play(
                            selected_tile)  # the turn is incremented here
                        if self.turn > 3:
                            if self.other_player.has_win():
                                socket.sendall(bytes([9]))
                                self.win_msg()
                                return True

                        socket.sendall(bytes([self.cursor.nb]))
                        self.refresh()
                        return False

    def wait_for_play(self, socket):
        while True:
            data = socket.recv(1024)
            if data:
                tile_nb = int.from_bytes(data, "little")
                if tile_nb == 9:
                    self.loose_msg()
                    return True
                else:
                    self.current_player.play(self.tiles[tile_nb])
                    self.refresh()
                    return False

    def intro_msg(self):
        self.sense.show_message(
            "Welcome to pitactoe!!",
            text_colour=colors["white"],
            scroll_speed=SCROLL_SPEED,
        )

    def draw_msg(self):
        self.sense.show_message("Draw!",
                                text_colour=colors["white"],
                                scroll_speed=SCROLL_SPEED)

    def win_msg(self):
        self.sense.show_message("You win!",
                                text_colour=colors["white"],
                                scroll_speed=SCROLL_SPEED)

    def loose_msg(self):
        self.sense.show_message("You loose!",
                                text_colour=colors["white"],
                                scroll_speed=SCROLL_SPEED)
コード例 #22
0
"""
from sense_emu import SenseHat
from time import sleep
sense = SenseHat()

### Variables ----------------------
blue = (0, 0, 255)
red = (255, 0, 0)
white = (255, 255, 255)
blank = (0, 0, 0)

snek = [[2, 4], [3, 4], [4, 4]]
direction = "right"

sense.set_pixel(0, 2, blue)
sense.set_pixel(7, 4, red)

#snek.append() #add suff to the snek


#### Functoions ---------------------
def draw_snek():
    for point in snek:
        sense.set_pixel(point[0], point[1], white)


def move(direction):
    last = snek[-1]
    first = snek[0]
    next = list(last)
コード例 #23
0
color = ''

sysarg = sys.argv[1:]

try:
    opts, args = getopt.getopt(sysarg, ':x:y:c:')
except getopt.GetoptError as err:
    print(err)
    sys.exit(1)

for opt, arg in opts:
    if opt in '-x':
        x = int(arg)
    if opt in '-y':
        y = int(arg)
    if opt in '-c':
        color = arg

if x < 0 or x > 7 or y < 0 or y > 7:
    print("Wrong coordinates!")
    exit(1)

elif (color == 'R') or (color == 'r'):
    sense.set_pixel(x, y, 255, 0, 0)  #setting color to red
elif (color == 'G') or (color == 'g'):
    sense.set_pixel(x, y, 0, 255, 0)
elif (color == 'B') or (color == 'b'):
    sense.set_pixel(x, y, 0, 0, 255)
else:
    print("No color chosen!")
    sense.clear(0, 0, 0)