ROM Comm / Mbed 2 deprecated I2C-LCD

Dependencies:   mbed TextLCD mRotaryEncoder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "mRotaryEncoder.h"
00004  
00005 DigitalOut fan_enable(PA_8); // high to enable fan, low to turn off
00006 PwmOut fan_pwm(PB_15);    //D9 D13);
00007 // I2C Communication
00008 I2C i2c_lcd(PB_7,PB_6); // SDA, SCL
00009  
00010 TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2, TextLCD::HD44780); // I2C bus, PCF8574 Slaveaddress, LCD Type, Device Type 
00011 
00012 // Here's the encoder object
00013 //mRotaryEncoder enc(D7,D8, D2,PullNone);
00014 //RPG rpg1(PA_3,PA_4,PA_7); //Set up RPG   
00015 //mRotaryEncoder enc(PA_3,PA_4,PA_7,PullUp);
00016 mRotaryEncoder enc(PB_12,PB_13,PB_14,PullUp);
00017 
00018 float fan_speed = 0.00;
00019 float fan_speed_old = 0.00;
00020 int fan_pwr=0;  //
00021 char buffer[20];
00022  
00023 
00024 // LCD print helper
00025 void lcd_printS(int line, int col, char text[])
00026 {
00027     lcd.locate(col, line);       //locate(int column, int row);
00028     lcd.printf(text);
00029     //for (i=0;i<strlen(text);i++) 
00030     //    printC(text[i]);
00031 }
00032 
00033 // Helper function to set the PWM values
00034 void setfanspeed()
00035 {
00036     //if (fan_pwr>0) fan_pwm.write(fan_speed);
00037     fan_pwm.write(fan_speed);
00038     //fan_pwm.write(1.00 - fan_speed);
00039 }
00040 
00041 // Library calls here when you go clockwise
00042 void cw()
00043 {
00044 // modify the selected RGB component    
00045     if (fan_pwr>0)
00046     {
00047         fan_speed +=0.010;
00048         if (fan_speed>0.95) fan_speed=0.950;
00049     }
00050     setfanspeed();
00051 }
00052 
00053 // Library calls here when you go anticlockwise
00054 void ccw()
00055 {
00056 // modify the selected RGB component    
00057    if (fan_pwr>0) 
00058    {
00059         fan_speed -=0.010;
00060         if (fan_speed<0.05) fan_speed=0.050;
00061    }
00062    setfanspeed();
00063 }
00064 
00065 // Library calls here when you push in on the encoder shaft
00066 void btn()
00067 {
00068     // change selected component (0, 1, 2)
00069     if (++fan_pwr>1)
00070     {
00071          fan_enable = 0;     // turn fan OFF
00072          fan_pwr=0;
00073          fan_speed_old = fan_speed;
00074          fan_speed = 0.00;         
00075     }
00076     else
00077     {
00078        fan_enable = 1;     // turn fan ON
00079        fan_speed = fan_speed_old; 
00080     }
00081     setfanspeed();
00082 }
00083 
00084 
00085 
00086 int main() 
00087 {
00088     fan_enable = 0;     // turn fan off
00089     //fan_pwm.period(0.01); // setup PWM in seconds. 0.01 == 100hz
00090     //fan_pwm.period_ms(100); // setup PWM in milli seconds. 1 == 1 khz
00091     // Note: the PWM using the 555 supplied by RIXENS runs around 200khz
00092     // - running the pwm at 100khz looses resolution at the top and bottom.
00093     // - running the pwm at 10khz has much more accuracy and resolution.
00094     // - running the pwm at 20khz has same accuracy and resolution as 10khz.
00095     fan_pwm.period_us(100); // setup PWM in micro seconds. 100 == 10khz, 50 == 20khz,10 == 100khz, 5 == 200khz,
00096     
00097     setfanspeed();
00098     // Set up encoder callbacks
00099     enc.attachROTCW(cw);
00100     enc.attachROTCCW(ccw);
00101     enc.attachSW(btn);   
00102        
00103     lcd.setMode(TextLCD::DispOn); //DispOff, DispOn
00104     lcd.setBacklight(TextLCD::LightOff);//LightOff, LightOn
00105     lcd.setCursor(TextLCD::CurOff_BlkOff);//CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn
00106     //lcd.printf("Testing I2C LCD");
00107     //lcd.locate(0, 1);       //locate(int column, int row);
00108     //lcd.printf("does it work");
00109     //lcd.setBacklight(TextLCD::LightOn);//LightOff, LightOn
00110        
00111     lcd.cls();
00112     lcd_printS(0,0,"Fan Setting:");
00113     lcd_printS(1,0,"Fan Speed: ");
00114  
00115     while (true)   // nothing else to do but wait
00116     {  
00117         wait(0.25);    
00118         if (fan_pwr>0) lcd_printS(0,12," ON ");
00119         else lcd_printS(0,12," OFF");
00120         sprintf(buffer,"%3d", (int)(fan_speed * 100));
00121         lcd_printS(1,12,buffer);  
00122         //LCD.printS(1, 0, buffer);   
00123     }
00124 }