NuMaker Brick I2C slave devices

Committer:
ccchang
Date:
Tue Oct 17 14:05:55 2017 +0800
Revision:
8:b60e846cae6a
Parent:
7:5b6936f0c99c
Child:
12:92db8dd90705
remove the multiplu definitions, all targets use the same source

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 I2C i2c(D14, D15);
ccli8 1:2fcc21a82c3b 21
ccli8 1:2fcc21a82c3b 22 /** Connect to one NuBrick slave via NuBrick master object
ccli8 1:2fcc21a82c3b 23 */
ccli8 2:48c6a70d121c 24 #define NUBRICK_CONNECT(MASTER, NAME) \
ccli8 2:48c6a70d121c 25 do { \
ccli8 2:48c6a70d121c 26 printf("\r\n\r\n"); \
ccli8 2:48c6a70d121c 27 if (! MASTER.connect()) { \
ccli8 2:48c6a70d121c 28 printf("Connect to NuBrick:\t\t"NAME" failed\r\n\r\n"); \
ccli8 2:48c6a70d121c 29 return; \
ccli8 2:48c6a70d121c 30 } \
ccli8 2:48c6a70d121c 31 else { \
ccli8 2:48c6a70d121c 32 printf("Connect to NuBrick:\t\t"NAME" OK\r\n\r\n"); \
ccli8 2:48c6a70d121c 33 MASTER.print_device_desc(); \
ccli8 2:48c6a70d121c 34 } \
ccli8 1:2fcc21a82c3b 35 } while (0);
ccli8 1:2fcc21a82c3b 36
ccli8 1:2fcc21a82c3b 37 /** Define NuBrick master objects to communicate with NuBrick slave devices
ccli8 1:2fcc21a82c3b 38 */
ccli8 1:2fcc21a82c3b 39 NuBrickMasterBuzzer master_buzzer(i2c, true);
ccli8 1:2fcc21a82c3b 40 NuBrickMasterLED master_led(i2c, true);
ccli8 1:2fcc21a82c3b 41 NuBrickMasterAHRS master_ahrs(i2c, true);
ccli8 1:2fcc21a82c3b 42 NuBrickMasterSonar master_sonar(i2c, true);
ccli8 1:2fcc21a82c3b 43 NuBrickMasterTemp master_temp(i2c, true);
ccli8 1:2fcc21a82c3b 44 NuBrickMasterGas master_gas(i2c, true);
ccli8 1:2fcc21a82c3b 45 NuBrickMasterIR master_ir(i2c, true);
ccli8 1:2fcc21a82c3b 46 NuBrickMasterKeys master_keys(i2c, true);
ccli8 1:2fcc21a82c3b 47
ccli8 1:2fcc21a82c3b 48 /** Test NuBrick slave devices
ccli8 1:2fcc21a82c3b 49 */
ccli8 1:2fcc21a82c3b 50 void test_nubrick_buzzer(void);
ccli8 1:2fcc21a82c3b 51 void test_nubrick_led(void);
ccli8 1:2fcc21a82c3b 52 void test_nubrick_ahrs(void);
ccli8 1:2fcc21a82c3b 53 void test_nubrick_sonar(void);
ccli8 1:2fcc21a82c3b 54 void test_nubrick_temp(void);
ccli8 1:2fcc21a82c3b 55 void test_nubrick_gas(void);
ccli8 1:2fcc21a82c3b 56 void test_nubrick_ir(void);
ccli8 1:2fcc21a82c3b 57 void test_nubrick_keys(void);
ccli8 1:2fcc21a82c3b 58
ccli8 1:2fcc21a82c3b 59 int main() {
ccli8 1:2fcc21a82c3b 60
ccli8 1:2fcc21a82c3b 61 // Test all supported NuBrick slave devices
ccli8 1:2fcc21a82c3b 62 test_nubrick_buzzer();
ccli8 1:2fcc21a82c3b 63 test_nubrick_led();
ccli8 1:2fcc21a82c3b 64 test_nubrick_ahrs();
ccli8 1:2fcc21a82c3b 65 test_nubrick_sonar();
ccli8 1:2fcc21a82c3b 66 test_nubrick_temp();
ccli8 1:2fcc21a82c3b 67 test_nubrick_gas();
ccli8 1:2fcc21a82c3b 68 test_nubrick_ir();
ccli8 1:2fcc21a82c3b 69 test_nubrick_keys();
ccli8 1:2fcc21a82c3b 70
ccli8 1:2fcc21a82c3b 71 return 0;
ccli8 1:2fcc21a82c3b 72 }
ccli8 1:2fcc21a82c3b 73
ccli8 1:2fcc21a82c3b 74 void test_nubrick_buzzer(void) {
ccli8 1:2fcc21a82c3b 75
ccli8 1:2fcc21a82c3b 76 NUBRICK_CONNECT(master_buzzer, "Buzzer");
ccli8 1:2fcc21a82c3b 77
ccli8 1:2fcc21a82c3b 78 // Configure the Buzzer
ccli8 1:2fcc21a82c3b 79 master_buzzer.pull_feature_report();
ccli8 1:2fcc21a82c3b 80 master_buzzer["feature.sleep_period"].set_value(100);
ccli8 1:2fcc21a82c3b 81 master_buzzer["feature.volume"].set_value(60); // Volume in %
ccli8 1:2fcc21a82c3b 82 master_buzzer["feature.tone"].set_value(196); // Tone in Hz
ccli8 1:2fcc21a82c3b 83 master_buzzer["feature.song"].set_value(0); // 0 (mono), 1 (Bee)
ccli8 1:2fcc21a82c3b 84 master_buzzer["feature.period"].set_value(200); // Period in ms
ccli8 1:2fcc21a82c3b 85 master_buzzer["feature.duty"].set_value(30); // Duty in %
ccli8 1:2fcc21a82c3b 86 master_buzzer["feature.latency"].set_value(3); // Alarm for time secs
ccli8 1:2fcc21a82c3b 87 master_buzzer.push_feature_report();
ccli8 1:2fcc21a82c3b 88
ccli8 1:2fcc21a82c3b 89 // The NuBrick I2C device may not respond in time. Add delay here.
ccli8 1:2fcc21a82c3b 90 wait_ms(50);
ccli8 1:2fcc21a82c3b 91
ccli8 1:2fcc21a82c3b 92 // Start sounding the buzzer
ccli8 1:2fcc21a82c3b 93 master_buzzer["output.start_flag"].set_value(1);
ccli8 1:2fcc21a82c3b 94 master_buzzer["output.stop_flag"].set_value(0);
ccli8 1:2fcc21a82c3b 95 master_buzzer.push_output_report();
ccli8 1:2fcc21a82c3b 96 }
ccli8 1:2fcc21a82c3b 97
ccli8 1:2fcc21a82c3b 98 void test_nubrick_led(void) {
ccli8 1:2fcc21a82c3b 99
ccli8 1:2fcc21a82c3b 100 NUBRICK_CONNECT(master_led, "LED");
ccli8 1:2fcc21a82c3b 101
ccli8 1:2fcc21a82c3b 102 // Configure the LED
ccli8 1:2fcc21a82c3b 103 master_led.pull_feature_report();
ccli8 1:2fcc21a82c3b 104 master_led["feature.sleep_period"].set_value(100);
ccli8 1:2fcc21a82c3b 105 master_led["feature.brightness"].set_value(30); // Brightness in %
ccli8 1:2fcc21a82c3b 106 master_led["feature.color"].set_value(0xF0); // 0x0F: full blue, 0xF0: full green, 0x0F00: full red
ccli8 1:2fcc21a82c3b 107 master_led["feature.blink"].set_value(0); // Blink method: 0: blink to setting, 1: blink to the song Bee
ccli8 1:2fcc21a82c3b 108 master_led["feature.period"].set_value(500); // Blink period in ms
ccli8 1:2fcc21a82c3b 109 master_led["feature.duty"].set_value(30); // Blink duty in %
ccli8 1:2fcc21a82c3b 110 master_led["feature.latency"].set_value(1); // Blink for time in secs
ccli8 1:2fcc21a82c3b 111 master_led.push_feature_report();
ccli8 1:2fcc21a82c3b 112
ccli8 1:2fcc21a82c3b 113 // The NuBrick I2C device may not respond in time. Add delay here.
ccli8 1:2fcc21a82c3b 114 wait_ms(50);
ccli8 1:2fcc21a82c3b 115
ccli8 1:2fcc21a82c3b 116 // Start blinking the LED
ccli8 1:2fcc21a82c3b 117 master_led["output.start_flag"].set_value(1);
ccli8 1:2fcc21a82c3b 118 master_led["output.stop_flag"].set_value(0);
ccli8 1:2fcc21a82c3b 119 master_led.push_output_report();
ccli8 1:2fcc21a82c3b 120 }
ccli8 1:2fcc21a82c3b 121
ccli8 1:2fcc21a82c3b 122 void test_nubrick_ahrs(void) {
ccli8 1:2fcc21a82c3b 123
ccli8 1:2fcc21a82c3b 124 NUBRICK_CONNECT(master_ahrs, "AHRS");
ccli8 1:2fcc21a82c3b 125
ccli8 1:2fcc21a82c3b 126 master_ahrs.pull_feature_report();
ccli8 1:2fcc21a82c3b 127 // Prescaled vibration alarm threshold
ccli8 2:48c6a70d121c 128 printf("Prescaled vibration alarm threshold\t\t%d\r\n", master_ahrs["feature.pre_vibration_AT"].get_value());
ccli8 1:2fcc21a82c3b 129
ccli8 1:2fcc21a82c3b 130 master_ahrs.pull_input_report();
ccli8 1:2fcc21a82c3b 131 // Detected vibration
ccli8 2:48c6a70d121c 132 printf("Detected vibration\t\t\t\t%d\r\n", master_ahrs["input.vibration"].get_value());
ccli8 1:2fcc21a82c3b 133 }
ccli8 1:2fcc21a82c3b 134
ccli8 1:2fcc21a82c3b 135 void test_nubrick_sonar(void) {
ccli8 1:2fcc21a82c3b 136
ccli8 1:2fcc21a82c3b 137 NUBRICK_CONNECT(master_sonar, "Sonar");
ccli8 1:2fcc21a82c3b 138
ccli8 1:2fcc21a82c3b 139 master_sonar.pull_feature_report();
ccli8 1:2fcc21a82c3b 140 // Distance alarm threshold in cm
ccli8 2:48c6a70d121c 141 printf("Distance alarm threshold\t\t%d\r\n", master_sonar["feature.distance_AT"].get_value());
ccli8 1:2fcc21a82c3b 142
ccli8 1:2fcc21a82c3b 143 master_sonar.pull_input_report();
ccli8 1:2fcc21a82c3b 144 // Detected distance in cm
ccli8 2:48c6a70d121c 145 printf("Detected distance\t\t\t%d\r\n", master_sonar["input.distance"].get_value());
ccli8 1:2fcc21a82c3b 146 }
ccli8 1:2fcc21a82c3b 147
ccli8 1:2fcc21a82c3b 148 void test_nubrick_temp(void) {
ccli8 1:2fcc21a82c3b 149
ccli8 1:2fcc21a82c3b 150 NUBRICK_CONNECT(master_temp, "Temperature & Humidity");
ccli8 1:2fcc21a82c3b 151
ccli8 1:2fcc21a82c3b 152 master_temp.pull_feature_report();
ccli8 1:2fcc21a82c3b 153 // Temp. alarm threshold in Celsius
ccli8 2:48c6a70d121c 154 printf("Temp. alarm threshold\t\t%d\r\n", master_temp["feature.temp_AT"].get_value());
ccli8 1:2fcc21a82c3b 155 // Hum. alarm threshold in %
ccli8 2:48c6a70d121c 156 printf("Hum. alarm threshold\t\t%d\r\n", master_temp["feature.hum_AT"].get_value());
ccli8 1:2fcc21a82c3b 157
ccli8 1:2fcc21a82c3b 158 master_temp.pull_input_report();
ccli8 1:2fcc21a82c3b 159 // Detected temp in Celsius
ccli8 2:48c6a70d121c 160 printf("Detected temp.\t\t\t%d\r\n", master_temp["input.temp"].get_value());
ccli8 1:2fcc21a82c3b 161 // Detected hum. in %
ccli8 2:48c6a70d121c 162 printf("Detected hum.\t\t\t%d\r\n", master_temp["input.hum"].get_value());
ccli8 1:2fcc21a82c3b 163 }
ccli8 1:2fcc21a82c3b 164
ccli8 1:2fcc21a82c3b 165 void test_nubrick_gas(void) {
ccli8 1:2fcc21a82c3b 166
ccli8 1:2fcc21a82c3b 167 NUBRICK_CONNECT(master_gas, "Gas");
ccli8 1:2fcc21a82c3b 168
ccli8 1:2fcc21a82c3b 169 master_gas.pull_feature_report();
ccli8 1:2fcc21a82c3b 170 // Gas alarm threshold in %.
ccli8 2:48c6a70d121c 171 printf("Gas alarm threshold\t\t%d\r\n", master_gas["feature.gas_AT"].get_value());
ccli8 1:2fcc21a82c3b 172
ccli8 1:2fcc21a82c3b 173 master_gas.pull_input_report();
ccli8 1:2fcc21a82c3b 174 // Detected gas in %. 80% above for normal.
ccli8 2:48c6a70d121c 175 printf("Gas\t\t\t\t%d\r\n", master_gas["input.gas"].get_value());
ccli8 1:2fcc21a82c3b 176 }
ccli8 1:2fcc21a82c3b 177
ccli8 1:2fcc21a82c3b 178 void test_nubrick_ir(void) {
ccli8 1:2fcc21a82c3b 179
ccli8 1:2fcc21a82c3b 180 NUBRICK_CONNECT(master_ir, "IR");
ccli8 1:2fcc21a82c3b 181
ccli8 1:2fcc21a82c3b 182 master_ir.pull_feature_report();
ccli8 2:48c6a70d121c 183 printf("Number of learned data\t\t%d\r\n", master_ir["feature.num_learned_data"].get_value());
ccli8 2:48c6a70d121c 184 printf("Using data type\t\t\t%d\r\n", master_ir["feature.using_data_type"].get_value());
ccli8 2:48c6a70d121c 185 printf("Index of original data to send\t%d\r\n", master_ir["feature.index_orig_data_to_send"].get_value());
ccli8 2:48c6a70d121c 186 printf("Index of learned data to send\t%d\r\n", master_ir["feature.index_learned_data_to_send"].get_value());
ccli8 1:2fcc21a82c3b 187
ccli8 1:2fcc21a82c3b 188 master_ir.pull_input_report();
ccli8 2:48c6a70d121c 189 printf("Has received data flag\t\t%d\r\n", master_ir["input.received_data_flag"].get_value());
ccli8 1:2fcc21a82c3b 190 }
ccli8 1:2fcc21a82c3b 191
ccli8 1:2fcc21a82c3b 192 void test_nubrick_keys(void) {
ccli8 1:2fcc21a82c3b 193
ccli8 1:2fcc21a82c3b 194 NUBRICK_CONNECT(master_keys, "Key");
ccli8 1:2fcc21a82c3b 195
ccli8 1:2fcc21a82c3b 196 // Detect 8 keys
ccli8 1:2fcc21a82c3b 197 master_keys.pull_input_report();
ccli8 1:2fcc21a82c3b 198 uint16_t key_state = master_keys["input.key_state"].get_value();
ccli8 1:2fcc21a82c3b 199 unsigned i = 0;
ccli8 1:2fcc21a82c3b 200 for (i = 0; i < 8; i ++) {
ccli8 1:2fcc21a82c3b 201 if (key_state & (1 << i)) {
ccli8 2:48c6a70d121c 202 printf("KEY%d PRESSED\r\n", i + 1);
ccli8 1:2fcc21a82c3b 203 }
ccli8 1:2fcc21a82c3b 204 else {
ccli8 2:48c6a70d121c 205 printf("KEY%d RELEASED\r\n", i + 1);
ccli8 1:2fcc21a82c3b 206 }
ccli8 1:2fcc21a82c3b 207 }
ccli8 1:2fcc21a82c3b 208 }