Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:9a49249d42b3, committed 2017-12-13
- Comitter:
- Charith Dassanayake
- Date:
- Wed Dec 13 00:06:47 2017 -0500
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPR121/MPR121.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,223 @@ +#ifndef CPP_TO_PIGPIO_I2C_HEADER +#include <CPPToPigpio.h> +#endif +#include <sstream> +#include <string> +#include <list> + +#include "MPR121.h" +Mpr121::Mpr121(CPPToPigpio::I2C *i2c, Address i2cAddress) +{ + this->i2c = i2c; + + address = i2cAddress; + + // Configure the MPR121 settings to default + this->configureSettings(); +} + + +void Mpr121::configureSettings() +{ + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Electrode filters for when data is > baseline + unsigned char gtBaseline[] = { + 0x01, //MHD_R + 0x01, //NHD_R + 0x00, //NCL_R + 0x00 //FDL_R + }; + + writeMany(MHD_R,gtBaseline,4); + + // Electrode filters for when data is < baseline + unsigned char ltBaseline[] = { + 0x01, //MHD_F + 0x01, //NHD_F + 0xFF, //NCL_F + 0x02 //FDL_F + }; + + writeMany(MHD_F,ltBaseline,4); + + // Electrode touch and release thresholds + unsigned char electrodeThresholds[] = { + E_THR_T, // Touch Threshhold + E_THR_R // Release Threshold + }; + + for(int i=0; i<12; i++){ + int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2); + } + + // Proximity Settings + unsigned char proximitySettings[] = { + 0xff, //MHD_Prox_R + 0xff, //NHD_Prox_R + 0x00, //NCL_Prox_R + 0x00, //FDL_Prox_R + 0x01, //MHD_Prox_F + 0x01, //NHD_Prox_F + 0xFF, //NCL_Prox_F + 0xff, //FDL_Prox_F + 0x00, //NHD_Prox_T + 0x00, //NCL_Prox_T + 0x00 //NFD_Prox_T + }; + writeMany(MHDPROXR,proximitySettings,11); + + unsigned char proxThresh[] = { + PROX_THR_T, // Touch Threshold + PROX_THR_R // Release Threshold + }; + writeMany(EPROXTTH,proxThresh,2); + + this->write(FIL_CFG,0x04); + + // Set the electrode config to transition to active mode + this->write(ELE_CFG,0x0c); +} + +void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){ + + if(electrode > 11) return; + + // Get the current mode + unsigned char mode = this->read(ELE_CFG); + + // Put the MPR into setup mode + this->write(ELE_CFG,0x00); + + // Write the new threshold + this->write((ELE0_T+(electrode*2)), touch); + this->write((ELE0_T+(electrode*2)+1), release); + + //Restore the operating mode + this->write(ELE_CFG, mode); +} + + +unsigned char Mpr121::read(int key){ + + unsigned int data[2]; + char out[2]={0}; + char cmd[] = {CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Addr, address, CPPToPigpio::I2C::Write, + 0x01, key, CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Read,0x02, CPPToPigpio::I2C::Stop, + CPPToPigpio::I2C::End}; + int ack = i2c->bang(cmd, 11, out, 2, false); + //printf("ack_read: %d", ack); + //Start the command + /*i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to read + int ack2 = i2c->write(key); + + // Re-start for read of data + i2c->start(); + + // Re-send the target address in read mode + int ack3 = i2c->write(address+1); + + // Read in the result + data[0] = i2c->read(0); + + // Reset the bus + i2c->stop();*/ + data[0] = out[0]; + return (unsigned char)data[0]; +} + + +int Mpr121::write(int key, unsigned char value){ + + char out[1]={0}; + char keyc = static_cast<char>(key); + char valc = static_cast<char>(value); + char cmd[] = {CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Addr, address, CPPToPigpio::I2C::Write, + 0x02, keyc, valc, CPPToPigpio::I2C::Stop, CPPToPigpio::I2C::End}; + int ack = i2c->bang(cmd, 9, out, 1, false); + //printf("ack_write: %d", ack); + //Start the command + /*i2c->start(); + + // Address the target (Write mode) + int ack1= i2c->write(address); + + // Set the register key to write + int ack2 = i2c->write(key); + + // Read in the result + int ack3 = i2c->write(value); + + // Reset the bus + i2c->stop();*/ + + return ack-1; +} + + +int Mpr121::writeMany(int start, unsigned char* dataSet, int length){ + //Start the command + /*i2c->start(); + + // Address the target (Write mode) + int ack= i2c->write(address); + if(ack!=1){ + return -1; + } + + // Set the register key to write + ack = i2c->write(start); + if(ack!=1){ + return -1; + } + + // Write the date set + int count = 0; + while(ack==1 && (count < length)){ + ack = i2c->write(dataSet[count]); + count++; + } + // Stop the cmd + i2c->stop();*/ + + char datasetb[length+1] = {0}; + datasetb[0] = start; + for (int i=0; i<length; i++){ + datasetb[i+1]=static_cast<char>(dataSet[i]); + //printf("a: %x, b :%x\n", dataSet[i], datasetb[i+1]); + } + //printf("here\n"); + int count = i2c->write(address, datasetb, length+1, false); + + + + return count; +} + + +bool Mpr121::getProximityMode(){ + if(this->read(ELE_CFG) > 0x0c) + return true; + else + return false; +} + +void Mpr121::setProximityMode(bool mode){ + this->write(ELE_CFG,0x00); + if(mode){ + this->write(ELE_CFG,0x30); //Sense proximity from ALL pads + } else { + this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active. + } +} + + +int Mpr121::readTouchData(){ + return this->read(0x00); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPR121/MPR121.h Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,134 @@ +#ifndef MPR121_H +#define MPR121_H + +class Mpr121 +{ + +public: + // i2c Addresses, bit-shifted + enum Address { ADD_VSS = 0x5a,// ADD->VSS = 0x5a <-wiring on Sparkfun board + ADD_VDD = 0x5b,// ADD->VDD = 0x5b + ADD_SCL = 0x5c,// ADD->SDA = 0x5c + ADD_SDA = 0x5d // ADD->SCL = 0x5d + }; + /*enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board + ADD_VDD = 0xb6,// ADD->VDD = 0x5b + ADD_SCL = 0xb8,// ADD->SDA = 0x5c + ADD_SDA = 0xba // ADD->SCL = 0x5d + };*/ + + // Real initialiser, takes the i2c address of the device. + Mpr121(CPPToPigpio::I2C *i2c, Address i2cAddress); + + bool getProximityMode(); + + void setProximityMode(bool mode); + + int readTouchData(); + + unsigned char read(int key); + + int write(int address, unsigned char value); + int writeMany(int start, unsigned char* dataSet, int length); + + void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold); + +protected: + // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes. + void configureSettings(); + +private: + // The I2C bus instance. + CPPToPigpio::I2C *i2c; + + // i2c address of this mpr121 + Address address; +}; + + +// MPR121 Register Defines +#define MHD_R 0x2B +#define NHD_R 0x2C +#define NCL_R 0x2D +#define FDL_R 0x2E +#define MHD_F 0x2F +#define NHD_F 0x30 +#define NCL_F 0x31 +#define FDL_F 0x32 +#define NHDT 0x33 +#define NCLT 0x34 +#define FDLT 0x35 +// Proximity sensing controls +#define MHDPROXR 0x36 +#define NHDPROXR 0x37 +#define NCLPROXR 0x38 +#define FDLPROXR 0x39 +#define MHDPROXF 0x3A +#define NHDPROXF 0x3B +#define NCLPROXF 0x3C +#define FDLPROXF 0x3D +#define NHDPROXT 0x3E +#define NCLPROXT 0x3F +#define FDLPROXT 0x40 +// Electrode Touch/Release thresholds +#define ELE0_T 0x41 +#define ELE0_R 0x42 +#define ELE1_T 0x43 +#define ELE1_R 0x44 +#define ELE2_T 0x45 +#define ELE2_R 0x46 +#define ELE3_T 0x47 +#define ELE3_R 0x48 +#define ELE4_T 0x49 +#define ELE4_R 0x4A +#define ELE5_T 0x4B +#define ELE5_R 0x4C +#define ELE6_T 0x4D +#define ELE6_R 0x4E +#define ELE7_T 0x4F +#define ELE7_R 0x50 +#define ELE8_T 0x51 +#define ELE8_R 0x52 +#define ELE9_T 0x53 +#define ELE9_R 0x54 +#define ELE10_T 0x55 +#define ELE10_R 0x56 +#define ELE11_T 0x57 +#define ELE11_R 0x58 +// Proximity Touch/Release thresholds +#define EPROXTTH 0x59 +#define EPROXRTH 0x5A +// Debounce configuration +#define DEB_CFG 0x5B +// AFE- Analogue Front End configuration +#define AFE_CFG 0x5C +// Filter configuration +#define FIL_CFG 0x5D +// Electrode configuration - transistions to "active mode" +#define ELE_CFG 0x5E + +#define GPIO_CTRL0 0x73 +#define GPIO_CTRL1 0x74 +#define GPIO_DATA 0x75 +#define GPIO_DIR 0x76 +#define GPIO_EN 0x77 +#define GPIO_SET 0x78 +#define GPIO_CLEAR 0x79 +#define GPIO_TOGGLE 0x7A +// Auto configration registers +#define AUTO_CFG_0 0x7B +#define AUTO_CFG_U 0x7D +#define AUTO_CFG_L 0x7E +#define AUTO_CFG_T 0x7F + +// Threshold defaults +// Electrode touch threshold +#define E_THR_T 0x0F +// Electrode release threshold +#define E_THR_R 0x0A +// Prox touch threshold +#define PROX_THR_T 0x02 +// Prox release threshold +#define PROX_THR_R 0x02 + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPR121/testi2cMPR.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,97 @@ +#include <iostream> +#include <CPPToPigpio.h> +#include "MPR121.cc" +//using namespace CPPToPigpio; + +CPPToPigpio::I2C myi2c(p2, p3); +Mpr121 mpr121(&myi2c, Mpr121::ADD_VSS); +void fallInterrupt(int gpio, int level, uint32_t tick) { + int key_code=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + + for (int i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code=i+1; + } + + if (key_code) printf("Key Pressed: %d\n", key_code-1); + + +} + +int main() +{ + + //gpioInitialise(); + + printf("\nHello from the mbed & mpr121\n\r"); + + unsigned char dataArray[2]; + int key; + int count = 0; + + printf("Test 1: read a value: \r\n"); + dataArray[0] = mpr121.read(AFE_CFG); + printf("Read value=%x\r\n\n",dataArray[0]); + + printf("Test 2: read a value: \r\n"); + dataArray[0] = mpr121.read(0x5d); + printf("Read value=%x\r\n\n",dataArray[0]); + + printf("Test 3: write & read a value: \r\n"); + mpr121.read(ELE0_T); + mpr121.write(ELE0_T,0x22); + dataArray[0] = mpr121.read(ELE0_T); + printf("Read value=%x\r\n\n",dataArray[0]); + + printf("Test 4: Write many values: \r\n"); + unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; + mpr121.writeMany(0x42,data,7); + + // Now read them back .. + key = 0x42; + count = 0; + while (count < 7) { + char result = mpr121.read(key); + key++; + count++; + printf("Read value: '%x'=%x\n\r",key,result); + } + + printf("Test 5: Read Electrodes:\r\n"); + key = ELE0_T; + count = 0; + while (count < 24) { + char result = mpr121.read(key); + printf("Read key:%x value:%x\n\r",key,result); + key++; + count++; + } + printf("--------- \r\n\n"); + + //mpr121.setProximityMode(true); + printf("reading...\r\n"); + printf("ELE_CFG=%x\r\n", mpr121.read(ELE_CFG)); + + CPPToPigpio::DigitalIn intr(p17); + intr.mode(PullUp); + gpioSetISRFunc(p17, FALLING_EDGE, 0, fallInterrupt); + + + while (1) { + time_sleep(5); + printf(".\r\n"); + + } + + + gpioTerminate(); +} + + + + + + + +
Binary file Test 1s/a.out has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 1s/test_1_CPPToPigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,36 @@ +#include <iostream> +#include <CPPToPigpio.h> + +#define I2C_SDA p2 +#define I2C_SCL p3 + +using namespace std; + +int main() { + + CPPToPigpio::I2C acc(I2C_SDA, I2C_SCL); + + CPPToPigpio::BusOut LEDs(p21, p20, p16, p12); + + char initWrite[] = {0x20, char(0xC0)}; + char dataGet[] = {0x28}; + char dataFromStuff[2]; + + acc.write(0x35 << (1), initWrite, 2, false); + + time_sleep(1); + + while(1) { + acc.write(0x35 << (1), dataGet, 1, false); + acc.read(0x35 << (1) | 1, dataFromStuff, 2, false); + + int superAcc = dataFromStuff[0] + (dataFromStuff[1] <<(8)); + + cout << "acc gotten is " << superAcc << endl; + + int writeEm = superAcc >>(12); + + LEDs = writeEm; + time_sleep(1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 1s/test_1_explain.txt Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,5 @@ +this tests gets a value from the accelerometer and "plots" it on a 4-bit busOut (it just shifts the 16-bit recieved value by 12). Also outputs the value to the command line + +circuit setup: +connect acclerometer SDA to pin 2, SCL to pin 3, and power it with 3.3v. +connect a pushbutton between 3.3v and pins 21, 20, 16, and 12. A pull down resistor is optional. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 1s/test_1_pigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,75 @@ +#include <pigpio.h> +#include <iostream> +#include <signal.h> +#include <sys/types.h> +#include <unistd.h> + +static void err_handler (int sig) { + gpioTerminate(); + signal(SIGINT, SIG_DFL); + kill(getppid(), SIGINT); + kill(getpid(), SIGINT); + exit(0); +} + +static void exit_handler () { + gpioTerminate(); +} + + + +using namespace std; + +int main() { + + if (gpioInitialise() < 0) return 1; + signal(SIGQUIT, err_handler); + signal(SIGINT, err_handler); + signal(SIGTERM, err_handler); + signal(SIGABRT, err_handler); + atexit(exit_handler); + + int bitLower = 21; + int bit2 = 20; + int bit3 = 16; + int bit4 = 12; + + gpioSetMode(bitLower, PI_OUTPUT); + gpioSetMode(bit2, PI_OUTPUT); + gpioSetMode(bit3, PI_OUTPUT); + gpioSetMode(bit4, PI_OUTPUT); + + // acc name is LSM9DS1 + + // write 11000000 to 0x20 + // read 0x28 and 0x29. lsb is in 28 + // its address is 0xD4. if that doesn't work try 0xD6, or d53 or d54 + + // make sure the right bus is being used + int accHandle = i2cOpen(1, 0x35, 0); + + if(accHandle < 0 ) { + cout << "i2c fail" << endl; + } + + i2cWriteByteData(accHandle, 0x20, 0xC0); + + // not sure if seconds or milliseconds + time_sleep(1); + + while(1) { + int superAcc = i2cReadWordData(accHandle, 0x28); + + cout << "acc gotten is " << superAcc << endl; + + int writeEm = superAcc >>(12); + + gpioWrite(bit4, (writeEm & 0x08) == 0x08); + gpioWrite(bit3, (writeEm & 0x04) == 0x04); + gpioWrite(bit2, (writeEm & 0x02) == 0x02); + gpioWrite(bitLower, (writeEm & 0x01) == 0x01); + time_sleep(1); + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 2s/BusIn_PWM_with_CPPToPigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,29 @@ +#include <iostream> +#include <CPPToPigpio.h> + + + +float starting_f = 8000.0f; + +CPPToPigpio::BusIn switchBus(5, 6, 13, 19); + +float divisions[] = {1.0f, 2.0f, 4.0f, 5.0f, 8.0f, 10.0f, 16.0f, 20.0f, 25.0f, 32.0f, 40.0f, 50.0f, 80.0f, 100.0f, 160.0f, 200.0f}; + +CPPToPigpio::PwmOut outThing(p12); + +int main(){ + switchBus.mode(PullUp); + outThing.period(1.0f/starting_f); + outThing.write(0.5f); + int total = switchBus; + while(total!=0) { + + + outThing.period(divisions[total]/starting_f); + + std::cout << "frequency should be " << starting_f/divisions[total]<<std::endl; + + time_sleep(1); + total = switchBus; + } +}
Binary file Test 2s/a.out has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 2s/test_2_CPPToPigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,21 @@ +#include <iostream> +#include <CPPToPigpio.h> + +using namespace std; +float starting_f = 8000.0f; +CPPToPigpio::BusIn switchBus(p25, p24, p23, p18); +float divisions[] = {1.0f, 2.0f, 4.0f, 5.0f, 8.0f, 10.0f, 16.0f, + 20.0f, 25.0f, 32.0f, 40.0f, 50.0f, 80.0f, 100.0f, 160.0f, 200.0f}; +CPPToPigpio::PwmOut outThing(p21); + +int main(){ + switchBus.mode(PullDown); + outThing.period(1.0f/starting_f); + outThing = 0.5f; + while(1) { + int total = switchBus; + outThing.period(divisions[total]/starting_f); + cout << "frequency should be " << starting_f/divisions[total]; + time_sleep(1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 2s/test_2_explain.txt Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,6 @@ +this test gets a value from a busIn and changes am output PWM frequency based on the busIn value. outputs frequency to command line as well. + +circuit setup: +connect a pushbutton between 3.3v and pins 25, 24, 23, and 18. A pull down resistor is optional. +connect pin 21 to oscilloscope or to audio amplifier to observe effect +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 2s/test_2_pigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,56 @@ +#include <iostream> +#include <pigpio.h> +#include <signal.h> +#include <sys/types.h> +#include <unistd.h> + +static void err_handler (int sig) { + gpioTerminate(); + signal(SIGINT, SIG_DFL); + kill(getppid(), SIGINT); + kill(getpid(), SIGINT); + exit(0); +} + +static void exit_handler () { + gpioTerminate(); +} + +int starting_f = 8000; +int bitHigher = 25; +int bit2 = 24; +int bit3 = 23; +int bit4 = 18; +int pwmPin = 21; +int divisions[] = {1, 2, 4, 5, 8, 10, 16, 20, + 25, 32, 40, 50, 80, 100, 160, 200}; +using namespace std; + +int main() { + if (gpioInitialise() < 0) return 1; + signal(SIGQUIT, err_handler); + signal(SIGINT, err_handler); + signal(SIGTERM, err_handler); + signal(SIGABRT, err_handler); + atexit(exit_handler); + gpioSetMode(bitHigher, PI_INPUT); + gpioSetMode(bit2, PI_INPUT); + gpioSetMode(bit3, PI_INPUT); + gpioSetMode(bit4, PI_INPUT); + gpioSetPullUpDown(bitHigher, PI_PUD_DOWN); + gpioSetPullUpDown(bit2, PI_PUD_DOWN); + gpioSetPullUpDown(bit3, PI_PUD_DOWN); + gpioSetPullUpDown(bit4, PI_PUD_DOWN); + gpioSetMode(pwmPin, PI_OUTPUT); + gpioPWM(pwmPin, 128); + while(1) { + int highest = gpioRead(bitHigher); + int b2 = gpioRead(bit2); + int b3 = gpioRead(bit3); + int b4 = gpioRead(bit4); + int total = highest*8 + b2 * 4 + b3 * 2 + b4; + gpioSetPWMfrequency(pwmPin, starting_f/divisions[total]); + cout << "frequency should be " << starting_f/divisions[total]; + time_sleep(1); + } +}
Binary file Test 3s/a.out has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 3s/test_3_CPPToPigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,30 @@ +#include <iostream> +#include <CPPToPigpio.h> + +using namespace std; + +CPPToPigpio::DigitalIn switchPin(p18); +CPPToPigpio::PwmOut servoPin(p20); + +int main() { + switchPin.mode(PullDown); + + // 20 Hz + servoPin.period(0.05); + + servoPin = 0.03; + + while(1) { + if(switchPin) { + // servo on + servoPin = 0.04; + cout << "servo pin high" << endl; + } + else{ + servoPin = 0.02; + cout << "servo pin low" << endl; + } + time_sleep(1); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 3s/test_3_explain.txt Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,5 @@ +this test takes in a digital input and outputs a PWM that will control a servo. The servo should go to it's "maximum" position when the button is pressed and to its "minimum" position when it is released + +circuit setup: +connect a pushbutton between pin 18 and 3.3v. A pull down resistor is optional. +connect a hobby servo's power supply wires to 5 V and ground, and the signal wire to pin 20.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 3s/test_3_pigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,60 @@ +#include <pigpio.h> +#include <signal.h> +#include <sys/types.h> +#include <unistd.h> +#include <iostream> + +static void err_handler (int sig) { + gpioTerminate(); + signal(SIGINT, SIG_DFL); + kill(getppid(), SIGINT); + kill(getpid(), SIGINT); + exit(0); +} + +static void exit_handler () { + gpioTerminate(); +} + + + +int pwmPin = 20; +int switchPin = 18; + +using namespace std; + +int main() { + if (gpioInitialise() < 0) return 1; + signal(SIGQUIT, err_handler); + signal(SIGINT, err_handler); + signal(SIGTERM, err_handler); + signal(SIGABRT, err_handler); + atexit(exit_handler); + + gpioSetMode(switchPin, PI_INPUT); + gpioSetPullUpDown(switchPin, PI_PUD_DOWN); + + + gpioSetMode(pwmPin, PI_OUTPUT); + + gpioPWM(pwmPin, 7); + + gpioSetPWMfrequency(pwmPin, 20); + + while(1) { + int switchOn = gpioRead(switchPin); + + if(switchOn) { + // a servo fully on + gpioPWM(pwmPin, 10); + } + else { + // a servo fully off + gpioPWM(pwmPin, 5); + } + + time_sleep(1); + } + + +}
Binary file Test 4s/a.out has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 4s/test_4_CPPToPigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,12 @@ +#include <CPPToPigpio.h> + +int main() { + CPPToPigpio::DigitalIn input(p18); + CPPToPigpio::DigitalOut LED(p20); + input.mode(PullDown); + time_sleep(1); + while(1) { + LED = input; + time_sleep(0.1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 4s/test_4_explain.txt Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,5 @@ +this test is simple; press the button to light up LED program. + +circuit set-up: +connect a pushbutton between pin 18 and 3.3v. A pull down resistor is optional. +connect an LED and resistor in series between pin 20 and ground. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test 4s/test_4_pigpio.cc Wed Dec 13 00:06:47 2017 -0500 @@ -0,0 +1,39 @@ +#include <iostream> +#include <pigpio.h> +#include <signal.h> +#include <sys/types.h> +#include <unistd.h> + +static void err_handler (int sig) { + gpioTerminate(); + signal(SIGINT, SIG_DFL); + kill(getppid(), SIGINT); + kill(getpid(), SIGINT); + exit(0); +} + +static void exit_handler () { + gpioTerminate(); +} + +int inputPin = 18; +int outputPin = 20; + +int main () { + if (gpioInitialise() < 0) return 1; + signal(SIGQUIT, err_handler); + signal(SIGINT, err_handler); + signal(SIGTERM, err_handler); + signal(SIGABRT, err_handler); + atexit(exit_handler); + gpioSetMode(inputPin, PI_INPUT); + gpioSetMode(outputPin, PI_OUTPUT); + gpioSetPullUpDown(inputPin, PI_PUD_DOWN); + while(1) { + int inputVal = gpioRead(inputPin); + gpioWrite(outputPin, inputVal); + time_sleep(0.1); + } +} + +