machine_lcd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 SummerGift <SummerGift@qq.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/mphal.h"
  29. #include "py/runtime.h"
  30. #if MICROPY_PY_MACHINE_LCD
  31. #include "machine_lcd.h"
  32. #include <drv_lcd.h>
  33. #define MAX_CO (240 - 1)
  34. typedef struct _machine_lcd_obj_t {
  35. mp_obj_base_t base;
  36. } machine_lcd_obj_t;
  37. STATIC void error_check(bool status, const char *msg) {
  38. if (!status) {
  39. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  40. }
  41. }
  42. /// \classmethod \constructor(skin_position)
  43. ///
  44. /// Construct an LCD object.
  45. STATIC mp_obj_t machine_lcd_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  46. // check arguments
  47. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  48. // create lcd object
  49. machine_lcd_obj_t *lcd = m_new_obj(machine_lcd_obj_t);
  50. lcd->base.type = &machine_lcd_type;
  51. return MP_OBJ_FROM_PTR(lcd);
  52. }
  53. /// \method light(value)
  54. ///
  55. /// Turn the backlight on/off. True or 1 turns it on, False or 0 turns it off.
  56. STATIC mp_obj_t machine_lcd_light(mp_obj_t self_in, mp_obj_t value) {
  57. if (mp_obj_is_true(value)) {
  58. lcd_display_on(); // set pin high to turn backlight on
  59. } else {
  60. lcd_display_off();// set pin low to turn backlight off
  61. }
  62. return mp_const_none;
  63. }
  64. STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_lcd_light_obj, machine_lcd_light);
  65. /// \method fill(colour)
  66. ///
  67. /// Fill the screen with the given colour.
  68. ///
  69. STATIC mp_obj_t machine_lcd_fill(mp_obj_t self_in, mp_obj_t col_in) {
  70. int col = mp_obj_get_int(col_in);
  71. lcd_clear(col);
  72. return mp_const_none;
  73. }
  74. STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_lcd_fill_obj, machine_lcd_fill);
  75. /// \method pixel(x, y, colour)
  76. ///
  77. /// Set the pixel at `(x, y)` to the given colour.
  78. ///
  79. STATIC mp_obj_t machine_lcd_pixel(size_t n_args, const mp_obj_t *args) {
  80. int x = mp_obj_get_int(args[1]);
  81. int y = mp_obj_get_int(args[2]);
  82. error_check((x >= 0 && x <= MAX_CO) && (y >= 0 && y <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  83. int col = mp_obj_get_int(args[3]);
  84. lcd_draw_point_color(x, y, col);
  85. return mp_const_none;
  86. }
  87. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lcd_pixel_obj, 4, 4, machine_lcd_pixel);
  88. /// \method text(str, x, y, size)
  89. ///
  90. /// Draw the given text to the position `(x, y)` using the given size (16 24 32).
  91. ///
  92. STATIC mp_obj_t machine_lcd_text(size_t n_args, const mp_obj_t *args) {
  93. size_t len;
  94. const char *data = mp_obj_str_get_data(args[1], &len);
  95. int x = mp_obj_get_int(args[2]);
  96. int y = mp_obj_get_int(args[3]);
  97. int size = mp_obj_get_int(args[4]);
  98. error_check((x >= 0 && x <= MAX_CO) && (y >= 0 && y <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  99. error_check(size == 16 || size == 24 || size == 32, "lcd only support font size 16 24 32");
  100. lcd_show_string(x, y, size, data);
  101. return mp_const_none;
  102. }
  103. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lcd_text_obj, 5, 5, machine_lcd_text);
  104. /// \method line(x1, y1, x2, y2)
  105. ///
  106. /// display a line on the lcd, from (x1, y1) to (x2, y2).
  107. ///
  108. STATIC mp_obj_t machine_lcd_line(size_t n_args, const mp_obj_t *args) {
  109. int x1 = mp_obj_get_int(args[1]);
  110. int y1 = mp_obj_get_int(args[2]);
  111. int x2 = mp_obj_get_int(args[3]);
  112. int y2 = mp_obj_get_int(args[4]);
  113. error_check((x1 >= 0 && x1 <= MAX_CO) && (y1 >= 0 && y1 <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  114. error_check((x2 >= 0 && x2 <= MAX_CO) && (y2 >= 0 && y2 <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  115. lcd_draw_line(x1, y1, x2, y2);
  116. return mp_const_none;
  117. }
  118. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lcd_line_obj, 5, 5, machine_lcd_line);
  119. /// \method rectangle(x1, y1, x2, y2)
  120. ///
  121. /// display a rectangle on the lcd, from (x1, y1) to (x2, y2).
  122. ///
  123. STATIC mp_obj_t machine_lcd_rectangle(size_t n_args, const mp_obj_t *args) {
  124. int x1 = mp_obj_get_int(args[1]);
  125. int y1 = mp_obj_get_int(args[2]);
  126. int x2 = mp_obj_get_int(args[3]);
  127. int y2 = mp_obj_get_int(args[4]);
  128. error_check((x1 >= 0 && x1 <= MAX_CO) && (y1 >= 0 && y1 <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  129. error_check((x2 >= 0 && x2 <= MAX_CO) && (y2 >= 0 && y2 <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  130. lcd_draw_rectangle(x1, y1, x2, y2);
  131. return mp_const_none;
  132. }
  133. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lcd_rectangle_obj, 5, 5, machine_lcd_rectangle);
  134. /// \method circle(x1, y1, r)
  135. ///
  136. /// display a circle on the lcd, center(x1, y1) R = r.
  137. ///
  138. STATIC mp_obj_t machine_lcd_circle(size_t n_args, const mp_obj_t *args) {
  139. int x1 = mp_obj_get_int(args[1]);
  140. int y1 = mp_obj_get_int(args[2]);
  141. int r = mp_obj_get_int(args[3]);
  142. error_check((x1 >= 0 && x1 <= MAX_CO) && (y1 >= 0 && y1 <= MAX_CO) , "The min/max X/Y coordinates is 0/239");
  143. lcd_draw_circle(x1, y1, r);
  144. return mp_const_none;
  145. }
  146. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lcd_circle_obj, 4, 4, machine_lcd_circle);
  147. STATIC const mp_rom_map_elem_t machine_lcd_locals_dict_table[] = {
  148. // instance methods
  149. { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_PTR(&machine_lcd_light_obj) },
  150. { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&machine_lcd_fill_obj) },
  151. { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&machine_lcd_pixel_obj) },
  152. { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&machine_lcd_text_obj) },
  153. { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&machine_lcd_line_obj) },
  154. { MP_ROM_QSTR(MP_QSTR_rectangle), MP_ROM_PTR(&machine_lcd_rectangle_obj) },
  155. { MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&machine_lcd_circle_obj) },
  156. // color
  157. { MP_ROM_QSTR(MP_QSTR_WHITE), MP_ROM_INT(WHITE) },
  158. { MP_ROM_QSTR(MP_QSTR_BLACK), MP_ROM_INT(BLACK) },
  159. { MP_ROM_QSTR(MP_QSTR_BLUE), MP_ROM_INT(BLUE) },
  160. { MP_ROM_QSTR(MP_QSTR_BRED), MP_ROM_INT(BRED) },
  161. { MP_ROM_QSTR(MP_QSTR_GRED), MP_ROM_INT(GRED) },
  162. { MP_ROM_QSTR(MP_QSTR_GBLUE), MP_ROM_INT(GBLUE) },
  163. { MP_ROM_QSTR(MP_QSTR_RED), MP_ROM_INT(RED) },
  164. { MP_ROM_QSTR(MP_QSTR_MAGENTA), MP_ROM_INT(MAGENTA) },
  165. { MP_ROM_QSTR(MP_QSTR_GREEN), MP_ROM_INT(GREEN) },
  166. { MP_ROM_QSTR(MP_QSTR_CYAN), MP_ROM_INT(CYAN) },
  167. { MP_ROM_QSTR(MP_QSTR_YELLOW), MP_ROM_INT(YELLOW) },
  168. { MP_ROM_QSTR(MP_QSTR_BROWN), MP_ROM_INT(BROWN) },
  169. { MP_ROM_QSTR(MP_QSTR_BRRED), MP_ROM_INT(BRRED) },
  170. { MP_ROM_QSTR(MP_QSTR_GRAY), MP_ROM_INT(GRAY) },
  171. { MP_ROM_QSTR(MP_QSTR_GRAY175), MP_ROM_INT(GRAY175) },
  172. { MP_ROM_QSTR(MP_QSTR_GRAY151), MP_ROM_INT(GRAY151) },
  173. { MP_ROM_QSTR(MP_QSTR_GRAY187), MP_ROM_INT(GRAY187) },
  174. { MP_ROM_QSTR(MP_QSTR_GRAY240), MP_ROM_INT(GRAY240) },
  175. };
  176. STATIC MP_DEFINE_CONST_DICT(machine_lcd_locals_dict, machine_lcd_locals_dict_table);
  177. const mp_obj_type_t machine_lcd_type = {
  178. { &mp_type_type },
  179. .name = MP_QSTR_LCD,
  180. .make_new = machine_lcd_make_new,
  181. .locals_dict = (mp_obj_dict_t*)&machine_lcd_locals_dict,
  182. };
  183. #endif // MICROPY_PY_MACHINE_LCD