joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "nexpaq_mdk.h"
00003 #include "MAX44000.h"
00004 #include "pwm_tone.h"
00005  
00006 //MAX44000 max44000(P1_6, P1_7);
00007 //i2c code goes here////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00008 I2C i2c(P1_6,P1_7);
00009 PwmOut ledR(P2_4);
00010 PwmOut ledG(P2_5);
00011 PwmOut ledB(P2_6);
00012 PwmOut Buzzer(P1_2);
00013 DigitalIn button(P0_1, PullUp);
00014 //DigitalOut buz(P0_0);
00015 
00016 //int buzzing=0;
00017  
00018 /***** Definitions *****/
00019 #define     FUNCTION_TABLE_NUM                  1
00020 #define     UUID_NUM                            16          //UUID number is 16, don't change it
00021 #define     LOOP_DELAY                          100
00022 #define     PROX_THRESHOLD                      50
00023  
00024 /***** Globals *****/
00025 void my_function_CMD_2700(unsigned char *pData, unsigned char len);
00026 const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = {
00027     {0x2700, my_function_CMD_2700},     // Command -> function
00028 };
00029  int buzzing=0;
00030 //int lastPrx = 0;
00031 int toggle = 0;
00032 unsigned char prxPress = 0x02;
00033 int lastBtn = 1;
00034 unsigned char btnPress = 0x01;
00035  
00036 /***** Functions *****/
00037 void my_function_CMD_2700(unsigned char *pData, unsigned char len)
00038 {
00039     unsigned char response = 0x00;
00040     ledR = 1.0f - (pData[0] / 255.0f);
00041     ledG = 1.0f - (pData[1] / 255.0f);
00042     ledB = 1.0f - (pData[2] / 255.0f);
00043     toggle = pData[0];
00044     np_api_upload(0x2701, &response, 1);
00045 }
00046 
00047 int i2c_read_gesture(char gestReg)
00048 {
00049     char    readVal;
00050     //char    gestReg;
00051  
00052     //gestReg    = 0x04;
00053  
00054     i2c.write( 0x20, &gestReg, 1 );
00055     i2c.read( 0x20, &readVal, 1 );
00056     
00057     //perform cast here 
00058     return( readVal );
00059 }
00060  
00061 /******************************************************************************/
00062 void app_setup()
00063 {
00064     if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) {
00065         // Register failed handle code
00066         error("MDK Register Failed");
00067     }
00068     //max44000.init(MAX44000::MODE_ALS_PROX, MAX44000::ALSTIM_64X, MAX44000::ALSPGA_1X, MAX44000::DRV_110);
00069     ledR = 1.0f;
00070     ledG = 1.0f;
00071     ledB = 1.0f;
00072     
00073     //buz = 0;
00074 }
00075  
00076 void app_loop()
00077 {
00078     float A_4 = 1000000/Do4;
00079     //int proxData = max44000.readReg(MAX44000::REG_PRX_DATA);
00080     int gesture = i2c_read_gesture(0x04);
00081     
00082     if(!buzzing){
00083     if (gesture == 0x01) {
00084         if (!toggle) {
00085             np_api_upload(0x2800, &prxPress, 1);
00086             //int dummy = i2c_read_gesture(0x0a);
00087             //Stop_tunes(Buzzer);
00088             }
00089         toggle = 1;
00090         
00091     } else if (gesture == 0x02) {
00092         if(toggle){
00093         np_api_upload(0x2800, &prxPress, 1);
00094         }
00095     toggle = 0;}
00096     
00097     if(toggle){
00098         int distance = i2c_read_gesture(0x0a);
00099         if(distance<60){
00100         buzzing =1;}
00101         }
00102     
00103  
00104     if (!button && lastBtn) {
00105         np_api_upload(0x2800, &btnPress, 1);
00106     }
00107     }
00108     lastBtn = button;
00109     if(buzzing){
00110         Tune(Buzzer, A_4, 4);  //4 Octave C beat 4/16
00111         }
00112     if(buzzing &&!toggle){
00113         buzzing=0;
00114         //np_api_upload(0x2800, &prxPress, 1);
00115         //toggle = 1;
00116         }
00117 }
00118 
00119 
00120  
00121 int main(void)
00122 {
00123  
00124     np_api_init();
00125     app_setup();
00126     np_api_start();
00127     while(1) {
00128         
00129         Thread::wait(LOOP_DELAY);
00130         app_loop();
00131         np_api_bsl_chk();
00132         
00133     }
00134  
00135     return 0;
00136 }