Beginning with the Parallax Propeller

It has been a while, a long while, but here is what I am working on tonight.

I recently picked up a Parallax Propeller.  After installing the software from the Parallax page I started to just mess around in code.  I am programming it in C.  With this being a multicore board the whole point is to use more than one core.

So before today I had never programmed anything using more than one thread.  So I had to do some research, found pthreads.h and went to work.  I also apparently forgot some basics about pointers, starting 8 threads all using the same pointer does exactly like it sounds like it does, the value is the same in all of them.  I tried setting the values of a variable and passing that in as a pointer to the creation of the thread, then change the value of the data in the memory and create another thread (and repeat a couple more times).  All I wanted to do was turn on all the lights on the board and then turn them off, what I got was all the lights turning on and the last one flickering.

After some careful thought I realized I made a rookie mistake, I used the same memory to store the  different values to pass into all the threads.  So I turned up with something that looked like this to make it better.

#include "simpletools.h"
#include <pthread.h>

typedef struct
{
  int light;
  int sleep;
} do_toggle_input;

void *do_toggle(void *argv)
{
  pthread_set_affinity_thiscog_np();
  do_toggle_input* vars = (do_toggle_input*)argv;
  printf("%d,%d\n",vars->light,vars->sleep);
  while(1)
  {
    high(vars->light);
    usleep(1000000);
    low(vars->light);
    usleep(1000000);
  }
}

void main (int argc,  char* argv[])
{
  pthread_t thr;
  do_toggle_input vars;
  void* var_ptr = &vars;
  usleep(200000);
  vars.sleep = 10000;
  vars.light = 16;
  var_ptr = &vars;
  printf("%d\n",vars.light);
  pthread_create(&thr, NULL, do_toggle, var_ptr);
  int i;
  for(i=16;i<24;i++)
  {
    vars.light = i;
    usleep(2000010);
    if(i==23)
    {
      i=15;
    }
  }
}

All this code does is turn on and off each light on the board, except if you notice the time is off, so it eventually switches over to all being on, and one being turned off and on.

One last thing to note, the struct that I use, I was using the sleep variable initially, but decided to hard code it.

Leave a comment