NuMaker Brick I2C slave devices

Committer:
ccli8
Date:
Wed Nov 09 11:00:02 2016 +0800
Revision:
2:48c6a70d121c
Parent:
1:2fcc21a82c3b
Child:
7:5b6936f0c99c
Change new line characters from "\n" to "\r\n"

Who changed what in which revision?

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