Shop OBEX P1 Docs P2 Docs Learn Events
SPIN2 namespaces — Parallax Forums

SPIN2 namespaces

Hey folks (especially spin2 toolchain developers: @cgracey @ersmith @Wuerfel_21 @macca )
How would you feel about the idea of extending the namespaces support in DAT blocks to the rest of a spin2 file? It feels like with some of the other features added to spin2 in recent years (like structures especially, but also interfaces in flexspin), that we're a hop, skip and a jump away from being able to have separate namespaces within the same spin2 file (of course I could be wrong!).

To illustrate: currently in spin2, a driver for an IMU chip (so, some combination of: accelerometer, gyroscope, magnetometer, barometer/altimeter) might be written such that methods for each of those "sub-sensors" that do the same thing (for example a data ready signal):

' parent object
obj imu: "imu-sensor-driver.spin2"

pub main() | ax, ay, az

if ( imu.accel_data_ready() )
    imu.accel_data(@ax, @ay, @az)
' etc...for gyro, or others
' child object: imu-sensor-driver.spin2
pub accel_data_ready(): r ' called like: imu.accel_data_ready()
pub gyro_data_ready(): r ' called like: imu.gyro_data_ready()
pub mag_data_ready(): r ' called like: imu.mag_data_ready()

etc. Basically any method relevant only to a specific logical block of the chip might be named with the appropriate prefix.
With namespaces, all of the methods that were relevant to those particular logical blocks within the chip might be rewritten something like this:

' parent object
obj imu: "imu-sensor-driver.spin2"

pub main() | ax, ay, az

if ( imu.accel.data_ready() )
    imu.data(@ax, @ay, @az)
' etc...for gyro, or others
' child object: imu-sensor-driver.spin2
%namespace accel ' change namespace to accel until it's changed again
pub data(x, y, z) ' this would be called from the parent object like imu.accel.data(@x, @y, @z)
pub data_ready(): r ' imu.accel.data_ready()
pub interrupt_flags(): f ' imu.accel_interrupt_flags()

%namespace gyro
pub data(x, y, z) ' imu.gyro.data(@x, @y, @z)
pub data_ready(): r ' imu.gyro.data_ready()
pub interrupt_flags(): f ' imu.gyro.interrupt_flags()

%namespace mag
pub data(x, y, z) ' imu.mag.data(@x, @y, @z)
pub data_ready(): r ' imu.mag.data_ready()
pub interrupt_flags(): f ' imu.mag.interrupt_flags()

This wouldn't be a replacement for child object files - it'd just provide the same separation they currently do, but for cases where you wouldn't really want to have to put things in separate files.

Is this something that could be completely resolved by the compiler or would it add some runtime bloat? I know child object files carry a small amount of runtime memory overhead. Hopefully this at least wouldn't be any worse.

This is mostly a "would be nice to have" thing, certainly not in my mind a necessity. Maybe people used to languages with similar features (C++? Py?) would see it as a more killer feature if they were considering spin2, not sure.

Thanks for reading!

Comments

Sign In or Register to comment.