Listing 2—The gait thread implementation is the same for both OS environments. Here, I use macros to map generic constructs like GetMutex() and Sleep() to OS-specific calls. Using C macros is one common technique for making C source code portable between different OS environments.

#include "global.h"
#include "servo.h"
#include "gait.h"
int Command;				/* current command */
int LastCommand;
int cmdmutex;
int MaxSteps[nCMD] = {4,4,4,4};	/* number of steps in pattern */
/* gait patterns */
int Pattern[nCMD][nSTEP][nCHAN]=
{ {{2,4,2,4,2,4,2,4},{4,2,4,2,4,2,4,2},{2,4,2,4,2,4,2,4},{4,2,4,2,4,2,4,2}},
  {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}},
  {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}},
  {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}} };
/* generate leg actuations depending on current command */
GaitThread() {
  int CurrStep;
  int i;
  GetMutex(cmdmutex);
  Command = CMD_STOP;
  LastCommand = Command;
  ReleaseMutex(cmdmutex);
  CurrStep = 0;
  while(1){
    Sleep(STEPTIME);
    GetMutex(cmdmutex);		/* check whether command mode changed */
    if(LastCommand != Command) CurrStep = 0;
    LastCommand = Command;
    ReleaseMutex(cmdmutex);
    for(i=0;i<nCHAN;i++)
      SetTime[i] = Pattern[LastCommand][CurrStep][i]-1;    /* set servo channels */
    CurrStep = (CurrStep + 1) % MaxSteps[LastCommand]; } } /* next step */