4646#include "font_petme128_8x8.h"
4747#include "lcd.h"
4848
49+ /// \moduleref pyb
50+ /// \class LCD - LCD control for the LCD touch-sensor pyskin
51+ ///
52+ /// The LCD class is used to control the LCD on the LCD touch-sensor pyskin,
53+ /// LCD32MKv1.0. The LCD is a 128x32 pixel monochrome screen, part NHD-C12832A1Z.
54+ ///
55+ /// The pyskin must be connected in either the X or Y positions, and then
56+ /// an LCD object is made using:
57+ ///
58+ /// lcd = pyb.LCD('X') # if pyskin is in the X position
59+ /// lcd = pyb.LCD('Y') # if pyskin is in the Y position
60+ ///
61+ /// Then you can use:
62+ ///
63+ /// lcd.light(True) # turn the backlight on
64+ /// lcd.write('Hello world!\n') # print text to the screen
65+ ///
66+ /// This driver implements a double buffer for setting/getting pixels.
67+ /// For example, to make a bouncing dot, try:
68+ ///
69+ /// x = y = 0
70+ /// dx = dy = 1
71+ /// while True:
72+ /// # update the dot's position
73+ /// x += dx
74+ /// y += dy
75+ ///
76+ /// # make the dot bounce of the edges of the screen
77+ /// if x <= 0 or x >= 127: dx = -dx
78+ /// if y <= 0 or y >= 31: dy = -dy
79+ ///
80+ /// lcd.fill(0) # clear the buffer
81+ /// lcd.pixel(x, y, 1) # draw the dot
82+ /// lcd.show() # show the buffer
83+ /// pyb.delay(50) # pause for 50ms
84+
4985#define LCD_INSTR (0)
5086#define LCD_DATA (1)
5187
@@ -158,6 +194,10 @@ STATIC void lcd_write_strn(pyb_lcd_obj_t *lcd, const char *str, unsigned int len
158194 }
159195}
160196
197+ /// \classmethod \constructor(skin_position)
198+ ///
199+ /// Construct an LCD object in the given skin position. `skin_position` can be 'X' or 'Y', and
200+ /// should match the position where the LCD pyskin is plugged in.
161201STATIC mp_obj_t pyb_lcd_make_new (mp_obj_t type_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
162202 // check arguments
163203 mp_arg_check_num (n_args , n_kw , 1 , 1 , false);
@@ -288,6 +328,11 @@ STATIC mp_obj_t pyb_lcd_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
288328 return lcd ;
289329}
290330
331+ /// \method command(instr_data, buf)
332+ ///
333+ /// Send an arbitrary command to the LCD. Pass 0 for `instr_data` to send an
334+ /// instruction, otherwise pass 1 to send data. `buf` is a buffer with the
335+ /// instructions/data to send.
291336STATIC mp_obj_t pyb_lcd_command (mp_obj_t self_in , mp_obj_t instr_data_in , mp_obj_t val ) {
292337 pyb_lcd_obj_t * self = self_in ;
293338
@@ -308,6 +353,9 @@ STATIC mp_obj_t pyb_lcd_command(mp_obj_t self_in, mp_obj_t instr_data_in, mp_obj
308353}
309354STATIC MP_DEFINE_CONST_FUN_OBJ_3 (pyb_lcd_command_obj , pyb_lcd_command );
310355
356+ /// \method contrast(value)
357+ ///
358+ /// Set the contrast of the LCD. Valid values are between 0 and 47.
311359STATIC mp_obj_t pyb_lcd_contrast (mp_obj_t self_in , mp_obj_t contrast_in ) {
312360 pyb_lcd_obj_t * self = self_in ;
313361 int contrast = mp_obj_get_int (contrast_in );
@@ -322,6 +370,9 @@ STATIC mp_obj_t pyb_lcd_contrast(mp_obj_t self_in, mp_obj_t contrast_in) {
322370}
323371STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_lcd_contrast_obj , pyb_lcd_contrast );
324372
373+ /// \method light(value)
374+ ///
375+ /// Turn the backlight on/off. True or 1 turns it on, False or 0 turns it off.
325376STATIC mp_obj_t pyb_lcd_light (mp_obj_t self_in , mp_obj_t value ) {
326377 pyb_lcd_obj_t * self = self_in ;
327378 if (mp_obj_is_true (value )) {
@@ -333,6 +384,9 @@ STATIC mp_obj_t pyb_lcd_light(mp_obj_t self_in, mp_obj_t value) {
333384}
334385STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_lcd_light_obj , pyb_lcd_light );
335386
387+ /// \method write(str)
388+ ///
389+ /// Write the string `str` to the screen. It will appear immediately.
336390STATIC mp_obj_t pyb_lcd_write (mp_obj_t self_in , mp_obj_t str ) {
337391 pyb_lcd_obj_t * self = self_in ;
338392 uint len ;
@@ -342,6 +396,11 @@ STATIC mp_obj_t pyb_lcd_write(mp_obj_t self_in, mp_obj_t str) {
342396}
343397STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_lcd_write_obj , pyb_lcd_write );
344398
399+ /// \method fill(colour)
400+ ///
401+ /// Fill the screen with the given colour (0 or 1 for white or black).
402+ ///
403+ /// This method writes to the hidden buffer. Use `show()` to show the buffer.
345404STATIC mp_obj_t pyb_lcd_fill (mp_obj_t self_in , mp_obj_t col_in ) {
346405 pyb_lcd_obj_t * self = self_in ;
347406 int col = mp_obj_get_int (col_in );
@@ -354,6 +413,11 @@ STATIC mp_obj_t pyb_lcd_fill(mp_obj_t self_in, mp_obj_t col_in) {
354413}
355414STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_lcd_fill_obj , pyb_lcd_fill );
356415
416+ /// \method get(x, y)
417+ ///
418+ /// Get the pixel at the position `(x, y)`. Returns 0 or 1.
419+ ///
420+ /// This method reads from the visible buffer.
357421STATIC mp_obj_t pyb_lcd_get (mp_obj_t self_in , mp_obj_t x_in , mp_obj_t y_in ) {
358422 pyb_lcd_obj_t * self = self_in ;
359423 int x = mp_obj_get_int (x_in );
@@ -368,6 +432,11 @@ STATIC mp_obj_t pyb_lcd_get(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
368432}
369433STATIC MP_DEFINE_CONST_FUN_OBJ_3 (pyb_lcd_get_obj , pyb_lcd_get );
370434
435+ /// \method pixel(x, y, colour)
436+ ///
437+ /// Set the pixel at `(x, y)` to the given colour (0 or 1).
438+ ///
439+ /// This method writes to the hidden buffer. Use `show()` to show the buffer.
371440STATIC mp_obj_t pyb_lcd_pixel (uint n_args , const mp_obj_t * args ) {
372441 pyb_lcd_obj_t * self = args [0 ];
373442 int x = mp_obj_get_int (args [1 ]);
@@ -384,6 +453,11 @@ STATIC mp_obj_t pyb_lcd_pixel(uint n_args, const mp_obj_t *args) {
384453}
385454STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_lcd_pixel_obj , 4 , 4 , pyb_lcd_pixel );
386455
456+ /// \method text(str, x, y, colour)
457+ ///
458+ /// Draw the given text to the position `(x, y)` using the given colour (0 or 1).
459+ ///
460+ /// This method writes to the hidden buffer. Use `show()` to show the buffer.
387461STATIC mp_obj_t pyb_lcd_text (uint n_args , const mp_obj_t * args ) {
388462 // extract arguments
389463 pyb_lcd_obj_t * self = args [0 ];
@@ -428,6 +502,9 @@ STATIC mp_obj_t pyb_lcd_text(uint n_args, const mp_obj_t *args) {
428502}
429503STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_lcd_text_obj , 5 , 5 , pyb_lcd_text );
430504
505+ /// \method show()
506+ ///
507+ /// Show the hidden buffer on the screen.
431508STATIC mp_obj_t pyb_lcd_show (mp_obj_t self_in ) {
432509 pyb_lcd_obj_t * self = self_in ;
433510 memcpy (self -> pix_buf , self -> pix_buf2 , LCD_PIX_BUF_BYTE_SIZE );
0 commit comments