def test_radio(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_dsb_chk_rad", pid=13) nexRadio0 = NexRadio(nexSerial, "r0", cid=8) nexRadio1 = NexRadio(nexSerial, "r1", cid=9) nexRadio2 = NexRadio(nexSerial, "r2", cid=10) nexPage.show() nexRadio0.value = False nexRadio1.value = False nexRadio2.value = False assert not nexRadio0.value time.sleep(1) nexRadio0.value = True assert nexRadio0.value time.sleep(1) nexRadio0.value = False assert not nexRadio0.value time.sleep(1) nexSerial.close()
def test_progressbar(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_pbar", pid=6) nexProgressBar = NexProgressBar(nexSerial, "j0", cid=1) nexPage.show() time.sleep(1) nexProgressBar.backcolor = NamedColor.GRAY nexProgressBar.forecolor = NamedColor.GREEN nexProgressBar.value = 30 with pytest.raises(Exception): nexProgressBar.value = 105 # should raise error because value must be in 0-100 with pytest.raises(Exception): nexProgressBar.value = -1 # should raise error because value must be in 0-100 time.sleep(1) nexSerial.close()
def test_qrcode(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_qr", pid=9) nexQRcode = NexQRcode(nexSerial, "qr0", cid=1) nexText = NexText(nexSerial, "t1", cid=3) nexPage.show() time.sleep(1) text = "Hello" nexQRcode.text = text nexText.text = text time.sleep(2) # text = "https://github.com/py3-nextion/pynextion" text = "http://bit.ly/2QRZsuV" nexText.text = text # nexQRcode.textmaxlength = len(text) # nexQRcode.textmaxlength = 50 # time.sleep(1) nexQRcode.text = text assert nexText.text == text assert nexQRcode.text == text nexSerial.close()
def test_slider(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_slider", pid=11) nexSlider = NexSlider(nexSerial, "h0", cid=4) nexPage.show() time.sleep(0.1) nexSlider.value = 43691 # 0-65535 time.sleep(1) # nexSlider.cursor.color = NamedColor.GRAY nexSlider.forecolor = NamedColor.GRAY time.sleep(1) w = 10 nexSlider.cursor.width = w assert nexSlider.cursor.width == w time.sleep(1) h = 13 nexSlider.cursor.height = h assert nexSlider.cursor.height == h time.sleep(1) nexSerial.close()
def test_button(port, delay): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_but", pid=8) nexButtonPlus = NexButton(nexSerial, "b0", cid=1) nexNumber0 = NexNumber(nexSerial, "n0", cid=2) nexButtonMinus = NexButton(nexSerial, "b1", cid=3) nexButtonEnter = NexButton(nexSerial, "b2", cid=4) # noqa: F841 nexPage.show() nexButtonPlus.backcolor = NamedColor.GREEN nexButtonMinus.backcolor = NamedColor.RED # nexNumber0.value = value value = nexNumber0.value dt_start = datetime.datetime.utcnow() while True: time.sleep(0.2) new_value = nexNumber0.value if new_value != value: value = new_value print(value) if datetime.datetime.utcnow() - dt_start > datetime.timedelta( seconds=delay): break nexSerial.close()
def test_checkbox(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_dsb_chk_rad", pid=13) nexCheckbox0 = NexCheckbox(nexSerial, "c0", cid=4) nexCheckbox1 = NexCheckbox(nexSerial, "c1", cid=5) nexCheckbox2 = NexCheckbox(nexSerial, "c2", cid=7) nexPage.show() nexCheckbox0.value = False nexCheckbox1.value = False nexCheckbox2.value = False assert not nexCheckbox0.value time.sleep(1) nexCheckbox0.value = True assert nexCheckbox0.value time.sleep(1) nexCheckbox0.value = False assert not nexCheckbox0.value time.sleep(1) nexSerial.close()
def test_gauge(port): nexSerial = PySerialNex(port) initialized = nexSerial.init() assert initialized nexPage = NexPage(nexSerial, "pg_gauge", pid=10) nexGauge = NexGauge(nexSerial, "z0", cid=3) nexPage.show() nexGauge.backcolor = NamedColor.WHITE nexGauge.forecolor = NamedColor.RED nexGauge.width = 3 # 0-5 # nexGauge.pointer.width = 3 # 0-5 # for angle in range(30, 360 + 30, 30) # time.sleep(1) # nexGauge.value = angle for angle in range(0, 360 + 6, 6): time.sleep(0.1) nexGauge.value = (angle + 90) % 360 time.sleep(1) assert nexGauge.value == 90 nexSerial.close()
def test_number(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_num", pid=7) nexNumber = NexNumber(nexSerial, "n0", cid=1) nexPage.show() time.sleep(1) nexNumber.value = 1 time.sleep(1) n = 2 nexNumber.value = n nexNumber.backcolor = NamedColor.RED nexNumber.forecolor = NamedColor.WHITE time.sleep(1) assert nexNumber.value == n time.sleep(1) nexNumber.value = 3 nexNumber.backcolor = NamedColor.WHITE nexNumber.forecolor = sign_color(n) time.sleep(1) n = -2 nexNumber.value = n nexNumber.forecolor = sign_color(n) time.sleep(1) assert nexNumber.value == n min_val, max_val = limits(True, 32) n = max_val nexNumber.value = n nexNumber.forecolor = sign_color(n) time.sleep(1) assert nexNumber.value == n n = min_val + 1 # display only "-" is n = min_val nexNumber.value = n nexNumber.forecolor = sign_color(n) time.sleep(1) assert nexNumber.value == n nexSerial.close()
def test_scroll(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_scroll", pid=5) nexScrollText = NexScrollText(nexSerial, "g0", pid=1) # nexSerial.write("page pg_scroll") nexPage.show() msg = "Hello Nextion!" nexScrollText.text = msg assert nexScrollText.text == msg time.sleep(10) nexSerial.close()
def test_picture(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_pic", pid=3) nexPicture = NexPicture(nexSerial, "p0", cid=1) nexPage.show() for i in range(1, 8): time.sleep(1) # nexPicture.picture = i nexPicture.picture = Picture(i) # assert nexPicture.picture == Picture(i) # ToDo nexSerial.close()
def test_text(port): nexSerial = PySerialNex(port) print("Init") nexSerial.init() nexPage = nexSerial.components.hook_page("pg_text", pid=2) assert len(list(nexSerial.components.pages)) == 1 nexText = nexPage.hook_widget(NexText, "t1", cid=1) assert len(list(nexPage.widgets)) == 1 nexPage.show() time.sleep(1) msg = "H. by code" # Hook nexText.text = msg nexSerial.close()
def test_text(port): nexSerial = PySerialNex(port) print("Init") nexSerial.init() pages = [ { "pid": 2, "name": "pg_text", "components": [ { "type": "Text", "cid": 2, "name": "t0" }, { "type": "Text", "cid": 3, "name": "t1" }, { "type": "Text", "cid": 1, "name": "t2" } ] } ] nexSerial.components.read_list(pages) nexPage = nexSerial.components.page(name="pg_text") nexPage.show() time.sleep(1) nexText = nexPage.widget(name="t1") msg = "H. by data" # Hook nexText.text = msg nexSerial.close()
def test_button(port, delay): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "pg_but", pid=8) nexButtonEnter = NexButton(nexSerial, "b2", cid=4) nexButtonEnter.callback = callback # button.when_pressed = callback_when_pressed # button.when_released = callback_when_released nexPage.show() dt_start = datetime.datetime.utcnow() while True: nexSerial.poll() if datetime.datetime.utcnow() - dt_start > datetime.timedelta(seconds=delay): break time.sleep(0.2) nexSerial.close()
def test_page(port): nexserial = PySerialNex(port) nexserial.init() pages = ["page0", "page1"] assert len(pages) == 2 pages = [ "page0", "page1", "pg_text", "pg_pic", "pg_var", "pg_scroll", "pg_pbar", "pg_num", "pg_but", "pg_qr", "pg_gauge", "pg_slider", "pg_waveform", "pg_dsb_chk_rad", "pg_hotspot", "pg_crop", "pg_timer", "pg_num_kb", "keybdB", "pg_text_kb", "keybdC", "keydbA" ] assert len(pages) == 22 nexpages = [] for i, name in enumerate(pages): page = NexPage(nexserial, name, i) nexpages.append(page) for i, page in enumerate(nexpages): print(page.show()) assert nexserial.current_page == i time.sleep(0.5) page = nexpages[0] page.show() time.sleep(2) assert nexserial.current_page == page.pid assert nexpages[0].ishown() assert not nexpages[1].ishown() nexserial.close()
def test_draw(port): nexSerial = PySerialNex(port) nexSerial.init() nexPage = NexPage(nexSerial, "page0", pid=0) nexPage.show() cls(nexSerial, DEFAULT_COLOUR) def print_several_lines(nexSerial, lcd_width=LCD_WIDTH, color1=NamedColor.WHITE, color2=NamedColor.BLACK): def swap(x, y): return y, x x, y = 0, 10 w, h = lcd_width, 30 font = Font(0) fontcolor = color1 backcolor = color2 xcenter = Alignment.Horizontal.CENTRE # NONE/LEFT/CENTRE/RIGHT ycenter = Alignment.Vertical.CENTRE # NONE/UP/CENTRE/DOWN sta = Background.SOLIDCOLOUR # NONE/CROPIMAGE/SOLIDCOLOUR/IMAGE/NOBACKCOLOUR rows = [ "Hello Python!", "", "This is Nextion", "", "I'm an intelligent display", "", "", "", "https://github.com/scls19fr", "/pynextion" ] for row in rows: print(nexSerial, row, x, y, w, h, font, fontcolor, backcolor, xcenter, ycenter, sta) xstr(nexSerial, row, x, y, w, h, font, fontcolor, backcolor, xcenter, ycenter, sta) y = y + h fontcolor, backcolor = swap(fontcolor, backcolor) def diagonal_line(nexSerial, lcd_width=LCD_WIDTH, lcd_height=LCD_HEIGHT): x1, y1 = 0, 0 x2, y2 = lcd_width, lcd_height colour = NamedColor.BLUE line(nexSerial, x1, y1, x2, y2, colour) def circle_frame_fill(nexSerial, lcd_width=LCD_WIDTH, lcd_height=LCD_HEIGHT, delay=1): x = int(round(lcd_width / 2)) y = int(round(lcd_height / 2)) r = 100 colour = NamedColor.BLUE circle(nexSerial, x, y, r, colour) time.sleep(delay) circle(nexSerial, x, y, r, colour, Background.SOLIDCOLOUR) def clear_screen_color_france(nexSerial, delay=1): cls(nexSerial, NamedColor.BLUE) time.sleep(delay) cls(nexSerial, NamedColor.WHITE) time.sleep(delay) cls(nexSerial, NamedColor.RED) def france_flag(nexSerial, lcd_width=LCD_WIDTH, lcd_height=LCD_HEIGHT): w, h = int(round(lcd_width / 3)), lcd_height x1, y1 = 0, 0 y2 = h x2 = x1 + w colour = NamedColor.BLUE rectangle(nexSerial, x1, y1, x2, y2, colour, Background.SOLIDCOLOUR) x1 = x1 + 2 * w x2 = x1 + w colour = NamedColor.RED rectangle(nexSerial, x1, y1, x2, y2, colour, Background.SOLIDCOLOUR) def rectangle_frame_fill(nexSerial, lcd_width=LCD_WIDTH, lcd_height=LCD_HEIGHT): w, h = 120, 80 x1 = int(round((lcd_width - w) / 2)) y1 = int(round((lcd_height - h) / 2)) x2 = x1 + w y2 = y1 + h colour = NamedColor.RED rectangle(nexSerial, x1, y1, x2, y2, colour) time.sleep(1) rectangle(nexSerial, x1, y1, x2, y2, colour, Background.SOLIDCOLOUR) def picture_example(nexSerial, lcd_width=LCD_WIDTH, lcd_height=LCD_HEIGHT, img_width=IMG_WIDTH, img_height=IMG_HEIGHT, delay=0.5): x = int(round((lcd_width - img_width) / 2)) y = int(round((lcd_height - img_height) / 2)) for picid in range(8): picture(nexSerial, x, y, Picture(picid)) time.sleep(delay) cls(nexSerial) print_several_lines(nexSerial) time.sleep(2) cls(nexSerial, DEFAULT_COLOUR) diagonal_line(nexSerial) time.sleep(2) cls(nexSerial) circle_frame_fill(nexSerial) time.sleep(2) cls(nexSerial) clear_screen_color_france(nexSerial) time.sleep(2) cls(nexSerial) france_flag(nexSerial) time.sleep(2) cls(nexSerial) time.sleep(1) rectangle_frame_fill(nexSerial) time.sleep(2) cls(nexSerial) picture_example(nexSerial) nexSerial.close()
def test_NexSlider(port): nexSerial = PySerialNex(port) print("Init") nexSerial.init() print("Create objects") # nexText = NexText(nexSerial, "t1", pid=2, cid=1) # nexText = NexText(nexSerial, "t1") nexText = NexText(nexSerial, "t1", cid=1) print("Reset") nexSerial.reset() time.sleep(1) nexSerial.send("page pg_text") time.sleep(1) msg = "Hello" nexText.text = msg time.sleep(1) assert nexText.text == msg time.sleep(0.5) nexText.backcolor = Colour.BLUE nexText.text = "1" time.sleep(0.5) nexText.backcolor = Colour.WHITE nexText.text = "2" time.sleep(0.5) nexText.backcolor = Colour.RED nexText.text = "3" time.sleep(0.5) nexText.text = "Bye nexText" time.sleep(0.5) nexText.visible = False nexText.backcolor = Colour.WHITE time.sleep(2) nexText.visible = True time.sleep(0.5) nexText.alignment.horizontal = Alignment.Horizontal.RIGHT nexText.alignment.vertical = Alignment.Vertical.DOWN nexText.forecolor = Colour.BLUE # Change fontid from 0 to 1 nexText.font = Font(1) # nexSerial.reset() # time.sleep(0.5) nexSerial.close()