Libary for PM2.

Dependencies:   LSM9DS1 RangeFinder FastPWM

Dependents:   PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board ... more

Files at this revision

API Documentation at this revision

Comitter:
pmic
Date:
Wed Jun 22 14:05:13 2022 +0000
Parent:
31:f62a64ffb273
Commit message:
Updated SensorBar so it is not blocking the program if it is not connected

Changed in this revision

SensorBar.cpp Show annotated file Show diff for this revision Revisions of this file
SensorBar.h Show annotated file Show diff for this revision Revisions of this file
diff -r f62a64ffb273 -r bb074bb17d39 SensorBar.cpp
--- a/SensorBar.cpp	Fri May 20 07:19:46 2022 +0000
+++ b/SensorBar.cpp	Wed Jun 22 14:05:13 2022 +0000
@@ -3,29 +3,34 @@
 const float SensorBar::TS = 0.004f;                       // period of 1 ms
 
 const char REG_I_ON[16] = {REG_I_ON_0, REG_I_ON_1, REG_I_ON_2, REG_I_ON_3,
-                    REG_I_ON_4, REG_I_ON_5, REG_I_ON_6, REG_I_ON_7,
-                    REG_I_ON_8, REG_I_ON_9, REG_I_ON_10, REG_I_ON_11,
-                    REG_I_ON_12, REG_I_ON_13, REG_I_ON_14, REG_I_ON_15};
-                    
+                           REG_I_ON_4, REG_I_ON_5, REG_I_ON_6, REG_I_ON_7,
+                           REG_I_ON_8, REG_I_ON_9, REG_I_ON_10, REG_I_ON_11,
+                           REG_I_ON_12, REG_I_ON_13, REG_I_ON_14, REG_I_ON_15
+                          };
+
 const char REG_T_ON[16] = {REG_T_ON_0, REG_T_ON_1, REG_T_ON_2, REG_T_ON_3,
-                    REG_T_ON_4, REG_T_ON_5, REG_T_ON_6, REG_T_ON_7,
-                    REG_T_ON_8, REG_T_ON_9, REG_T_ON_10, REG_T_ON_11,
-                    REG_T_ON_12, REG_T_ON_13, REG_T_ON_14, REG_T_ON_15};
-                    
+                           REG_T_ON_4, REG_T_ON_5, REG_T_ON_6, REG_T_ON_7,
+                           REG_T_ON_8, REG_T_ON_9, REG_T_ON_10, REG_T_ON_11,
+                           REG_T_ON_12, REG_T_ON_13, REG_T_ON_14, REG_T_ON_15
+                          };
+
 const char REG_OFF[16] = {REG_OFF_0, REG_OFF_1, REG_OFF_2, REG_OFF_3,
-                    REG_OFF_4, REG_OFF_5, REG_OFF_6, REG_OFF_7,
-                    REG_OFF_8, REG_OFF_9, REG_OFF_10, REG_OFF_11,
-                    REG_OFF_12, REG_OFF_13, REG_OFF_14, REG_OFF_15};
+                          REG_OFF_4, REG_OFF_5, REG_OFF_6, REG_OFF_7,
+                          REG_OFF_8, REG_OFF_9, REG_OFF_10, REG_OFF_11,
+                          REG_OFF_12, REG_OFF_13, REG_OFF_14, REG_OFF_15
+                         };
 
 const char REG_T_RISE[16] = {0xFF, 0xFF, 0xFF, 0xFF,
-                    REG_T_RISE_4, REG_T_RISE_5, REG_T_RISE_6, REG_T_RISE_7,
-                    0xFF, 0xFF, 0xFF, 0xFF,
-                    REG_T_RISE_12, REG_T_RISE_13, REG_T_RISE_14, REG_T_RISE_15};
-                    
+                             REG_T_RISE_4, REG_T_RISE_5, REG_T_RISE_6, REG_T_RISE_7,
+                             0xFF, 0xFF, 0xFF, 0xFF,
+                             REG_T_RISE_12, REG_T_RISE_13, REG_T_RISE_14, REG_T_RISE_15
+                            };
+
 const char REG_T_FALL[16] = {0xFF, 0xFF, 0xFF, 0xFF,
-                    REG_T_FALL_4, REG_T_FALL_5, REG_T_FALL_6, REG_T_FALL_7,
-                    0xFF, 0xFF, 0xFF, 0xFF,
-                    REG_T_FALL_12, REG_T_FALL_13, REG_T_FALL_14, REG_T_FALL_15};
+                             REG_T_FALL_4, REG_T_FALL_5, REG_T_FALL_6, REG_T_FALL_7,
+                             0xFF, 0xFF, 0xFF, 0xFF,
+                             REG_T_FALL_12, REG_T_FALL_13, REG_T_FALL_14, REG_T_FALL_15
+                            };
 
 SensorBar::SensorBar(I2C& i2c, float distAxisToSensor) : i2c(i2c), thread(osPriorityAboveNormal, 4096)
 {
@@ -47,11 +52,12 @@
 
     clearBarStrobe();  // to illuminate all the time
     clearInvertBits(); // to make the bar look for a dark line on a reflective surface
-    begin();
 
     // set up thread
-    thread.start(callback(this, &SensorBar::update));
-    ticker.attach(callback(this, &SensorBar::sendThreadFlag), std::chrono::microseconds{static_cast<long int>(1.0e6f * TS)});
+    if (begin()) {
+        thread.start(callback(this, &SensorBar::update));
+        ticker.attach(callback(this, &SensorBar::sendThreadFlag), std::chrono::microseconds{static_cast<long int>(1.0e6f * TS)});
+    }
 }
 
 SensorBar::~SensorBar()
@@ -191,9 +197,9 @@
 
 //Run this once during initialization to configure the SX1509 as a sensor bar
 //Returns 1 for success
-uint8_t SensorBar::begin(void)
+bool SensorBar::begin(void)
 {
-    uint8_t returnVar = 0;
+    bool returnVar = false;
 
     // Reset the SX1509
     reset();
@@ -209,9 +215,7 @@
         writeByte(REG_DIR_B, 0xFC);
         writeByte(REG_DATA_B, 0x01);
 
-        returnVar = 1;
-    } else {
-        returnVar = 0;
+        returnVar = true;
     }
 
     return returnVar;
@@ -385,7 +389,7 @@
             if(is_first_avg) {
                 is_first_avg = false;
                 avg_filter.reset(angle);
-            } 
+            }
             avg_angle = avg_filter.update(angle);
         }
     }
diff -r f62a64ffb273 -r bb074bb17d39 SensorBar.h
--- a/SensorBar.h	Fri May 20 07:19:46 2022 +0000
+++ b/SensorBar.h	Wed Jun 22 14:05:13 2022 +0000
@@ -39,7 +39,7 @@
 #define     REG_MISC                0x1F    //  RegMisc Miscellaneous device settings register 0000 0000
 #define     REG_LED_DRIVER_ENABLE_B 0x20    //  RegLEDDriverEnableB LED driver enable register _ I/O[15_8] (Bank B) 0000 0000
 #define     REG_LED_DRIVER_ENABLE_A 0x21    //  RegLEDDriverEnableA LED driver enable register _ I/O[7_0] (Bank A) 0000 0000
-// Debounce and Keypad Engine       
+// Debounce and Keypad Engine
 #define     REG_DEBOUNCE_CONFIG     0x22    //  RegDebounceConfig Debounce configuration register 0000 0000
 #define     REG_DEBOUNCE_ENABLE_B   0x23    //  RegDebounceEnableB Debounce enable register _ I/O[15_8] (Bank B) 0000 0000
 #define     REG_DEBOUNCE_ENABLE_A   0x24    //  RegDebounceEnableA Debounce enable register _ I/O[7_0] (Bank A) 0000 0000
@@ -47,7 +47,7 @@
 #define     REG_KEY_CONFIG_2        0x26    //  RegKeyConfig2 Key scan configuration register 0000 0000
 #define     REG_KEY_DATA_1          0x27    //  RegKeyData1 Key value (column) 1111 1111
 #define     REG_KEY_DATA_2          0x28    //  RegKeyData2 Key value (row) 1111 1111
-// LED Driver (PWM, blinking, breathing)        
+// LED Driver (PWM, blinking, breathing)
 #define     REG_T_ON_0              0x29    //  RegTOn0 ON time register for I/O[0] 0000 0000
 #define     REG_I_ON_0              0x2A    //  RegIOn0 ON intensity register for I/O[0] 1111 1111
 #define     REG_OFF_0               0x2B    //  RegOff0 OFF time/intensity register for I/O[0] 0000 0000
@@ -112,10 +112,10 @@
 #define     REG_OFF_15              0x66    //  RegOff15 OFF time/intensity register for I/O[15] 0000 0000
 #define     REG_T_RISE_15           0x67    //  RegTRise15 Fade in register for I/O[15] 0000 0000
 #define     REG_T_FALL_15           0x68    //  RegTFall15 Fade out register for I/O[15] 0000 0000
-//  Miscellaneous       
+//  Miscellaneous
 #define     REG_HIGH_INPUT_B        0x69    //  RegHighInputB High input enable register _ I/O[15_8] (Bank B) 0000 0000
 #define     REG_HIGH_INPUT_A        0x6A    //  RegHighInputA High input enable register _ I/O[7_0] (Bank A) 0000 0000
-//  Software Reset      
+//  Software Reset
 #define     REG_RESET               0x7D    //  RegReset Software reset register 0000 0000
 #define     REG_TEST_1              0x7E    //  RegTest1 Test register 0000 0000
 #define     REG_TEST_2              0x7F    //  RegTest2 Test register 0000 0000
@@ -171,7 +171,7 @@
     uint8_t pinOscillator;
     uint8_t pinReset;
 
-    uint8_t begin();        // Run this once during initialization to configure the SX1509 as a sensor bar
+    bool begin();        // Run this once during initialization to configure the SX1509 as a sensor bar
     void reset();
 
     // Read Functions: