Skip to content

Commit 23d21ab

Browse files
committed
Clean up JS code
1 parent b7ba4f6 commit 23d21ab

1 file changed

Lines changed: 10 additions & 19 deletions

File tree

assets/js/dp100.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const deviceAddr = 251 // DP100's device address
55
* Calculate the buffers CRC-16/MODBUS checksum.
66
*
77
* @param {ArrayBuffer} buffer - The buffer to calculate the CRC16 for.
8-
* @return {number} - The CRC16 checksum.
8+
* @returns {Number} - The CRC16 checksum.
99
*/
1010
export function crc16 (buffer) {
1111
let crc = 0xFFFF
@@ -43,18 +43,6 @@ const FUNCTIONS = Object.freeze({
4343
NONE: 0xFF,
4444
})
4545

46-
const WORK_MODES = Object.freeze({
47-
CC: 0,
48-
CV: 1,
49-
OFF: 2,
50-
})
51-
52-
const WORK_MODE_MAP = {
53-
[WORK_MODES.CV]: 'Constant Voltage',
54-
[WORK_MODES.CC]: 'Constant Current',
55-
[WORK_MODES.OFF]: 'Off',
56-
}
57-
5846
/** DP100 device class.
5947
*
6048
* This class is used to interact with the DP100 power supply.
@@ -69,7 +57,7 @@ const WORK_MODE_MAP = {
6957
* console.info('Temperature 1:', temp1, '°C')
7058
* console.info('Temperature 2:', temp2, '°C')
7159
* console.info('DC 5V:', dc5V, 'V')
72-
* console.info('Output Mode:', WORK_MODE_MAP[outMode])
60+
* console.info('Output Mode:', outMode)
7361
* console.info('Work State:', workSt)
7462
* }
7563
* }
@@ -79,6 +67,7 @@ const WORK_MODE_MAP = {
7967
*/
8068
export class DP100 {
8169

70+
/** The connected DP100 device. */
8271
device = null
8372

8473
/** Connect to the DP100 device. */
@@ -93,9 +82,12 @@ export class DP100 {
9382
)
9483
}
9584

96-
/** Send a report to the DP100
85+
/**
86+
* Send a report to the DP100.
87+
*
9788
* @param {Number} functionId -- The function to call on the DP100.
9889
* @param {Uint8Array} content -- The data to send to the DP100.
90+
* @returns {Promise<void>} -- A promise that resolves when the report is sent.
9991
*/
10092
async sendReport (functionId, content = null) {
10193
content = content || new Uint8Array(0)
@@ -111,7 +103,7 @@ export class DP100 {
111103
const checksum = crc16(report.buffer.slice(0, report.length - 2))
112104
reportView.setUint16(report.length - 2, checksum, true)
113105
console.debug('device.sendReport', reportView)
114-
return this.device.sendReport(0, report)
106+
return await this.device.sendReport(0, report)
115107
}
116108

117109
/** Handle input reports from the DP100
@@ -141,7 +133,7 @@ export class DP100 {
141133

142134
switch (header.functionType) {
143135
case FUNCTIONS.BASIC_INFO:
144-
const basicInfo = {
136+
this.receiveBasicInfo({
145137
vIn: contentView.getUint16(0, true) / 1000,
146138
vOut: contentView.getUint16(2, true) / 1000,
147139
iOut: contentView.getUint16(4, true) / 1000,
@@ -151,8 +143,7 @@ export class DP100 {
151143
dc5V: contentView.getUint16(12, true) / 1000,
152144
outMode: contentView.getUint8(14),
153145
workSt: contentView.getUint8(15)
154-
}
155-
this.receiveBasicInfo(basicInfo)
146+
})
156147
break
157148
default:
158149
console.warn('Unhandled function', header.functionType)

0 commit comments

Comments
 (0)