Skip to content

Commit

Permalink
Make modal
Browse files Browse the repository at this point in the history
  • Loading branch information
efyang committed May 11, 2019
1 parent a3d8f63 commit 10b3cad
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 23 deletions.
6 changes: 6 additions & 0 deletions esp32-heart-pair-handler-device-client/BLE_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include <BLEServer.h>
#include <Arduino.h>
#include "color.h"
#include "opmode.h"

#include "blemoodcharacteristicupdatercallbacks.h"
#include "blemodeupdatercallbacks.h"
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

Expand All @@ -21,6 +23,8 @@

#define LAMP_COLOR_CHARACTERISTIC_UUID "c367b354-c1cf-43d6-8c3f-24288fc231ce"

#define MODE_CHARACTERISTIC_UUID "ae2c2e59-fb28-4737-9144-7dc72d69ccf4"

void setup_ble_gatt() {
Serial.println("Starting BLE work!");

Expand All @@ -36,6 +40,8 @@ void setup_ble_gatt() {

BLECharacteristic *pLampColorCharacteristic = create_mood_color_characteristic(pService, LAMP_COLOR_CHARACTERISTIC_UUID, &lampColor);

BLECharacteristic *pModeCharacteristic = create_mode_characteristic(pService, MODE_CHARACTERISTIC_UUID, &opmode);

pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
Expand Down
41 changes: 41 additions & 0 deletions esp32-heart-pair-handler-device-client/blemodeupdatercallbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef MODE_UPDATER_H
#define MODE_UPDATER_H

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <Arduino.h>

class BLEModeUpdaterCallbacks: public BLECharacteristicCallbacks {
private:
uint8_t* mode_ref;
uint8_t tmp[1];
public:
BLEModeUpdaterCallbacks(uint8_t* val_ref) {
mode_ref = val_ref;
}

void onRead(BLECharacteristic* pCharacteristic) {
pCharacteristic->setValue(tmp, 1);
}

void onWrite(BLECharacteristic* pCharacteristic) {
*mode_ref = *(pCharacteristic->getData());
pCharacteristic->setValue(tmp, 1);
}
};

BLECharacteristic * create_mode_characteristic(BLEService *pService, std::string uuid, uint8_t* opmode) {
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
uuid,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
uint8_t tmp[1];
tmp[0] = *opmode;
pCharacteristic->setValue(tmp, 1);
pCharacteristic->setCallbacks(new BLEModeUpdaterCallbacks(opmode));
return pCharacteristic;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
#include <Arduino.h>
#include <FastLED.h>

std::string colorToString(CRGB* color) {
char buffer [50];
sprintf(buffer, "%02X%02X%02X", color->r, color->g, color->b);
return buffer;
}

CRGB bytesToColor(uint8_t* input) {
return CRGB(*(input + 0), *(input + 1), *(input + 2));
}
Expand All @@ -33,16 +27,12 @@ class BLEMoodCharacteristicUpdaterCallbacks: public BLECharacteristicCallbacks {
}

void onRead(BLECharacteristic* pCharacteristic) {
Serial.println("Read value");
Serial.println(colorToString(color_ref).c_str());
colorToBytes(color_ref, tmp);
pCharacteristic->setValue(tmp, 3);
}

void onWrite(BLECharacteristic* pCharacteristic) {
Serial.println("Wrote value");
*color_ref = bytesToColor(pCharacteristic->getData());
Serial.println(colorToString(color_ref).c_str());
colorToBytes(color_ref, tmp);
pCharacteristic->setValue(tmp, 3);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "blemoodcharacteristicupdatercallbacks.h"
#include "blemodeupdatercallbacks.h"
#include "BLE_setup.h"
#include "wifi_setup.h"
#include <FastLED.h>
#include "touch_sensor.h"
#include "color.h"
#include "opmode.h"

#define NUM_HEART_LEDS 27
#define HEART_LEDS_DATA_PIN 26
Expand Down Expand Up @@ -56,22 +58,52 @@ void loop() {
fear_mood_sensor.update_state();
anger_mood_sensor.update_state();

button_leds[0] = ifThenColor(happy_mood_sensor.latched, happyColor);
button_leds[1] = ifThenColor(sad_mood_sensor.latched, sadColor);
button_leds[2] = ifThenColor(fear_mood_sensor.latched, fearColor);
button_leds[3] = ifThenColor(anger_mood_sensor.latched, angerColor);
switch (opmode) {
case NORMAL_MODE:
button_leds[0] = ifThenColor(happy_mood_sensor.latched, happyColor);
button_leds[1] = ifThenColor(sad_mood_sensor.latched, sadColor);
button_leds[2] = ifThenColor(fear_mood_sensor.latched, fearColor);
button_leds[3] = ifThenColor(anger_mood_sensor.latched, angerColor);

if (previous_held == false && love_hold_sensor.pressed) {
gt = 0;
}
previous_held = love_hold_sensor.pressed;

EVERY_N_MILLISECONDS( 10 ) {
gt += GT_STEP;
}

set_all_leds(heart_leds, NUM_HEART_LEDS, ifThenColor(love_hold_sensor.pressed, CHSV(60, 0, fade_in_out_2(gt) / 2.5)));
break;

case LAMP_MODE:
set_all_leds(heart_leds, NUM_HEART_LEDS, lampColor);
break;

if (previous_held == false && love_hold_sensor.pressed) {
gt = 0;
case LAMP_MODE_NO_INDICATORS:
set_all_leds(heart_leds, NUM_HEART_LEDS, lampColor);
break;

case CONFIGURE_MODE:
button_leds[0] = happyColor;
button_leds[1] = sadColor;
button_leds[2] = fearColor;
button_leds[3] = angerColor;
set_all_leds(heart_leds, NUM_HEART_LEDS, loveColor);
break;

case PROM_MODE:
prom_loop(heart_leds, NUM_HEART_LEDS);
break;

case OFF_MODE:
default:
set_all_leds(heart_leds, NUM_HEART_LEDS, CRGB::Black);
set_all_leds(button_leds, NUM_BUTTON_LEDS, CRGB::Black);
break;
}
previous_held = love_hold_sensor.pressed;

EVERY_N_MILLISECONDS( 10 ) {
gt += GT_STEP;
}

// prom_loop(heart_leds, NUM_HEART_LEDS);
set_all_leds(heart_leds, NUM_HEART_LEDS, ifThenColor(love_hold_sensor.pressed, CHSV(60, 0, fade_in_out_2(gt) / 2.5)));
FastLED.show();
}

Expand Down
14 changes: 14 additions & 0 deletions esp32-heart-pair-handler-device-client/opmode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef MODE_H
#define MODE_H

#define NORMAL_MODE 0
#define LAMP_MODE 1
#define LAMP_MODE_NO_INDICATORS 2
#define CONFIGURE_MODE 3
#define PROM_MODE 4
#define OFF_MODE 5

uint8_t opmode = NORMAL_MODE;


#endif

0 comments on commit 10b3cad

Please sign in to comment.