Skip to content

Commit 30640a3

Browse files
authored
Merge pull request #6 from harp-tech/gl-dev
Add schema reader module
2 parents 87b4934 + f4fee24 commit 30640a3

5 files changed

Lines changed: 185 additions & 4 deletions

File tree

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include LICENSE
2-
include README.md
2+
include README.md
3+
include harp/common.yml

harp/common.yml

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# yaml-language-server: $schema=registers.json
2+
registers:
3+
WhoAmI:
4+
address: 0
5+
type: U16
6+
access: Read
7+
description: Specifies the identity class of the device.
8+
HardwareVersionHigh:
9+
address: 1
10+
type: U8
11+
access: Read
12+
description: Specifies the major hardware version of the device.
13+
HardwareVersionLow:
14+
address: 2
15+
type: U8
16+
access: Read
17+
description: Specifies the minor hardware version of the device.
18+
AssemblyVersion:
19+
address: 3
20+
type: U8
21+
access: Read
22+
description: Specifies the version of the assembled components in the device.
23+
CoreVersionHigh:
24+
address: 4
25+
type: U8
26+
access: Read
27+
description: Specifies the major version of the Harp core implemented by the device.
28+
CoreVersionLow:
29+
address: 5
30+
type: U8
31+
access: Read
32+
description: Specifies the minor version of the Harp core implemented by the device.
33+
FirmwareVersionHigh:
34+
address: 6
35+
type: U8
36+
access: Read
37+
description: Specifies the major version of the Harp core implemented by the device.
38+
FirmwareVersionLow:
39+
address: 7
40+
type: U8
41+
access: Read
42+
description: Specifies the minor version of the Harp core implemented by the device.
43+
TimestampSeconds:
44+
address: 8
45+
type: U32
46+
access: [Read, Write, Event]
47+
description: Stores the integral part of the system timestamp, in seconds.
48+
volatile: true
49+
TimestampMicroseconds:
50+
address: 9
51+
type: U16
52+
access: Read
53+
description: Stores the fractional part of the system timestamp, in microseconds.
54+
volatile: true
55+
OperationControl:
56+
address: 10
57+
type: U8
58+
access: Write
59+
description: Stores the configuration mode of the device.
60+
payloadSpec:
61+
OperationMode:
62+
description: Specifies the operation mode of the device.
63+
maskType: OperationMode
64+
mask: 0x3
65+
DumpRegisters:
66+
description: Specifies whether the device should report the content of all registers on initialization.
67+
interfaceType: bool
68+
mask: 0x8
69+
MuteReplies:
70+
description: Specifies whether the replies to all commands will be muted, i.e. not sent by the device.
71+
interfaceType: bool
72+
mask: 0x10
73+
VisualIndicators:
74+
description: Specifies the state of all visual indicators on the device.
75+
maskType: LedState
76+
mask: 0x20
77+
OperationLed:
78+
description: Specifies whether the device state LED should report the operation mode of the device.
79+
maskType: LedState
80+
mask: 0x40
81+
Heartbeat:
82+
description: Specifies whether the device should report the content of the seconds register each second.
83+
maskType: EnableFlag
84+
mask: 0x80
85+
ResetDevice:
86+
address: 11
87+
type: U8
88+
access: Write
89+
maskType: ResetFlags
90+
description: Resets the device and saves non-volatile registers.
91+
DeviceName:
92+
address: 12
93+
type: U8
94+
length: 25
95+
access: Write
96+
description: Stores the user-specified device name.
97+
SerialNumber:
98+
address: 13
99+
type: U16
100+
access: Write
101+
description: Specifies the unique serial number of the device.
102+
ClockConfiguration:
103+
address: 14
104+
type: U8
105+
access: Write
106+
maskType: ClockConfigurationFlags
107+
description: Specifies the configuration for the device synchronization clock.
108+
groupMasks:
109+
OperationMode:
110+
description: Specifies the operation mode of the device.
111+
values:
112+
Standby: 0
113+
Active: 1
114+
Speed: 3
115+
EnableFlag:
116+
description: Specifies whether a specific register flag is enabled or disabled.
117+
values:
118+
Disabled: 0
119+
Enabled: 1
120+
LedState:
121+
description: Specifies the state of an LED on the device.
122+
values:
123+
Off: 0
124+
On: 1
125+
bitMasks:
126+
ResetFlags:
127+
description: Specifies the behavior of the non-volatile registers when resetting the device.
128+
bits:
129+
None: 0
130+
RestoreDefault: 0x1
131+
RestoreEeprom: 0x2
132+
Save: 0x4
133+
RestoreName: 0x8
134+
BootFromDefault: 0x40
135+
BootFromEeprom: 0x80
136+
ClockConfigurationFlags:
137+
description: Specifies configuration flags for the device synchronization clock.
138+
bits:
139+
None: 0
140+
ClockRepeater: 0x1
141+
ClockGenerator: 0x2
142+
RepeaterCapability: 0x8
143+
GeneratorCapability: 0x10
144+
ClockUnlock: 0x40
145+
ClockLock: 0x80

harp/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ class MaskValueItem(BaseModel):
3232
model_config = ConfigDict(
3333
extra='forbid',
3434
)
35+
value: int = Field(..., description='Specifies the numerical mask value.')
3536
description: Optional[str] = Field(
3637
None, description='Specifies a summary description of the mask value function.'
3738
)
3839

40+
def __int__(self):
41+
return self.value
42+
3943

4044
class MaskValue(RootModel[Union[int, MaskValueItem]]):
4145
root: Union[int, MaskValueItem]

harp/reader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _id_camel_to_snake(id: str):
4141
return _camel_to_snake_regex.sub("_", id).lower()
4242

4343

44-
def _create_bit_parser(mask: Union[int, MaskValueItem]):
44+
def _create_bit_parser(mask: int):
4545
def parser(xs: Series) -> Series:
4646
return (xs & mask) != 0
4747

@@ -50,7 +50,7 @@ def parser(xs: Series) -> Series:
5050

5151
def _create_bitmask_parser(bitMask: BitMask):
5252
lookup = [
53-
(_id_camel_to_snake(k), _create_bit_parser(v.root))
53+
(_id_camel_to_snake(k), _create_bit_parser(int(v.root)))
5454
for k, v in bitMask.bits.items()
5555
]
5656

@@ -61,7 +61,7 @@ def parser(df: DataFrame):
6161

6262

6363
def _create_groupmask_lookup(groupMask: GroupMask):
64-
return {v.root: n for n, v in groupMask.values.items()}
64+
return {int(v.root): n for n, v in groupMask.values.items()}
6565

6666

6767
def _create_groupmask_parser(name: str, groupMask: GroupMask):

harp/schema.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from os import PathLike
2+
from pathlib import Path
3+
from typing import TextIO, Union
4+
from harp.model import Model, Registers
5+
from pydantic_yaml import parse_yaml_raw_as
6+
7+
_common_yaml_path = Path(__file__).absolute().parent.joinpath("common.yml")
8+
9+
10+
def _read_common_registers(file: Union[str, PathLike, TextIO]) -> Registers:
11+
try:
12+
with open(file) as fileIO:
13+
return _read_common_registers(fileIO)
14+
except TypeError:
15+
return parse_yaml_raw_as(Registers, file.read())
16+
17+
18+
def read_schema(
19+
file: Union[str, PathLike, TextIO], include_common_registers: bool = True
20+
) -> Model:
21+
try:
22+
with open(file) as fileIO:
23+
return read_schema(fileIO, include_common_registers)
24+
except TypeError:
25+
schema = parse_yaml_raw_as(Model, file.read())
26+
if not "WhoAmI" in schema.registers and include_common_registers:
27+
common = _read_common_registers(_common_yaml_path)
28+
schema.registers = dict(common.registers, **schema.registers)
29+
schema.bitMasks = dict(common.bitMasks, **schema.bitMasks)
30+
schema.groupMasks = dict(common.groupMasks, **schema.groupMasks)
31+
return schema

0 commit comments

Comments
 (0)