Coder Social home page Coder Social logo

sense-hat-examples's Issues

dice.py New sense-hat example to add if you are interested. See code comments for details

#!/usr/bin/python
"""
Original code modified by Claude Pageau [email protected]
This code was originally posted 
here http://www.suppertime.co.uk/blogmywiki/2015/12/raspberrypi-dice-project/
Not sure who the original author is.
I had a Raspberry pi sense-hat and decided to do some improvements.
This code is designed to run on a raspberry pi with a sense hat installed and
working per instructions here https://www.raspberrypi.org/documentation/hardware/sense-hat/

"""
# User Settings
debug = True
accel_thresh = 0.4
shake_timer = 3  # seconds to show die before cleaing display

print("Settings dice.py ver 1.1")
print("------------------------")
print("debug = %s" % debug)
print("accel_thresh = %0.3f (delta)" % accel_thresh)
print("shake_timer = %i sec (show die)" % shake_timer)
print("------------------------")
print("Loading .....")

from sense_hat import SenseHat
import time
import random
sense = SenseHat() # initialize 

# Setup dice display variables

b = [0, 0, 0]
g = [0, 255, 0]
r = [255, 0, 0]

one = [
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
]

two = [
b,b,b,b,b,b,b,b,
b,g,g,b,b,b,b,b,
b,g,g,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,g,g,b,b,
b,b,b,b,g,g,b,b,
b,b,b,b,b,b,b,b,
]

three = [
g,g,b,b,b,b,b,b,
g,g,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,g,g,
b,b,b,b,b,b,g,g,
]

four = [
b,b,b,b,b,b,b,b,
b,g,g,b,b,g,g,b,
b,g,g,b,b,g,g,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,g,g,b,b,g,g,b,
b,g,g,b,b,g,g,b,
b,b,b,b,b,b,b,b,
]

five = [
g,g,b,b,b,b,g,g,
g,g,b,b,b,b,g,g,
b,b,b,b,b,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,g,g,b,b,b,
b,b,b,b,b,b,b,b,
g,g,b,b,b,b,g,g,
g,g,b,b,b,b,g,g,
]

six = [
r,r,b,b,b,b,r,r,
r,r,b,b,b,b,r,r,
b,b,b,b,b,b,b,b,
r,r,b,b,b,b,r,r,
r,r,b,b,b,b,r,r,
b,b,b,b,b,b,b,b,
r,r,b,b,b,b,r,r,
r,r,b,b,b,b,r,r,
]

def roll_dice():
    r = random.randint(1,6)
    if r == 1:
        sense.set_pixels(one)
    elif r == 2:
        sense.set_pixels(two)
    elif r == 3:
        sense.set_pixels(three)
    elif r == 4:
        sense.set_pixels(four)
    elif r == 5:
        sense.set_pixels(five)
    elif r == 6:
        sense.set_pixels(six)

try:
    sense.clear()
    sense.show_message("Shake...")
    print("Shake to Roll the Die")
    start_time = time.time()
    next_roll = True
    while True:
        x, y, z = sense.get_accelerometer_raw().values()
        x1 = abs(x)
        y1 = abs(y)
        z1 = abs(z)
        time.sleep(0.1)
        x, y, z = sense.get_accelerometer_raw().values()
        dx = abs(abs(x1) - abs(x))
        dy = abs(abs(y1) - abs(y))
        dz = abs(abs(z1) - abs(z))
        if dx > accel_thresh or dy > accel_thresh or dz > accel_thresh:
            if next_roll:
                roll_dice()
                next_roll = False
                start_time = time.time()
                if debug:
                    print("accel base  x1=%0.3f y1=%0.3f z1=%0.3f" %( x1, y1, z1 ))
                    print("accel delta dx=%0.3f dy=%0.3f dz=%0.3f" %( dx, dy, dz ))
                print("Shake ...")
        if time.time() - start_time > shake_timer:
            next_roll = True
            sense.clear() 
except:
   print("Bye ...")
   sense.clear()

New sensors!

Hello there fellow dev. There are now new sensors on the AstroPi which means this is outdated. The temperature might be read from the humidity sensor as well from the pressure sensor. Check to see which one is more accurate in your case!
Thank you for your time.

Updated conway.py to life_cycles.py that uses shake to restart. Much better

This is the new code for life_cycles.py. It works much better for restarting by shaking RPI with a sense-hat.

#!/usr/bin/python
print("life_cycles.py ver 1.1")

"""
Original program Conway.py modified by
Claude Pageau    [email protected]
01-Dec-2016
Added shake to restart new Life Cycle

"""

from itertools import product
from random import choice
from time import sleep
from sense_hat import SenseHat

sense = SenseHat()
accel_thresh = 0.4

#-----------------------------------------------------------------------------------------------  
class GameOfLife(object):
    def __init__(self, width, height):
        self.size = (width, height)
        self.random_world()

    def __str__(self):
        width, height = self.size
        return '\n'.join(
            ' '.join(
                self.draw_cell(x, y) for x in range(width)
            )
            for y in range(height)
        )

    def __iter__(self):
        return self

    def __next__(self):
        self.evolve_world()
        return self

    next = __next__

    def evolve_cell(self, cell):
        alive = cell in self.live_cells
        neighbours = self.count_neighbours(cell)
        return neighbours == 3 or (alive and neighbours == 2)

    def count_neighbours(self, cell):
        x, y = cell
        deltas = set(product([-1, 0, 1], repeat=2)) - set([(0, 0)])
        neighbours = ((x + dx, y + dy) for (dx, dy) in deltas)
        return sum(neighbour in self.live_cells for neighbour in neighbours)

    def evolve_world(self):
        width, height = self.size
        world = product(range(width), range(height))
        self.live_cells = {cell for cell in world if self.evolve_cell(cell)}

    def random_world(self):
        width, height = self.size
        world = product(range(width), range(height))
        self.live_cells = {cell for cell in world if choice([0, 1])}

    def draw_cell(self, x, y):
        cell = (x, y)
        return 'O' if cell in self.live_cells else ' '

    def get_cell_color(self, x, y):
        cell = (x, y)
        red = (255, 0, 0)
        black = (0, 0, 0)
        return red if cell in self.live_cells else black

    def update(self):
        width, height = self.size
        for x in range(width):
            for y in range(height):
                color = self.get_cell_color(x, y)
                sense.set_pixel(x, y, color)
                
#-----------------------------------------------------------------------------------------------                  
def detect_shake(thresh):
    restart = False
    x, y, z = sense.get_accelerometer_raw().values()
    x1 = abs(x)
    y1 = abs(y)
    z1 = abs(z)
    sleep(0.1)
    x, y, z = sense.get_accelerometer_raw().values()
    dx = abs(abs(x1) - abs(x))
    dy = abs(abs(y1) - abs(y))
    dz = abs(abs(z1) - abs(z))
    if dx > thresh or dy > thresh or dz > thresh:
        restart = True
    return  restart    
                
#-----------------------------------------------------------------------------------------------               
def main():
    print("Shake Pi to Restart Life Cycle")
    print("    or ctrl-c to Quit")
    sense.show_message("Shake Restarts")
    life_cycles = 1
    while True:
        msg = str(life_cycles)
        sense.show_message(msg)
        game = GameOfLife(8, 8)
        for i in game:
            game.update()
            if detect_shake(accel_thresh):
                life_cycles += 1
                break      

if __name__ == '__main__':
    try:
        main()
    except:
        print("Thanks for the Life Cycles")
        print("Bye")
        sense.show_message("Thanks for the Life Cycles .. Bye")
        sense.clear()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.