#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef WINCE
#include <windows.h>
#endif
#if defined(WINCE) || defined(WIN32)
#include <roboard_dll.h> // use the DLL version of RoBoIO lib
// #include <roboard.h> // use the static version of RoBoIO lib
#else
#include <roboard.h>
#endif
void PWM_help(char* s) {
printf(
"Parameter \n"
"========== \n"
"%s [channel] [period] [duty] [count] \n"
" \n"
" [channel] : \n"
" 0 ~ 23 (for RB-100) \n"
" 0 ~ 15 (for RB-110) \n"
" \n"
" [period] (PWM period): \n"
" set PWM period (unit: microsecond) \n"
" period time must > duty time. \n"
" \n"
" [duty] (PWM duty): \n"
" set PWM duty (unit: microsecond) \n"
" \n"
" [count] : (PWM pulse count) \n"
" set the count of PWM pulses to output. \n"
" \n"
"(Press Enter key to continue...) \n"
, s
);
getchar();
printf(
"example \n"
"======== \n"
"%s 0 1000 150 200 \n"
" send 200 PWM pulses with period 1ms and duty 0.15ms on PWM \n"
" channel 0 (pin S1). \n"
" \n"
"%s 3 10000 1200 100 \n"
" send 100 PWM pulses with period 10ms and duty 1.2ms on PWM \n"
" channel 3 (pin S4). \n"
" \n"
, s, s
);
}
int GetChannel(char* str[]) {
unsigned int val;
val = (unsigned int) atoi(str[1]);
//if(val >= 16) // for RB-110
if(val >= 24) // for RB-100
{
printf("ERR: wrong PWM channel!!!\n\n");
PWM_help(str[0]);
exit(1);
}
return val;
}
unsigned long GetPeriod(char* str[]) {
unsigned long period;
period = (unsigned long) atol(str[2]);
if(period == 0L)
{
printf("ERR: period is zero !!!\n\n");
PWM_help(str[0]);
exit(1);
}
return period;
}
unsigned long GetDuty(char* str[], unsigned long period) {
unsigned long duty;
duty = (unsigned long) atol(str[3]);
if(duty == 0L)
{
printf("ERR: duty is zero !!!\n\n");
PWM_help(str[0]);
exit(1);
}
if(duty >= period)
{
printf("ERR: duty >= period !!!\n\n");
PWM_help(str[0]);
exit(1);
}
return duty;
}
unsigned long GetCount(char* str[]) {
unsigned long count;
count = (unsigned long) atol(str[4]);
if(count == 0L)
{
printf("ERR: count is zero !!!\n\n");
PWM_help(str[0]);
exit(1);
}
return count;
}
#ifdef WINCE
int _tmain(int argc, _TCHAR* _argv[])
{
char argvbuf[50][100] = {'\0'};
char* argv[50];
if (argc > 50) argc = 50;
for (int _argc=0; _argc<argc; _argc++)
{
argv[_argc] = &(argvbuf[_argc][0]);
wcstombs(argv[_argc], _argv[_argc], 100);
}
#else
int main(int argc, char* argv[])
{
#endif
int channel;
unsigned long usedchannel;
unsigned long period, duty, count;
usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 + RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15 +RCSERVO_USECHANNEL16 +RCSERVO_USECHANNEL17 +RCSERVO_USECHANNEL18 +RCSERVO_USECHANNEL19 +RCSERVO_USECHANNEL20 +RCSERVO_USECHANNEL21 +RCSERVO_USECHANNEL22 +RCSERVO_USECHANNEL23; // for RB-100
/*
usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 +RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15; // for RB-110
*/
// Before using any specific lib in RoBoIO, one must
// call roboio_SetRBVer() to set the version of RoBoard
// so that the hardware features of different version
// of RoBoards can be recognized correctly.
roboio_SetRBVer(RB_100);
if(argc == 1)
{
PWM_help(argv[0]);
return 0;
}
if (argc != 5)
{
printf("ERR: wrong parameter number!!!\n\n");
PWM_help(argv[0]);
return 1;
}
channel = GetChannel(argv);
period = GetPeriod(argv);
duty = GetDuty(argv, period);
count = GetCount(argv);
// Initialize PWM interface
if (rcservo_Initialize(usedchannel) == false)
{
printf("ERR: fail to initialize RCSERVO (%s)!\n", roboio_GetErrMsg());
return 1;
}
rcservo_EnterPWMMode();
rcservo_SendPWMPulses(channel, period, duty, count);
printf("Start to send %ld PWM pluses with period %ld us\n"
"and duty %ld us on PWM channel %d.\n\n", count, period, duty, channel);
while(rcservo_IsPWMCompleted(channel) == false);
printf("Send %ld PWM pulses completed.\n", count);
rcservo_Close();
return 0;
}