Skip to content

Commit 278461b

Browse files
Add logics to try to receive and send information from the device.
Vendor radiacode.js from https://github.com/bsharper/radiacode.js and try to use it to decode the information received from the device.
1 parent 6fdf30b commit 278461b

7 files changed

Lines changed: 2667 additions & 12 deletions

File tree

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import BluetoothExtensionPlugins from './bluetooth-extension';
22
import MoveHubExtensionPlugins from './movehub-extension';
33
import RadiacodeDectectorExtensionPlugins from './radiacode-extension';
44

5-
const plugins = BluetoothExtensionPlugins.concat(MoveHubExtensionPlugins, RadiacodeDectectorExtensionPlugins);
5+
const plugins = BluetoothExtensionPlugins.concat(
6+
MoveHubExtensionPlugins,
7+
RadiacodeDectectorExtensionPlugins
8+
);
69
export default plugins;
7-
Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
1-
import { BluetoothManager } from "../bluetooth/BluetoothManager";
1+
import { BluetoothManager } from '../bluetooth/BluetoothManager';
2+
import { DeviceInfo } from './type';
3+
import { buildShortIdentifier } from '../bluetooth-extension';
4+
import {
5+
radiacodeNotifyCharacteristicUUID,
6+
radiacodeServiceUUID,
7+
radiacodeWriteCharacteristicUUID
8+
} from '.';
9+
//import { Buffer } from '../movehub-extension/moveHub/helpers/buffer';
10+
import { ProtocolManager } from './protocol/protocolManager';
11+
import { COMMAND } from './protocol/protocol';
212

13+
export const defaultDeviceInfo: DeviceInfo = {
14+
err: '',
15+
connected: false,
16+
batteryLevel: undefined,
17+
identifier: '',
18+
primaryMACAddress: ''
19+
};
320

421
export class RadiacodeDetector extends BluetoothManager.Device {
5-
constructor(native: BluetoothDevice) {
6-
super(native);
22+
public deviceInfo: DeviceInfo;
23+
public notifyCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
24+
public writeCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
25+
public protocolManager: ProtocolManager;
26+
27+
constructor(native: BluetoothDevice) {
28+
super(native);
29+
this.deviceInfo = defaultDeviceInfo;
30+
}
31+
32+
async initDevice(): Promise<void> {
33+
this.connected.connect(async (sender, connected: boolean) => {
34+
if (connected) {
35+
this.deviceInfo.connected = connected;
36+
this.isConnected = connected;
37+
}
38+
console.warn('The connection state is', this.isConnected);
39+
this.deviceInfo.identifier = buildShortIdentifier(this.native);
40+
});
41+
this.disconnected.connect(async (sender, disconnected: boolean) => {
42+
if (disconnected) {
43+
this.deviceInfo.connected = false;
44+
this.isConnected = false;
45+
}
46+
console.warn('The connection state is', this.isConnected);
47+
});
48+
49+
this.writeCharacteristic = await this.getCharacteristic(
50+
radiacodeServiceUUID,
51+
radiacodeWriteCharacteristicUUID
52+
);
53+
54+
this.notifyCharacteristic = await this.getCharacteristic(
55+
radiacodeServiceUUID,
56+
radiacodeNotifyCharacteristicUUID
57+
);
58+
this.protocolManager = new ProtocolManager(
59+
this.notifyCharacteristic,
60+
this.writeCharacteristic
61+
);
62+
63+
await this.protocolManager.init();
64+
65+
/* this.protocolManager.readData(new Uint8Array([0x11, 0x05, 0x00, 0x00]), COMMAND.RD_VIRT_SFR, "brightness").then((brightness) => {
66+
console.log(`Read ${brightness} command sent successfully.`);
67+
}).catch(error => {
68+
console.error('Error sending read brightness command:', error);
69+
});*/
70+
71+
/*this.protocolManager.readData(new Uint8Array([0x24, 0x08, 0x00, 0x00]), COMMAND.RD_VIRT_SFR, "temperature").then((temperature) => {
72+
console.log(`Read ${temperature} command sent successfully.`);
73+
}).catch(error => {
74+
console.error('Error sending read temperature command:', error);
75+
});*/
76+
77+
try {
78+
const brightness = await this.protocolManager.readData(
79+
new Uint8Array([0x11, 0x05, 0x00, 0x00]),
80+
COMMAND.RD_VIRT_SFR,
81+
'brightness'
82+
);
83+
console.log(`Read ${brightness} command returned successfully.`);
84+
} catch (error) {
85+
console.error('Error sending read brightness command:', error);
786
}
8-
}
87+
}
88+
}

src/radiacode-extension/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {
99
BluetoothManager
1010
} from '../bluetooth/BluetoothManager';
1111
import { RadiacodeDetector } from './detector';
12-
export const radiacodeServiceUUID = 'e63215e5-7003-49d8-96b0-b024798fb901'
13-
export const radiacodeReadCharacteristicUUID = 'e63215e6-7003-49d8-96b0-b024798fb901';
14-
export const radiacodeWriteCharacteristicUUID = 'e63215e6-7003-49d8-96b0-b024798fb901';
12+
export const radiacodeServiceUUID = 'e63215e5-7003-49d8-96b0-b024798fb901';
13+
export const radiacodeNotifyCharacteristicUUID =
14+
'e63215e7-7003-49d8-96b0-b024798fb901';
15+
export const radiacodeWriteCharacteristicUUID =
16+
'e63215e6-7003-49d8-96b0-b024798fb901';
1517
export const radiacodeRegistryItem: IDeviceTypeRegistryItem = {
1618
deviceType: 'Radiacode® 110',
1719
options: {
@@ -21,13 +23,15 @@ export const radiacodeRegistryItem: IDeviceTypeRegistryItem = {
2123
},
2224
factory: async (native: BluetoothDevice) => {
2325
const device = new RadiacodeDetector(native);
26+
await device.initDevice();
2427
return device;
2528
}
2629
};
2730

2831
const RadiacodeDetectorRegisterPlugin: JupyterFrontEndPlugin<void> = {
2932
id: 'bluetooth-manager:radiacode-detector-register-plugin',
30-
description: 'Registers the radiacode detector device and provides a factory.',
33+
description:
34+
'Registers the radiacode detector device and provides a factory.',
3135
requires: [IBluetoothManager],
3236
autoStart: true,
3337
activate: (
@@ -46,8 +50,7 @@ const RadiacodeDetectorRegisterPlugin: JupyterFrontEndPlugin<void> = {
4650
}
4751
};
4852

49-
5053
const RadiacodeDetectorExtensionPlugins: JupyterFrontEndPlugin<any>[] = [
51-
RadiacodeDetectorRegisterPlugin,
54+
RadiacodeDetectorRegisterPlugin
5255
];
5356
export default RadiacodeDetectorExtensionPlugins;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* These data are taken from https://github.com/cdump/radiacode/blob/master/src/radiacode/types.py */
2+
3+
export enum COMMAND {
4+
GET_STATUS = 0x0005,
5+
SET_EXCHANGE = 0x0007,
6+
GET_VERSION = 0x000a,
7+
GET_SERIAL = 0x000b,
8+
FW_IMAGE_GET_INFO = 0x0012,
9+
FW_SIGNATURE = 0x0101,
10+
RD_HW_CONFIG = 0x0807,
11+
RD_VIRT_SFR = 0x0824,
12+
WR_VIRT_SFR = 0x0825,
13+
RD_VIRT_STRING = 0x0826,
14+
WR_VIRT_STRING = 0x0827,
15+
RD_VIRT_SFR_BATCH = 0x082a,
16+
WR_VIRT_SFR_BATCH = 0x082b,
17+
RD_FLASH = 0x081c,
18+
SET_TIME = 0x0a04
19+
}
20+
21+
export enum VSFR {
22+
DEVICE_CTRL = 0x0500,
23+
DEVICE_LANG = 0x0502,
24+
DEVICE_ON = 0x0503,
25+
DEVICE_TIME = 0x0504,
26+
27+
DISP_CTRL = 0x0510,
28+
DISP_BRT = 0x0511,
29+
DISP_CONTR = 0x0512,
30+
DISP_OFF_TIME = 0x0513,
31+
DISP_ON = 0x0514,
32+
DISP_DIR = 0x0515,
33+
DISP_BACKLT_ON = 0x0516,
34+
35+
SOUND_CTRL = 0x0520,
36+
SOUND_VOL = 0x0521,
37+
SOUND_ON = 0x0522,
38+
SOUND_BUTTON = 0x0523,
39+
40+
VIBRO_CTRL = 0x0530,
41+
VIBRO_ON = 0x0531,
42+
43+
LEDS_CTRL = 0x0540,
44+
LED0_BRT = 0x0541,
45+
LED1_BRT = 0x0542,
46+
LED2_BRT = 0x0543,
47+
LED3_BRT = 0x0544,
48+
LEDS_ON = 0x0545,
49+
50+
ALARM_MODE = 0x05e0,
51+
PLAY_SIGNAL = 0x05e1,
52+
53+
MS_CTRL = 0x0600,
54+
MS_MODE = 0x0601,
55+
MS_SUB_MODE = 0x0602,
56+
MS_RUN = 0x0603,
57+
58+
BLE_TX_PWR = 0x0700,
59+
60+
DR_LEV1_uR_h = 0x8000,
61+
DR_LEV2_uR_h = 0x8001,
62+
DS_LEV1_100uR = 0x8002,
63+
DS_LEV2_100uR = 0x8003,
64+
DS_UNITS = 0x8004,
65+
CPS_FILTER = 0x8005,
66+
RAW_FILTER = 0x8006,
67+
DOSE_RESET = 0x8007,
68+
CR_LEV1_cp10s = 0x8008,
69+
CR_LEV2_cp10s = 0x8009,
70+
71+
USE_nSv_h = 0x800c,
72+
73+
CHN_TO_keV_A0 = 0x8010,
74+
CHN_TO_keV_A1 = 0x8011,
75+
CHN_TO_keV_A2 = 0x8012,
76+
CR_UNITS = 0x8013,
77+
DS_LEV1_uR = 0x8014,
78+
DS_LEV2_uR = 0x8015,
79+
80+
CPS = 0x8020,
81+
DR_uR_h = 0x8021,
82+
DS_uR = 0x8022,
83+
84+
TEMP_degC = 0x8024,
85+
ACC_X = 0x8025,
86+
ACC_Y = 0x8026,
87+
ACC_Z = 0x8027,
88+
OPT = 0x8028,
89+
90+
RAW_TEMP_degC = 0x8033,
91+
TEMP_UP_degC = 0x8034,
92+
TEMP_DN_degC = 0x8035,
93+
94+
VBIAS_mV = 0xc000,
95+
COMP_LEV = 0xc001,
96+
CALIB_MODE = 0xc002,
97+
DPOT_RDAC = 0xc004,
98+
DPOT_RDAC_EEPROM = 0xc005,
99+
DPOT_TOLER = 0xc006,
100+
101+
SYS_MCU_ID0 = 0xffff0000,
102+
SYS_MCU_ID1 = 0xffff0001,
103+
SYS_MCU_ID2 = 0xffff0002,
104+
105+
SYS_DEVICE_ID = 0xffff0005,
106+
SYS_SIGNATURE = 0xffff0006,
107+
SYS_RX_SIZE = 0xffff0007,
108+
SYS_TX_SIZE = 0xffff0008,
109+
SYS_BOOT_VERSION = 0xffff0009,
110+
SYS_TARGET_VERSION = 0xffff000a,
111+
SYS_STATUS = 0xffff000b,
112+
SYS_MCU_VREF = 0xffff000c,
113+
SYS_MCU_TEMP = 0xffff000d,
114+
SYS_FW_VER_BT = 0xffff010 // note: original had 0xFFFF010 (looks like a typo in source); keep as-is or correct if needed
115+
}

0 commit comments

Comments
 (0)