esp32.dev.js 178 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867
  1. "use strict";
  2. /* additional math */
  3. Blockly.Blocks['math_degrad'] = {
  4. init: function init() {
  5. this.appendValueInput("convert").setCheck(null).appendField(new Blockly.FieldDropdown([[Blockly.Msg.math_degrad_rad, "radians"], [Blockly.Msg.math_degrad_deg, "degrees"]]), "rad_deg");
  6. this.setOutput(true, null);
  7. this.setColour("#5472ea");
  8. // this.setTooltip("");
  9. this.setHelpUrl("");
  10. var thisBlock = this;
  11. this.setTooltip(function () {
  12. var mode = thisBlock.getFieldValue('rad_deg');
  13. var TOOLTIPS = {
  14. 'radians': Blockly.Msg.Math_Degrad_RAD_TOOLTIP,
  15. 'degrees': Blockly.Msg.Math_Degrad_DEG_TOOLTIP
  16. };
  17. return TOOLTIPS[mode];
  18. });
  19. }
  20. };
  21. Blockly.Python['math_degrad'] = function (block) {
  22. var dropdown_rad_deg = block.getFieldValue('rad_deg');
  23. var value_convert = Blockly.Python.valueToCode(block, 'convert', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  24. Blockly.Python.definitions_['import_math'] = 'import math\n';
  25. var code = 'math.' + dropdown_rad_deg + '(' + value_convert + ')'; // TODO: Change ORDER_NONE to the correct strength.
  26. return [code, Blockly.Python.ORDER_NONE];
  27. };
  28. /* Serial Comm. */
  29. Blockly.Blocks['serial_comm_print'] = {
  30. init: function init() {
  31. this.appendValueInput("serial_comm_input").setCheck("String").appendField(Blockly.Msg.serialcomm_print);
  32. this.setInputsInline(false);
  33. this.setPreviousStatement(true, null);
  34. this.setNextStatement(true, null);
  35. this.setColour("#22b845");
  36. this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
  37. this.setHelpUrl("");
  38. }
  39. };
  40. Blockly.Python['serial_comm_print'] = function (block) {
  41. var value_name = Blockly.Python.valueToCode(block, 'serial_comm_input', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  42. var code = 'print(' + value_name + ')\n';
  43. return code;
  44. };
  45. /*
  46. INPUT & OUTPUT
  47. */
  48. var ESP32_IO_COLOR = "#ff8027";
  49. Blockly.Blocks['esp32_main_controller_text_print'] = {
  50. init: function init() {
  51. this.jsonInit({
  52. "message0": Blockly.Msg.TEXT_PRINT_TITLE,
  53. "args0": [{
  54. "type": "input_value",
  55. "name": "TEXT"
  56. }],
  57. "previousStatement": null,
  58. "nextStatement": null,
  59. "colour": "#33cc99",
  60. "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
  61. "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
  62. });
  63. }
  64. };
  65. Blockly.Python['esp32_main_controller_text_print'] = function (block) {
  66. // Print statement.
  67. var msg = Blockly.Python.valueToCode(block, 'TEXT', Blockly.Python.ORDER_NONE) || '___';
  68. return 'print(' + msg + ')\n';
  69. };
  70. Blockly.Blocks['esp32_main_controller_text'] = {
  71. init: function init() {
  72. this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
  73. this.setColour("#33cc99");
  74. this.appendDummyInput().appendField(this.newQuote_(true)).appendField(new Blockly.FieldTextInput(''), 'TEXT').appendField(this.newQuote_(false));
  75. this.setOutput(true, 'String'); // Assign 'this' to a variable for use in the tooltip closure below.
  76. var thisBlock = this; // Text block is trivial. Use tooltip of parent block if it exists.
  77. this.setTooltip(function () {
  78. var parent = thisBlock.getParent();
  79. return parent && parent.getInputsInline() && parent.tooltip || Blockly.Msg.TEXT_TEXT_TOOLTIP;
  80. });
  81. },
  82. newQuote_: function newQuote_(open) {
  83. if (open == this.RTL) {
  84. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
  85. } else {
  86. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
  87. }
  88. return new Blockly.FieldImage(file, 12, 12, '"');
  89. }
  90. };
  91. Blockly.Python['esp32_main_controller_text'] = function (block) {
  92. // Text value.
  93. var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
  94. return [code, Blockly.Python.ORDER_ATOMIC];
  95. };
  96. Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
  97. init: function init() {
  98. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "1"], [Blockly.Msg.time_ai_module, "2"]]), "digital_get_type").appendField(Blockly.Msg.digital_get);
  99. this.appendValueInput("digital_pin_input").setCheck(null);
  100. this.appendDummyInput().appendField(Blockly.Msg.digital_get_value);
  101. this.setInputsInline(true);
  102. this.setOutput(true, null);
  103. this.setColour(ESP32_IO_COLOR);
  104. var thisBlock = this;
  105. this.setTooltip(function () {
  106. var mode = thisBlock.getFieldValue('digital_get_type');
  107. var TOOLTIPS = {
  108. '1': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  109. '2': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  110. };
  111. return TOOLTIPS[mode];
  112. });
  113. this.setHelpUrl("");
  114. }
  115. };
  116. Blockly.Python['esp32_main_controller_io_from_digital_pin'] = function (block) {
  117. var value_name = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
  118. var timer_type = block.getFieldValue('digital_get_type'); // TODO: Assemble Python into code variable.
  119. // TODO: Assemble Python into code variable.
  120. var code = '';
  121. if (timer_type == 2) {
  122. Blockly.Python.definitions_['import_setup'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA,GPIO\n' + '';
  123. var aa = 'FPIOA().set_function(' + value_name + ',fm.fpioa.GPIOHS' + value_name + ')\n' + '_read_ai_ext_pin' + value_name + ' = GPIO(GPIO.GPIOHS' + value_name + ', GPIO.IN,GPIO.PULL_UP)\n';
  124. Blockly.Python.addVariable('_read_ai_ext_pin' + value_name, aa, true);
  125. code = '_read_ai_ext_pin' + value_name + '.value()';
  126. } else if (timer_type == 1) {
  127. Blockly.Python.definitions_['import_setup'] = '' + 'from machine import Pin\n' + '';
  128. var _aa = '_PIN_IN_' + value_name + ' = Pin(' + value_name + ', Pin.IN)\n';
  129. Blockly.Python.addVariable('_PIN_IN_' + value_name, _aa, true);
  130. code = '_PIN_IN_' + value_name + '.value()';
  131. } // TODO: Change ORDER_NONE to the correct strength.
  132. return [code, Blockly.Python.ORDER_CONDITIONAL];
  133. };
  134. Blockly.Blocks['esp32_main_controller_io_high_low'] = {
  135. init: function init() {
  136. this.appendDummyInput().appendField(new Blockly.FieldDropdown([[Blockly.Msg.io_high, "1"], [Blockly.Msg.io_low, "0"]]), "high_low");
  137. this.setInputsInline(true);
  138. this.setOutput(true, null);
  139. this.setColour(ESP32_IO_COLOR);
  140. this.setHelpUrl("");
  141. var thisBlock = this;
  142. this.setTooltip(function () {
  143. var mode = thisBlock.getFieldValue('high_low');
  144. var TOOLTIPS = {
  145. '1': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_high),
  146. '0': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_low)
  147. };
  148. return TOOLTIPS[mode];
  149. });
  150. }
  151. };
  152. Blockly.Python['esp32_main_controller_io_high_low'] = function (block) {
  153. var dropdown_name = block.getFieldValue('high_low'); // TODO: Assemble Python into code variable.
  154. var code = dropdown_name; // TODO: Change ORDER_NONE to the correct strength.
  155. return [code, Blockly.Python.ORDER_NONE];
  156. };
  157. Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
  158. init: function init() {
  159. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "1"], [Blockly.Msg.time_ai_module, "2"]]), "digital_set_type").appendField(Blockly.Msg.digital_set);
  160. this.appendValueInput("digital_pin_input").setCheck(null);
  161. this.appendDummyInput().appendField(Blockly.Msg.digital_set_as);
  162. this.appendValueInput("high_low").setCheck(null);
  163. this.setInputsInline(true);
  164. this.setPreviousStatement(true, null);
  165. this.setNextStatement(true, null);
  166. this.setColour(ESP32_IO_COLOR);
  167. this.setHelpUrl("");
  168. var thisBlock = this;
  169. this.setTooltip(function () {
  170. var mode = thisBlock.getFieldValue('digital_set_type');
  171. var TOOLTIPS = {
  172. '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  173. '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  174. };
  175. return TOOLTIPS[mode];
  176. });
  177. }
  178. };
  179. Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function (block) {
  180. var value_name1 = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
  181. var value_name2 = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC);
  182. var timer_type = block.getFieldValue('digital_set_type');
  183. var code = "";
  184. if (timer_type == 1) {
  185. // TODO: Assemble Python into code variable.
  186. Blockly.Python.definitions_['import_setup'] = '' + 'from machine import Pin\n' + '';
  187. var variable = '_PIN_OUT_' + value_name1 + ' = Pin(' + value_name1 + ', Pin.OUT)\n';
  188. Blockly.Python.addVariable('_PIN_OUT_' + value_name1, variable, true);
  189. code = '_PIN_OUT_' + value_name1 + '.value' + value_name2 + '\n';
  190. } else if (timer_type == 2) {
  191. Blockly.Python.definitions_['import_setup'] = '' + 'from fpioa_manager import *\n' + 'from Maix import GPIO\n' + '';
  192. var _variable = 'fm.register(' + value_name1 + ',fm.fpioa.GPIOHS' + value_name1 + ')\n' + '_set_ai_ext_pin' + value_name1 + ' = GPIO(GPIO.GPIOHS' + value_name1 + ', GPIO.OUT)\n';
  193. Blockly.Python.addVariable('_set_ai_ext_pin' + value_name1, _variable, true);
  194. code = '_set_ai_ext_pin' + value_name1 + '.value(' + value_name2 + ')\n';
  195. }
  196. return code;
  197. };
  198. Blockly.Blocks['esp32_main_controller_io_from_analog_pin'] = {
  199. init: function init() {
  200. this.appendDummyInput().appendField(Blockly.Msg.analog_get);
  201. this.appendValueInput("analog_pin_input").setCheck(null);
  202. this.appendDummyInput().appendField(Blockly.Msg.analog_get_value);
  203. this.setInputsInline(true);
  204. this.setOutput(true, null);
  205. this.setColour(ESP32_IO_COLOR);
  206. this.setTooltip("");
  207. this.setHelpUrl("");
  208. }
  209. };
  210. Blockly.Python['esp32_main_controller_io_from_analog_pin'] = function (block) {
  211. var value_name = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  212. Blockly.Python.addVariable('_PWM_PIN_IN_' + value_name, '', true);
  213. Blockly.Python.definitions_['import_setup'] = '' + 'from machine import Pin, ADC\n\n' + '_PWM_PIN_IN_' + value_name + ' = ADC(Pin(' + value_name + '))\n' + '_PWM_PIN_IN_' + value_name + '.atten(ADC.ATTN_11DB)\n' + '';
  214. var code = '_PWM_PIN_IN_' + value_name + '.read()'; // TODO: Change ORDER_NONE to the correct strength.
  215. return [code, Blockly.Python.ORDER_CONDITIONAL];
  216. };
  217. Blockly.Blocks['esp32_main_controller_io_set_analog_pin'] = {
  218. init: function init() {
  219. this.appendDummyInput().appendField(Blockly.Msg.analog_set);
  220. this.appendValueInput("analog_pin_input").setCheck(null);
  221. this.appendDummyInput().appendField(Blockly.Msg.analog_set_freq);
  222. this.appendValueInput("frequency").setCheck(null);
  223. this.appendDummyInput().appendField(Blockly.Msg.analog_set_pwm);
  224. this.appendValueInput("pwm").setCheck(null);
  225. this.setInputsInline(true);
  226. this.setPreviousStatement(true, null);
  227. this.setNextStatement(true, null);
  228. this.setColour(ESP32_IO_COLOR);
  229. this.setHelpUrl("");
  230. var thisBlock = this;
  231. this.setTooltip(function () {
  232. var mode = thisBlock.getFieldValue('LED');
  233. var TOOLTIPS = {
  234. 'LED': Blockly.Msg.Esp32_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
  235. };
  236. return TOOLTIPS[mode];
  237. });
  238. }
  239. };
  240. Blockly.Python['esp32_main_controller_io_set_analog_pin'] = function (block) {
  241. var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
  242. var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
  243. var value_name3 = Blockly.Python.valueToCode(block, 'frequency', Blockly.Python.ORDER_ATOMIC);
  244. Blockly.Python.addVariable('_PWM_PIN_OUT_' + value_name1, '', true); // TODO: Assemble Python into code variable.
  245. Blockly.Python.definitions_['import_setup'] = '' + 'from machine import Pin, PWM\n' + '_PWM_PIN_OUT_' + value_name1 + ' = PWM(Pin(' + value_name1 + '), freq=' + value_name3 + ')\n' + '';
  246. var code = '_PWM_PIN_OUT_' + value_name1 + '.freq(' + value_name3 + ')\n' + '_PWM_PIN_OUT_' + value_name1 + '.duty(' + value_name2 + ')\n';
  247. return code;
  248. };
  249. /*
  250. Button
  251. */
  252. Blockly.Blocks['esp32_main_controller_button_read_pressed'] = {
  253. init: function init() {
  254. // this.appendDummyInput()
  255. // .appendField(new Blockly.FieldImage("blockly/media/button_pressed.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  256. this.appendDummyInput().appendField(Blockly.Msg.ESP32BUTTON).appendField(new Blockly.FieldDropdown([["P1", "button_type_1"], ["P2", "button_type_2"]]), "button_type").appendField(Blockly.Msg.ESP32PRESSED);
  257. this.setOutput(true, null);
  258. this.setColour("#e8795b");
  259. var thisBlock = this;
  260. this.setTooltip(function () {
  261. var mode = thisBlock.getFieldValue('button_type');
  262. var TOOLTIPS = {
  263. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P1"),
  264. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P2")
  265. };
  266. return TOOLTIPS[mode];
  267. });
  268. this.setHelpUrl("");
  269. }
  270. };
  271. Blockly.Python['esp32_main_controller_button_read_pressed'] = function (block) {
  272. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  273. var _type = block.getField('button_type').text_;
  274. var _button_state = block.getFieldValue('button_state');
  275. var _num = _type == "P1" ? "34" : "35";
  276. var _upstr = "";
  277. if (_type == "P2") {
  278. _upstr = ",Pin.PULL_UP";
  279. }
  280. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  281. var code = '(_' + _type + '.value() == ' + 1 + ')';
  282. return [code, Blockly.Python.ORDER_CONDITIONAL];
  283. };
  284. Blockly.Blocks['esp32_main_controller_button_read_released'] = {
  285. init: function init() {
  286. // this.appendDummyInput()
  287. // .appendField(new Blockly.FieldImage("blockly/media/button_released.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  288. this.appendDummyInput().appendField(Blockly.Msg.ESP32BUTTON).appendField(new Blockly.FieldDropdown([["P1", "button_type_1"], ["P2", "button_type_2"]]), "button_type").appendField(Blockly.Msg.ESP32RELEASED);
  289. this.setOutput(true, null);
  290. this.setColour("#e8795b");
  291. var thisBlock = this;
  292. this.setTooltip(function () {
  293. var mode = thisBlock.getFieldValue('button_type');
  294. var TOOLTIPS = {
  295. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P1"),
  296. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P2")
  297. };
  298. return TOOLTIPS[mode];
  299. });
  300. this.setHelpUrl("");
  301. }
  302. };
  303. Blockly.Python['esp32_main_controller_button_read_released'] = function (block) {
  304. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  305. var _type = block.getField('button_type').text_;
  306. var _button_state = block.getFieldValue('button_state');
  307. var _num = _type == "P1" ? "34" : "35";
  308. var _upstr = "";
  309. if (_type == "P2") {
  310. _upstr = ",Pin.PULL_UP";
  311. }
  312. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  313. var code = '(_' + _type + '.value() == ' + 0 + ')';
  314. return [code, Blockly.Python.ORDER_CONDITIONAL];
  315. };
  316. Blockly.Blocks['esp32_main_controller_button_read_value'] = {
  317. init: function init() {
  318. this.appendDummyInput().appendField(Blockly.Msg.basic_button_read_value).appendField(new Blockly.FieldDropdown([["P1", "button_type_1"], ["P2", "button_type_2"]]), "button_type").appendField(Blockly.Msg.basic_button_read_value_after);
  319. this.setInputsInline(true);
  320. this.setOutput(true, null);
  321. this.setColour("#e8795b");
  322. var thisBlock = this;
  323. this.setTooltip(function () {
  324. var mode = thisBlock.getFieldValue('button_type');
  325. var TOOLTIPS = {
  326. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P1"),
  327. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P2")
  328. };
  329. return TOOLTIPS[mode];
  330. });
  331. this.setHelpUrl("");
  332. }
  333. };
  334. Blockly.Python['esp32_main_controller_button_read_value'] = function (block) {
  335. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  336. var _type = block.getField('button_type').text_; // TODO: Assemble Python into code variable.
  337. var _num = _type == "P1" ? "34" : "35";
  338. var _upstr = "";
  339. if (_type == "P2") {
  340. _upstr = ",Pin.PULL_UP";
  341. }
  342. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  343. var code = '_' + _type + '.value()'; // TODO: Change ORDER_NONE to the correct strength.
  344. return [code, Blockly.Python.ORDER_NONE];
  345. };
  346. /*
  347. Blockly.Python['esp32_main_controller_button_read'] = function (block) {
  348. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  349. var _type = block.getField('button_type').text_;
  350. var _button_state = block.getFieldValue('button_state');
  351. var _num = _type == "1" ? "36" : "22";
  352. var _upstr = "";
  353. if (_type == "2") { _upstr = ",Pin.PULL_UP"; }
  354. Blockly.Python.addVariable('p' + _type, 'p' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  355. var code = '(p' + _type + '.value() == ' + (_button_state == "button_state_pressed" ? "1" : "0") + ')';
  356. return [code, Blockly.Python.ORDER_CONDITIONAL];
  357. };
  358. */
  359. /*
  360. Dialer
  361. */
  362. Blockly.Blocks['esp32_main_controller_dial_switch_read_pressed'] = {
  363. init: function init() {
  364. this.appendDummyInput().appendField(Blockly.Msg.ESP32DIALSWITCH).appendField(new Blockly.FieldDropdown([[Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"], [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"], [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]]), "switch_type").appendField(Blockly.Msg.ESP32PRESSED);
  365. this.setOutput(true, null);
  366. this.setColour("#e8795b");
  367. this.setTooltip("");
  368. this.setHelpUrl("");
  369. }
  370. };
  371. Blockly.Python['esp32_main_controller_dial_switch_read_pressed'] = function (block) {
  372. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  373. var _type = block.getFieldValue('switch_type');
  374. var _num = "34";
  375. var _upstr = "";
  376. var _button_type = "";
  377. if (_type == "switch_type_left") {
  378. var _num = "34";
  379. _button_type = "Left";
  380. } else if (_type == "switch_type_middle") {
  381. var _num = "19";
  382. _upstr = ", Pin.PULL_UP";
  383. _button_type = "Middle";
  384. } else if (_type == "switch_type_right") {
  385. var _num = "35";
  386. _button_type = "Right";
  387. } // Blockly.Python.addVariable("dialer_type", 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
  388. Blockly.Python.addVariable("pLeft", 'pLeft = Pin(38, Pin.IN)');
  389. Blockly.Python.addVariable("pMiddle", 'pMiddle = Pin(19, Pin.IN)');
  390. Blockly.Python.addVariable("pRight", 'pRight = Pin(35, Pin.IN)');
  391. var code = '(p' + _button_type + '.value() == 1)';
  392. return [code, Blockly.Python.ORDER_CONDITIONAL];
  393. };
  394. Blockly.Blocks['esp32_main_controller_dial_switch_read_released'] = {
  395. init: function init() {
  396. this.appendDummyInput().appendField(Blockly.Msg.ESP32DIALSWITCH).appendField(new Blockly.FieldDropdown([[Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"], [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"], [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]]), "switch_type").appendField(Blockly.Msg.ESP32RELEASED);
  397. this.setOutput(true, null);
  398. this.setColour("#e8795b");
  399. this.setTooltip("");
  400. this.setHelpUrl("");
  401. }
  402. };
  403. Blockly.Python['esp32_main_controller_dial_switch_read_released'] = function (block) {
  404. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  405. var _type = block.getFieldValue('switch_type');
  406. var _state = block.getFieldValue('switch_state');
  407. var _num = "34";
  408. var _upstr = "";
  409. var _button_type = "";
  410. if (_type == "switch_type_left") {
  411. var _num = "34";
  412. _button_type = "Left";
  413. } else if (_type == "switch_type_middle") {
  414. var _num = "19";
  415. _upstr = ", Pin.PULL_UP";
  416. _button_type = "Middle";
  417. } else if (_type == "switch_type_right") {
  418. var _num = "35";
  419. _button_type = "Right";
  420. }
  421. Blockly.Python.addVariable('p' + _button_type, 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
  422. var code = '(p' + _button_type + '.value() == 0)';
  423. return [code, Blockly.Python.ORDER_CONDITIONAL];
  424. };
  425. /*
  426. LED
  427. */
  428. Blockly.Blocks['esp32_main_controller_set_led_turnon'] = {
  429. init: function init() {
  430. // this.appendDummyInput()
  431. // .appendField(new Blockly.FieldImage("blockly/media/led_on.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  432. this.appendDummyInput().appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
  433. this.appendValueInput("led_type").setCheck(null);
  434. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNON);
  435. this.setPreviousStatement(true, null);
  436. this.setNextStatement(true, null);
  437. this.setColour("#e8795b");
  438. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnon_TOOLTIP);
  439. this.setHelpUrl("");
  440. }
  441. };
  442. Blockly.Python['esp32_main_controller_set_led_turnon'] = function (block) {
  443. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  444. var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
  445. var _state = block.getFieldValue('led_state');
  446. var _num = "2";
  447. if (_type == "1") {
  448. var _num = "2";
  449. } else if (_type == "2") {
  450. var _num = "12";
  451. } else if (_type == "3") {
  452. var _num = "5";
  453. } else if (_type == "4") {
  454. var _num = "13";
  455. } else if (_type == "5") {
  456. var _num = "14";
  457. } else if (_type == "6") {
  458. var _num = "15";
  459. } else if (_type == "7") {
  460. var _num = "18";
  461. } else if (_type == "8") {
  462. var _num = "19";
  463. } else if (_type == "9") {
  464. var _num = "23";
  465. } else {
  466. var _num = "ERROR";
  467. }
  468. Blockly.Python.addSetup('iot_led_light_setup', '' + '_light = [0,2,12,5,13,14,15,18,19,23]\n' + '\n' + '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' + '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' + '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' + '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' + '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' + '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' + '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' + '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' + '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' + '\n' + 'def _led_light(state, pin):\n' + ' if (state == True):\n' + ' if pin == 1: _light_1.value(1)\n' + ' if pin == 2: _light_2.value(1)\n' + ' if pin == 3: _light_3.value(1)\n' + ' if pin == 4: _light_4.value(1)\n' + ' if pin == 5: _light_5.value(1)\n' + ' if pin == 6: _light_6.value(1)\n' + ' if pin == 7: _light_7.value(1)\n' + ' if pin == 8: _light_8.value(1)\n' + ' if pin == 9: _light_9.value(1)\n' + ' elif (state == False):\n' + ' if pin == 1: _light_1.value(0)\n' + ' if pin == 2: _light_2.value(0)\n' + ' if pin == 3: _light_3.value(0)\n' + ' if pin == 4: _light_4.value(0)\n' + ' if pin == 5: _light_5.value(0)\n' + ' if pin == 6: _light_6.value(0)\n' + ' if pin == 7: _light_7.value(0)\n' + ' if pin == 8: _light_8.value(0)\n' + ' if pin == 9: _light_9.value(0)\n' + '');
  469. var code = '_led_light(True,' + _type + ')\n';
  470. return code;
  471. };
  472. Blockly.Blocks['esp32_main_controller_set_led_turnoff'] = {
  473. init: function init() {
  474. // this.appendDummyInput()
  475. // .appendField(new Blockly.FieldImage("blockly/media/led_off.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  476. this.appendDummyInput().appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
  477. this.appendValueInput("led_type").setCheck(null);
  478. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNOFF);
  479. this.setPreviousStatement(true, null);
  480. this.setNextStatement(true, null);
  481. this.setColour("#e8795b");
  482. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnoff_TOOLTIP);
  483. this.setHelpUrl("");
  484. }
  485. };
  486. Blockly.Python['esp32_main_controller_set_led_turnoff'] = function (block) {
  487. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  488. var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
  489. var _state = block.getFieldValue('led_state');
  490. var _num = "2";
  491. if (_type == "1") {
  492. var _num = "2";
  493. } else if (_type == "2") {
  494. var _num = "12";
  495. } else if (_type == "3") {
  496. var _num = "5";
  497. } else if (_type == "4") {
  498. var _num = "13";
  499. } else if (_type == "5") {
  500. var _num = "14";
  501. } else if (_type == "6") {
  502. var _num = "15";
  503. } else if (_type == "7") {
  504. var _num = "18";
  505. } else if (_type == "8") {
  506. var _num = "19";
  507. } else if (_type == "9") {
  508. var _num = "23";
  509. } else {
  510. var _num = "ERROR";
  511. }
  512. Blockly.Python.addSetup('iot_led_light_setup', '' + '_light = [0,2,12,5,13,14,15,18,19,23]\n' + '\n' + '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' + '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' + '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' + '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' + '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' + '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' + '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' + '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' + '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' + '\n' + 'def _led_light(state, pin):\n' + ' if (state == True):\n' + ' if pin == 1: _light_1.value(1)\n' + ' if pin == 2: _light_2.value(1)\n' + ' if pin == 3: _light_3.value(1)\n' + ' if pin == 4: _light_4.value(1)\n' + ' if pin == 5: _light_5.value(1)\n' + ' if pin == 6: _light_6.value(1)\n' + ' if pin == 7: _light_7.value(1)\n' + ' if pin == 8: _light_8.value(1)\n' + ' if pin == 9: _light_9.value(1)\n' + ' elif (state == False):\n' + ' if pin == 1: _light_1.value(0)\n' + ' if pin == 2: _light_2.value(0)\n' + ' if pin == 3: _light_3.value(0)\n' + ' if pin == 4: _light_4.value(0)\n' + ' if pin == 5: _light_5.value(0)\n' + ' if pin == 6: _light_6.value(0)\n' + ' if pin == 7: _light_7.value(0)\n' + ' if pin == 8: _light_8.value(0)\n' + ' if pin == 9: _light_9.value(0)\n' + '');
  513. var code = '_led_light(False,' + _type + ')\n';
  514. return code;
  515. };
  516. Blockly.Blocks['esp32_main_controller_set_led_drawpixel_on'] = {
  517. init: function init() {
  518. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTUP + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
  519. this.appendValueInput("x").setCheck(null);
  520. this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
  521. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
  522. this.setInputsInline(true);
  523. this.setPreviousStatement(true, null);
  524. this.setNextStatement(true, null);
  525. this.setColour("#e8795b");
  526. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_On_TOOLTIP);
  527. this.setHelpUrl("");
  528. }
  529. };
  530. Blockly.Python['esp32_main_controller_set_led_drawpixel_on'] = function (block) {
  531. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  532. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  533. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  534. Blockly.Python.addSetup('iot_led_light_setup', '' + '_light = [0,2,12,5,13,14,15,18,19,23]\n' + '\n' + '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' + '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' + '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' + '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' + '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' + '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' + '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' + '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' + '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' + '\n' + 'def _led_light(state, pin):\n' + ' if (state == True):\n' + ' if pin == 1: _light_1.value(1)\n' + ' if pin == 2: _light_2.value(1)\n' + ' if pin == 3: _light_3.value(1)\n' + ' if pin == 4: _light_4.value(1)\n' + ' if pin == 5: _light_5.value(1)\n' + ' if pin == 6: _light_6.value(1)\n' + ' if pin == 7: _light_7.value(1)\n' + ' if pin == 8: _light_8.value(1)\n' + ' if pin == 9: _light_9.value(1)\n' + ' elif (state == False):\n' + ' if pin == 1: _light_1.value(0)\n' + ' if pin == 2: _light_2.value(0)\n' + ' if pin == 3: _light_3.value(0)\n' + ' if pin == 4: _light_4.value(0)\n' + ' if pin == 5: _light_5.value(0)\n' + ' if pin == 6: _light_6.value(0)\n' + ' if pin == 7: _light_7.value(0)\n' + ' if pin == 8: _light_8.value(0)\n' + ' if pin == 9: _light_9.value(0)\n' + '');
  535. Blockly.Python.addSetup('drawpixel_setup', '' + 'def _led_draw_pixel(state, x, y):\n' + ' real_pin = (3*y-3+x)\n' + ' _led_light(state,real_pin)\n' + '\n');
  536. var code = '_led_draw_pixel(True,' + value_x + ',' + value_y + ')\n';
  537. return code;
  538. };
  539. Blockly.Blocks['esp32_main_controller_set_led_drawpixel_off'] = {
  540. init: function init() {
  541. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTOFF + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
  542. this.appendValueInput("x").setCheck(null);
  543. this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
  544. this.appendDummyInput().appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
  545. this.setInputsInline(true);
  546. this.setPreviousStatement(true, null);
  547. this.setNextStatement(true, null);
  548. this.setColour("#e8795b");
  549. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_Off_TOOLTIP);
  550. this.setHelpUrl("");
  551. }
  552. };
  553. Blockly.Python['esp32_main_controller_set_led_drawpixel_off'] = function (block) {
  554. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  555. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  556. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  557. Blockly.Python.addSetup('iot_led_light_setup', '' + '_light = [0,2,12,5,13,14,15,18,19,23]\n' + '\n' + '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' + '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' + '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' + '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' + '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' + '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' + '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' + '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' + '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' + '\n' + 'def _led_light(state, pin):\n' + ' if (state == True):\n' + ' if pin == 1: _light_1.value(1)\n' + ' if pin == 2: _light_2.value(1)\n' + ' if pin == 3: _light_3.value(1)\n' + ' if pin == 4: _light_4.value(1)\n' + ' if pin == 5: _light_5.value(1)\n' + ' if pin == 6: _light_6.value(1)\n' + ' if pin == 7: _light_7.value(1)\n' + ' if pin == 8: _light_8.value(1)\n' + ' if pin == 9: _light_9.value(1)\n' + ' elif (state == False):\n' + ' if pin == 1: _light_1.value(0)\n' + ' if pin == 2: _light_2.value(0)\n' + ' if pin == 3: _light_3.value(0)\n' + ' if pin == 4: _light_4.value(0)\n' + ' if pin == 5: _light_5.value(0)\n' + ' if pin == 6: _light_6.value(0)\n' + ' if pin == 7: _light_7.value(0)\n' + ' if pin == 8: _light_8.value(0)\n' + ' if pin == 9: _light_9.value(0)\n' + '');
  558. Blockly.Python.addSetup('drawpixel_setup', '' + 'def _led_draw_pixel(state, x, y):\n' + ' real_pin = (3*y-3+x)\n' + ' _led_light(state,real_pin)\n' + '\n');
  559. var code = '_led_draw_pixel(False,' + value_x + ',' + value_y + ')\n';
  560. return code;
  561. };
  562. /*
  563. BUZZER
  564. */
  565. Blockly.Blocks['esp32_main_controller_set_buzzer'] = {
  566. init: function init() {
  567. // this.appendDummyInput()
  568. // .appendField(new Blockly.FieldImage("blockly/media/buzzer.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  569. this.appendDummyInput().appendField(); //.appendField(new Blockly.FieldNumber(31, 31, 65535, 1), "buzzer_frequency");
  570. this.appendValueInput("esp32_var_num").setCheck("Number").appendField(Blockly.Msg.basic_buzzer_set);
  571. this.setInputsInline(true);
  572. this.setPreviousStatement(true);
  573. this.setNextStatement(true); // this.setPreviousStatement(true, null);
  574. // this.setNextStatement(true, null);
  575. this.setColour("#e8795b");
  576. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Buzzer_TOOLTIP);
  577. this.setHelpUrl("");
  578. }
  579. };
  580. Blockly.Python['esp32_main_controller_set_buzzer'] = function (block) {
  581. Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
  582. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  583. var _type = Blockly.Python.valueToCode(block, 'esp32_var_num', Blockly.Python.ORDER_ATOMIC) || '0';
  584. Blockly.Python.definitions_['buzzer_operation'] = '' + 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' + 'beeper.deinit()\n' +
  585. /*
  586. 'def _beeper_control(state, freq=31):\n' +
  587. ' if freq <= 31: freq = 31\n' +
  588. ' elif freq >= 67108863: freq = 67108863\n' +
  589. ' if state == True:\n' +
  590. ' beeper.init()\n' +
  591. ' beeper.freq(freq)\n' +
  592. ' time.sleep_ms(20)\n' +
  593. ' elif state == False:\n' +
  594. ' time.sleep_ms(20)\n' +
  595. ' beeper.deinit()\n' +
  596. */
  597. '\n';
  598. var code = 'beeper.init()\nbeeper.freq(' + _type + ')\n';
  599. return code;
  600. };
  601. Blockly.Blocks['esp32_main_controller_turn_off_buzzer'] = {
  602. init: function init() {
  603. this.appendDummyInput().appendField(Blockly.Msg.ESP32TURNOB);
  604. this.setPreviousStatement(true, null);
  605. this.setNextStatement(true, null);
  606. this.setColour("#e8795b");
  607. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Turn_Off_Buzzer_TOOLTIP);
  608. this.setHelpUrl("");
  609. }
  610. };
  611. Blockly.Python['esp32_main_controller_turn_off_buzzer'] = function (block) {
  612. Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
  613. Blockly.Python.definitions_['buzzer_operation'] = '' + 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' + 'beeper.deinit()\n' +
  614. /*
  615. 'def _beeper_control(state, freq=31):\n' +
  616. ' if freq <= 31: freq = 31\n' +
  617. ' elif freq >= 67108863: freq = 67108863\n' +
  618. ' if state == True:\n' +
  619. ' beeper.init()\n' +
  620. ' beeper.freq(freq)\n' +
  621. ' time.sleep_ms(20)\n' +
  622. ' elif state == False:\n' +
  623. ' beeper.deinit()\n' +
  624. ' time.sleep_ms(20)\n' +
  625. */
  626. '\n';
  627. var code = 'beeper.deinit()\n';
  628. return code; //return [code, Blockly.Python.ORDER_FUNCTION_CALL];
  629. };
  630. /*
  631. TOUCH AREA
  632. */
  633. Blockly.Blocks['esp32_main_controller_touch_read_touched'] = {
  634. init: function init() {
  635. // this.appendDummyInput()
  636. // .appendField(new Blockly.FieldImage("blockly/media/touched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  637. this.appendDummyInput().appendField(Blockly.Msg.ESP32TA).appendField(new Blockly.FieldDropdown([["A", "touch_area_type_a"], ["B", "touch_area_type_b"]]), "touch_area_type").appendField(Blockly.Msg.ESP32_TOUCHED);
  638. this.setOutput(true, null);
  639. this.setColour("#e8795b");
  640. this.setTooltip("");
  641. this.setHelpUrl("");
  642. }
  643. };
  644. Blockly.Python['esp32_main_controller_touch_read_touched'] = function (block) {
  645. Blockly.Python.definitions_['import_TouchPad'] = 'from machine import TouchPad';
  646. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  647. var _type = block.getFieldValue('touch_area_type');
  648. var _state = block.getFieldValue('touch_area_state');
  649. var _num = "4";
  650. if (_type == "touch_area_type_a") {
  651. var _num = "4";
  652. } else if (_type == "touch_area_type_b") {
  653. var _num = "27";
  654. }
  655. Blockly.Python.addVariable('t' + block.getField('touch_area_type').text_, '_TOUCH_' + block.getField('touch_area_type').text_ + ' = TouchPad(Pin(' + _num + '))');
  656. var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
  657. return [code, Blockly.Python.ORDER_CONDITIONAL];
  658. };
  659. Blockly.Blocks['esp32_main_controller_touch_read_untouched'] = {
  660. init: function init() {
  661. // this.appendDummyInput()
  662. // .appendField(new Blockly.FieldImage("blockly/media/untouched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  663. this.appendDummyInput().appendField(Blockly.Msg.ESP32TA).appendField(new Blockly.FieldDropdown([["A", "touch_area_type_a"], ["B", "touch_area_type_b"]]), "touch_area_type").appendField(Blockly.Msg.ESP32_UNTOUCHED);
  664. this.setOutput(true, null);
  665. this.setColour("#e8795b");
  666. this.setTooltip("");
  667. this.setHelpUrl("");
  668. }
  669. };
  670. Blockly.Python['esp32_main_controller_touch_read_untouched'] = function (block) {
  671. Blockly.Python.definitions_['import_TouchPad'] = 'from machine import TouchPad';
  672. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  673. var _type = block.getFieldValue('touch_area_type');
  674. var _state = block.getFieldValue('touch_area_state');
  675. var _num = "4";
  676. if (_type == "touch_area_type_a") {
  677. var _num = "4";
  678. } else if (_type == "touch_area_type_b") {
  679. var _num = "27";
  680. }
  681. Blockly.Python.addVariable('t' + block.getField('touch_area_type').text_, '_TOUCH_' + block.getField('touch_area_type').text_ + ' = TouchPad(Pin(' + _num + '))');
  682. var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + ">= 100";
  683. return [code, Blockly.Python.ORDER_CONDITIONAL];
  684. };
  685. Blockly.Blocks['esp32_main_controller_touch_read_value'] = {
  686. init: function init() {
  687. this.appendDummyInput().appendField(Blockly.Msg.ESP32_READ).appendField(new Blockly.FieldDropdown([["A", "touch_area_type_a"], ["B", "touch_area_type_b"]]), "touch_area_type").appendField(Blockly.Msg.ESP32_READ_VALUE);
  688. this.setInputsInline(false);
  689. this.setOutput(true, null);
  690. this.setColour("#e8795b");
  691. this.setTooltip("");
  692. this.setHelpUrl("");
  693. }
  694. };
  695. Blockly.Python['esp32_main_controller_touch_read_value'] = function (block) {
  696. Blockly.Python.definitions_['import_TouchPad'] = 'from machine import TouchPad';
  697. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  698. var _type = block.getFieldValue('touch_area_type');
  699. var _state = block.getFieldValue('touch_area_state');
  700. var _num = "4";
  701. if (_type == "touch_area_type_a") {
  702. var _num = "4";
  703. } else if (_type == "touch_area_type_b") {
  704. var _num = "27";
  705. }
  706. Blockly.Python.addVariable('t' + block.getField('touch_area_type').text_, '_TOUCH_' + block.getField('touch_area_type').text_ + ' = TouchPad(Pin(' + _num + '))');
  707. var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ';
  708. return [code, Blockly.Python.ORDER_CONDITIONAL];
  709. };
  710. /*
  711. Env Sensor: BME280
  712. */
  713. Blockly.Blocks['esp32_main_controller_get_environmental_value'] = {
  714. init: function init() {
  715. // this.appendDummyInput()
  716. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  717. this.appendDummyInput().appendField(Blockly.Msg.ESP32_ENV_GET_TEXT).appendField(new Blockly.FieldDropdown([[Blockly.Msg.basic_temperature, "temperature"], [Blockly.Msg.basic_humidity, "humidity"] // [Blockly.Msg.basic_pressure, "pressure"]
  718. ]), "env_get_type").appendField(Blockly.Msg.basic_env_value);
  719. this.setInputsInline(false);
  720. this.setOutput(true, null);
  721. this.setColour("#e8795b");
  722. var thisBlock = this;
  723. this.setTooltip(function () {
  724. var mode = thisBlock.getFieldValue('env_get_type');
  725. var TOOLTIPS = {
  726. 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
  727. 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
  728. };
  729. return TOOLTIPS[mode];
  730. });
  731. this.setHelpUrl("");
  732. }
  733. };
  734. Blockly.Python['esp32_main_controller_get_environmental_value'] = function (block) {
  735. var _type_selected = block.getFieldValue('env_get_type');
  736. /*
  737. from machine import Pin, I2C
  738. import bme280 as BME280
  739. _SENSOR_BME_PINS = I2C(scl=Pin(17), sda=Pin(16), freq=10000)
  740. _BME = BME280.BME280(i2c=_SENSOR_BME_PINS)
  741. */
  742. Blockly.Python.definitions_['import_bme280'] = 'from machine import Pin, I2C\n' + 'from aht10 import AHT10\n\n' + '_aht10_i2c = I2C(sda=Pin(32), scl=Pin(33))\n' + '_aht10_sensor = AHT10(_aht10_i2c)\n' + '_aht10_sensor.sensor_init()\n' + '_aht10_sensor.is_calibration_enabled()\n' + '';
  743. Blockly.Python.addVariable('_BME', '');
  744. if (_type_selected == 'temperature') {
  745. var code = '_aht10_sensor.read_' + _type_selected + '()';
  746. } else if (_type_selected == 'humidity') {
  747. var code = '_aht10_sensor.read_' + _type_selected + '()';
  748. } else {
  749. var code = _type_selected;
  750. } // TODO: Change ORDER_NONE to the correct strength.
  751. return [code, Blockly.Python.ORDER_CONDITIONAL];
  752. };
  753. /*
  754. Motion Sensor: MPU6050
  755. */
  756. Blockly.Blocks['esp32_main_controller_motion_init'] = {
  757. init: function init() {
  758. //this.appendDummyInput()
  759. // .appendField(new Blockly.FieldImage("blockly/media/motion_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  760. this.appendDummyInput().appendField(Blockly.Msg.basic_motion_use);
  761. this.setInputsInline(false);
  762. this.setPreviousStatement(true, null);
  763. this.setNextStatement(true, null);
  764. this.setColour("#e8795b");
  765. this.setTooltip("");
  766. this.setHelpUrl("");
  767. }
  768. };
  769. Blockly.Python['esp32_main_controller_motion_init'] = function (block) {
  770. // Blockly.Python.addVariable('_SENSOR_MPU_PINS', '');
  771. Blockly.Python.definitions_['import_mpu6050'] = '' + 'from machine import I2C, Pin\n' + 'import math\n' + 'import mpu6050\n' + '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' + '';
  772. var code = '' + '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' + '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' + '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + '';
  773. return code;
  774. };
  775. Blockly.Blocks['esp32_main_controller_motion_when_shaking'] = {
  776. init: function init() {
  777. this.appendDummyInput().appendField(Blockly.Msg.basic_when_shaking);
  778. this.setInputsInline(false);
  779. this.setOutput(true, null);
  780. this.setColour("#e8795b");
  781. this.setTooltip("");
  782. this.setHelpUrl("");
  783. }
  784. };
  785. Blockly.Python['esp32_main_controller_motion_when_shaking'] = function (block) {
  786. Blockly.Python.definitions_['shaking_def'] = '' + 'def _CHECK_SHAKING():\n' + ' if _MPU_SHAKING_OFFSET < 0 and _SSTATE == 0:\n' + ' _SSTATE = 1\n' + ' return True\n' + ' elif _MPU_SHAKING_OFFSET > 0 and _SSTATE == 1:\n' + ' _SSTATE = 0\n' + ' return False\n' + '';
  787. var code = '(_CHECK_SHAKING() == True)';
  788. return [code, Blockly.Python.ORDER_CONDITIONAL];
  789. };
  790. Blockly.Blocks['esp32_main_controller_motion_when_shaking_STATEMENT'] = {
  791. init: function init() {
  792. this.appendDummyInput().appendField(Blockly.Msg.basic_when_shaking_when_module).appendField(new Blockly.FieldDropdown([[Blockly.Msg.basic_when_shaking_when_is_in, "True"], [Blockly.Msg.basic_when_shaking_when_is_not_in, "False"]]), "status").appendField(Blockly.Msg.basic_when_shaking_when_shaking);
  793. this.appendStatementInput("input").setCheck(null).appendField(Blockly.Msg.basic_motion_statement_exec);
  794. this.setPreviousStatement(true, null);
  795. this.setNextStatement(true, null);
  796. this.setColour("#e8795b");
  797. var thisBlock = this;
  798. this.setTooltip(function () {
  799. var mode = thisBlock.getFieldValue('status');
  800. var TOOLTIPS = {
  801. 'True': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_in),
  802. 'False': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_not_in)
  803. };
  804. return TOOLTIPS[mode];
  805. });
  806. this.setHelpUrl("");
  807. }
  808. };
  809. Blockly.Python['esp32_main_controller_motion_when_shaking_STATEMENT'] = function (block) {
  810. var dropdown_status = block.getFieldValue('status');
  811. var statements_input = Blockly.Python.statementToCode(block, 'input');
  812. Blockly.Python.definitions_['import_mpu6050'] = '' + 'from machine import I2C, Pin\n' + 'import math\n' + 'import mpu6050\n' + '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' + '';
  813. Blockly.Python.definitions_['mpu6050_standard_def'] = '' + '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '';
  814. Blockly.Python.definitions_['shaking_def'] = '' + 'def _CHECK_SHAKING():\n' + ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + ' _SSTATE = 0\n' + ' if _MPU_SHAKING_OFFSET < 0:\n' + ' _SSTATE = 1\n' + ' return True\n' + ' elif _MPU_SHAKING_OFFSET > 0:\n' + ' _SSTATE = 0\n' + ' return False\n' + '';
  815. var code = '' + 'if (_CHECK_SHAKING() == ' + dropdown_status + '):\n' + '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '' + statements_input + '\n' + '';
  816. return code;
  817. };
  818. Blockly.Blocks['esp32_main_controller_motion_when_tilting'] = {
  819. init: function init() {
  820. this.appendDummyInput().appendField(Blockly.Msg.basic_when_tilting_get).appendField(new Blockly.FieldDropdown([[Blockly.Msg.basic_when_tilting_get_left, "L"], [Blockly.Msg.basic_when_tilting_get_right, "R"], [Blockly.Msg.basic_when_tilting_get_forward, "F"], [Blockly.Msg.basic_when_tilting_get_backward, "B"]]), "tilt_type").appendField(Blockly.Msg.basic_when_tilting_get_when);
  821. this.setInputsInline(false);
  822. this.setOutput(true, null);
  823. this.setColour("#e8795b");
  824. this.setTooltip("");
  825. this.setHelpUrl("");
  826. }
  827. };
  828. Blockly.Python['esp32_main_controller_motion_when_tilting'] = function (block) {
  829. var _type_selected = block.getFieldValue('tilt_type');
  830. Blockly.Python.definitions_['tilting_def'] = '' + 'def _CHECK_TILT_LR():\n' + ' if _MPU_OFFSET_TILT_X < 160 and _MPU_OFFSET_TILT_X > 0:\n' + ' return "R"\n' + ' elif _MPU_OFFSET_TILT_X > 200 and _MPU_OFFSET_TILT_X < 360:\n' + ' return "L"\n' + ' else:\n' + ' return None\n' + '\n' + 'def _CHECK_TILT_FB():\n' + ' if _MPU_OFFSET_TILT_Y < 160 and _MPU_OFFSET_TILT_Y > 0:\n' + ' return "F"\n' + ' elif _MPU_OFFSET_TILT_Y > 200 and _MPU_OFFSET_TILT_Y < 360:\n' + ' return "B"\n' + ' else:\n' + ' return None\n' + '';
  831. if (_type_selected == "L" || _type_selected == "R") {
  832. _check_function = '(_CHECK_TILT_LR() == "';
  833. } else if (_type_selected == "F" || _type_selected == "B") {
  834. _check_function = '(_CHECK_TILT_FB() == "';
  835. }
  836. var code = _check_function + _type_selected + '")';
  837. return [code, Blockly.Python.ORDER_CONDITIONAL];
  838. };
  839. Blockly.Blocks['esp32_main_controller_motion_when_tilting_STATEMENT'] = {
  840. init: function init() {
  841. this.appendDummyInput().appendField(Blockly.Msg.basic_when_tilting_get).appendField(new Blockly.FieldDropdown([[Blockly.Msg.basic_when_tilting_get_left, "L"], [Blockly.Msg.basic_when_tilting_get_right, "R"], [Blockly.Msg.basic_when_tilting_get_forward, "F"], [Blockly.Msg.basic_when_tilting_get_backward, "B"], [Blockly.Msg.basic_when_tilting_not_tilting, "N"]]), "tilt_type").appendField(Blockly.Msg.basic_when_tilting_get_when);
  842. this.appendStatementInput("input").setCheck(null).appendField(Blockly.Msg.basic_motion_statement_exec);
  843. this.setPreviousStatement(true, null);
  844. this.setNextStatement(true, null);
  845. this.setColour("#e8795b");
  846. var thisBlock = this;
  847. this.setTooltip(function () {
  848. var mode = thisBlock.getFieldValue('tilt_type');
  849. var TOOLTIPS = {
  850. 'L': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left),
  851. 'R': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right),
  852. 'F': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_forward),
  853. 'B': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_backward),
  854. 'N': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_not_tilting)
  855. };
  856. return TOOLTIPS[mode];
  857. });
  858. this.setHelpUrl("");
  859. }
  860. };
  861. Blockly.Python['esp32_main_controller_motion_when_tilting_STATEMENT'] = function (block) {
  862. var _type_selected = block.getFieldValue('tilt_type');
  863. var statements_input = Blockly.Python.statementToCode(block, 'input');
  864. Blockly.Python.definitions_['import_mpu6050'] = '' + 'from machine import I2C, Pin\n' + 'import math\n' + 'import mpu6050\n' + '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' + '';
  865. Blockly.Python.definitions_['mpu6050_standard_def'] = '' + '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n'
  866. /* +
  867. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  868. '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  869. '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  870. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  871. '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  872. '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  873. '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  874. ''*/
  875. ;
  876. Blockly.Python.definitions_['tilting_def'] = '' + 'def _CHECK_TILT_DIRECTION():\n' + ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + ' _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + ' _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + ' _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + ' if _MPU_OFFSET_TILT_X < 150 and _MPU_OFFSET_TILT_X > 0:\n' + ' return "L"\n' + ' elif _MPU_OFFSET_TILT_X > 210 and _MPU_OFFSET_TILT_X < 360:\n' + ' return "R"\n' + ' if _MPU_OFFSET_TILT_Y < 150 and _MPU_OFFSET_TILT_Y > 0:\n' + ' return "B"\n' + ' elif _MPU_OFFSET_TILT_Y > 210 and _MPU_OFFSET_TILT_Y < 360:\n' + ' return "F"\n' + ' else:\n' + ' return "N"\n' + '';
  877. var _check_function = "(_CHECK_TILT_DIRECTION() == \"";
  878. var code = '' + 'if ' + _check_function + _type_selected + '"):\n' + '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '' + statements_input + '\n' + '\n';
  879. return code;
  880. };
  881. Blockly.Blocks['esp32_main_controller_motion_acceleration'] = {
  882. init: function init() {
  883. this.appendDummyInput().appendField(Blockly.Msg.basic_motion_get).appendField(new Blockly.FieldDropdown([["X", "X"], ["Y", "Y"], ["Z", "Z"]]), "accel_type").appendField(Blockly.Msg.basic_motion_accel);
  884. this.setInputsInline(false);
  885. this.setOutput(true, null);
  886. this.setColour("#e8795b");
  887. var thisBlock = this;
  888. this.setTooltip(function () {
  889. var mode = thisBlock.getFieldValue('accel_type');
  890. var TOOLTIPS = {
  891. 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'X'),
  892. 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Y'),
  893. 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Z')
  894. };
  895. return TOOLTIPS[mode];
  896. });
  897. this.setHelpUrl("");
  898. }
  899. };
  900. Blockly.Python['esp32_main_controller_motion_acceleration'] = function (block) {
  901. var _type_selected = block.getFieldValue('accel_type');
  902. Blockly.Python.definitions_['import_mpu6050'] = '' + 'from machine import I2C, Pin\n' + 'import math\n' + 'import mpu6050\n' + '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' + '';
  903. Blockly.Python.definitions_['mpu6050_standard_def'] = '' + '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' + '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' + '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + '';
  904. Blockly.Python.definitions_['mpu6050_get_accel'] = '' + 'def mpu6050_get_accel(type):\n' + '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' + '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' + '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + '\tif type == \'X\':\n' + '\t\treturn _MPU_ACCL_X\n' + '\tif type == \'Y\':\n' + '\t\treturn _MPU_ACCL_Y\n' + '\tif type == \'Z\':\n' + '\t\treturn _MPU_ACCL_Z\n' + ''; // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
  905. var code = 'mpu6050_get_accel(\'' + _type_selected + '\')';
  906. return [code, Blockly.Python.ORDER_CONDITIONAL];
  907. };
  908. Blockly.Blocks['esp32_main_controller_motion_tilt_angle'] = {
  909. init: function init() {
  910. this.appendDummyInput().appendField(Blockly.Msg.basic_motion_get).appendField(new Blockly.FieldDropdown([["X", "X"], ["Y", "Y"], ["Z", "Z"]]), "tilt_angle_type").appendField(Blockly.Msg.basic_motion_angle);
  911. this.setInputsInline(false);
  912. this.setOutput(true, null);
  913. this.setColour("#e8795b");
  914. var thisBlock = this;
  915. this.setTooltip(function () {
  916. var mode = thisBlock.getFieldValue('tilt_angle_type');
  917. var TOOLTIPS = {
  918. 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'X'),
  919. 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Y'),
  920. 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Z')
  921. };
  922. return TOOLTIPS[mode];
  923. });
  924. this.setHelpUrl("");
  925. }
  926. };
  927. Blockly.Python['esp32_main_controller_motion_tilt_angle'] = function (block) {
  928. var _type_selected = block.getFieldValue('tilt_angle_type');
  929. Blockly.Python.definitions_['import_mpu6050'] = '' + 'from machine import I2C, Pin\n' + 'import math\n' + 'import mpu6050\n' + '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' + '';
  930. Blockly.Python.definitions_['mpu6050_standard_def'] = '' + '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' + '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' + '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + '';
  931. Blockly.Python.definitions_['mpu6050_get_angle'] = '' + 'def mpu6050_get_tilt_angle(type):\n' + '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' + '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' + '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' + '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' + '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' + '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' + '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' + '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' + '\tif type == \'X\':\n' + '\t\treturn _MPU_OFFSET_TILT_X\n' + '\tif type == \'Y\':\n' + '\t\treturn _MPU_OFFSET_TILT_Y\n' + '\tif type == \'Z\':\n' + '\t\treturn _MPU_OFFSET_TILT_Z\n' + ''; // Blockly.Python.addVariable('_MPU_OFFSET_TILT_' + _type_selected, '');
  932. var code = 'mpu6050_get_tilt_angle(\'' + _type_selected + '\')';
  933. return [code, Blockly.Python.ORDER_CONDITIONAL];
  934. };
  935. /*
  936. LIGHT & SOUND
  937. */
  938. Blockly.Blocks['esp32_main_controller_get_light'] = {
  939. init: function init() {
  940. // this.appendDummyInput()
  941. // .appendField(new Blockly.FieldImage("blockly/media/light_sensor.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  942. this.appendDummyInput().appendField(Blockly.Msg.basic_light_get);
  943. this.setInputsInline(false);
  944. this.setOutput(true, null);
  945. this.setColour("#e8795b");
  946. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Light_TOOLTIP);
  947. this.setHelpUrl("");
  948. }
  949. };
  950. Blockly.Python['esp32_main_controller_get_light'] = function (block) {
  951. Blockly.Python.definitions_['light_import_def'] = '' + 'from machine import Pin, ADC\n' + '_PHOTONRESISTOR = machine.ADC(Pin(39))\n' + '_PHOTONRESISTOR.atten(ADC.ATTN_11DB)\n' + '_PHOTONRESISTOR.width(ADC.WIDTH_10BIT)\n' + '';
  952. Blockly.Python.addVariable('_PHOTONRESISTOR');
  953. var code = '_PHOTONRESISTOR.read()';
  954. return [code, Blockly.Python.ORDER_CONDITIONAL];
  955. };
  956. /* time */
  957. var ESP32_TIME_COLOR = "#fabe23";
  958. Blockly.Blocks['esp32_main_controller_time_timer_init'] = {
  959. init: function init() {
  960. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "on_esp32"], [Blockly.Msg.time_ai_module, "on_ai"]]), "type").appendField(Blockly.Msg.time_init_timer);
  961. this.setPreviousStatement(true, null);
  962. this.setNextStatement(true, null);
  963. this.setColour(ESP32_TIME_COLOR);
  964. var thisBlock = this;
  965. this.setTooltip(function () {
  966. var mode = thisBlock.getFieldValue('type');
  967. var TOOLTIPS = {
  968. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  969. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  970. };
  971. return TOOLTIPS[mode];
  972. });
  973. this.setHelpUrl("");
  974. }
  975. };
  976. Blockly.Python['esp32_main_controller_time_timer_init'] = function (block) {
  977. var timer_type = block.getFieldValue('type');
  978. if (timer_type == "on_esp32") {
  979. Blockly.Python.definitions_['import_def'] = '' + 'import machine, time\n' + '_TIMER = machine.Timer(0)\n' + '';
  980. var code = '_TIMER.init()\n';
  981. } else if (timer_type == "on_ai") {
  982. Blockly.Python.definitions_['import_def'] = '' + 'import time\n' + 'from machine import Timer\n' + '\n' + 'def _timer_on_timer(timer):\n' + ' global _timer_current_time_elapsed\n' + ' _timer_current_time_elapsed = _timer_current_time_elapsed + 1\n' + '\n' + '_timer_current_time_elapsed = 0\n' + '_timer_tim = Timer(Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PERIODIC, period=1, callback=_timer_on_timer)\n' + '';
  983. var code = '_timer_tim.start()\n';
  984. }
  985. return code;
  986. };
  987. Blockly.Blocks['esp32_main_controller_time_timer_get_current'] = {
  988. init: function init() {
  989. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "on_esp32"], [Blockly.Msg.time_ai_module, "on_ai"]]), "type").appendField(Blockly.Msg.time_timer_get_time);
  990. this.setOutput(true, null);
  991. this.setColour(ESP32_TIME_COLOR);
  992. var thisBlock = this;
  993. this.setTooltip(function () {
  994. var mode = thisBlock.getFieldValue('type');
  995. var TOOLTIPS = {
  996. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  997. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  998. };
  999. return TOOLTIPS[mode];
  1000. });
  1001. this.setHelpUrl("");
  1002. }
  1003. };
  1004. Blockly.Python['esp32_main_controller_time_timer_get_current'] = function (block) {
  1005. var timer_type = block.getFieldValue('type'); // TODO: Assemble Python into code variable.
  1006. if (timer_type == "on_esp32") {
  1007. var code = '_TIMER.value()';
  1008. } else if (timer_type == "on_ai") {
  1009. var code = '_timer_current_time_elapsed';
  1010. } // TODO: Change ORDER_NONE to the correct strength.
  1011. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1012. };
  1013. Blockly.Blocks['esp32_main_controller_time_timer_clear'] = {
  1014. init: function init() {
  1015. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "on_esp32"], [Blockly.Msg.time_ai_module, "on_ai"]]), "type").appendField(Blockly.Msg.time_timer_clear);
  1016. this.setPreviousStatement(true, null);
  1017. this.setNextStatement(true, null);
  1018. this.setColour(ESP32_TIME_COLOR);
  1019. var thisBlock = this;
  1020. this.setTooltip(function () {
  1021. var mode = thisBlock.getFieldValue('type');
  1022. var TOOLTIPS = {
  1023. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1024. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1025. };
  1026. return TOOLTIPS[mode];
  1027. });
  1028. this.setHelpUrl("");
  1029. }
  1030. };
  1031. Blockly.Python['esp32_main_controller_time_timer_clear'] = function (block) {
  1032. var timer_type = block.getFieldValue('type'); // TODO: Assemble Python into code variable.
  1033. if (timer_type == "on_esp32") {
  1034. var code = '_TIMER.deinit()\n';
  1035. } else if (timer_type == "on_ai") {
  1036. var code = '_timer_tim.stop()\n';
  1037. }
  1038. return code;
  1039. };
  1040. Blockly.Blocks['esp32_main_controller_time_period_timer'] = {
  1041. init: function init() {
  1042. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "on_esp32"], [Blockly.Msg.time_ai_module, "on_ai"]]), "type").appendField(Blockly.Msg.time_timer_periodically);
  1043. this.appendDummyInput().appendField(Blockly.Msg.time_timer_every).appendField(new Blockly.FieldTextInput("500"), "period_timer_count").appendField(Blockly.Msg.time_timer_ms);
  1044. this.appendStatementInput("exec_period_timer").setCheck(null).appendField(Blockly.Msg.time_execution);
  1045. this.setPreviousStatement(true, null);
  1046. this.setNextStatement(true, null);
  1047. this.setColour(ESP32_TIME_COLOR);
  1048. var thisBlock = this;
  1049. this.setTooltip(function () {
  1050. var mode = thisBlock.getFieldValue('type');
  1051. var TOOLTIPS = {
  1052. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1053. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1054. };
  1055. return TOOLTIPS[mode];
  1056. });
  1057. this.setHelpUrl("");
  1058. }
  1059. };
  1060. Blockly.Python['esp32_main_controller_time_period_timer'] = function (block) {
  1061. var timer_type = block.getFieldValue('type');
  1062. var text_period_timer_count = block.getFieldValue('period_timer_count');
  1063. var statements_name = Blockly.Python.statementToCode(block, 'exec_period_timer');
  1064. if (timer_type == "on_esp32") {
  1065. Blockly.Python.definitions_['import_def'] = '' + 'from machine import Timer\n' + '\n' + 'def _PRD_EXEC_tick(_):\n' + ' global _ControlTopic, _ReportTopic\n' + statements_name + '\n' + '_PRD_EXEC_TIMER = Timer(0)\n' + '_PRD_EXEC_TIMER.deinit()\n' + '_PRD_EXEC_TIMER.init(period=' + text_period_timer_count + ', mode=Timer.PERIODIC, callback=_PRD_EXEC_tick)\n' + '';
  1066. } else if (timer_type == "on_ai") {
  1067. Blockly.Python.definitions_['import_def'] = '' + 'import time\n' + 'from machine import Timer\n' + '\n' + 'def _on_timer(timer):\n' + statements_name + '\n' + 'from machine import Timer\n' + '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=' + text_period_timer_count + ', callback=_on_timer, arg=_on_timer)\n\n' + '_periodic_tim.start()\n' + '';
  1068. } // TODO: Assemble Python into code variable.
  1069. var code = '';
  1070. return code;
  1071. };
  1072. Blockly.Blocks['esp32_main_controller_time_period_timer_clear'] = {
  1073. init: function init() {
  1074. this.appendDummyInput().appendField("").appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_iot_module, "on_esp32"], [Blockly.Msg.time_ai_module, "on_ai"]]), "type").appendField(Blockly.Msg.time_timer_periodically_clear);
  1075. this.setPreviousStatement(true, null);
  1076. this.setNextStatement(true, null);
  1077. this.setColour(ESP32_TIME_COLOR);
  1078. var thisBlock = this;
  1079. this.setTooltip(function () {
  1080. var mode = thisBlock.getFieldValue('type');
  1081. var TOOLTIPS = {
  1082. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1083. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1084. };
  1085. return TOOLTIPS[mode];
  1086. });
  1087. this.setHelpUrl("");
  1088. }
  1089. };
  1090. Blockly.Python['esp32_main_controller_time_period_timer_clear'] = function (block) {
  1091. var timer_type = block.getFieldValue('type'); // TODO: Assemble Python into code variable.
  1092. if (timer_type == "on_esp32") {
  1093. var code = '_PRD_EXEC_TIMER.deinit()\n';
  1094. } else if (timer_type == "on_ai") {
  1095. var code = '_periodic_tim.stop()\n';
  1096. }
  1097. return code;
  1098. };
  1099. Blockly.Blocks['esp32_get_current_date'] = {
  1100. init: function init() {
  1101. this.appendDummyInput().appendField(Blockly.Msg.time_get_current_date_title).appendField(new Blockly.FieldDropdown([[Blockly.Msg.time_get_current_date_y, "0"], [Blockly.Msg.time_get_current_date_m, "1"], [Blockly.Msg.time_get_current_date_d, "2"], [Blockly.Msg.time_get_current_date_hour, "3"], [Blockly.Msg.time_get_current_date_min, "4"], [Blockly.Msg.time_get_current_date_sec, "5"]]), "type");
  1102. this.setInputsInline(false);
  1103. this.setOutput(true, null);
  1104. this.setColour(ESP32_TIME_COLOR);
  1105. var thisBlock = this;
  1106. this.setTooltip(function () {
  1107. var mode = thisBlock.getFieldValue('type');
  1108. var TOOLTIPS = {
  1109. '0': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_y),
  1110. '1': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_m),
  1111. '2': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_d),
  1112. '3': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_hour),
  1113. '4': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_min),
  1114. '5': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_sec)
  1115. };
  1116. return TOOLTIPS[mode];
  1117. });
  1118. this.setHelpUrl("");
  1119. }
  1120. };
  1121. Blockly.Python['esp32_get_current_date'] = function (block) {
  1122. var dropdown_type = block.getFieldValue('type');
  1123. Blockly.Python.definitions_['get_date_current_ntp'] = '' + 'import time\n' + '\n'; // TODO: Assemble Python into code variable.
  1124. var code = 'time.localtime()[' + dropdown_type + ']'; // TODO: Change ORDER_NONE to the correct strength.
  1125. return [code, Blockly.Python.ORDER_NONE];
  1126. };
  1127. /*
  1128. LCD
  1129. */
  1130. var ESP32_SCREEN_COLOR = "#5bb2d6";
  1131. Blockly.Blocks['esp32_main_controller_lcd_rotation'] = {
  1132. init: function init() {
  1133. this.appendDummyInput().appendField(Blockly.Msg.OLCD_SETROTATION).appendField(new Blockly.FieldDropdown([["0°", "1"], ["90°", "2"], ["180°", "3"], ["270°", "0"]]), "DEGREE").appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE);
  1134. this.setPreviousStatement(true, null);
  1135. this.setNextStatement(true, null);
  1136. this.setColour("#5bb2d6");
  1137. this.setTooltip('');
  1138. this.setHelpUrl('');
  1139. }
  1140. };
  1141. Blockly.Python['esp32_main_controller_lcd_rotation'] = function (block) {
  1142. var degree = block.getFieldValue("DEGREE");
  1143. var code = "tft.rotation(" + degree + ")\n"; // Blockly.Python.addSetup("setRotation", code);
  1144. return code;
  1145. };
  1146. Blockly.Blocks['esp32_main_controller_clear_screen'] = {
  1147. init: function init() {
  1148. this.appendDummyInput().appendField(Blockly.Msg.display_clear_screen);
  1149. this.setInputsInline(true);
  1150. this.setPreviousStatement(true, null);
  1151. this.setNextStatement(true, null);
  1152. this.setColour(ESP32_SCREEN_COLOR);
  1153. this.setTooltip("");
  1154. this.setHelpUrl("");
  1155. }
  1156. };
  1157. Blockly.Python['esp32_main_controller_clear_screen'] = function (block) {
  1158. // TODO: Assemble Python into code variable.
  1159. var code = 'tft.fill(TFT.BLACK)\n';
  1160. return code;
  1161. };
  1162. Blockly.Blocks['esp32_main_controller_lcd_screen_fill'] = {
  1163. init: function init() {
  1164. this.appendDummyInput().appendField(Blockly.Msg.display_fill_screen);
  1165. this.appendValueInput("COLOR").setCheck(null);
  1166. this.setInputsInline(true);
  1167. this.setPreviousStatement(true, null);
  1168. this.setNextStatement(true, null);
  1169. this.setColour(ESP32_SCREEN_COLOR);
  1170. this.setTooltip("");
  1171. this.setHelpUrl("");
  1172. }
  1173. };
  1174. Blockly.Python['esp32_main_controller_lcd_screen_fill'] = function (block) {
  1175. var value_lcd_rgb_value = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  1176. if (value_lcd_rgb_value.charAt(0) != '#') {
  1177. var code = 'tft.fill(_TFTColor' + value_lcd_rgb_value + ')\n';
  1178. } else if (value_lcd_rgb_value.charAt(0) == '#') {
  1179. var d = 0,
  1180. e = 0,
  1181. f = 0;
  1182. try {
  1183. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  1184. } catch (g) { }
  1185. var code = "tft.fill(_TFTColor" + d + "," + e + "," + f + ")\n";
  1186. }
  1187. return code;
  1188. };
  1189. Blockly.Blocks['esp32_main_controller_lcd_drawpixel'] = {
  1190. init: function init() {
  1191. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_pixel.png", 42, 42, {
  1192. alt: "*",
  1193. flipRtl: "FALSE"
  1194. }));
  1195. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_pixel);
  1196. this.appendValueInput("COLOR").setCheck("String").appendField(Blockly.Msg.OLCD_COLOR);
  1197. this.appendValueInput("POS").setCheck("String").appendField(Blockly.Msg.OLCD_COORDINATE);
  1198. this.setInputsInline(false);
  1199. this.setPreviousStatement(true);
  1200. this.setNextStatement(true);
  1201. this.setColour("#5bb2d6");
  1202. this.setTooltip('');
  1203. this.setHelpUrl('');
  1204. }
  1205. };
  1206. Blockly.Python['esp32_main_controller_lcd_drawpixel'] = function (block) {
  1207. // var drawrectcolor = sharpTo565(
  1208. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  1209. var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  1210. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1211. if (color.charAt(0) == '#') {
  1212. var d = 0,
  1213. e = 0,
  1214. f = 0;
  1215. try {
  1216. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  1217. } catch (g) { }
  1218. code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + d + "," + e + "," + f + ")\n";
  1219. } else if (color.charAt(0) != '#') {
  1220. code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + color + ")\n";
  1221. }
  1222. return code;
  1223. };
  1224. Blockly.Blocks['esp32_main_controller_lcd_drawline'] = {
  1225. init: function init() {
  1226. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, {
  1227. alt: "*",
  1228. flipRtl: "FALSE"
  1229. })); // this.appendDummyInput()
  1230. // .appendField(
  1231. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  1232. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_LINE);
  1233. this.appendValueInput("COLOR").setCheck("String").appendField(Blockly.Msg.OLCD_COLOR);
  1234. this.appendValueInput("POSA").setCheck("String").appendField(Blockly.Msg.OLCD_LINE_START);
  1235. this.appendValueInput("POSB").setCheck("String").appendField(Blockly.Msg.OLCD_LINE_END);
  1236. this.setInputsInline(false);
  1237. this.setPreviousStatement(true);
  1238. this.setNextStatement(true);
  1239. this.setColour("#5bb2d6");
  1240. this.setTooltip('');
  1241. this.setHelpUrl('');
  1242. }
  1243. };
  1244. Blockly.Python['esp32_main_controller_lcd_drawline'] = function (block) {
  1245. // var drawlinecolor = sharpTo565(
  1246. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  1247. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1248. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1249. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1250. if (color.charAt(0) == '#') {
  1251. var d = 0,
  1252. e = 0,
  1253. f = 0;
  1254. try {
  1255. 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16));
  1256. } catch (g) { }
  1257. code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + d + "," + e + "," + f + ")\n";
  1258. } else if (color.charAt(0) != '#') {
  1259. code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n";
  1260. }
  1261. return code;
  1262. };
  1263. Blockly.Blocks['esp32_main_controller_lcd_drawlinelen'] = {
  1264. init: function init() {
  1265. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, {
  1266. alt: "*",
  1267. flipRtl: "FALSE"
  1268. })); // this.appendDummyInput()
  1269. // .appendField(
  1270. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  1271. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW).appendField(new Blockly.FieldDropdown([[Blockly.Msg.display_draw_vertical, "vline"], [Blockly.Msg.display_draw_horizontal, "hline"]]), "rect_type").appendField(Blockly.Msg.OLCD_LINE);
  1272. this.appendValueInput("COLOR").setCheck(null).appendField(Blockly.Msg.OLCD_COLOR);
  1273. this.appendValueInput("POS").setCheck(null).appendField(Blockly.Msg.display_start_cord);
  1274. this.appendValueInput("LENGTH").setCheck(null).appendField(Blockly.Msg.display_length);
  1275. this.setInputsInline(false);
  1276. this.setPreviousStatement(true);
  1277. this.setNextStatement(true);
  1278. this.setColour("#5bb2d6");
  1279. this.setTooltip('');
  1280. this.setHelpUrl('');
  1281. }
  1282. };
  1283. Blockly.Python['esp32_main_controller_lcd_drawlinelen'] = function (block) {
  1284. var _type = block.getFieldValue("rect_type");
  1285. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1286. var drawlinestart = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  1287. var drawlineend = Blockly.Python.valueToCode(block, 'LENGTH', Blockly.Python.ORDER_ATOMIC);
  1288. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1289. code = "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", _TFTColor" + color + ")\n";
  1290. return code;
  1291. };
  1292. Blockly.Blocks['esp32_main_controller_lcd_drawrect'] = {
  1293. init: function init() {
  1294. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 42, 42, {
  1295. alt: "*",
  1296. flipRtl: "FALSE"
  1297. })); // this.appendDummyInput()
  1298. // .appendField(
  1299. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  1300. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW).appendField(new Blockly.FieldDropdown([[Blockly.Msg.OLCD_FULL, "fillrect"], [Blockly.Msg.OLCD_CIRCLE, "rect"]]), "rect_type").appendField(Blockly.Msg.display_draw_rectangle);
  1301. this.appendValueInput("COLOR").setCheck("String").appendField(Blockly.Msg.OLCD_COLOR);
  1302. this.appendValueInput("POSA").setCheck("String").appendField(Blockly.Msg.OLCD_COORDINATE);
  1303. this.appendValueInput("POSB").setCheck("String").appendField(Blockly.Msg.OLCD_SIZE);
  1304. this.setInputsInline(false);
  1305. this.setPreviousStatement(true);
  1306. this.setNextStatement(true);
  1307. this.setColour("#5bb2d6");
  1308. this.setTooltip('');
  1309. this.setHelpUrl('');
  1310. }
  1311. };
  1312. Blockly.Python['esp32_main_controller_lcd_drawrect'] = function (block) {
  1313. // var drawlinecolor = sharpTo565(
  1314. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  1315. var _type = block.getFieldValue("rect_type");
  1316. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1317. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1318. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1319. code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n";
  1320. return code;
  1321. };
  1322. Blockly.Blocks['esp32_main_controller_lcd_drawcircle'] = {
  1323. init: function init() {
  1324. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 42, 42, {
  1325. alt: "*",
  1326. flipRtl: "FALSE"
  1327. }));
  1328. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW).appendField(new Blockly.FieldDropdown([[Blockly.Msg.OLCD_FULL, "fillcircle"], [Blockly.Msg.OLCD_STROKED, "circle"]]), "circle_type").appendField(Blockly.Msg.OLCD_CIRCLE);
  1329. this.appendValueInput("COLOR").setCheck("String").appendField(Blockly.Msg.OLCD_COLOR);
  1330. this.appendValueInput("POSA").setCheck("String").appendField(Blockly.Msg.OLCD_CENTER_POS);
  1331. this.appendValueInput("RADIUS").setCheck(null).appendField(Blockly.Msg.OLCD_RADIUS);
  1332. this.setInputsInline(false);
  1333. this.setPreviousStatement(true);
  1334. this.setNextStatement(true);
  1335. this.setColour("#5bb2d6");
  1336. this.setTooltip('');
  1337. this.setHelpUrl('');
  1338. }
  1339. };
  1340. Blockly.Python['esp32_main_controller_lcd_drawcircle'] = function (block) {
  1341. // var drawcirclecolor = sharpTo565(
  1342. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  1343. var circletype = block.getFieldValue('circle_type');
  1344. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1345. var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
  1346. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1347. code = 'tft.' + circletype + '((' + position + '), ' + radius + ', _TFTColor' + color + ')\n';
  1348. return code;
  1349. };
  1350. Blockly.Blocks['esp32_main_controller_lcd_drawqrcode'] = {
  1351. init: function init() {
  1352. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_qr.png", 42, 42, {
  1353. alt: "*",
  1354. flipRtl: "FALSE"
  1355. })); // this.appendDummyInput()
  1356. // .appendField(
  1357. // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
  1358. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_drawqr_text);
  1359. this.appendValueInput("POSA").setCheck("String").appendField(Blockly.Msg.OLCD_SET_POS);
  1360. this.appendValueInput("CONTENT").setCheck("String").appendField(Blockly.Msg.display_drawqr_content);
  1361. this.appendValueInput("WIDTH").setCheck("Number").appendField(Blockly.Msg.display_drawqr_padding);
  1362. this.setInputsInline(false);
  1363. this.setPreviousStatement(true);
  1364. this.setNextStatement(true);
  1365. this.setColour("#5bb2d6");
  1366. this.setTooltip('');
  1367. this.setHelpUrl('');
  1368. }
  1369. };
  1370. Blockly.Python['esp32_main_controller_lcd_drawqrcode'] = function (block) {
  1371. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1372. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1373. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  1374. var code = 'tft.drawqrcode(' + '(' + position + '), ' + content + ', ' + width + ')\n';
  1375. return code;
  1376. };
  1377. Blockly.Blocks['esp32_main_controller_lcd_drawtext'] = {
  1378. init: function init() {
  1379. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 42, 42, {
  1380. alt: "*",
  1381. flipRtl: "FALSE"
  1382. })); // this.appendDummyInput()
  1383. // .appendField(
  1384. // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
  1385. this.appendDummyInput().appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_text);
  1386. this.appendValueInput("COLOR").setCheck("String").appendField(Blockly.Msg.OLCD_COLOR);
  1387. this.appendValueInput("POSA").setCheck("String").appendField(Blockly.Msg.display_start_cord);
  1388. this.appendValueInput("CONTENT").setCheck("String").appendField(Blockly.Msg.display_draw_text_content);
  1389. this.appendValueInput("SIZE").setCheck("Number").appendField(Blockly.Msg.display_draw_text_size);
  1390. this.appendDummyInput().appendField(Blockly.Msg.display_draw_text_return).appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
  1391. this.setInputsInline(false);
  1392. this.setPreviousStatement(true);
  1393. this.setNextStatement(true);
  1394. this.setColour("#5bb2d6");
  1395. this.setTooltip('');
  1396. this.setHelpUrl('');
  1397. }
  1398. };
  1399. Blockly.Python['esp32_main_controller_lcd_drawtext'] = function (block) {
  1400. // var drawcirclecolor = sharpTo565(
  1401. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  1402. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1403. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1404. var size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
  1405. var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
  1406. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1407. code = 'tft.text((' + position + '), ' + content + ', _TFTColor' + color + ', sysfont, ' + size + ', nowrap=' + box + ')\n';
  1408. return code;
  1409. };
  1410. Blockly.Blocks['esp32_main_controller_rgb_value'] = {
  1411. init: function init() {
  1412. this.appendValueInput("rgb_value_r").setCheck(null).appendField(Blockly.Msg.display_red);
  1413. this.appendValueInput("rgb_value_g").setCheck(null).appendField(Blockly.Msg.display_green);
  1414. this.appendValueInput("rgb_value_b").setCheck(null).appendField(Blockly.Msg.display_blue);
  1415. this.setInputsInline(true);
  1416. this.setOutput(true, null);
  1417. this.setColour(ESP32_SCREEN_COLOR);
  1418. this.setTooltip("");
  1419. this.setHelpUrl("");
  1420. }
  1421. };
  1422. Blockly.Python['esp32_main_controller_rgb_value'] = function (block) {
  1423. var r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1424. var g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1425. var b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1426. var original_r = parseInt(r, 10);
  1427. var original_g = parseInt(g, 10);
  1428. var original_b = parseInt(b, 10);
  1429. _r = original_r.toString(16);
  1430. _g = original_g.toString(16);
  1431. _b = original_b.toString(16);
  1432. if (_r.length == 1) _r = "0" + _r;
  1433. if (_g.length == 1) _g = "0" + _g;
  1434. if (_b.length == 1) _b = "0" + _b; // TODO: Assemble Python into code variable.
  1435. var code = '' + r + ',' + g + ',' + b + ''; // TODO: Change ORDER_NONE to the correct strength.
  1436. return [code, Blockly.Python.ORDER_NONE];
  1437. };
  1438. Blockly.Blocks['esp32_main_controller_lcd_color_hex_to_rgb'] = {
  1439. init: function init() {
  1440. this.appendDummyInput().appendField(Blockly.Msg.COLOR).appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
  1441. this.setOutput(true, null);
  1442. this.setColour("#5bb2d6");
  1443. this.setTooltip('');
  1444. this.setHelpUrl('');
  1445. }
  1446. };
  1447. Blockly.Python['esp32_main_controller_lcd_color_hex_to_rgb'] = function (a) {
  1448. a = a.getFieldValue("COLOR"); // var d = 0
  1449. // , e = 0
  1450. // , f = 0;
  1451. // try {
  1452. // 7 == a.length && (d = a.substring(1, 3),
  1453. // e = a.substring(3, 5),
  1454. // f = a.substring(5, 7));
  1455. // }
  1456. // catch (g) { }
  1457. var gethex = a;
  1458. function hexToRGB(h) {
  1459. var r = 0,
  1460. g = 0,
  1461. b = 0; // 3 digits
  1462. if (h.length == 4) {
  1463. r = "0x" + h[1] + h[1];
  1464. g = "0x" + h[2] + h[2];
  1465. b = "0x" + h[3] + h[3]; // 6 digits
  1466. } else if (h.length == 7) {
  1467. r = "0x" + h[1] + h[2];
  1468. g = "0x" + h[3] + h[4];
  1469. b = "0x" + h[5] + h[6];
  1470. }
  1471. return "" + +r + "," + +g + "," + +b + "";
  1472. }
  1473. var _code = hexToRGB(gethex); //_code += "tft.fill(TFT.TFTColor(FS_COLOR))\n";
  1474. return [_code, Blockly.Python.ORDER_CONDITIONAL];
  1475. };
  1476. /*
  1477. WiFi
  1478. */
  1479. var ESP32_WIFI_COLOR = "#3062c1";
  1480. Blockly.Blocks['esp32_main_controller_wifi_enable_hotspot_mode'] = {
  1481. init: function init() {
  1482. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/hotspot_setup.png", 38, 38, {
  1483. alt: "*",
  1484. flipRtl: "FALSE"
  1485. }));
  1486. this.appendDummyInput().appendField(Blockly.Msg.wifi_setup_hotspot);
  1487. this.appendValueInput("SSID").setCheck(null).appendField(Blockly.Msg.wifi_hotspot_ssid);
  1488. this.appendValueInput("PASSWORD").setCheck(null).appendField(Blockly.Msg.wifi_hotspot_pass);
  1489. this.setInputsInline(false);
  1490. this.setPreviousStatement(true, null);
  1491. this.setNextStatement(true, null);
  1492. this.setColour(ESP32_WIFI_COLOR);
  1493. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Enable_Hotspot_Mode_TOOLTIP);
  1494. this.setHelpUrl("");
  1495. }
  1496. };
  1497. Blockly.Python['esp32_main_controller_wifi_enable_hotspot_mode'] = function (block) {
  1498. var text_ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC);
  1499. var text_password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC);
  1500. var _code = '' + '_WIFI = network.WLAN(network.STA_IF)\n' + '_WIFI.active(False)\n' + '_WIFI = network.WLAN(network.AP_IF)\n' + '_WIFI.active(True)\n';
  1501. Blockly.Python.definitions_['import_network'] = 'import network\n' + _code + '_WIFI.config(essid=' + text_ssid + ', password=' + text_password + ', authmode=2, channel=11, hidden=0)\n' + '\n';
  1502. code = '';
  1503. return code;
  1504. };
  1505. Blockly.Blocks['esp32_main_controller_wifi_connect_internet'] = {
  1506. init: function init() {
  1507. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, {
  1508. alt: "*",
  1509. flipRtl: "FALSE"
  1510. }));
  1511. this.appendDummyInput().appendField(Blockly.Msg.wifi_connect_router);
  1512. this.appendValueInput("ssid").setCheck(null).appendField(Blockly.Msg.wifi_router_ssid);
  1513. this.appendValueInput("password").setCheck(null).appendField(Blockly.Msg.wifi_router_pass);
  1514. this.setInputsInline(false);
  1515. this.setPreviousStatement(true, null);
  1516. this.setNextStatement(true, null);
  1517. this.setColour(ESP32_WIFI_COLOR);
  1518. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Connect_Internet_TOOLTIP);
  1519. this.setHelpUrl("");
  1520. }
  1521. };
  1522. Blockly.Python['esp32_main_controller_wifi_connect_internet'] = function (block) {
  1523. var text_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
  1524. var text_password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
  1525. Blockly.Python.definitions_['esp32_import_network'] = '' + 'import network\n' + 'if (network.WLAN(network.STA_IF).isconnected()) == False:\n' + ' _WIFI = network.WLAN(network.AP_IF)\n' + ' _WIFI.active(False)\n' + ' _WIFI = network.WLAN(network.STA_IF)\n' + ' _WIFI.active(True)\n' + ' _WIFI.connect(' + text_ssid + ', ' + text_password + ')\n' + 'else:\n' + ' pass\n' + '\n';
  1526. var code = '';
  1527. return code;
  1528. };
  1529. Blockly.Blocks['esp32_main_controller_wifi_disconnect'] = {
  1530. init: function init() {
  1531. this.appendDummyInput().appendField(Blockly.Msg.wifi_router_disconnect);
  1532. this.setInputsInline(false);
  1533. this.setPreviousStatement(true, null);
  1534. this.setNextStatement(true, null);
  1535. this.setColour(ESP32_WIFI_COLOR);
  1536. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Disconnect_TOOLTIP);
  1537. this.setHelpUrl("");
  1538. }
  1539. };
  1540. Blockly.Python['esp32_main_controller_wifi_disconnect'] = function (block) {
  1541. // TODO: Assemble Python into code variable.
  1542. var code = '_WIFI.active(False)\n';
  1543. return code;
  1544. };
  1545. Blockly.Blocks['esp32_main_controller_get_wifi_devices_number'] = {
  1546. init: function init() {
  1547. this.appendDummyInput().appendField(Blockly.Msg.wifi_hotspot_get_clients);
  1548. this.setOutput(true, null);
  1549. this.setColour(ESP32_WIFI_COLOR);
  1550. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Devices_Number_TOOLTIP);
  1551. this.setHelpUrl("");
  1552. }
  1553. };
  1554. Blockly.Python['esp32_main_controller_get_wifi_devices_number'] = function (block) {
  1555. // TODO: Assemble Python into code variable.
  1556. var code = 'len(_WIFI.status("stations"))'; // TODO: Change ORDER_NONE to the correct strength.
  1557. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1558. };
  1559. Blockly.Blocks['esp32_main_controller_wifi_acquiring_equipment'] = {
  1560. init: function init() {
  1561. this.appendDummyInput().appendField(Blockly.Msg.wifi_hotspot_get_list);
  1562. this.setOutput(true, null);
  1563. this.setColour(ESP32_WIFI_COLOR);
  1564. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Acquiring_Equipment_TOOLTIP);
  1565. this.setHelpUrl("");
  1566. }
  1567. };
  1568. Blockly.Python['esp32_main_controller_wifi_acquiring_equipment'] = function (block) {
  1569. // TODO: Assemble Python into code variable.
  1570. var code = '_WIFI.status("stations")'; // TODO: Change ORDER_NONE to the correct strength.
  1571. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1572. };
  1573. Blockly.Blocks['esp32_main_controller_get_wifi_connection_status'] = {
  1574. init: function init() {
  1575. this.appendDummyInput().appendField(Blockly.Msg.wifi_router_connection_status);
  1576. this.setOutput(true, null);
  1577. this.setColour(ESP32_WIFI_COLOR);
  1578. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Connection_Status_TOOLTIP);
  1579. this.setHelpUrl("");
  1580. }
  1581. };
  1582. Blockly.Python['esp32_main_controller_get_wifi_connection_status'] = function (block) {
  1583. // TODO: Assemble Python into code variable.
  1584. var code = '_WIFI.isconnected()'; // TODO: Change ORDER_NONE to the correct strength.
  1585. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1586. };
  1587. Blockly.Blocks['esp32_main_controller_wifi_close'] = {
  1588. init: function init() {
  1589. this.appendDummyInput().appendField(Blockly.Msg.wifi_close_hotspot);
  1590. this.setPreviousStatement(true, null);
  1591. this.setNextStatement(true, null);
  1592. this.setColour(ESP32_WIFI_COLOR);
  1593. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Close_TOOLTIP);
  1594. this.setHelpUrl("");
  1595. }
  1596. };
  1597. Blockly.Python['esp32_main_controller_wifi_close'] = function (block) {
  1598. // TODO: Assemble Python into code variable.
  1599. var code = '_WIFI.active(False)'; // TODO: Change ORDER_NONE to the correct strength.
  1600. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1601. };
  1602. Blockly.Blocks['esp32_main_controller_wifi_get_info'] = {
  1603. init: function init() {
  1604. this.appendDummyInput().appendField(Blockly.Msg.wifi_router_get_info).appendField(new Blockly.FieldDropdown([[Blockly.Msg.wifi_router_device_ip, "0"], [Blockly.Msg.wifi_router_device_mask, "1"], [Blockly.Msg.wifi_router_device_gateway, "2"], [Blockly.Msg.wifi_router_device_dns, "3"]]), "wifi_info");
  1605. this.setOutput(true, null);
  1606. this.setColour(ESP32_WIFI_COLOR);
  1607. var thisBlock = this;
  1608. this.setTooltip(function () {
  1609. var mode = thisBlock.getFieldValue('wifi_info');
  1610. var TOOLTIPS = {
  1611. '0': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_ip),
  1612. '1': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_mask),
  1613. '2': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_gateway),
  1614. '3': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_dns)
  1615. };
  1616. return TOOLTIPS[mode];
  1617. });
  1618. this.setHelpUrl("");
  1619. }
  1620. };
  1621. Blockly.Python['esp32_main_controller_wifi_get_info'] = function (block) {
  1622. var dropdown_name = block.getFieldValue('wifi_info'); // TODO: Assemble Python into code variable.
  1623. var code = '_WIFI.ifconfig()[' + dropdown_name + ']'; // TODO: Change ORDER_NONE to the correct strength.
  1624. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1625. };
  1626. Blockly.Blocks['esp32_main_controller_wifi_ntptime'] = {
  1627. init: function init() {
  1628. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/ntptime_header.png", 38, 38, {
  1629. alt: "*",
  1630. flipRtl: "FALSE"
  1631. }));
  1632. this.appendDummyInput().appendField(Blockly.Msg.wifi_ntptime_title);
  1633. this.appendDummyInput().appendField(Blockly.Msg.wifi_ntptime_set_timezone).appendField(new Blockly.FieldDropdown([[Blockly.Msg.wifi_ntptime_utc8, "8"], [Blockly.Msg.wifi_ntptime_utc9, "9"], [Blockly.Msg.wifi_ntptime_utc10, "10"], [Blockly.Msg.wifi_ntptime_utc11, "11"], [Blockly.Msg.wifi_ntptime_utc12, "12"], [Blockly.Msg.wifi_ntptime_utcm12, "-12"], [Blockly.Msg.wifi_ntptime_utcm11, "-11"], [Blockly.Msg.wifi_ntptime_utcm10, "-10"], [Blockly.Msg.wifi_ntptime_utcm9, "-9"], [Blockly.Msg.wifi_ntptime_utcm8, "-8"], [Blockly.Msg.wifi_ntptime_utcm7, "-7"], [Blockly.Msg.wifi_ntptime_utcm6, "-6"], [Blockly.Msg.wifi_ntptime_utcm5, "-5"], [Blockly.Msg.wifi_ntptime_utcm4, "-4"], [Blockly.Msg.wifi_ntptime_utcm3, "-3"], [Blockly.Msg.wifi_ntptime_utcm2, "-2"], [Blockly.Msg.wifi_ntptime_utcm1, "-1"], [Blockly.Msg.wifi_ntptime_utc0, "0"], [Blockly.Msg.wifi_ntptime_utc1, "1"], [Blockly.Msg.wifi_ntptime_utc2, "2"], [Blockly.Msg.wifi_ntptime_utc3, "3"], [Blockly.Msg.wifi_ntptime_utc4, "4"], [Blockly.Msg.wifi_ntptime_utc5, "5"], [Blockly.Msg.wifi_ntptime_utc6, "6"], [Blockly.Msg.wifi_ntptime_utc7, "7"]]), "timezone");
  1634. this.appendDummyInput().appendField(Blockly.Msg.wifi_ntptime_time_server).appendField(new Blockly.FieldTextInput("time.windows.com"), "website");
  1635. this.setInputsInline(false);
  1636. this.setPreviousStatement(true, null);
  1637. this.setNextStatement(true, null);
  1638. this.setColour(ESP32_WIFI_COLOR);
  1639. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Ntptime_TOOLTIP);
  1640. this.setHelpUrl("");
  1641. }
  1642. };
  1643. Blockly.Python['esp32_main_controller_wifi_ntptime'] = function (block) {
  1644. var dropdown_timezone = block.getFieldValue('timezone');
  1645. var text_website = block.getFieldValue('website');
  1646. Blockly.Python.definitions_['esp32_main_controller_wifi_ntptime_def'] = '' + 'import network\n' + 'import ntptime\n' + '\n'; // TODO: Assemble Python into code variable.
  1647. var code = 'ntptime.settime(' + dropdown_timezone + ', "' + text_website + '")\n';
  1648. return code;
  1649. };
  1650. /*
  1651. IoT Service
  1652. */
  1653. Blockly.Blocks['esp32_main_controller_ThingSpeak_send_data'] = {
  1654. init: function init() {
  1655. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 190, 50, {
  1656. alt: "*",
  1657. flipRtl: "FALSE"
  1658. }));
  1659. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_send_title);
  1660. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_send_api_set).appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_API_KEY"), "key");
  1661. this.appendValueInput("data").setCheck(null).appendField(Blockly.Msg.iotservice_thingspeak_send_data_1);
  1662. this.setPreviousStatement(true, null);
  1663. this.setNextStatement(true, null);
  1664. this.setColour(ESP32_IOT_COLOR);
  1665. this.setTooltip("");
  1666. this.setHelpUrl("");
  1667. }
  1668. };
  1669. Blockly.Python['esp32_main_controller_ThingSpeak_send_data'] = function (block) {
  1670. var text_key = block.getFieldValue('key');
  1671. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  1672. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  1673. '';
  1674. var code = '_THINGSPEAK_CHANNEL_API = "' + text_key + '"\n';
  1675. Blockly.Python.addSetup("esp32_main_controller_ThingSpeak_send_data", code);
  1676. var code = '_THINGSPEAK_DATA1 = ' + data + '\n';
  1677. code += '_THINGSPEAK_ENDPOINT = "http://api.thingspeak.com/update?api_key=" + _THINGSPEAK_CHANNEL_API + "&field1=" + _THINGSPEAK_DATA1\n';
  1678. code += '_THINGSPEAK_REQEUST = urequests.get(_THINGSPEAK_ENDPOINT)\n';
  1679. return code;
  1680. };
  1681. Blockly.Blocks['CocoRobo_set_max'] = {
  1682. init: function init() {
  1683. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 50, {
  1684. alt: "*",
  1685. flipRtl: "FALSE"
  1686. }));
  1687. this.appendDummyInput().appendField(Blockly.Msg.internet_server_setup);
  1688. this.appendDummyInput().appendField(Blockly.Msg.internet_server_set_max).appendField(new Blockly.FieldTextInput("5"), "number");
  1689. this.setPreviousStatement(true, null);
  1690. this.setNextStatement(true, null);
  1691. this.setColour("#183895");
  1692. this.setTooltip("");
  1693. this.setHelpUrl("");
  1694. }
  1695. };
  1696. Blockly.Python['CocoRobo_set_max'] = function (block) {
  1697. var number = block.getFieldValue('number');
  1698. Blockly.Python.definitions_['import_socket'] = 'import usocket as socket';
  1699. var code = '_S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n';
  1700. code += '_S.bind((_WIFI.ifconfig()[0], 80))\n';
  1701. code += '_S.listen(' + number + ')\n';
  1702. Blockly.Python.addSetup("CocoRobo_set_max", code);
  1703. var code = '_SERVER_CONN, _SERVER_ADDR = _S.accept()\n';
  1704. code += '_SERVER_REQUEST = _CONN.recv(1024)\n';
  1705. return code;
  1706. };
  1707. Blockly.Blocks['CocoRobo_get_source'] = {
  1708. init: function init() {
  1709. this.appendDummyInput().appendField(Blockly.Msg.internet_server_set_source);
  1710. this.setOutput(true, null);
  1711. this.setColour("#183895");
  1712. this.setTooltip("");
  1713. this.setHelpUrl("");
  1714. }
  1715. };
  1716. Blockly.Python['CocoRobo_get_source'] = function (block) {
  1717. var code = 'str(_SERVER_ADDR)\n';
  1718. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1719. };
  1720. Blockly.Blocks['CocoRobo_get'] = {
  1721. init: function init() {
  1722. this.appendDummyInput().appendField(Blockly.Msg.internet_response_http_content_title).appendField(new Blockly.FieldDropdown([[Blockly.Msg.internet_response_text, "text"], [Blockly.Msg.internet_response_content, "content"], [Blockly.Msg.internet_response_status, "state"], [Blockly.Msg.internet_response_json, "json"], [Blockly.Msg.internet_response_encode, "code"], [Blockly.Msg.internet_response_reason, "reason"]]), "op");
  1723. this.setOutput(true, null);
  1724. this.setColour("#183895");
  1725. this.setTooltip(Blockly.Msg.CocoRobo_get_TOOLTIP);
  1726. this.setHelpUrl("");
  1727. }
  1728. };
  1729. Blockly.Python['CocoRobo_get'] = function (block) {
  1730. var op = block.getFieldValue('op');
  1731. var code = '_SEND_HTTP_REQUEST.text';
  1732. if (op == "text") {
  1733. code = '_SEND_HTTP_REQUEST.text';
  1734. } else if (op == "content") {
  1735. code = '_SEND_HTTP_REQUEST.content';
  1736. } else if (op == "state") {
  1737. code = '_SEND_HTTP_REQUEST.status_code';
  1738. } else if (op == "json") {
  1739. code = '_SEND_HTTP_REQUEST.json()';
  1740. } else if (op == "code") {
  1741. code = '_SEND_HTTP_REQUEST.encoding';
  1742. } else if (op == "reason") {
  1743. code = '_SEND_HTTP_REQUEST.reason';
  1744. }
  1745. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1746. };
  1747. Blockly.Blocks['iot_system_run'] = {
  1748. init: function init() {
  1749. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_RUN);
  1750. this.appendValueInput("PATH").setCheck(null).appendField(Blockly.Msg.system_code_path);
  1751. this.setInputsInline(false);
  1752. this.setPreviousStatement(true, null);
  1753. this.setNextStatement(true, null);
  1754. this.setColour(32);
  1755. this.setTooltip(Blockly.Msg.Iot_System_Run_TOOLTIP);
  1756. this.setHelpUrl("");
  1757. }
  1758. };
  1759. Blockly.Python.iot_system_run = function (block) {
  1760. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  1761. var _code = "exec(open(" + _path + ").read())\n";
  1762. return _code;
  1763. };
  1764. Blockly.Blocks['iot_system_create'] = {
  1765. init: function init() {
  1766. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_CREATE);
  1767. this.appendValueInput("PATH").setCheck(null).appendField(Blockly.Msg.SYSTEM_CREATE_F);
  1768. this.setInputsInline(false);
  1769. this.setPreviousStatement(true, null);
  1770. this.setNextStatement(true, null);
  1771. this.setColour(32);
  1772. this.setTooltip(Blockly.Msg.Iot_System_Create_TOOLTIP);
  1773. this.setHelpUrl("");
  1774. }
  1775. };
  1776. Blockly.Python.iot_system_create = function (block) {
  1777. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  1778. var _code = "def _CREATE_TEXT_FILE(_path):\n";
  1779. _code += " f = open(_path, 'w')\n";
  1780. _code += " f.close()\n";
  1781. Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
  1782. _code = "_CREATE_TEXT_FILE(" + _path + ")\n";
  1783. return _code;
  1784. };
  1785. Blockly.Blocks['iot_system_write'] = {
  1786. init: function init() {
  1787. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_WRITE);
  1788. this.appendValueInput("PATH").setCheck(null).appendField(Blockly.Msg.SYSTEM_WRITE_F);
  1789. this.appendValueInput("WPATH").setCheck(null).appendField(Blockly.Msg.SYSTEM_WRITE_W);
  1790. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_WRITE_S).appendField(new Blockly.FieldDropdown([[Blockly.Msg.SYSTEM_WRITE_S_N, "newline"], [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"], [Blockly.Msg.SYSTEM_WRITE_S_S, "space"], [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]]), "separator");
  1791. this.setInputsInline(false);
  1792. this.setPreviousStatement(true, null);
  1793. this.setNextStatement(true, null);
  1794. this.setColour(32);
  1795. this.setHelpUrl("");
  1796. var thisBlock = this;
  1797. this.setTooltip(function () {
  1798. var mode = thisBlock.getFieldValue('separator');
  1799. var TOOLTIPS = {
  1800. 'newline': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
  1801. 'comma': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
  1802. 'space': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
  1803. 'semicolon': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
  1804. };
  1805. return TOOLTIPS[mode];
  1806. });
  1807. }
  1808. };
  1809. Blockly.Python.iot_system_write = function (block) {
  1810. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  1811. var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
  1812. var _separator = block.getFieldValue('separator');
  1813. var _code = "def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):\n";
  1814. _code += " f = open(_path, 'a')\n";
  1815. _code += " f.write(_data + _sep)\n";
  1816. _code += " f.close()\n";
  1817. Blockly.Python.addFunction("_CREATE_TEXT_FILE_WITH_CONTENT", _code);
  1818. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')";
  1819. if (_separator == "newline") {
  1820. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')";
  1821. } else if (_separator == "comma") {
  1822. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ',')";
  1823. } else if (_separator == "space") {
  1824. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ' ')";
  1825. } else if (_separator == "semicolon") {
  1826. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ';')";
  1827. }
  1828. return _code;
  1829. };
  1830. Blockly.Blocks['iot_system_input'] = {
  1831. init: function init() {
  1832. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_INPUT);
  1833. this.appendValueInput("PATH").setCheck(null).appendField(Blockly.Msg.SYSTEM_WRITE_F);
  1834. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM_WRITE_S).appendField(new Blockly.FieldDropdown([[Blockly.Msg.SYSTEM_WRITE_S_N, "newline"], [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"], [Blockly.Msg.SYSTEM_WRITE_S_S, "space"], [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]]), "separator");
  1835. this.setInputsInline(false);
  1836. this.setPreviousStatement(true, null);
  1837. this.setNextStatement(true, null);
  1838. this.setColour(32);
  1839. // this.setTooltip("");
  1840. this.setHelpUrl("");
  1841. var thisBlock = this;
  1842. this.setTooltip(function () {
  1843. var mode = thisBlock.getFieldValue('separator');
  1844. var TOOLTIPS = {
  1845. 'newline': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
  1846. 'comma': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
  1847. 'space': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
  1848. 'semicolon': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
  1849. };
  1850. return TOOLTIPS[mode];
  1851. });
  1852. }
  1853. };
  1854. Blockly.Python.iot_system_input = function (block) {
  1855. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  1856. var _separator = block.getFieldValue('separator');
  1857. var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n";
  1858. _code += " f = open(_path, 'r')\n";
  1859. _code += " result = f.read().split(_sep)\n";
  1860. _code += " f.close()\n";
  1861. _code += " return result\n";
  1862. Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
  1863. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  1864. if (_separator == "newline") {
  1865. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  1866. } else if (_separator == "comma") {
  1867. _code = "_GET_LIST_FROM_FILE(" + _path + ", ',')";
  1868. } else if (_separator == "space") {
  1869. _code = "_GET_LIST_FROM_FILE(" + _path + ", ' ')";
  1870. } else if (_separator == "semicolon") {
  1871. _code = "_GET_LIST_FROM_FILE(" + _path + ", ';')";
  1872. }
  1873. return _code;
  1874. };
  1875. Blockly.Blocks['iot_system_restart'] = {
  1876. init: function init() {
  1877. this.appendDummyInput().appendField(Blockly.Msg.SYSTEM);
  1878. this.setPreviousStatement(true);
  1879. this.setNextStatement(true);
  1880. this.setColour("#22398e");
  1881. this.setTooltip(Blockly.Msg.Iot_System_Restart_TOOLTIP);
  1882. this.setHelpUrl("");
  1883. }
  1884. };
  1885. Blockly.Python.iot_system_restart = function (block) {
  1886. Blockly.Python.definitions_.import_machine = "import machine";
  1887. var _code = "machine.reset()\n";
  1888. return _code;
  1889. };
  1890. Blockly.Blocks['iot_system_get'] = {
  1891. init: function init() {
  1892. this.appendDummyInput().appendField(Blockly.Msg.system_get_size);
  1893. this.setOutput(true, null);
  1894. this.setColour("#22398e");
  1895. this.setTooltip(Blockly.Msg.Iot_System_Get_TOOLTIP);
  1896. this.setHelpUrl("");
  1897. }
  1898. };
  1899. Blockly.Python.iot_system_get = function (block) {
  1900. var code = "_MCU_FREESPACE";
  1901. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1902. };
  1903. Blockly.Blocks['iot_system_mac'] = {
  1904. init: function init() {
  1905. this.appendDummyInput().appendField(Blockly.Msg.system_get_mac);
  1906. this.setOutput(true, null);
  1907. this.setColour("#22398e");
  1908. this.setTooltip(Blockly.Msg.Iot_System_Mac_TOOLTIP);
  1909. this.setHelpUrl("");
  1910. }
  1911. };
  1912. Blockly.Python.iot_system_mac = function (block) {
  1913. Blockly.Python.definitions_.import_machine_ub = "import machine, ubinascii";
  1914. var _code = "ubinascii.hexlify(machine.unique_id()).decode().upper()";
  1915. return [_code, Blockly.Python.ORDER_CONDITIONAL];
  1916. };
  1917. Blockly.Blocks['iot_system_get_cocorobo_id'] = {
  1918. init: function init() {
  1919. this.appendDummyInput().appendField(Blockly.Msg.system_get_cocorobo_cid);
  1920. this.setInputsInline(false);
  1921. this.setOutput(true, null);
  1922. this.setColour("#22398e");
  1923. this.setTooltip(Blockly.Msg.Iot_System_Get_Cocorobo_Id_TOOLTIP);
  1924. this.setHelpUrl("");
  1925. }
  1926. };
  1927. Blockly.Python['iot_system_get_cocorobo_id'] = function (block) {
  1928. Blockly.Python.definitions_['import_cocorobo_cid'] = 'from cocorobo import cid\n'; // TODO: Assemble Python into code variable.
  1929. var code = 'cid._GET_MACHINE_ID().strip(\'\\n\')'; // TODO: Change ORDER_NONE to the correct strength.
  1930. return [code, Blockly.Python.ORDER_NONE];
  1931. };
  1932. /*
  1933. _____ _____ __ _
  1934. \_ \___/__ \ / _\ ___ _ ____ _(_) ___ ___
  1935. / /\/ _ \ / /\/ \ \ / _ \ '__\ \ / / |/ __/ _ \
  1936. /\/ /_| (_) / / _\ \ __/ | \ V /| | (_| __/
  1937. \____/ \___/\/ \__/\___|_| \_/ |_|\___\___|
  1938. */
  1939. var ESP32_IOT_COLOR = "#3c7683";
  1940. var IFTTT_BLOCK_COLOR = "#ff3b00";
  1941. var ONENET_BLOCK_COLOR = "#00b5ff";
  1942. Blockly.Blocks['esp32_main_controller_onenet_setup'] = {
  1943. init: function init() {
  1944. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 130, 70, {
  1945. alt: "*",
  1946. flipRtl: "FALSE"
  1947. }));
  1948. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_setup);
  1949. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_server).appendField(new Blockly.FieldTextInput("183.230.40.39"), "server_ip");
  1950. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_productid).appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_ID"), "product_id");
  1951. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_productkey).appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_KEY"), "product_api_key");
  1952. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_deviceid).appendField(new Blockly.FieldTextInput("ENTER_YOUR_DEVICE_ID"), "device_id");
  1953. this.setPreviousStatement(true, null);
  1954. this.setNextStatement(true, null);
  1955. this.setColour(ONENET_BLOCK_COLOR);
  1956. this.setTooltip("");
  1957. this.setHelpUrl("");
  1958. }
  1959. };
  1960. Blockly.Python['esp32_main_controller_onenet_setup'] = function (block) {
  1961. var text_server_ip = block.getFieldValue('server_ip');
  1962. var text_product_id = block.getFieldValue('product_id');
  1963. var text_product_api_key = block.getFieldValue('product_api_key');
  1964. var text_device_id = block.getFieldValue('device_id'); // TODO: Assemble Python into code variable.
  1965. var code = '...\n';
  1966. return code;
  1967. };
  1968. Blockly.Blocks['iot_service_onenet'] = {
  1969. init: function init() {
  1970. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 120, 70, {
  1971. alt: "*",
  1972. flipRtl: "FALSE"
  1973. }));
  1974. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_send);
  1975. /*this.appendValueInput("first_input")
  1976. .setCheck(null)
  1977. .appendField("Field 0");*/
  1978. this.itemCount_ = 1;
  1979. this.updateShape_();
  1980. this.setMutator(new Blockly.Mutator(['iot_service_onenet_create_with_item']));
  1981. this.setColour(ONENET_BLOCK_COLOR);
  1982. this.setPreviousStatement(true, null);
  1983. this.setNextStatement(true, null);
  1984. this.setTooltip("");
  1985. this.setHelpUrl("");
  1986. },
  1987. mutationToDom: function mutationToDom() {
  1988. var container = document.createElement('mutation');
  1989. container.setAttribute('items', this.itemCount_);
  1990. return container;
  1991. },
  1992. domToMutation: function domToMutation(xmlElement) {
  1993. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  1994. this.updateShape_();
  1995. },
  1996. decompose: function decompose(workspace) {
  1997. var containerBlock = workspace.newBlock('iot_service_onenet_create_with_container');
  1998. containerBlock.initSvg();
  1999. var connection = containerBlock.getInput('STACK').connection;
  2000. for (var i = 0; i < this.itemCount_; i++) {
  2001. var itemBlock = workspace.newBlock('iot_service_onenet_create_with_item');
  2002. itemBlock.initSvg();
  2003. connection.connect(itemBlock.previousConnection);
  2004. connection = itemBlock.nextConnection;
  2005. }
  2006. return containerBlock;
  2007. },
  2008. compose: function compose(containerBlock) {
  2009. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  2010. var connections = [];
  2011. while (itemBlock) {
  2012. connections.push(itemBlock.valueConnection_);
  2013. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2014. } // Disconnect any children that don't belong.
  2015. for (var i = 0; i < this.itemCount_; i++) {
  2016. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2017. if (connection && connections.indexOf(connection) == -1) {
  2018. connection.disconnect();
  2019. }
  2020. }
  2021. this.itemCount_ = connections.length;
  2022. this.updateShape_(); // Reconnect any child blocks.
  2023. for (var i = 0; i < this.itemCount_; i++) {
  2024. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2025. }
  2026. },
  2027. saveConnections: function saveConnections(containerBlock) {
  2028. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2029. var i = 0;
  2030. while (itemBlock) {
  2031. var input = this.getInput('ADD' + i);
  2032. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2033. i++;
  2034. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2035. }
  2036. },
  2037. updateShape_: function updateShape_() {
  2038. for (var i = 0; i < this.itemCount_; i++) {
  2039. if (!this.getInput('ADD' + i)) {
  2040. var input = this.appendValueInput('ADD' + i);
  2041. input.appendField(Blockly.Msg.iotservice_onenet_mqtt_send_item).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  2042. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2043. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2044. }
  2045. }
  2046. while (this.getInput('ADD' + i)) {
  2047. this.removeInput('ADD' + i);
  2048. i++;
  2049. }
  2050. }
  2051. };
  2052. Blockly.Blocks['iot_service_onenet_create_with_container'] = {
  2053. init: function init() {
  2054. this.setColour(ONENET_BLOCK_COLOR);
  2055. this.appendDummyInput().appendField("Items");
  2056. this.appendStatementInput('STACK');
  2057. this.setTooltip('');
  2058. this.contextMenu = false;
  2059. }
  2060. };
  2061. Blockly.Blocks['iot_service_onenet_create_with_item'] = {
  2062. init: function init() {
  2063. this.setColour(ONENET_BLOCK_COLOR);
  2064. this.appendDummyInput().appendField("Field");
  2065. this.setPreviousStatement(true);
  2066. this.setNextStatement(true);
  2067. this.setTooltip('');
  2068. this.contextMenu = false;
  2069. }
  2070. };
  2071. Blockly.Python['iot_service_onenet'] = function (block) {
  2072. var code = ""; // onenet_data_final.slice(0, -3) + '}\''
  2073. return code;
  2074. };
  2075. Blockly.Blocks['esp32_main_controller_onenet_when_receive_msg'] = {
  2076. init: function init() {
  2077. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_receive);
  2078. this.appendValueInput("content").setCheck(null);
  2079. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_when);
  2080. this.appendStatementInput("exec").setCheck(null).appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_exec);
  2081. this.setInputsInline(true);
  2082. this.setPreviousStatement(true, null);
  2083. this.setNextStatement(true, null);
  2084. this.setColour(ONENET_BLOCK_COLOR);
  2085. this.setTooltip("");
  2086. this.setHelpUrl("");
  2087. }
  2088. };
  2089. Blockly.Python['esp32_main_controller_onenet_when_receive_msg'] = function (block) {
  2090. var value_content = Blockly.Python.valueToCode(block, 'content', Blockly.Python.ORDER_ATOMIC);
  2091. var statements_exec = Blockly.Python.statementToCode(block, 'exec'); // TODO: Assemble Python into code variable.
  2092. var code = '...\n';
  2093. return code;
  2094. };
  2095. Blockly.Blocks['esp32_main_controller_onenet_disconnect'] = {
  2096. init: function init() {
  2097. this.appendDummyInput().appendField(Blockly.Msg.iotservice_onenet_mqtt_disconnect);
  2098. this.setInputsInline(true);
  2099. this.setPreviousStatement(true, null);
  2100. this.setNextStatement(true, null);
  2101. this.setColour(ONENET_BLOCK_COLOR);
  2102. this.setTooltip("");
  2103. this.setHelpUrl("");
  2104. }
  2105. };
  2106. Blockly.Python['esp32_main_controller_onenet_disconnect'] = function (block) {
  2107. // TODO: Assemble Python into code variable.
  2108. var code = '\n';
  2109. return code;
  2110. };
  2111. Blockly.Blocks['esp32_main_controller_ifttt_send_data'] = {
  2112. init: function init() {
  2113. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, {
  2114. alt: "*",
  2115. flipRtl: "FALSE"
  2116. }));
  2117. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_send_title);
  2118. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_send_webhook).appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "ifttt_api_key");
  2119. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_send_eventname).appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "ifttt_event");
  2120. this.appendValueInput("ifttt_data_1").setCheck(null).appendField(Blockly.Msg.iotservice_ifttt_send_1);
  2121. this.appendValueInput("ifttt_data_2").setCheck(null).appendField(Blockly.Msg.iotservice_ifttt_send_2);
  2122. this.appendValueInput("ifttt_data_3").setCheck(null).appendField(Blockly.Msg.iotservice_ifttt_send_3);
  2123. this.setPreviousStatement(true, null);
  2124. this.setNextStatement(true, null);
  2125. this.setColour(IFTTT_BLOCK_COLOR);
  2126. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Send_Data_TOOLTIP);
  2127. this.setHelpUrl("");
  2128. }
  2129. };
  2130. Blockly.Python['esp32_main_controller_ifttt_send_data'] = function (block) {
  2131. var text_ifttt_api_key = block.getFieldValue('ifttt_api_key');
  2132. var text_ifttt_event = block.getFieldValue('ifttt_event');
  2133. var value_ifttt_data_1 = Blockly.Python.valueToCode(block, 'ifttt_data_1', Blockly.Python.ORDER_ATOMIC);
  2134. var value_ifttt_data_2 = Blockly.Python.valueToCode(block, 'ifttt_data_2', Blockly.Python.ORDER_ATOMIC);
  2135. var value_ifttt_data_3 = Blockly.Python.valueToCode(block, 'ifttt_data_3', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2136. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2137. '';
  2138. var code = '' + '_IFTTT_POST_API_KEY = "' + text_ifttt_api_key + '"\n' + '_IFTTT_POST_EVENT_NAME = "' + text_ifttt_event + '"\n' + '_IFTTT_POST_ENDPOINT = "http://maker.ifttt.com/trigger/"+ _IFTTT_POST_EVENT_NAME + "/with/key/" + _IFTTT_POST_API_KEY\n' + '_IFTTT_POST_DATA = \'{"value1":"\'+ str(' + value_ifttt_data_1 + ') +\'","value2":"\'+ str(' + value_ifttt_data_2 + ') +\'","value3":"\'+ str(' + value_ifttt_data_3 + ') +\'"}\'\n' + '_IFTTT_POST_REQUEST = urequests.post(_IFTTT_POST_ENDPOINT, data = _IFTTT_POST_DATA , headers = { "Content-type": "application/json" }, timeout=60)\n' + '\n';
  2139. return code;
  2140. };
  2141. Blockly.Blocks['esp32_main_controller_ifttt_touched'] = {
  2142. init: function init() {
  2143. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, {
  2144. alt: "*",
  2145. flipRtl: "FALSE"
  2146. }));
  2147. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_trigger_title);
  2148. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_trigger_webhook).appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "key");
  2149. this.appendDummyInput().appendField(Blockly.Msg.iotservice_ifttt_trigger_eventname).appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "timename");
  2150. this.setPreviousStatement(true, null);
  2151. this.setNextStatement(true, null);
  2152. this.setColour(IFTTT_BLOCK_COLOR);
  2153. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Touched_TOOLTIP);
  2154. this.setHelpUrl("");
  2155. }
  2156. };
  2157. Blockly.Python['esp32_main_controller_ifttt_touched'] = function (block) {
  2158. var text_ifttt_api_key = block.getFieldValue('key');
  2159. var text_timename = block.getFieldValue('timename');
  2160. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2161. '';
  2162. var code = '_IFTTT_TRIGGER_EVENT_NAME = "' + text_timename + '"\n';
  2163. code += '_IFTTT_TRIGGER_API_KEY = "' + text_ifttt_api_key + '"\n';
  2164. code += '_IFTTT_TRIGGER_ENDPOINT = "https://maker.ifttt.com/trigger/" + _IFTTT_TRIGGER_EVENT_NAME + "/with/key/" + _IFTTT_TRIGGER_API_KEY\n';
  2165. Blockly.Python.addSetup("esp32_main_controller_ifttt_touched", code);
  2166. var code = '_IFTTT_GET_REQUEST = urequests.get(_IFTTT_TRIGGER_ENDPOINT, timeout=60)\n';
  2167. return code;
  2168. };
  2169. IOT_THINGSPEAK_COLOR = "#1b379f"; // IOT_THINGSPEAK_COLOR = ESP32_IOT_COLOR;
  2170. Blockly.Blocks['iot_service_thingspeak'] = {
  2171. init: function init() {
  2172. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, {
  2173. alt: "*",
  2174. flipRtl: "FALSE"
  2175. }));
  2176. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_send_title);
  2177. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_send_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_CHANNEL_WRITE_API_KEY"), "t_api");
  2178. /*this.appendValueInput("first_input")
  2179. .setCheck(null)
  2180. .appendField("Field 0");*/
  2181. this.itemCount_ = 1;
  2182. this.updateShape_();
  2183. this.setMutator(new Blockly.Mutator(['iot_service_thingspeak_create_with_item']));
  2184. this.setColour(IOT_THINGSPEAK_COLOR);
  2185. this.setPreviousStatement(true, null);
  2186. this.setNextStatement(true, null);
  2187. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_TOOLTIP);
  2188. this.setHelpUrl("");
  2189. },
  2190. mutationToDom: function mutationToDom() {
  2191. var container = document.createElement('mutation');
  2192. container.setAttribute('items', this.itemCount_);
  2193. return container;
  2194. },
  2195. domToMutation: function domToMutation(xmlElement) {
  2196. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2197. this.updateShape_();
  2198. },
  2199. decompose: function decompose(workspace) {
  2200. var containerBlock = workspace.newBlock('iot_service_thingspeak_create_with_container');
  2201. containerBlock.initSvg();
  2202. var connection = containerBlock.getInput('STACK').connection;
  2203. for (var i = 0; i < this.itemCount_; i++) {
  2204. var itemBlock = workspace.newBlock('iot_service_thingspeak_create_with_item');
  2205. itemBlock.initSvg();
  2206. connection.connect(itemBlock.previousConnection);
  2207. connection = itemBlock.nextConnection;
  2208. }
  2209. return containerBlock;
  2210. },
  2211. compose: function compose(containerBlock) {
  2212. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  2213. var connections = [];
  2214. while (itemBlock) {
  2215. connections.push(itemBlock.valueConnection_);
  2216. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2217. } // Disconnect any children that don't belong.
  2218. for (var i = 0; i < this.itemCount_; i++) {
  2219. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2220. if (connection && connections.indexOf(connection) == -1) {
  2221. connection.disconnect();
  2222. }
  2223. }
  2224. this.itemCount_ = connections.length;
  2225. this.updateShape_(); // Reconnect any child blocks.
  2226. for (var i = 0; i < this.itemCount_; i++) {
  2227. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2228. }
  2229. },
  2230. saveConnections: function saveConnections(containerBlock) {
  2231. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2232. var i = 0;
  2233. while (itemBlock) {
  2234. var input = this.getInput('ADD' + i);
  2235. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2236. i++;
  2237. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2238. }
  2239. },
  2240. updateShape_: function updateShape_() {
  2241. for (var i = 0; i < this.itemCount_; i++) {
  2242. if (!this.getInput('ADD' + i)) {
  2243. var input = this.appendValueInput('ADD' + i);
  2244. input.appendField("Field " + (i + 1) + ":"); //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2245. // input.appendField(new Blockly.FieldTextInput("field" + (i + 1)), 'FIELD' + i)
  2246. }
  2247. }
  2248. while (this.getInput('ADD' + i)) {
  2249. this.removeInput('ADD' + i);
  2250. i++;
  2251. }
  2252. }
  2253. };
  2254. Blockly.Blocks['iot_service_thingspeak_create_with_container'] = {
  2255. init: function init() {
  2256. this.setColour(IOT_THINGSPEAK_COLOR);
  2257. this.appendDummyInput().appendField("Items");
  2258. this.appendStatementInput('STACK');
  2259. this.setTooltip('');
  2260. this.contextMenu = false;
  2261. }
  2262. };
  2263. Blockly.Blocks['iot_service_thingspeak_create_with_item'] = {
  2264. init: function init() {
  2265. this.setColour(IOT_THINGSPEAK_COLOR);
  2266. this.appendDummyInput().appendField("Field");
  2267. this.setPreviousStatement(true);
  2268. this.setNextStatement(true);
  2269. this.setTooltip('');
  2270. this.contextMenu = false;
  2271. }
  2272. };
  2273. Blockly.Python['iot_service_thingspeak'] = function (block) {
  2274. var first_input = Blockly.Python.valueToCode(block, 'first_input', Blockly.Python.ORDER_ATOMIC);
  2275. var api = block.getFieldValue('t_api');
  2276. Blockly.Python.addVariable('_THINGSPEAK_REQUEST', '', true);
  2277. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2278. ''; // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2279. var item_field = '',
  2280. item_value = '';
  2281. var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + api;
  2282. for (var n = 0; n < this.itemCount_; n++) {
  2283. item_field = "field" + (n + 1);
  2284. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2285. thingspeak_url += '&' + item_field + '=\"+str(' + item_value + ')+\"';
  2286. }
  2287. var code = '' + '_THINGSPEAK_ENDPOINT = "' + thingspeak_url + '"\n' + '_THINGSPEAK_REQUEST = urequests.get(_THINGSPEAK_ENDPOINT,timeout=60)\n' + '';
  2288. return code;
  2289. };
  2290. Blockly.Blocks['iot_service_thingspeak_read'] = {
  2291. init: function init() {
  2292. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, {
  2293. alt: "*",
  2294. flipRtl: "FALSE"
  2295. }));
  2296. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_query_title);
  2297. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_query_api).appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_READ_API_KEY"), "api_key");
  2298. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_channel_id).appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_ID"), "id");
  2299. this.setInputsInline(false);
  2300. this.setPreviousStatement(true, null);
  2301. this.setNextStatement(true, null);
  2302. this.setColour(IOT_THINGSPEAK_COLOR);
  2303. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_TOOLTIP);
  2304. this.setHelpUrl("");
  2305. }
  2306. };
  2307. Blockly.Python['iot_service_thingspeak_read'] = function (block) {
  2308. var text_api_key = block.getFieldValue('api_key');
  2309. var text_id = block.getFieldValue('id');
  2310. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2311. '';
  2312. Blockly.Python.addVariable('_THINGSPEAK_READ_REQUEST', '', true); // TODO: Assemble Python into code variable.
  2313. var code = '_THINGSPEAK_READ_REQUEST = urequests.get("https://api.thingspeak.com/channels/" + "' + text_id + '" + "/feeds.json?api_key=" + "' + text_api_key + '",timeout=60)\n';
  2314. return code;
  2315. };
  2316. Blockly.Blocks['iot_service_thingspeak_read_total'] = {
  2317. init: function init() {
  2318. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_read_total);
  2319. this.setInputsInline(false);
  2320. this.setOutput(true, null);
  2321. this.setColour(IOT_THINGSPEAK_COLOR);
  2322. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Total_TOOLTIP);
  2323. this.setHelpUrl("");
  2324. }
  2325. };
  2326. Blockly.Python['iot_service_thingspeak_read_total'] = function (block) {
  2327. // TODO: Assemble Python into code variable.
  2328. var code = '_THINGSPEAK_READ_REQUEST.json()[\'channel\'][\'last_entry_id\']'; // TODO: Change ORDER_NONE to the correct strength.
  2329. return [code, Blockly.Python.ORDER_NONE];
  2330. };
  2331. Blockly.Blocks['iot_service_thingspeak_read_specific'] = {
  2332. init: function init() {
  2333. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeak_get_title);
  2334. this.appendValueInput("entry").setCheck(null);
  2335. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeaK_get_field);
  2336. this.appendValueInput("field").setCheck(null);
  2337. this.appendDummyInput().appendField(Blockly.Msg.iotservice_thingspeaK_get_value);
  2338. this.setInputsInline(true);
  2339. this.setOutput(true, null);
  2340. this.setColour(IOT_THINGSPEAK_COLOR);
  2341. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Specific_TOOLTIP);
  2342. this.setHelpUrl("");
  2343. }
  2344. };
  2345. Blockly.Python['iot_service_thingspeak_read_specific'] = function (block) {
  2346. var value_entry = Blockly.Python.valueToCode(block, 'entry', Blockly.Python.ORDER_ATOMIC);
  2347. var value_field = Blockly.Python.valueToCode(block, 'field', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2348. var code = '_THINGSPEAK_READ_REQUEST.json()[\'feeds\'][' + value_entry + '][\'field' + value_field + '\']'; // TODO: Change ORDER_NONE to the correct strength.
  2349. return [code, Blockly.Python.ORDER_NONE];
  2350. }; // cococloud
  2351. var COCOCLOUD_BLOCK_COLOR = "#4085de";
  2352. Blockly.Blocks['iot_service_cococloud'] = {
  2353. init: function init() {
  2354. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, {
  2355. alt: "*",
  2356. flipRtl: "FALSE"
  2357. }));
  2358. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_send_title);
  2359. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_send_title_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  2360. /*this.appendValueInput("first_input")
  2361. .setCheck(null)
  2362. .appendField("Field 0");*/
  2363. this.itemCount_ = 1;
  2364. this.updateShape_();
  2365. this.setMutator(new Blockly.Mutator(['iot_service_cococloud_create_with_item']));
  2366. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2367. this.setPreviousStatement(true, null);
  2368. this.setNextStatement(true, null);
  2369. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_TOOLTIP);
  2370. this.setHelpUrl("");
  2371. },
  2372. mutationToDom: function mutationToDom() {
  2373. var container = document.createElement('mutation');
  2374. container.setAttribute('items', this.itemCount_);
  2375. return container;
  2376. },
  2377. domToMutation: function domToMutation(xmlElement) {
  2378. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2379. this.updateShape_();
  2380. },
  2381. decompose: function decompose(workspace) {
  2382. var containerBlock = workspace.newBlock('iot_service_cococloud_create_with_container');
  2383. containerBlock.initSvg();
  2384. var connection = containerBlock.getInput('STACK').connection;
  2385. for (var i = 0; i < this.itemCount_; i++) {
  2386. var itemBlock = workspace.newBlock('iot_service_cococloud_create_with_item');
  2387. itemBlock.initSvg();
  2388. connection.connect(itemBlock.previousConnection);
  2389. connection = itemBlock.nextConnection;
  2390. }
  2391. return containerBlock;
  2392. },
  2393. compose: function compose(containerBlock) {
  2394. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  2395. var connections = [];
  2396. while (itemBlock) {
  2397. connections.push(itemBlock.valueConnection_);
  2398. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2399. } // Disconnect any children that don't belong.
  2400. for (var i = 0; i < this.itemCount_; i++) {
  2401. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2402. if (connection && connections.indexOf(connection) == -1) {
  2403. connection.disconnect();
  2404. }
  2405. }
  2406. this.itemCount_ = connections.length;
  2407. this.updateShape_(); // Reconnect any child blocks.
  2408. for (var i = 0; i < this.itemCount_; i++) {
  2409. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2410. }
  2411. },
  2412. saveConnections: function saveConnections(containerBlock) {
  2413. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2414. var i = 0;
  2415. while (itemBlock) {
  2416. var input = this.getInput('ADD' + i);
  2417. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2418. i++;
  2419. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2420. }
  2421. },
  2422. updateShape_: function updateShape_() {
  2423. for (var i = 0; i < this.itemCount_; i++) {
  2424. if (!this.getInput('ADD' + i)) {
  2425. var input = this.appendValueInput('ADD' + i);
  2426. input.appendField(Blockly.Msg.iotservice_cococloud_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  2427. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2428. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2429. }
  2430. }
  2431. while (this.getInput('ADD' + i)) {
  2432. this.removeInput('ADD' + i);
  2433. i++;
  2434. }
  2435. }
  2436. };
  2437. Blockly.Blocks['iot_service_cococloud_create_with_container'] = {
  2438. init: function init() {
  2439. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2440. this.appendDummyInput().appendField("Items");
  2441. this.appendStatementInput('STACK');
  2442. this.setTooltip('');
  2443. this.contextMenu = false;
  2444. }
  2445. };
  2446. Blockly.Blocks['iot_service_cococloud_create_with_item'] = {
  2447. init: function init() {
  2448. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2449. this.appendDummyInput().appendField("Field");
  2450. this.setPreviousStatement(true);
  2451. this.setNextStatement(true);
  2452. this.setTooltip('');
  2453. this.contextMenu = false;
  2454. }
  2455. };
  2456. Blockly.Python['iot_service_cococloud'] = function (block) {
  2457. var api = block.getFieldValue('t_api');
  2458. Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '', true);
  2459. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2460. ''; // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2461. var item_field = '',
  2462. item_value = '';
  2463. var cococloud_data = "'{";
  2464. for (var n = 0; n < this.itemCount_; n++) {
  2465. item_field = block.getFieldValue("field" + n);
  2466. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2467. cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2468. }
  2469. var cococloud_data_final = cococloud_data + "}'";
  2470. var code = '' + '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' + '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' + '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n';
  2471. ''; // cococloud_data_final.slice(0, -3) + '}\''
  2472. return code;
  2473. };
  2474. Blockly.Blocks['iot_service_cococloud_read'] = {
  2475. init: function init() {
  2476. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, {
  2477. alt: "*",
  2478. flipRtl: "FALSE"
  2479. }));
  2480. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_get_title);
  2481. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_get_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "api");
  2482. this.setInputsInline(false);
  2483. this.setPreviousStatement(true, null);
  2484. this.setNextStatement(true, null);
  2485. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2486. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_TOOLTIP);
  2487. this.setHelpUrl("");
  2488. }
  2489. };
  2490. Blockly.Python['iot_service_cococloud_read'] = function (block) {
  2491. var api = block.getFieldValue('api');
  2492. Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '', true);
  2493. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests\n' + 'import ujson as json\n';
  2494. ''; // TODO: Assemble Python into code variable.
  2495. var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
  2496. return code;
  2497. };
  2498. Blockly.Blocks['iot_service_cococloud_read_data'] = {
  2499. init: function init() {
  2500. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_get_property);
  2501. this.appendValueInput("property").setCheck(null);
  2502. this.appendDummyInput().appendField(Blockly.Msg.iotservice_cococloud_get_data);
  2503. this.setInputsInline(true);
  2504. this.setOutput(true, null);
  2505. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2506. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_Data_TOOLTIP);
  2507. this.setHelpUrl("");
  2508. }
  2509. };
  2510. Blockly.Python['iot_service_cococloud_read_data'] = function (block) {
  2511. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2512. var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']'; // TODO: Change ORDER_NONE to the correct strength.
  2513. return [code, Blockly.Python.ORDER_NONE];
  2514. };
  2515. /*
  2516. __ _ _ __ _
  2517. /\ \ \___| |___ _____ _ __| | __ /__\ ___ __ _ _ _ ___ ___| |_
  2518. / \/ / _ \ __\ \ /\ / / _ \| '__| |/ / / \/// _ \/ _` | | | |/ _ \/ __| __|
  2519. / /\ / __/ |_ \ V V / (_) | | | < / _ \ __/ (_| | |_| | __/\__ \ |_
  2520. \_\ \/ \___|\__| \_/\_/ \___/|_| |_|\_\ \/ \_/\___|\__, |\__,_|\___||___/\__|
  2521. |_|
  2522. */
  2523. var NETWORK_BLOCK_COLOR = "#16318a";
  2524. Blockly.Blocks['esp32_network_http_get'] = {
  2525. init: function init() {
  2526. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/http_header_get.png", 180, 50, {
  2527. alt: "*",
  2528. flipRtl: "FALSE"
  2529. }));
  2530. this.appendDummyInput().appendField(Blockly.Msg.internet_http_get_title);
  2531. this.appendValueInput("http_get_url").setCheck(null).appendField(Blockly.Msg.internet_http_get_url);
  2532. this.setInputsInline(false);
  2533. this.setPreviousStatement(true, null);
  2534. this.setNextStatement(true, null);
  2535. this.setColour(NETWORK_BLOCK_COLOR);
  2536. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Get_TOOLTIP);
  2537. this.setHelpUrl("");
  2538. }
  2539. };
  2540. Blockly.Python['esp32_network_http_get'] = function (block) {
  2541. var url = Blockly.Python.valueToCode(block, 'http_get_url', Blockly.Python.ORDER_ATOMIC);
  2542. Blockly.Python.addVariable('_SEND_HTTP_GET_ENDPOINT', '', true);
  2543. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  2544. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests as requests\n' + 'import ujson as json\n' + '\n' + '_SEND_HTTP_GET_ENDPOINT = ' + url + '\n' + ''; // TODO: Assemble Python into code variable.
  2545. var code = '_SEND_HTTP_REQUEST = requests.get(_SEND_HTTP_GET_ENDPOINT)\n';
  2546. return code;
  2547. };
  2548. Blockly.Blocks['esp32_network_http_post'] = {
  2549. init: function init() {
  2550. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/http_header_post.png", 180, 50, {
  2551. alt: "*",
  2552. flipRtl: "FALSE"
  2553. }));
  2554. this.appendDummyInput().appendField(Blockly.Msg.internet_http_post_title);
  2555. this.appendValueInput("http_post_url").setCheck(null).appendField(Blockly.Msg.internet_http_post_url);
  2556. this.appendValueInput("data").setCheck(null).appendField(Blockly.Msg.internet_http_post_json);
  2557. this.setInputsInline(false);
  2558. this.setPreviousStatement(true, null);
  2559. this.setNextStatement(true, null);
  2560. this.setColour(NETWORK_BLOCK_COLOR);
  2561. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Post_TOOLTIP);
  2562. this.setHelpUrl("");
  2563. }
  2564. };
  2565. Blockly.Python['esp32_network_http_post'] = function (block) {
  2566. var url = Blockly.Python.valueToCode(block, 'http_post_url', Blockly.Python.ORDER_ATOMIC);
  2567. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  2568. Blockly.Python.addVariable('_SEND_HTTP_POST_ENDPOINT', '', true);
  2569. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  2570. Blockly.Python.definitions_['import_urequests'] = '' + 'import urequests as requests\n' + 'import ujson as json\n' + '\n' + '_SEND_HTTP_POST_ENDPOINT = ' + url + '\n' + ''; // TODO: Assemble Python into code variable.
  2571. var code = '' + '_SEND_HTTP_POST_DATA = \'' + data + '\'\n' + '_SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, data = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })\n' + '';
  2572. return code;
  2573. };
  2574. Blockly.Blocks['esp32_network_http_get_data_from_local_server'] = {
  2575. init: function init() {
  2576. this.appendDummyInput().appendField(Blockly.Msg.internet_http_get_server_data_title);
  2577. this.appendDummyInput().appendField(Blockly.Msg.internet_http_get_server_data_type).appendField(new Blockly.FieldDropdown([[Blockly.Msg.internet_http_get_server_data_json, "json"], [Blockly.Msg.internet_http_get_server_data_text, "text"]]), "type");
  2578. this.setInputsInline(false);
  2579. this.setOutput(true, null);
  2580. this.setColour(NETWORK_BLOCK_COLOR);
  2581. var thisBlock = this;
  2582. this.setTooltip(function () {
  2583. var mode = thisBlock.getFieldValue('type');
  2584. var TOOLTIPS = {
  2585. 'json': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_json),
  2586. 'text': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_text)
  2587. };
  2588. return TOOLTIPS[mode];
  2589. });
  2590. this.setHelpUrl("");
  2591. this.setHelpUrl("");
  2592. }
  2593. };
  2594. Blockly.Python['esp32_network_http_get_data_from_local_server'] = function (block) {
  2595. var dropdown_type = block.getFieldValue('type'); // TODO: Assemble Python into code variable.
  2596. if (dropdown_type == "text") {
  2597. var request_code = "_SEND_HTTP_REQUEST.text";
  2598. } else if (dropdown_type == "json") {
  2599. var request_code = "eval(_SEND_HTTP_REQUEST.text)";
  2600. }
  2601. var code = request_code; // TODO: Change ORDER_NONE to the correct strength.
  2602. return [code, Blockly.Python.ORDER_NONE];
  2603. };
  2604. Blockly.Blocks['esp32_network_http_server_setup'] = {
  2605. init: function init() {
  2606. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 40, {
  2607. alt: "*",
  2608. flipRtl: "FALSE"
  2609. }));
  2610. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_setup);
  2611. this.setInputsInline(false);
  2612. this.setPreviousStatement(true, null);
  2613. this.setNextStatement(true, null);
  2614. this.setColour(NETWORK_BLOCK_COLOR);
  2615. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Server_Setup_TOOLTIP);
  2616. this.setHelpUrl("");
  2617. }
  2618. };
  2619. Blockly.Python['esp32_network_http_server_setup'] = function (block) {
  2620. // TODO: Assemble Python into code variable.
  2621. Blockly.Python.definitions_['esp32_network_http_server_setup_def_setup'] = '' + 'from microWebSrv import MicroWebSrv\n' + 'import time\n' + '';
  2622. var code = '';
  2623. return code;
  2624. };
  2625. Blockly.Blocks['esp32_network_http_server_route'] = {
  2626. init: function init() {
  2627. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_route_setting_title);
  2628. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_route_setting_path).appendField(new Blockly.FieldTextInput("/"), "path").appendField(Blockly.Msg.wifi_web_http_server_route_setting_being_requested);
  2629. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_route_setting_property).appendField(new Blockly.FieldDropdown([[Blockly.Msg.wifi_web_http_server_route_setting_get, "get"], [Blockly.Msg.wifi_web_http_server_route_setting_post, "post"]]), "response_type");
  2630. this.appendStatementInput("exec").setCheck(null).appendField(Blockly.Msg.wifi_web_http_server_route_setting_do);
  2631. this.setInputsInline(false);
  2632. this.setPreviousStatement(true, null);
  2633. this.setNextStatement(true, null);
  2634. this.setColour(NETWORK_BLOCK_COLOR);
  2635. var thisBlock = this;
  2636. this.setTooltip(function () {
  2637. var mode = thisBlock.getFieldValue('response_type');
  2638. var TOOLTIPS = {
  2639. 'get': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_get),
  2640. 'post': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_post)
  2641. };
  2642. return TOOLTIPS[mode];
  2643. });
  2644. this.setHelpUrl("");
  2645. }
  2646. };
  2647. Blockly.Python['esp32_network_http_server_route'] = function (block) {
  2648. var dropdown_response_type = block.getFieldValue('response_type');
  2649. var text_path = block.getFieldValue('path');
  2650. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  2651. var response_type = "";
  2652. var response_writeok = "";
  2653. if (dropdown_response_type == "get") {
  2654. response_type = "";
  2655. } else if (dropdown_response_type == "post") {
  2656. response_type = ", 'POST'";
  2657. }
  2658. /*Blockly.Python.addVariable('', '' +
  2659. '' +
  2660. '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
  2661. 'def _httpHandlerTestGet(httpClient, httpResponse):\n' +
  2662. statements_exec +
  2663. '', true);*/
  2664. Blockly.Python.definitions_['esp32_network_http_server_route_def_' + text_path.substring(1)] = '' + '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' + 'def _httpHandler' + text_path.substring(1) + 'Get(httpClient, httpResponse):\n' + statements_exec + '\n';
  2665. Blockly.Python.addSetup("server_route_setup", '' + 'def _acceptWebSocketCallback(webSocket, httpClient) :\n' + ' print("WS ACCEPT")\n' + ' webSocket.RecvTextCallback = _recvTextCallback\n' + ' webSocket.RecvBinaryCallback = _recvBinaryCallback\n' + ' webSocket.ClosedCallback = _closedCallback\n' + '\n' + 'def _recvTextCallback(webSocket, msg) :\n' + ' print("WS RECV TEXT : %s" % msg)\n' + ' webSocket.SendText("Reply for %s" % msg)\n' + '\n' + 'def _recvBinaryCallback(webSocket, data) :\n' + ' print("WS RECV DATA : %s" % data)\n' + '\n' + 'def _closedCallback(webSocket) :\n' + ' print("WS CLOSED")\n' + '\n' + 'try:\n' + ' srv.Stop()\n' + ' srv = MicroWebSrv()\n' + ' srv.MaxWebSocketRecvLen = 1024\n' + ' srv.WebSocketThreaded = False\n' + ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' + ' srv.Start(threaded=True)\n' + 'except BaseException as e:\n' + ' print(str(e))\n' + ' srv = MicroWebSrv()\n' + ' srv.MaxWebSocketRecvLen = 1024\n' + ' srv.WebSocketThreaded = False\n' + ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' + ' srv.Start(threaded=True)\n' + ''); // TODO: Assemble Python into code variable.
  2666. var code = '';
  2667. return code;
  2668. };
  2669. Blockly.Blocks['esp32_network_http_server_route_respond'] = {
  2670. init: function init() {
  2671. this.appendValueInput("exec").setCheck(null).appendField(Blockly.Msg.wifi_web_http_server_respond_title);
  2672. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_respond_type).appendField(new Blockly.FieldDropdown([[Blockly.Msg.wifi_web_http_server_respond_text, "text/plain"], [Blockly.Msg.wifi_web_http_server_respond_json, "application/json"], [Blockly.Msg.wifi_web_http_server_respond_html, "text/html"]]), "response_type");
  2673. this.setInputsInline(false);
  2674. this.setPreviousStatement(true, null);
  2675. this.setNextStatement(true, null);
  2676. this.setColour(NETWORK_BLOCK_COLOR);
  2677. var thisBlock = this;
  2678. this.setTooltip(function () {
  2679. var mode = thisBlock.getFieldValue('response_type');
  2680. var TOOLTIPS = {
  2681. 'text/plain': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_text),
  2682. 'application/json': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_json),
  2683. 'text/html': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_html)
  2684. };
  2685. return TOOLTIPS[mode];
  2686. });
  2687. this.setHelpUrl("");
  2688. }
  2689. };
  2690. Blockly.Python['esp32_network_http_server_route_respond'] = function (block) {
  2691. var dropdown_response_type = block.getFieldValue('response_type');
  2692. var statements_exec = Blockly.Python.valueToCode(block, 'exec', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2693. var code = '' + 'content = ' + statements_exec + '\n' + 'httpResponse.WriteResponseOk( headers = None,\n' + ' contentType = "' + dropdown_response_type + '",\n' + ' contentCharset = "UTF-8",\n' + ' content = str(content) )\n' + '';
  2694. return code;
  2695. };
  2696. Blockly.Blocks['esp32_network_http_server_get_data'] = {
  2697. init: function init() {
  2698. this.appendDummyInput().appendField(Blockly.Msg.wifi_web_http_server_get_title).appendField(new Blockly.FieldDropdown([[Blockly.Msg.wifi_web_http_server_get_as_json, "json"], [Blockly.Msg.wifi_web_http_server_get_as_text, "text"]]), "type");
  2699. this.setInputsInline(false);
  2700. this.setOutput(true, null);
  2701. this.setColour(NETWORK_BLOCK_COLOR);
  2702. var thisBlock = this;
  2703. this.setTooltip(function () {
  2704. var mode = thisBlock.getFieldValue('type');
  2705. var TOOLTIPS = {
  2706. 'json': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_json),
  2707. 'text': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_text)
  2708. };
  2709. return TOOLTIPS[mode];
  2710. });
  2711. this.setHelpUrl("");
  2712. }
  2713. };
  2714. Blockly.Python['esp32_network_http_server_get_data'] = function (block) {
  2715. var dropdown_type = block.getFieldValue('type');
  2716. if (dropdown_type == "json") {
  2717. var as_type = "ReadRequestContentAsJSON()";
  2718. } else if (dropdown_type == "text") {
  2719. var as_type = "ReadRequestContent(size=None)";
  2720. } // TODO: Assemble Python into code variable.
  2721. var code = 'httpClient.' + as_type; // TODO: Change ORDER_NONE to the correct strength.
  2722. return [code, Blockly.Python.ORDER_NONE];
  2723. };
  2724. var WS_BLOCK_COLOR = "#83178a";
  2725. Blockly.Blocks['esp32_web_ws_setup_send'] = {
  2726. init: function init() {
  2727. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/ws_header_send.png", 220, 50, {
  2728. alt: "*",
  2729. flipRtl: "FALSE"
  2730. }));
  2731. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_send_text);
  2732. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_send_ip).appendField(new Blockly.FieldTextInput("192.168.4.1"), "ip");
  2733. this.setInputsInline(false);
  2734. this.setPreviousStatement(true, null);
  2735. this.setNextStatement(true, null);
  2736. this.setColour(WS_BLOCK_COLOR);
  2737. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Send_TOOLTIP);
  2738. this.setHelpUrl("");
  2739. }
  2740. };
  2741. Blockly.Python['esp32_web_ws_setup_send'] = function (block) {
  2742. var text_ip = block.getFieldValue('ip');
  2743. Blockly.Python.definitions_['ws_setup_send'] = '' + 'import uwebsockets.client\n' + '\n'; // TODO: Assemble Python into code variable.
  2744. var code = '_client_websocket = uwebsockets.client.connect("ws://' + text_ip + ':80")\n';
  2745. return code;
  2746. };
  2747. Blockly.Blocks['esp32_web_ws_send'] = {
  2748. init: function init() {
  2749. this.appendValueInput("command").setCheck(null).appendField(Blockly.Msg.internet_ws_send_msg);
  2750. this.setInputsInline(true);
  2751. this.setPreviousStatement(true, null);
  2752. this.setNextStatement(true, null);
  2753. this.setColour(WS_BLOCK_COLOR);
  2754. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Send_TOOLTIP);
  2755. this.setHelpUrl("");
  2756. }
  2757. };
  2758. Blockly.Python['esp32_web_ws_send'] = function (block) {
  2759. var value_command = Blockly.Python.valueToCode(block, 'command', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2760. var code = '' + 'message = ' + value_command + '\n' + '_client_websocket.send(message)\n';
  2761. return code;
  2762. };
  2763. Blockly.Blocks['esp32_web_ws_setup_receive'] = {
  2764. init: function init() {
  2765. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/ws_header_receive.png", 220, 50, {
  2766. alt: "*",
  2767. flipRtl: "FALSE"
  2768. }));
  2769. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_recv);
  2770. this.setInputsInline(false);
  2771. this.setPreviousStatement(true, null);
  2772. this.setNextStatement(true, null);
  2773. this.setColour(WS_BLOCK_COLOR);
  2774. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Receive_TOOLTIP);
  2775. this.setHelpUrl("");
  2776. }
  2777. };
  2778. Blockly.Python['esp32_web_ws_setup_receive'] = function (block) {
  2779. Blockly.Python.definitions_['ws_setup_receive'] = '' + 'import uasyncio, logging\n' + 'from uasyncio.websocket.server import WSReader, WSWriter\n' + 'logging.basicConfig(level=logging.DEBUG)\n' + '\n'; // TODO: Assemble Python into code variable.
  2780. var code = '';
  2781. return code;
  2782. };
  2783. Blockly.Blocks['esp32_web_ws_receive_statement'] = {
  2784. init: function init() {
  2785. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_receiving);
  2786. this.appendStatementInput("command").setCheck(null).appendField(Blockly.Msg.internet_ws_setup_recv_exec);
  2787. this.setInputsInline(false);
  2788. this.setPreviousStatement(true, null);
  2789. this.setNextStatement(true, null);
  2790. this.setColour(WS_BLOCK_COLOR);
  2791. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Receive_Statement_TOOLTIP);
  2792. this.setHelpUrl("");
  2793. }
  2794. };
  2795. Blockly.Python['esp32_web_ws_receive_statement'] = function (block) {
  2796. var statements_command = Blockly.Python.statementToCode(block, 'command');
  2797. Blockly.Python.definitions_['ws_setup_receive_statement'] = '' + 'def _ws_exec(msg):\n' + '' + statements_command + '\n' + 'def _ws_echo(reader, writer):\n' + ' yield from reader.readline()\n' + '\n' + ' reader = yield from WSReader(reader, writer)\n' + ' writer = WSWriter(reader, writer)\n' + '\n' + ' while 1:\n' + ' _ws_l = yield from reader.read(1024)\n' + ' print(_ws_l)\n' + ' _ws_exec(_ws_l)\n' + '\n' + ' if _ws_l == b"\\r":\n' + ' await writer.awrite(b"\\r\\n")\n' + ' else:\n' + ' await writer.awrite(_ws_l)\n' + '\n' + '_ws_loop = uasyncio.get_event_loop()\n' + '_ws_loop.create_task(uasyncio.start_server(_ws_echo, "0.0.0.0", 80))\n' + '_ws_loop.run_forever()\n' + '_ws_loop.close()\n' + ''; // TODO: Assemble Python into code variable.
  2798. var code = '\n';
  2799. return code;
  2800. };
  2801. Blockly.Blocks['esp32_web_ws_receive_get_data'] = {
  2802. init: function init() {
  2803. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_get_msg);
  2804. this.appendDummyInput().appendField(Blockly.Msg.internet_ws_setup_get_msg_type).appendField(new Blockly.FieldDropdown([[Blockly.Msg.internet_ws_setup_get_msg_text, "text"], [Blockly.Msg.internet_ws_setup_get_msg_json, "json"]]), "type");
  2805. this.setInputsInline(false);
  2806. this.setOutput(true, null);
  2807. this.setColour(WS_BLOCK_COLOR);
  2808. var thisBlock = this;
  2809. this.setTooltip(function () {
  2810. var mode = thisBlock.getFieldValue('type');
  2811. var TOOLTIPS = {
  2812. 'text': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_text),
  2813. 'json': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_json)
  2814. };
  2815. return TOOLTIPS[mode];
  2816. });
  2817. this.setHelpUrl("");
  2818. }
  2819. };
  2820. Blockly.Python['esp32_web_ws_receive_get_data'] = function (block) {
  2821. var dropdown_type = block.getFieldValue('type'); // TODO: Assemble Python into code variable.
  2822. if (dropdown_type == "text") {
  2823. var code = "msg.decode(\"utf-8\")";
  2824. } else if (dropdown_type == "json") {
  2825. var code = "eval(msg.decode(\"utf-8\"))";
  2826. } // TODO: Change ORDER_NONE to the correct strength.
  2827. return [code, Blockly.Python.ORDER_NONE];
  2828. };
  2829. /*
  2830. __ ___ _____ _____
  2831. /\ \ \/ __\ \_ \___/__ \
  2832. / \/ /__\//_____ / /\/ _ \ / /\/
  2833. / /\ / \/ \_____/\/ /_| (_) / /
  2834. \_\ \/\_____/ \____/ \___/\/
  2835. Blockly.Python.addVariable('', '', true);
  2836. Blockly.Python.definitions_['definition'] = '';
  2837. Blockly.Python.addSetup("setup", '');
  2838. */
  2839. var ESP32_NBIOT_COLOR = "#629978";
  2840. Blockly.Blocks['esp32_nbiot_setup'] = {
  2841. init: function init() {
  2842. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, {
  2843. alt: "*",
  2844. flipRtl: "FALSE"
  2845. }));
  2846. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_init);
  2847. this.setPreviousStatement(true, null);
  2848. this.setNextStatement(true, null);
  2849. this.setColour(ESP32_NBIOT_COLOR);
  2850. this.setTooltip("");
  2851. this.setHelpUrl("");
  2852. }
  2853. };
  2854. Blockly.Python['esp32_nbiot_setup'] = function (block) {
  2855. // TODO: Assemble Python into code variable.
  2856. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2857. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2858. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2859. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2860. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2861. Blockly.Python.definitions_['import_definition'] = '' + 'from machine import UART\n' + 'from machine import Timer\n' + 'import ubinascii as binascii\n' + 'import time\n' + '\n' + '_nbiot_serial = UART(2,rx=16, tx=17, baudrate=115200, timeout=10)\n' + '\n' + '_nbiot_serial_timer = Timer(1)\n' + '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' + '\n' + 'def _read_nbiot_serial(uart):\n' + ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' + ' if _nbiot_serial.any():\n' + ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' + ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' + ' _nbiot_readings = _nbiot_serial_read.strip()\n' + '\n' + ' if _nbiot_serial_read == "+CPIN: READY":\n' + ' _nbiot_power_on_status = True\n' + ' print("NB-IoT Module Initiated!\\n")\n' + ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' + '\n' + ' if "+IP" in _nbiot_serial_read:\n' + ' _nbiot_internet_status = True\n' + ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' + '';
  2862. var code = '';
  2863. return code;
  2864. };
  2865. Blockly.Blocks['esp32_nbiot_get_power_status'] = {
  2866. init: function init() {
  2867. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_power);
  2868. this.setOutput(true, null);
  2869. this.setColour(ESP32_NBIOT_COLOR);
  2870. this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
  2871. this.setHelpUrl("");
  2872. }
  2873. };
  2874. Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
  2875. // TODO: Assemble Python into code variable.
  2876. var code = '_nbiot_power_on_status'; // TODO: Change ORDER_NONE to the correct strength.
  2877. return [code, Blockly.Python.ORDER_NONE];
  2878. };
  2879. Blockly.Blocks['esp32_nbiot_get_network_status'] = {
  2880. init: function init() {
  2881. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_connectivity);
  2882. this.setOutput(true, null);
  2883. this.setColour(ESP32_NBIOT_COLOR);
  2884. this.setTooltip("");
  2885. this.setHelpUrl("");
  2886. }
  2887. };
  2888. Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
  2889. // TODO: Assemble Python into code variable.
  2890. var code = '_nbiot_internet_status'; // TODO: Change ORDER_NONE to the correct strength.
  2891. return [code, Blockly.Python.ORDER_NONE];
  2892. };
  2893. Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
  2894. init: function init() {
  2895. this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_ip);
  2896. this.setOutput(true, null);
  2897. this.setColour(ESP32_NBIOT_COLOR);
  2898. this.setTooltip("");
  2899. this.setHelpUrl("");
  2900. }
  2901. };
  2902. Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
  2903. // TODO: Assemble Python into code variable.
  2904. var code = '_nbiot_internet_ip_address'; // TODO: Change ORDER_NONE to the correct strength.
  2905. return [code, Blockly.Python.ORDER_NONE];
  2906. };
  2907. Blockly.Blocks['esp32_nbiot_onenet_init'] = {
  2908. init: function init() {
  2909. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, {
  2910. alt: "*",
  2911. flipRtl: "FALSE"
  2912. }));
  2913. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_title);
  2914. this.appendValueInput("product_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_productid);
  2915. this.appendValueInput("device_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
  2916. this.appendValueInput("device_apikey").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
  2917. this.setPreviousStatement(true, null);
  2918. this.setNextStatement(true, null);
  2919. this.setColour(ONENET_BLOCK_COLOR);
  2920. this.setTooltip("");
  2921. this.setHelpUrl("");
  2922. }
  2923. };
  2924. Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
  2925. var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
  2926. var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
  2927. var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  2928. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
  2929. Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
  2930. Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
  2931. Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
  2932. Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
  2933. Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
  2934. Blockly.Python.addSetup("setup", '' + '_nbiot_onenet_init_state = False\n' + '_nbiot_onenet_connection_status = False\n' + '_nbiot_onenet_read_anything = False\n' + '_nbiot_onenet_product_id = ' + value_product_id + '\n' + '_nbiot_onenet_device_id = ' + value_device_id + '\n' + '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' + '\n' + 'def _nbiot_send_onenet_data(data):\n' + ' _nbiot_send_message = data\n' + ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' + ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' + ' _nbiot_send_at_content = "AT+MQTTPUB=$dp,0,1,0," + str(len(_nbiot_send_message)+len(_nbiot_send_header[1:7].replace("0",""))) + ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1]) + str(_nbiot_send_data_content)\n' + ' return _nbiot_send_at_content\n' + '\n' + 'def _nbiot_read_onenet_data(raw_response):\n' + ' _nbiot_receive = raw_response\n' + ' if "+MQTTPUBLISH" not in raw_response:\n' + ' return ""\n' + ' elif "+MQTTPUBLISH" in raw_response:\n' + ' _nbiot_occur = -1\n' + ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' + ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' + '\n' + 'def _read_nbiot_onenet(_incoming_data):\n' + ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' + '\n' + ' if "+MQTTOPEN: OK" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = True\n' + ' # print("OneNET Connected!")\n' + ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = False\n' + ' # print("OneNET Lost Connection!")\n' + '\n' + ' if "+MQTTPUBLISH" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = True\n' + ' else:\n' + ' _nbiot_onenet_read_anything = False\n' + '\n' + ' if "+MQTTDISC" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = False\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + '\n' + 'def _init_onenet_connection():\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + ' time.sleep_ms(100)\n' + '');
  2935. var code = '' + 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' + ' _init_onenet_connection()\n' + ' _nbiot_onenet_init_state = True\n' + '\n' + '_read_nbiot_onenet(_nbiot_readings)\n' + '';
  2936. return code;
  2937. };
  2938. Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
  2939. init: function init() {
  2940. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_connectivity);
  2941. this.setOutput(true, null);
  2942. this.setColour(ONENET_BLOCK_COLOR);
  2943. this.setTooltip("");
  2944. this.setHelpUrl("");
  2945. }
  2946. };
  2947. Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
  2948. // TODO: Assemble Python into code variable.
  2949. var code = '_nbiot_onenet_connection_status'; // TODO: Change ORDER_NONE to the correct strength.
  2950. return [code, Blockly.Python.ORDER_NONE];
  2951. };
  2952. Blockly.Blocks['iot_service_nbiot_onenet'] = {
  2953. init: function init() {
  2954. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, {
  2955. alt: "*",
  2956. flipRtl: "FALSE"
  2957. }));
  2958. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_send_title);
  2959. /*this.appendValueInput("first_input")
  2960. .setCheck(null)
  2961. .appendField("Field 0");*/
  2962. this.itemCount_ = 1;
  2963. this.updateShape_();
  2964. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
  2965. this.setColour(ONENET_BLOCK_COLOR);
  2966. this.setPreviousStatement(true, null);
  2967. this.setNextStatement(true, null);
  2968. this.setTooltip("");
  2969. this.setHelpUrl("");
  2970. },
  2971. mutationToDom: function mutationToDom() {
  2972. var container = document.createElement('mutation');
  2973. container.setAttribute('items', this.itemCount_);
  2974. return container;
  2975. },
  2976. domToMutation: function domToMutation(xmlElement) {
  2977. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2978. this.updateShape_();
  2979. },
  2980. decompose: function decompose(workspace) {
  2981. var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
  2982. containerBlock.initSvg();
  2983. var connection = containerBlock.getInput('STACK').connection;
  2984. for (var i = 0; i < this.itemCount_; i++) {
  2985. var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
  2986. itemBlock.initSvg();
  2987. connection.connect(itemBlock.previousConnection);
  2988. connection = itemBlock.nextConnection;
  2989. }
  2990. return containerBlock;
  2991. },
  2992. compose: function compose(containerBlock) {
  2993. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  2994. var connections = [];
  2995. while (itemBlock) {
  2996. connections.push(itemBlock.valueConnection_);
  2997. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  2998. } // Disconnect any children that don't belong.
  2999. for (var i = 0; i < this.itemCount_; i++) {
  3000. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3001. if (connection && connections.indexOf(connection) == -1) {
  3002. connection.disconnect();
  3003. }
  3004. }
  3005. this.itemCount_ = connections.length;
  3006. this.updateShape_(); // Reconnect any child blocks.
  3007. for (var i = 0; i < this.itemCount_; i++) {
  3008. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3009. }
  3010. },
  3011. saveConnections: function saveConnections(containerBlock) {
  3012. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3013. var i = 0;
  3014. while (itemBlock) {
  3015. var input = this.getInput('ADD' + i);
  3016. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3017. i++;
  3018. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  3019. }
  3020. },
  3021. updateShape_: function updateShape_() {
  3022. for (var i = 0; i < this.itemCount_; i++) {
  3023. if (!this.getInput('ADD' + i)) {
  3024. var input = this.appendValueInput('ADD' + i);
  3025. input.appendField(Blockly.Msg.nbiot_onenet_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  3026. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3027. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  3028. }
  3029. }
  3030. while (this.getInput('ADD' + i)) {
  3031. this.removeInput('ADD' + i);
  3032. i++;
  3033. }
  3034. }
  3035. };
  3036. Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
  3037. init: function init() {
  3038. this.setColour(ONENET_BLOCK_COLOR);
  3039. this.appendDummyInput().appendField("Items");
  3040. this.appendStatementInput('STACK');
  3041. this.setTooltip('');
  3042. this.contextMenu = false;
  3043. }
  3044. };
  3045. Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
  3046. init: function init() {
  3047. this.setColour(ONENET_BLOCK_COLOR);
  3048. this.appendDummyInput().appendField("Field");
  3049. this.setPreviousStatement(true);
  3050. this.setNextStatement(true);
  3051. this.setTooltip('');
  3052. this.contextMenu = false;
  3053. }
  3054. };
  3055. Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
  3056. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  3057. var item_field = '',
  3058. item_value = '';
  3059. var nbiot_onenet_data = "'{";
  3060. for (var n = 0; n < this.itemCount_; n++) {
  3061. item_field = block.getFieldValue("field" + n);
  3062. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  3063. nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  3064. }
  3065. var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
  3066. var code = '' + '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' + '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' + '_data_for_at = _data_add_rn.encode()\n' + '_nbiot_serial.write(_data_for_at)\n' + // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
  3067. '\n' + '';
  3068. return code;
  3069. };
  3070. Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
  3071. init: function init() {
  3072. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
  3073. this.appendValueInput("message").setCheck(null);
  3074. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
  3075. this.appendStatementInput("exec").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
  3076. this.setPreviousStatement(true, null);
  3077. this.setNextStatement(true, null);
  3078. this.setColour(ONENET_BLOCK_COLOR);
  3079. this.setTooltip("");
  3080. this.setHelpUrl("");
  3081. }
  3082. };
  3083. Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
  3084. var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
  3085. var statements_exec = Blockly.Python.statementToCode(block, 'exec'); // TODO: Assemble Python into code variable.
  3086. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' + statements_exec + '\n';
  3087. return code;
  3088. };
  3089. Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
  3090. init: function init() {
  3091. this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_receive_command);
  3092. this.setOutput(true, null);
  3093. this.setColour(ONENET_BLOCK_COLOR);
  3094. this.setTooltip("");
  3095. this.setHelpUrl("");
  3096. }
  3097. };
  3098. Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
  3099. // TODO: Assemble Python into code variable.
  3100. var code = '_nbiot_read_onenet_data(_nbiot_readings)'; // TODO: Change ORDER_NONE to the correct strength.
  3101. return [code, Blockly.Python.ORDER_NONE];
  3102. };
  3103. Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
  3104. init: function init() {
  3105. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, {
  3106. alt: "*",
  3107. flipRtl: "FALSE"
  3108. }));
  3109. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
  3110. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_api).appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  3111. this.setPreviousStatement(true, null);
  3112. this.setNextStatement(true, null);
  3113. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3114. this.setTooltip("");
  3115. this.setHelpUrl("");
  3116. }
  3117. };
  3118. Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
  3119. var api = block.getFieldValue('t_api'); // TODO: Assemble Python into code variable.
  3120. var code = '\n';
  3121. return code;
  3122. };
  3123. Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
  3124. init: function init() {
  3125. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_property);
  3126. this.appendValueInput("property").setCheck(null);
  3127. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_data);
  3128. this.setInputsInline(true);
  3129. this.setOutput(true, null);
  3130. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3131. this.setTooltip("");
  3132. this.setHelpUrl("");
  3133. }
  3134. };
  3135. Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
  3136. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable.
  3137. var code = '\n'; // TODO: Change ORDER_NONE to the correct strength.
  3138. return [code, Blockly.Python.ORDER_NONE];
  3139. };
  3140. Blockly.Blocks['iot_service_nbiot_cococloud'] = {
  3141. init: function init() {
  3142. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, {
  3143. alt: "*",
  3144. flipRtl: "FALSE"
  3145. }));
  3146. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_title);
  3147. this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  3148. /*this.appendValueInput("first_input")
  3149. .setCheck(null)
  3150. .appendField("Field 0");*/
  3151. this.itemCount_ = 1;
  3152. this.updateShape_();
  3153. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
  3154. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3155. this.setPreviousStatement(true, null);
  3156. this.setNextStatement(true, null);
  3157. this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
  3158. this.setHelpUrl("");
  3159. },
  3160. mutationToDom: function mutationToDom() {
  3161. var container = document.createElement('mutation');
  3162. container.setAttribute('items', this.itemCount_);
  3163. return container;
  3164. },
  3165. domToMutation: function domToMutation(xmlElement) {
  3166. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  3167. this.updateShape_();
  3168. },
  3169. decompose: function decompose(workspace) {
  3170. var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
  3171. containerBlock.initSvg();
  3172. var connection = containerBlock.getInput('STACK').connection;
  3173. for (var i = 0; i < this.itemCount_; i++) {
  3174. var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
  3175. itemBlock.initSvg();
  3176. connection.connect(itemBlock.previousConnection);
  3177. connection = itemBlock.nextConnection;
  3178. }
  3179. return containerBlock;
  3180. },
  3181. compose: function compose(containerBlock) {
  3182. var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs.
  3183. var connections = [];
  3184. while (itemBlock) {
  3185. connections.push(itemBlock.valueConnection_);
  3186. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  3187. } // Disconnect any children that don't belong.
  3188. for (var i = 0; i < this.itemCount_; i++) {
  3189. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3190. if (connection && connections.indexOf(connection) == -1) {
  3191. connection.disconnect();
  3192. }
  3193. }
  3194. this.itemCount_ = connections.length;
  3195. this.updateShape_(); // Reconnect any child blocks.
  3196. for (var i = 0; i < this.itemCount_; i++) {
  3197. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3198. }
  3199. },
  3200. saveConnections: function saveConnections(containerBlock) {
  3201. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3202. var i = 0;
  3203. while (itemBlock) {
  3204. var input = this.getInput('ADD' + i);
  3205. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3206. i++;
  3207. itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
  3208. }
  3209. },
  3210. updateShape_: function updateShape_() {
  3211. for (var i = 0; i < this.itemCount_; i++) {
  3212. if (!this.getInput('ADD' + i)) {
  3213. var input = this.appendValueInput('ADD' + i);
  3214. input.appendField(Blockly.Msg.nbiot_cococloud_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":");
  3215. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3216. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  3217. }
  3218. }
  3219. while (this.getInput('ADD' + i)) {
  3220. this.removeInput('ADD' + i);
  3221. i++;
  3222. }
  3223. }
  3224. };
  3225. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
  3226. init: function init() {
  3227. this.setColour(ESP32_NBIOT_COLOR);
  3228. this.appendDummyInput().appendField("Items");
  3229. this.appendStatementInput('STACK');
  3230. this.setTooltip('');
  3231. this.contextMenu = false;
  3232. }
  3233. };
  3234. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
  3235. init: function init() {
  3236. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3237. this.appendDummyInput().appendField("Field");
  3238. this.setPreviousStatement(true);
  3239. this.setNextStatement(true);
  3240. this.setTooltip('');
  3241. this.contextMenu = false;
  3242. }
  3243. };
  3244. Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
  3245. var api = block.getFieldValue('t_api');
  3246. Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
  3247. Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
  3248. Blockly.Python.definitions_['import'] = '' + '_nbiot_cococloud_send_init_state = False\n' + '\n' + 'def _nbiot_cococloud_send(data):\n' + ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' + ' print(_nbiot_cococloud_send_data)\n' + ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.cn/\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + '';
  3249. var item_field = '',
  3250. item_value = '';
  3251. var nbiot_cococloud_data = "'{";
  3252. for (var n = 0; n < this.itemCount_; n++) {
  3253. item_field = block.getFieldValue("field" + n);
  3254. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  3255. nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  3256. }
  3257. var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
  3258. var code = '' + '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' + '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' + '';
  3259. return code;
  3260. };
  3261. Blockly.Blocks['esp32_network_nbiot_http_get'] = {
  3262. init: function init() {
  3263. this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 150, 50, {
  3264. alt: "*",
  3265. flipRtl: "FALSE"
  3266. }));
  3267. this.appendDummyInput().appendField(Blockly.Msg.nbiot_http_get_title);
  3268. this.appendValueInput("nbiot_http_get_url").setCheck(null).appendField(Blockly.Msg.nbiot_http_get_url);
  3269. this.setInputsInline(false);
  3270. this.setPreviousStatement(true, null);
  3271. this.setNextStatement(true, null);
  3272. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3273. this.setTooltip("");
  3274. this.setHelpUrl("");
  3275. }
  3276. };
  3277. /*
  3278. Blockly.Python['esp32_network_nbiot_http_get'] = function(block) {
  3279. var url = Blockly.Python.valueToCode(block, 'nbiot_http_get_url', Blockly.Python.ORDER_ATOMIC);
  3280. Blockly.Python.addVariable('_SEND_HTTP_GET_ENDPOINT', '', true);
  3281. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  3282. Blockly.Python.definitions_['import_urequests'] = '' +
  3283. 'import urequests as requests\n' +
  3284. 'import ujson as json\n' +
  3285. '\n' +
  3286. '_SEND_HTTP_GET_ENDPOINT = ' + url + '\n' +
  3287. '';
  3288. // TODO: Assemble Python into code variable.
  3289. var code = '_SEND_HTTP_REQUEST = requests.get(_SEND_HTTP_GET_ENDPOINT)\n';
  3290. return code;
  3291. };
  3292. Blockly.Blocks['esp32_network_nbiot_http_post'] = {
  3293. init: function() {
  3294. this.appendDummyInput()
  3295. .appendField(new Blockly.FieldImage("blockly/media/nbiot_http_post.png", 220, 45, { alt: "*", flipRtl: "FALSE" }));
  3296. this.appendDummyInput()
  3297. .appendField("發送 HTTP POST 請求");
  3298. this.appendValueInput("nbiot_http_post_url")
  3299. .setCheck(null)
  3300. .appendField("設定目標網址:");
  3301. this.appendValueInput("data")
  3302. .setCheck(null)
  3303. .appendField("提交資料 (JSON):");
  3304. this.setInputsInline(false);
  3305. this.setPreviousStatement(true, null);
  3306. this.setNextStatement(true, null);
  3307. this.setColour(ESP32_NBIOT_COLOR);
  3308. this.setTooltip("");
  3309. this.setHelpUrl("");
  3310. }
  3311. };
  3312. Blockly.Python['esp32_network_nbiot_http_post'] = function(block) {
  3313. var url = Blockly.Python.valueToCode(block, 'nbiot_http_post_url', Blockly.Python.ORDER_ATOMIC);
  3314. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  3315. Blockly.Python.addVariable('_SEND_HTTP_POST_ENDPOINT', '', true);
  3316. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  3317. Blockly.Python.definitions_['import_urequests'] = '' +
  3318. 'import urequests as requests\n' +
  3319. 'import ujson as json\n' +
  3320. '\n' +
  3321. '_SEND_HTTP_POST_ENDPOINT = ' + url + '\n' +
  3322. '';
  3323. // TODO: Assemble Python into code variable.
  3324. var code = '' +
  3325. '_SEND_HTTP_POST_DATA = \'' + data + '\'\n' +
  3326. '_SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, data = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })\n' +
  3327. '';
  3328. return code;
  3329. };
  3330. */
  3331. /*
  3332. ____
  3333. / ___| ___ _ ____ _____
  3334. \___ \ / _ \ '__\ \ / / _ \
  3335. ___) | __/ | \ V / (_) |
  3336. |____/ \___|_| \_/ \___/
  3337. Blockly.Python.addVariable('', '', true);
  3338. Blockly.Python.definitions_['definition'] = '';
  3339. Blockly.Python.addSetup("setup", '');
  3340. */