make display class a singleton

This commit is contained in:
Pavol Rusnak 2016-03-31 14:08:13 +02:00
parent 774d6aaee4
commit ee67b0bab7
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 51 additions and 33 deletions

View File

@ -7,7 +7,6 @@
#include <SDL2/SDL.h>
static int SDL_inited = 0;
static SDL_Renderer *RENDERER = 0;
static SDL_Surface *SCREEN = 0;
static SDL_Texture *TEXTURE = 0;
@ -21,7 +20,6 @@ static int ROTATION = 0;
#define DISPLAY_BORDER 8
static void DATAfunc(uint8_t x) {
if (!SDL_inited) return;
if (POSX <= EX && POSY <= EY) {
((uint8_t *)SCREEN->pixels)[POSX * 2 + POSY * SCREEN->pitch + (DATAODD ^ 1)] = x;
}
@ -57,7 +55,6 @@ static int HandleEvents(void *ptr)
static void display_init(void)
{
if (SDL_inited) return;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL_Init Error: %s\n", SDL_GetError());
}
@ -76,11 +73,9 @@ static void display_init(void)
SCREEN = SDL_CreateRGBSurface(0, RESX, RESY, 16, 0xF800, 0x07E0, 0x001F, 0x0000);
TEXTURE = SDL_CreateTexture(RENDERER, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, RESX, RESY);
THREAD = SDL_CreateThread(HandleEvents, "EventThread", NULL);
SDL_inited = 1;
}
static void display_set_window(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
if (!SDL_inited) return;
SX = x; SY = y;
EX = x + w - 1; EY = y + h - 1;
POSX = SX; POSY = SY;
@ -89,7 +84,6 @@ static void display_set_window(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
static void display_update(void)
{
if (!SDL_inited) return;
SDL_RenderClear(RENDERER);
SDL_UpdateTexture(TEXTURE, NULL, SCREEN->pixels, SCREEN->pitch);
const SDL_Rect r = {DISPLAY_BORDER, DISPLAY_BORDER, RESX, RESY};

View File

@ -155,8 +155,11 @@ typedef struct _mp_obj_Display_t {
// def Display.__init__(self):
STATIC mp_obj_t mod_TrezorUi_Display_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
display_init();
mp_obj_Display_t *o = m_new_obj(mp_obj_Display_t);
static mp_obj_Display_t *o = 0; // singleton
if (!o) {
display_init();
o = m_new_obj(mp_obj_Display_t);
}
o->base.type = type;
return MP_OBJ_FROM_PTR(o);
}

44
src/layout.py Normal file
View File

@ -0,0 +1,44 @@
from TrezorUi import Display
def rgb2color(r, g, b):
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
RED = rgb2color(0xF4, 0x43, 0x36)
PINK = rgb2color(0xE9, 0x1E, 0x63)
PURPLE = rgb2color(0x9C, 0x27, 0xB0)
DEEP_PURPLE = rgb2color(0x67, 0x3A, 0xB7)
INDIGO = rgb2color(0x3F, 0x51, 0xB5)
BLUE = rgb2color(0x21, 0x96, 0xF3)
LIGHT_BLUE = rgb2color(0x03, 0xA9, 0xF4)
CYAN = rgb2color(0x00, 0xBC, 0xD4)
TEAL = rgb2color(0x00, 0x96, 0x88)
GREEN = rgb2color(0x4C, 0xAF, 0x50)
LIGHT_GREEN = rgb2color(0x8B, 0xC3, 0x4A)
LIME = rgb2color(0xCD, 0xDC, 0x39)
YELLOW = rgb2color(0xFF, 0xEB, 0x3B)
AMBER = rgb2color(0xFF, 0xC1, 0x07)
ORANGE = rgb2color(0xFF, 0x98, 0x00)
DEEP_ORANGE = rgb2color(0xFF, 0x57, 0x22)
BROWN = rgb2color(0x79, 0x55, 0x48)
GREY = rgb2color(0x9E, 0x9E, 0x9E)
BLUE_GRAY = rgb2color(0x60, 0x7D, 0x8B)
BLACK = rgb2color(0x00, 0x00, 0x00)
WHITE = rgb2color(0xFF, 0xFF, 0xFF)
MONO = 0
NORMAL = 1
BOLD = 2
d = Display()
class Layout(object):
@staticmethod
def show_send(address, amount, currency='BTC'):
d.bar(0, 0, 240, 40, GREEN)
d.bar(0, 40, 240, 200, WHITE)
d.text(10, 28, 'Sending', BOLD, WHITE, GREEN)
d.text(10, 80, '%f %s' % (amount, currency), BOLD, BLACK, WHITE)
d.text(10, 110, 'to this address:', NORMAL, BLACK, WHITE)
d.text(10, 140, address[:18], MONO, BLACK, WHITE)
d.text(10, 160, address[18:], MONO, BLACK, WHITE)

View File

@ -1,29 +1,6 @@
from TrezorUi import Display
def rgb2color(r, g, b):
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
BLACK = 0x0000
WHITE = 0xFFFF
BLUEGRAY = rgb2color(0x80, 0x80, 0x80)
MONO = 0
NORMAL = 1
BOLD = 2
d = Display()
d.bar(0, 0, 240, 40, BLUEGRAY)
d.bar(0, 40, 240, 200, WHITE)
d.text(10, 28, 'Sending', BOLD, WHITE, BLUEGRAY)
d.text(10, 80, '110.126967 BTC', BOLD, BLACK, WHITE)
d.text(10, 110, 'to this address:', NORMAL, BLACK, WHITE)
d.text(10, 140, '1BitkeyP2nDd5oa64x', MONO, BLACK, WHITE)
d.text(10, 160, '7AjvBbbwST54W5Zmx2', MONO, BLACK, WHITE)
from layout import Layout
Layout.show_send('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967)
while True:
pass