Shop OBEX P1 Docs P2 Docs Learn Events
simpletools code to C conversion, can't figure it out. — Parallax Forums

simpletools code to C conversion, can't figure it out.

Beavis3215Beavis3215 Posts: 229
edited 2018-07-15 22:39 in Propeller 1
I have some code examples for using DIRA, OUTA and waitpeq, but nothing as simple as the C equivalent of on_sensor = input(19) or if (on_sensor == 1).

Here is what I mean

void button_time();

static volatile int delay;          // delay is global

int main()                          // Main function
{
  
 cog_run(button_time,128);          // Push button control runs in other cog
 set_directions(17,16,11);
 int on_sensor;
 int off_sensor;
 
  while(1)
  {
    on_sensor = input(19);
    off_sensor = input(18);
    if (on_sensor == 1)
    {
      set_output(17,1);             // Turn magnet on
    }
    
    if (off_sensor == 1)
    {
      pause(delay);                 // Delay is assigned in button_time
      set_output(17,0);             // Turn magnet off
      set_output(16,1);             // Timing light on
      pause(5);
      set_output(16,0);             // Timing light off
    }            
  }  
}

This Works


This is what Im trying to do

void button_time();

static volatile int delay;          // delay is global

int main()                          // Main function
{
  
 cog_run(button_time,128);          // Push button control runs in other cog
 set_directions(17,16,11);
 int on_sensor = 1 << 19;
 int off_sensor = 1 << 18;
  DIRA &= ~(1 << 19);
  DIRA &= ~(1 << 18);
  while(1)
  {
   on_sensor = input(19);
   off_sensor= input(18);
    if (on_sensor == on_sensor)
    {
      set_output(17,1);             // Turn magnet on
    }
    
    if (off_sensor == off_sensor)
    {
      pause(delay);                 // Delay is assigned in button_time
      set_output(17,0);             // Turn magnet off
      set_output(16,1);             // Timing light on
      pause(5);
      set_output(16,0);             // Timing light off
    }            
  }  
}

Just tying to understand the inputs for now. It seems like I should be pretty close.
Is there somewhere in help that talks about this?

Comments

  • JonnyMacJonnyMac Posts: 8,918
    edited 2018-07-15 23:15
    I'm not a Propeller C programmer, so I hope I don't screw you up. Perhaps you can create a general purpose function like this to check the state of any pin. If I've done this correctly, it should return 0 or 1.

    (edited to correct syntax)
    int get_pin(int pinNum) {
      return (INA >> pinNum) & 1;
    }
    
    This doesn't change the direction of the IO pin which can be useful when using it to monitor the output state controlled by other processes.
  • Here's the code for the input function in the simple library
    int input(int pin)                            // input function definition
    {
      int mask = 1 << pin;                        // Set up mask
      DIRA &= ~mask;                              // AND DIRA with NOT mask
      return (INA & mask) >> pin;                 // Return input state
    }
    
    It returns the value of the pin. Note that it also forces the pin to be input only.
  • Thanks Dave

    Awesome as always
  • 
    void button_time()
    {
      //set_directions(13,8,0b111111);
      int lights = 0b111111 << 14;       //Timing delay in binary on LED's
      DIRA |= (lights);
      
      int t = 20;
      //int advance;
      //int retard;
      int advance = 1 << 15;
      int retard = 1 << 14;
      
      while(1)
      {
        
         if ((INA & retard) == retard)   // If retard button high
         {
           t = t + 1;
           
           if (t > 64)                   // Don't allow t to exceed 64
           {
             t = 64;
           }
                     
         }
         
         if ((INA & advance) == advance) // If advance button high
         {
           t = t - 1;
           
           if (t < 1)                    // Keep t 0 or larger
           {
             t = 1;
           }
                   
         }
         
         delay = t;                      // Pass delay to main function
         
            //set_outputs(13,8,t);         // Put t value on output lights
            OUTA |= (t << 14);
            pause(300);               
        
      
      }
    }      
    

    The simpletools code is remarked out as to be non operational. I am attempting to replace the simpletools code with C code, but I am making a mistake. I don't quite get it, any Ideas?

    Dave, I also tried to find in simple libraries the C code conversions of simple code, but I can't find it. Can you get me the path through Simple IDE help to get to them?
  • RaymanRayman Posts: 13,855
    It returns the value of the pin. Note that it also forces the pin to be input only.

    But any other cog can make the pin an output, right?
  • Dave HeinDave Hein Posts: 6,347
    edited 2018-07-22 23:47
    @Beavis3215, to set bits 8 to 13 to the 6-bit value of t you should do
    OUTA = (OUTA & ~(0b111111 << 8 )) | (t << 8 );
    
    You have to first zero out the 6 bits, and then or in the new value.
    To set the direction of the 6 bits to outputs you should do
    DIRA |= 0b111111 << 8;
    
    You can find the location of the library code by clicking on Tools and then Properties. Or you can just hit the F6 key. Look for "Library Folder" to find the location. The source for the pin I/O routines is located in the Utility/libsimpletools/source folder.

    @Rayman, you are correct. Any other cog can make the pin an output.
  • Thanks Dave, I got it to work. This one has been educational for me.
Sign In or Register to comment.