void timerCallback(void) { DEBUG("start timer callback"); sprintf(answer,"+.!"); DEBUG("writing beatpulse \"%s\" with len %d to ble\n",answer,strlen(answer)); int l = strlen(answer); for(int i = 0; i*20 < strlen(answer); i++) { int len = 20 < l ? 20 : l; ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)&answer[i*20], len); l -= 20; } ticker.detach(); }
/** * Write callback * Called when a Gatt characteristic is written to by a central device */ void writeCharCallback(const GattWriteCallbackParams *params) { /* Prepare to add header to transmit message to the MicroView */ char *total; total = (char*)malloc(sizeof(char)*(params->len+1)); strcat(total+1, (char*)params->data); total[params->len+1]= '\0'; // Check to see what characteristic was written, by handle /* If it's a standard message (comming from a notification) */ if(params->handle == writeChar.getValueHandle()) { /* If only one byte is sent, we change the LED's state with its value*/ if(params->len == 1) led1 = params->data[0]; else { //pc.printf("\n\r Data received: length = %d, data = 0x",params->len); /* If the data received is equal to "ON" or "OFF", we modify the LED's state accordingly */ if(strncmp((char*)params->data, "ON",2) == 0) { //pc.printf("LED ON"); led1=1; } else if(strncmp((char*)params->data, "OFF",3) == 0) { //pc.printf("LED OFF"); led1=0; } else { /* For each message, we toggle the LED's state */ led1=!led1; } } /* Add the header specific to standard messages (not time sync) */ total[0] = headerSms; /* Update the readChar characteristic with the value of writeChar */ //ble.updateCharacteristicValue(readChar.getValueHandle(), params->data, params->len); ble.updateCharacteristicValue(readChar.getValueHandle(), (const uint8_t *)total, strlen(total)+1); /* Send the whole string, including the header, to the Microview */ i2c_port.write(addr, total, strlen(total)+1); } /* If the message has been sent to the timeSync Gatt characteristic */ else if(params->handle == timeSyncChar.getValueHandle()) { /* Set the corresponding header byte */ total[0] = headerTime; /* Send the whole string, including the header, to the MicroView */ i2c_port.write(addr, total, strlen(total)+1); led1 = !led1; } free(total); }
void onDataWritten(const GattWriteCallbackParams *params) { if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { uint16_t bytesRead = params->len; bool handled = false; for(int i = 0; !handled && i < bytesRead; i++) { switch(params->data[i]) { case '?': { DEBUG("Handle ?\n"); switch(getStateBM019()) { case BM019_STATE_UNKNOWN: DEBUG("BM019 Status: unknown\n"); sprintf(answer,"+?:0!"); break; case BM019_STATE_ANSWERING: DEBUG("BM019 Status: answering\n"); sprintf(answer,"+?:1!"); break; case BM019_STATE_PROTOCOL: DEBUG("BM019 Status: protocol set\n"); sprintf(answer,"+?:2!"); break; default: sprintf(answer,"-?%d!",getStateBM019()); DEBUG("BM019 Status: forgotten state\n"); } handled = true; } break; case 'b': case 'B': DEBUG("Handling b\n"); if(i + 5 <= bytesRead) { sprintf(answer,"+b!"); int adr = 0; char b[3]; b[0] = params->data[i+4]; b[1] = params->data[i+5]; b[2] = 0; sscanf(b,"%x",&adr); DEBUG("beat in %d sec\n",adr); ticker.attach(timerCallback, adr); i+=5; handled=true; } else { sprintf(answer,"-b!"); handled=true; } break; case 'h': case 'H': { DEBUG("Handling h\n"); if(hybernateBM019()) { sprintf(answer,"+h!"); } else { DEBUG("BM019 did hybernate wake\n") sprintf(answer,"-h!"); } handled=true; } break; case 'w': case 'W': { DEBUG("handle w\n") if(wakeBM019(100)) { DEBUG("BM019 did wake\n") sprintf(answer,"+w!"); } else { DEBUG("BM019 did NOT wake\n") sprintf(answer,"-w!"); } handled = true; } break; case 'i': case 'I': { DEBUG("handle i\n"); BM019_IDN idn; if(idnBM019(&idn)) { sprintf(answer,"+i:"); int i; for(i = 0; i < 13; i++) { sprintf(&answer[strlen(answer)],"%x",idn.deviceID[i]); } sprintf(&answer[strlen(answer)],":"); sprintf(&answer[strlen(answer)],"%x%x!",idn.romCRC[0],idn.romCRC[1]); DEBUG("answered: %s",answer); } else { DEBUG("BM019 failed idn\n") sprintf(answer,"-i!"); } handled = true; } break; case 'p': case 'P': { DEBUG("handle p\n"); if(setProtocolISO_EIC_15693BM019((BM019_PROTOCOL_ISO_IEC_15693_BYTE_0)( BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_0_CRC | BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_1_SINGLE_SUBCARRIER | BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_2_10_MODULATION | BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_3_WAIT_FOR_SOF | BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_45_26_KBPS) )) { DEBUG("BM019 proto\n") sprintf(answer,"+p!"); } else { DEBUG("BM019 failed proto\n") sprintf(answer,"-p!"); } handled = true; } break; case 'r': case 'R': { DEBUG("handle r\n"); resetBM019(); sprintf(answer,"+r!"); handled = true; } break; case 'e': case 'E': { DEBUG("handle e\n"); if(echoBM019()) { DEBUG("BM019 sent echo\n"); sprintf(answer,"+e!"); } else { DEBUG("BM019 NOT sent echo\n"); sprintf(answer,"-e!"); } handled = true; } break; case 't': case 'T': { DEBUG("handle t\n"); BM019_TAG tag; if(inventoryISO_IES_15693BM019(&tag)) { DEBUG("BM019 answered inventory\n"); sprintf(answer,"+t:"); for(int i = 0; i < 8; i++) { sprintf(&answer[strlen(answer)],"%02x",tag.uid[i]); } sprintf(&answer[strlen(answer)],"!"); } else { DEBUG("BM019 NOT answered inventory\n"); sprintf(answer,"-t!"); } handled = true; } break; case 'd': case 'D': { DEBUG("handle d\n"); if(i + 5 <= bytesRead) { int adr = 0; char b[3]; b[0] = params->data[i+4]; b[1] = params->data[i+5]; b[2] = 0; sscanf(b,"%x",&adr); DEBUG("read from %#04x\n",adr); i+=5; uint8_t rb[256]; int l = readBM019(adr,rb,256); if(l>0) { DEBUG("BM019 answered read\n"); sprintf(answer,"+d:"); for(int i = 0; i < l; i++) { sprintf(&answer[strlen(answer)],"%02x",rb[i]); } sprintf(&answer[strlen(answer)],"!"); } else { DEBUG("BM019 NOT answered read\n"); sprintf(answer,"-d!"); } } else { DEBUG("BM019 NOT answered read, no adr given\n"); sprintf(answer,"-d!"); } handled = true; } break; case 'm': case 'M': { DEBUG("handle multi d\n"); if(i + 10 <= bytesRead) { int adr = 0; char b[3]; b[0] = params->data[i+4]; b[1] = params->data[i+5]; b[2] = 0; sscanf(b,"%x",&adr); int count = 0; b[0] = params->data[i+9]; b[1] = params->data[i+10]; b[2] = 0; sscanf(b,"%x",&count); DEBUG("read from %#04x for %d\n",adr,count); i+=10; uint8_t rb[256]; int l = readMultiBM019(adr,count,rb,256); if(l>0) { DEBUG("BM019 answered multi\n"); sprintf(answer,"+m:"); for(int i = 0; i < l; i++) { sprintf(&answer[strlen(answer)],"%02x",rb[i]); } sprintf(&answer[strlen(answer)],"!"); } else { DEBUG("BM019 NOT answered multi\n"); sprintf(answer,"-m!"); } } else { DEBUG("BM019 NOT answered read, no adr&count given\n"); sprintf(answer,"-m!"); } handled = true; } break; } } if(handled) { DEBUG("writing \"%s\" with len %d to ble\n",answer,strlen(answer)); int l = strlen(answer); for(int i = 0; i*20 < strlen(answer); i++) { int len = 20 < l ? 20 : l; ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)&answer[i*20], len); l -= 20; } } else { DEBUG("received %u bytes.. nothing handled.. echo\n", bytesRead); ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); } }
void echo() { ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), data, len); }
void tick(void) { bool send_threshold = threshold_update; int last_compare = current_compare; current_input = sensor.read(); if (threshold_update) { threshold_update = 0; input_threshold = (single_click_input + double_click_input) / 2.0; invert_output = (double_click_input < single_click_input); force_update = true; DEBUG("threshold: %f\n\r", input_threshold); } if (0 == current_compare && current_input > input_threshold) { float extra_input = sensor.read(); if (extra_input > input_threshold) { current_compare = 1; } } else if (1 == current_compare && current_input < input_threshold) { float extra_input = sensor.read(); if (extra_input < input_threshold) { current_compare = 0; } } current_output = current_compare; if ((last_compare != current_compare) || force_update) { if (invert_output) { current_output = 1 - current_compare; } actuator = current_output; } DEBUG("in: %f, out: %d, invert: %d\n\r", current_input, current_output, invert_output); int txPayloadLength; if (send_threshold) { txPayloadLength = snprintf((char*)txPayload, 20, "%c %d", invert_output ? '<' : '>', (int)(input_threshold * 10000)); } else { txPayloadLength = snprintf((char*)txPayload, 20, "* %d %d", (int)(current_input * 10000), current_output); } ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), txPayload, txPayloadLength); if (blue_led_time_to_off) { blue_led_time_to_off--; if (blue_led_blink) { blue = (blue_led_time_to_off & 1) ? LED_ON : LED_OFF; } if (0 == blue_led_time_to_off) { blue_led_blink = false; blue = LED_OFF; } } if (green_led_time_to_off) { green_led_time_to_off--; if (green_led_blink) { green = (green_led_time_to_off & 1) ? LED_ON : LED_OFF; } if (0 == green_led_time_to_off) { green_led_blink = false; green = LED_OFF; } } }