I'm trying to create a type to represent a microcontroller register where the
address of the register is part of the type. This is the closest I can get to
what I want that will compile:
type
RegisterType* = uint8 | uint16 | uint32
RegisterPtr* = ptr RegisterType
RegisterAttr = enum
Readable,
Writable
RegisterAttrs = set[RegisterAttr]
RegisterConfig[
TAddress: static RegisterPtr,
TReadWriteAttr: static RegisterAttrs,
] = distinct NimbedConfig
proc initRegister*(address: static RegisterPtr, rwAttrs: static
RegisterAttrs): RegisterConfig[address, rwAttrs] =
discard
Run
and I declare and use a fictional register like this:
const PORTA = initRegister(cast[ptr uint8](0x0100), {Readable, Writable})
let a = PORTA.load()
PORTA.store(a+1)
Run
Can someone suggest how I should implement `initRegister()` so that the
declaration of `PORTA` becomes a bit cleaner:
const PORTA = initRegister(uint8, 0x0100, {Readable, Writable})
Run
side quest: What is it called when I put a static value such as 0x0100 into the
datatype? Is `RegisterConfig` a "type class"? I need to learn this topic better
and am looking for some search terms to use.