nimphea/nimphea_macros

Source   Edit  

Nimphea - C++ Interop Macros

This module contains the compile-time macro system for automatic C++ type generation. It provides two macros:

  • useNimpheaNamespace() - Include all typedefs (recommended)
  • useNimpheaModules(...) - Selective inclusion of specific modules

Users should import this module indirectly through nimphea.nim

Consts

adcTypedefs = ["AdcChannelConfig AdcChannelConfig",
               "AdcHandle::OverSampling OverSampling",
               "AdcChannelConfig::ConversionSpeed ConversionSpeed",
               "AdcChannelConfig::MuxPin MuxPin"]
Source   Edit  
codec_ak4556Typedefs: seq[string] = []
Source   Edit  
codec_pcm3060Typedefs = ["Pcm3060::Result Pcm3060Result"]
Source   Edit  
codec_wm8731Typedefs = ["Wm8731::Result Wm8731Result",
                        "Wm8731::Config Wm8731Config",
                        "Wm8731::Config::Format Wm8731Format",
                        "Wm8731::Config::WordLength Wm8731WordLength"]
Source   Edit  
controlsTypedefs = ["Switch::Type SwitchType",
                    "Switch::Polarity SwitchPolarity", "GPIO::Pull SwitchPull"]
Source   Edit  
coreTypedefs = ["GPIO::Mode GPIOMode", "GPIO::Pull GPIOPull",
                "GPIO::Speed GPIOSpeed",
                "SaiHandle::Config::SampleRate SampleRate",
                "DaisySeed::BoardVersion BoardVersion"]
Source   Edit  
dacTypedefs = ["DacHandle::Result DacResult", "DacHandle::Channel DacChannel",
               "DacHandle::Mode DacMode", "DacHandle::BitDepth DacBitDepth",
               "DacHandle::BufferState DacBufferState",
               "DacHandle::Config DacConfig", "DacHandle DacHandle"]
Source   Edit  
daisyHeaders = """#include "daisy_seed.h"
#include "hid/switch.h"
#include "hid/encoder.h"
#include "hid/usb.h"
#include "dev/sdram.h"
#include "per/i2c.h"
#include "per/spi.h"
#include "per/uart.h"
#include "per/adc.h"
#include "per/pwm.h"
#include "dev/oled_ssd130x.h"
"""
Source   Edit  
daisyTypedefsList = ["GPIO::Mode GPIOMode", "GPIO::Pull GPIOPull",
                     "GPIO::Speed GPIOSpeed",
                     "SaiHandle::Config::SampleRate SampleRate",
                     "DaisySeed::BoardVersion BoardVersion",
                     "Switch::Type SwitchType",
                     "Switch::Polarity SwitchPolarity", "GPIO::Pull SwitchPull",
                     "AdcChannelConfig AdcChannelConfig",
                     "AdcHandle::OverSampling OverSampling",
                     "AdcChannelConfig::ConversionSpeed ConversionSpeed",
                     "AdcChannelConfig::MuxPin MuxPin", "PWMHandle PwmHandle",
                     "PWMHandle::Config PwmConfig",
                     "PWMHandle::Config::Peripheral PwmPeripheral",
                     "PWMHandle::Result PwmResult",
                     "PWMHandle::Channel PwmChannel",
                     "PWMHandle::Channel::Config PwmChannelConfig",
                     "PWMHandle::Channel::Config::Polarity PwmPolarity",
                     "SSD130xI2CTransport SSD130xI2CTransport",
                     "SSD130xI2CTransport::Config SSD130xI2CTransportConfig",
                     "SSD130x4WireSpiTransport SSD130x4WireSpiTransport", "SSD130x4WireSpiTransport::Config SSD130x4WireSpiTransportConfig",
                     "SSD130xI2c128x64Driver OledDisplay128x64I2c",
                     "SSD130xI2c128x32Driver OledDisplay128x32I2c",
                     "SSD130xI2c64x48Driver OledDisplay64x48I2c",
                     "SSD130xI2c64x32Driver OledDisplay64x32I2c",
                     "SSD130x4WireSpi128x64Driver OledDisplay128x64Spi",
                     "SSD130x4WireSpi128x32Driver OledDisplay128x32Spi",
                     "SSD130x4WireSpi64x48Driver OledDisplay64x48Spi",
                     "SSD130x4WireSpi64x32Driver OledDisplay64x32Spi",
                     "SSD130xI2c128x64Driver::Config OledDisplayI2cConfig", "SSD130x4WireSpi128x64Driver::Config OledDisplaySpiConfig",
                     "I2CHandle I2CHandle", "I2CHandle::Config I2CConfig",
                     "I2CHandle::Config::Speed I2CSpeed",
                     "I2CHandle::Config::Peripheral I2CPeripheral",
                     "I2CHandle::Config::Mode I2CMode",
                     "I2CHandle::Result I2CResult",
                     "SpiHandle::Config SpiConfig",
                     "SpiHandle::Config::Peripheral SpiPeripheral",
                     "SpiHandle::Config::Mode SpiMode",
                     "SpiHandle::Config::ClockPolarity SpiClockPolarity",
                     "SpiHandle::Config::ClockPhase SpiClockPhase",
                     "SpiHandle::Config::Direction SpiDirection",
                     "SpiHandle::Config::NSS SpiNSS",
                     "SpiHandle::Config::BaudPrescaler SpiBaudPrescaler",
                     "SpiHandle::Result SpiResult",
                     "SdramHandle::Result SdramResult",
                     "UsbHandle::Result UsbResult",
                     "UsbHandle::UsbPeriph UsbPeriph",
                     "SdmmcHandler::Result SdmmcResult",
                     "SdmmcHandler::BusWidth SdmmcBusWidth",
                     "SdmmcHandler::Speed SdmmcSpeed",
                     "SdmmcHandler::Config SdmmcConfig",
                     "SdmmcHandler SdmmcHandler",
                     "FatFSInterface::Result FatFSResult",
                     "FatFSInterface::Config::Media FatFSMedia",
                     "FatFSInterface::Config FatFSConfig",
                     "FatFSInterface FatFSInterface",
                     "Wm8731::Result Wm8731Result",
                     "Wm8731::Config Wm8731Config",
                     "Wm8731::Config::Format Wm8731Format",
                     "Wm8731::Config::WordLength Wm8731WordLength",
                     "Pcm3060::Result Pcm3060Result",
                     "LcdHD44780::Config LcdHD44780Config", "FontDef FontDef",
                     "QSPIHandle::Result QSPIResult",
                     "QSPIHandle::Config QSPIConfig",
                     "QSPIHandle::Config::Mode QSPIMode",
                     "QSPIHandle::Config::Device QSPIDevice",
                     "PersistentStorage<int>::State StorageState"]
Source   Edit  
dmaTypedefs: seq[string] = []
Source   Edit  
dotstar: seq[string] = []
Source   Edit  
fatfsTypedefs: seq[string] = []
Source   Edit  
fieldTypedefs = ["LedDriverPca9685<2, true> FieldLedDriver"]
Source   Edit  
fileReaderTypedefs: seq[string] = []
Source   Edit  
fileTableTypedefs = ["daisy::FileTable<8> FileTable8",
                     "daisy::FileTable<16> FileTable16",
                     "daisy::FileTable<32> FileTable32",
                     "daisy::FileTable<64> FileTable64",
                     "daisy::FileTable<128> FileTable128"]
Source   Edit  
i2cTypedefs = ["I2CHandle I2CHandle", "I2CHandle::Config I2CConfig",
               "I2CHandle::Config::Speed I2CSpeed",
               "I2CHandle::Config::Peripheral I2CPeripheral",
               "I2CHandle::Config::Mode I2CMode", "I2CHandle::Result I2CResult"]
Source   Edit  
lcd_hd44780Typedefs = ["LcdHD44780::Config LcdHD44780Config"]
Source   Edit  
leddriver: seq[string] = []
Source   Edit  
legioTypedefs: seq[string] = []
Source   Edit  
loggerTypedefs = ["LoggerDestination LoggerDestination"]
Source   Edit  
max11300: seq[string] = []
Source   Edit  
mcp23x17: seq[string] = []
Source   Edit  
midiTypedefs: seq[string] = []
Source   Edit  
neopixel: seq[string] = []
Source   Edit  
oled_fontsTypedefs = ["FontDef FontDef"]
Source   Edit  
oledTypedefs = ["SSD130xI2CTransport SSD130xI2CTransport",
                "SSD130xI2CTransport::Config SSD130xI2CTransportConfig",
                "SSD130x4WireSpiTransport SSD130x4WireSpiTransport", "SSD130x4WireSpiTransport::Config SSD130x4WireSpiTransportConfig",
                "SSD130xI2c128x64Driver OledDisplay128x64I2c",
                "SSD130xI2c128x32Driver OledDisplay128x32I2c",
                "SSD130xI2c64x48Driver OledDisplay64x48I2c",
                "SSD130xI2c64x32Driver OledDisplay64x32I2c",
                "SSD130x4WireSpi128x64Driver OledDisplay128x64Spi",
                "SSD130x4WireSpi128x32Driver OledDisplay128x32Spi",
                "SSD130x4WireSpi64x48Driver OledDisplay64x48Spi",
                "SSD130x4WireSpi64x32Driver OledDisplay64x32Spi",
                "SSD130xI2c128x64Driver::Config OledDisplayI2cConfig",
                "SSD130x4WireSpi128x64Driver::Config OledDisplaySpiConfig"]
Source   Edit  
parameterTypedefs = ["Parameter::Curve Curve"]
Source   Edit  
patchSmTypedefs: seq[string] = []
Source   Edit  
persistentStorageTypedefs = ["PersistentStorage<int>::State StorageState"]
Source   Edit  
petalTypedefs = ["LedDriverPca9685<2, true> PetalLedDriver"]
Source   Edit  
podTypedefs: seq[string] = []
Source   Edit  
pwmTypedefs = ["PWMHandle PwmHandle", "PWMHandle::Config PwmConfig",
               "PWMHandle::Config::Peripheral PwmPeripheral",
               "PWMHandle::Result PwmResult", "PWMHandle::Channel PwmChannel",
               "PWMHandle::Channel::Config PwmChannelConfig",
               "PWMHandle::Channel::Config::Polarity PwmPolarity"]
Source   Edit  
qspiTypedefs = ["QSPIHandle::Result QSPIResult",
                "QSPIHandle::Config QSPIConfig",
                "QSPIHandle::Config::Mode QSPIMode",
                "QSPIHandle::Config::Device QSPIDevice"]
Source   Edit  
saiTypedefs: seq[string] = []
Source   Edit  
scopedIrqTypedefs: seq[string] = []
Source   Edit  
sdmmcTypedefs = ["SdmmcHandler::Result SdmmcResult",
                 "SdmmcHandler::BusWidth SdmmcBusWidth",
                 "SdmmcHandler::Speed SdmmcSpeed",
                 "SdmmcHandler::Config SdmmcConfig",
                 "SdmmcHandler SdmmcHandler",
                 "FatFSInterface::Result FatFSResult",
                 "FatFSInterface::Config::Media FatFSMedia",
                 "FatFSInterface::Config FatFSConfig",
                 "FatFSInterface FatFSInterface"]
Source   Edit  
sdramHelperFunction = """inline void clearSdramBss() {
    extern uint32_t _ssdram_bss;
    extern uint32_t _esdram_bss;
    uint32_t* start = &_ssdram_bss;
    uint32_t* end = &_esdram_bss;
    while(start < end) {
        *start++ = 0;
    }
}
"""
Source   Edit  
sdramTypedefs = ["SdramHandle::Result SdramResult"]
Source   Edit  
sh1106Typedefs = ["SH1106I2c128x64Driver SH1106I2c128x64",
                  "SH11064WireSpi128x64Driver SH1106Spi128x64",
                  "SH1106I2c128x64::Config SH1106I2cConfig",
                  "SH1106Spi128x64::Config SH1106SpiConfig"]
Source   Edit  
spiMultislaveTypedefs: seq[string] = []
Source   Edit  
spiTypedefs = ["SpiHandle::Config SpiConfig",
               "SpiHandle::Config::Peripheral SpiPeripheral",
               "SpiHandle::Config::Mode SpiMode",
               "SpiHandle::Config::ClockPolarity SpiClockPolarity",
               "SpiHandle::Config::ClockPhase SpiClockPhase",
               "SpiHandle::Config::Direction SpiDirection",
               "SpiHandle::Config::NSS SpiNSS",
               "SpiHandle::Config::BaudPrescaler SpiBaudPrescaler",
               "SpiHandle::Result SpiResult"]
Source   Edit  
sr595: seq[string] = []
Source   Edit  
sr4021: seq[string] = []
Source   Edit  
ssd1327Typedefs = ["SSD13274WireSpi128x128Driver SSD1327Spi128x128",
                   "SSD1327Spi128x128::Config SSD1327SpiConfig"]
Source   Edit  
ssd1351Typedefs = ["SSD13514WireSpi128x128Driver SSD1351Spi128x128",
                   "SSD1351Spi128x128::Config SSD1351SpiConfig"]
Source   Edit  
systemTypedefs = ["System::Config SystemConfig",
                  "System::Config::SysClkFreq SysClkFreq",
                  "System::MemoryRegion MemoryRegion",
                  "System::BootInfo BootInfo"]
Source   Edit  
timerTypedefs = ["TimerHandle::Config::Peripheral TimerPeripheral",
                 "TimerHandle::Config::CounterDir TimerCounterDir",
                 "TimerHandle::Result TimerResult",
                 "TimerHandle::Config TimerConfig", "TimerHandle TimerHandle"]
Source   Edit  
uiCoreTypedefs = ["daisy::UI::SpecialControlIds UiSpecialControlIds"]
Source   Edit  
uiTypedefs: seq[string] = []
Source   Edit  
usbHostTypedefs: seq[string] = []
Source   Edit  
usbMidiTypedefs: seq[string] = []
Source   Edit  
usbTypedefs = ["UsbHandle::Result UsbResult", "UsbHandle::UsbPeriph UsbPeriph"]
Source   Edit  
versioTypedefs: seq[string] = []
Source   Edit  
voctTypedefs: seq[string] = []
Source   Edit  
wavPlayerTypedefs = ["WavPlayer<4096> WavPlayer4K",
                     "WavPlayer<8192> WavPlayer8K",
                     "WavPlayer<16384> WavPlayer16K",
                     "WavPlayer<4096>::FileInfo WavPlayerFileInfo"]
Source   Edit  
wavWriterTypedefs = ["WavWriter<4096> WavWriter4K",
                     "WavWriter<8192> WavWriter8K",
                     "WavWriter<16384> WavWriter16K",
                     "WavWriter<4096>::Config WavWriterConfig"]
Source   Edit  

Procs

proc buildTypedefsString(typedefs: openArray[string]): string {....raises: [],
    tags: [], forbids: [].}
Helper to build typedef emit string from a list of typedefs Source   Edit  
proc getModuleHeaders(moduleName: string): string {....raises: [], tags: [],
    forbids: [].}
Returns the C++ header includes needed for a specific module Source   Edit  

Macros

macro useNimpheaModules(modules: varargs[untyped]): untyped

Selective inclusion - only includes typedefs for specified modules. This is more efficient than including everything.

Use this for minimal projects or when you want to reduce generated code size.

Available modules:

  • core - Always included automatically (GPIO, SampleRate, BoardVersion)
  • controls - Switches, encoders (Switch types)
  • adc - ADC types (AdcChannelConfig, OverSampling, etc.)
  • pwm - PWM types (PwmPeripheral, PwmChannel, etc.)
  • oled - OLED display types (OledDisplay128x64I2c, etc.)
  • i2c - I2C types (Speed, Peripheral, Mode)
  • spi - SPI types (Peripheral, Mode, ClockPolarity, etc.)
  • serial - UART types
  • sdram - SDRAM types (Result, helper functions)
  • usb - USB types (Result, UsbPeriph)
  • qspi - QSPI flash types (Result, Mode, Device)
  • spi_multislave - Multi-slave SPI (uses SPI typedefs)
  • persistent_storage - Persistent storage types (StorageState)

Usage:

import nimphea, per/i2c, per/spi
useNimpheaModules(i2c, spi)  # Only I2C and SPI typedefs

proc main() =
  var daisy = initDaisy()
  var i2c = initI2C(...)
  var spi = initSPI(...)
  # ...

Generates minimal C++ code at compile time - Only what you need!

Source   Edit  
macro useNimpheaNamespace(): untyped

Automatically generates all necessary C++ emit statements for Daisy types. This macro runs at compile time and injects the required C++ code.

This is the recommended approach for most projects.

Includes:

  • All C++ headers for Nimphea modules
  • Namespace declaration (using namespace daisy;)
  • All 26 type aliases
  • Helper functions (clearSdramBss)

Usage:

import nimphea
useNimpheaNamespace()  # One line - includes everything!

proc main() =
  var daisy = initDaisy()
  # ... use any Nimphea features

Compile-time code generation - Zero runtime cost!

Source   Edit