|
| 1 | +#!/bin/python |
| 2 | +import json |
| 3 | +import os.path |
| 4 | + |
| 5 | +import unitGenerator |
| 6 | +import unitHeaders |
| 7 | + |
| 8 | +script_dir = os.path.realpath(os.path.dirname(__file__)) |
| 9 | +arduino_src_dir = os.path.join(script_dir, 'unit-system-adruino') |
| 10 | +arduino_src_dir = os.path.join(arduino_src_dir, 'src') |
| 11 | + |
| 12 | +arduino_export_macro = '' |
| 13 | + |
| 14 | +units = [] |
| 15 | +# base units |
| 16 | +units += [unitGenerator.Unit('time_si', 'seconds', 1, [ |
| 17 | + unitGenerator.UnitLiteral(json.loads('{"name":"a", "multiplier":31536000000}')), |
| 18 | + unitGenerator.UnitLiteral(json.loads('{"name":"d", "multiplier":86400000}')), |
| 19 | + unitGenerator.UnitLiteral(json.loads('{"name":"h", "multiplier":3600}')), |
| 20 | + unitGenerator.UnitLiteral(json.loads('{"name":"minute", "multiplier":60}')), |
| 21 | + unitGenerator.UnitLiteral(json.loads('{"name":"s", "multiplier":1e0}')), |
| 22 | + unitGenerator.UnitLiteral(json.loads('{"name":"ms", "multiplier":1e-3}')), |
| 23 | + unitGenerator.UnitLiteral(json.loads('{"name":"ns", "multiplier":1e-6}')), |
| 24 | + unitGenerator.UnitLiteral(json.loads('{"name":"us", "multiplier":1e-9}')), |
| 25 | + unitGenerator.UnitLiteral(json.loads('{"name":"fs", "multiplier":1e-12}')), |
| 26 | +], arduino_export_macro, arduino_src_dir)] |
| 27 | +units += [unitGenerator.Unit('length', 'meter', 2, [ |
| 28 | + unitGenerator.UnitLiteral(json.loads('{"name":"km", "multiplier":1e3}')), |
| 29 | + unitGenerator.UnitLiteral(json.loads('{"name":"m", "multiplier":1e0}')), |
| 30 | + unitGenerator.UnitLiteral(json.loads('{"name":"dm", "multiplier":1e-1}')), |
| 31 | + unitGenerator.UnitLiteral(json.loads('{"name":"cm", "multiplier":1e-2}')), |
| 32 | + unitGenerator.UnitLiteral(json.loads('{"name":"mm", "multiplier":1e-3}')), |
| 33 | + unitGenerator.UnitLiteral(json.loads('{"name":"um", "multiplier":1e-6}')), |
| 34 | + unitGenerator.UnitLiteral(json.loads('{"name":"nm", "multiplier":1e-9}')), |
| 35 | +], arduino_export_macro, arduino_src_dir)] |
| 36 | +units += [unitGenerator.Unit('mass', 'kilogram', 3, [ |
| 37 | + unitGenerator.UnitLiteral(json.loads('{"name":"t", "multiplier":1e3}')), |
| 38 | + unitGenerator.UnitLiteral(json.loads('{"name":"kg", "multiplier":1e0}')), |
| 39 | + unitGenerator.UnitLiteral(json.loads('{"name":"g", "multiplier":1e-3}')), |
| 40 | + unitGenerator.UnitLiteral(json.loads('{"name":"mg", "multiplier":1e-6}')), |
| 41 | + unitGenerator.UnitLiteral(json.loads('{"name":"ug", "multiplier":1e-9}')), |
| 42 | +], arduino_export_macro, arduino_src_dir)] |
| 43 | +units += [unitGenerator.Unit('temperature', 'Kelvin', 4, [ |
| 44 | + unitGenerator.UnitLiteral(json.loads('{"name":"K", "multiplier":1e0}')), |
| 45 | + unitGenerator.UnitLiteral(json.loads('{"name":"C", "multiplier":1e0, "offset":273.15}')), |
| 46 | +], arduino_export_macro, arduino_src_dir)] |
| 47 | +units += [unitGenerator.Unit('amount', 'things', 5, [ |
| 48 | + unitGenerator.UnitLiteral(json.loads('{"name":"mol", "multiplier":6.02214076e23}')), |
| 49 | + unitGenerator.UnitLiteral(json.loads('{"name":"things", "multiplier":1e0}')), |
| 50 | +], arduino_export_macro, arduino_src_dir)] |
| 51 | +units += [unitGenerator.Unit('electric_current', 'Ampere', 6, [ |
| 52 | + unitGenerator.UnitLiteral(json.loads('{"name":"MA", "multiplier":1e6}')), |
| 53 | + unitGenerator.UnitLiteral(json.loads('{"name":"kA", "multiplier":1e3}')), |
| 54 | + unitGenerator.UnitLiteral(json.loads('{"name":"A", "multiplier":1e-0}')), |
| 55 | + unitGenerator.UnitLiteral(json.loads('{"name":"mA", "multiplier":1e-3}')), |
| 56 | + unitGenerator.UnitLiteral(json.loads('{"name":"uA", "multiplier":1e-6}')), |
| 57 | + unitGenerator.UnitLiteral(json.loads('{"name":"nA", "multiplier":1e-9}')), |
| 58 | +], arduino_export_macro, arduino_src_dir)] |
| 59 | +units += [unitGenerator.Unit('luminous_intensity', 'candela', 7, [ |
| 60 | + unitGenerator.UnitLiteral(json.loads('{"name":"cd", "multiplier":1e-0}')), |
| 61 | +], arduino_export_macro, arduino_src_dir)] |
| 62 | + |
| 63 | + |
| 64 | +# common units |
| 65 | +units += [unitGenerator.Unit('energy', 'Joules', 8, [ |
| 66 | + unitGenerator.UnitLiteral(json.loads('{"name":"MJ", "multiplier":1e6}')), |
| 67 | + unitGenerator.UnitLiteral(json.loads('{"name":"kJ", "multiplier":1e3}')), |
| 68 | + unitGenerator.UnitLiteral(json.loads('{"name":"J", "multiplier":1e0}')), |
| 69 | + unitGenerator.UnitLiteral(json.loads('{"name":"Nm", "multiplier":1e0}')), |
| 70 | + unitGenerator.UnitLiteral(json.loads('{"name":"kWh", "multiplier":3600000}')), |
| 71 | + unitGenerator.UnitLiteral(json.loads('{"name":"Wh", "multiplier":3600}')), |
| 72 | + unitGenerator.UnitLiteral(json.loads('{"name":"GeV", "multiplier":1.602176565e-10}')), |
| 73 | + unitGenerator.UnitLiteral(json.loads('{"name":"MeV", "multiplier":1.602176565e-13}')), |
| 74 | + unitGenerator.UnitLiteral(json.loads('{"name":"keV", "multiplier":1.602176565e-16}')), |
| 75 | + unitGenerator.UnitLiteral(json.loads('{"name":"eV", "multiplier":1.602176565e-19}')), |
| 76 | +], arduino_export_macro, arduino_src_dir)] |
| 77 | +units += [unitGenerator.Unit('power', 'Watt', 801, [ |
| 78 | + unitGenerator.UnitLiteral(json.loads('{"name":"GW", "multiplier":1e9}')), |
| 79 | + unitGenerator.UnitLiteral(json.loads('{"name":"MW", "multiplier":1e6}')), |
| 80 | + unitGenerator.UnitLiteral(json.loads('{"name":"kW", "multiplier":1e3}')), |
| 81 | + unitGenerator.UnitLiteral(json.loads('{"name":"W", "multiplier":1e0}')), |
| 82 | + unitGenerator.UnitLiteral(json.loads('{"name":"mW", "multiplier":1e-3}')), |
| 83 | +], arduino_export_macro, arduino_src_dir)] |
| 84 | +units += [unitGenerator.Unit('speed', 'meter per second', 201, [ |
| 85 | + unitGenerator.UnitLiteral(json.loads('{"name":"kmph", "multiplier":0.27777777777777777777777777777}')), |
| 86 | + unitGenerator.UnitLiteral(json.loads('{"name":"mps", "multiplier":1e0}')), |
| 87 | +], arduino_export_macro, arduino_src_dir)] |
| 88 | +units += [unitGenerator.Unit('acceleration', 'meter per second^2', 2011, [ |
| 89 | + unitGenerator.UnitLiteral(json.loads('{"name":"G", "multiplier":9.81}')), |
| 90 | + unitGenerator.UnitLiteral(json.loads('{"name":"mps2", "multiplier":1e0}')), |
| 91 | +], arduino_export_macro, arduino_src_dir)] |
| 92 | +units += [unitGenerator.Unit('area', 'meters^2', 22, [ |
| 93 | + unitGenerator.UnitLiteral(json.loads('{"name":"km2", "multiplier":1e6}')), |
| 94 | + unitGenerator.UnitLiteral(json.loads('{"name":"m2", "multiplier":1e0}')), |
| 95 | + unitGenerator.UnitLiteral(json.loads('{"name":"mm2", "multiplier":1e-6}')), |
| 96 | +], arduino_export_macro, arduino_src_dir)] |
| 97 | +units += [unitGenerator.Unit('force', 'Newton', 32011, [ |
| 98 | + unitGenerator.UnitLiteral(json.loads('{"name":"N", "multiplier":1.0}')), |
| 99 | +], arduino_export_macro, arduino_src_dir)] |
| 100 | +units += [unitGenerator.Unit('momentum', 'kilogram * meter per second', 3201, [ |
| 101 | + unitGenerator.UnitLiteral(json.loads('{"name":"kgmps", "multiplier":1.0}')), |
| 102 | +], arduino_export_macro, arduino_src_dir)] |
| 103 | + |
| 104 | +unit_strings = [] |
| 105 | + |
| 106 | +for unit in units: |
| 107 | + unitGenerator.generate_sources(unit) |
| 108 | + unit_strings += [unit.name] |
| 109 | + |
| 110 | +combinations = [] |
| 111 | +combinations += [['speed', 'time_si', 'length']] |
| 112 | +combinations += [['acceleration', 'time_si', 'speed']] |
| 113 | +combinations += [['length', 'length', 'area']] |
| 114 | +combinations += [['force', 'length', 'energy']] |
| 115 | +combinations += [['power', 'time_si', 'energy']] |
| 116 | +combinations += [['momentum', 'speed', 'energy']] |
| 117 | +combinations += [['force', 'time_si', 'momentum']] |
| 118 | +combinations += [['mass', 'acceleration', 'force']] |
| 119 | +combinations += [['mass', 'speed', 'momentum']] |
| 120 | +combinations += [['force', 'speed', 'power']] |
| 121 | + |
| 122 | +constants = [] |
| 123 | +constants += [{'name': 'avogadro_constant', 'value': 6.02214076e23}] |
| 124 | +constants += [{'name': 'waterFreezingPoint', 'value': 273.15}] |
| 125 | + |
| 126 | +fillDict = { |
| 127 | + 'export_macro': arduino_export_macro, |
| 128 | + 'units': unit_strings, |
| 129 | + 'disable_std': True, |
| 130 | + 'combinations': combinations, |
| 131 | + 'constants': constants, |
| 132 | +} |
| 133 | + |
| 134 | +unitHeaders.create_headers( |
| 135 | + fillDict, |
| 136 | + True, |
| 137 | + True, |
| 138 | + True, |
| 139 | + True, |
| 140 | + arduino_src_dir, |
| 141 | +) |
0 commit comments