Quellcode durchsuchen

【完善】对坐标的输入进行参数检查

SummerGift vor 6 Jahren
Ursprung
Commit
a734f1604a
1 geänderte Dateien mit 16 neuen und 4 gelöschten Zeilen
  1. 16 4
      port/machine_lcd.c

+ 16 - 4
port/machine_lcd.c

@@ -97,6 +97,8 @@ STATIC mp_obj_t machine_lcd_pixel(size_t n_args, const mp_obj_t *args) {
     int x = mp_obj_get_int(args[1]);
     int y = mp_obj_get_int(args[2]);
     
+    error_check((x >= 0 && x <= 240) && (y >= 0 && y <= 240) , "The min/max X/Y coordinates is 0/240");
+
     int col = mp_obj_get_int(args[3]);
     lcd_draw_point_color(x, y, col);
 
@@ -113,13 +115,15 @@ STATIC mp_obj_t machine_lcd_text(size_t n_args, const mp_obj_t *args) {
     machine_lcd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
     size_t len;
     const char *data = mp_obj_str_get_data(args[1], &len);
-    int x0 = mp_obj_get_int(args[2]);
-    int y0 = mp_obj_get_int(args[3]);
+    int x = mp_obj_get_int(args[2]);
+    int y = mp_obj_get_int(args[3]);
     int size = mp_obj_get_int(args[4]);
     
+    error_check((x >= 0 && x <= 240) && (y >= 0 && y <= 240) , "The min/max X/Y coordinates is 0/240");
+    
     error_check(size == 16 || size == 24 || size == 32, "lcd only support font size 16 24 32");
     
-    lcd_show_string(x0, y0, size, data);
+    lcd_show_string(x, y, size, data);
 
     return mp_const_none;
 }
@@ -137,6 +141,9 @@ STATIC mp_obj_t machine_lcd_line(size_t n_args, const mp_obj_t *args) {
     int y1 = mp_obj_get_int(args[2]);
     int x2 = mp_obj_get_int(args[3]);
     int y2 = mp_obj_get_int(args[4]);
+    
+    error_check((x1 >= 0 && x1 <= 240) && (y1 >= 0 && y1 <= 240) , "The min/max X/Y coordinates is 0/240");
+    error_check((x2 >= 0 && x2 <= 240) && (y2 >= 0 && y2 <= 240) , "The min/max X/Y coordinates is 0/240");
 
     lcd_draw_line(x1, y1, x2, y2);
     return mp_const_none;
@@ -156,6 +163,9 @@ STATIC mp_obj_t machine_lcd_rectangle(size_t n_args, const mp_obj_t *args) {
     int x2 = mp_obj_get_int(args[3]);
     int y2 = mp_obj_get_int(args[4]);
 
+    error_check((x1 >= 0 && x1 <= 240) && (y1 >= 0 && y1 <= 240) , "The min/max X/Y coordinates is 0/240");
+    error_check((x2 >= 0 && x2 <= 240) && (y2 >= 0 && y2 <= 240) , "The min/max X/Y coordinates is 0/240");
+
     lcd_draw_rectangle(x1, y1, x2, y2);
     return mp_const_none;
 }
@@ -171,7 +181,9 @@ STATIC mp_obj_t machine_lcd_circle(size_t n_args, const mp_obj_t *args) {
 
     int x1 = mp_obj_get_int(args[1]);
     int y1 = mp_obj_get_int(args[2]);
-    int r = mp_obj_get_int(args[3]);
+    int r  = mp_obj_get_int(args[3]);
+
+    error_check((x1 >= 0 && x1 <= 240) && (y1 >= 0 && y1 <= 240) , "The min/max X/Y coordinates is 0/240");
 
     lcd_draw_circle(x1, y1, r);
     return mp_const_none;