Shop OBEX P1 Docs P2 Docs Learn Events
Translating the Arduino Sketches into a SPIN function — Parallax Forums

Translating the Arduino Sketches into a SPIN function

Buck RogersBuck Rogers Posts: 2,161
edited 2020-01-11 07:02 in Propeller 1
Hello!
The camera boards I bought from Micro Center a while ago, the Mini Spy Camera have a learning example set in those sketches for their simpler versions of the Arduino. For example:
int trig = 0;
int led = 1;

void setup() {                
  // initialize the digital pins as output.
  pinMode(led, OUTPUT);
  pinMode(trig, OUTPUT);         

  digitalWrite(led, HIGH);  
  digitalWrite(trig, HIGH); 
}

// Hold HIGH and trigger quick (<250ms) LOW to take a photo. Holding LOW and trigger HIGH starts/stops video recording

void loop() {
  digitalWrite(trig, LOW);   
  digitalWrite(led, HIGH);
  
  delay(50);               

  digitalWrite(trig, HIGH);    
  digitalWrite(led, LOW);   
  
  delay(5000);               
  
}

They believe that would run on one of their full featured ones. I want to do one better and see what's involved in translating it into SPIN. And as for how, I'm thinking it can't be too difficult as the code for the NeoPixels was successfully translated into SPIN, and it runs faster and better on the different Prop ones I've tried it out on versus the Arduino design I first did try theirs on. (Incidentally this is the thing in the Altoids tin.)

Comments

  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2020-01-11 09:51
    The Propeller Tool, makes rewriting Arduino programs
    fun.

    On larger programs you need to decide how to handle
    C/C++ pointers, structs, classes, enumerations, files
    (.ino, .cpp, .h) etc.


    Bill M.


  • @"Dave Hein" 's cspin tool for translating C to SPIN may be able to help as well. I'm not sure how much C++ - specific code it handles, and the produced spin code will need some post-processing by hand afterwards, for sure, but it takes care of a lot of what would be creative "Find and Replace"-ing in PropellerIDE or text editor. I've used it before and for larger source it takes a lot of manual labor off your hands.

    Cheers,
    Jesse
  • cspin only converts C, and it doesn't handle C++ at all. There are also a number of features in C that it doesn't handle. See the readme file for the details. I have been able to convert some fairly complex C code to spin using cspin, but you probably need to edit your source program some to get it to work. There is a SPIN OBJECT directive that is used to equate functions with an object, which guides cspin to generate the appropriate spin object references. If you'd like, you could post some of the code that you want to convert, and I could show what it takes for cspin to handle it.
  • Dave Hein wrote: »
    cspin only converts C, and it doesn't handle C++ at all. There are also a number of features in C that it doesn't handle. See the readme file for the details. I have been able to convert some fairly complex C code to spin using cspin, but you probably need to edit your source program some to get it to work. There is a SPIN OBJECT directive that is used to equate functions with an object, which guides cspin to generate the appropriate spin object references. If you'd like, you could post some of the code that you want to convert, and I could show what it takes for cspin to handle it.

    Hello!
    Dave? That's the code above that I'm interesting in converting. Here I'll snag it and post it again:
    nt trig = 0;
    int led = 1;
    
    void setup() {                
      // initialize the digital pins as output.
      pinMode(led, OUTPUT);
      pinMode(trig, OUTPUT);         
    
      digitalWrite(led, HIGH);  
      digitalWrite(trig, HIGH); 
    }
    
    // Hold HIGH and trigger quick (<250ms) LOW to take a photo. Holding LOW and trigger HIGH starts/stops video recording
    
    void loop() {
      digitalWrite(trig, LOW);   
      digitalWrite(led, HIGH);
      
      delay(50);               
    
      digitalWrite(trig, HIGH);    
      digitalWrite(led, LOW);   
      
      delay(5000);               
      
    }
    
    And that's the code I need help in translating. In fact that's one of two, code blocks, published for the purpose. The other in a different Adafruit Learn paging describes a related function and also similar methods for wiring the camera board.
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-01-11 16:57
    Sorry, I thought there was some C++ code that needed to be translated. Running your code through cspin generates the following:
    '*********************************************
    '* Spin code generated by cspin version 0.84 *
    '*********************************************
    
    
    DAT
      trig long 0
      led long 1
    
    PUB setup
      ' initialize the digital pins as output.
      pinMode(led, OUTPUT)
      pinMode(trig, OUTPUT)
      digitalWrite(led, HIGH)
      digitalWrite(trig, HIGH)
    
    ' Hold HIGH and trigger quick (<250ms) LOW to take a photo. Holding LOW and trigger HIGH starts/stops video recording
    PUB loop
      digitalWrite(trig, LOW)
      digitalWrite(led, HIGH)
      delay(50)
      digitalWrite(trig, HIGH)
      digitalWrite(led, LOW)
      delay(5000)
    
    Of course, you will also need the code for the pinMode, digitalWrite and delay routines. Those should be fairly easy to write.
  • And I did figure one of you did write an amazing translator for such work. It was those routines that I'm currently lost with, meaning I'm not sure how to write them.
  • Sorry to mislead; I only mentioned C++ because I thought Arduino was C++. Nothing to see here ;)
  • avsa242 wrote: »
    Sorry to mislead; I only mentioned C++ because I thought Arduino was C++. Nothing to see here ;)

    You are not. What they use is a language called Processing. It's big problem is that as the program gets bigger it starts to look like a bad case of noodles all over the place.
  • AribaAriba Posts: 2,682
    ...
    What they use is a language called Processing. It's big problem is that as the program gets bigger it starts to look like a bad case of noodles all over the place.

    That's the same problem in all languages, if a beginner writes all the commands in a row, without giving it a structure with loops and subroutines.
    'Processing' and 'Wiring', which Arduino uses, is just C/C++, with a special library, and some simplified rules.

    The functions used in your example are really the most basic ones, that every beginner learns to blink a LED.

    Translated to Spin it can look like that:
    CON
      _clkmode  = xtal1 + pll16x
      _xinfreq  = 5_000_000
    
      trig = 0     'pins
      led  = 1
    
      OUTPUT = 1
      LOW    = 0
      HIGH   = 1
    
    PUB setup
      dira[led]  := OUTPUT
      dira[trig] := OUTPUT
    
      outa[trig] := HIGH
      outa[led]  := HIGH
    
      repeat
        loop
    
    PUB loop
      outa[trig] := LOW
      outa[led]  := HIGH
    
      delay(50)
    
      outa[trig] := HIGH
      outa[led]  := LOW
    
      delay(5000)
    
    
    PRI delay(ms)
      waitcnt(clkfreq/1000 * ms + cnt)
    
Sign In or Register to comment.