News:

Let's find out together what makes a PIC Tick!

Main Menu

Enum in Positron

Started by Frizie, Jul 01, 2026, 02:49 PM

Previous topic - Next topic

Frizie

Hi Les.

I regularly write code like this:

Symbol Monday    = 1
Symbol Tuesday   = 2
Symbol Wednesday = 3
Symbol Thursday  = 4
Symbol Friday    = 5
Symbol Saturday  = 6
Symbol Sunday    = 7

Is there a way to do this sort of thing like this:

Enum 1, (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
Ohm sweet Ohm | www.picbasic.nl

Abdullah

Hi
I would also like to know if there's a way to do this. It would make things much easier.
Best regards
Abdullah abid
Abdullah

top204

#2
I will investigate a simplistic enum mechanism, such as the one you pseudo coded. However, in the list of values, they are not always sequential, so each one has the ability to start a new value onwards.

For example:

Enum {Monday = 1, Tuesday, Wednesday, Thursday, Friday = 8, Saturday, Sunday}

Or

Enum {Monday = 1,
      Tuesday,
      Wednesday,
      Thursday,
      Friday = 8,
      Saturday,
      Sunday}


Regards
Les


JonW

#3
Les, any chance of also looking at doing a Struct for positron so a .dot notation can be used.  Here is a theoretical example of what it could possibly look like.

'=========================================================
' PROPOSED syntax - Struct does NOT exist in Positron
' today. Minimal demo: sensor readings grouped in a
' struct, passed ByRef to shared procedures.
'=========================================================
Device = 18F26K22
Declare Xtal = 64

'--- USART setup
Declare HSerial_Baud  = 115200
Declare HSerial_Clear = On

'--- ADC setup for ADIn
Declare Adin_Res   = 10             ' 10-bit result
Declare Adin_Tad   = FRC            ' internal RC ADC clock
Declare Adin_Stime = 50             ' acquisition time in us

'---------------------------------------------------------
' Type definition - a template only, consumes NO RAM.
'---------------------------------------------------------
Struct TSensor
    RawADC   As Word                ' offset 0-1: raw ADC value
    Scaled   As Word                ' offset 2-3: engineering units
    Status   As Byte                ' offset 4  : status code
    Retries  As Byte                ' offset 5  : fault retry counter
EndStruct                           ' SizeOf(TSensor) = 6 bytes

'--- Status codes: plain Symbols, current Positron syntax, could be Enum
Symbol stIdle  = 0
Symbol stBusy  = 1
Symbol stDone  = 2
Symbol stFault = 10

'---------------------------------------------------------
' Instances - THIS is where RAM is allocated: 6 bytes
' each, members become ordinary variables at fixed
' offsets from the instance base.
'---------------------------------------------------------
Dim Temperature As TSensor
Dim Pressure    As TSensor

'---------------------------------------------------------
' ByRef proc: nothing copied - caller loads FSR0 with the
' instance base, members access [FSR0 + constant offset].
' One proc in flash serves every instance.
'---------------------------------------------------------
Proc InitSensor(ByRef pSensor As TSensor)
    pSensor.RawADC  = 0
    pSensor.Scaled  = 0
    pSensor.Status  = stIdle
    pSensor.Retries = 0
EndProc

'--- Reads ADC, updates the struct in place, returns Scaled
Proc ReadScaled(ByRef pSensor As TSensor), Word
    pSensor.RawADC = ADIn 0
    pSensor.Scaled = (pSensor.RawADC * 500) / 1023
    Result = pSensor.Scaled
EndProc

'---------------------------------------------------------
' Main - init once, loop measurements
'---------------------------------------------------------
Main:
    InitSensor(Temperature)
    InitSensor(Pressure)

MeasureLoop:
    '--- Direct member access: compiles identically to a
    '    plain Dim'd Word - zero struct overhead
    Temperature.Status = stBusy
    HRSOut "Temp: ", Dec ReadScaled(Temperature), 13
    Temperature.Status = stDone

    If Pressure.Status = stFault Then
        Pressure.Retries = Pressure.Retries + 1
    EndIf

    DelayMS 500
    GoTo MeasureLoop

Structs also have a great use case in multi-register memory allocation, here is another theoretical example of a Positron Struct


'=========================================================
' PROPOSED syntax - Struct/Bits do NOT exist in Positron
' today. Mixed-member chip driver example: generic
' synth/clock IC with 8-bit and 16-bit registers.
'
' Storage members (Byte/Word/Dword) occupy RAM.
' Bits(pos,width) members are compile-time VIEWS onto a
' named storage member - they occupy NO RAM.
'=========================================================
Device = 18F26K22
Declare Xtal = 64

Struct TSynth
    '--- Real storage: 8 bytes of RAM total -----------------
    DevID    As Byte                    ' offset 0: read-back ID
    Control  As Byte                    ' offset 1: packed flags
    RefDiv   As Byte                    ' offset 2: reference divider
    Status   As Byte                    ' offset 3: last status read
    NCount   As Word                    ' offset 4-5: 16-bit N divider
    Config   As Word                    ' offset 6-7: packed 16-bit reg

    '--- Views onto Control (Byte) - no RAM ----------------
    PwrDown  As Control.Bits(0, 1)      ' single bit
    OutEn    As Control.Bits(1, 1)
    Icp      As Control.Bits(2, 3)      ' 3-bit charge pump field
    MuxSel   As Control.Bits(5, 3)      ' 3-bit mux field

    '--- Views onto Config (Word) - no RAM -----------------
    ChanSel  As Config.Bits(0, 4)       ' 4-bit field = a nibble
    Gain     As Config.Bits(4, 2)
    FiltBW   As Config.Bits(6, 3)
    TestMode As Config.Bits(15, 1)
EndStruct                               ' SizeOf(TSynth) = 8 bytes

Dim Synth As TSynth                     ' Instance: the Struct above is only a
                                        ' template - THIS line allocates the 8
                                        ' bytes of RAM, and each member becomes
                                        ' an ordinary variable at a fixed offset
                                        ' from the instance base.

'--- Charge pump current settings (datasheet table) - plain
'    Symbols do the job here, no new syntax needed
Symbol icp310uA = 0
Symbol icp625uA = 1
Symbol icp1mA25 = 3
Symbol icp2mA5  = 7

'---------------------------------------------------------
' Usage - mixing all four granularities on one struct
'---------------------------------------------------------
Main:
    '--- Whole-member access: plain Byte/Word writes,
    '    identical codegen to individually Dim'd variables
    Synth.RefDiv = 10
    Synth.NCount = 4800                 ' 16-bit divider in one line

    '--- Bitfield views: each compiles to one constant-
    '    masked read-modify-write on the parent member
    Synth.PwrDown = 0
    Synth.OutEn   = 1
    Synth.Icp     = icp1mA25            ' named constant into 3-bit field
    Synth.ChanSel = 5                   ' 4-bit field in Config
    Synth.FiltBW  = 2

    '--- EXISTING Positron accessors still work, because
    '    Synth.NCount resolves to an ordinary Word:
    HRSOut "N hi: ", Dec Synth.NCount.Byte1, 13   ' high byte
    HRSOut "N lo: ", Dec Synth.NCount.Byte0, 13   ' low byte

    If Synth.Config.15 = 1 Then         ' existing Word.bit access
        HRSOut "Test mode!", 13         ' (same bit as TestMode view)
    EndIf

    Stop



be a great addition IMHO, one of the advantages of C

CPR

Definite +1 "yes" to this if it's possible!   :)

Dompie

YES, these are two very good and very useful expansions, if that is possible.

Johan