Skip to content

Commit

Permalink
added control Widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoincorrect committed Mar 22, 2021
1 parent 7666620 commit 5ec8588
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 167 deletions.
3 changes: 2 additions & 1 deletion Embedded_system/smart_mask/src/sensors/sensor_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ void sensor_handles_init(void)
for (sensor_t s_i = SENSOR_FIRST; s_i <= SENSOR_LAST; s_i++)
{
ctrl = sensor_handle_get_control(s_i);
//ctrl->gain = SAADC_CH_CONFIG_GAIN_Gain1_6;
ctrl->gain = SAADC_CH_CONFIG_GAIN_Gain1_6;
ctrl->enable = true;
ctrl->sample_period_ms = 1000; // ms
ctrl->sample_period_ms = 100; // ms
}

s_h_1.hardware.pwr_pin = SENSOR_1_PWR_PIN;
Expand Down
22 changes: 14 additions & 8 deletions Embedded_system/smart_mask/src/sensors/sensor_sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
************************/

#define SAMPLES_IN_BUFFER SENSORS_COUNT
#define SAMPLE_RATE_MS 1000
#define INITIAL_SAMPLE_RATE_MS 200

/*************************
* Static Variables
Expand Down Expand Up @@ -70,7 +70,8 @@ ret_code_t saadc_config_channel(sensor_t sensor)
NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NULL);

conf.pin_p = hardware->analog_input;
conf.gain = SAADC_CH_CONFIG_GAIN_Gain1_6;
conf.gain = ctrl->gain;
//conf.reference = NRF_SAADC_REFERENCE_VDD4;
conf.reference = NRF_SAADC_REFERENCE_INTERNAL;
return nrfx_saadc_channel_init(hardware->adc_chanel, &conf);
}
Expand Down Expand Up @@ -124,6 +125,7 @@ static ret_code_t sample_one_sensor(sensor_t sensor)
err = sensor_handle_add_value(sensor, adc_val);
APP_ERROR_CHECK(err);
nrf_gpio_pin_clear(hardware->pwr_pin);
NRF_LOG_INFO("sensor %d adc_val %d", sensor + 1, adc_val);

return err;
}
Expand All @@ -140,21 +142,25 @@ static ret_code_t sample_all_sensors(void)

ctrl = sensor_handle_get_control(SENSOR_1);
if (ctrl->enable)
sample_one_sensor(SENSOR_1);
{
//err = sensor_handle_add_value(SENSOR_1, 0);
err = sample_one_sensor(SENSOR_1);
APP_ERROR_CHECK(err);
}

ctrl = sensor_handle_get_control(SENSOR_2);
if (ctrl->enable)
{
// err = nrfx_saadc_sample_convert(SENSOR_2_ADC_CHANNEL, &adc_val);
err = sensor_handle_add_value(SENSOR_2, mock_adc++);
//err = sample_one_sensor(SENSOR_2);
err = sensor_handle_add_value(SENSOR_2, 0);
APP_ERROR_CHECK(err);
}

ctrl = sensor_handle_get_control(SENSOR_3);
if (ctrl->enable)
{
// err = nrfx_saadc_sample_convert(SENSOR_3_ADC_CHANNEL, &adc_val);
err = sensor_handle_add_value(SENSOR_3, 0);
err = sensor_handle_add_value(SENSOR_3, mock_adc++);
APP_ERROR_CHECK(err);
}

Expand Down Expand Up @@ -203,7 +209,7 @@ static ret_code_t saadc_timer_init(void)
APP_ERROR_CHECK(err_code);

uint32_t time_ticks =
nrfx_timer_ms_to_ticks(&saadc_timer_instance, SAMPLE_RATE_MS);
nrfx_timer_ms_to_ticks(&saadc_timer_instance, INITIAL_SAMPLE_RATE_MS);

nrfx_timer_extended_compare(&saadc_timer_instance, NRF_TIMER_CC_CHANNEL0,
time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
Expand All @@ -230,7 +236,7 @@ static ret_code_t update_saadc_timer(sensor_t sensor)
&saadc_timer_instance, &timer_conf, saadc_timer_handler);
APP_ERROR_CHECK(err);

sensor_ctrl_t* ctrl = sensor_handle_get_control(sensor);
sensor_ctrl_t * ctrl = sensor_handle_get_control(sensor);
uint32_t time_ticks =
nrfx_timer_ms_to_ticks(&saadc_timer_instance, ctrl->sample_period_ms);

Expand Down
2 changes: 1 addition & 1 deletion Mobile_app/smart_mask/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_blue","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.7.2\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.28\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.3.2+4\\\\","dependencies":[]}],"android":[{"name":"flutter_blue","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.7.2\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.28\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.3.2+4\\\\","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_macos-0.0.4+8\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.3.2+4\\\\","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-0.0.1+2\\\\","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\Flutter_1_12_13\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-0.0.4+3\\\\","dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2021-03-18 13:39:53.775359","version":"1.22.6"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_blue","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.7.2\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-2.0.1\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+3\\\\","dependencies":[]}],"android":[{"name":"flutter_blue","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.7.2\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-2.0.1\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+3\\\\","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_macos-2.0.0\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-2.0.0+3\\\\","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-2.0.0\\\\","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"C:\\\\Users\\\\maxim\\\\Programming\\\\SDKs\\\\Flutter\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-2.0.0\\\\","dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2021-03-22 13:34:03.330506","version":"2.0.2"}
6 changes: 3 additions & 3 deletions Mobile_app/smart_mask/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ class _TabControlState extends State<TabControl> {
appBar: AppBar(
title: Row(
children: <Widget>[
const Text('Q-Blue'),
const Text('Smart Mask'),
Expanded(child: Container()),
StreamBuilder<bool>(
stream: bluetoothBloc.isConnected,
initialData: false,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == true) {
return Text("Device Connected");
return Text("Connected");
}
return Text("Device Disconnected");
return Text("Disconnected");
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@
// contain the bluetooth state management for the app
// manage data stream (sensors, and notification)

import 'dart:math';
import 'dart:typed_data';

import 'package:flutter_blue/flutter_blue.dart';
import 'package:smart_mask/src/logic/database/models/sensor_model.dart';
import 'package:smart_mask/src/logic/database/models/sensor_control_model.dart';
import 'package:smart_mask/src/logic/repositories/sensor_data_repo.dart';
import './smart_mask_services_const.dart' as servicesConst;
import 'package:smart_mask/src/logic/blocs/bluetooth/smart_mask_services_const.dart'
as smsConst;

final String sensorService = smsConst.S["sensorMeasurementService"]["UUID"];

final String sensorService = servicesConst.s["smartMaskService"]["UUID"];
final String sensorCharactUUID =
servicesConst.s["smartMaskService"]["characteristics"]["sensors"]["UUID"];
final Map<String, Map<String, String>> valuesChars =
smsConst.S["sensorMeasurementService"]["characteristics"]["values"];

final Map<String, Map<String, String>> controlChars =
smsConst.S["sensorMeasurementService"]["characteristics"]["control"];

class BluetoothBloc {
int _sampleRateValue;
SensorGain _gainValue;

// Map<Sensor, SensorControl> _sensorControls;
Stream<BluetoothState> _bluetoothState;
SensorDataRepository _sensorDataRepository;
Stream<bool> _isConnected;
Expand All @@ -23,6 +36,8 @@ class BluetoothBloc {
_bluetoothState = FlutterBlue.instance.state;
_isConnected =
Stream.periodic(Duration(seconds: 2)).asyncMap((_) => checkConnected());
_sampleRateValue = 200;
_gainValue = SensorGain.one;
}

Future<bool> checkConnected() async {
Expand All @@ -47,45 +62,76 @@ class BluetoothBloc {
return _isConnected;
}

onUpdateServices(List<BluetoothService> services) {
onUpdateServices(List<BluetoothService> services) async {
for (var service in services) {
// print("service: ${service.uuid.toString()}");
updateCharacteristics(service.characteristics);
}
}

updateCharacteristics(List<BluetoothCharacteristic> characteristics) {
updateCharacteristics(List<BluetoothCharacteristic> characteristics) async {
for (var characteristic in characteristics) {
// print("characteristic: ${characteristic.uuid.toString()}");
if (characteristic.uuid.toString() == sensorCharactUUID) {
setSensorReceive(characteristic);
var uuid = characteristic.uuid.toString();

dynamic uuids = [];
valuesChars.forEach((key, value) {
uuids.add(value["UUID"]);
});

if (uuids.contains(uuid)) {
await setSensorReceive(characteristic);
}
}
}

setSensorReceive(BluetoothCharacteristic characteristic) {
// print("sensor chararcteristic ${characteristic.uuid.toString()}");
characteristic.value.listen(onReceiveValue);
if (!characteristic.isNotifying) characteristic.setNotifyValue(true);
setSensorReceive(BluetoothCharacteristic characteristic) async {
characteristic.value.listen((x) => onReceiveValue(x, characteristic));
if (!characteristic.isNotifying) {
await characteristic.setNotifyValue(true);
print(characteristic.isNotifying);
}
}

onReceiveValue(List<int> values) async {
var sensorDatas = List<SensorData>();
parseSensorValues(values, sensorDatas);
onReceiveValue(List<int> values, BluetoothCharacteristic char) async {
List<SensorData> sensorDatas;
final String uuid = char.uuid.toString();
final sensor = sensorFromBLEchararcteristicUUID(uuid);
sensorDatas = await parseSensorValues(values, sensor);
for (var sensorData in sensorDatas) {
await _sensorDataRepository.insertSensorData(sensorData);
}
}

List<SensorData> parseSensorValues(
List<int> values, List<SensorData> sensorDatas) {
for (var i = 0, j = 0; i < values.length; i += 2, j++) {
int value = values[i] + (values[i + 1] << 8);
sensorDatas.add(SensorData.fromSensorAndValue(Sensor.values[j], value));
Future<List<SensorData>> parseSensorValues(
List<int> values, Sensor sensor) async {
List<SensorData> sensorDatas = [];
int timeNow = DateTime.now().millisecondsSinceEpoch;

for (var i = 0, j = smsConst.SENSOR_VALS_PER_PACKET;
i < values.length;
i += 2, j--) {
var buffer = new Uint8List(2).buffer;
var bdata = new ByteData.view(buffer);
bdata.setUint8(1, values[i]);
bdata.setUint8(0, values[i + 1]);
int value = bdata.getInt16(0);
// Since we receive a list of value from one sensor, we need to assign
// a different time stamp for each value
int calcTime = timeNow - j * smsConst.SAMPLE_PERIOD_MS;
sensorDatas.add(SensorData.fromSensorAndValue(sensor, value, calcTime));
}
// print("sensor data added");
return sensorDatas;
}

setSampleRateValue(int newValue) {
if (!validateSensorSampleRate(newValue)) return null;
_sampleRateValue = newValue.round();
}

int getSampleRateValue() => _sampleRateValue;

setgainValue(SensorGain newValue) => _gainValue = newValue;

SensorGain getgainValue() => _gainValue;

dispose() {}
}
108 changes: 54 additions & 54 deletions Mobile_app/smart_mask/lib/src/logic/blocs/bluetooth/bluetooth_sms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@
// of the BLE device connected. handle the sensor data by storing them to
// database (or other repositories)

import 'package:flutter_blue/flutter_blue.dart';
import 'package:smart_mask/src/logic/database/models/sensor_model.dart';
import 'package:smart_mask/src/logic/repositories/sensor_data_repo.dart';
import './smart_mask_services_const.dart' as servicesConst;

class SensorManagementService {
SensorDataRepository _sensorDataRepository;
final String sensorService = servicesConst.s["smartMaskService"]["UUID"];
final String sensorCharactUUID =
servicesConst.s["smartMaskService"]["characteristics"]["sensors"]["UUID"];

SensorManagementService() {
this._sensorDataRepository = SensorDataRepository();
}

onUpdateServices(List<BluetoothService> services) {
for (var service in services) {
print("service: ${service.uuid.toString()}");
updateCharacteristics(service.characteristics);
}
}

updateCharacteristics(List<BluetoothCharacteristic> characteristics) {
for (var characteristic in characteristics) {
print("characteristic: ${characteristic.uuid.toString()}");
if (characteristic.uuid.toString() == sensorCharactUUID) {
setSensorReceive(characteristic);
}
}
}

setSensorReceive(BluetoothCharacteristic characteristic) {
print("sensor chararcteristic ${characteristic.uuid.toString()}");
characteristic.value.listen(onReceiveValue);
if (!characteristic.isNotifying) characteristic.setNotifyValue(true);
}

onReceiveValue(List<int> values) async {
var sensorDatas = List<SensorData>();
parseSensorValues(values, sensorDatas);
for (var sensorData in sensorDatas) {
await _sensorDataRepository.insertSensorData(sensorData);
}
}

List<SensorData> parseSensorValues(
List<int> values, List<SensorData> sensorDatas) {
for (var i = 0, j = 0; i < values.length; i += 2, j++) {
int value = values[i] + (values[i + 1] << 8);
sensorDatas.add(SensorData.fromSensorAndValue(Sensor.values[j], value));
}
return sensorDatas;
}
}
// import 'package:flutter_blue/flutter_blue.dart';
// import 'package:smart_mask/src/logic/database/models/sensor_model.dart';
// import 'package:smart_mask/src/logic/repositories/sensor_data_repo.dart';
// import './smart_mask_services_const.dart' as servicesConst;
//
// class SensorManagementService {
// SensorDataRepository _sensorDataRepository;
// final String sensorService = servicesConst.s["smartMaskService"]["UUID"];
// final String sensorCharactUUID =
// servicesConst.s["smartMaskService"]["characteristics"]["sensors"]["UUID"];
//
// SensorManagementService() {
// this._sensorDataRepository = SensorDataRepository();
// }
//
// onUpdateServices(List<BluetoothService> services) {
// for (var service in services) {
// print("service: ${service.uuid.toString()}");
// updateCharacteristics(service.characteristics);
// }
// }
//
// updateCharacteristics(List<BluetoothCharacteristic> characteristics) {
// for (var characteristic in characteristics) {
// print("characteristic: ${characteristic.uuid.toString()}");
// if (characteristic.uuid.toString() == sensorCharactUUID) {
// setSensorReceive(characteristic);
// }
// }
// }
//
// setSensorReceive(BluetoothCharacteristic characteristic) {
// print("sensor chararcteristic ${characteristic.uuid.toString()}");
// characteristic.value.listen(onReceiveValue);
// if (!characteristic.isNotifying) characteristic.setNotifyValue(true);
// }
//
// onReceiveValue(List<int> values) async {
// var sensorDatas = List<SensorData>();
// parseSensorValues(values, sensorDatas);
// for (var sensorData in sensorDatas) {
// await _sensorDataRepository.insertSensorData(sensorData);
// }
// }
//
// List<SensorData> parseSensorValues(
// List<int> values, List<SensorData> sensorDatas) {
// for (var i = 0, j = 0; i < values.length; i += 2, j++) {
// int value = values[i] + (values[i + 1] << 8);
// sensorDatas.add(SensorData.fromSensorAndValue(Sensor.values[j], value));
// }
// return sensorDatas;
// }
// }
Loading

0 comments on commit 5ec8588

Please sign in to comment.