35754347.72fcd90e.js 4.0 KB

1
  1. "use strict";(self.webpackChunkwebserial_docs=self.webpackChunkwebserial_docs||[]).push([[757],{3905:(e,r,t)=>{t.d(r,{Zo:()=>p,kt:()=>b});var n=t(7294);function a(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function i(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function l(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?i(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):i(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function o(e,r){if(null==e)return{};var t,n,a=function(e,r){if(null==e)return{};var t,n,a={},i=Object.keys(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||(a[t]=e[t]);return a}(e,r);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}var c=n.createContext({}),s=function(e){var r=n.useContext(c),t=r;return e&&(t="function"==typeof e?e(r):l(l({},r),e)),t},p=function(e){var r=s(e.components);return n.createElement(c.Provider,{value:r},e.children)},u={inlineCode:"code",wrapper:function(e){var r=e.children;return n.createElement(n.Fragment,{},r)}},m=n.forwardRef((function(e,r){var t=e.components,a=e.mdxType,i=e.originalType,c=e.parentName,p=o(e,["components","mdxType","originalType","parentName"]),m=s(t),b=a,d=m["".concat(c,".").concat(b)]||m[b]||u[b]||i;return t?n.createElement(d,l(l({ref:r},p),{},{components:t})):n.createElement(d,l({ref:r},p))}));function b(e,r){var t=arguments,a=r&&r.mdxType;if("string"==typeof e||a){var i=t.length,l=new Array(i);l[0]=m;var o={};for(var c in r)hasOwnProperty.call(r,c)&&(o[c]=r[c]);o.originalType=e,o.mdxType="string"==typeof e?e:a,l[1]=o;for(var s=2;s<i;s++)l[s]=t[s];return n.createElement.apply(null,l)}return n.createElement.apply(null,t)}m.displayName="MDXCreateElement"},8119:(e,r,t)=>{t.r(r),t.d(r,{assets:()=>c,contentTitle:()=>l,default:()=>u,frontMatter:()=>i,metadata:()=>o,toc:()=>s});var n=t(7462),a=(t(7294),t(3905));const i={title:"Callbacks",sidebar_label:"Callbacks",sidebar_position:5},l=void 0,o={unversionedId:"callbacks",id:"callbacks",title:"Callbacks",description:"WebSerial comes with a single callback called onMessage. It's responsible for notifying your firmware whenever it has received a command/message from user via webserial terminal.",source:"@site/docs/callbacks.md",sourceDirName:".",slug:"/callbacks",permalink:"/callbacks",draft:!1,tags:[],version:"current",sidebarPosition:5,frontMatter:{title:"Callbacks",sidebar_label:"Callbacks",sidebar_position:5},sidebar:"tutorialSidebar",previous:{title:"Authentication",permalink:"/authentication"},next:{title:"Printing Logs",permalink:"/printing"}},c={},s=[{value:"Example Usage",id:"example-usage",level:3}],p={toc:s};function u(e){let{components:r,...t}=e;return(0,a.kt)("wrapper",(0,n.Z)({},p,t,{components:r,mdxType:"MDXLayout"}),(0,a.kt)("p",null,"WebSerial comes with a single callback called ",(0,a.kt)("inlineCode",{parentName:"p"},"onMessage"),". It's responsible for notifying your firmware whenever it has received a command/message from user via webserial terminal."),(0,a.kt)("p",null,"If the callback is not registered, then the command received from terminal is simply discarded."),(0,a.kt)("h3",{id:"example-usage"},"Example Usage"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-cpp"},'...\n#include <WebSerial.h>\n\nvoid setup() {\n ...\n\n /* Attach Message Callback */\n WebSerial.onMessage([&](uint8_t *data, size_t len) {\n Serial.printf("Received %lu bytes from WebSerial: ", len);\n Serial.write(data, len);\n Serial.println();\n });\n}\n\nvoid loop() {\n ...\n}\n')))}u.isMDXComponent=!0}}]);