Al Williams / Mbed 2 deprecated Nucleo_Encoder_RGB

Dependencies:   mRotaryEncoder mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mRotaryEncoder.h"
00003 
00004 // Simple demo for Hackaday
00005 // Al Williams
00006 
00007 // This require two Keyes modules (or equivalent)
00008 // A KY-040 rotary encoder is connected to D7, D8 and the switch to D2
00009 // D8 connects to CLK and D7 connects to DT
00010 // if the rotation is backwards switch the wiring or swap the definitions in 
00011 // software
00012 
00013 // There is also a KY-016 RGB LED with integrated resistors
00014 // This board plugs in with the ground pin next to D13
00015 // then the other pins naturally hit D13, D12, and D11
00016 
00017 PwmOut blueled(D13);
00018 PwmOut greenled(D12);
00019 PwmOut redled(D11);
00020 
00021 // RGB values for  LEDS
00022 PwmOut *leds[]={&redled, &greenled, &blueled};
00023 float rgb[]={0.0, 0,0, 0.0};
00024 int sel=0;  // which component are we changing?
00025 
00026 DigitalIn mybutton(USER_BUTTON);  // not used here
00027 
00028 // Here's the encoder object
00029 mRotaryEncoder enc(D7,D8, D2,PullNone);
00030 
00031 
00032 // Helper function to set the PWM values
00033 void setleds()
00034 {
00035     for (int i=0;i<sizeof(leds)/sizeof(leds[0]);i++) leds[i]->write(rgb[i]);
00036 }
00037 
00038 
00039 // Library calls here when you go clockwise
00040 void cw()
00041 {
00042 // modify the selected RGB component    
00043    rgb[sel]+=0.1;
00044    if (rgb[sel]>1.0) rgb[sel]=1.0;
00045    setleds();
00046 }
00047 
00048 // Library calls here when you go anticlockwise
00049 void ccw()
00050 {
00051 // modify the selected RGB component    
00052    rgb[sel]-=0.1;
00053    if (rgb[sel]<0.0) rgb[sel]=0.0;
00054    setleds();
00055 }
00056 
00057 // Library calls here when you push in on the encoder shaft
00058 void btn()
00059 {
00060     // change selected component (0, 1, 2)
00061     if (++sel>2) sel=0;
00062 }
00063 
00064 int main() {
00065 // Set up encoder callbacks
00066     enc.attachROTCW(cw);
00067     enc.attachROTCCW(ccw);
00068     enc.attachSW(btn);
00069     // set fast period
00070     redled.period(0.01);
00071     greenled.period(0.01);
00072     blueled.period(0.01);
00073     while (true);   // nothing else to do but wait
00074 }