WORKS
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
main.cpp@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 8:782d1dcdfd94
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:b86eda0e990d | 1 | #include "mbed.h" |
nexpaq | 0:b86eda0e990d | 2 | #include "nexpaq_mdk.h" |
cyberjoey | 7:04a9ea641c79 | 3 | #include "MAX44000.h" |
cyberjoey | 7:04a9ea641c79 | 4 | #include "pwm_tone.h" |
cyberjoey | 7:04a9ea641c79 | 5 | |
cyberjoey | 7:04a9ea641c79 | 6 | //MAX44000 max44000(P1_6, P1_7); |
cyberjoey | 7:04a9ea641c79 | 7 | //i2c code goes here//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cyberjoey | 7:04a9ea641c79 | 8 | I2C i2c(P1_6,P1_7); |
cyberjoey | 7:04a9ea641c79 | 9 | PwmOut ledR(P2_4); |
cyberjoey | 7:04a9ea641c79 | 10 | PwmOut ledG(P2_5); |
cyberjoey | 7:04a9ea641c79 | 11 | PwmOut ledB(P2_6); |
cyberjoey | 7:04a9ea641c79 | 12 | PwmOut Buzzer(P1_2); |
nexpaq | 0:b86eda0e990d | 13 | DigitalIn button(P0_1, PullUp); |
cyberjoey | 7:04a9ea641c79 | 14 | //DigitalOut buz(P0_0); |
nexpaq | 0:b86eda0e990d | 15 | |
cyberjoey | 7:04a9ea641c79 | 16 | //int buzzing=0; |
cyberjoey | 7:04a9ea641c79 | 17 | |
nexpaq | 0:b86eda0e990d | 18 | /***** Definitions *****/ |
cyberjoey | 7:04a9ea641c79 | 19 | #define FUNCTION_TABLE_NUM 1 |
cyberjoey | 7:04a9ea641c79 | 20 | #define UUID_NUM 16 //UUID number is 16, don't change it |
nexpaq | 4:494741f7f5b2 | 21 | #define LOOP_DELAY 100 |
cyberjoey | 7:04a9ea641c79 | 22 | #define PROX_THRESHOLD 50 |
cyberjoey | 7:04a9ea641c79 | 23 | |
nexpaq | 0:b86eda0e990d | 24 | /***** Globals *****/ |
nexpaq | 0:b86eda0e990d | 25 | void my_function_CMD_2700(unsigned char *pData, unsigned char len); |
nexpaq | 0:b86eda0e990d | 26 | const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = { |
cyberjoey | 7:04a9ea641c79 | 27 | {0x2700, my_function_CMD_2700}, // Command -> function |
nexpaq | 0:b86eda0e990d | 28 | }; |
cyberjoey | 8:782d1dcdfd94 | 29 | int buzzing=0; |
cyberjoey | 7:04a9ea641c79 | 30 | //int lastPrx = 0; |
cyberjoey | 7:04a9ea641c79 | 31 | int toggle = 0; |
cyberjoey | 7:04a9ea641c79 | 32 | unsigned char prxPress = 0x02; |
gsteiert | 3:9d15891f9352 | 33 | int lastBtn = 1; |
nexpaq | 0:b86eda0e990d | 34 | unsigned char btnPress = 0x01; |
cyberjoey | 7:04a9ea641c79 | 35 | |
nexpaq | 0:b86eda0e990d | 36 | /***** Functions *****/ |
nexpaq | 6:7b51d2ebef23 | 37 | void my_function_CMD_2700(unsigned char *pData, unsigned char len) |
nexpaq | 6:7b51d2ebef23 | 38 | { |
nexpaq | 6:7b51d2ebef23 | 39 | unsigned char response = 0x00; |
cyberjoey | 7:04a9ea641c79 | 40 | ledR = 1.0f - (pData[0] / 255.0f); |
cyberjoey | 7:04a9ea641c79 | 41 | ledG = 1.0f - (pData[1] / 255.0f); |
cyberjoey | 7:04a9ea641c79 | 42 | ledB = 1.0f - (pData[2] / 255.0f); |
cyberjoey | 8:782d1dcdfd94 | 43 | toggle = pData[0]; |
nexpaq | 6:7b51d2ebef23 | 44 | np_api_upload(0x2701, &response, 1); |
nexpaq | 0:b86eda0e990d | 45 | } |
nexpaq | 0:b86eda0e990d | 46 | |
cyberjoey | 8:782d1dcdfd94 | 47 | int i2c_read_gesture(char gestReg) |
cyberjoey | 7:04a9ea641c79 | 48 | { |
cyberjoey | 7:04a9ea641c79 | 49 | char readVal; |
cyberjoey | 8:782d1dcdfd94 | 50 | //char gestReg; |
cyberjoey | 7:04a9ea641c79 | 51 | |
cyberjoey | 8:782d1dcdfd94 | 52 | //gestReg = 0x04; |
cyberjoey | 7:04a9ea641c79 | 53 | |
cyberjoey | 7:04a9ea641c79 | 54 | i2c.write( 0x20, &gestReg, 1 ); |
cyberjoey | 7:04a9ea641c79 | 55 | i2c.read( 0x20, &readVal, 1 ); |
cyberjoey | 7:04a9ea641c79 | 56 | |
cyberjoey | 7:04a9ea641c79 | 57 | //perform cast here |
cyberjoey | 7:04a9ea641c79 | 58 | return( readVal ); |
cyberjoey | 7:04a9ea641c79 | 59 | } |
cyberjoey | 7:04a9ea641c79 | 60 | |
nexpaq | 0:b86eda0e990d | 61 | /******************************************************************************/ |
nexpaq | 6:7b51d2ebef23 | 62 | void app_setup() |
nexpaq | 6:7b51d2ebef23 | 63 | { |
nexpaq | 6:7b51d2ebef23 | 64 | if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) { |
nexpaq | 6:7b51d2ebef23 | 65 | // Register failed handle code |
nexpaq | 6:7b51d2ebef23 | 66 | error("MDK Register Failed"); |
nexpaq | 6:7b51d2ebef23 | 67 | } |
cyberjoey | 7:04a9ea641c79 | 68 | //max44000.init(MAX44000::MODE_ALS_PROX, MAX44000::ALSTIM_64X, MAX44000::ALSPGA_1X, MAX44000::DRV_110); |
cyberjoey | 7:04a9ea641c79 | 69 | ledR = 1.0f; |
cyberjoey | 7:04a9ea641c79 | 70 | ledG = 1.0f; |
cyberjoey | 7:04a9ea641c79 | 71 | ledB = 1.0f; |
cyberjoey | 7:04a9ea641c79 | 72 | |
cyberjoey | 7:04a9ea641c79 | 73 | //buz = 0; |
nexpaq | 0:b86eda0e990d | 74 | } |
cyberjoey | 7:04a9ea641c79 | 75 | |
nexpaq | 6:7b51d2ebef23 | 76 | void app_loop() |
nexpaq | 6:7b51d2ebef23 | 77 | { |
cyberjoey | 7:04a9ea641c79 | 78 | float A_4 = 1000000/Do4; |
cyberjoey | 7:04a9ea641c79 | 79 | //int proxData = max44000.readReg(MAX44000::REG_PRX_DATA); |
cyberjoey | 8:782d1dcdfd94 | 80 | int gesture = i2c_read_gesture(0x04); |
cyberjoey | 8:782d1dcdfd94 | 81 | |
cyberjoey | 8:782d1dcdfd94 | 82 | if(!buzzing){ |
cyberjoey | 7:04a9ea641c79 | 83 | if (gesture == 0x01) { |
cyberjoey | 7:04a9ea641c79 | 84 | if (!toggle) { |
cyberjoey | 7:04a9ea641c79 | 85 | np_api_upload(0x2800, &prxPress, 1); |
cyberjoey | 8:782d1dcdfd94 | 86 | //int dummy = i2c_read_gesture(0x0a); |
cyberjoey | 7:04a9ea641c79 | 87 | //Stop_tunes(Buzzer); |
cyberjoey | 7:04a9ea641c79 | 88 | } |
cyberjoey | 7:04a9ea641c79 | 89 | toggle = 1; |
cyberjoey | 7:04a9ea641c79 | 90 | |
cyberjoey | 7:04a9ea641c79 | 91 | } else if (gesture == 0x02) { |
cyberjoey | 7:04a9ea641c79 | 92 | if(toggle){ |
cyberjoey | 7:04a9ea641c79 | 93 | np_api_upload(0x2800, &prxPress, 1); |
cyberjoey | 7:04a9ea641c79 | 94 | } |
cyberjoey | 7:04a9ea641c79 | 95 | toggle = 0;} |
cyberjoey | 7:04a9ea641c79 | 96 | |
cyberjoey | 8:782d1dcdfd94 | 97 | if(toggle){ |
cyberjoey | 8:782d1dcdfd94 | 98 | int distance = i2c_read_gesture(0x0a); |
cyberjoey | 8:782d1dcdfd94 | 99 | if(distance<60){ |
cyberjoey | 8:782d1dcdfd94 | 100 | buzzing =1;} |
cyberjoey | 8:782d1dcdfd94 | 101 | } |
cyberjoey | 8:782d1dcdfd94 | 102 | |
cyberjoey | 7:04a9ea641c79 | 103 | |
nexpaq | 6:7b51d2ebef23 | 104 | if (!button && lastBtn) { |
gsteiert | 3:9d15891f9352 | 105 | np_api_upload(0x2800, &btnPress, 1); |
nexpaq | 6:7b51d2ebef23 | 106 | } |
cyberjoey | 8:782d1dcdfd94 | 107 | } |
nexpaq | 6:7b51d2ebef23 | 108 | lastBtn = button; |
cyberjoey | 8:782d1dcdfd94 | 109 | if(buzzing){ |
cyberjoey | 8:782d1dcdfd94 | 110 | Tune(Buzzer, A_4, 4); //4 Octave C beat 4/16 |
cyberjoey | 8:782d1dcdfd94 | 111 | } |
cyberjoey | 8:782d1dcdfd94 | 112 | if(buzzing &&!toggle){ |
cyberjoey | 8:782d1dcdfd94 | 113 | buzzing=0; |
cyberjoey | 8:782d1dcdfd94 | 114 | //np_api_upload(0x2800, &prxPress, 1); |
cyberjoey | 8:782d1dcdfd94 | 115 | //toggle = 1; |
cyberjoey | 8:782d1dcdfd94 | 116 | } |
nexpaq | 0:b86eda0e990d | 117 | } |
nexpaq | 0:b86eda0e990d | 118 | |
cyberjoey | 7:04a9ea641c79 | 119 | |
cyberjoey | 7:04a9ea641c79 | 120 | |
nexpaq | 6:7b51d2ebef23 | 121 | int main(void) |
nexpaq | 6:7b51d2ebef23 | 122 | { |
cyberjoey | 7:04a9ea641c79 | 123 | |
nexpaq | 6:7b51d2ebef23 | 124 | np_api_init(); |
nexpaq | 6:7b51d2ebef23 | 125 | app_setup(); |
nexpaq | 6:7b51d2ebef23 | 126 | np_api_start(); |
nexpaq | 6:7b51d2ebef23 | 127 | while(1) { |
cyberjoey | 7:04a9ea641c79 | 128 | |
nexpaq | 6:7b51d2ebef23 | 129 | Thread::wait(LOOP_DELAY); |
nexpaq | 6:7b51d2ebef23 | 130 | app_loop(); |
nexpaq | 6:7b51d2ebef23 | 131 | np_api_bsl_chk(); |
cyberjoey | 7:04a9ea641c79 | 132 | |
nexpaq | 6:7b51d2ebef23 | 133 | } |
cyberjoey | 7:04a9ea641c79 | 134 | |
nexpaq | 6:7b51d2ebef23 | 135 | return 0; |
cyberjoey | 7:04a9ea641c79 | 136 | } |