Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API nRF51822 mbed
Fork of KS7 by
Diff: main.cpp
- Branch:
- KS3
- Revision:
- 31:b5e19d153db4
- Parent:
- 30:f67850cc3cfe
- Child:
- 33:d7b53d548c33
- Child:
- 34:55b9048a1055
diff -r f67850cc3cfe -r b5e19d153db4 main.cpp
--- a/main.cpp Mon May 23 05:15:58 2016 +0000
+++ b/main.cpp Thu Jul 28 07:46:56 2016 +0000
@@ -1,12 +1,14 @@
#include "mbed.h"
-#include "io.h"
+#include "exio.h"
#include "BLE.h"
#include "DFUService.h"
#include "common.h"
#include <stdlib.h>
#include <arm_math.h>
+#include "CurrentTimeService.h"
+#include "MMA845x.h"
+#include "common.h"
-#define DISPLAY_DEMO // display random number for demo & LED check
// BLE
#define INTERVAL_500MSEC (500UL)
@@ -23,7 +25,7 @@
#define FIRMWARE_REVISION_STRING "v1.00.009@rev0028" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
// Weight Scale Service (Original)
-#define UUID_WEIGHT_SCALE_SERVICE (0x181D)
+//#define UUID_WEIGHT_SCALE_SERVICE (0x181D)
#if defined(PCB_VER1) || defined(PCB_VER2)
// Switch
@@ -36,6 +38,10 @@
#define MODE_START (1) // LED OFF -> ON
#define MODE_ON (2) // LED ON
#define MODE_END (3) // LED ON -> OFF
+/*S-----------------------------------------------------------*/
+State_t _state = S_WATCH;
+Mode_t _mode = M_SCALE;
+/*E-----------------------------------------------------------*/
// Led
#define LED_INTERVAL_MSEC (100)
@@ -45,7 +51,10 @@
// Properties
//io io;
-io io(P0_15, P0_13); // HX711's CLK & DAT
+/*S---------------------------------------------------------------*/
+//io io(P0_15, P0_13); // HX711's CLK & DAT
+exio io;
+/*E---------------------------------------------------------------*/
//io(P0_5, P0_4); // HX711's CLK & DAT for BLEnano debug
uint32_t weight_data;
float32_t weight = 0.0;
@@ -102,6 +111,12 @@
GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion};
GattService DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *));
+/*S--------------------------------------------------------------------*/
+CurrentTimeService *p_CurrentTimeService; // Current Time Service
+MMA845x *p_MMA845x; // 加速度センサー(本体)
+I2C i2c(P0_17, P0_18); // 加速度センサー(I2C)
+/*E--------------------------------------------------------------------*/
+
#ifdef PCB_VER1
bool check_joystick()
{
@@ -182,6 +197,8 @@
}
}
+
+
void BleInitialize(void)
{
@@ -219,21 +236,127 @@
ble.setAdvertisingInterval(INTERVAL_500MSEC);
ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */
+/*S-----------------------------------------------------------------------*/
+ /*CurrentTimeServiceの初期化とサービス追加*/
+ ble_date_time_t dt; // 1970/1/1 00:00:00 以降の初期値
+ dt.year = 2016;
+ dt.month = 7;
+ dt.day = 31;
+ dt.hours = 23;
+ dt.minutes = 45;
+ dt.seconds = 0;
+ p_CurrentTimeService = new CurrentTimeService(ble, dt);
+/*E-----------------------------------------------------------------------*/
ble.addService(HWS);
ble.addService(DIS);
DFUService dfu(ble);
+
}
+/*S---------------------------------------------------------------------*/
+//void zTap(void)
+//{
+//}
+void AccelInit(void)
+{
+ /*加速度センサー初期化*/
+ p_MMA845x = new MMA845x(i2c, MMA845x::SA0_VSS);
+ //p_MMA845x->attachZAxisPulse(&zTap);
+
+ const int ADR = 0x3A;
+ char data[2], cmd = 0x0D;
+ i2c.write(ADR, &cmd, 1, true);
+ i2c.read(ADR, &data[0], 1); // data[0]: Who am I?
+
+ //p_MMA845x->enableMotionMode();
+ //p_MMA845x->registerDump();
+ p_MMA845x->enableDataReadyMode();
+}
+/*モード決定*/
+void GetMode(void)
+{
+ // 横置きなら時計タイマーモード、縦置きなら計量モード
+ //int x = p_MMA845x->getX();
+ //int y = p_MMA845x->getY();
+ //int z = p_MMA845x->getZ();
+ float xx = p_MMA845x->getXX();
+ _mode = (xx < 0.3) ? M_SCALE : M_WATCHTIMER;
+#ifdef UART_DEBUG
+ pc.printf("KATAMUKI xx=%f, mode=%s\r\n", xx, (_mode == M_SCALE) ? "SCALE" : "WATCHTIMER");
+#endif
+}
+/*STATE決定*/
+void GetState(int sw)
+{
+ ble_date_time_t dt;
+ // スイッチ長押しでタイマー、通常押しで時間表示
+ if(sw == exio::LongPressed) {
+ p_CurrentTimeService->setCounter(0); // タイマーカウンタリセット
+ io.displaySeconds(0);
+ _state = S_TIMER;
+#ifdef UART_DEBUG
+ pc.printf("S_TIMER START\r\n");
+#endif
+ } else if(sw == exio::Pressed){
+ p_CurrentTimeService->setTm(60/*sec*/); /*表示タイムアウト*/
+ p_CurrentTimeService->readDateTime(dt); // 時計取得
+ io.displayHHMM(dt);
+ _state = S_WATCH;
+#ifdef UART_DEBUG
+ pc.printf("S_WATCH START\r\n");
+#endif
+ }
+}
+int TMain(int sw)
+{
+ /*下記は時計/タイマーモード*/
+ ble_date_time_t dt;
+ int t;
+ switch(_state){
+ case S_TIMER:
+ /*タイマー表示の場合*/
+ t = p_CurrentTimeService->getCounter();
+ io.displaySeconds(t); // タイマー表示
+ // スイッチ押された場合終了
+ if(sw != exio::None){
+#ifdef UART_DEBUG
+ pc.printf("S_TIMER EXIT\r\n");
+#endif
+ return 1; // モード終了
+ }
+ break;
+ case S_WATCH:
+ /*時計表示の場合*/
+ p_CurrentTimeService->readDateTime(dt); // 時計取得
+ io.displayHHMM(dt); // 時計表示
+ // 表示タイムアウトもしくはスイッチ押しの場合終了
+ if(/*p_CurrentTimeService->getTm() <= 0 ||*/ sw != exio::None){ // タイムアウトorスイッチ押し
+#ifdef UART_DEBUG
+ pc.printf("S_WATCH EXIT\r\n");
+#endif
+ return 1; // モード終了
+ }
+ break;
+ default:
+ break;
+ }
+ return 0; // モード継続
+}
+/*E-----------------------------------------------------------------------*/
//DigitalOut _reg_ps(P0_1, 1); // 1=normal, 0=power_save
//DigitalOut _adc_rate(P0_6, 1); // 0=10Hz, 1=80Hz (HX711's RATE pin)
int main()
{
+/*S----------------------------------------------------*/
+ int sw;
+/*E----------------------------------------------------*/
float weight_s;
int Navg = 5;
int sample = 0;
#ifdef UART_DEBUG
pc.baud(UART_BAUD_RATE);
+ pc.printf("%s(%d): Program Start\r\n", __FILE__, __LINE__);
// for checking SPI configuration (SPI1 is used_)
// pc.printf("SPI->PSELSCK = %x\r\n", NRF_SPI0->PSELSCK); // will be 15 (P0_15)
// pc.printf("SPI->PSELMISO = %x\r\n", NRF_SPI0->PSELMISO); // will be 13 (P0_13)
@@ -249,6 +372,9 @@
#endif
BleInitialize();
+/*S-----------------------------------------------*/
+ AccelInit();
+/*E-----------------------------------------------*/
AppInit();
led_mode = MODE_OFF;
@@ -269,18 +395,46 @@
case MODE_OFF:
io.analog_pow(0);
io.display(0);
- if(io.get_switch()) {
+/*S--------------------------------------------------------------------*/
+ sw = io.get_switch();
+ io.switch_reset();
+ if(sw != exio::None) {/*通常押しもしくは長押し*/
+ GetMode(); /*傾きからモード決定*/
+ if(_mode == M_WATCHTIMER){
+ GetState(sw); //WATCH or TIMER ?
+ }
+#ifdef UART_DEBUG
+ pc.printf("GO MODE_START\r\n");
+#endif
+/*E--------------------------------------------------------------------*/
led_mode = MODE_START;
}
break;
case MODE_START:
io.analog_pow(1);
io.power_save_mode(0);
+/*S----------------------------------------------------------------*/
+ // タイマー時はここで毎回更新
+ if(_mode == M_WATCHTIMER && _state == S_TIMER){
+ p_CurrentTimeService->setCounter(0); // タイマーカウンタリセット
+ io.displaySeconds(0);
+ } else if(_mode == M_SCALE){
+ io.display_value = 0;
+ }
+/*E----------------------------------------------------------------*/
led_brightness += BRIGHTNESS_ADDVALUE;
io.display(led_brightness);
if(led_brightness >= BRIGHTNESS_MAXVALUE) {
+/*S-----------------------------------------------------------------------*/
+ if(_mode == M_WATCHTIMER){
+ ble.startAdvertising();
+ led_mode = MODE_ON;
+ break; /*時間/タイマーモードはここで終了*/
+ }
+/*E-----------------------------------------------------------------------*/
+ /*計量モードの初期処理*/
update_counter = 0;
- io.calibrate_weight();
+ io.calibrate_weight(); /*ここがゼロ補正*/
#if defined(PCB_VER1) || defined(PCB_VER2)
SWInit();
#endif
@@ -292,7 +446,7 @@
break;
case MODE_ON:
#ifdef UART_DEBUG
- pc.printf("%d %d %.2f\r\n", io.get_switch(), io.get_weight_raw(), io.get_weight());
+ //pc.printf("%d %d %.2f\r\n", io.get_switch(), io.get_weight_raw(), io.get_weight());
#endif
io.analog_pow(1);
#ifdef DISPLAY_DEMO
@@ -306,7 +460,23 @@
sizeof(weight_data));
}
#else
- if(io.get_switch()) {
+/*S-------------------------------------------------------------------------*/
+ sw = io.get_switch();
+ io.switch_reset();
+ if(_mode == M_WATCHTIMER){
+ int ret = TMain(sw); // 時間/タイマー処理へ
+ if(ret == 1){ // モード終了
+ goto L010;
+ }
+ break; // 時間/タイマーモードはここで終了
+ }
+ /*下記は計量モード*/
+ if(sw == exio::Pressed) {
+L010:
+#ifdef UART_DEBUG
+ pc.printf("GO MODE_END\r\n");
+#endif
+/*E-------------------------------------------------------------------------*/
led_mode = MODE_END;
if(ble.getGapState().connected) {
ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
@@ -326,6 +496,9 @@
io.display_value = (uint16_t)weight;
weight_s = 0.0;
sample = 0;
+#ifdef UART_DEBUG
+ pc.printf("weight=%.1f\r\n", weight);
+#endif
}
// pc.printf("weight=%.1f\r\n", weight);
// io.display_value = 8888; // for LED soldering check
