// pwm-09: adapted from Sarvesh Kulkarni's main.cc // // Changes: enclosed the code in extern "C", added #include "pwm.h", // changed main to pwm_test, made my_pwm static and global, and added // C interfaces to the PWM_Board public methods. // // R. Perry, September 2013 /********************************************************************** A quick-and-dirty test-control program to test the library of routines written by Sarvesh Kulkarni for Adafruit's PCA9685 based 16 channel PWM board. *********************************************************************/ #include "pca9685_pwm.h" #include using namespace std; #include "pwm.h" extern "C" { static PWM_Board my_pwm; int pwm_test() { int menu(void); int choice, ret_code; float dc = 20, delay = 0, freq, pw; unsigned int ch_num = 0, r_addr; choice = menu(); while (1) { switch (choice) { case 1: freq = my_pwm.getFreq(); if(ret_code < 0) cout << " ERROR in setting Frequency - Err Code " << ret_code << endl << endl; else // for all channels cout << " Freq reported by PreScale Register: " << freq << " Hz" << endl << endl; choice = menu(); break; case 2: cout << " Set Freq (Hz) ? --> "; // affects all channels cin >> freq; cout << " Requested Freq : " << freq << " Hz" << endl; ret_code = my_pwm.setFreq(freq); if(ret_code < 0) cout << " ERROR in setting Frequency - Err Code " << ret_code << endl << endl; else cout << " Actually set Freq: " << my_pwm.getFreq() << " Hz" << endl << endl; choice = menu(); break; case 3: cout << " Duty Cycle (%), Channel Num, Delay (%) ? --> "; cin >> dc >> ch_num >> delay; cout << " Setting duty cycle: " << dc; cout << ", Channel no: " << ch_num << ", Delay: " << delay << endl << endl; ret_code = my_pwm.setDutyCycle(dc, ch_num, delay); if(ret_code < 0) cout << " ERROR in setting Duty Cycle - Err Code " << ret_code << endl << endl; choice = menu(); break; case 4: cout << " Pulse Width (ms), Channel Num, Delay (%) ? --> "; cin >> pw >> ch_num >> delay; cout << " Setting pulse width: " << pw; cout << ", Channel no: " << ch_num << ", Delay: " << delay << endl << endl; ret_code = my_pwm.setPulseWidth(pw, ch_num, delay); if(ret_code < 0) cout << " ERROR in setting Pulse Width - Err Code " << ret_code << endl << endl; choice = menu(); break; case 5: cout << " Channel Num, Delay (%) ? --> "; cin >> ch_num >> delay; if(my_pwm.fullOn(ch_num, delay) < 0) cout << " ERROR in setting Full ON - Err Code " << ret_code << endl << endl; else // for all channels cout << " PWM channel(s) is/are Full ON: " << endl << endl; choice = menu(); break; case 6: cout << " Channel Num, Delay (%) ? --> "; cin >> ch_num >> delay; if (my_pwm.fullOff(ch_num, delay) < 0) cout << " ERROR in setting Full OFF - Err Code " << ret_code << endl << endl; else // for all channels cout << " PWM channel(s) is/are Full OFF: " << endl << endl; choice = menu(); break; case 7: cout << " Register address ? --> "; cin >> r_addr; ret_code = my_pwm.readRegister(r_addr); if(ret_code < 0) cout << " ERROR in Reading Register Contents - Err Code " << ret_code << endl << endl; else cout << " Register " << r_addr << " contains " << ret_code << endl << endl; choice = menu(); break; case 8: ret_code = my_pwm.sleep(); if(ret_code < 0) cout << " ERROR in Sleeping - Err Code " << ret_code << endl << endl; choice = menu(); break; case 9: ret_code = my_pwm.wakeUp(); if(ret_code < 0) cout << " ERROR in Waking Up - Err Code " << ret_code << endl << endl; choice = menu(); break; case 10:ret_code = my_pwm.softReset(); if(ret_code < 0) cout << " ERROR in Soft Reset - Err Code " << ret_code << endl << endl; choice = menu(); break; case 11:ret_code = my_pwm.hardReset(); if(ret_code < 0) cout << " ERROR in Hard Reset - Err Code " << ret_code << endl << endl; choice = menu(); break; default: cout <<" Quitting!" << endl << endl; return 0; break; } // switch } // while return 0; } int menu(void) { int sel; cout << endl << " Select a Routine to test:" << endl; cout << " (1): Get Frequency" << endl; cout << " (2): Set Frequency" << endl; cout << " (3): Set Duty Cycle" << endl; cout << " (4): Set Pulse Width" << endl; cout << " (5): Set Full ON" << endl; cout << " (6): Set Full OFF" << endl; cout << " (7): Get Register Contents" << endl; cout << " (8): Sleep" << endl; cout << " (9): Wake Up" << endl; cout << " (10): Soft Reset" << endl; cout << " (11): Hard Reset" << endl; cout << " (12): Exit --> "; cin >> sel; cout << endl; return sel; } // C interfaces to the PWM_Board public methods // int pwm_isInitialized( void) { return my_pwm.isInitialized(); } int pwm_readRegister( unsigned int reg_addr) { return my_pwm.readRegister( reg_addr); } int pwm_getFreq( void) { return my_pwm.getFreq(); } int pwm_setFreq( float freq) { return my_pwm.setFreq( freq); } int pwm_setDutyCycle( float duty_cycle, unsigned int channel_num, float delay ) { return my_pwm.setDutyCycle( duty_cycle, channel_num, delay); } int pwm_setPulseWidth( float pulse_width, unsigned int channel_num, float delay) { return my_pwm.setPulseWidth( pulse_width, channel_num, delay); } int pwm_fullOn( unsigned int channel_num, float delay) { return my_pwm.fullOn( channel_num, delay); } int pwm_fullOff( unsigned int channel_num, float delay) { return my_pwm.fullOff( channel_num, delay); } int pwm_sleep( void) { return my_pwm.sleep(); } int pwm_wakeUp( void) { return my_pwm.wakeUp(); } int pwm_softReset( void) { return my_pwm.softReset(); } int pwm_hardReset( void) { return my_pwm.hardReset(); } }