def log(program, function, msg, level="I", display=True): global LAST_SHOWN # open syslog syslog.openlog("system: %s %s:%s:" % (level, program, function)) # set priority priority = syslog.LOG_INFO if level == "E": priority = syslog.LOG_ERR elif level == "W": priority = syslog.LOG_WARNING priority |= syslog.LOG_LOCAL4 # write to syslog syslog.syslog(priority, msg) # # NOTE: showlog / showlog -f to check the logs # if display: program_display = " %s: " % program displayed = " " # If loglevel is anything else than I, add it to our tag if level != "I": displayed += "[%s] " % level displayed += utf8_str(msg) # print using FBInk (via cFFI) fbink.fbink_print(fbink.FBFD_AUTO, "%s\n%s" % (program_display, displayed), FBINK_CFG)
def mprint(string, row=0, col=0): fbink_cfg.row = row fbink_cfg.col = col fbink.fbink_print(fbfd, string, fbink_cfg) fbink_cfg.row = 0 fbink_cfg.col = 0 return True
def log(program, function, msg, level="I", display=True): global LAST_SHOWN # open syslog syslog.openlog("system: %s %s:%s:" % (level, program, function)) # set priority priority = syslog.LOG_INFO if level == "E": priority = syslog.LOG_ERR elif level == "W": priority = syslog.LOG_WARNING priority |= syslog.LOG_LOCAL4 # write to syslog syslog.syslog(priority, msg) # # NOTE: showlog / showlog -f to check the logs # if display: # NOTE: FBInk takes a const char*, that's explicitly bytes in Python 3! program_display = " %s: " % program tag = "" # If loglevel is anything else than I, add it to our tag if level != "I": tag += "[%s] " % level message = unicode_str(msg) # print using FBInk (via cFFI) msg_as_bytes = bstr("{}\n{} {}".format(program_display, tag, message)) fbink.fbink_print(fbink.FBFD_AUTO, msg_as_bytes, FBINK_CFG)
def printHelp(): fbink_cfg.row = 0 fbink_cfg.col = 0 FBInk.fbink_print(fbfd, " WOLFRAMALPHA FOR KOBO", fbink_cfg) fbink_cfg.row = 2 FBInk.fbink_print(fbfd, "You can now enter you query", fbink_cfg) fbink_cfg.row = 3 FBInk.fbink_print(fbfd, "Or type 'reboot' to reboot the device", fbink_cfg) fbink_cfg.row = 5 FBInk.fbink_print(fbfd, ">", fbink_cfg) fbink_cfg.col = 1
(x, y, err) = t.getInput() if isKeyboardMode: if err != None: continue k = vk.getPressedKey(x, y) if k == None: continue if not k["isKey"]: continue if k["keyType"] == osk.KTstandardChar: if upperCase: key = str(k["keyCode"]).upper() else: key = str(k["keyCode"]).lower() runeStr = runeStr + key FBInk.fbink_print(fbfd, str(runeStr), fbink_cfg) elif k["keyType"] == osk.KTbackspace: if len(runeStr) > 0: # removing last element and drawing and empty space instead runeStr = runeStr[:-1] FBInk.fbink_print(fbfd, str(runeStr) + " ", fbink_cfg) elif k["keyType"] == osk.KTcapsLock: if upperCase: upperCase = False else: upperCase = True elif k["keyType"] == osk.KTcarriageReturn: if runeStr == "reboot": fbink_cfg.is_centered = True fbink_cfg.is_halfway = True FBInk.fbink_print(fbfd, "Rebooting...", fbink_cfg)
def mprompt(title, text, filePath="temp_mprompt.png", screen_width=default_screenWidth, screen_height=default_screenHeight): """ Pauses the app, displays a popup with an on-screen-keyboard Then restore a screen dump from before the popup appeared Then returns the string) """ # Setup the config... fbink_cfg = ffi.new("FBInkConfig *") # Open the FB... fbfd = fbink.fbink_open() fbink.fbink_init(fbfd, fbink_cfg) # INITIALIZING TOUCH t = KIP.inputObject(touchPath, screen_width, screen_height) # INITIALIZING KEYBOARD with open(mainFolder + 'sample-keymap-en_us.json') as json_file: km = json.load(json_file) vk = osk.virtKeyboard(km, screen_width, screen_height) # Generate an image of the OSK vkPNG = "img/vk.png" vk.createIMG(vkPNG) # Init : pp_width = int(4 * screen_width / 5) pp_height = int(screen_height / 3) start_coord_x = int(0.5 * screen_width / 5) start_coord_y = int(1 * screen_width / 3) img = Image.new('L', (pp_width + 1, pp_height + 1), color=white) mpopup_img = ImageDraw.Draw(img, 'L') # Main rectangle: mpopup_img.rectangle([(0, 0), (pp_width, pp_height)], fill=pp_color, outline=pp_outline) # Main separation lines: mpopup_img.line([0, int(pp_height / 4), pp_width, int(pp_height / 4)], pp_outline) # Title and text mpopup_img.line( [0, int(3.2 * pp_height / 4), pp_width, int(3.2 * pp_height / 4)], pp_outline) # text and buttons # Main texts title_w, title_h = mpopup_img.textsize(title, font=small_font_bold) mpopup_img.text((int(pp_width / 2 - 0.5 * title_w), int(pp_height / 8 - 0.5 * title_h)), title, font=small_font_bold, fill=black) text_w, text_h = mpopup_img.textsize(text, font=small_font) mpopup_img.text( (int(pp_width / 2 - 0.5 * text_w), int(pp_height / 2 - 0.5 * text_h)), text, font=small_font, fill=black) # Saving to a file : img.save(filePath) # Making a copy of the screen fbink_dumpcfg = ffi.new("FBInkDump *") fbink.fbink_region_dump(fbfd, 0, 0, screen_width, screen_height, fbink_cfg, fbink_dumpcfg) # Displaying image of the popup fbink.fbink_print_image(fbfd, str(filePath).encode('ascii'), start_coord_x, start_coord_y, fbink_cfg) # Displaying image of the OSK fbink.fbink_print_image(fbfd, str(vkPNG).encode('ascii'), int(vk.StartCoords["X"]), int(vk.StartCoords["Y"]), fbink_cfg) # Listening for touch in one of a button's area lastTouch = time() lastTouchArea = [-3, -3, -2, -2] runeStr = "" upperCase = False # For an easy example, we only print it "manually" at the correct place. # You may want to print it using a better font and at a fixed coordinates # Should you want it, the text should be printed at the following coordinates: # x = start_coord_x + int(3.2*pp_height/4) + 10 # y = start_coord_y + 10 fbink_cfg.row = 32 fbink_cfg.col = 6 while True: # try: (x, y, err) = t.getInput() if time() - lastTouch > 0.2 or not coordsInArea(x, y, lastTouchArea): # Simple yet effective debounce system lastTouchArea = [x - 7, y - 7, x + 7, y + 7] k = vk.getPressedKey(x, y) if k != None: if not k["isKey"]: continue if k["keyType"] == osk.KTstandardChar: if upperCase: key = str(k["keyCode"]).upper() else: key = str(k["keyCode"]).lower() runeStr = runeStr + key fbink.fbink_print(fbfd, str(runeStr).encode('ascii'), fbink_cfg) elif k["keyType"] == osk.KTbackspace: if len(runeStr) > 0: # removing last element and drawing and empty space instead runeStr = runeStr[:-1] fbink.fbink_print( fbfd, str(str(runeStr) + " ").encode('ascii'), fbink_cfg) elif k["keyType"] == osk.KTcapsLock: if upperCase: upperCase = False else: upperCase = True elif k["keyType"] == osk.KTcarriageReturn: #Closing touch file t.close() #Restoring dump fbink.fbink_restore(fbfd, fbink_cfg, fbink_dumpcfg) fbink.fbink_close(fbfd) return runeStr else: continue # except: # print("Bad touch event") # continue return True
# Do stuff! if FBInk.fbink_print(fbfd, b"Hello World", fbink_cfg) < 0: print("Failed to print that string!", file=sys.stderr) # And now we can wind things down... if FBInk.fbink_close(fbfd) < 0: raise SystemExit("Failed to close the framebuffer, aborting . . .") """ # Or, the same but in a slightly more Pythonic approach ;). fbfd = FBInk.fbink_open() try: FBInk.fbink_init(fbfd, fbink_cfg) # NOTE: On Python 3, cFFI maps char to bytes, not str FBInk.fbink_print(fbfd, b"Hello World", fbink_cfg) # And a few other random examples... """ # A full-screen, flashing refresh fbink_cfg.is_flashing = True FBInk.fbink_refresh(fbfd, 0, 0, 0, 0, fbink_cfg) fbink_cfg.is_flashing = False # A (fairly useless) dump & restore cycle (with nightmode enabled for a free inversion) dump = ffi.new("FBInkDump *") FBInk.fbink_region_dump(fbfd, 350, 350, 250, 250, fbink_cfg, dump) fbink_cfg.is_nightmode = True
# And now we're good to go! Let's print "Hello World" in the center of the screen... # Setup the config... fbink_cfg = ffi.new("FBInkConfig *") fbink_cfg.is_centered = True fbink_cfg.is_halfway = True # Open the FB... fbfd = FBInk.fbink_open() FBInk.fbink_init(fbfd, fbink_cfg) #FBInk.fbink_close(fbfd) touchPath = "/dev/input/event1" t = KIP.inputObject(touchPath, 1080, 1440) FBInk.fbink_print(fbfd, b"Test ! Have Fun... Starting in 5 secs", fbink_cfg) FBInk.fbink_close(fbfd) time.sleep(5) fbfd = FBInk.fbink_open() FBInk.fbink_print(fbfd, b"Started", fbink_cfg) FBInk.fbink_close(fbfd) while True: (x,y,err) = t.getInput() print(x,y) fbfd = FBInk.fbink_open() FBInk.fbink_print(fbfd, str(x) + " - " + str(y), fbink_cfg)