1+ /*
2+ * Copyright (c) wyrdsec (MIT License)
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
5+ * this software and associated documentation files (the "Software"), to deal in
6+ * the Software without restriction, including without limitation the rights to
7+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+ * the Software, and to permit persons to whom the Software is furnished to do so,
9+ * subject to the following conditions:
10+ * - The above copyright notice and this permission notice shall be included in
11+ * all copies or substantial portions of the Software.
12+ * - The Software is provided "as is", without warranty of any kind, express or
13+ * implied, including but not limited to the warranties of merchantability,
14+ * fitness for a particular purpose and noninfringement. In no event shall the
15+ * authors or copyright holders be liable for any claim, damages or other
16+ * liability, whether in an action of contract, tort or otherwise, arising from,
17+ * out of or in connection with the Software or the use or other dealings in the
18+ * Software.
19+ */
20+ #include "py/objarray.h"
21+ #include "py/runtime.h"
22+ #include "py/objproperty.h"
23+
24+
25+ #include "shared-bindings/aurora_epaper/aurora_framebuffer.h"
26+ #include "shared-module/aurora_epaper/aurora_framebuffer.h"
27+ #include "shared-bindings/busio/SPI.h"
28+ #include "shared-bindings/microcontroller/Pin.h"
29+ #include "shared-module/displayio/__init__.h"
30+
31+
32+ static mp_obj_t aurora_epaper_framebuffer_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
33+ enum { ARG_spi_bus , ARG_chip_select , ARG_reset , ARG_busy , ARG_discharge , ARG_width , ARG_height , ARG_power , ARG_free_bus , NUM_ARGS };
34+ static const mp_arg_t allowed_args [] = {
35+ { MP_QSTR_spi_bus , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = MP_OBJ_NULL } },
36+ { MP_QSTR_chip_select , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = MP_OBJ_NULL } },
37+ { MP_QSTR_reset , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = MP_OBJ_NULL } },
38+ { MP_QSTR_busy , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = MP_OBJ_NULL } },
39+ { MP_QSTR_discharge , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = MP_OBJ_NULL } },
40+ { MP_QSTR_width , MP_ARG_INT | MP_ARG_REQUIRED , {.u_int = 0 } },
41+ { MP_QSTR_height , MP_ARG_INT | MP_ARG_REQUIRED , {.u_int = 0 } },
42+ { MP_QSTR_power , MP_ARG_OBJ , {.u_obj = mp_const_none } },
43+ { MP_QSTR_free_bus , MP_ARG_BOOL , {.u_bool = true} },
44+ };
45+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
46+ MP_STATIC_ASSERT (MP_ARRAY_SIZE (allowed_args ) == NUM_ARGS );
47+
48+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
49+
50+ // Pins
51+ const mcu_pin_obj_t * chip_select = validate_obj_is_free_pin (args [ARG_chip_select ].u_obj , MP_QSTR_chip_select );
52+ const mcu_pin_obj_t * reset = validate_obj_is_free_pin (args [ARG_reset ].u_obj , MP_QSTR_reset );
53+ const mcu_pin_obj_t * busy = validate_obj_is_free_pin (args [ARG_busy ].u_obj , MP_QSTR_busy );
54+ const mcu_pin_obj_t * discharge = validate_obj_is_free_pin (args [ARG_discharge ].u_obj , MP_QSTR_discharge );
55+ const mcu_pin_obj_t * power = validate_obj_is_free_pin_or_none (args [ARG_power ].u_obj , MP_QSTR_power );
56+
57+ busio_spi_obj_t * spi = validate_obj_is_spi_bus (args [ARG_spi_bus ].u_obj , MP_QSTR_spi_bus );
58+ aurora_epaper_framebuffer_obj_t * self = & allocate_display_bus_or_raise ()-> aurora_epaper ;
59+ self -> base .type = & aurora_framebuffer_type ;
60+
61+ common_hal_aurora_epaper_framebuffer_construct (self , spi , chip_select , reset , busy , discharge , power , args [ARG_width ].u_int , args [ARG_height ].u_int , args [ARG_free_bus ].u_bool );
62+ return MP_OBJ_FROM_PTR (self );
63+ }
64+
65+ static mp_int_t aurora_epaper_framebuffer_get_buffer (mp_obj_t self_in , mp_buffer_info_t * bufinfo , mp_uint_t flags ) {
66+ aurora_epaper_framebuffer_obj_t * self = self_in ;
67+ if ((flags & MP_BUFFER_WRITE ) && !(self -> bufinfo .typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW )) {
68+ return 1 ;
69+ }
70+ * bufinfo = self -> bufinfo ;
71+ return 0 ;
72+ }
73+
74+
75+ static mp_obj_t aurora_epaper_framebuffer_deinit (mp_obj_t self_in ) {
76+ aurora_epaper_framebuffer_obj_t * self = (aurora_epaper_framebuffer_obj_t * )self_in ;
77+ common_hal_aurora_epaper_framebuffer_deinit (self );
78+
79+ return mp_const_none ;
80+ }
81+ static MP_DEFINE_CONST_FUN_OBJ_1 (aurora_epaper_framebuffer_deinit_obj , aurora_epaper_framebuffer_deinit ) ;
82+
83+
84+ static mp_obj_t aurora_epaper_frambuffer_set_temperature (mp_obj_t self_in , mp_obj_t temperature ) {
85+ aurora_epaper_framebuffer_obj_t * self = (aurora_epaper_framebuffer_obj_t * )self_in ;
86+
87+ common_hal_aurora_epaper_framebuffer_set_temperature (self , mp_obj_get_float (temperature ));
88+
89+ return mp_const_none ;
90+ }
91+ static MP_DEFINE_CONST_FUN_OBJ_2 (aurora_epaper_frambuffer_set_temperature_obj , aurora_epaper_frambuffer_set_temperature ) ;
92+
93+
94+ static mp_obj_t aurora_epaper_framebuffer_get_free_bus (mp_obj_t self_in ) {
95+ aurora_epaper_framebuffer_obj_t * self = (aurora_epaper_framebuffer_obj_t * )self_in ;
96+ return mp_obj_new_bool (self -> free_bus );
97+ }
98+ static MP_DEFINE_CONST_FUN_OBJ_1 (aurora_epaper_framebuffer_get_free_bus_obj , aurora_epaper_framebuffer_get_free_bus ) ;
99+
100+
101+ static mp_obj_t aurora_epaper_framebuffer_set_free_bus (mp_obj_t self_in , mp_obj_t free_bus ) {
102+ aurora_epaper_framebuffer_obj_t * self = (aurora_epaper_framebuffer_obj_t * )self_in ;
103+ common_hal_aurora_epaper_framebuffer_set_free_bus (self , mp_obj_is_true (free_bus ));
104+ return mp_const_none ;
105+ }
106+ static MP_DEFINE_CONST_FUN_OBJ_2 (aurora_epaper_framebuffer_set_free_bus_obj , aurora_epaper_framebuffer_set_free_bus ) ;
107+
108+ MP_PROPERTY_GETSET (aurora_epaper_framebuffer_free_bus_obj ,
109+ (mp_obj_t )& aurora_epaper_framebuffer_get_free_bus_obj ,
110+ (mp_obj_t )& aurora_epaper_framebuffer_set_free_bus_obj );
111+
112+ static const mp_rom_map_elem_t aurora_epaper_framebuffer_locals_dict_table [] = {
113+ { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& aurora_epaper_framebuffer_deinit_obj ) },
114+ { MP_ROM_QSTR (MP_QSTR_set_temperature ), MP_ROM_PTR (& aurora_epaper_frambuffer_set_temperature_obj ) },
115+ { MP_ROM_QSTR (MP_QSTR_free_bus ), MP_ROM_PTR (& aurora_epaper_framebuffer_free_bus_obj ) },
116+ };
117+ static MP_DEFINE_CONST_DICT (aurora_epaper_framebuffer_locals_dict , aurora_epaper_framebuffer_locals_dict_table ) ;
118+
119+ MP_DEFINE_CONST_OBJ_TYPE (
120+ aurora_framebuffer_type ,
121+ MP_QSTR_AuroraEpaperFramebuffer ,
122+ MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS ,
123+ make_new , aurora_epaper_framebuffer_make_new ,
124+ locals_dict , & aurora_epaper_framebuffer_locals_dict ,
125+ buffer , aurora_epaper_framebuffer_get_buffer ,
126+ protocol , & aurora_epaper_framebuffer_proto
127+ );
0 commit comments