```PD uses odd units for voltages and currents.  Power too, but we don't
work with power (yet).  In the shell, we have to convert to and from
these odd units.  We had been doing this in-place every time, which was
a bit crumby.  Now it's done with macros in pd.h, making prettier and
more maintainable code.``` Clara Hobbs 5 年之前

b5d499bf1f

1. 27
0
src/pd.h
2. 3
6
src/shell.c
3. 8
4
src/storage.c

#### + 27 - 0 src/pd.h查看文件

 ``@@ -216,4 +216,31 @@`` 216 216 `` #define PD_T_TYPEC_SINK_WAIT_CAP MS2ST(465)`` 217 217 `` `` 218 218 `` `` 219 ``+/*`` 220 ``+ * Unit conversions`` 221 ``+ *`` 222 ``+ * V: volt`` 223 ``+ * CV: centivolt`` 224 ``+ * MV: millivolt`` 225 ``+ * PDV: Power Delivery voltage unit (50 mV)`` 226 ``+ * A: ampere`` 227 ``+ * CA: centiampere`` 228 ``+ * MA: milliampere`` 229 ``+ * PDI: Power Delivery current unit (10 mA)`` 230 ``+ */`` 231 ``+#define PD_MV2PDV(mv) ((mv) / 50)`` 232 ``+#define PD_PDV2MV(pdv) ((pdv) * 50)`` 233 ``+#define PD_MA2PDI(ma) ((ma) / 10)`` 234 ``+#define PD_PDI2MA(pdi) ((pdi) * 10)`` 235 ``+`` 236 ``+/* Get portions of a PD voltage in more normal units */`` 237 ``+#define PD_PDV_V(pdv) ((pdv) / 20)`` 238 ``+#define PD_PDV_CV(pdv) (5 * ((pdv) % 20))`` 239 ``+`` 240 ``+/* Get portions of a PD current in more normal units */`` 241 ``+#define PD_PDI_A(pdv) ((pdv) / 100)`` 242 ``+#define PD_PDI_CA(pdv) ((pdv) % 100)`` 243 ``+`` 244 ``+`` 245 ``+`` 219 246 `` #endif /* PDB_PD_H */``

#### + 3 - 6 src/shell.c查看文件

 ``@@ -37,13 +37,12 @@`` 37 37 `` #include `` 38 38 `` #include `` 39 39 `` `` 40 ``-#include `` 41 ``-`` 42 40 `` #include "chprintf.h"`` 43 41 `` `` 44 42 `` #include "usbcfg.h"`` 45 43 `` #include "storage.h"`` 46 44 `` #include "led.h"`` 45 ``+#include "pd.h"`` 47 46 `` `` 48 47 `` `` 49 48 `` /* Buffer for unwritten configuration */`` ``@@ -178,8 +177,7 @@ static void cmd_set_v(BaseSequentialStream *chp, int argc, char *argv[])`` 178 177 `` if (i >= 0 && i <= UINT16_MAX && endptr > argv) {`` 179 178 `` tmpcfg.status = PDB_CONFIG_STATUS_VALID;`` 180 179 `` /* Convert mV to the unit used by USB PD */`` 181 ``- /* XXX this could use a macro */`` 182 ``- tmpcfg.v = i / 50;`` 180 ``+ tmpcfg.v = PD_MV2PDV(i);`` 183 181 `` } else {`` 184 182 `` chprintf(chp, "Invalid voltage\r\n");`` 185 183 `` return;`` ``@@ -198,8 +196,7 @@ static void cmd_set_i(BaseSequentialStream *chp, int argc, char *argv[])`` 198 196 `` if (i >= 0 && i <= UINT16_MAX && endptr > argv) {`` 199 197 `` tmpcfg.status = PDB_CONFIG_STATUS_VALID;`` 200 198 `` /* Convert mA to the unit used by USB PD */`` 201 ``- /* XXX this could use a macro */`` 202 ``- tmpcfg.i = i / 10;`` 199 ``+ tmpcfg.i = PD_MA2PDI(i);`` 203 200 `` } else {`` 204 201 `` chprintf(chp, "Invalid current\r\n");`` 205 202 `` return;``

#### + 8 - 4 src/storage.c查看文件

 ``@@ -20,6 +20,8 @@`` 20 20 `` `` 21 21 `` #include "chprintf.h"`` 22 22 `` `` 23 ``+#include "pd.h"`` 24 ``+`` 23 25 `` `` 24 26 `` struct pdb_config *pdb_config_array = (struct pdb_config *) PDB_CONFIG_BASE;`` 25 27 `` `` ``@@ -59,11 +61,13 @@ void pdb_config_print(BaseSequentialStream *chp, const struct pdb_config *cfg)`` 59 61 `` chprintf(chp, "\r\n");`` 60 62 `` `` 61 63 `` /* Print voltages and current */`` 62 ``- chprintf(chp, "v: %d.%02d V\r\n", cfg->v/20, 5*(cfg->v%20));`` 63 ``- chprintf(chp, "i: %d.%02d A\r\n", cfg->i/100, cfg->i%100);`` 64 ``+ chprintf(chp, "v: %d.%02d V\r\n", PD_PDV_V(cfg->v), PD_PDV_CV(cfg->v));`` 65 ``+ chprintf(chp, "i: %d.%02d A\r\n", PD_PDI_A(cfg->i), PD_PDI_CA(cfg->i));`` 64 66 `` if (cfg->flags & PDB_CONFIG_FLAGS_VAR_BAT) {`` 65 ``- chprintf(chp, "v_min: %d.%02d V\r\n", cfg->v_min/20, 5*(cfg->v_min%20));`` 66 ``- chprintf(chp, "v_max: %d.%02d V\r\n", cfg->v_max/20, 5*(cfg->v_max%20));`` 67 ``+ chprintf(chp, "v_min: %d.%02d V\r\n", PD_PDV_V(cfg->v_min),`` 68 ``+ PD_PDV_CV(cfg->v_min));`` 69 ``+ chprintf(chp, "v_max: %d.%02d V\r\n", PD_PDV_V(cfg->v_max),`` 70 ``+ PD_PDV_CV(cfg->v_max));`` 67 71 `` }`` 68 72 `` }`` 69 73 `` ``