BLE some values correct / button added: works / started with onWrite()

This commit is contained in:
vanarebane 2025-02-28 09:12:51 +02:00
parent 92a4df4a70
commit 638ad3535c
6 changed files with 80 additions and 58 deletions

View File

@ -26,8 +26,8 @@
<input name="position_set" value="0" type="text" disabled>
</div>
<div class="profilegroup">
<label for="light" class="pwroff">Light:</label>
<input style="text-align: right;" name="light" value="0" type="text" disabled>
<label for="light_raw" class="pwroff">Light:</label>
<input style="text-align: right;" name="light_raw" value="0" type="text" disabled>
<span>millilux</span>
</div>
<div class="profilegroup">
@ -38,7 +38,7 @@
</div>
<div class="profilegroup">
<label for="knob_button" class="pwroff">Button:</label>
<input style="width: 100%;" name="knob_button" value="0" type="text" disabled>
<button style="width: 100%; text-align: center;" class="statebtns" id="knob_button">Push</button>
</div>
<div class="profilegroup">
@ -95,13 +95,21 @@
$('#request').on('click', async event => {sk.connect()});
// Position notifications
sk.addListener('handlePositionNotifications', (e)=>{
$('[name="position_cur"]').val(e.detail.value)
})
// Position set value notifications
sk.addListener('handlePositionSetNotifications', (e)=>{
$('[name="position_set"]').val(e.detail.value)
})
// Lux reading notification / 1 per sec
sk.addListener('handleLuxNotifications', (e)=>{
$('[name="light_raw"]').val(e.detail.value)
})
// Scale push notifications
sk.addListener('handlePushNotifications', (e)=>{
if(e.detail.value){
$('#strain_push').addClass('active')
@ -110,13 +118,22 @@
$('#strain_push').removeClass('active')
}
})
// Scale reading notifications / 1 per sec
sk.addListener('handleScaleNotifications', (e)=>{
$('[name="strain_raw"]').val(e.detail.value)
})
sk.addListener('handleButtonNotifications', (e)=>{
$('[name="knob_button"]').val(e.detail.value)
})
// Button notification
sk.addListener('handleButtonNotifications', (e)=>{
if(e.detail.value){
$('#knob_button').addClass('active')
}
else{
$('#knob_button').removeClass('active')
}
})
/////////////////////////////////////////////////
/// PROCESSOR

View File

@ -180,7 +180,7 @@ const SmartKnob = class {
let value = 0
for(let i=4; i<buffer.byteLength; i++){
value += buffer.getUint8(i)
value += (i>4) ? buffer.getUint8(i)*(256*(i-4)) : buffer.getUint8(i)
}
console.log(buffer, buffer.byteLength, response, value)
@ -198,11 +198,20 @@ const SmartKnob = class {
break;
case 4:
value = buffer.getUint8(4)+(buffer.getUint8(5)*256)+(buffer.getUint8(6))+(buffer.getUint8(7)*256)
if(buffer.getUint8(6) > 0){ // if last two last bits have any value, the value is in negative
value = (value-131071)
}
document.dispatchEvent(new CustomEvent('handlePositionNotifications', {detail: {'value': parseFloat(value)}}))
break;
case 5:
document.dispatchEvent(new CustomEvent('handlePositionSetNotifications', {detail: {'value': parseFloat(value)}}))
document.dispatchEvent(new CustomEvent('handlePositionSetNotifications', {detail: {'value': parseFloat(value-1)}}))
break;
case 6:
document.dispatchEvent(new CustomEvent('handleLuxNotifications', {detail: {'value': parseFloat(value-1)}}))
break;
}
}

View File

@ -131,6 +131,10 @@ InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task, BLE
assert(display_task != nullptr);
#endif
#if PIN_BUTTON_NEXT
pinMode(PIN_BUTTON_NEXT, INPUT_PULLUP);
#endif
log_queue_ = xQueueCreate(10, sizeof(std::string *));
assert(log_queue_ != NULL);
@ -247,20 +251,30 @@ void InterfaceTask::updateHardware() {
static uint32_t last_als;
if (millis() - last_als > 1000) {
snprintf(buf_, sizeof(buf_), "millilux: %.2f", lux*1000);
ble_task_.updateLux(lux*1000);
log(buf_);
last_als = millis();
}
#endif
#if PIN_BUTTON_NEXT
if (digitalRead(PIN_BUTTON_NEXT) == HIGH) {
ble_task_.updateButton(false);
}
else{
ble_task_.updateButton(true);
}
#endif
#if SK_STRAIN
if (scale.wait_ready_timeout(100)) {
int32_t reading = scale.read();
//ble_task_.updateScale(reading);
static uint32_t last_reading_display;
if (millis() - last_reading_display > 1000) {
snprintf(buf_, sizeof(buf_), "HX711 reading: %d", reading);
ble_task_.updateScale(reading);
log(buf_);
last_reading_display = millis();
}

View File

@ -15,6 +15,17 @@ class KnobServerCallbacks: public BLEServerCallbacks {
}
};
class KnobCharacteristicCallBack : public BLECharacteristicCallbacks{
public:
//This method not called
void onWrite(BLECharacteristic *pCharacteristic) override{
std::string rxValue = pCharacteristic->getValue();
Serial.print("value received = ");
Serial.println(rxValue.c_str());
log_i("data is received");
}
};
BLETask::BLETask(const uint8_t task_core) : Task("BLE", 2700, 1, task_core) {
queue_ = xQueueCreate(5, sizeof(Message));
assert(queue_ != NULL);
@ -45,6 +56,7 @@ void BLETask::run() {
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new KnobServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
@ -59,6 +71,8 @@ void BLETask::run() {
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->setCallbacks(new KnobCharacteristicCallBack());
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
@ -99,44 +113,7 @@ void BLETask::run() {
// notify changed value
if (deviceConnected) {
// if (xQueueReceive(knob_state_queue_, &state, portMAX_DELAY) == pdFALSE) {
// if (current_position != state.current_position){
// current_position = state.current_position;
// sendNotify("V+"+num_positions);
// }
// if (num_positions != state.config.num_positions){
// num_positions = state.config.num_positions;
// sendNotify("M+"+num_positions);
// }
// }
// if(hasUpdate){
// hasUpdate = false;
// // formulate response
// response = 0;
// if(button_state_) response += 1;
// if(press_value_unit_) response += 2;
// // if(...) response += 4;
// // if(...) response += 8;
// // if(...) response += 16;
// // if(...) response += 32;
// // if(...) response += 64;
// // if(...) response += 128;
// // if(...) response += 256;
// // current position into bit 2-5
// response += current_position*65536;
// // current position_set into bit 7-8
// //response += num_positions*(256^7);
// pCharacteristic->setValue((uint8_t*)&response, 16);
// pCharacteristic->notify();
// delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
// }
if(button_state_old != button_state_){
button_state_old = button_state_;
sendNotify(1, button_state_old);
@ -161,6 +138,11 @@ void BLETask::run() {
num_positions = state.config.num_positions;
sendNotify(5, num_positions);
}
if (lux_value_old != lux_value_){
lux_value_old = lux_value_;
sendNotify(6, (uint32_t)&lux_value_old);
}
}
@ -190,8 +172,8 @@ void BLETask::run() {
void BLETask::sendNotify(int key, bool data){
uint8_t temp[2];
temp[0] = key;
if(data) temp[1] = 0;
else temp[1] = 1;
if(data) temp[1] = 1;
else temp[1] = 0;
pCharacteristic->setValue((uint8_t*)&temp, 2);
pCharacteristic->notify();
delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
@ -213,17 +195,18 @@ void BLETask::sendNotify(int key, uint32_t data32){
void BLETask::updateScale(int32_t new_press_value_unit){
press_value_unit_ = new_press_value_unit;
// hasUpdate = true;
}
void BLETask::updateScale(bool new_press_state){
press_state_ = new_press_state;
// hasUpdate = true;
}
void BLETask::updateLux(float new_lux_value){
lux_value_ = new_lux_value;
}
void BLETask::updateButton(bool new_button_state){
button_state_ = new_button_state;
// hasUpdate = true;
}
void BLETask::addListener(QueueHandle_t queue) {
@ -240,10 +223,6 @@ QueueHandle_t BLETask::getKnobStateQueue() {
return knob_state_queue_;
}
// void BLETask::setBrightness(uint16_t brightness) {
// SemaphoreGuard lock(mutex_);
// brightness_ = brightness;
// }
void BLETask::setLogger(Logger* logger) {
logger_ = logger;

View File

@ -40,6 +40,7 @@ class BLETask : public Task<BLETask> {
void updateScale(int32_t press_value_unit);
void updateScale(bool press_value_state);
void updateButton(bool button_state);
void updateLux(float new_lux_value);
QueueHandle_t getKnobStateQueue();
void addListener(QueueHandle_t queue);
@ -71,6 +72,8 @@ class BLETask : public Task<BLETask> {
bool button_state_old;
uint32_t current_position;
uint32_t num_positions;
float lux_value_;
float lux_value_old;
QueueHandle_t knob_state_queue_;

View File

@ -67,7 +67,7 @@ build_flags =
-DPIN_WH=12
-DPIN_WL=32 #33
-DPIN_SPKR=26
-DPIN_BUTTON_NEXT=-1
-DPIN_BUTTON_NEXT=4
-DPIN_BUTTON_PREV=-1
-DPIN_SDA=15
-DPIN_SCL=8
@ -95,7 +95,7 @@ build_flags =
-DTFT_SCLK=20
-DTFT_CS=21
-DTFT_DC=22
-DTFT_RST=4
-DTFT_RST=-1 #4
-DTFT_BL=-1
-DLOAD_GLCD=1
-DLOAD_GFXFF=1