jim hamblen / Mbed 2 deprecated RPG_demo

Dependencies:   mbed PinDetect

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Thu Jan 13 14:29:07 2022 +0000
Parent:
0:3eea8ad2dbbc
Commit message:
ver 1.1 put lookup table in Flash using const; Added a few more comments

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jan 12 19:52:47 2022 +0000
+++ b/main.cpp	Thu Jan 13 14:29:07 2022 +0000
@@ -22,16 +22,21 @@
 volatile int new_enc = 0;
 volatile int enc_count = 0;
 //Instead of a slow 16 case statement use a faster table lookup of truth table
-//index with (previous AB value | current AB value) to find -1,0,1 change in count
-int lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
+//index table with (previous encoder AB value<<2 | current current encoder AB value) 
+//to find -1(CCW),0,or 1(CW) change in count each time a bit changes state
+//Always want Interrupt routines to run fast!
+//const puts data in read only Flash instead of RAM
+const int lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
 //Encoder bit change interrupt service routine
+//called whenever one of the two A,B encoder bits change state
 void Enc_change_ISR(void)
 {
-    new_enc = RPG_A<<1 | RPG_B;
+    new_enc = RPG_A<<1 | RPG_B;//current encoder bits
+    //check truth table for -1,0 or +1 added to count
     enc_count = enc_count + lookup_table[old_enc<<2 | new_enc];
     old_enc = new_enc;
 }
-//debounced pushbutton callback
+//debounced RPG pushbutton switch callback
 void PB_callback(void)
 {
     ledPB= !ledPB;
@@ -39,7 +44,7 @@
 int main()
 {
 //turn off built-in RPG encoder RGB led
-    red = 1.0;//1.0 is "off", 0.0 is full "on"
+    red = 1.0;//PWM value 1.0 is "off", 0.0 is full "on"
     green = 1.0;
     blue = 1.0;
 //current color adjust set to red
@@ -57,7 +62,6 @@
         // Scale/limit count to 0..100
         if (enc_count>100) enc_count = 100;
         if (enc_count<0) enc_count = 0;
-        ledPB = RPG_PB;
         red = 1.0 - enc_count/100.0;
 //        pc.printf("%D\n\r",enc_count);
     }