|
| 1 | +importos |
| 2 | + |
| 3 | +try: |
| 4 | +importtomlibastoml |
| 5 | +exceptImportError: |
| 6 | +try: |
| 7 | +importtoml |
| 8 | +exceptImportError: |
| 9 | +raiseRuntimeError( |
| 10 | +'A "toml" library is needed to read a .toml file\n' |
| 11 | +'One is included with Python 3.11+ or you can install\n' |
| 12 | +'into your current version of Python using the following command:\n' |
| 13 | +'\n' |
| 14 | +'pip3 install toml' |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +classTOMLMeta(type): |
| 19 | + |
| 20 | +def__call__(cls,name,parent=None,**kwargs): |
| 21 | +children= {} |
| 22 | +forkey,valueinlist(kwargs.items()): |
| 23 | +ifisinstance(value,dict): |
| 24 | +children[key]=value |
| 25 | +delkwargs[key] |
| 26 | + |
| 27 | +instance=super().__call__(name,parent=parent,**kwargs) |
| 28 | + |
| 29 | +forchild_name,child_datainchildren.items(): |
| 30 | +child=TOMLObject(child_name,parent=instance,**child_data) |
| 31 | +instance.add_child(child) |
| 32 | + |
| 33 | +returninstance |
| 34 | + |
| 35 | + |
| 36 | +classTOMLObject(metaclass=TOMLMeta): |
| 37 | + |
| 38 | +def__init__(self,name,parent=None,**kwargs): |
| 39 | + |
| 40 | +ifparentisnotNoneandparent.name=='MCU': |
| 41 | +self.build_args=kwargs |
| 42 | + |
| 43 | +self.name=name |
| 44 | +self.parent=parent |
| 45 | +self.__kwargs=kwargs |
| 46 | +self.__children= [] |
| 47 | +self.imports= [] |
| 48 | +self.mcu=None |
| 49 | + |
| 50 | +defadd_child(self,child): |
| 51 | +ifself.name=='MCU': |
| 52 | +self.__dict__.update(child.__dict__) |
| 53 | +self.board=child.name |
| 54 | +self.name='MCU' |
| 55 | +elifself.parentisNoneandchild.name=='MCU': |
| 56 | +self.mcu=TOMLmcu(child) |
| 57 | +else: |
| 58 | +self.__children.append(child) |
| 59 | + |
| 60 | +def__getattr__(self,item): |
| 61 | +ifiteminself.__dict__: |
| 62 | +returnself.__dict__[item] |
| 63 | + |
| 64 | +ifiteminself.__kwargs: |
| 65 | +returnself.__kwargs[item] |
| 66 | + |
| 67 | +raiseAttributeError(item) |
| 68 | + |
| 69 | +@property |
| 70 | +deffqn(self): |
| 71 | +ifself.__kwargs: |
| 72 | +if'params'inself.__kwargsor'value'inself.__kwargs: |
| 73 | +returnself.parent.fqn+'.'+self.name |
| 74 | + |
| 75 | +returnself.name+' = '+self.parent.fqn |
| 76 | + |
| 77 | +ifself.name=='I2C': |
| 78 | +return'i2c.I2C' |
| 79 | + |
| 80 | +ifself.name=='SPI': |
| 81 | +return'machine.SPI' |
| 82 | + |
| 83 | +ifself.name=='SDCard': |
| 84 | +return'machine.SDCard' |
| 85 | + |
| 86 | +ifself.name.lower()indisplay_drivers: |
| 87 | +returnself.name.lower()+'.'+self.name |
| 88 | + |
| 89 | +ifself.name.lower()inindev_drivers: |
| 90 | +returnself.name.lower()+'.'+self.name |
| 91 | + |
| 92 | +ifself.name.lower()inio_expanders: |
| 93 | +returnself.name.lower()+'.'+self.name |
| 94 | + |
| 95 | +ifself.namein ('I80Bus','SPIBus','I2CBus','RGBBus'): |
| 96 | +return'lcd_bus.'+self.name |
| 97 | + |
| 98 | +ifself.parentisNone: |
| 99 | +returnNone |
| 100 | + |
| 101 | +ifself.parent.name: |
| 102 | +returnself.parent.fqn+'.'+self.name |
| 103 | + |
| 104 | +returnself.name |
| 105 | + |
| 106 | +@property |
| 107 | +defvar_names(self): |
| 108 | +ifself.__kwargs: |
| 109 | +fqn=self.fqn |
| 110 | + |
| 111 | +if'='infqn: |
| 112 | +return [fqn.split('=')[0].strip()] |
| 113 | + |
| 114 | +return [] |
| 115 | + |
| 116 | +var_names= [] |
| 117 | + |
| 118 | +forchildinself.__children: |
| 119 | +var_names.extend(child.var_names) |
| 120 | + |
| 121 | +returnvar_names |
| 122 | + |
| 123 | +@property |
| 124 | +defconstants(self): |
| 125 | +res= [] |
| 126 | + |
| 127 | +ifnotself.__children: |
| 128 | +forkey,valueinlist(self.__kwargs.items()): |
| 129 | +ifnotisinstance(value,int)orkey=='value': |
| 130 | +continue |
| 131 | + |
| 132 | +name=self.name.upper() |
| 133 | + |
| 134 | +key_upper=key.upper() |
| 135 | +ifnamenotinkey_upper: |
| 136 | +key_upper=name+'_'+key_upper |
| 137 | + |
| 138 | +res.append(f'_{key_upper} = const({value})') |
| 139 | +self.__kwargs[key]=f'_{key_upper}' |
| 140 | +else: |
| 141 | +forchildinself.__children: |
| 142 | +res.extend(child.constants) |
| 143 | + |
| 144 | +returnres |
| 145 | + |
| 146 | +def__str__(self): |
| 147 | +ifself.parentisNone: |
| 148 | +var_names=self.var_names |
| 149 | + |
| 150 | +output= [] |
| 151 | +output.extend(self.constants) |
| 152 | + |
| 153 | +forchildinself.__children: |
| 154 | +ifchild.namenotinvar_names: |
| 155 | +module=child.fqn.split('.')[0] |
| 156 | +ifmodulenotinself.imports: |
| 157 | +self.imports.append(module) |
| 158 | +output.extend(['',f'import{module}','']) |
| 159 | + |
| 160 | +output.append(str(child)) |
| 161 | + |
| 162 | +ifoutput: |
| 163 | +output= [ |
| 164 | +'from micropython import const', |
| 165 | +'import lvgl as lv', |
| 166 | +'', |
| 167 | +'' |
| 168 | + ]+output |
| 169 | + |
| 170 | +return'\n'.join(output) |
| 171 | + |
| 172 | +ifself.__childrenandnotself.__kwargs: |
| 173 | +output= [str(child)forchildinself.__children] |
| 174 | +return'\n'.join(output) |
| 175 | + |
| 176 | +fqn=self.fqn |
| 177 | + |
| 178 | +iflen(self.__kwargs)==1: |
| 179 | +key=list(self.__kwargs.keys())[0] |
| 180 | +ifkey=='params': |
| 181 | +params=', '.join(str(itm)foritminself.__kwargs[key]) |
| 182 | +returnf'{fqn}({params})' |
| 183 | +elifkey=='value': |
| 184 | +returnf'{fqn} = '+str(self.__kwargs[key]) |
| 185 | +else: |
| 186 | +returnf'{fqn}({key}={str(self.__kwargs[key])})' |
| 187 | +else: |
| 188 | +params=',\n'.join(f'{k}={str(v)}'fork,vinself.__kwargs.items()ifnotisinstance(v,dict)) |
| 189 | +ifparams: |
| 190 | +output= [f'{fqn}(\n{params}\n)',''] |
| 191 | +else: |
| 192 | +raiseRuntimeError |
| 193 | + |
| 194 | +forchildinself.__children: |
| 195 | +output.append(self.name+'.'+str(child).split('.',2)[-1]) |
| 196 | + |
| 197 | +iflen(output)>2: |
| 198 | +output.append('') |
| 199 | + |
| 200 | +return'\n'.join(output) |
| 201 | + |
| 202 | + |
| 203 | +classTOMLmcu: |
| 204 | + |
| 205 | +def__init__(self,mcu): |
| 206 | +name=mcu.board |
| 207 | +build_args=mcu.build_args |
| 208 | + |
| 209 | +command= [name] |
| 210 | +forarg,valueinbuild_args.items(): |
| 211 | +ifarg.islower(): |
| 212 | +build_arg='--'+arg.replace('_','-') |
| 213 | +ifisinstance(value,bool)andnotvalue: |
| 214 | +raiseSyntaxError( |
| 215 | +'optionless build commands must be set to "true"\n' |
| 216 | +f'if they are used in the toml file. ({arg} ={repr(value)})' |
| 217 | + ) |
| 218 | +else: |
| 219 | +build_arg=arg |
| 220 | + |
| 221 | +ifnotisinstance(value,bool): |
| 222 | +build_arg=f'{build_arg}={value}' |
| 223 | + |
| 224 | +command.append(build_arg) |
| 225 | + |
| 226 | +self.build_command=command |
| 227 | + |
| 228 | + |
| 229 | +base_path=os.path.abspath(os.path.join(os.path.dirname(__file__),'..')) |
| 230 | + |
| 231 | +display_driver_path=os.path.join(base_path,'api_drivers/common_api_drivers/display') |
| 232 | +indev_driver_path=os.path.join(base_path,'api_drivers/common_api_drivers/indev') |
| 233 | +io_expander_path=os.path.join(base_path,'api_drivers/common_api_drivers/io_expander') |
| 234 | + |
| 235 | + |
| 236 | +display_drivers= [fileforfileinos.listdir(display_driver_path)ifnotfile.endswith('.wip')andnotfile.endswith('.py')] |
| 237 | +indev_drivers= [file[:-3]forfileinos.listdir(indev_driver_path)iffile.endswith('.py')] |
| 238 | +io_expanders= [file[:-3]forfileinos.listdir(io_expander_path)iffile.endswith('.py')] |
| 239 | + |
| 240 | + |
| 241 | +defrun(toml_path,output_file): |
| 242 | + |
| 243 | +ifnotos.path.exists(toml_path): |
| 244 | +raiseRuntimeError(f'inable to locate .toml ({toml_path})') |
| 245 | + |
| 246 | +try: |
| 247 | +withopen(toml_path,'r')asf: |
| 248 | +toml_data=toml.load(f) |
| 249 | + |
| 250 | +toml_obj=TOMLObject('',**toml_data) |
| 251 | + |
| 252 | +t_data=str(toml_obj) |
| 253 | + |
| 254 | +ift_data: |
| 255 | +withopen(output_file,'w')asf: |
| 256 | +f.write(t_data) |
| 257 | + |
| 258 | +displays= [f'DISPLAY={item}'foritemintoml_obj.importsifitemindisplay_drivers] |
| 259 | +indevs= [f'INDEV={item}'foritemintoml_obj.importsifiteminindev_drivers] |
| 260 | +expanders= [f'EXPANDER={item}'foritemintoml_obj.importsifiteminio_expanders] |
| 261 | + |
| 262 | +iftoml_obj.mcuisNone: |
| 263 | +build_command= [] |
| 264 | +else: |
| 265 | +build_command=toml_obj.build_command |
| 266 | + |
| 267 | +fordisplayindisplays[:]: |
| 268 | +ifdisplaynotinbuild_command: |
| 269 | +build_command.append(display) |
| 270 | + |
| 271 | +forindevinindevs[:]: |
| 272 | +ifindevnotinbuild_command: |
| 273 | +build_command.append(indev) |
| 274 | + |
| 275 | +forexpanderinexpanders[:]: |
| 276 | +ifexpandernotinbuild_command: |
| 277 | +build_command.append(expander) |
| 278 | + |
| 279 | +returnbuild_command |
| 280 | + |
| 281 | +exceptOSErroraserr: |
| 282 | +raiseRuntimeError(f'Unable to write data to{output_file}')fromerr |
| 283 | +exceptExceptionaserr:# NOQA |
| 284 | +raiseSyntaxError(f'Unable to parse .toml file ({toml_path})')fromerr |
| 285 | + |
| 286 | + |