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.
Dependencies: RF24 USBDevice mbed
Revision 6:db4538895ae7, committed 2015-12-05
- Comitter:
- pro100kot14
- Date:
- Sat Dec 05 16:57:30 2015 +0000
- Parent:
- 5:ce998523d17d
- Commit message:
- Added unit testing. Test results are displayed on the device LEDs.
Changed in this revision
--- a/Sensors/Photoresistor.cpp Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/Photoresistor.cpp Sat Dec 05 16:57:30 2015 +0000
@@ -3,9 +3,13 @@
Photoresistor::Photoresistor(AnalogIn inputChanel):input(inputChanel){}
Illumination Photoresistor::getIllumination(){
+ return getIlluminationByAdcValue(input.read());
+}
+
+Illumination Photoresistor::getIlluminationByAdcValue(float adcVal){
double realV;
//3.3 - ADC maximum
- realV = input.read()*3.3;
+ realV = adcVal*3.3;
if(realV > 0){
double resistance = (10.0 * 3.3) / realV - 10.0; //resistance in kOhms.
@@ -17,4 +21,28 @@
}else{
return DARK; //resistance == +inf. No light
}
-}
\ No newline at end of file
+}
+
+bool PhotoresistorTest::adcValue_0_isDark(){
+ return Photoresistor::getIlluminationByAdcValue(0.0)==DARK;
+}
+
+bool PhotoresistorTest::adcValue_0_009_isDark(){
+ return Photoresistor::getIlluminationByAdcValue(0.009)==DARK;
+}
+
+bool PhotoresistorTest::adcValue_0_34_isVeryCloudly(){
+ return Photoresistor::getIlluminationByAdcValue(0.34)==VERY_CLOUDLY;
+}
+
+bool PhotoresistorTest::adcValue_0_93_isCloudly(){
+ return Photoresistor::getIlluminationByAdcValue(0.93)==CLOUDLY;
+}
+
+bool PhotoresistorTest::adcValue_0_97_isClear(){
+ return Photoresistor::getIlluminationByAdcValue(0.97)==CLEAR;
+}
+
+bool PhotoresistorTest::adcValue_0_98_isVerySunny(){
+ return Photoresistor::getIlluminationByAdcValue(0.98)==VERY_SUNNY;
+}
\ No newline at end of file
--- a/Sensors/Photoresistor.h Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/Photoresistor.h Sat Dec 05 16:57:30 2015 +0000
@@ -22,9 +22,32 @@
* @returns Level of illumination
*/
Illumination getIllumination();
+
+ /**
+ * Level of illumination provided by enum Illumination.
+ * Сomfortably for testing.
+ *
+ * @param adcVal ADC value on the port to which the sensor is connected
+ *
+ * @returns Level of illumination
+ */
+ static Illumination getIlluminationByAdcValue(float adcVal);
private:
AnalogIn input;
};
+/**
+* Tested the class Photoresistor
+*/
+class PhotoresistorTest{
+public:
+ static bool adcValue_0_isDark();
+ static bool adcValue_0_009_isDark();
+ static bool adcValue_0_34_isVeryCloudly();
+ static bool adcValue_0_93_isCloudly();
+ static bool adcValue_0_97_isClear();
+ static bool adcValue_0_98_isVerySunny();
+};
+
#endif
\ No newline at end of file
--- a/Sensors/Thermistor.cpp Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/Thermistor.cpp Sat Dec 05 16:57:30 2015 +0000
@@ -6,12 +6,17 @@
this->c = c;
}
+
double Thermistor::getTemperature(){
+ return getTemperatureByAdcValue(input.read(), a, b, c, error);
+}
+
+double Thermistor::getTemperatureByAdcValue(float adcVal, double a, double b, double c, double error){
double temp;
double realV;
double resistance;
//3.3 - ADC maximum
- realV = input.read()*3.3;
+ realV = adcVal*3.3;
resistance = (10000 * 3.3) / realV - 10000;
//Considering the error
resistance -= error;
@@ -23,6 +28,7 @@
return temp;
}
+
void Thermistor::setError(double error){
this->error = error;
}
@@ -53,4 +59,16 @@
double Thermistor::getCoefficientC(){
return c;
- }
\ No newline at end of file
+ }
+
+ bool ThermistorTest::adcValue_0_049_is_0_degree(){
+ return abs(Thermistor::getTemperatureByAdcValue(0.049, 0.001995, 0.00007997, 0.0000003863)) < 1.0;
+ }
+
+ bool ThermistorTest::adcValue_0_104_is_15_degree(){
+ return abs(Thermistor::getTemperatureByAdcValue(0.104, 0.001995, 0.00007997, 0.0000003863) - 15.0) < 1.0;
+ }
+
+ bool ThermistorTest::adcValue_0_202_is_30_degree(){
+ return abs(Thermistor::getTemperatureByAdcValue(0.202, 0.001995, 0.00007997, 0.0000003863) - 30.0) < 1.0;
+ }
\ No newline at end of file
--- a/Sensors/Thermistor.h Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/Thermistor.h Sat Dec 05 16:57:30 2015 +0000
@@ -29,6 +29,20 @@
double getTemperature();
/**
+ * The temperature in degrees Celsius, calculated based on a parameter.
+ * Сomfortably for testing.
+ *
+ * @param adcVal ADC value on the port to which the sensor is connected
+ * @param a Steinhart–Hart coefficient
+ * @param b Steinhart–Hart coefficient
+ * @param c Steinhart–Hart coefficient
+ * @param error Error in Ohms (0 by default)
+ *
+ * @returns Temperature
+ */
+ static double getTemperatureByAdcValue(float adcVal, double a, double b, double c, double error = 0);
+
+ /**
* Set error value used in the calculation.
* The resulting resistance is computed as <real resistance> - error;
* @param error Error in Ohms
@@ -85,4 +99,14 @@
double a, b, c;
};
+/**
+* Tested the class Thermistor
+*/
+class ThermistorTest{
+public:
+ static bool adcValue_0_049_is_0_degree();
+ static bool adcValue_0_104_is_15_degree();
+ static bool adcValue_0_202_is_30_degree();
+};
+
#endif
\ No newline at end of file
--- a/Sensors/ThermometerTmp36.cpp Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/ThermometerTmp36.cpp Sat Dec 05 16:57:30 2015 +0000
@@ -1,14 +1,30 @@
#include "ThermometerTmp36.h"
double ThermometerTmp36::getTemperature(){
+ return getTemperatureByAdcValue(input.read());
+}
+
+double ThermometerTmp36::getTemperatureByAdcValue(float adcVal){
double temp;
double realV;
//3.3 - ADC maximum
- realV = input.read()*3.3;
+ realV = adcVal*3.3;
//500mV - voltage in 0 degree
//For mo information look datasheet
//Low Voltage Temperature Sensors TMP35/TMP36/TMP37
//Page 5 of 20, Figure 6 (Output Voltage vs. Temperature)
temp = (realV - 0.5)*100;
return temp;
+}
+
+bool ThermometerTmp36Test::adcValue_0_15_is_0_degree(){
+ return abs(ThermometerTmp36::getTemperatureByAdcValue(0.15)) < 0.5;
+}
+
+bool ThermometerTmp36Test::adcValue_0_196_is_15_degree(){
+ return abs(ThermometerTmp36::getTemperatureByAdcValue(0.196) - 15.0) < 0.5;
+}
+
+bool ThermometerTmp36Test::adcValue_0_242_is_30_degree(){
+ return abs(ThermometerTmp36::getTemperatureByAdcValue(0.242) - 30.0) < 0.5;
}
\ No newline at end of file
--- a/Sensors/ThermometerTmp36.h Wed Oct 28 20:52:12 2015 +0000
+++ b/Sensors/ThermometerTmp36.h Sat Dec 05 16:57:30 2015 +0000
@@ -22,8 +22,28 @@
*/
double getTemperature();
+ /**
+ * The temperature in degrees Celsius, calculated based on a parameter.
+ * Сomfortably for testing.
+ *
+ * @param adcVal ADC value on the port to which the sensor is connected
+ *
+ * @returns Temperature
+ */
+ static double getTemperatureByAdcValue(float adcVal);
+
private:
AnalogIn input;
};
+/**
+* Tested the class ThermometerTmp36
+*/
+class ThermometerTmp36Test{
+public:
+ static bool adcValue_0_15_is_0_degree();
+ static bool adcValue_0_196_is_15_degree();
+ static bool adcValue_0_242_is_30_degree();
+};
+
#endif
\ No newline at end of file
--- a/main.cpp Wed Oct 28 20:52:12 2015 +0000
+++ b/main.cpp Sat Dec 05 16:57:30 2015 +0000
@@ -1,6 +1,44 @@
#include "WirelessListener.h"
+#define TEST
+
+#ifdef TEST
+#include "USBSerial.h"
+DigitalOut ledD1(LED1);
+DigitalOut ledD2(LED2);
+DigitalOut ledD3(LED3);
+DigitalOut ledD4(LED4);
+
+void doTest(){
+ ledD1 = ledD2 = ledD3 = ledD4 = 0;
+ if(
+ ThermometerTmp36Test::adcValue_0_15_is_0_degree() &&
+ ThermometerTmp36Test::adcValue_0_196_is_15_degree() &&
+ ThermometerTmp36Test::adcValue_0_242_is_30_degree()
+ ) ledD1 = 1;
+
+ if(
+ ThermistorTest::adcValue_0_049_is_0_degree() &&
+ ThermistorTest::adcValue_0_104_is_15_degree() &&
+ ThermistorTest::adcValue_0_202_is_30_degree()
+ ) ledD2 = 1;
+
+ if(
+ PhotoresistorTest::adcValue_0_isDark() &&
+ PhotoresistorTest::adcValue_0_009_isDark() &&
+ PhotoresistorTest::adcValue_0_34_isVeryCloudly() &&
+ PhotoresistorTest::adcValue_0_93_isCloudly() &&
+ PhotoresistorTest::adcValue_0_97_isClear() &&
+ PhotoresistorTest::adcValue_0_98_isVerySunny()
+ ) ledD3 = 1;
+}
+#endif
+
int main(){
+ #ifdef TEST
+ doTest();
+ #endif
+
WirelessListener listener(0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL);
listener.startListening();