-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrice.py
executable file
·198 lines (164 loc) · 5.83 KB
/
Matrice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Florian DUCORD
# florian.developpement@gmail.com
# https://github.com/pingouin84/Mirror_Led.git
import config
import pygame as pg
import time
#from pygame.locals import *
from math import pi
if not config.DEBUG:
import board
import neopixel as neo
class Matrice:
if not config.DEBUG:
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
PIXEL_PIN = board.D21
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neo.GRB
# The number of NeoPixels
FIELD_WIDTH = 12
FIELD_HEIGHT = 20
NUM_PIXELS = 240
SIZE = [FIELD_WIDTH, FIELD_HEIGHT]
FONT_MANE = "data/fonts/Picopixel.ttf"
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (247, 255, 0)
BTN_RIGHT = (1, 0)
BTN_LEFT = (-1, 0)
BTN_UP = (0, 1)
BTN_DOWN = (0, -1)
BTN_KILL = 256
BTN_START = 11
BTN_SELECT = 12
BTN_A = 0
BTN_B = 1
BTN_X = 3
BTN_Y = 4
BTN_R3 = 14
BTN_R2 = 9
BTN_R1 = 7
BTN_L3 = 13
BTN_L2 = 8
BTN_L1 = 6
BTN_HOME = 3
def __init__(self):
# self.BLACK = 0x000000
# self.BLUE = 0x001F
# self.RED = 0xF80000
# self.GREEN = 0x07E000
# self.CYAN = 0x07FF
# self.MAGENTA = 0xF81F
# self.YELLOW = 0xFFE0
# self.WHITE = 0xFFFFFF
self.colorLib = [self.YELLOW, self.BLUE, self.GREEN, self.RED]
def initMatrice(self):
pg.init()
if not config.DEBUG:
self.screen = pg.display.set_mode(self.SIZE) # ,pg.SCALED)
else:
self.screen = pg.display.set_mode(self.SIZE, pg.SCALED)
pg.display.set_caption("Example code for the draw module")
self.clock = pg.time.Clock()
# self.screen.fill((255,0,0))
if not config.DEBUG:
self.pixels = self.neo.NeoPixel(
self.PIXEL_PIN, self.NUM_PIXELS, brightness=0.2, auto_write=False, pixel_order=self.ORDER
)
#self.pixels.fill((255, 0, 0))
def initControl(self):
# On compte les joysticks
nb_joysticks = pg.joystick.get_count()
# Et on en crée un s'il y a en au moins un
if nb_joysticks > 0:
mon_joystick = pg.joystick.Joystick(0)
mon_joystick.init() # Initialisation
return True
return False
def control(self):
self.clock.tick(10)
for event in pg.event.get(): # User did something
if event.type == pg.QUIT: # If user clicked close
return self.BTN_KILL
elif event.type == pg.JOYHATMOTION:
return event.value
elif event.type == pg.JOYBUTTONDOWN:
print(event.button)
return event.button
# utilisation clavier
elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
return self.BTN_START
elif event.key == pg.K_ESCAPE:
return self.BTN_KILL
elif event.key == pg.K_LEFT:
return self.BTN_LEFT
elif event.key == pg.K_RIGHT:
return self.BTN_RIGHT
else:
print(event.key)
def showPixels(self):
pg.display.flip()
if not config.DEBUG:
self.pixels.show()
def setTablePixel(self, x, y, color):
pg.draw.rect(self.screen, color, [x, y, 1, 1])
#FIELD_WIDTH = 12
#FIELD_HEIGHT = 20
#value = (12-x)*20 + (20-y)
if x % 2 == 0:
# print("pair")
value = ((self.FIELD_WIDTH-x)*self.FIELD_HEIGHT-1) - \
((self.FIELD_HEIGHT-1)-y)
else:
# print("impair")
value = ((self.FIELD_WIDTH-x)*self.FIELD_HEIGHT-1) - y
#value = (self.FIELD_WIDTH-x)*self.FIELD_HEIGHT + (self.FIELD_HEIGHT-y)
#print("{} : {}".format(value,color))
if not config.DEBUG:
self.pixels[value] = tuple(color) # (0,255,0)
def afficher_heure(self):
self.screen.fill(self.BLACK)
#font = pg.font.SysFont ( "free" , 6 )
# font_name = "Pixeled.ttf" #5
# font_name = "Picopixel.ttf" #7
#font = pg.font.SysFont ( font_name , 5 )
font = pg.font.Font(self.FONT_MANE, 7)
#val_time = time.localtime()
val_heure = time.strftime("%H")
val_minute = time.strftime("%M")
val_seconde = time.strftime("%S")
text = font.render(val_heure, True, self.GREEN)
self.screen.blit(text, [3, 0])
text = font.render(val_minute, True, self.GREEN)
self.screen.blit(text, [3, 7])
text = font.render(val_seconde, True, self.GREEN)
self.screen.blit(text, [3, 14])
pg.display.flip()
self.actualiser_led()
def afficher_image(self,path):
image = pg.image.load(path)
self.screen.blit(image, (0,0))
pg.display.flip()
def actualiser_led(self):
surface = pg.Surface(self.SIZE)
pixels = pg.surfarray.array2d(self.screen)
for x in range(11):
for y in range(19):
if x % 2 == 0:
# print("pair")
value = ((self.FIELD_WIDTH-x)*self.FIELD_HEIGHT -
1) - ((self.FIELD_HEIGHT-1)-y)
else:
# print("impair")
value = ((self.FIELD_WIDTH-x)*self.FIELD_HEIGHT-1) - y
if pixels[x, y] > 0:
value = value
if not config.DEBUG:
self.pixels[value] = pixels[x, y] # (0,255,0)
self.pixels.show()