NuMaker Brick I2C slave devices

Committer:
SHLIU1@OANBE02333.nuvoton.com
Date:
Thu Feb 25 11:05:27 2021 +0800
Revision:
12:92db8dd90705
Parent:
8:b60e846cae6a
Support the both V5.X and V6.X for mbed-os

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()) { \
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 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 { \
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 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() {
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 60 #ifdef MBED_MAJOR_VERSION
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 61 printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 62 #endif
ccli8 1:2fcc21a82c3b 63 // Test all supported NuBrick slave devices
ccli8 1:2fcc21a82c3b 64 test_nubrick_buzzer();
ccli8 1:2fcc21a82c3b 65 test_nubrick_led();
ccli8 1:2fcc21a82c3b 66 test_nubrick_ahrs();
ccli8 1:2fcc21a82c3b 67 test_nubrick_sonar();
ccli8 1:2fcc21a82c3b 68 test_nubrick_temp();
ccli8 1:2fcc21a82c3b 69 test_nubrick_gas();
ccli8 1:2fcc21a82c3b 70 test_nubrick_ir();
ccli8 1:2fcc21a82c3b 71 test_nubrick_keys();
ccli8 1:2fcc21a82c3b 72
ccli8 1:2fcc21a82c3b 73 return 0;
ccli8 1:2fcc21a82c3b 74 }
ccli8 1:2fcc21a82c3b 75
ccli8 1:2fcc21a82c3b 76 void test_nubrick_buzzer(void) {
ccli8 1:2fcc21a82c3b 77
ccli8 1:2fcc21a82c3b 78 NUBRICK_CONNECT(master_buzzer, "Buzzer");
ccli8 1:2fcc21a82c3b 79
ccli8 1:2fcc21a82c3b 80 // Configure the Buzzer
ccli8 1:2fcc21a82c3b 81 master_buzzer.pull_feature_report();
ccli8 1:2fcc21a82c3b 82 master_buzzer["feature.sleep_period"].set_value(100);
ccli8 1:2fcc21a82c3b 83 master_buzzer["feature.volume"].set_value(60); // Volume in %
ccli8 1:2fcc21a82c3b 84 master_buzzer["feature.tone"].set_value(196); // Tone in Hz
ccli8 1:2fcc21a82c3b 85 master_buzzer["feature.song"].set_value(0); // 0 (mono), 1 (Bee)
ccli8 1:2fcc21a82c3b 86 master_buzzer["feature.period"].set_value(200); // Period in ms
ccli8 1:2fcc21a82c3b 87 master_buzzer["feature.duty"].set_value(30); // Duty in %
ccli8 1:2fcc21a82c3b 88 master_buzzer["feature.latency"].set_value(3); // Alarm for time secs
ccli8 1:2fcc21a82c3b 89 master_buzzer.push_feature_report();
ccli8 1:2fcc21a82c3b 90
ccli8 1:2fcc21a82c3b 91 // The NuBrick I2C device may not respond in time. Add delay here.
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 92 #if MBED_MAJOR_VERSION >= 6
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 93 ThisThread::sleep_for(50);
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 94 #else
ccli8 1:2fcc21a82c3b 95 wait_ms(50);
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 96 #endif
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.
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 120 #if MBED_MAJOR_VERSION >= 6
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 121 ThisThread::sleep_for(50);
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 122 #else
ccli8 1:2fcc21a82c3b 123 wait_ms(50);
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 124 #endif
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 125
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 126
SHLIU1@OANBE02333.nuvoton.com 12:92db8dd90705 127
ccli8 1:2fcc21a82c3b 128
ccli8 1:2fcc21a82c3b 129 // Start blinking the LED
ccli8 1:2fcc21a82c3b 130 master_led["output.start_flag"].set_value(1);
ccli8 1:2fcc21a82c3b 131 master_led["output.stop_flag"].set_value(0);
ccli8 1:2fcc21a82c3b 132 master_led.push_output_report();
ccli8 1:2fcc21a82c3b 133 }
ccli8 1:2fcc21a82c3b 134
ccli8 1:2fcc21a82c3b 135 void test_nubrick_ahrs(void) {
ccli8 1:2fcc21a82c3b 136
ccli8 1:2fcc21a82c3b 137 NUBRICK_CONNECT(master_ahrs, "AHRS");
ccli8 1:2fcc21a82c3b 138
ccli8 1:2fcc21a82c3b 139 master_ahrs.pull_feature_report();
ccli8 1:2fcc21a82c3b 140 // Prescaled vibration alarm threshold
ccli8 2:48c6a70d121c 141 printf("Prescaled vibration alarm threshold\t\t%d\r\n", master_ahrs["feature.pre_vibration_AT"].get_value());
ccli8 1:2fcc21a82c3b 142
ccli8 1:2fcc21a82c3b 143 master_ahrs.pull_input_report();
ccli8 1:2fcc21a82c3b 144 // Detected vibration
ccli8 2:48c6a70d121c 145 printf("Detected vibration\t\t\t\t%d\r\n", master_ahrs["input.vibration"].get_value());
ccli8 1:2fcc21a82c3b 146 }
ccli8 1:2fcc21a82c3b 147
ccli8 1:2fcc21a82c3b 148 void test_nubrick_sonar(void) {
ccli8 1:2fcc21a82c3b 149
ccli8 1:2fcc21a82c3b 150 NUBRICK_CONNECT(master_sonar, "Sonar");
ccli8 1:2fcc21a82c3b 151
ccli8 1:2fcc21a82c3b 152 master_sonar.pull_feature_report();
ccli8 1:2fcc21a82c3b 153 // Distance alarm threshold in cm
ccli8 2:48c6a70d121c 154 printf("Distance alarm threshold\t\t%d\r\n", master_sonar["feature.distance_AT"].get_value());
ccli8 1:2fcc21a82c3b 155
ccli8 1:2fcc21a82c3b 156 master_sonar.pull_input_report();
ccli8 1:2fcc21a82c3b 157 // Detected distance in cm
ccli8 2:48c6a70d121c 158 printf("Detected distance\t\t\t%d\r\n", master_sonar["input.distance"].get_value());
ccli8 1:2fcc21a82c3b 159 }
ccli8 1:2fcc21a82c3b 160
ccli8 1:2fcc21a82c3b 161 void test_nubrick_temp(void) {
ccli8 1:2fcc21a82c3b 162
ccli8 1:2fcc21a82c3b 163 NUBRICK_CONNECT(master_temp, "Temperature & Humidity");
ccli8 1:2fcc21a82c3b 164
ccli8 1:2fcc21a82c3b 165 master_temp.pull_feature_report();
ccli8 1:2fcc21a82c3b 166 // Temp. alarm threshold in Celsius
ccli8 2:48c6a70d121c 167 printf("Temp. alarm threshold\t\t%d\r\n", master_temp["feature.temp_AT"].get_value());
ccli8 1:2fcc21a82c3b 168 // Hum. alarm threshold in %
ccli8 2:48c6a70d121c 169 printf("Hum. alarm threshold\t\t%d\r\n", master_temp["feature.hum_AT"].get_value());
ccli8 1:2fcc21a82c3b 170
ccli8 1:2fcc21a82c3b 171 master_temp.pull_input_report();
ccli8 1:2fcc21a82c3b 172 // Detected temp in Celsius
ccli8 2:48c6a70d121c 173 printf("Detected temp.\t\t\t%d\r\n", master_temp["input.temp"].get_value());
ccli8 1:2fcc21a82c3b 174 // Detected hum. in %
ccli8 2:48c6a70d121c 175 printf("Detected hum.\t\t\t%d\r\n", master_temp["input.hum"].get_value());
ccli8 1:2fcc21a82c3b 176 }
ccli8 1:2fcc21a82c3b 177
ccli8 1:2fcc21a82c3b 178 void test_nubrick_gas(void) {
ccli8 1:2fcc21a82c3b 179
ccli8 1:2fcc21a82c3b 180 NUBRICK_CONNECT(master_gas, "Gas");
ccli8 1:2fcc21a82c3b 181
ccli8 1:2fcc21a82c3b 182 master_gas.pull_feature_report();
ccli8 1:2fcc21a82c3b 183 // Gas alarm threshold in %.
ccli8 2:48c6a70d121c 184 printf("Gas alarm threshold\t\t%d\r\n", master_gas["feature.gas_AT"].get_value());
ccli8 1:2fcc21a82c3b 185
ccli8 1:2fcc21a82c3b 186 master_gas.pull_input_report();
ccli8 1:2fcc21a82c3b 187 // Detected gas in %. 80% above for normal.
ccli8 2:48c6a70d121c 188 printf("Gas\t\t\t\t%d\r\n", master_gas["input.gas"].get_value());
ccli8 1:2fcc21a82c3b 189 }
ccli8 1:2fcc21a82c3b 190
ccli8 1:2fcc21a82c3b 191 void test_nubrick_ir(void) {
ccli8 1:2fcc21a82c3b 192
ccli8 1:2fcc21a82c3b 193 NUBRICK_CONNECT(master_ir, "IR");
ccli8 1:2fcc21a82c3b 194
ccli8 1:2fcc21a82c3b 195 master_ir.pull_feature_report();
ccli8 2:48c6a70d121c 196 printf("Number of learned data\t\t%d\r\n", master_ir["feature.num_learned_data"].get_value());
ccli8 2:48c6a70d121c 197 printf("Using data type\t\t\t%d\r\n", master_ir["feature.using_data_type"].get_value());
ccli8 2:48c6a70d121c 198 printf("Index of original data to send\t%d\r\n", master_ir["feature.index_orig_data_to_send"].get_value());
ccli8 2:48c6a70d121c 199 printf("Index of learned data to send\t%d\r\n", master_ir["feature.index_learned_data_to_send"].get_value());
ccli8 1:2fcc21a82c3b 200
ccli8 1:2fcc21a82c3b 201 master_ir.pull_input_report();
ccli8 2:48c6a70d121c 202 printf("Has received data flag\t\t%d\r\n", master_ir["input.received_data_flag"].get_value());
ccli8 1:2fcc21a82c3b 203 }
ccli8 1:2fcc21a82c3b 204
ccli8 1:2fcc21a82c3b 205 void test_nubrick_keys(void) {
ccli8 1:2fcc21a82c3b 206
ccli8 1:2fcc21a82c3b 207 NUBRICK_CONNECT(master_keys, "Key");
ccli8 1:2fcc21a82c3b 208
ccli8 1:2fcc21a82c3b 209 // Detect 8 keys
ccli8 1:2fcc21a82c3b 210 master_keys.pull_input_report();
ccli8 1:2fcc21a82c3b 211 uint16_t key_state = master_keys["input.key_state"].get_value();
ccli8 1:2fcc21a82c3b 212 unsigned i = 0;
ccli8 1:2fcc21a82c3b 213 for (i = 0; i < 8; i ++) {
ccli8 1:2fcc21a82c3b 214 if (key_state & (1 << i)) {
ccli8 2:48c6a70d121c 215 printf("KEY%d PRESSED\r\n", i + 1);
ccli8 1:2fcc21a82c3b 216 }
ccli8 1:2fcc21a82c3b 217 else {
ccli8 2:48c6a70d121c 218 printf("KEY%d RELEASED\r\n", i + 1);
ccli8 1:2fcc21a82c3b 219 }
ccli8 1:2fcc21a82c3b 220 }
ccli8 1:2fcc21a82c3b 221 }