Hello,

I am a young embedded software engineer, small microcontrollers almost 
exclusively, C exclusively. I'm very excited about what I see in Nim, but I am 
having trouble translating some concepts that I could do in my sleep in 
embedded C or assembly. I wonder if I'm just trying to shoehorn in what I'm 
comfortable with in C. I want to create a statically allocated ring buffer 
using a template/generic/whatever so that I can instantiate it in the file in 
question and then use a handle to it for add and remove operations and have 
full/empty status covered in the add/remove routines.

I realize a ring buffer is simple, but as a first library type of exercise I 
would like to be able to use the implementation over and over again by 
including the file in other projects. I guess this means it would need two 
parameters if it was a generic like so maybe:
    
    
    type
      RingBuffer*[N,T] = ref object #N=size, T=type
        buf: array[N,T]
        head, tail: int
    

I think ref force allocates something on the heap at runtime and I'm fine with 
that if static allocation isn't possible but at least it can't be resizable. 
I'm particularly confused about how to do an add method though. Here's where 
I'm stuck:
    
    
    proc add*[T](self: var RingBuffer[N,T], data: T) =
      # some stuff
    

I am totally confused as to how to do this. I haven't seen any examples like 
this either. I've seen the one involving linked lists and plenty of examples 
using seqs.

Further, I would like the head and tail index variables to be the smallest 
width they can be and still point to anywhere in the array. I think that can be 
done at compile time, maybe with a process? It would be even better if the 
smallest power of 2 was selected automatically, provided Nim and/or the C 
compiler were smart enough to use bitwise and for bounds checking.

Thank you very much in advance for your help. I typically learn by reading 
everything I can find on a subject but there just isn't an exhaustive list of 
material out there for Nim yet.

* * *

Maybe I'm just too tired to think and the answer is obvious! I need to get that 
book... A few examples that target the use cases of embedded software would 
really help. I think embedded could be a breakthrough arena for Nim. I was 
particularly excited about Nim when I started thinking of how great it would be 
if the Nim compiler could target FreeRTOS or something similar using a flag 
like \--os=freertos. That would be a game changer. Of course I'm aware you can 
wrap libraries, but to have it built into the language would rock. Maybe when I 
get the hang of this I'll try to tackle that.

I'm running Nim code on a medium size MSP430 32K Flash, 4K RAM. The 
MSP430F5510-STK board from Olimex.

Reply via email to