Shop OBEX P1 Docs P2 Docs Learn Events
from spin to C , how to translate this address ? — Parallax Forums

from spin to C , how to translate this address ?

I'm trying to convert the code for the OLED 128 from spin to C.

the function Fxn takes only 3 parameters:
- nb of arg
- command
- first adress of argument

With that it can be called with higher function with several parameter. see code below with function Drawcircle

How can it be translated correctly to C ?
this line cause me some problem :
SendData(long[ArgAddress][index++])

pri Fxn(argCount,Command,ArgAddress)|index
   SendData(Command)
   index := 0
   repeat argCount
        SendData(long[ArgAddress][index++])                  ''Main Function to communicate with various methods 
    result := ser.CharIn                                    ''associated with the uOLED display


pub DrawCircle(X,Y,R,Co)                             
{{
Description:
draws a circle with center at X,Y with radius R using the specified color
}}
_Ack := Fxn(4,Cmd_gfx_Circle,@X)

pri SendData(Data)                                      ''Send Word variable to the uOLED display 
   ser.char(Data.byte[1])                                           
   ser.char(Data.byte[0])

Comments

  • I used the Spin Converter forums.parallax.com/discussion/163635/interactive-spin-to-pasm-or-c-converter/p1
    which used the 'spin2cpp' executable to produce the following output from your code fragment:
    
    static int32_t Fxn(int32_t argCount, int32_t Command, int32_t ArgAddress)
    {
      int32_t	index, _idx__0001, _limit__0002;
      int32_t result = 0;
      SendData(Command);
      index = 0;
      for(( (_idx__0001 = 0), (_limit__0002 = argCount) ); _idx__0001 < _limit__0002; _idx__0001++)
      { 
        SendData(((int32_t *)ArgAddress)[(index++)]); // Main Function to communicate with various methods                                                                        
        result = index;                               // associated with the uOLED display
      }
      return result;
    }
    
    void SendData(int32_t Data) // Send Word variable to the uOLED display
    {
    }
    

    To simplify the above output I removed references to the 'ser' object and the content of the SendData routine in the Spin code before conversion, so you can see more clearly the answer to your specific question regarding the 'SendData' statement.

    The Spin Converter is a great tool to convert Spin code into either PASM or C, even if you just want to see how the resulting code looks for your own understanding/knowledge. Give the converter a try...
  • laurent974laurent974 Posts: 77
    edited 2017-02-18 19:01
    excellent . that part was what i was looking for:
    SendData(((int32_t *)ArgAddress)[(index++)]);
    

    i gave a try of this tool, and it's really impressive. there's still left a lot of rework after the first pass. But it helps a lot for pointer conversion.

    [EDIT]: The accurate translation in C is with VA_Arg.
    char Fxn(int argCount, int Command,...) {
      int index;
      va_list ap;
    
      va_start(ap, Command);
    
      SendData(Command);
      for (index = 0;index < argCount; index++) {
         SendData(va_arg(ap,int));
      }
      va_end(ap);
      return fdserial_rxChar(oled);                       // associated with the uOLED display
    }
    
Sign In or Register to comment.