Exemplo n.º 1
0
def restart():
	""" Restart the drawing """
	global triangle
	# Clear the screen
	gd.fill(RAM_SPRIMG, 0, 16384);

 	#Position triangle at random
	for i in range( 3 ):
		triangle.points[i].x = 3 + urandom.randrange(0, 250)
		triangle.points[i].y = 3 + urandom.randrange(0, 250)

	# Define triangle
	# Improvement regarding the original code is the random move coeficient
	# "* urandom.randrange(1,5)". But it will also create some minor glicth
	# 
	triangle.points[0].xv = 1
	triangle.points[0].yv = 1
	# triangle.points[1].xv = -1
	triangle.points[1].xv = -1 * urandom.randrange(1,5)
	triangle.points[1].yv = 1
	triangle.points[2].xv = 1
	# triangle.points[2].yv = -1
	triangle.points[2].yv = -1 * urandom.randrange(1,5)

	# Choose a random palette
	gd.wr16(PALETTE4A, rgb(0,0,0));
	gd.wr16(PALETTE4A + 2, random_rgb())
	gd.wr16(PALETTE4A + 4, random_rgb())
	gd.wr16(PALETTE4A + 6, random_rgb())
Exemplo n.º 2
0
def move_snake(snake_dir):
    global snake
    global apple
    global score

    if snake_dir == 'e':
        head = snake[0][0] - 6
        print(head)
        snake.insert(0, [head, snake[0][1]])
        time.sleep_ms(50)

    elif snake_dir == 'w':
        head = snake[0][0] + 6
        snake.insert(0, [head, snake[0][1]])
        time.sleep_ms(50)

    elif snake_dir == 'n':
        head = snake[0][1] - 6
        snake.insert(0, [snake[0][0], head])
        time.sleep_ms(50)

    elif snake_dir == 's':
        head = snake[0][1] + 6
        snake.insert(0, [snake[0][0], head])
        time.sleep_ms(50)

    snake.pop()

    if (snake[0][0] > 128) or (snake[0][0] < 0) or (snake[0][1] >
                                                    64) or (snake[0][1] < 16):
        print('bump!')
        snake[0] = [64, 32]
        snake_dir = 'w'

    if (apple[0] < snake[0][0] + 5) and (apple[0] + 3 > snake[0][0]) and (
            apple[1] < snake[0][1] + 5) and (apple[1] + 3 > snake[0][1]):
        print('yummy!')
        oled.invert(1)
        time.sleep(0.01)
        oled.invert(0)
        time.sleep(0.01)
        score += 1

        #extend body after apple
        snake.insert(0, [snake[0][0], snake[0][1]])

        #new apple
        apple = [urandom.randrange(18, 120), urandom.randrange(18, 55)]

    print('--------------')
    #print(snake)

    oled.fill(0)
    oled.rect(0, 16, 128, 48, 1)
    oled.text('Apples %i' % score, 0, 0)
    oled.rect(apple[0], apple[1], 3, 3, 1)
    for segment in snake:
        oled.fill_rect(segment[0], segment[1], 5, 5, 1)
    oled.show()
Exemplo n.º 3
0
 def update(self):
     self.V = randrange(118, 138)/10
     self.A = randrange(-40, 40)/10
     AHtemp = self.A * pyb.elapsed_millis(self.timer) / 3600000
     self.AH += AHtemp
     # set current AH value and adjust if beyond limits
     if self.AH < 0:
         self.AH = 0
     elif self.AH > self.Battery_AH_Capacity:
         self.AH = self.Battery_AH_Capacity
     self.AH_15 += AHtemp
     self.AH_60 += AHtemp
     self.AH_day += AHtemp
     self.timer = pyb.millis()
     self.capacity()
Exemplo n.º 4
0
 def update(self):
     self.V = randrange(118, 138) / 10
     self.A = randrange(-40, 40) / 10
     AHtemp = self.A * pyb.elapsed_millis(self.timer) / 3600000
     self.AH += AHtemp
     # set current AH value and adjust if beyond limits
     if self.AH < 0:
         self.AH = 0
     elif self.AH > self.Battery_AH_Capacity:
         self.AH = self.Battery_AH_Capacity
     self.AH_15 += AHtemp
     self.AH_60 += AHtemp
     self.AH_day += AHtemp
     self.timer = pyb.millis()
     self.capacity()
Exemplo n.º 5
0
    def update(self):

        for x in range(led.WIDTH):
            for y in range(led.HEIGHT - 1):
                led.led_matrix[x][y] = led.led_matrix[x][y + 1]

        for x in range(led.WIDTH):
            if led.led_matrix[x][led.HEIGHT - 2][2] == 0 and (
                    urandom.randrange(self.scale) == 0):
                led.led_matrix[x][led.HEIGHT - 1] = [(94, 16, 255),
                                                     (93, 25, 255),
                                                     (95, 31, 253),
                                                     (123, 43, 255)
                                                     ][urandom.randrange(4)]
            else:
                led.led_matrix[x][led.HEIGHT - 1] = (0, 0, 0)
Exemplo n.º 6
0
    def update(self):

        for i in range(self.scale):
            x = urandom.randrange(led.WIDTH)
            y = urandom.randrange(led.HEIGHT)
            if led.led_matrix[x][y][2] == 0:
                led.led_matrix[x][y] = (ord(uos.urandom(1)), 255, 255)

        for x in range(led.WIDTH):
            for y in range(led.HEIGHT):
                this_color = led.led_matrix[x][y]
                if this_color[2] > self.step:
                    led.led_matrix[x][y] = (this_color[0], this_color[1],
                                            this_color[2] - self.step)
                else:
                    led.led_matrix[x][y] = (0, 0, 0)
Exemplo n.º 7
0
def draw_column(gd, dst):
    """ Draw a random 8-character wide background column at picture RAM dst """
    # byte y, x, ch;

    # Clouds and sky, 11 lines
    rect(gd, dst, 0, 0, 8, 11)  #8, 11

    # bottom plain sky, lines 11-28
    ch = single(0, 11)
    for y in range(11, 28):  # for (y = 11; y < 28; y++)
        gd.fill(dst + (y << 6), ch, 8)

    # randomly choose between background elements
    what = urandom.randrange(0, 256)
    if (what < 10):
        # big mushroom thing
        y = urandom.randrange(11, 18)
        rect(gd, dst + atxy(0, y), 8, 18, 8, 9)
        y += 9
        i = 0
        while (y < 28):
            rect(gd, dst + atxy(0, y), 8, 23 + (i & 3), 8, 1)
            i += 1
            y += 1
    elif (what < 32):
        # pair of green bollards
        for x in range(0, 8, 4):  # for (x = 0; x < 8; x += 4) {
            y = urandom.randrange(20, 25)
            rect(gd, dst + atxy(x, y), 6, 11, 4, 3)
            y += 3
            while (y < 28):
                rect(gd, dst + atxy(x, y), 6, 13, 4, 1)
                y += 1
    else:
        # hills
        for x in range(0, 8, 2):  # for (x = 0; x < 8; x += 2) {
            y = urandom.randrange(20, 25)
            rect(gd, dst + atxy(x, y), 4, 11, 2, 3)
            y += 3
            while (y < 28):
                rect(gd, dst + atxy(x, y), 4, 13, 2, 1)
                y += 1

    #  foreground blocks
    x = urandom.randrange(5)
    y = urandom.randrange(11, 24)
    blk = urandom.randrange(4)
    rect(gd, dst + atxy(x, y), blk * 4, 14, 4, 3)
    y += 3
    while (y < 28):
        rect(gd, dst + atxy(x, y), blk * 4, 17, 4, 1)
        y += 1

    # Ground, line 28
    ch = single(0, 18)
    gd.fill(dst + atxy(0, 28), ch, 8)
    # Underground, line 29
    ch = single(0, 19)
    gd.fill(dst + atxy(0, 29), ch, 8)
Exemplo n.º 8
0
 def render(self, oled):
     oled.fill(0)
     add_drip = True
     new_drips = []
     for drip in self.drips:
         x = drip[0]
         dy = drip[2]
         y = drip[1] + dy
         if y < 5:
             add_drip = False
         if y >= 1:
             oled.vline(x - 1, max(0, y - 2), min(2, y), 1)
             oled.vline(x + 1, max(0, y - 2), min(2, y), 1)
         oled.vline(x, max(0, y - 4), min(5, y + 1), 1)
         if y <= SCREEN_H:
             new_drips.append((x, y, dy))
     oled.show()
     if add_drip:
         x = urandom.randrange(SCREEN_W)
         y = 0
         dy = urandom.randrange(1, 7)
         new_drips.append((x, y, dy))
     self.drips = new_drips
     self.ui.schedule_render(50)
Exemplo n.º 9
0
    def update(self):

        if self.loading_flag:
            self.loading_flag = False
            self.generate_line()

        if self.pcnt >= 100:

            for y in range(led.HEIGHT - 1, 0, -1):
                for x in range(led.WIDTH):
                    if y <= 7:
                        self.matrix_value[y][x] = self.matrix_value[y - 1][x]

            for x in range(led.WIDTH):
                self.matrix_value[0][x] = self.line[x]

            self.generate_line()
            self.pcnt = 0

        for y in range(led.HEIGHT - 1, 0, -1):
            for x in range(0, led.WIDTH):
                if y < 8:
                    led.led_matrix[x][y] = (
                        int(self.hue_rotation + self.hue_mask[y][x]), 255,
                        int(
                            max(0, ((
                                (100.0 - self.pcnt) * self.matrix_value[y][x] +
                                self.pcnt * self.matrix_value[y - 1][x]) /
                                    100.0) - self.value_mask[y][x])))
                elif y == 8 and self.sparkles:
                    if (urandom.randrange(0, 20)
                            == 0) and led.led_matrix[x][y - 1][2] > 0:
                        led.led_matrix[x][y] = led.led_matrix[x][y - 1]
                    else:
                        led.led_matrix[x][y] = (0, 0, 0)
                elif self.sparkles:
                    if led.led_matrix[x][y - 1][2] > 0:
                        led.led_matrix[x][y] = led.led_matrix[x][y - 1]
                    else:
                        led.led_matrix[x][y] = (0, 0, 0)

        for x in range(led.WIDTH):
            led.led_matrix[x][0] = (
                int(self.hue_rotation + self.hue_mask[0][x]), 255,
                int(((100.0 - self.pcnt) * self.matrix_value[0][x] +
                     self.pcnt * self.line[x]) / 100.0))

        self.pcnt = self.pcnt + 30
Exemplo n.º 10
0
    def update(self):

        for x in range(led.WIDTH):
            this_color_v = led.led_matrix[x][led.HEIGHT - 1][2]
            if this_color_v == 0:
                led.led_matrix[x][led.HEIGHT -
                                  1] = (96, 255, 255 *
                                        (urandom.randrange(self.scale) == 0))
            elif this_color_v < self.step:
                led.led_matrix[x][led.HEIGHT - 1] = (0, 0, 0)
            else:
                led.led_matrix[x][led.HEIGHT - 1] = (96, 255,
                                                     this_color_v - self.step)

        for x in range(led.WIDTH):
            for y in range(led.HEIGHT - 1):
                led.led_matrix[x][y] = led.led_matrix[x][y + 1]
Exemplo n.º 11
0
    def update(self):

        if self.loading_flag:
            self.loading_flag = False
            for i in range(self.number):
                self.lighters_pos[0][i] = urandom.randrange(led.WIDTH * 10)
                self.lighters_pos[1][i] = urandom.randrange(led.HEIGHT * 10)
                self.lighters_speed[0][i] = urandom.randrange(-10, 10)
                self.lighters_speed[1][i] = urandom.randrange(-10, 10)
                self.lighters_color[i] = (ord(uos.urandom(1)), 255, 255)

        led.fill_solid(0, 0, 0)

        self.loop_counter = (self.loop_counter + 1) % self.freq

        for i in range(self.number):

            if self.loop_counter == 0:
                self.lighters_speed[0][
                    i] = self.lighters_speed[0][i] + urandom.randrange(-3, 4)
                self.lighters_speed[1][
                    i] = self.lighters_speed[1][i] + urandom.randrange(-3, 4)
                self.lighters_speed[0][i] = func.constrain(
                    self.lighters_speed[0][i], -20, 20)
                self.lighters_speed[1][i] = func.constrain(
                    self.lighters_speed[1][i], -20, 20)

            self.lighters_pos[0][
                i] = self.lighters_pos[0][i] + self.lighters_speed[0][i]
            self.lighters_pos[1][
                i] = self.lighters_pos[1][i] + self.lighters_speed[1][i]

            if self.lighters_pos[0][i] < 0:
                self.lighters_pos[0][i] = (led.WIDTH - 1) * 10

            if self.lighters_pos[0][i] >= led.WIDTH * 10:
                self.lighters_pos[0][i] = 0

            if self.lighters_pos[1][i] < 0:
                self.lighters_pos[1][i] = 0
                self.lighters_speed[1][i] = -self.lighters_speed[1][i]

            if self.lighters_pos[1][i] >= (led.HEIGHT - 1) * 10:
                self.lighters_pos[1][i] = (led.HEIGHT - 1) * 10
                self.lighters_speed[1][i] = -self.lighters_speed[1][i]

            led.led_matrix[self.lighters_pos[0][i] //
                           10][self.lighters_pos[1][i] //
                               10] = self.lighters_color[i]
Exemplo n.º 12
0
def place_balls():
    """ Place all balls so that none collide.  Do this by placing all at random, then moving until there are no collisions """
    global balls, coll

    for i in range(NBALLS):
        balls[i].x = (2 + urandom.randrange(0, 380)) << 4
        balls[i].y = (2 + urandom.randrange(0, 280)) << 4
        balls[i].vx = urandom.randrange(-128, 127)
        balls[i].vy = urandom.randrange(-128, 127)
        balls[i].lasthit = 255

    while anycolliding():
        for i in range(NBALLS):
            if coll[i] != 0xff:
                balls[i].x = (2 + urandom.randrange(0, 380)) << 4
                balls[i].y = (2 + urandom.randrange(0, 280)) << 4
Exemplo n.º 13
0
def random_rgb():
	return rgb( urandom.randrange(0,256), urandom.randrange(0,256), urandom.randrange(0,256) )
Exemplo n.º 14
0
 def generate_line(self):
     for x in range(led.WIDTH):
         self.line[x] = urandom.randrange(64, 255)
Exemplo n.º 15
0

''' TODO with ESP-device with WiFi!
def get_weather():
    r = urequests.get("http://api.openweathermap.org/data/2.5/weather"
                      "?q=%s&appid=%s" % (CITY, API_KEY)).json()
    return r["weather"][0]["icon"], int(r["main"]["temp"] - 273.15)
'''
wait = 0
brightness = 1
while True:
    if wait <= 0:
        clear(lefteye)
        clear(righteye)
        #icon, temp = get_weather()
        temp = float(urandom.randrange(100))  #25.3
        print('T:{0}'.format(temp))
        wait = 10  #60
        #if icon[-1] == 'd':
        brightness = 9
    else:
        brightness = 2

    lefteye.brightness(brightness)
    righteye.brightness(brightness)

    show_text(lefteye, '%2d' % temp)
    #time.sleep(4)
    #show(righteye, ICONS[icon[:2]])
    show(righteye, ICONS['fog'])
Exemplo n.º 16
0
# First 64 use bits 0-1, next 64 use bits 2-4, etc.
# This gives a 256 x 256 4-color bitmap.

for i in range( 256 ):
	x =     72 + 16 * ((i >> 4) & 15)
	y =     22 + 16 * (i & 15)
	image = i & 63         # image 0-63
	pal =   3 - (i >> 6)   # palettes bits in columns 3,2,1,0
	gd.sprite( i, x, y, image, 0x8 | (pal << 1), 0)

restart()
# === Loop =====================================================================
print( "Go go go..." )
color = 0
while True:
	if urandom.randrange(1000) == 0:
		print("Restart drawing...")
		restart()

	line( triangle.points[0].x, triangle.points[0].y, triangle.points[1].x, triangle.points[1].y, color)
	line( triangle.points[1].x, triangle.points[1].y, triangle.points[2].x, triangle.points[2].y, color)
	line( triangle.points[2].x, triangle.points[2].y, triangle.points[0].x, triangle.points[0].y, color)
	color = (color + 1) & 3

	for i in range(3):
		triangle.points[i].x += triangle.points[i].xv
		triangle.points[i].y += triangle.points[i].yv
		if (triangle.points[i].x <= 0) or (triangle.points[i].x >= 255):
			triangle.points[i].xv = -1* triangle.points[i].xv
		if (triangle.points[i].y <= 0) or (triangle.points[i].y >= 255):
			triangle.points[i].yv = -1* triangle.points[i].yv;
Exemplo n.º 17
0
async def another_task(name):
    while True:
        await asyncio.sleep(urandom.randrange(2, 5))
        print("{} woke up".format(name))
        time.sleep_ms(10)  # simulates task doing something
Exemplo n.º 18
0
def random_color():
	return rgb(64 + urandom.randrange(0,192), 64 + urandom.randrange(0,192), 64 + urandom.randrange(0,192))
Exemplo n.º 19
0
#Snake clone for Micropython
#Problems with debouncing rotary encoder

from machine import Pin, I2C
import ssd1306
import time
from rotary_quad_encoder import RotaryQuadEncoder
import urandom

snake = [[10, 10], [15, 10], [20, 10], [25, 10], [30, 10]]
directions = ['e', 'n', 'w', 's']
snake_dir = 'w'
apple = [urandom.randrange(18, 120), urandom.randrange(18, 55)]
score = 0

# ESP32 Pin assignment
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))

# ESP8266 Pin assignment
#i2c = I2C(-1, scl=Pin(5), sda=Pin(4))

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
r = RotaryQuadEncoder(pin1=12,
                      pin2=13,
                      half_steps=False,
                      pins_pull_up=False,
                      track_count=True,
                      reverse=False,
                      range_mode=RotaryQuadEncoder.RANGE_BOUNDED,
Exemplo n.º 20
0
 def randomize_color(self):
     self.red = urandom.randrange(0, 150, 1)
     self.green = urandom.randrange(0, 150, 1)
     self.blue = urandom.randrange(0, 150, 1)
Exemplo n.º 21
0
		sprite.x += sprite.vx;
		sprite.y += sprite.vy;

# Gameduino Lib
gd = Gameduino( spi, ss )
gd.begin()

with open( 'sprites256_pic.bin', 'rb' ) as f:
	gd.copybin( f, RAM_PIC )
with open( 'sprites256_chr.bin', 'rb' ) as f:
	gd.copybin( f, RAM_CHR )
with open( 'sprites256_pal.bin', 'rb' ) as f:
	gd.copybin( f, RAM_PAL )
with open( 'pickups2_img.bin', 'rb' ) as f:
	gd.copybin( f, RAM_SPRIMG )
with open( 'pickups2_pal.bin', 'rb' ) as f:
	gd.copybin( f, RAM_SPRPAL )

# randomize the sprites position
for sprite in sprites:
	sprite.x = urandom.randrange( 0, 400 << 4 )
	sprite.y = urandom.randrange( 0, 300 << 4 )
	sprite.vx = urandom.randrange( -16, 16 )
	sprite.vy = urandom.randrange( -16, 16 )

while True:
	plot(gd)
	move()

print( 'That''s all folks')
Exemplo n.º 22
0
# Draw the rank and file markers 1-8 a-h
for i in range( 8 ):
	gd.wr(atxy(3 + (i << 2), 2) , ord('a') + i)
	gd.wr(atxy(3 + (i << 2), 35), ord('a') + i)
	gd.wr(atxy(0, 5 + (i << 2)) , ord('8') - i)
	gd.wr(atxy(33, 5 + (i << 2)), ord('8') - i)

print( 'Starting board...')
startboard()
drawboard()

# === Loop =====================================================================
while True:
	# Random pause
	iRand    = urandom.randrange(25)
	idxClock = (1 & turn) ^ 1 # select the right clock to update (1 or 0)
	for i in range( iRand ):
		clock[ idxClock ] = clock[ idxClock ] + 1
		gd.waitvblank()
		showclock(0)
		showclock(1)
		sleep_ms(100) # Increase time from 20ms to 100ms

	if (turn < (len(game) / 2)):
		yy = 8 + (turn >> 1);
		xx =  44 if (turn & 1) else 38
		i = 1 + (turn >> 1)
		if (i >= 10):
			gd.wr(atxy(35, yy), int(ord('0') + i / 10) )
		gd.wr(atxy(36, yy), ord('0') + i % 10)
 def play_adhan(self, folder):
     self.play_track(folder,
                     urandom.randrange(1,
                                       self.query_track_count(folder) + 1),
                     waitmillis=ADHAN_TIMEOUT)
Exemplo n.º 24
0
def upy_session_keygen(rsa_key, save_sessionkey=False, token=None):
    if sys.platform == 'esp8266':
        raw_key_list = [
            line.encode() for line in rsa_key.decode().split('\n')[1:-2]
        ]
    else:
        raw_key_list = [line for line in rsa_key.splitlines()[1:-1]]
    raw_key = b''
    for line in raw_key_list:
        raw_key += line
    if token is None:
        random_token = uos.urandom(32)  # send this
    else:
        random_token = token[:32]
    for b in random_token:
        raw_key += bytes(chr(raw_key[b]), 'utf-8')
    key_hash = hashlib.sha256()
    key_hash.update(raw_key)
    hashed_key = key_hash.digest()
    if token is None:
        if sys.platform == 'esp8266':
            index_pvkey = [
                urandom.getrandbits(4) + urandom.getrandbits(4)
                for i in range(32)
            ]
        else:
            index_pvkey = [
                urandom.randrange(0, len(hashed_key)) for i in range(32)
            ]  # send this
    else:
        index_pvkey = token[32:64]
    pv_key = bytes([hashed_key[val] for val in index_pvkey])
    if token is None:
        if sys.platform == 'esp8266':
            index_ivkey = [
                urandom.getrandbits(4) + urandom.getrandbits(4)
                for i in range(16)
            ]
        else:
            index_ivkey = [
                urandom.randrange(0, len(hashed_key)) for i in range(16)
            ]  # send this
    else:
        index_ivkey = token[64:]
    iv = bytes([hashed_key[val] for val in index_ivkey])

    if save_sessionkey:
        if token is None:
            with open('session.key', 'wb') as sess_config:
                sess_keys = dict(SKEY=pv_key, IV=iv)
                sess_config.write(json.dumps(sess_keys))
            print('New session.key saved in the device!')
        else:
            id = 'session{}.key'.format(hexlify(unique_id()).decode())
            with open(id, 'wb') as sess_config:
                sess_keys = dict(SKEY=pv_key, IV=iv)
                sess_config.write(json.dumps(sess_keys))
            print('New {} saved in the device!'.format(id))
    if token is None:
        return (pv_key, iv,
                [random_token + bytes(index_pvkey) + bytes(index_ivkey)])
    else:
        return (pv_key, iv)
Exemplo n.º 25
0
led_colors = [((i >> 2) * gs, (i >> 1 & 1) * gs, (i & 1) * gs)
              for i in range(1, 8)]
disp_colors = [((i >> 2) * 0xff, (i >> 1 & 1) * 0xff, (i & 1) * 0xff)
               for i in range(1, 8)]

nick = 'sample text'
try:
    with open('/nickname.txt') as f:
        nick = f.read()
except:
    pass

while True:
    with display.open() as d:
        for k in range(4):
            (x1, y1) = (randrange(159), randrange(79))
            (x2, y2) = (min(x1 + randrange(40),
                            159), min(y1 + randrange(40), 79))
            try:
                d.rect(x1, y1, x2, y2, col=choice(disp_colors), filled=True)
            except:
                pass
        fg = choice(disp_colors)
        nx = 80 - round(len(nick) / 2 * 14)
        d.print(nick,
                fg=fg,
                bg=[0xff - c for c in fg],
                posx=(nx - 8) + randrange(16),
                posy=22 + randrange(16))
        d.update()
        d.close()
Exemplo n.º 26
0
except ImportError:
    try:
        import random
    except ImportError:
        print("SKIP")
        raise SystemExit

try:
    random.randint
except AttributeError:
    print('SKIP')
    raise SystemExit

print('randrange')
for i in range(50):
    assert 0 <= random.randrange(4) < 4
    assert 2 <= random.randrange(2, 6) < 6
    assert -2 <= random.randrange(-2, 2) < 2
    assert random.randrange(1, 9, 2) in (1, 3, 5, 7)
    assert random.randrange(2, 1, -1) in (1, 2)

# empty range
try:
    random.randrange(0)
except ValueError:
    print('ValueError')

# empty range
try:
    random.randrange(2, 1)
except ValueError:
Exemplo n.º 27
0
    show(righteye, ICONS['10'], 0, 0)
    time.sleep(dt)

    show(lefteye, ICONS['11'], 0, 0)
    show(righteye, ICONS['13'], 0, 0)
    time.sleep(dt)

    show(lefteye, ICONS['fog'], 0, 0)
    clear(righteye)
    time.sleep(dt)

    #cleanup
    clear(lefteye)
    clear(righteye)


# run demo
import urandom
try:
    while True:
        b = urandom.randrange(10)  #pickup random value 0..9
        print('brightness:{0}'.format(b))
        demo(1.5, b)
        time.sleep(4)
except:
    print('Demo interrupted')
    clear(lefteye)
    clear(righteye)

print('Demo done')
Exemplo n.º 28
0
    num = random.randint(1, 4)
    random_log.info(num)

    # random between 0~1
    num = random.random()
    random_log.info(num)

    # urandom.unifrom(start, end)
    # 在开始和结束之间生成浮点数
    num = random.uniform(2, 4)
    random_log.info(num)

    # urandom.randrange(start, end, step)
    # 2-bit binary,the range is [00~11] (0~3)
    num = random.getrandbits(2)
    random_log.info(num)

    # 8-bit binary,the range is [0000 0000~1111 11111] (0~255)
    num = random.getrandbits(8)
    random_log.info(num)

    # urandom.randrange(start, end, step)
    # 从开始到结束随机生成递增的正整数
    num = random.randrange(2, 8, 2)
    random_log.info(num)

    # urandom.choice(obj)
    # 随机生成对象中元素的数量
    num = random.choice("QuecPython")
    random_log.info(num)
Exemplo n.º 29
0
def random_color():
    return rgb(64 + urandom.randrange(0, 192), 64 + urandom.randrange(0, 192),
               64 + urandom.randrange(0, 192))
Exemplo n.º 30
0
        5、
"""

import gc
import time
import ujson
import urandom
import mqtt.simple
import drv.function
import drv.ConnectWIFI

SERVER = "www.chenhao-home.cn"  # MQTT服务器地址
PORT = 1883  # MQTT服务器端口
USER = "******"  # MQTT用户名
PASSWORD = "******"  # MQTT密码
CLIENT_ID = (urandom.randrange(0, 10000000, 8))  # 客户端ID,随机8位数字
TOPIC1 = "domoticz/in"  # Domoticz接收主题
TOPIC2 = "domoticz/out"  # Domoticz发送主题
SSID = "*****************"  # WiFi SSID
WIFI_PASSWORD = "******"  # WiFi 密码
SW_idx = [2, 3, 4, 5]  # domoticz 开关IDX值
SW_Pin = [2, 4, 14, 15]  # 各个开关Pin引脚
THI_idx = [8]
THI_Pin = [13]  # 温湿度采集引脚

# 为紧急异常缓冲区分配RAM(ESP-32下使用)
micropython.alloc_emergency_exception_buf(100)
# 分配最小内存保证系统正常运行
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
# WiFi开始连接
drv.ConnectWIFI.network_connect(SSID, WIFI_PASSWORD)
Exemplo n.º 31
0
try:
    import urandom as random
except ImportError:
    import random

try:
    random.randint
except AttributeError:
    import sys
    print('SKIP')
    sys.exit()

print('randrange')
for i in range(50):
    assert 0 <= random.randrange(4) < 4
    assert 2 <= random.randrange(2, 6) < 6
    assert -2 <= random.randrange(-2, 2) < 2
    assert random.randrange(1, 9, 2) in (1, 3, 5, 7)

print('randint')
for i in range(50):
    assert 0 <= random.randint(0, 4) <= 4
    assert 2 <= random.randint(2, 6) <= 6
    assert -2 <= random.randint(-2, 2) <= 2

print('choice')
lst = [1, 2, 5, 6]
for i in range(50):
    assert random.choice(lst) in lst

print('random')
Exemplo n.º 32
0
lcd.line(0, 0, 95, 67, 1)
lcd.line(95, 0, 0, 67, 1)
lcd.show()

# hourglass lines
lcd.clear()
for i in range(0,96,4):
    lcd.line(0+i, 0, 95-i, 67, 1)
    lcd.show()

# random pixels
import urandom
lcd.clear()
for i in range(50):
    for j in range(50):
        x = urandom.randrange(96)
        y = urandom.randrange(68)
        lcd.pixel(x, y, 1)
    lcd.show()

# random lines
import urandom
lcd.clear()
for i in range(20):
    for j in range(20):
        x1 = urandom.randrange(96)
        y1 = urandom.randrange(68)
        x2 = urandom.randrange(96)
        y2 = urandom.randrange(68)
        lcd.line(x1, y1, x2, y2, 1)
    lcd.show()
Exemplo n.º 33
0
 def gen_new_food(self):
     while True:
         self.food_pos = (urandom.randrange(0, game_width),
                          urandom.randrange(0, game_height))
         if self.food_pos not in self.pos:
             break