test
Dependencies: BLE_API nRF51822 mbed
Fork of KS7 by
main.cpp@25:da723b86c60b, 2016-03-29 (annotated)
- Committer:
- akita11
- Date:
- Tue Mar 29 02:46:41 2016 +0000
- Branch:
- KS3
- Revision:
- 25:da723b86c60b
- Parent:
- 24:c6ef55535900
- Child:
- 27:8c3c29b3f704
hardware SPI for HX711
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akita11 | 0:08c8325a21ec | 1 | #include "mbed.h" |
akita11 | 0:08c8325a21ec | 2 | #include "io.h" |
gomihgy | 12:880400fa5c44 | 3 | #include "BLE.h" |
gomihgy | 23:ca1806081f0c | 4 | #include "DFUService.h" |
gomihgy | 9:a7bd5ae66d6d | 5 | #include "common.h" |
akita11 | 19:05526fe9365e | 6 | #include <stdlib.h> |
akita11 | 0:08c8325a21ec | 7 | |
gomihgy | 6:bc960e6fd1ed | 8 | // BLE |
gomihgy | 6:bc960e6fd1ed | 9 | #define INTERVAL_500MSEC (500UL) |
gomihgy | 6:bc960e6fd1ed | 10 | #define CONNTIMEOUT_3000MSEC (3000UL) |
gomihgy | 5:1730064b94bd | 11 | #define ADV_TIMEOUT (0) |
gomihgy | 5:1730064b94bd | 12 | #define DEVICE_NAME "Kitchen Scale" |
gomihgy | 5:1730064b94bd | 13 | #define BLE_TXPOWER_4DBM (4) |
gomihgy | 5:1730064b94bd | 14 | |
gomihgy | 5:1730064b94bd | 15 | // Device Information Service (DIS) (20 character limit) |
gomihgy | 5:1730064b94bd | 16 | // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml |
gomihgy | 6:bc960e6fd1ed | 17 | #define MANUFACTURER_NAME_STRING "Hacarus" // Manufacturer Name String - shall represent the name of the manufacturer of the device. |
gomihgy | 6:bc960e6fd1ed | 18 | #define MODEL_NUMBER_STRING "0001" // Model Number String - shall represent the model number that is assigned by the device vendor. |
gomihgy | 6:bc960e6fd1ed | 19 | #define SERIAL_NUMBER_STRING "000780c0ffeef00d" // Serial Number String - shall represent the serial number for a particular instance of the device. |
gomihgy | 23:ca1806081f0c | 20 | #define FIRMWARE_REVISION_STRING "v1.00.009@rev0023" // Firmware Revision String - shall represent the firmware revision for the firmware within the device. |
gomihgy | 5:1730064b94bd | 21 | |
gomihgy | 5:1730064b94bd | 22 | // Weight Scale Service (Original) |
gomihgy | 5:1730064b94bd | 23 | #define UUID_WEIGHT_SCALE_SERVICE (0x181D) |
gomihgy | 6:bc960e6fd1ed | 24 | |
akita11 | 20:c3fadeef5842 | 25 | #if defined(PCB_VER1) || defined(PCB_VER2) |
gomihgy | 16:fc66feb533d0 | 26 | // Switch |
gomihgy | 14:81347f8f0bda | 27 | #define SW_THRESHOLD (0.5) |
gomihgy | 14:81347f8f0bda | 28 | #define SW_SAMPLECOUNT (3) |
akita11 | 20:c3fadeef5842 | 29 | #endif |
gomihgy | 5:1730064b94bd | 30 | |
gomihgy | 6:bc960e6fd1ed | 31 | // Mode |
gomihgy | 6:bc960e6fd1ed | 32 | #define MODE_OFF (0) // LED OFF |
gomihgy | 6:bc960e6fd1ed | 33 | #define MODE_START (1) // LED OFF -> ON |
gomihgy | 6:bc960e6fd1ed | 34 | #define MODE_ON (2) // LED ON |
gomihgy | 6:bc960e6fd1ed | 35 | #define MODE_END (3) // LED ON -> OFF |
gomihgy | 6:bc960e6fd1ed | 36 | |
gomihgy | 6:bc960e6fd1ed | 37 | // Led |
gomihgy | 16:fc66feb533d0 | 38 | #define LED_INTERVAL_MSEC (100) |
gomihgy | 6:bc960e6fd1ed | 39 | #define BRIGHTNESS_ADDVALUE (0.1) |
gomihgy | 7:9ebf125e405f | 40 | #define BRIGHTNESS_MINVALUE (0.0) |
gomihgy | 6:bc960e6fd1ed | 41 | #define BRIGHTNESS_MAXVALUE (1.0) |
gomihgy | 6:bc960e6fd1ed | 42 | |
gomihgy | 6:bc960e6fd1ed | 43 | // Properties |
akita11 | 25:da723b86c60b | 44 | //io io; |
akita11 | 25:da723b86c60b | 45 | //io io(P0_15, P0_13); // HX711's CLK & DAT |
akita11 | 25:da723b86c60b | 46 | io io(P0_5, P0_4); // HX711's CLK & DAT |
gomihgy | 9:a7bd5ae66d6d | 47 | uint32_t weight_data; |
gomihgy | 9:a7bd5ae66d6d | 48 | float32_t weight = 0.0; |
gomihgy | 6:bc960e6fd1ed | 49 | uint32_t scale = 0; |
gomihgy | 6:bc960e6fd1ed | 50 | int update_counter = 0; |
akita11 | 20:c3fadeef5842 | 51 | #if defined(PCB_VER1) || defined(PCB_VER2) |
gomihgy | 14:81347f8f0bda | 52 | float sw_data[SW_SAMPLECOUNT]; |
gomihgy | 14:81347f8f0bda | 53 | uint8_t sw_count = 0; |
akita11 | 20:c3fadeef5842 | 54 | #endif |
gomihgy | 6:bc960e6fd1ed | 55 | int led_mode = MODE_OFF; |
gomihgy | 6:bc960e6fd1ed | 56 | float led_brightness = BRIGHTNESS_MINVALUE; |
akita11 | 4:edd6e262de9e | 57 | |
akita11 | 10:95c1e5a218d5 | 58 | #ifdef UART_DEBUG |
akita11 | 20:c3fadeef5842 | 59 | #if defined(PCB_VER1) || defined(PCB_VER2) |
akita11 | 10:95c1e5a218d5 | 60 | Serial pc(P0_9, P0_8);// TX=P0_9 |
akita11 | 20:c3fadeef5842 | 61 | #else |
akita11 | 20:c3fadeef5842 | 62 | Serial pc(P0_9, P0_11);// TX=P0_9 |
akita11 | 20:c3fadeef5842 | 63 | #endif |
akita11 | 10:95c1e5a218d5 | 64 | #define UART_BAUD_RATE (9600UL) |
akita11 | 10:95c1e5a218d5 | 65 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
akita11 | 10:95c1e5a218d5 | 66 | #else |
akita11 | 10:95c1e5a218d5 | 67 | #define DEBUG(...) {} |
akita11 | 10:95c1e5a218d5 | 68 | #endif |
akita11 | 10:95c1e5a218d5 | 69 | |
akita11 | 20:c3fadeef5842 | 70 | Timer t; |
akita11 | 20:c3fadeef5842 | 71 | |
akita11 | 20:c3fadeef5842 | 72 | // BLE |
gomihgy | 12:880400fa5c44 | 73 | BLE ble; |
gomihgy | 5:1730064b94bd | 74 | Gap::ConnectionParams_t connectionParams; |
gomihgy | 5:1730064b94bd | 75 | |
gomihgy | 5:1730064b94bd | 76 | /* Complete list of 16-bit Service IDs */ |
gomihgy | 6:bc960e6fd1ed | 77 | uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
gomihgy | 5:1730064b94bd | 78 | |
gomihgy | 5:1730064b94bd | 79 | /* Weight Scale Service */ |
gomihgy | 6:bc960e6fd1ed | 80 | static const uint8_t UUID_HACARUS_WEIGHT_CHAR[] = {0x00, 0x00, 0x2A, 0x9D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; |
gomihgy | 9:a7bd5ae66d6d | 81 | GattCharacteristic WeightMeasurement (UUID(UUID_HACARUS_WEIGHT_CHAR), (uint8_t *)&weight_data, sizeof(weight_data), sizeof(weight_data), |
akita11 | 20:c3fadeef5842 | 82 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
gomihgy | 6:bc960e6fd1ed | 83 | static const uint8_t UUID_HACARUS_SCALE_CHAR[] = {0x00, 0x00, 0x2A, 0x9E, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; |
gomihgy | 6:bc960e6fd1ed | 84 | GattCharacteristic WeightScale (UUID(UUID_HACARUS_SCALE_CHAR), (uint8_t *)&scale, sizeof(scale), sizeof(scale), |
akita11 | 20:c3fadeef5842 | 85 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
gomihgy | 6:bc960e6fd1ed | 86 | GattCharacteristic *Chars[] = {&WeightMeasurement,&WeightScale}; |
gomihgy | 6:bc960e6fd1ed | 87 | static const uint8_t UUID_HACARUS_WEIGHT_SERVICE[] = {0x00, 0x00, 0x18, 0x1D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'}; |
gomihgy | 6:bc960e6fd1ed | 88 | GattService HWS = GattService(UUID(UUID_HACARUS_WEIGHT_SERVICE), Chars, sizeof(Chars) / sizeof(GattCharacteristic *)); |
gomihgy | 5:1730064b94bd | 89 | |
gomihgy | 5:1730064b94bd | 90 | /* Device Information Service */ |
gomihgy | 5:1730064b94bd | 91 | GattCharacteristic ManuName(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)&MANUFACTURER_NAME_STRING, sizeof(MANUFACTURER_NAME_STRING) - 1, sizeof(MANUFACTURER_NAME_STRING) - 1, |
akita11 | 20:c3fadeef5842 | 92 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
gomihgy | 5:1730064b94bd | 93 | GattCharacteristic ModelNum(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)&MODEL_NUMBER_STRING, sizeof(MODEL_NUMBER_STRING) - 1, sizeof(MODEL_NUMBER_STRING) - 1, |
akita11 | 20:c3fadeef5842 | 94 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
gomihgy | 6:bc960e6fd1ed | 95 | GattCharacteristic SerialNum(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)&SERIAL_NUMBER_STRING, sizeof(SERIAL_NUMBER_STRING) - 1, sizeof(SERIAL_NUMBER_STRING) - 1, |
akita11 | 20:c3fadeef5842 | 96 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
gomihgy | 5:1730064b94bd | 97 | GattCharacteristic FWVersion(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)&FIRMWARE_REVISION_STRING, sizeof(FIRMWARE_REVISION_STRING) - 1, sizeof(FIRMWARE_REVISION_STRING) - 1, |
akita11 | 20:c3fadeef5842 | 98 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
gomihgy | 6:bc960e6fd1ed | 99 | GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion}; |
gomihgy | 5:1730064b94bd | 100 | GattService DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *)); |
gomihgy | 5:1730064b94bd | 101 | |
akita11 | 20:c3fadeef5842 | 102 | #ifdef PCB_VER1 |
akita11 | 20:c3fadeef5842 | 103 | bool check_joystick() |
akita11 | 20:c3fadeef5842 | 104 | { |
gomihgy | 14:81347f8f0bda | 105 | float sum_data = 0; |
akita11 | 20:c3fadeef5842 | 106 | |
gomihgy | 14:81347f8f0bda | 107 | sw_data[sw_count] = io.get_x(); |
akita11 | 20:c3fadeef5842 | 108 | |
akita11 | 20:c3fadeef5842 | 109 | if(++sw_count >= SW_SAMPLECOUNT) { |
gomihgy | 14:81347f8f0bda | 110 | sw_count = 0; |
gomihgy | 7:9ebf125e405f | 111 | } |
akita11 | 20:c3fadeef5842 | 112 | |
akita11 | 20:c3fadeef5842 | 113 | for(int count = 0; count < SW_SAMPLECOUNT; count++) { |
gomihgy | 14:81347f8f0bda | 114 | sum_data += sw_data[count]; |
akita11 | 0:08c8325a21ec | 115 | } |
akita11 | 20:c3fadeef5842 | 116 | |
gomihgy | 14:81347f8f0bda | 117 | return ((sum_data / SW_SAMPLECOUNT) >= SW_THRESHOLD); |
gomihgy | 7:9ebf125e405f | 118 | } |
akita11 | 20:c3fadeef5842 | 119 | #endif |
gomihgy | 7:9ebf125e405f | 120 | |
gomihgy | 9:a7bd5ae66d6d | 121 | uint32_t quick_ieee11073_from_float(float data) |
gomihgy | 9:a7bd5ae66d6d | 122 | { |
gomihgy | 9:a7bd5ae66d6d | 123 | uint8_t exponent = 0xFE; //exponent is -2 |
gomihgy | 9:a7bd5ae66d6d | 124 | uint32_t mantissa = (uint32_t)(data*100); |
akita11 | 20:c3fadeef5842 | 125 | |
gomihgy | 9:a7bd5ae66d6d | 126 | return ( ((uint32_t)exponent) << 24) | mantissa; |
gomihgy | 9:a7bd5ae66d6d | 127 | } |
gomihgy | 9:a7bd5ae66d6d | 128 | |
akita11 | 20:c3fadeef5842 | 129 | #if defined(PCB_VER1) || defined(PCB_VER2) |
akita11 | 20:c3fadeef5842 | 130 | void SWInit(void) |
akita11 | 20:c3fadeef5842 | 131 | { |
gomihgy | 16:fc66feb533d0 | 132 | // SW Initialize |
akita11 | 20:c3fadeef5842 | 133 | for(int count = 0; count < SW_SAMPLECOUNT; count++) { |
gomihgy | 16:fc66feb533d0 | 134 | sw_data[count] = 0; |
gomihgy | 16:fc66feb533d0 | 135 | } |
gomihgy | 16:fc66feb533d0 | 136 | } |
akita11 | 20:c3fadeef5842 | 137 | #endif |
akita11 | 10:95c1e5a218d5 | 138 | |
akita11 | 20:c3fadeef5842 | 139 | void AppInit(void) |
akita11 | 20:c3fadeef5842 | 140 | { |
akita11 | 20:c3fadeef5842 | 141 | |
akita11 | 20:c3fadeef5842 | 142 | #if defined(PCB_VER1) || defined(PCB_VER2) |
gomihgy | 16:fc66feb533d0 | 143 | SWInit(); |
akita11 | 20:c3fadeef5842 | 144 | #endif |
gomihgy | 16:fc66feb533d0 | 145 | io.analog_pow(1); |
akita11 | 20:c3fadeef5842 | 146 | |
akita11 | 21:ab5b55d1947d | 147 | #ifdef PCB_VER3 |
akita11 | 21:ab5b55d1947d | 148 | // check XTALFREQ for TaiyoYuden module in PCB_VER3 |
akita11 | 21:ab5b55d1947d | 149 | /* |
akita11 | 20:c3fadeef5842 | 150 | if(NRF_UICR->XTALFREQ == 0xFFFFFF00){ |
akita11 | 20:c3fadeef5842 | 151 | io.display_value = 3232; |
akita11 | 20:c3fadeef5842 | 152 | }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){ |
akita11 | 20:c3fadeef5842 | 153 | io.display_value = 1616; |
akita11 | 20:c3fadeef5842 | 154 | } |
akita11 | 21:ab5b55d1947d | 155 | */ |
akita11 | 21:ab5b55d1947d | 156 | #endif |
akita11 | 0:08c8325a21ec | 157 | } |
akita11 | 0:08c8325a21ec | 158 | |
gomihgy | 5:1730064b94bd | 159 | /* |
gomihgy | 5:1730064b94bd | 160 | * BLE CallBacks |
gomihgy | 5:1730064b94bd | 161 | */ |
gomihgy | 12:880400fa5c44 | 162 | void BLEConnectionCallback(const Gap::ConnectionCallbackParams_t *params) |
gomihgy | 5:1730064b94bd | 163 | { |
akita11 | 20:c3fadeef5842 | 164 | ble.updateConnectionParams(params->handle, params->connectionParams); |
gomihgy | 5:1730064b94bd | 165 | } |
gomihgy | 5:1730064b94bd | 166 | |
gomihgy | 12:880400fa5c44 | 167 | void BLEDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
gomihgy | 5:1730064b94bd | 168 | { |
akita11 | 20:c3fadeef5842 | 169 | if(led_mode == MODE_ON) { |
gomihgy | 6:bc960e6fd1ed | 170 | ble.startAdvertising(); |
gomihgy | 5:1730064b94bd | 171 | } |
gomihgy | 5:1730064b94bd | 172 | } |
gomihgy | 5:1730064b94bd | 173 | |
gomihgy | 12:880400fa5c44 | 174 | void BLERadioNotificationCallback(bool radio_active) |
gomihgy | 12:880400fa5c44 | 175 | { |
akita11 | 20:c3fadeef5842 | 176 | if (radio_active == false) { |
akita11 | 20:c3fadeef5842 | 177 | if(led_mode == MODE_ON) { |
gomihgy | 12:880400fa5c44 | 178 | } |
gomihgy | 12:880400fa5c44 | 179 | } |
gomihgy | 12:880400fa5c44 | 180 | } |
gomihgy | 12:880400fa5c44 | 181 | |
akita11 | 20:c3fadeef5842 | 182 | void BleInitialize(void) |
akita11 | 20:c3fadeef5842 | 183 | { |
akita11 | 20:c3fadeef5842 | 184 | |
gomihgy | 6:bc960e6fd1ed | 185 | uint8_t advertiseServiceID[16]; |
akita11 | 20:c3fadeef5842 | 186 | |
gomihgy | 5:1730064b94bd | 187 | // Initialize |
gomihgy | 5:1730064b94bd | 188 | ble.init(); |
akita11 | 20:c3fadeef5842 | 189 | |
gomihgy | 5:1730064b94bd | 190 | // Event Set |
gomihgy | 5:1730064b94bd | 191 | ble.onConnection(&BLEConnectionCallback); |
gomihgy | 5:1730064b94bd | 192 | ble.onDisconnection(&BLEDisconnectionCallback); |
gomihgy | 12:880400fa5c44 | 193 | ble.onRadioNotification(&BLERadioNotificationCallback); |
akita11 | 20:c3fadeef5842 | 194 | |
gomihgy | 5:1730064b94bd | 195 | ble.getPreferredConnectionParams(&connectionParams); |
gomihgy | 6:bc960e6fd1ed | 196 | connectionParams.maxConnectionInterval = INTERVAL_500MSEC; |
gomihgy | 6:bc960e6fd1ed | 197 | connectionParams.minConnectionInterval = INTERVAL_500MSEC; |
gomihgy | 6:bc960e6fd1ed | 198 | connectionParams.connectionSupervisionTimeout = CONNTIMEOUT_3000MSEC; |
gomihgy | 6:bc960e6fd1ed | 199 | connectionParams.slaveLatency = 2; |
gomihgy | 5:1730064b94bd | 200 | ble.setPreferredConnectionParams(&connectionParams); |
akita11 | 20:c3fadeef5842 | 201 | |
gomihgy | 5:1730064b94bd | 202 | ble.setTxPower(BLE_TXPOWER_4DBM); |
akita11 | 20:c3fadeef5842 | 203 | |
akita11 | 20:c3fadeef5842 | 204 | for(int i=0; i<16; i++) { |
gomihgy | 6:bc960e6fd1ed | 205 | advertiseServiceID[i] = UUID_HACARUS_WEIGHT_SERVICE[16 - 1 - i]; |
gomihgy | 6:bc960e6fd1ed | 206 | } |
gomihgy | 6:bc960e6fd1ed | 207 | |
gomihgy | 5:1730064b94bd | 208 | ble.accumulateAdvertisingPayload((GapAdvertisingData::Flags)(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED)); |
gomihgy | 5:1730064b94bd | 209 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
akita11 | 20:c3fadeef5842 | 210 | (const uint8_t *)DEVICE_NAME, |
akita11 | 20:c3fadeef5842 | 211 | strlen(DEVICE_NAME)); |
gomihgy | 5:1730064b94bd | 212 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
gomihgy | 6:bc960e6fd1ed | 213 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
gomihgy | 6:bc960e6fd1ed | 214 | (const uint8_t *)advertiseServiceID, sizeof(advertiseServiceID)); |
akita11 | 20:c3fadeef5842 | 215 | |
gomihgy | 12:880400fa5c44 | 216 | ble.setAdvertisingInterval(INTERVAL_500MSEC); |
gomihgy | 5:1730064b94bd | 217 | ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */ |
akita11 | 20:c3fadeef5842 | 218 | |
gomihgy | 6:bc960e6fd1ed | 219 | ble.addService(HWS); |
gomihgy | 5:1730064b94bd | 220 | ble.addService(DIS); |
gomihgy | 23:ca1806081f0c | 221 | DFUService dfu(ble); |
gomihgy | 5:1730064b94bd | 222 | } |
gomihgy | 5:1730064b94bd | 223 | |
akita11 | 0:08c8325a21ec | 224 | int main() |
akita11 | 0:08c8325a21ec | 225 | { |
akita11 | 24:c6ef55535900 | 226 | float weight_s; |
akita11 | 24:c6ef55535900 | 227 | int Navg = 5; |
akita11 | 24:c6ef55535900 | 228 | int sample = 0; |
gomihgy | 5:1730064b94bd | 229 | #ifdef UART_DEBUG |
gomihgy | 5:1730064b94bd | 230 | pc.baud(UART_BAUD_RATE); |
akita11 | 25:da723b86c60b | 231 | /* |
akita11 | 25:da723b86c60b | 232 | pc.printf("SPI->PSELSCK = %x\r\n", NRF_SPI0->PSELSCK); // will be 15 (P0_15) |
akita11 | 25:da723b86c60b | 233 | pc.printf("SPI->PSELMISO = %x\r\n", NRF_SPI0->PSELMISO); // will be 13 (P0_13) |
akita11 | 25:da723b86c60b | 234 | pc.printf("SPI->PSELMOSI = %x\r\n", NRF_SPI0->PSELMOSI); // will be 14 (P0_14): dummy |
akita11 | 25:da723b86c60b | 235 | pc.printf("SPI->PSELSCK = %x\r\n", NRF_SPI1->PSELSCK); // will be 15 (P0_15) |
akita11 | 25:da723b86c60b | 236 | pc.printf("SPI->PSELMISO = %x\r\n", NRF_SPI1->PSELMISO); // will be 13 (P0_13) |
akita11 | 25:da723b86c60b | 237 | pc.printf("SPI->PSELMOSI = %x\r\n", NRF_SPI1->PSELMOSI); // will be 14 (P0_14): dummy |
akita11 | 25:da723b86c60b | 238 | io.analog_pow(1); |
akita11 | 25:da723b86c60b | 239 | while(1){ |
akita11 | 25:da723b86c60b | 240 | pc.printf("%x\r\n", io._get_adc_raw(0)); |
akita11 | 25:da723b86c60b | 241 | } |
akita11 | 25:da723b86c60b | 242 | */ |
gomihgy | 5:1730064b94bd | 243 | #endif |
akita11 | 21:ab5b55d1947d | 244 | #ifdef PCB_VER3 |
akita11 | 10:95c1e5a218d5 | 245 | // set XTAL=32MHz for TaiyoYuden's module |
akita11 | 11:be439189e3f7 | 246 | // is moved to mbed-src/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c |
akita11 | 11:be439189e3f7 | 247 | DEBUG("UICR->XTALFREQ=%x\r\n", NRF_UICR->XTALFREQ); // this should be 0xffffff00, not 0xffffffff |
akita11 | 10:95c1e5a218d5 | 248 | #endif |
akita11 | 10:95c1e5a218d5 | 249 | |
akita11 | 10:95c1e5a218d5 | 250 | BleInitialize(); |
gomihgy | 16:fc66feb533d0 | 251 | AppInit(); |
akita11 | 20:c3fadeef5842 | 252 | led_mode = MODE_OFF; |
gomihgy | 6:bc960e6fd1ed | 253 | for (;; ) { |
gomihgy | 14:81347f8f0bda | 254 | // 100msec waitForEvent |
gomihgy | 14:81347f8f0bda | 255 | t.reset(); |
akita11 | 20:c3fadeef5842 | 256 | while(t.read_ms() < LED_INTERVAL_MSEC) { |
gomihgy | 16:fc66feb533d0 | 257 | t.start(); |
gomihgy | 14:81347f8f0bda | 258 | ble.waitForEvent(); |
gomihgy | 16:fc66feb533d0 | 259 | t.stop(); |
gomihgy | 14:81347f8f0bda | 260 | } |
akita11 | 20:c3fadeef5842 | 261 | switch(led_mode) { |
gomihgy | 14:81347f8f0bda | 262 | case MODE_OFF: |
akita11 | 20:c3fadeef5842 | 263 | io.analog_pow(0); |
akita11 | 20:c3fadeef5842 | 264 | io.display(0); |
akita11 | 20:c3fadeef5842 | 265 | if(io.get_switch()) { |
gomihgy | 14:81347f8f0bda | 266 | led_mode = MODE_START; |
gomihgy | 14:81347f8f0bda | 267 | } |
akita11 | 20:c3fadeef5842 | 268 | break; |
gomihgy | 14:81347f8f0bda | 269 | case MODE_START: |
akita11 | 20:c3fadeef5842 | 270 | io.analog_pow(1); |
akita11 | 24:c6ef55535900 | 271 | io.power_save_mode(0); |
gomihgy | 14:81347f8f0bda | 272 | led_brightness += BRIGHTNESS_ADDVALUE; |
gomihgy | 14:81347f8f0bda | 273 | io.display(led_brightness); |
akita11 | 20:c3fadeef5842 | 274 | if(led_brightness >= BRIGHTNESS_MAXVALUE) { |
gomihgy | 14:81347f8f0bda | 275 | update_counter = 0; |
gomihgy | 16:fc66feb533d0 | 276 | io.calibrate_weight(); |
akita11 | 20:c3fadeef5842 | 277 | #if defined(PCB_VER1) || defined(PCB_VER2) |
gomihgy | 16:fc66feb533d0 | 278 | SWInit(); |
akita11 | 20:c3fadeef5842 | 279 | #endif |
gomihgy | 16:fc66feb533d0 | 280 | ble.startAdvertising(); |
gomihgy | 14:81347f8f0bda | 281 | led_mode = MODE_ON; |
akita11 | 24:c6ef55535900 | 282 | weight_s = 0.0; |
akita11 | 24:c6ef55535900 | 283 | sample = 0; |
gomihgy | 14:81347f8f0bda | 284 | } |
akita11 | 20:c3fadeef5842 | 285 | break; |
gomihgy | 14:81347f8f0bda | 286 | case MODE_ON: |
akita11 | 24:c6ef55535900 | 287 | #ifdef UART_DEBUG |
akita11 | 24:c6ef55535900 | 288 | // pc.printf("%d %d %.2f\r\n", io.get_switch(), io.get_weight_raw(), io.get_weight()); |
akita11 | 24:c6ef55535900 | 289 | #endif |
akita11 | 20:c3fadeef5842 | 290 | io.analog_pow(1); |
akita11 | 20:c3fadeef5842 | 291 | if(io.get_switch()) { |
gomihgy | 14:81347f8f0bda | 292 | led_mode = MODE_END; |
akita11 | 20:c3fadeef5842 | 293 | if(ble.getGapState().connected) { |
gomihgy | 14:81347f8f0bda | 294 | ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION); |
akita11 | 20:c3fadeef5842 | 295 | } else { |
gomihgy | 14:81347f8f0bda | 296 | ble.stopAdvertising(); |
gomihgy | 14:81347f8f0bda | 297 | } |
akita11 | 20:c3fadeef5842 | 298 | } else { |
gomihgy | 18:d1d15ec580fa | 299 | scale++; |
gomihgy | 18:d1d15ec580fa | 300 | ble.updateCharacteristicValue(WeightScale.getValueAttribute().getHandle(), |
akita11 | 20:c3fadeef5842 | 301 | (uint8_t *)&scale, |
akita11 | 20:c3fadeef5842 | 302 | sizeof(scale)); |
akita11 | 20:c3fadeef5842 | 303 | |
akita11 | 24:c6ef55535900 | 304 | weight_s += io.get_weight(); |
akita11 | 24:c6ef55535900 | 305 | sample++; |
akita11 | 24:c6ef55535900 | 306 | if (sample == Navg){ |
akita11 | 24:c6ef55535900 | 307 | weight = weight_s / (float)Navg; |
akita11 | 24:c6ef55535900 | 308 | io.display_value = (uint16_t)weight; |
akita11 | 24:c6ef55535900 | 309 | weight_s = 0.0; |
akita11 | 24:c6ef55535900 | 310 | sample = 0; |
akita11 | 24:c6ef55535900 | 311 | } |
akita11 | 24:c6ef55535900 | 312 | // pc.printf("weight=%.1f\r\n", weight); |
akita11 | 20:c3fadeef5842 | 313 | // io.display_value = 8888; // for LED soldering check |
akita11 | 20:c3fadeef5842 | 314 | #ifdef UART_DEBUG |
akita11 | 21:ab5b55d1947d | 315 | // pc.printf("%d\r\n", io._get_adc_raw(0)); |
akita11 | 20:c3fadeef5842 | 316 | // pc.printf("weight=%f %d / %d\r\n", weight, io.display_value, io._adc0); |
akita11 | 20:c3fadeef5842 | 317 | // pc.printf("%02x %02x %02x %02x\r\n", io._seg[0], io._seg[1], io._seg[2], io._seg[3]); |
akita11 | 20:c3fadeef5842 | 318 | #endif |
akita11 | 20:c3fadeef5842 | 319 | if(++update_counter >= 5) { |
akita11 | 20:c3fadeef5842 | 320 | weight_data = quick_ieee11073_from_float(weight); |
akita11 | 20:c3fadeef5842 | 321 | ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(), |
akita11 | 20:c3fadeef5842 | 322 | (uint8_t *)&weight_data, |
akita11 | 20:c3fadeef5842 | 323 | sizeof(weight_data)); |
akita11 | 20:c3fadeef5842 | 324 | update_counter = 0; |
gomihgy | 14:81347f8f0bda | 325 | } |
gomihgy | 14:81347f8f0bda | 326 | } |
akita11 | 20:c3fadeef5842 | 327 | break; |
gomihgy | 14:81347f8f0bda | 328 | case MODE_END: |
gomihgy | 14:81347f8f0bda | 329 | led_brightness -= BRIGHTNESS_ADDVALUE; |
gomihgy | 14:81347f8f0bda | 330 | io.display(led_brightness); |
akita11 | 20:c3fadeef5842 | 331 | if(led_brightness <= BRIGHTNESS_MINVALUE) { |
akita11 | 20:c3fadeef5842 | 332 | #if defined(PCB_VER1) || defined(PCB_VER2) |
gomihgy | 16:fc66feb533d0 | 333 | SWInit(); |
akita11 | 20:c3fadeef5842 | 334 | #endif |
gomihgy | 14:81347f8f0bda | 335 | led_mode = MODE_OFF; |
akita11 | 24:c6ef55535900 | 336 | io.power_save_mode(1); |
gomihgy | 14:81347f8f0bda | 337 | } |
akita11 | 20:c3fadeef5842 | 338 | break; |
gomihgy | 14:81347f8f0bda | 339 | } |
akita11 | 0:08c8325a21ec | 340 | } |
akita11 | 0:08c8325a21ec | 341 | } |