Ray Liu
/
NuMaker-mbed-NuBrick-example
Modify the file main.cpp for M487
main.cpp@0:a67fc999dd68, 2017-09-29 (annotated)
- Committer:
- shliu1
- Date:
- Fri Sep 29 05:44:02 2017 +0000
- Revision:
- 0:a67fc999dd68
main.cpp adds the setting of TARGET_NUMAKER_PFM_M487 for M487
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shliu1 | 0:a67fc999dd68 | 1 | /* mbed Microcontroller Library |
shliu1 | 0:a67fc999dd68 | 2 | * Copyright (c) 2016 ARM Limited |
shliu1 | 0:a67fc999dd68 | 3 | * |
shliu1 | 0:a67fc999dd68 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
shliu1 | 0:a67fc999dd68 | 5 | * you may not use this file except in compliance with the License. |
shliu1 | 0:a67fc999dd68 | 6 | * You may obtain a copy of the License at |
shliu1 | 0:a67fc999dd68 | 7 | * |
shliu1 | 0:a67fc999dd68 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
shliu1 | 0:a67fc999dd68 | 9 | * |
shliu1 | 0:a67fc999dd68 | 10 | * Unless required by applicable law or agreed to in writing, software |
shliu1 | 0:a67fc999dd68 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
shliu1 | 0:a67fc999dd68 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
shliu1 | 0:a67fc999dd68 | 13 | * See the License for the specific language governing permissions and |
shliu1 | 0:a67fc999dd68 | 14 | * limitations under the License. |
shliu1 | 0:a67fc999dd68 | 15 | */ |
shliu1 | 0:a67fc999dd68 | 16 | |
shliu1 | 0:a67fc999dd68 | 17 | #include "mbed.h" |
shliu1 | 0:a67fc999dd68 | 18 | #include "nubrick.h" |
shliu1 | 0:a67fc999dd68 | 19 | |
shliu1 | 0:a67fc999dd68 | 20 | #if defined(TARGET_NUMAKER_PFM_NUC472) |
shliu1 | 0:a67fc999dd68 | 21 | I2C i2c(D14, D15); |
shliu1 | 0:a67fc999dd68 | 22 | |
shliu1 | 0:a67fc999dd68 | 23 | #elif defined(TARGET_NUMAKER_PFM_M453) |
shliu1 | 0:a67fc999dd68 | 24 | I2C i2c(D14, D15); |
shliu1 | 0:a67fc999dd68 | 25 | |
shliu1 | 0:a67fc999dd68 | 26 | #elif defined(TARGET_NUMAKER_PFM_M487) |
shliu1 | 0:a67fc999dd68 | 27 | I2C i2c(D14, D15); // in M487 D14 is SDA, D15 is SCL |
shliu1 | 0:a67fc999dd68 | 28 | #endif |
shliu1 | 0:a67fc999dd68 | 29 | |
shliu1 | 0:a67fc999dd68 | 30 | /** Connect to one NuBrick slave via NuBrick master object |
shliu1 | 0:a67fc999dd68 | 31 | */ |
shliu1 | 0:a67fc999dd68 | 32 | #define NUBRICK_CONNECT(MASTER, NAME) \ |
shliu1 | 0:a67fc999dd68 | 33 | do { \ |
shliu1 | 0:a67fc999dd68 | 34 | printf("\r\n\r\n"); \ |
shliu1 | 0:a67fc999dd68 | 35 | if (! MASTER.connect()) { \ |
shliu1 | 0:a67fc999dd68 | 36 | printf("Connect to NuBrick:\t\t"NAME" failed\r\n\r\n"); \ |
shliu1 | 0:a67fc999dd68 | 37 | return; \ |
shliu1 | 0:a67fc999dd68 | 38 | } \ |
shliu1 | 0:a67fc999dd68 | 39 | else { \ |
shliu1 | 0:a67fc999dd68 | 40 | printf("Connect to NuBrick:\t\t"NAME" OK\r\n\r\n"); \ |
shliu1 | 0:a67fc999dd68 | 41 | MASTER.print_device_desc(); \ |
shliu1 | 0:a67fc999dd68 | 42 | } \ |
shliu1 | 0:a67fc999dd68 | 43 | } while (0); |
shliu1 | 0:a67fc999dd68 | 44 | |
shliu1 | 0:a67fc999dd68 | 45 | /** Define NuBrick master objects to communicate with NuBrick slave devices |
shliu1 | 0:a67fc999dd68 | 46 | */ |
shliu1 | 0:a67fc999dd68 | 47 | NuBrickMasterBuzzer master_buzzer(i2c, true); |
shliu1 | 0:a67fc999dd68 | 48 | NuBrickMasterLED master_led(i2c, true); |
shliu1 | 0:a67fc999dd68 | 49 | NuBrickMasterAHRS master_ahrs(i2c, true); |
shliu1 | 0:a67fc999dd68 | 50 | NuBrickMasterSonar master_sonar(i2c, true); |
shliu1 | 0:a67fc999dd68 | 51 | NuBrickMasterTemp master_temp(i2c, true); |
shliu1 | 0:a67fc999dd68 | 52 | NuBrickMasterGas master_gas(i2c, true); |
shliu1 | 0:a67fc999dd68 | 53 | NuBrickMasterIR master_ir(i2c, true); |
shliu1 | 0:a67fc999dd68 | 54 | NuBrickMasterKeys master_keys(i2c, true); |
shliu1 | 0:a67fc999dd68 | 55 | |
shliu1 | 0:a67fc999dd68 | 56 | /** Test NuBrick slave devices |
shliu1 | 0:a67fc999dd68 | 57 | */ |
shliu1 | 0:a67fc999dd68 | 58 | void test_nubrick_buzzer(void); |
shliu1 | 0:a67fc999dd68 | 59 | void test_nubrick_led(void); |
shliu1 | 0:a67fc999dd68 | 60 | void test_nubrick_ahrs(void); |
shliu1 | 0:a67fc999dd68 | 61 | void test_nubrick_sonar(void); |
shliu1 | 0:a67fc999dd68 | 62 | void test_nubrick_temp(void); |
shliu1 | 0:a67fc999dd68 | 63 | void test_nubrick_gas(void); |
shliu1 | 0:a67fc999dd68 | 64 | void test_nubrick_ir(void); |
shliu1 | 0:a67fc999dd68 | 65 | void test_nubrick_keys(void); |
shliu1 | 0:a67fc999dd68 | 66 | |
shliu1 | 0:a67fc999dd68 | 67 | int main() { |
shliu1 | 0:a67fc999dd68 | 68 | |
shliu1 | 0:a67fc999dd68 | 69 | // Test all supported NuBrick slave devices |
shliu1 | 0:a67fc999dd68 | 70 | test_nubrick_buzzer(); |
shliu1 | 0:a67fc999dd68 | 71 | test_nubrick_led(); |
shliu1 | 0:a67fc999dd68 | 72 | test_nubrick_ahrs(); |
shliu1 | 0:a67fc999dd68 | 73 | test_nubrick_sonar(); |
shliu1 | 0:a67fc999dd68 | 74 | test_nubrick_temp(); |
shliu1 | 0:a67fc999dd68 | 75 | test_nubrick_gas(); |
shliu1 | 0:a67fc999dd68 | 76 | test_nubrick_ir(); |
shliu1 | 0:a67fc999dd68 | 77 | test_nubrick_keys(); |
shliu1 | 0:a67fc999dd68 | 78 | |
shliu1 | 0:a67fc999dd68 | 79 | return 0; |
shliu1 | 0:a67fc999dd68 | 80 | } |
shliu1 | 0:a67fc999dd68 | 81 | |
shliu1 | 0:a67fc999dd68 | 82 | void test_nubrick_buzzer(void) { |
shliu1 | 0:a67fc999dd68 | 83 | |
shliu1 | 0:a67fc999dd68 | 84 | NUBRICK_CONNECT(master_buzzer, "Buzzer"); |
shliu1 | 0:a67fc999dd68 | 85 | |
shliu1 | 0:a67fc999dd68 | 86 | // Configure the Buzzer |
shliu1 | 0:a67fc999dd68 | 87 | master_buzzer.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 88 | master_buzzer["feature.sleep_period"].set_value(100); |
shliu1 | 0:a67fc999dd68 | 89 | master_buzzer["feature.volume"].set_value(60); // Volume in % |
shliu1 | 0:a67fc999dd68 | 90 | master_buzzer["feature.tone"].set_value(196); // Tone in Hz |
shliu1 | 0:a67fc999dd68 | 91 | master_buzzer["feature.song"].set_value(0); // 0 (mono), 1 (Bee) |
shliu1 | 0:a67fc999dd68 | 92 | master_buzzer["feature.period"].set_value(200); // Period in ms |
shliu1 | 0:a67fc999dd68 | 93 | master_buzzer["feature.duty"].set_value(30); // Duty in % |
shliu1 | 0:a67fc999dd68 | 94 | master_buzzer["feature.latency"].set_value(3); // Alarm for time secs |
shliu1 | 0:a67fc999dd68 | 95 | master_buzzer.push_feature_report(); |
shliu1 | 0:a67fc999dd68 | 96 | |
shliu1 | 0:a67fc999dd68 | 97 | // The NuBrick I2C device may not respond in time. Add delay here. |
shliu1 | 0:a67fc999dd68 | 98 | wait_ms(50); |
shliu1 | 0:a67fc999dd68 | 99 | |
shliu1 | 0:a67fc999dd68 | 100 | // Start sounding the buzzer |
shliu1 | 0:a67fc999dd68 | 101 | master_buzzer["output.start_flag"].set_value(1); |
shliu1 | 0:a67fc999dd68 | 102 | master_buzzer["output.stop_flag"].set_value(0); |
shliu1 | 0:a67fc999dd68 | 103 | master_buzzer.push_output_report(); |
shliu1 | 0:a67fc999dd68 | 104 | } |
shliu1 | 0:a67fc999dd68 | 105 | |
shliu1 | 0:a67fc999dd68 | 106 | void test_nubrick_led(void) { |
shliu1 | 0:a67fc999dd68 | 107 | |
shliu1 | 0:a67fc999dd68 | 108 | NUBRICK_CONNECT(master_led, "LED"); |
shliu1 | 0:a67fc999dd68 | 109 | |
shliu1 | 0:a67fc999dd68 | 110 | // Configure the LED |
shliu1 | 0:a67fc999dd68 | 111 | master_led.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 112 | master_led["feature.sleep_period"].set_value(100); |
shliu1 | 0:a67fc999dd68 | 113 | master_led["feature.brightness"].set_value(30); // Brightness in % |
shliu1 | 0:a67fc999dd68 | 114 | master_led["feature.color"].set_value(0xF0); // 0x0F: full blue, 0xF0: full green, 0x0F00: full red |
shliu1 | 0:a67fc999dd68 | 115 | master_led["feature.blink"].set_value(0); // Blink method: 0: blink to setting, 1: blink to the song Bee |
shliu1 | 0:a67fc999dd68 | 116 | master_led["feature.period"].set_value(500); // Blink period in ms |
shliu1 | 0:a67fc999dd68 | 117 | master_led["feature.duty"].set_value(30); // Blink duty in % |
shliu1 | 0:a67fc999dd68 | 118 | master_led["feature.latency"].set_value(1); // Blink for time in secs |
shliu1 | 0:a67fc999dd68 | 119 | master_led.push_feature_report(); |
shliu1 | 0:a67fc999dd68 | 120 | |
shliu1 | 0:a67fc999dd68 | 121 | // The NuBrick I2C device may not respond in time. Add delay here. |
shliu1 | 0:a67fc999dd68 | 122 | wait_ms(50); |
shliu1 | 0:a67fc999dd68 | 123 | |
shliu1 | 0:a67fc999dd68 | 124 | // Start blinking the LED |
shliu1 | 0:a67fc999dd68 | 125 | master_led["output.start_flag"].set_value(1); |
shliu1 | 0:a67fc999dd68 | 126 | master_led["output.stop_flag"].set_value(0); |
shliu1 | 0:a67fc999dd68 | 127 | master_led.push_output_report(); |
shliu1 | 0:a67fc999dd68 | 128 | } |
shliu1 | 0:a67fc999dd68 | 129 | |
shliu1 | 0:a67fc999dd68 | 130 | void test_nubrick_ahrs(void) { |
shliu1 | 0:a67fc999dd68 | 131 | |
shliu1 | 0:a67fc999dd68 | 132 | NUBRICK_CONNECT(master_ahrs, "AHRS"); |
shliu1 | 0:a67fc999dd68 | 133 | |
shliu1 | 0:a67fc999dd68 | 134 | master_ahrs.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 135 | // Prescaled vibration alarm threshold |
shliu1 | 0:a67fc999dd68 | 136 | printf("Prescaled vibration alarm threshold\t\t%d\r\n", master_ahrs["feature.pre_vibration_AT"].get_value()); |
shliu1 | 0:a67fc999dd68 | 137 | |
shliu1 | 0:a67fc999dd68 | 138 | master_ahrs.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 139 | // Detected vibration |
shliu1 | 0:a67fc999dd68 | 140 | printf("Detected vibration\t\t\t\t%d\r\n", master_ahrs["input.vibration"].get_value()); |
shliu1 | 0:a67fc999dd68 | 141 | } |
shliu1 | 0:a67fc999dd68 | 142 | |
shliu1 | 0:a67fc999dd68 | 143 | void test_nubrick_sonar(void) { |
shliu1 | 0:a67fc999dd68 | 144 | |
shliu1 | 0:a67fc999dd68 | 145 | NUBRICK_CONNECT(master_sonar, "Sonar"); |
shliu1 | 0:a67fc999dd68 | 146 | |
shliu1 | 0:a67fc999dd68 | 147 | master_sonar.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 148 | // Distance alarm threshold in cm |
shliu1 | 0:a67fc999dd68 | 149 | printf("Distance alarm threshold\t\t%d\r\n", master_sonar["feature.distance_AT"].get_value()); |
shliu1 | 0:a67fc999dd68 | 150 | |
shliu1 | 0:a67fc999dd68 | 151 | master_sonar.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 152 | // Detected distance in cm |
shliu1 | 0:a67fc999dd68 | 153 | printf("Detected distance\t\t\t%d\r\n", master_sonar["input.distance"].get_value()); |
shliu1 | 0:a67fc999dd68 | 154 | } |
shliu1 | 0:a67fc999dd68 | 155 | |
shliu1 | 0:a67fc999dd68 | 156 | void test_nubrick_temp(void) { |
shliu1 | 0:a67fc999dd68 | 157 | |
shliu1 | 0:a67fc999dd68 | 158 | NUBRICK_CONNECT(master_temp, "Temperature & Humidity"); |
shliu1 | 0:a67fc999dd68 | 159 | |
shliu1 | 0:a67fc999dd68 | 160 | master_temp.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 161 | // Temp. alarm threshold in Celsius |
shliu1 | 0:a67fc999dd68 | 162 | printf("Temp. alarm threshold\t\t%d\r\n", master_temp["feature.temp_AT"].get_value()); |
shliu1 | 0:a67fc999dd68 | 163 | // Hum. alarm threshold in % |
shliu1 | 0:a67fc999dd68 | 164 | printf("Hum. alarm threshold\t\t%d\r\n", master_temp["feature.hum_AT"].get_value()); |
shliu1 | 0:a67fc999dd68 | 165 | |
shliu1 | 0:a67fc999dd68 | 166 | master_temp.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 167 | // Detected temp in Celsius |
shliu1 | 0:a67fc999dd68 | 168 | printf("Detected temp.\t\t\t%d\r\n", master_temp["input.temp"].get_value()); |
shliu1 | 0:a67fc999dd68 | 169 | // Detected hum. in % |
shliu1 | 0:a67fc999dd68 | 170 | printf("Detected hum.\t\t\t%d\r\n", master_temp["input.hum"].get_value()); |
shliu1 | 0:a67fc999dd68 | 171 | } |
shliu1 | 0:a67fc999dd68 | 172 | |
shliu1 | 0:a67fc999dd68 | 173 | void test_nubrick_gas(void) { |
shliu1 | 0:a67fc999dd68 | 174 | |
shliu1 | 0:a67fc999dd68 | 175 | NUBRICK_CONNECT(master_gas, "Gas"); |
shliu1 | 0:a67fc999dd68 | 176 | |
shliu1 | 0:a67fc999dd68 | 177 | master_gas.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 178 | // Gas alarm threshold in %. |
shliu1 | 0:a67fc999dd68 | 179 | printf("Gas alarm threshold\t\t%d\r\n", master_gas["feature.gas_AT"].get_value()); |
shliu1 | 0:a67fc999dd68 | 180 | |
shliu1 | 0:a67fc999dd68 | 181 | master_gas.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 182 | // Detected gas in %. 80% above for normal. |
shliu1 | 0:a67fc999dd68 | 183 | printf("Gas\t\t\t\t%d\r\n", master_gas["input.gas"].get_value()); |
shliu1 | 0:a67fc999dd68 | 184 | } |
shliu1 | 0:a67fc999dd68 | 185 | |
shliu1 | 0:a67fc999dd68 | 186 | void test_nubrick_ir(void) { |
shliu1 | 0:a67fc999dd68 | 187 | |
shliu1 | 0:a67fc999dd68 | 188 | NUBRICK_CONNECT(master_ir, "IR"); |
shliu1 | 0:a67fc999dd68 | 189 | |
shliu1 | 0:a67fc999dd68 | 190 | master_ir.pull_feature_report(); |
shliu1 | 0:a67fc999dd68 | 191 | printf("Number of learned data\t\t%d\r\n", master_ir["feature.num_learned_data"].get_value()); |
shliu1 | 0:a67fc999dd68 | 192 | printf("Using data type\t\t\t%d\r\n", master_ir["feature.using_data_type"].get_value()); |
shliu1 | 0:a67fc999dd68 | 193 | printf("Index of original data to send\t%d\r\n", master_ir["feature.index_orig_data_to_send"].get_value()); |
shliu1 | 0:a67fc999dd68 | 194 | printf("Index of learned data to send\t%d\r\n", master_ir["feature.index_learned_data_to_send"].get_value()); |
shliu1 | 0:a67fc999dd68 | 195 | |
shliu1 | 0:a67fc999dd68 | 196 | master_ir.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 197 | printf("Has received data flag\t\t%d\r\n", master_ir["input.received_data_flag"].get_value()); |
shliu1 | 0:a67fc999dd68 | 198 | } |
shliu1 | 0:a67fc999dd68 | 199 | |
shliu1 | 0:a67fc999dd68 | 200 | void test_nubrick_keys(void) { |
shliu1 | 0:a67fc999dd68 | 201 | |
shliu1 | 0:a67fc999dd68 | 202 | NUBRICK_CONNECT(master_keys, "Key"); |
shliu1 | 0:a67fc999dd68 | 203 | |
shliu1 | 0:a67fc999dd68 | 204 | // Detect 8 keys |
shliu1 | 0:a67fc999dd68 | 205 | master_keys.pull_input_report(); |
shliu1 | 0:a67fc999dd68 | 206 | uint16_t key_state = master_keys["input.key_state"].get_value(); |
shliu1 | 0:a67fc999dd68 | 207 | unsigned i = 0; |
shliu1 | 0:a67fc999dd68 | 208 | for (i = 0; i < 8; i ++) { |
shliu1 | 0:a67fc999dd68 | 209 | if (key_state & (1 << i)) { |
shliu1 | 0:a67fc999dd68 | 210 | printf("KEY%d PRESSED\r\n", i + 1); |
shliu1 | 0:a67fc999dd68 | 211 | } |
shliu1 | 0:a67fc999dd68 | 212 | else { |
shliu1 | 0:a67fc999dd68 | 213 | printf("KEY%d RELEASED\r\n", i + 1); |
shliu1 | 0:a67fc999dd68 | 214 | } |
shliu1 | 0:a67fc999dd68 | 215 | } |
shliu1 | 0:a67fc999dd68 | 216 | } |