From a2ac6a49885c355ad40216d69ffdec369a788613 Mon Sep 17 00:00:00 2001 From: Suhayl <838119509@qq.com> Date: Wed, 11 Mar 2020 16:06:06 +0800 Subject: [PATCH] Update ADC module. update adc module ,add ads7830. --- Code/C_Code/07.1.1_ADC/ADC.c | 35 ------ Code/C_Code/07.1.1_ADC/ADC.cpp | 39 +++++++ Code/C_Code/08.1.1_Softlight/Softlight.c | 39 ------- Code/C_Code/08.1.1_Softlight/Softlight.cpp | 44 ++++++++ ...orfulSoftlight.c => ColorfulSoftlight.cpp} | 46 ++++---- Code/C_Code/10.1.1_Nightlamp/Nightlamp.c | 39 ------- Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp | 44 ++++++++ Code/C_Code/11.1.1_Thermometer/Thermometer.c | 39 ------- .../C_Code/11.1.1_Thermometer/Thermometer.cpp | 43 ++++++++ Code/C_Code/12.1.1_Joystick/Joystick.c | 41 ------- Code/C_Code/12.1.1_Joystick/Joystick.cpp | 46 ++++++++ Code/C_Code/13.1.1_Motor/Motor.c | 70 ------------ Code/C_Code/13.1.1_Motor/Motor.cpp | 76 +++++++++++++ Code/Python_Code/07.1.1_ADC/ADC.py | 43 ++++---- .../Python_Code/08.1.1_Softlight/Softlight.py | 50 ++++----- .../ColorfulSoftlight.py | 37 +++---- .../Python_Code/10.1.1_Nightlamp/Nightlamp.py | 29 ++--- .../11.1.1_Thermometer/Thermometer.py | 37 ++++--- Code/Python_Code/12.1.1_Joystick/Joystick.py | 33 +++--- Code/Python_Code/13.1.1_Motor/Motor.py | 26 ++--- Libs/C-Libs/ADCDevice/ADCDevice.cpp | 68 ++++++++++++ Libs/C-Libs/ADCDevice/ADCDevice.hpp | 37 +++++++ Libs/C-Libs/ADCDevice/build.sh | 12 ++ Libs/Python-Libs/ADCDevice-1.0.2.tar.gz | Bin 0 -> 1439 bytes .../App_01_1_1_Oscilloscope/ADCDevice.pde | 104 ++++++++++++++++++ .../App_01_1_1_Oscilloscope.pde | 26 +++-- .../Apps/App_01_1_1_Oscilloscope/PCF8591.pde | 47 -------- .../Apps/App_02_1_1_Ellipse/ADCDevice.pde | 104 ++++++++++++++++++ .../App_02_1_1_Ellipse/App_02_1_1_Ellipse.pde | 16 ++- .../Apps/App_02_1_1_Ellipse/PCF8591.pde | 47 -------- .../Apps/App_03_1_1_Pong_Game/ADCDevice.pde | 104 ++++++++++++++++++ .../App_03_1_1_Pong_Game.pde | 20 +++- .../Apps/App_03_1_1_Pong_Game/PCF8591.pde | 47 -------- .../Sketches/Sketch_06_1_1_ADC/ADCDevice.pde | 104 ++++++++++++++++++ .../Sketch_06_1_1_ADC.pde} | 33 +++--- .../Sketch_06_1_1_PCF8591/PCF8591.pde | 47 -------- .../Sketch_07_1_1_SoftLight/ADCDevice.pde | 104 ++++++++++++++++++ .../Sketch_07_1_1_SoftLight/PCF8591.pde | 47 -------- .../Sketch_07_1_1_SoftLight.pde | 24 ++-- .../Sketch_08_1_1_Thermometer/ADCDevice.pde | 104 ++++++++++++++++++ .../Sketch_08_1_1_Thermometer/PCF8591.pde | 47 -------- .../Sketch_08_1_1_Thermometer.pde | 22 ++-- .../Sketch_14_1_1_Joystick/ADCDevice.pde | 104 ++++++++++++++++++ .../Sketch_14_1_1_Joystick/PCF8591.pde | 52 --------- .../Sketch_14_1_1_Joystick.pde | 26 +++-- 45 files changed, 1399 insertions(+), 803 deletions(-) delete mode 100644 Code/C_Code/07.1.1_ADC/ADC.c create mode 100644 Code/C_Code/07.1.1_ADC/ADC.cpp delete mode 100644 Code/C_Code/08.1.1_Softlight/Softlight.c create mode 100644 Code/C_Code/08.1.1_Softlight/Softlight.cpp rename Code/C_Code/09.1.1_ColorfulSoftlight/{ColorfulSoftlight.c => ColorfulSoftlight.cpp} (50%) delete mode 100644 Code/C_Code/10.1.1_Nightlamp/Nightlamp.c create mode 100644 Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp delete mode 100644 Code/C_Code/11.1.1_Thermometer/Thermometer.c create mode 100644 Code/C_Code/11.1.1_Thermometer/Thermometer.cpp delete mode 100644 Code/C_Code/12.1.1_Joystick/Joystick.c create mode 100644 Code/C_Code/12.1.1_Joystick/Joystick.cpp delete mode 100644 Code/C_Code/13.1.1_Motor/Motor.c create mode 100644 Code/C_Code/13.1.1_Motor/Motor.cpp create mode 100644 Libs/C-Libs/ADCDevice/ADCDevice.cpp create mode 100644 Libs/C-Libs/ADCDevice/ADCDevice.hpp create mode 100644 Libs/C-Libs/ADCDevice/build.sh create mode 100644 Libs/Python-Libs/ADCDevice-1.0.2.tar.gz create mode 100644 Processing/Apps/App_01_1_1_Oscilloscope/ADCDevice.pde delete mode 100644 Processing/Apps/App_01_1_1_Oscilloscope/PCF8591.pde create mode 100644 Processing/Apps/App_02_1_1_Ellipse/ADCDevice.pde delete mode 100644 Processing/Apps/App_02_1_1_Ellipse/PCF8591.pde create mode 100644 Processing/Apps/App_03_1_1_Pong_Game/ADCDevice.pde delete mode 100644 Processing/Apps/App_03_1_1_Pong_Game/PCF8591.pde create mode 100644 Processing/Sketches/Sketch_06_1_1_ADC/ADCDevice.pde rename Processing/Sketches/{Sketch_06_1_1_PCF8591/Sketch_06_1_1_PCF8591.pde => Sketch_06_1_1_ADC/Sketch_06_1_1_ADC.pde} (50%) delete mode 100644 Processing/Sketches/Sketch_06_1_1_PCF8591/PCF8591.pde create mode 100644 Processing/Sketches/Sketch_07_1_1_SoftLight/ADCDevice.pde delete mode 100644 Processing/Sketches/Sketch_07_1_1_SoftLight/PCF8591.pde create mode 100644 Processing/Sketches/Sketch_08_1_1_Thermometer/ADCDevice.pde delete mode 100644 Processing/Sketches/Sketch_08_1_1_Thermometer/PCF8591.pde create mode 100644 Processing/Sketches/Sketch_14_1_1_Joystick/ADCDevice.pde delete mode 100644 Processing/Sketches/Sketch_14_1_1_Joystick/PCF8591.pde diff --git a/Code/C_Code/07.1.1_ADC/ADC.c b/Code/C_Code/07.1.1_ADC/ADC.c deleted file mode 100644 index 33b09aa..0000000 --- a/Code/C_Code/07.1.1_ADC/ADC.c +++ /dev/null @@ -1,35 +0,0 @@ -/********************************************************************** -* Filename : ADC.c -* Description : ADC and DAC -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 (according to the wiringPi library) -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -int main(void){ - int value; - float voltage; - - printf("Program is starting ... \n"); - - wiringPiSetup(); //Initialize wiringPi. - - pcf8591Setup(pinbase,address); - - while(1){ - value = analogRead(A0); //read analog value of A0 pin - analogWrite(pinbase+0,value); - voltage = (float)value / 255.0 * 3.3; // Calculate voltage - printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage); - delay(100); - } -} diff --git a/Code/C_Code/07.1.1_ADC/ADC.cpp b/Code/C_Code/07.1.1_ADC/ADC.cpp new file mode 100644 index 0000000..5809043 --- /dev/null +++ b/Code/C_Code/07.1.1_ADC/ADC.cpp @@ -0,0 +1,39 @@ +/********************************************************************** +* Filename : ADC.cpp +* Description : Use ADC module to read the voltage value of potentiometer. +* Author : www.freenove.com +* modification: 2020/03/06 +**********************************************************************/ +#include +#include +#include + +ADCDevice *adc; // Define an ADC Device class object + +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + + while(1){ + int adcValue = adc->analogRead(0); //read analog value of A0 pin + float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage + printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage); + delay(100); + } + return 0; +} diff --git a/Code/C_Code/08.1.1_Softlight/Softlight.c b/Code/C_Code/08.1.1_Softlight/Softlight.c deleted file mode 100644 index 95ccb6c..0000000 --- a/Code/C_Code/08.1.1_Softlight/Softlight.c +++ /dev/null @@ -1,39 +0,0 @@ -/********************************************************************** -* Filename : Softlight.c -* Description : Potentiometer control LED -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -#define ledPin 0 -int main(void){ - int value; - float voltage; - - printf("Program is starting ... \n"); - - wiringPiSetup(); - - softPwmCreate(ledPin,0,100); - pcf8591Setup(pinbase,address); - - while(1){ - value = analogRead(A0); //read analog value of A0 pin - softPwmWrite(ledPin,value*100/255); - voltage = (float)value / 255.0 * 3.3; // calculate voltage - printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage); - delay(100); - } - return 0; -} diff --git a/Code/C_Code/08.1.1_Softlight/Softlight.cpp b/Code/C_Code/08.1.1_Softlight/Softlight.cpp new file mode 100644 index 0000000..1ccf431 --- /dev/null +++ b/Code/C_Code/08.1.1_Softlight/Softlight.cpp @@ -0,0 +1,44 @@ +/********************************************************************** +* Filename : Softlight.cpp +* Description : Use potentiometer to control LED +* Author : www.freenove.com +* modification: 2020/03/07 +**********************************************************************/ +#include +#include +#include +#include + +#define ledPin 0 + +ADCDevice *adc; // Define an ADC Device class object + +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + wiringPiSetup(); + softPwmCreate(ledPin,0,100); + while(1){ + int adcValue = adc->analogRead(0); //read analog value of A0 pin + softPwmWrite(ledPin,adcValue*100/255); // Mapping to PWM duty cycle + float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage + printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage); + delay(30); + } + return 0; +} diff --git a/Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.c b/Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.cpp similarity index 50% rename from Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.c rename to Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.cpp index a1f95c4..0f53bca 100644 --- a/Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.c +++ b/Code/C_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.cpp @@ -1,40 +1,46 @@ /********************************************************************** -* Filename : ColorfulSoftlight.c -* Description : Potentiometer control RGBLED +* Filename : Softlight.cpp +* Description : Use potentiometer to control LED * Author : www.freenove.com -* modification: 2019/12/27 +* modification: 2020/03/07 **********************************************************************/ #include -#include #include #include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 +#include #define ledRedPin 3 //define 3 pins for RGBLED #define ledGreenPin 2 #define ledBluePin 0 + +ADCDevice *adc; // Define an ADC Device class object + int main(void){ - int val_Red,val_Green,val_Blue; - + adc = new ADCDevice(); printf("Program is starting ... \n"); - + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } wiringPiSetup(); - softPwmCreate(ledRedPin,0,100); //creat 3 PMW output pins for RGBLED softPwmCreate(ledGreenPin,0,100); softPwmCreate(ledBluePin,0,100); - pcf8591Setup(pinbase,address); //initialize PCF8591 - while(1){ - val_Red = analogRead(A0); //read analog value of 3 potentiometers - val_Green = analogRead(A1); - val_Blue = analogRead(A2); + int val_Red = adc->analogRead(0); //read analog value of 3 potentiometers + int val_Green = adc->analogRead(1); + int val_Blue = adc->analogRead(2); softPwmWrite(ledRedPin,val_Red*100/255); //map the read value of potentiometers into PWM value and output it softPwmWrite(ledGreenPin,val_Green*100/255); softPwmWrite(ledBluePin,val_Blue*100/255); diff --git a/Code/C_Code/10.1.1_Nightlamp/Nightlamp.c b/Code/C_Code/10.1.1_Nightlamp/Nightlamp.c deleted file mode 100644 index 0c071d2..0000000 --- a/Code/C_Code/10.1.1_Nightlamp/Nightlamp.c +++ /dev/null @@ -1,39 +0,0 @@ -/********************************************************************** -* Filename : Nightlamp.c -* Description : Photoresistor control LED -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -#define ledPin 0 -int main(void){ - int value; - float voltage; - - printf("Program is starting ... \n"); - - wiringPiSetup(); - - softPwmCreate(ledPin,0,100); - pcf8591Setup(pinbase,address); - - while(1){ - value = analogRead(A0); //read analog value of A0 pin - softPwmWrite(ledPin,value*100/255); - voltage = (float)value / 255.0 * 3.3; // calculate voltage - printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage); - delay(100); - } - return 0; -} diff --git a/Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp b/Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp new file mode 100644 index 0000000..46b1b23 --- /dev/null +++ b/Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp @@ -0,0 +1,44 @@ +/********************************************************************** +* Filename : Nightlamp.cpp +* Description : Photoresistor control LED +* Author : www.freenove.com +* modification: 2020/03/09 +**********************************************************************/ +#include +#include +#include +#include + +#define ledPin 0 + +ADCDevice *adc; // Define an ADC Device class object + +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + wiringPiSetup(); + softPwmCreate(ledPin,0,100); + while(1){ + int value = adc->analogRead(0); //read analog value of A0 pin + softPwmWrite(ledPin,value*100/255); + float voltage = (float)value / 255.0 * 3.3; // calculate voltage + printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage); + delay(100); + } + return 0; +} diff --git a/Code/C_Code/11.1.1_Thermometer/Thermometer.c b/Code/C_Code/11.1.1_Thermometer/Thermometer.c deleted file mode 100644 index 325e1ca..0000000 --- a/Code/C_Code/11.1.1_Thermometer/Thermometer.c +++ /dev/null @@ -1,39 +0,0 @@ -/********************************************************************** -* Filename : Thermometer.c -* Description : DIY Thermometer -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -int main(void){ - int adcValue; - float tempK,tempC; - float voltage,Rt; - - printf("Program is starting ... \n"); - - wiringPiSetup(); - - pcf8591Setup(pinbase,address); - while(1){ - adcValue = analogRead(A0); //read analog value A0 pin - voltage = (float)adcValue / 255.0 * 3.3; // calculate voltage - Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor - tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin) - tempC = tempK -273.15; //calculate temperature (Celsius) - printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC); - delay(100); - } - return 0; -} diff --git a/Code/C_Code/11.1.1_Thermometer/Thermometer.cpp b/Code/C_Code/11.1.1_Thermometer/Thermometer.cpp new file mode 100644 index 0000000..5b95a0d --- /dev/null +++ b/Code/C_Code/11.1.1_Thermometer/Thermometer.cpp @@ -0,0 +1,43 @@ +/********************************************************************** +* Filename : Thermometer.cpp +* Description : DIY Thermometer +* Author : www.freenove.com +* modification: 2020/03/09 +**********************************************************************/ +#include +#include +#include +#include + +ADCDevice *adc; // Define an ADC Device class object + +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + printf("Program is starting ... \n"); + while(1){ + int adcValue = adc->analogRead(0); //read analog value A0 pin + float voltage = (float)adcValue / 255.0 * 3.3; // calculate voltage + float Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor + float tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin) + float tempC = tempK -273.15; //calculate temperature (Celsius) + printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC); + delay(100); + } + return 0; +} diff --git a/Code/C_Code/12.1.1_Joystick/Joystick.c b/Code/C_Code/12.1.1_Joystick/Joystick.c deleted file mode 100644 index 9def72c..0000000 --- a/Code/C_Code/12.1.1_Joystick/Joystick.c +++ /dev/null @@ -1,41 +0,0 @@ -/********************************************************************** -* Filename : Joystick.c -* Description : Read Joystick -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -#define Z_Pin 1 //define pin for axis Z - -int main(void){ - int val_X,val_Y,val_Z; - - printf("Program is starting ... \n"); - - wiringPiSetup(); - - pinMode(Z_Pin,INPUT); //set Z_Pin as input pin and pull-up mode - pullUpDnControl(Z_Pin,PUD_UP); - pcf8591Setup(pinbase,address); //initialize PCF8591 - - while(1){ - val_Z = digitalRead(Z_Pin); //read digital value of axis Z - val_Y = analogRead(A0); //read analog value of axis X and Y - val_X = analogRead(A1); - printf("val_X: %d ,\tval_Y: %d ,\tval_Z: %d \n",val_X,val_Y,val_Z); - delay(100); - } - return 0; -} - diff --git a/Code/C_Code/12.1.1_Joystick/Joystick.cpp b/Code/C_Code/12.1.1_Joystick/Joystick.cpp new file mode 100644 index 0000000..3004e52 --- /dev/null +++ b/Code/C_Code/12.1.1_Joystick/Joystick.cpp @@ -0,0 +1,46 @@ +/********************************************************************** +* Filename : Joystick.cpp +* Description : Read Joystick +* Author : www.freenove.com +* modification: 2020/03/09 +**********************************************************************/ +#include +#include +#include +#include + +#define Z_Pin 1 //define pin for axis Z + +ADCDevice *adc; // Define an ADC Device class object + +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + wiringPiSetup(); + pinMode(Z_Pin,INPUT); //set Z_Pin as input pin and pull-up mode + pullUpDnControl(Z_Pin,PUD_UP); + while(1){ + int val_Z = digitalRead(Z_Pin); //read digital value of axis Z + int val_Y = adc->analogRead(0); //read analog value of axis X and Y + int val_X = adc->analogRead(1); + printf("val_X: %d ,\tval_Y: %d ,\tval_Z: %d \n",val_X,val_Y,val_Z); + delay(100); + } + return 0; +} + diff --git a/Code/C_Code/13.1.1_Motor/Motor.c b/Code/C_Code/13.1.1_Motor/Motor.c deleted file mode 100644 index 70a64e9..0000000 --- a/Code/C_Code/13.1.1_Motor/Motor.c +++ /dev/null @@ -1,70 +0,0 @@ -/********************************************************************** -* Filename : Motor.c -* Description : Control Motor by L293D -* Author : www.freenove.com -* modification: 2019/12/27 -**********************************************************************/ -#include -#include -#include -#include -#include -#include - -#define address 0x48 //pcf8591 default address -#define pinbase 64 //any number above 64 -#define A0 pinbase + 0 -#define A1 pinbase + 1 -#define A2 pinbase + 2 -#define A3 pinbase + 3 - -#define motorPin1 2 //define the pin connected to L293D -#define motorPin2 0 -#define enablePin 3 -//Map function: map the value from a range to another range. -long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){ - return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow; -} -//motor function: determine the direction and speed of the motor according to the ADC -void motor(int ADC){ - int value = ADC -128; - if(value>0){ - digitalWrite(motorPin1,HIGH); - digitalWrite(motorPin2,LOW); - printf("turn Forward...\n"); - } - else if (value<0){ - digitalWrite(motorPin1,LOW); - digitalWrite(motorPin2,HIGH); - printf("turn Back...\n"); - } - else { - digitalWrite(motorPin1,LOW); - digitalWrite(motorPin2,LOW); - printf("Motor Stop...\n"); - } - softPwmWrite(enablePin,map(abs(value),0,128,0,100)); - printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);//print the PMW duty cycle -} -int main(void){ - int value; - - printf("Program is starting ... \n"); - - wiringPiSetup(); - - pinMode(enablePin,OUTPUT);//set mode for the pin - pinMode(motorPin1,OUTPUT); - pinMode(motorPin2,OUTPUT); - softPwmCreate(enablePin,0,100);//define PMW pin - pcf8591Setup(pinbase,address);//initialize PCF8591 - - while(1){ - value = analogRead(A0); //read analog value of A0 pin - printf("ADC value : %d \n",value); - motor(value); //make the motor rotate with speed(analog value of A0 pin) - delay(100); - } - return 0; -} - diff --git a/Code/C_Code/13.1.1_Motor/Motor.cpp b/Code/C_Code/13.1.1_Motor/Motor.cpp new file mode 100644 index 0000000..2ad5d93 --- /dev/null +++ b/Code/C_Code/13.1.1_Motor/Motor.cpp @@ -0,0 +1,76 @@ +/********************************************************************** +* Filename : Motor.cpp +* Description : Control Motor by L293D +* Author : www.freenove.com +* modification: 2020/03/09 +**********************************************************************/ +#include +#include +#include +#include +#include +#include + +#define motorPin1 2 //define the pin connected to L293D +#define motorPin2 0 +#define enablePin 3 + +ADCDevice *adc; // Define an ADC Device class object + +//Map function: map the value from a range to another range. +long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){ + return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow; +} +//motor function: determine the direction and speed of the motor according to the ADC +void motor(int ADC){ + int value = ADC -128; + if(value>0){ + digitalWrite(motorPin1,HIGH); + digitalWrite(motorPin2,LOW); + printf("turn Forward...\n"); + } + else if (value<0){ + digitalWrite(motorPin1,LOW); + digitalWrite(motorPin2,HIGH); + printf("turn Back...\n"); + } + else { + digitalWrite(motorPin1,LOW); + digitalWrite(motorPin2,LOW); + printf("Motor Stop...\n"); + } + softPwmWrite(enablePin,map(abs(value),0,128,0,100)); + printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);//print the PMW duty cycle +} +int main(void){ + adc = new ADCDevice(); + printf("Program is starting ... \n"); + + if(adc->detectI2C(0x48)){ // Detect the pcf8591. + delete adc; // Free previously pointed memory + adc = new PCF8591(); // If detected, create an instance of PCF8591. + } + else if(adc->detectI2C(0x4b)){// Detect the ads7830 + delete adc; // Free previously pointed memory + adc = new ADS7830(); // If detected, create an instance of ADS7830. + } + else{ + printf("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + return -1; + } + wiringPiSetup(); + pinMode(enablePin,OUTPUT);//set mode for the pin + pinMode(motorPin1,OUTPUT); + pinMode(motorPin2,OUTPUT); + softPwmCreate(enablePin,0,100);//define PMW pin + while(1){ + int value = adc->analogRead(0); //read analog value of A0 pin + printf("ADC value : %d \n",value); + motor(value); //make the motor rotate with speed(analog value of A0 pin) + delay(100); + } + return 0; +} + diff --git a/Code/Python_Code/07.1.1_ADC/ADC.py b/Code/Python_Code/07.1.1_ADC/ADC.py index ba98031..41693af 100644 --- a/Code/Python_Code/07.1.1_ADC/ADC.py +++ b/Code/Python_Code/07.1.1_ADC/ADC.py @@ -1,38 +1,41 @@ #!/usr/bin/env python3 -############################################################################# -# Filename : ADC.py -# Description : Analog and Digital Conversion, ADC and DAC -# Author : www.freenove.com -# modification: 2019/12/27 ######################################################################## -import smbus +# Filename : ADC.py +# Description : Use ADC module to read the voltage value of potentiometer. +# Author : www.freenove.com +# modification: 2020/03/06 +######################################################################## import time +from ADCDevice import * -address = 0x48 # default address of PCF8591 -bus=smbus.SMBus(1) -cmd=0x40 # command, 0100 0000 +adc = ADCDevice() # Define an ADCDevice class object -def analogRead(chn): # read ADC value,chn:0,1,2,3 - value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): # write DAC value - bus.write_byte_data(address,cmd,value) - +def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) + def loop(): while True: - value = analogRead(0) # read the ADC value of channel 0 - analogWrite(value) # write the DAC value to control led + value = adc.analogRead(0) # read the ADC value of channel 0 voltage = value / 255.0 * 3.3 # calculate the voltage value print ('ADC Value : %d, Voltage : %.2f'%(value,voltage)) - time.sleep(0.01) + time.sleep(0.1) def destroy(): - bus.close() + adc.close() if __name__ == '__main__': # Program entrance print ('Program is starting ... ') try: + setup() loop() except KeyboardInterrupt: # Press ctrl-c to end the program. destroy() diff --git a/Code/Python_Code/08.1.1_Softlight/Softlight.py b/Code/Python_Code/08.1.1_Softlight/Softlight.py index b64e54c..568a152 100644 --- a/Code/Python_Code/08.1.1_Softlight/Softlight.py +++ b/Code/Python_Code/08.1.1_Softlight/Softlight.py @@ -1,51 +1,49 @@ #!/usr/bin/env python3 -############################################################################# -# Filename : Softlight.py -# Description : Control LED with Potentiometer +######################################################################## +# Filename : ADC.py +# Description : Use ADC module to read the voltage value of potentiometer. # Author : www.freenove.com -# modification: 2019/12/27 +# modification: 2020/03/06 ######################################################################## import RPi.GPIO as GPIO -import smbus import time +from ADCDevice import * -address = 0x48 # default address of PCF8591 -bus=smbus.SMBus(1) -cmd=0x40 # command, 0100 0000 ledPin = 11 - -def analogRead(chn): - value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) +adc = ADCDevice() # Define an ADCDevice class object def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) global p GPIO.setmode(GPIO.BOARD) GPIO.setup(ledPin,GPIO.OUT) - GPIO.output(ledPin,GPIO.LOW) - p = GPIO.PWM(ledPin,1000) p.start(0) - + def loop(): while True: - value = analogRead(0) #read ADC value of A0 pin - p.ChangeDutyCycle(value*100/255) #Convert ADC value to duty cycle of PWM - voltage = value / 255.0 * 3.3 #calculate voltage + value = adc.analogRead(0) # read the ADC value of channel 0 + p.ChangeDutyCycle(value*100/255) # Mapping to PWM duty cycle + voltage = value / 255.0 * 3.3 # calculate the voltage value print ('ADC Value : %d, Voltage : %.2f'%(value,voltage)) - time.sleep(0.01) + time.sleep(0.03) def destroy(): - bus.close() - GPIO.cleanup() + adc.close() -if __name__ == '__main__': # Program entrance +if __name__ == '__main__': # Program entrance print ('Program is starting ... ') - setup() try: + setup() loop() except KeyboardInterrupt: # Press ctrl-c to end the program. destroy() diff --git a/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py b/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py index 96b4df3..d033603 100644 --- a/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py +++ b/Code/Python_Code/09.1.1_ColorfulSoftlight/ColorfulSoftlight.py @@ -3,30 +3,29 @@ # Filename : Softlight.py # Description : Control RGBLED with Potentiometer # Author : www.freenove.com -# modification: 2019/12/27 +# modification: 2020/03/09 ######################################################################## import RPi.GPIO as GPIO -import smbus import time - -address = 0x48 -bus=smbus.SMBus(1) -cmd=0x40 +from ADCDevice import * ledRedPin = 15 # define 3 pins for RGBLED ledGreenPin = 13 ledBluePin = 11 - -def analogRead(chn): # read ADC value - bus.write_byte(address,cmd+chn) - value = bus.read_byte(address) - value = bus.read_byte(address) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) +adc = ADCDevice() # Define an ADCDevice class object def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) + global p_Red,p_Green,p_Blue GPIO.setmode(GPIO.BOARD) GPIO.setup(ledRedPin,GPIO.OUT) # set RGBLED pins to OUTPUT mode @@ -42,9 +41,9 @@ def setup(): def loop(): while True: - value_Red = analogRead(0) # read ADC value of 3 potentiometers - value_Green = analogRead(1) - value_Blue = analogRead(2) + value_Red = adc.analogRead(0) # read ADC value of 3 potentiometers + value_Green = adc.analogRead(1) + value_Blue = adc.analogRead(2) p_Red.ChangeDutyCycle(value_Red*100/255) # map the read value of potentiometers into PWM value and output it p_Green.ChangeDutyCycle(value_Green*100/255) p_Blue.ChangeDutyCycle(value_Blue*100/255) @@ -53,7 +52,7 @@ def loop(): time.sleep(0.01) def destroy(): - bus.close() + adc.close() GPIO.cleanup() if __name__ == '__main__': # Program entrance diff --git a/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py b/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py index a294e3a..7d82006 100644 --- a/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py +++ b/Code/Python_Code/10.1.1_Nightlamp/Nightlamp.py @@ -3,25 +3,26 @@ # Filename : Nightlamp.py # Description : Control LED with Photoresistor # Author : www.freenove.com -# modification: 2019/12/27 +# modification: 2020/03/09 ######################################################################## import RPi.GPIO as GPIO -import smbus import time +from ADCDevice import * -address = 0x48 -bus=smbus.SMBus(1) -cmd=0x40 ledPin = 11 # define ledPin - -def analogRead(chn): - value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) +adc = ADCDevice() # Define an ADCDevice class object def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) global p GPIO.setmode(GPIO.BOARD) GPIO.setup(ledPin,GPIO.OUT) # set ledPin to OUTPUT mode @@ -32,14 +33,14 @@ def setup(): def loop(): while True: - value = analogRead(0) # read the ADC value of channel 0 + value = adc.analogRead(0) # read the ADC value of channel 0 p.ChangeDutyCycle(value*100/255) voltage = value / 255.0 * 3.3 print ('ADC Value : %d, Voltage : %.2f'%(value,voltage)) time.sleep(0.01) def destroy(): - bus.close() + adc.close() GPIO.cleanup() if __name__ == '__main__': # Program entrance diff --git a/Code/Python_Code/11.1.1_Thermometer/Thermometer.py b/Code/Python_Code/11.1.1_Thermometer/Thermometer.py index 40d2f75..a4c4a4b 100644 --- a/Code/Python_Code/11.1.1_Thermometer/Thermometer.py +++ b/Code/Python_Code/11.1.1_Thermometer/Thermometer.py @@ -3,38 +3,39 @@ # Filename : Thermometer.py # Description : DIY Thermometer # Author : www.freenove.com -# modification: 2019/12/27 +# modification: 2019/03/09 ######################################################################## import RPi.GPIO as GPIO -import smbus import time import math +from ADCDevice import * -address = 0x48 -bus=smbus.SMBus(1) -cmd=0x40 +adc = ADCDevice() # Define an ADCDevice class object -def analogRead(chn): - value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) - def setup(): - GPIO.setmode(GPIO.BOARD) - + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) + def loop(): while True: - value = analogRead(0) # read ADC value A0 pin - voltage = value / 255.0 * 3.3 # calculate voltage - Rt = 10 * voltage / (3.3 - voltage) # calculate resistance value of thermistor + value = adc.analogRead(0) # read ADC value A0 pin + voltage = value / 255.0 * 3.3 # calculate voltage + Rt = 10 * voltage / (3.3 - voltage) # calculate resistance value of thermistor tempK = 1/(1/(273.15 + 25) + math.log(Rt/10)/3950.0) # calculate temperature (Kelvin) - tempC = tempK -273.15 # calculate temperature (Celsius) + tempC = tempK -273.15 # calculate temperature (Celsius) print ('ADC Value : %d, Voltage : %.2f, Temperature : %.2f'%(value,voltage,tempC)) time.sleep(0.01) def destroy(): + adc.close() GPIO.cleanup() if __name__ == '__main__': # Program entrance diff --git a/Code/Python_Code/12.1.1_Joystick/Joystick.py b/Code/Python_Code/12.1.1_Joystick/Joystick.py index ca22ab6..134155e 100644 --- a/Code/Python_Code/12.1.1_Joystick/Joystick.py +++ b/Code/Python_Code/12.1.1_Joystick/Joystick.py @@ -3,39 +3,38 @@ # Filename : Joystick.py # Description : Read Joystick state # Author : www.freenove.com -# modification: 2019/12/27 +# modification: 2020/03/09 ######################################################################## import RPi.GPIO as GPIO -import smbus import time +from ADCDevice import * -address = 0x48 -bus=smbus.SMBus(1) -cmd=0x40 Z_Pin = 12 # define Z_Pin -def analogRead(chn): # read ADC value - bus.write_byte(address,cmd+chn) - value = bus.read_byte(address) - value = bus.read_byte(address) - # value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) +adc = ADCDevice() # Define an ADCDevice class object def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) GPIO.setmode(GPIO.BOARD) GPIO.setup(Z_Pin,GPIO.IN,GPIO.PUD_UP) # set Z_Pin to pull-up mode def loop(): while True: val_Z = GPIO.input(Z_Pin) # read digital value of axis Z - val_Y = analogRead(0) # read analog value of axis X and Y - val_X = analogRead(1) + val_Y = adc.analogRead(0) # read analog value of axis X and Y + val_X = adc.analogRead(1) print ('value_X: %d ,\tvlue_Y: %d ,\tvalue_Z: %d'%(val_X,val_Y,val_Z)) time.sleep(0.01) def destroy(): - bus.close() + adc.close() GPIO.cleanup() if __name__ == '__main__': diff --git a/Code/Python_Code/13.1.1_Motor/Motor.py b/Code/Python_Code/13.1.1_Motor/Motor.py index b0b5a08..8fef6c1 100644 --- a/Code/Python_Code/13.1.1_Motor/Motor.py +++ b/Code/Python_Code/13.1.1_Motor/Motor.py @@ -6,25 +6,26 @@ # modification: 2019/12/27 ######################################################################## import RPi.GPIO as GPIO -import smbus import time +from ADCDevice import * -address = 0x48 -bus=smbus.SMBus(1) -cmd=0x40 # define the pins connected to L293D motoRPin1 = 13 motoRPin2 = 11 enablePin = 15 - -def analogRead(chn): - value = bus.read_byte_data(address,cmd+chn) - return value - -def analogWrite(value): - bus.write_byte_data(address,cmd,value) +adc = ADCDevice() # Define an ADCDevice class object def setup(): + global adc + if(adc.detectI2C(0x48)): # Detect the pcf8591. + adc = PCF8591() + elif(adc.detectI2C(0x4b)): # Detect the ads7830 + adc = ADS7830() + else: + print("No correct I2C address found, \n" + "Please use command 'i2cdetect -y 1' to check the I2C address! \n" + "Program Exit. \n"); + exit(-1) global p GPIO.setmode(GPIO.BOARD) GPIO.setup(motoRPin1,GPIO.OUT) # set pins to OUTPUT mode @@ -58,13 +59,12 @@ def motor(ADC): def loop(): while True: - value = analogRead(0) # read ADC value of channel 0 + value = adc.analogRead(0) # read ADC value of channel 0 print ('ADC Value : %d'%(value)) motor(value) time.sleep(0.01) def destroy(): - bus.close() GPIO.cleanup() if __name__ == '__main__': # Program entrance diff --git a/Libs/C-Libs/ADCDevice/ADCDevice.cpp b/Libs/C-Libs/ADCDevice/ADCDevice.cpp new file mode 100644 index 0000000..692c1b1 --- /dev/null +++ b/Libs/C-Libs/ADCDevice/ADCDevice.cpp @@ -0,0 +1,68 @@ +/********************************************************************** +* Filename : ADCDevice.cpp +* Description : Freenove ADC Module library. +* Author : www.freenove.com +* modification: 2020/03/06 +**********************************************************************/ +#include "ADCDevice.hpp" + +ADCDevice::ADCDevice(){ + address = 0; + wiringPiSetup(); //Initialize wiringPi. +} + +int ADCDevice::detectI2C(int addr){ + _fd = wiringPiI2CSetup (addr); + if (_fd < 0){ + printf("Error address : 0x%x \n",addr); + return 0 ; + } + else{ + if(wiringPiI2CWrite(_fd,0) < 0){ + printf("Not found device in address 0x%x \n",addr); + return 0; + } + else{ + printf("Found device in address 0x%x \n",addr); + return 1 ; + } + } +} + +int ADCDevice::analogRead(int chn){ + printf("Implemented in subclass! \n"); + return 0; +} + + +PCF8591::PCF8591(int addr) +{ + address = addr; + cmd = 0x40; //The default command for PCF8591 is 0x40. + wiringPiSetup(); + detectI2C(address); + printf("PCF8591 setup successful! \n"); +} +int PCF8591::analogRead(int chn){ + wiringPiI2CWrite(_fd, cmd+chn); + wiringPiI2CRead(_fd); + wiringPiI2CWrite(_fd, cmd+chn); + return wiringPiI2CRead(_fd); +} +int PCF8591::analogWrite(int value){ + return wiringPiI2CWriteReg8(_fd, cmd, value); +} + +ADS7830::ADS7830(int addr) +{ + address = addr; + cmd = 0x84; + wiringPiSetup(); + detectI2C(address); + printf("ADS7830 setup successful! \n"); +} + +int ADS7830::analogRead(int chn){ + wiringPiI2CWrite(_fd, cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + return wiringPiI2CRead(_fd); +} diff --git a/Libs/C-Libs/ADCDevice/ADCDevice.hpp b/Libs/C-Libs/ADCDevice/ADCDevice.hpp new file mode 100644 index 0000000..4ff12ba --- /dev/null +++ b/Libs/C-Libs/ADCDevice/ADCDevice.hpp @@ -0,0 +1,37 @@ +/********************************************************************** +* Filename : ADCDevice.hpp +* Description : Header file of Freenove ADC Module library. +* Author : www.freenove.com +* modification: 2020/03/06 +**********************************************************************/ +#include +#include +#include + +class ADCDevice{ + + protected: + int _fd; + public: + int address; + int cmd; + + ADCDevice(); + virtual ~ADCDevice(){}; + int detectI2C(int addr); + virtual int analogRead(int chn); + //~ADCDevice(){printf("Destructor ... \n");} +}; + +class PCF8591:public ADCDevice{ + public: + PCF8591(int addr = 0x48); //0x48 is the default i2c address for PCF8591 Module. + int analogRead(int chn); //PCF8591 has 4 ADC input pins, chn:0,1,2,3 + int analogWrite(int value); //PCF8591 has DAC function +}; + +class ADS7830:public ADCDevice{ + public: + ADS7830(int addr = 0x4b); //0x4b is the default i2c address for ADS7830 Module. + int analogRead(int chn); //ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7 +}; diff --git a/Libs/C-Libs/ADCDevice/build.sh b/Libs/C-Libs/ADCDevice/build.sh new file mode 100644 index 0000000..ff70966 --- /dev/null +++ b/Libs/C-Libs/ADCDevice/build.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +current_file=$0 +cd "$(dirname "$current_file")" + +g++ -c ADCDevice.cpp +g++ -shared -fPIC -o libADCDevice.so ADCDevice.o +sudo cp ADCDevice.hpp /usr/include/ +sudo cp libADCDevice.so /usr/lib +sudo ldconfig + +echo "build completed!" diff --git a/Libs/Python-Libs/ADCDevice-1.0.2.tar.gz b/Libs/Python-Libs/ADCDevice-1.0.2.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..997f3efda310ed0387c2092589754c82874b72f1 GIT binary patch literal 1439 zcmV;Q1z`FgiwFoPBw}6y|72-%bT2_fLquhEX=7zAF)lDJGA?vsascgG>u=jO5YN}~ zui#>ImN!M#%XVg@1+rw>f-XtX^zor61X-dTwR%CK5(n%4_noMRWhajFbXM$sA0q2` zyd!yc> zOSzp;x{*W?zlm>E`{Lxj6YMV7|IO<^whoUEP9EF-ebZcOf5Yf6*x%H9+6Gn8{!fl0 z#%!MR zA@#jc0z3-&GYZ&NY)?So8Rzk^+r7BBP{w6X#fbv4n?aXkCaxrGvB38HA$@=R`uODI zG5HII5~d)Bl6{ssi5GJ*sH**qZ}V}K1T)G8;*Rm3GM>eXGoGw5{#Elz{0{~O#{W9- ziA^S!7mlOPgxeEqlzF~OUsCY``vQaCD#1AVpT&QCxz6){pw{uP^&uC?;~$za{?`D~ z8YfXeh1q!&`6=~+I7)ayk;h)>TCweXh4VWlg4Oj>o^j7l;pk4-96MPeYhDT=fgJG)D*y@ps{g} zr`MMg>N-M;7F7XC3rSb12rU{!t~d4^yC@6js;+ibqpS8GZ#tw(cY;v{nT+GxX*!#e z6pg+xhj)f$sxqb)+^sxrSyIaUaY@gSzba1PitE)J(RSU0rRmiMQ20`)RZfq7%Th_} z)a@-ACtk>LW`zf1i zsF%g@qm*51qZO8PS?W?#a~s5n2k`QSI8S%#m28SE!ce*DNp@)a(c~?& z-NKcqa~5`nAie5&W~bB?q3nfm#%b(@so)x_vL@@YQOi8H{VdO|>jh6hs&F=u5=+fl z$o10vk}D(&HWs4DH3R*aJ8&^l!6Sw$yqfOs?lt-?ar7Hq?7~ zL)r7T-mq=nVBS$--q9-amO3l*R_k>;3s;?U3vb_KVcC>>a$g=i(9+)|Ndl=mJ39TA zi+}Y>>-?ln)j?-x$LuWe0j%vjJpZd)@nHp_RrMbY?gmx;$JBI@|MfBdTMIAX5Oo(Nb#Tw}TlD35(wSijJObY7J;k!15cJKF7 zg-`ht#J{dB#J{HMI>!GxP<`(u%L~6`>79VAI4#I(Z(7FUvY3S~3!O{IBg3!hO}spc zEuWn;|9f6x}YGX6Dv@%-=idl>&~ twLfkI2m}IwKp+qZ1OkCTAP@)y0)apv5C{YUfk1q>_!pWAv5Ejt000xG>Zbqz literal 0 HcmV?d00001 diff --git a/Processing/Apps/App_01_1_1_Oscilloscope/ADCDevice.pde b/Processing/Apps/App_01_1_1_Oscilloscope/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Apps/App_01_1_1_Oscilloscope/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Apps/App_01_1_1_Oscilloscope/App_01_1_1_Oscilloscope.pde b/Processing/Apps/App_01_1_1_Oscilloscope/App_01_1_1_Oscilloscope.pde index 1103e40..0dad416 100644 --- a/Processing/Apps/App_01_1_1_Oscilloscope/App_01_1_1_Oscilloscope.pde +++ b/Processing/Apps/App_01_1_1_Oscilloscope/App_01_1_1_Oscilloscope.pde @@ -1,7 +1,7 @@ /******************************************************************************* //<>// * Sketch App_01_1_1_Oscilloscope * Author Freenove (http://www.freenove.com) - * Date 2016/08/26 + * Date 2020/03/10 ****************************************************************************** * Brief * This sketch is used to make an oscilloscope @@ -15,7 +15,7 @@ */ import processing.io.*; -PCF8591 pcf = new PCF8591(0x48); +ADCDevice adc = new ADCDevice(); int[] analogs; // Analog data send from serial device int analogsCount; // Length of analogs[] array int voltage = 0; // Voltage @@ -25,6 +25,14 @@ boolean pause = false; // Storage is suspended display void setup() { size(530, 290); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } background(102); textAlign(CENTER, CENTER); textSize(64); @@ -42,7 +50,7 @@ void setup() void draw() { - int analog = pcf.analogRead(0); //serialDevice.requestAnalog(); + int analog = adc.analogRead(0); //serialDevice.requestAnalog(); if (analog != -1) { // GUI @@ -91,7 +99,6 @@ void draw() line(i, analogs[a], i - hMult * width / analogsCount, analogs[a - 1]); } } - } void keyPressed() @@ -106,8 +113,7 @@ void keyPressed() hMult = 5; else if (hMult == 5) hMult = 10; - } - else if (keyCode == DOWN) + } else if (keyCode == DOWN) { if (hMult == 10) hMult = 5; @@ -116,8 +122,7 @@ void keyPressed() else if (hMult == 2) hMult = 1; } - } - else + } else { if (key == ' ') { @@ -127,10 +132,9 @@ void keyPressed() for (int i = 0; i < analogsCount; i++) analogs[i] = -1; } - } - else if (key == '\n' || key == '\r') + } else if (key == '\n' || key == '\r') { link("http://www.freenove.com"); } } -} \ No newline at end of file +} diff --git a/Processing/Apps/App_01_1_1_Oscilloscope/PCF8591.pde b/Processing/Apps/App_01_1_1_Oscilloscope/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Apps/App_01_1_1_Oscilloscope/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Apps/App_02_1_1_Ellipse/ADCDevice.pde b/Processing/Apps/App_02_1_1_Ellipse/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Apps/App_02_1_1_Ellipse/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Apps/App_02_1_1_Ellipse/App_02_1_1_Ellipse.pde b/Processing/Apps/App_02_1_1_Ellipse/App_02_1_1_Ellipse.pde index e64901e..4f4a911 100644 --- a/Processing/Apps/App_02_1_1_Ellipse/App_02_1_1_Ellipse.pde +++ b/Processing/Apps/App_02_1_1_Ellipse/App_02_1_1_Ellipse.pde @@ -14,10 +14,18 @@ ****************************************************************************** */ import processing.io.*; -PCF8591 pcf = new PCF8591(0x48); +ADCDevice adc = new ADCDevice(); void setup() { size(360, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } background(102); textAlign(CENTER, CENTER); textSize(64); @@ -29,8 +37,8 @@ void setup() void draw() { int[] analogs = new int[2]; - analogs[0] = pcf.analogRead(0); - analogs[1] = pcf.analogRead(1); + analogs[0] = adc.analogRead(0); + analogs[1] = adc.analogRead(1); if (analogs != null) { background(102); @@ -61,4 +69,4 @@ void keyPressed() { link("http://www.freenove.com"); } -} \ No newline at end of file +} diff --git a/Processing/Apps/App_02_1_1_Ellipse/PCF8591.pde b/Processing/Apps/App_02_1_1_Ellipse/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Apps/App_02_1_1_Ellipse/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Apps/App_03_1_1_Pong_Game/ADCDevice.pde b/Processing/Apps/App_03_1_1_Pong_Game/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Apps/App_03_1_1_Pong_Game/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Apps/App_03_1_1_Pong_Game/App_03_1_1_Pong_Game.pde b/Processing/Apps/App_03_1_1_Pong_Game/App_03_1_1_Pong_Game.pde index 9aecac2..b4cdc22 100644 --- a/Processing/Apps/App_03_1_1_Pong_Game/App_03_1_1_Pong_Game.pde +++ b/Processing/Apps/App_03_1_1_Pong_Game/App_03_1_1_Pong_Game.pde @@ -2,7 +2,7 @@ ****************************************************************************** * Sketch App_03_1_1_Pong_Game * Author Freenove (http://www.freenove.com) - * Date 2016/08/26 + * Date 2020/03/10 ****************************************************************************** * Brief * This sketch is used to play pong game @@ -16,7 +16,7 @@ */ import processing.io.*; -PCF8591 pcf = new PCF8591(0x48); +ADCDevice adc = new ADCDevice(); int winScore = 3; float acceleration = 0.5; @@ -30,6 +30,14 @@ int lScore, rScore; void setup() { size(640, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } background(102); textAlign(CENTER, CENTER); textSize(64); @@ -45,9 +53,9 @@ void setup() { void draw() { int[] analogs = new int[2]; - analogs[0] = pcf.analogRead(0); - analogs[1] = pcf.analogRead(1); - if (analogs != null) + analogs[0] = adc.analogRead(0); + analogs[1] = adc.analogRead(1); + if (analogs !=null) { lPaddle.position.y = analogs[0] * height /255; rPaddle.position.y = analogs[1] * height /255; @@ -169,4 +177,4 @@ void keyPressed() { ball.reset(); gameState = GameState.PLAYING; } -} \ No newline at end of file +} diff --git a/Processing/Apps/App_03_1_1_Pong_Game/PCF8591.pde b/Processing/Apps/App_03_1_1_Pong_Game/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Apps/App_03_1_1_Pong_Game/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Sketches/Sketch_06_1_1_ADC/ADCDevice.pde b/Processing/Sketches/Sketch_06_1_1_ADC/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Sketches/Sketch_06_1_1_ADC/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Sketches/Sketch_06_1_1_PCF8591/Sketch_06_1_1_PCF8591.pde b/Processing/Sketches/Sketch_06_1_1_ADC/Sketch_06_1_1_ADC.pde similarity index 50% rename from Processing/Sketches/Sketch_06_1_1_PCF8591/Sketch_06_1_1_PCF8591.pde rename to Processing/Sketches/Sketch_06_1_1_ADC/Sketch_06_1_1_ADC.pde index 466f055..4b4e950 100644 --- a/Processing/Sketches/Sketch_06_1_1_PCF8591/Sketch_06_1_1_PCF8591.pde +++ b/Processing/Sketches/Sketch_06_1_1_ADC/Sketch_06_1_1_ADC.pde @@ -1,28 +1,33 @@ /***************************************************** - * Filename : Sketch_06_1_1_PCF8591 - * Description : ADC and DAC of PCF8591 + * Filename : Sketch_06_1_1_ADC + * Description : Making a voltmeter with Freenove ADC module. * auther : www.freenove.com - * modification: 2016/08/20 + * modification: 2020/03/09 *****************************************************/ import processing.io.*; -//Create a object of class PCF8591 -PCF8591 pcf = new PCF8591(0x48); +//Create a object of class ADCDevice +ADCDevice adc = new ADCDevice(); void setup() { size(640, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } } void draw() { - int adc = pcf.analogRead(0); //Read the ADC value of channel 0 - float volt = adc*3.3/255.0; //calculate the voltage - pcf.analogWrite(adc); //Write the DAC + int adcValue = adc.analogRead(0); //Read the ADC value of channel 0 + float volt = adcValue*3.3/255.0; //calculate the voltage background(255); titleAndSiteInfo(); - + fill(0); textAlign(CENTER); //set the text centered textSize(30); - text("ADC: "+nf(adc, 3, 0), width / 2, height/2+50); - textSize(30); - text("DAC: "+nf(adc, 3, 0), width / 2, height/2+100); + text("ADC: "+nf(adcValue, 3, 0), width / 2, height/2+50); textSize(40); //set text size text("Voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2); // } @@ -30,7 +35,7 @@ void titleAndSiteInfo() { fill(0); textAlign(CENTER); //set the text centered textSize(40); //set text size - text("ADC & DAC", width / 2, 40); //title + text("ADC", width / 2, 40); //title textSize(16); text("www.freenove.com", width / 2, height - 20); //site -} \ No newline at end of file +} diff --git a/Processing/Sketches/Sketch_06_1_1_PCF8591/PCF8591.pde b/Processing/Sketches/Sketch_06_1_1_PCF8591/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Sketches/Sketch_06_1_1_PCF8591/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Sketches/Sketch_07_1_1_SoftLight/ADCDevice.pde b/Processing/Sketches/Sketch_07_1_1_SoftLight/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Sketches/Sketch_07_1_1_SoftLight/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Sketches/Sketch_07_1_1_SoftLight/PCF8591.pde b/Processing/Sketches/Sketch_07_1_1_SoftLight/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Sketches/Sketch_07_1_1_SoftLight/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Sketches/Sketch_07_1_1_SoftLight/Sketch_07_1_1_SoftLight.pde b/Processing/Sketches/Sketch_07_1_1_SoftLight/Sketch_07_1_1_SoftLight.pde index b66fcc5..0fa4504 100644 --- a/Processing/Sketches/Sketch_07_1_1_SoftLight/Sketch_07_1_1_SoftLight.pde +++ b/Processing/Sketches/Sketch_07_1_1_SoftLight/Sketch_07_1_1_SoftLight.pde @@ -2,21 +2,29 @@ * Filename : Sketch_07_1_1_SoftLight * Description : control the brightness of led through a potentiometer * auther : www.freenove.com - * modification: 2016/08/20 + * modification: 2020/03/09 *****************************************************/ import processing.io.*; int ledPin = 17; //led -//Create a object of class PCF8591 -PCF8591 pcf = new PCF8591(0x48); +//Create a object of class ADCDevice +ADCDevice adc = new ADCDevice(); SOFTPWM p = new SOFTPWM(ledPin, 0, 100); void setup() { size(640, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } } void draw() { - int adc = pcf.analogRead(0); //Read the ADC value of channel 0 - float volt = adc*3.3/255.0; //calculate the voltage - float dt = adc/255.0; + int adcValue = adc.analogRead(0); //Read the ADC value of channel 0 + float volt = adcValue*3.3/255.0; //calculate the voltage + float dt = adcValue/255.0; p.softPwmWrite((int)(dt*100)); //output the pwm background(255); titleAndSiteInfo(); @@ -28,7 +36,7 @@ void draw() { fill(0); textAlign(CENTER); //set the text centered textSize(30); - text("ADC: "+nf(adc, 3, 0), width / 2, height/2+130); + text("ADC: "+nf(adcValue, 3, 0), width / 2, height/2+130); text("Voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2+100); // } void titleAndSiteInfo() { @@ -38,4 +46,4 @@ void titleAndSiteInfo() { text("SoftLight", width / 2, 40); //title textSize(16); text("www.freenove.com", width / 2, height - 20); //site -} \ No newline at end of file +} diff --git a/Processing/Sketches/Sketch_08_1_1_Thermometer/ADCDevice.pde b/Processing/Sketches/Sketch_08_1_1_Thermometer/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Sketches/Sketch_08_1_1_Thermometer/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Sketches/Sketch_08_1_1_Thermometer/PCF8591.pde b/Processing/Sketches/Sketch_08_1_1_Thermometer/PCF8591.pde deleted file mode 100644 index 267a48a..0000000 --- a/Processing/Sketches/Sketch_08_1_1_Thermometer/PCF8591.pde +++ /dev/null @@ -1,47 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - public int analogRead(int chn) { - int result = 0; - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - try { - byte[] in = i2c.read(1); - result = in[0]&0xff; - } - catch(Exception e) { - println(e); - } - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - i2c.endTransmission(); - byte[] in = i2c.read(4); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Sketches/Sketch_08_1_1_Thermometer/Sketch_08_1_1_Thermometer.pde b/Processing/Sketches/Sketch_08_1_1_Thermometer/Sketch_08_1_1_Thermometer.pde index 6b80aba..ecdbfc7 100644 --- a/Processing/Sketches/Sketch_08_1_1_Thermometer/Sketch_08_1_1_Thermometer.pde +++ b/Processing/Sketches/Sketch_08_1_1_Thermometer/Sketch_08_1_1_Thermometer.pde @@ -2,17 +2,25 @@ * Filename : Sketch_08_1_1_Thermometer * Description : A DIY Thermometer * auther : www.freenove.com - * modification: 2016/08/21 + * modification: 2020/03/09 *****************************************************/ import processing.io.*; -//Create a object of class PCF8591 -PCF8591 pcf = new PCF8591(0x48); +//Create a object of class ADCDevice +ADCDevice adc = new ADCDevice(); void setup() { size(640, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } } void draw() { - int adc = pcf.analogRead(0); //Read the ADC value of channel 0 - float volt = adc*3.3/255.0; //calculate the voltage + int adcValue = adc.analogRead(0); //Read the ADC value of channel 0 + float volt = adcValue*3.3/255.0; //calculate the voltage float tempK,tempC,Rt; // Rt = 10*volt / (3.3-volt); //calculate the resistance value of thermistor tempK = 1/(1/(273.15+25) + log(Rt/10)/3950); //calaulate temperature(Kelvin) @@ -24,7 +32,7 @@ void draw() { fill(0); textAlign(CENTER); //set the text centered textSize(30); - text("ADC: "+nf(adc, 0, 0), width / 2, height/2+50); + text("ADC: "+nf(adcValue, 0, 0), width / 2, height/2+50); textSize(30); text("voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2+100); textSize(40); //set text size @@ -37,4 +45,4 @@ void titleAndSiteInfo() { text("Thermometer", width / 2, 40); //title textSize(16); text("www.freenove.com", width / 2, height - 20); //site -} \ No newline at end of file +} diff --git a/Processing/Sketches/Sketch_14_1_1_Joystick/ADCDevice.pde b/Processing/Sketches/Sketch_14_1_1_Joystick/ADCDevice.pde new file mode 100644 index 0000000..59be0db --- /dev/null +++ b/Processing/Sketches/Sketch_14_1_1_Joystick/ADCDevice.pde @@ -0,0 +1,104 @@ +/***************************************************** + * Filename : ADCDevice + * Description : class ADCDevice + * auther : www.freenove.com + * modification: 2020/03/09 + *****************************************************/ + +class ADCDevice { + public int address = 0; + public int cmd = 0; + public I2C i2c; + public ADCDevice() { + i2c = new I2C(I2C.list()[0]); + } + + public boolean detectI2C(int addr) { + i2c.beginTransmission(addr); + i2c.write(0); + try { + i2c.endTransmission(); + System.out.printf("Found device in address 0x%x\n", addr); + return true; + } + catch(Exception e) { + System.out.printf("Not found device in address 0x%x\n", addr); + return false; + } + } + + public int analogRead(int chn) { + println("Implemented in subclass! \n"); + return 0; + } +} +class PCF8591 extends ADCDevice { + //constructor,Parameters for the PCF8591 I2C address + public PCF8591(int addr) { + address = addr; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + i2c.beginTransmission(address); + i2c.write(0x40 | chn); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } + //Read the ADC value of all channels + public byte[] analogRead() { + i2c.beginTransmission(address); + i2c.write(0x44); + i2c.endTransmission(); + byte[] in = i2c.read(4); + return in; + } + //Write the DACvalue + public void analogWrite(int data) { + i2c.beginTransmission(address); + i2c.write(0x40); + i2c.write(data); + i2c.endTransmission(); + } +} +class ADS7830 extends ADCDevice { + //constructor,Parameters for the ADS7830 I2C address + public ADS7830(int addr) { + address = addr; + cmd = 0x84; + } + //Read the ADC value of one channel + public int analogRead(int chn) { + int result = 0; + i2c.beginTransmission(address); + constrain(chn, 0, 3); + i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4)); + try { + byte[] in = i2c.read(1); + result = in[0]&0xff; + } + catch(Exception e) { + println(e); + } + i2c.endTransmission(); + return result; + } +} diff --git a/Processing/Sketches/Sketch_14_1_1_Joystick/PCF8591.pde b/Processing/Sketches/Sketch_14_1_1_Joystick/PCF8591.pde deleted file mode 100644 index af82ec8..0000000 --- a/Processing/Sketches/Sketch_14_1_1_Joystick/PCF8591.pde +++ /dev/null @@ -1,52 +0,0 @@ -/***************************************************** - * Filename : PCF8591 - * Description : class PCF8591,DAC and ADC - * auther : www.freenove.com - * modification: 2016/08/20 - *****************************************************/ - -class PCF8591 { - private int address; - private I2C i2c; - //constructor,Parameters for the PCF8591 I2C address - public PCF8591(int addr) { - address = addr; - i2c = new I2C(I2C.list()[0]); - } - //Read the ADC value of one channel - //read twice,otherwise errors will occur - public int analogRead(int chn) { - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - byte[] in = i2c.read(1); - int result = in[0]&0xff; - i2c.endTransmission(); - - i2c.beginTransmission(address); - constrain(chn, 0, 3); - i2c.write(0x40 | chn); - in = i2c.read(1); - result = in[0]&0xff; - i2c.endTransmission(); - return result; - } - //Read the ADC value of all channels - public byte[] analogRead() { - i2c.beginTransmission(address); - i2c.write(0x44); - //i2c.endTransmission(); - //i2c.beginTransmission(address); - //i2c.write(0x44); - byte[] in = i2c.read(4); - //i2c.endTransmission(); - return in; - } - //Write the DACvalue - public void analogWrite(int data) { - i2c.beginTransmission(address); - i2c.write(0x40); - i2c.write(data); - i2c.endTransmission(); - } -} \ No newline at end of file diff --git a/Processing/Sketches/Sketch_14_1_1_Joystick/Sketch_14_1_1_Joystick.pde b/Processing/Sketches/Sketch_14_1_1_Joystick/Sketch_14_1_1_Joystick.pde index 882dbc1..009e58d 100644 --- a/Processing/Sketches/Sketch_14_1_1_Joystick/Sketch_14_1_1_Joystick.pde +++ b/Processing/Sketches/Sketch_14_1_1_Joystick/Sketch_14_1_1_Joystick.pde @@ -2,15 +2,23 @@ * Filename : Sketch_14_1_1_Joystick * Description : Display the position of the joystick * auther : www.freenove.com - * modification: 2016/08/29 + * modification: 2020/03/11 *****************************************************/ import processing.io.*; -//Create a object of class PCF8591 -PCF8591 pcf = new PCF8591(0x48); +//Create a object of class ADCDevice +ADCDevice adc = new ADCDevice(); int cx, cy, cd, cr; //define the center point,side length & half. void setup() { size(640, 360); + if (adc.detectI2C(0x48)) { + adc = new PCF8591(0x48); + } else if (adc.detectI2C(0x4b)) { + adc = new ADS7830(0x4b); + } else { + println("Not found ADC Module!"); + System.exit(-1); + } cx = width/2; //center of the display window cy = height/2; // cd = (int)(height/1.5); @@ -18,15 +26,15 @@ void setup() { } void draw() { int x=0, y=0, z=0; - x = pcf.analogRead(2); //read the ADC of joystick - y = pcf.analogRead(1); // - z = pcf.analogRead(0); + x = adc.analogRead(0); //read the ADC of joystick + y = adc.analogRead(1); // + z = adc.analogRead(2); background(102); titleAndSiteInfo(); fill(0); textSize(20); - textAlign(LEFT,TOP); - text("X:"+x+"\nY:"+y+"\nZ:"+z,10,10); + textAlign(LEFT, TOP); + text("X:"+x+"\nY:"+y+"\nZ:"+z, 10, 10); fill(255); //wall color rect(cx-cr, cy-cr, cd, cd); @@ -40,4 +48,4 @@ void titleAndSiteInfo() { text("Joystick", width / 2, 40); //title textSize(16); text("www.freenove.com", width / 2, height - 20); //site -} \ No newline at end of file +}