Maxim nexpaq / ALS_Prox_Demo

Dependencies:   MAX44000 nexpaq_dev nexpaq_mdk

Fork of LED_Prox_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 
00005 MAX44000 max44000(P1_6, P1_7);
00006 PwmOut ledR(P2_4);
00007 PwmOut ledG(P2_5);
00008 PwmOut ledB(P2_6);
00009 DigitalIn button(P0_1, PullUp);
00010 
00011 /***** Definitions *****/
00012 #define     FUNCTION_TABLE_NUM                  2
00013 #define     UUID_NUM                            16          // UUID number is 16, don't change it
00014 #define     LOOP_DELAY                          100
00015 #define     PROX_THRESHOLD                      10
00016 #define     ALS_INTERVAL                        10          // about once per second
00017 
00018 /***** Globals *****/
00019 void my_function_CMD_2700(unsigned char *pData, unsigned char len);
00020 void my_function_CMD_2702(unsigned char *pData, unsigned char len);
00021 const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = {
00022     {0x2700, my_function_CMD_2700},     // Command -> function
00023     {0x2702, my_function_CMD_2702},     // Command -> function
00024 };
00025 
00026 int alsCnt = 0;
00027 int prxThrsh = PROX_THRESHOLD ;
00028 int lastPrx = 0;
00029 unsigned char prxPress = 0x02;
00030 int lastBtn = 1;
00031 unsigned char btnPress = 0x01;
00032 
00033 /***** Functions *****/
00034 void my_function_CMD_2700(unsigned char *pData, unsigned char len)
00035 {
00036     unsigned char response = 0x00;
00037     ledR = 1.0f - (pData[0] / 255.0f);
00038     ledG = 1.0f - (pData[1] / 255.0f);
00039     ledB = 1.0f - (pData[2] / 255.0f);
00040     np_api_upload(0x2701, &response, 1);
00041 }
00042 
00043 void my_function_CMD_2702(unsigned char *pData, unsigned char len)
00044 {
00045     unsigned char response = 0x00;
00046     prxThrsh = pData[0] ;
00047     np_api_upload(0x2703, &response, 1);
00048 }
00049 
00050 void sendALS()
00051 {
00052     unsigned char pData[3];
00053     max44000.writeReg(MAX44000::REG_MAIN_CONFIG, 0x20);
00054     pData[0] = 10;
00055     pData[1] = max44000.readReg(MAX44000::REG_ALS_DATA_HIGH);
00056     pData[2] = max44000.readReg(MAX44000::REG_ALS_DATA_LOW);
00057     np_api_upload(0x2800, pData, 3);
00058     max44000.writeReg(MAX44000::REG_MAIN_CONFIG, 0x30);
00059 }
00060 
00061 /******************************************************************************/
00062 void app_setup()
00063 {
00064 //  np_api_set_app_version(0, 0, 3);
00065     if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) {
00066         // Register failed handle code
00067     }
00068     max44000.init(MAX44000::MODE_ALS_PROX, MAX44000::ALSTIM_64X, MAX44000::ALSPGA_1X, MAX44000::DRV_10);
00069     ledR = 1.0f;
00070     ledG = 1.0f;
00071     ledB = 1.0f;
00072 }
00073 
00074 void app_loop()
00075 {
00076     int proxData = max44000.readReg(MAX44000::REG_PRX_DATA);
00077     if (proxData > prxThrsh) {
00078         if (!lastPrx) {
00079             np_api_upload(0x2800, &prxPress, 1);
00080         }
00081         lastPrx = 1;
00082     } else {
00083         lastPrx = 0;
00084     }
00085 
00086     if (!button && lastBtn) {
00087         np_api_upload(0x2800, &btnPress, 1);
00088     }
00089     lastBtn = button;
00090 
00091     alsCnt += 1;
00092     if (alsCnt > ALS_INTERVAL) {
00093         sendALS();
00094         alsCnt = 0;
00095     }
00096 }
00097 
00098 int main(void)
00099 {
00100     np_api_init();
00101     app_setup();
00102     np_api_start();
00103     while(1) {
00104         app_loop();
00105         np_api_bsl_chk();
00106         Thread::wait(LOOP_DELAY);
00107     }
00108     return 0;
00109 }