extension_module.js 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440
  1. // Serial
  2. Blockly.Blocks['serial_write_data_create_with_container'] = {
  3. init: function () {
  4. this.setColour("#22b845");
  5. this.appendDummyInput()
  6. .appendField(Blockly.Msg.serialcomm_write_item);
  7. this.appendStatementInput('STACK');
  8. this.setTooltip('');
  9. this.contextMenu = false;
  10. }
  11. };
  12. Blockly.Blocks['serial_write_data_create_with_item'] = {
  13. init: function () {
  14. this.setColour("#22b845");
  15. this.appendDummyInput()
  16. .appendField(Blockly.Msg.serialcomm_write_data);
  17. this.setPreviousStatement(true);
  18. this.setNextStatement(true);
  19. this.setTooltip('');
  20. this.contextMenu = false;
  21. }
  22. };
  23. Blockly.Blocks['serial_write_data'] = {
  24. init: function () {
  25. this.appendDummyInput()
  26. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  27. this.appendDummyInput()
  28. .appendField(Blockly.Msg.serialcomm_write_title_text);
  29. this.appendDummyInput()
  30. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  31. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
  32. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  33. this.appendValueInput("ADD1")
  34. .setCheck(null)
  35. // .appendField("Field 0");
  36. this.itemCount_ = 1;
  37. this.setColour("#22b845");
  38. this.setPreviousStatement(true, null);
  39. this.setNextStatement(true, null);
  40. var thisBlock = this;
  41. this.setTooltip(function () {
  42. var mode = thisBlock.getFieldValue('type');
  43. var TOOLTIPS = {
  44. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  45. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  46. };
  47. return TOOLTIPS[mode];
  48. });
  49. this.setHelpUrl("");
  50. },
  51. };
  52. Blockly.Python['serial_write_data'] = function (block) {
  53. // var api = block.getFieldValue('t_api');
  54. var number_uart_bps = block.getFieldValue('uart_bps');
  55. var ai_uart_data = Blockly.Python.valueToCode(this, "ADD1", Blockly.Python.ORDER_NONE);
  56. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  57. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  58. `
  59. var code = `uart_data = bytes(str(${ai_uart_data})+"\\n","utf-8")
  60. SERIAL.write(uart_data)
  61. `
  62. return code;
  63. };
  64. Blockly.Blocks['serial_write_data_coco'] = {
  65. init: function () {
  66. this.appendDummyInput()
  67. .appendField(new Blockly.FieldImage("blockly/media/cocorobo.png", 65, 65, { alt: "*", flipRtl: "FALSE" }));
  68. // this.appendDummyInput()
  69. // .appendField(Blockly.Msg.serialcomm_write_title_text_coco);
  70. this.appendDummyInput()
  71. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  72. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps");
  73. this.appendDummyInput()
  74. .appendField(Blockly.Msg.serial_send_data_on_send_data_cocorobo);
  75. /*this.appendValueInput("first_input")
  76. .setCheck(null)
  77. .appendField("Field 0");*/
  78. this.itemCount_ = 1;
  79. this.updateShape_();
  80. this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item']));
  81. this.setColour("#22b845");
  82. this.setPreviousStatement(true, null);
  83. this.setNextStatement(true, null);
  84. var thisBlock = this;
  85. this.setTooltip(function () {
  86. var mode = thisBlock.getFieldValue('type');
  87. var TOOLTIPS = {
  88. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  89. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  90. };
  91. return TOOLTIPS[mode];
  92. });
  93. this.setHelpUrl("");
  94. },
  95. mutationToDom: function () {
  96. var container = document.createElement('mutation');
  97. container.setAttribute('items', this.itemCount_);
  98. return container;
  99. },
  100. domToMutation: function (xmlElement) {
  101. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  102. this.updateShape_();
  103. },
  104. decompose: function (workspace) {
  105. var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
  106. containerBlock.initSvg();
  107. var connection = containerBlock.getInput('STACK').connection;
  108. for (var i = 0; i < this.itemCount_; i++) {
  109. var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
  110. itemBlock.initSvg();
  111. connection.connect(itemBlock.previousConnection);
  112. connection = itemBlock.nextConnection;
  113. }
  114. return containerBlock;
  115. },
  116. compose: function (containerBlock) {
  117. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  118. // Count number of inputs.
  119. var connections = [];
  120. while (itemBlock) {
  121. connections.push(itemBlock.valueConnection_);
  122. itemBlock = itemBlock.nextConnection &&
  123. itemBlock.nextConnection.targetBlock();
  124. }
  125. // Disconnect any children that don't belong.
  126. for (var i = 0; i < this.itemCount_; i++) {
  127. var connection = this.getInput('ADD' + i).connection.targetConnection;
  128. if (connection && connections.indexOf(connection) == -1) {
  129. connection.disconnect();
  130. }
  131. }
  132. this.itemCount_ = connections.length;
  133. this.updateShape_();
  134. // Reconnect any child blocks.
  135. for (var i = 0; i < this.itemCount_; i++) {
  136. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  137. }
  138. },
  139. saveConnections: function (containerBlock) {
  140. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  141. var i = 0;
  142. while (itemBlock) {
  143. var input = this.getInput('ADD' + i);
  144. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  145. i++;
  146. itemBlock = itemBlock.nextConnection &&
  147. itemBlock.nextConnection.targetBlock();
  148. }
  149. },
  150. updateShape_: function () {
  151. for (var i = 0; i < this.itemCount_; i++) {
  152. if (!this.getInput('ADD' + i)) {
  153. var input = this.appendValueInput('ADD' + i);
  154. input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last);
  155. //input.appendField("資料 " + (i + 1) + ":");
  156. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  157. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  158. }
  159. }
  160. while (this.getInput('ADD' + i)) {
  161. this.removeInput('ADD' + i);
  162. i++;
  163. }
  164. },
  165. };
  166. Blockly.Python['serial_write_data_coco'] = function (block) {
  167. // var api = block.getFieldValue('t_api');
  168. var number_uart_bps = block.getFieldValue('uart_bps');
  169. var item_value = '';
  170. var ai_uart_data = "";
  171. for (var n = 0; n < this.itemCount_; n++) {
  172. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  173. ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
  174. }
  175. var ai_uart_data_final = ai_uart_data + "";
  176. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  177. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  178. `
  179. var code = `uart_data = bytes("SOF|"+${ai_uart_data_final.slice(0, -8)}+"|\\r\\n","utf-8")
  180. SERIAL.write(uart_data)
  181. `
  182. return code;
  183. };
  184. Blockly.Blocks['serial_read_data_setup'] = {
  185. init: function () {
  186. let types = getLocalStorage("type")
  187. this.appendDummyInput()
  188. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  189. this.appendDummyInput()
  190. .appendField(Blockly.Msg.serialcomm_read_setup_title_text + Blockly.Msg.serialcomm_read_setup_title_text_1);
  191. this.appendDummyInput()
  192. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  193. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
  194. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  195. this.setColour("#22b845");
  196. this.setPreviousStatement(true, null);
  197. this.setNextStatement(true, null);
  198. var thisBlock = this;
  199. this.setTooltip(function () {
  200. var mode = thisBlock.getFieldValue('type');
  201. var TOOLTIPS = {
  202. 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  203. 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  204. };
  205. return TOOLTIPS[mode];
  206. });
  207. this.setHelpUrl("");
  208. }
  209. };
  210. Blockly.Python['serial_read_data_setup'] = function (block) {
  211. var number_uart_bps = block.getFieldValue('uart_bps');
  212. // TODO: Assemble Python into code variable.
  213. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  214. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  215. `;
  216. Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
  217. if read_data != None:
  218. read_str = ""
  219. try:
  220. read_str = str(read_data.decode("utf-8")).split(split)[index]
  221. except:
  222. read_str = str(read_data).split(split)[index]
  223. return read_str
  224. `
  225. var code = "";
  226. return code;
  227. };
  228. Blockly.Blocks['serial_read_data_setup_arduino'] = {
  229. init: function () {
  230. this.appendDummyInput()
  231. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  232. this.appendDummyInput()
  233. .appendField(Blockly.Msg.serialcomm_read_setup_title_text_arduino);
  234. this.appendDummyInput()
  235. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  236. .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
  237. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  238. this.setColour("#22b845");
  239. this.setPreviousStatement(true, null);
  240. this.setNextStatement(true, null);
  241. var thisBlock = this;
  242. this.setTooltip(function () {
  243. var mode = thisBlock.getFieldValue('type');
  244. var TOOLTIPS = {
  245. 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  246. 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  247. };
  248. return TOOLTIPS[mode];
  249. });
  250. this.setHelpUrl("");
  251. }
  252. };
  253. Blockly.Python['serial_read_data_setup_arduino'] = function (block) {
  254. var number_uart_bps = block.getFieldValue('uart_bps');
  255. // TODO: Assemble Python into code variable.
  256. Blockly.Python.definitions_["v831_import_os"] =`import os`
  257. Blockly.Python.definitions_['v831_import_serial_ardino_read'] = `import serial
  258. SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
  259. SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})
  260. `;
  261. var code = "";
  262. return code;
  263. };
  264. Blockly.Blocks['serial_read_data_all'] = {
  265. init: function () {
  266. this.appendDummyInput()
  267. .appendField(Blockly.Msg.Serial_Read_Data_all)
  268. // .appendField(new Blockly.FieldDropdown([
  269. // ["serial_data", "serial_data"]
  270. // ]), "TYPE");
  271. this.setOutput(true, null);
  272. this.setColour("#22b845");
  273. this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
  274. this.setHelpUrl("");
  275. }
  276. }
  277. Blockly.Python['serial_read_data_all'] = function (block) {
  278. var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
  279. return [code, Blockly.Python.ORDER_NONE];
  280. };
  281. Blockly.Blocks['serial_read_data_all_usb'] = {
  282. init: function () {
  283. this.appendDummyInput()
  284. .appendField(Blockly.Msg.Serial_Read_Data_all_usb)
  285. // .appendField(new Blockly.FieldDropdown([
  286. // ["serial_data", "serial_data"]
  287. // ]), "TYPE");
  288. this.setOutput(true, null);
  289. this.setColour("#22b845");
  290. this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
  291. this.setHelpUrl("");
  292. }
  293. }
  294. Blockly.Python['serial_read_data_all_usb'] = function (block) {
  295. var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
  296. return [code, Blockly.Python.ORDER_NONE];
  297. };
  298. Blockly.Blocks['serial_read_data'] = {
  299. init: function () {
  300. this.appendDummyInput()
  301. .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_GET_TEXT)
  302. .appendField(new Blockly.FieldDropdown([
  303. ["0", "0"],
  304. ["1", "1"],
  305. ["2", "2"],
  306. ["3", "3"],
  307. ["4", "4"],
  308. ["5", "5"],
  309. ["6", "6"],
  310. ["7", "7"],
  311. ["8", "8"],
  312. ["9", "9"],
  313. ["10", "10"],
  314. ["11", "11"],
  315. ]), "INDEX")
  316. .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_TEXT)
  317. // .appendField(new Blockly.FieldDropdown([
  318. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_STRING, "0"],
  319. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_INTEGER, "1"],
  320. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_FLOAT, "2"],
  321. // ]), "TYPE");
  322. this.setOutput(true, null);
  323. this.setColour("#22b845");
  324. this.setTooltip(Blockly.Msg.Serial_Read_Data_TOOLTIP);
  325. this.setHelpUrl("");
  326. }
  327. }
  328. Blockly.Python['serial_read_data'] = function (block) {
  329. var index = parseInt(block.getFieldValue("INDEX")) + 1;
  330. // var type = block.getFieldValue("TYPE")
  331. // var type_1 = block.getFieldValue("TYPE_1")
  332. Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
  333. if read_data != None:
  334. read_str = ""
  335. try:
  336. read_str = str(read_data.decode("utf-8")).split(split)[index]
  337. except:
  338. read_str = str(read_data).split(split)[index]
  339. return read_str
  340. `
  341. var code = `_read_serial_data(SERIAL.readline().decode("UTF-8","ignore").strip(),"|",${index})`;
  342. return [code, Blockly.Python.ORDER_NONE];
  343. }
  344. Blockly.Blocks['serial_send_data_to_microbit'] = {
  345. init: function () {
  346. this.appendDummyInput()
  347. .appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
  348. this.appendDummyInput()
  349. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  350. .appendField("115200")
  351. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  352. this.appendValueInput("NAME")
  353. // .appendField(Blockly.Msg.serial_send_data_on)
  354. .appendField(Blockly.Msg.serial_send_data_on_send_data);
  355. this.setInputsInline(false);
  356. this.setColour("#22b845");
  357. this.setPreviousStatement(true, null);
  358. this.setNextStatement(true, null);
  359. this.setHelpUrl("");
  360. this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
  361. }
  362. };
  363. Blockly.Python['serial_send_data_to_microbit'] = function (block) {
  364. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
  365. // TODO: Assemble Python into code variable.
  366. Blockly.Python.definitions_["831_import_serial"] = `import serial
  367. SERIAL = serial.Serial("/dev/ttyS1",115200)`
  368. var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
  369. SERIAL.write(uart_data)
  370. `;
  371. return code
  372. };
  373. Blockly.Blocks['serial_send_data_to_arduino'] = {
  374. init: function () {
  375. this.appendDummyInput()
  376. .appendField(new Blockly.FieldImage("blockly/media/arduino.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
  377. this.appendDummyInput()
  378. .appendField(Blockly.Msg.serialcomm_write_title_text_arduino);
  379. this.appendDummyInput()
  380. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  381. .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
  382. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  383. this.appendValueInput("NAME")
  384. // .appendField(Blockly.Msg.serial_send_data_on)
  385. .appendField(Blockly.Msg.serial_send_data_on_send_data_control_panel_arduino);
  386. this.setInputsInline(false);
  387. this.setColour("#22b845");
  388. this.setPreviousStatement(true, null);
  389. this.setNextStatement(true, null);
  390. this.setHelpUrl("");
  391. this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
  392. }
  393. };
  394. Blockly.Python['serial_send_data_to_arduino'] = function (block) {
  395. var number_uart_bps = block.getFieldValue('uart_bps');
  396. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
  397. // TODO: Assemble Python into code variable.
  398. Blockly.Python.definitions_["v831_import_os"] =`import os`
  399. Blockly.Python.definitions_["v831_import_serial_ardino_read"] = `import serial
  400. SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
  401. SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})`
  402. var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
  403. SERIAL.write(uart_data)
  404. `;
  405. return code
  406. };
  407. /*
  408. ____
  409. / ___| ___ _ ____ _____
  410. \___ \ / _ \ '__\ \ / / _ \
  411. ___) | __/ | \ V / (_) |
  412. |____/ \___|_| \_/ \___/
  413. */
  414. var SERVO_BLOCK_COLOR = "#5bb2d6";
  415. var ai_servo_timer = [
  416. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  417. "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  418. "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  419. "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  420. "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  421. "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  422. "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  423. "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  424. "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  425. "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  426. "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  427. "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  428. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM"
  429. // "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM"
  430. ];
  431. var ai_servo_pin = [
  432. // '0',
  433. '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24', '29', '30']
  434. Blockly.Blocks['extension_servo_setup_on_ai'] = {
  435. init: function () {
  436. this.appendDummyInput()
  437. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  438. this.appendDummyInput()
  439. .appendField(Blockly.Msg.servo_setup_ai);
  440. this.setInputsInline(false);
  441. this.setPreviousStatement(true, null);
  442. this.setNextStatement(true, null);
  443. this.setColour(servoCloor);
  444. this.setTooltip(Blockly.Msg.extension_servo_setup_on_ai_TOOLTIP);
  445. this.setHelpUrl("");
  446. }
  447. };
  448. Blockly.Python['extension_servo_setup_on_ai'] = function (block) {
  449. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  450. Blockly.Python.definitions_['v831_import_time'] = `import time`
  451. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  452. sys.path.append("/root/")`
  453. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  454. // Blockly.Python.definitions_["v831_set_servo_S1_S2"] = `S1= multiFuncGpio(0,1)
  455. // S2= multiFuncGpio(1,1)`
  456. var code = '';
  457. return code;
  458. };
  459. Blockly.Blocks['extension_servo_write_on_ai'] = {
  460. init: function () {
  461. this.appendDummyInput()
  462. .appendField(Blockly.Msg.servo_set_gpio_ai)
  463. .appendField(new Blockly.FieldDropdown([
  464. ["S1", "S1"],
  465. ["S2", "S2"]
  466. ]), "gpio");
  467. this.appendValueInput("degree")
  468. .setCheck(null)
  469. .appendField(Blockly.Msg.servo_rotate_to_ai);
  470. this.appendDummyInput()
  471. .appendField(Blockly.Msg.servo_degree_ai);
  472. this.setInputsInline(true);
  473. this.setPreviousStatement(true, null);
  474. this.setNextStatement(true, null);
  475. this.setColour(servoCloor);
  476. this.setTooltip(Blockly.Msg.extension_servo_write_on_ai_TOOLTIP);
  477. this.setHelpUrl("");
  478. }
  479. };
  480. Blockly.Python['extension_servo_write_on_ai'] = function (block) {
  481. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  482. var value_gpio = block.getFieldValue('gpio');
  483. console.log(value_gpio[1])
  484. Blockly.Python.definitions_["v831_servo_init"+value_gpio] = `${value_gpio}= multiFuncGpio(${value_gpio[1]-1},1)`
  485. var code = `${value_gpio}.servoCtrl(${value_degree})
  486. `
  487. return code;
  488. };
  489. let servoCloor = "#386dc8"
  490. var iot_servo_pin = [
  491. '4', '12', '13', '14', '15', '16', '17', '21', '22', '25', '27']
  492. Blockly.Blocks['esp32_main_controller_servo_setup'] = {
  493. init: function () {
  494. this.appendDummyInput()
  495. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  496. this.appendDummyInput()
  497. .appendField(Blockly.Msg.servo_setup);
  498. this.setPreviousStatement(true, null);
  499. this.setNextStatement(true, null);
  500. this.setColour(servoCloor);
  501. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP);
  502. this.setHelpUrl("");
  503. }
  504. };
  505. Blockly.Python['esp32_main_controller_servo_setup'] = function (block) {
  506. // TODO: Assemble Python into code variable.
  507. Blockly.Python.definitions_['definition_servo_setup'] = '' +
  508. 'from machine import Pin, PWM\n' +
  509. 'import time\n' +
  510. '';
  511. var code = '\n';
  512. return code;
  513. };
  514. Blockly.Blocks['esp32_main_controller_servo_set'] = {
  515. init: function () {
  516. this.appendDummyInput()
  517. .appendField(Blockly.Msg.servo_set_gpio)
  518. .appendField(new Blockly.FieldDropdown([
  519. ["4", "0"],
  520. ["12", "1"],
  521. ["13", "2"],
  522. ["14", "3"],
  523. ["15", "4"],
  524. ["16", "5"],
  525. ["17", "6"],
  526. ["21", "7"],
  527. ["22", "8"],
  528. ["25", "9"],
  529. ["27", "10"]
  530. ]), "gpio");
  531. this.appendDummyInput()
  532. .appendField(Blockly.Msg.servo_rotate_to);
  533. this.appendValueInput("degree")
  534. .setCheck(null);
  535. this.appendDummyInput()
  536. .appendField(Blockly.Msg.servo_degree);
  537. this.setInputsInline(true);
  538. this.setPreviousStatement(true, null);
  539. this.setNextStatement(true, null);
  540. this.setColour(servoCloor);
  541. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP);
  542. this.setHelpUrl("");
  543. }
  544. };
  545. Blockly.Python['esp32_main_controller_servo_set'] = function (block) {
  546. var value_gpio = block.getFieldValue('gpio');
  547. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  548. // TODO: Assemble Python into code variable.
  549. Blockly.Python.addVariable('S' + iot_servo_pin[value_gpio], 'S' + iot_servo_pin[value_gpio] + ' = PWM(Pin(' + iot_servo_pin[value_gpio] + '), freq=50, duty=0)', true);
  550. Blockly.Python.definitions_['definition_servo_set'] = '' +
  551. 'def Servo(servo,angle):\n' +
  552. ' angle = angle - 90\n' +
  553. ' servo.freq(50)\n' +
  554. ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' +
  555. ' time.sleep_ms(22)\n' +
  556. '';
  557. var code = '' +
  558. 'Servo(S' + iot_servo_pin[value_gpio] + ',' + value_degree + ')\n' +
  559. '';
  560. return code;
  561. };
  562. /*
  563. __ __ _
  564. | \/ | ___ | |_ ___ _ __
  565. | |\/| |/ _ \| __/ _ \| '__|
  566. | | | | (_) | || (_) | |
  567. |_| |_|\___/ \__\___/|_|
  568. */
  569. var MOTOR_BLOCK_COLOR = "#0000FF";
  570. Blockly.Blocks['esp32_motor_setup'] = {
  571. init: function () {
  572. this.appendDummyInput()
  573. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  574. this.appendDummyInput()
  575. .appendField(Blockly.Msg.x_motor_set_motor)
  576. this.setInputsInline(false);
  577. this.setPreviousStatement(true, null);
  578. this.setNextStatement(true, null);
  579. this.setColour(MOTOR_BLOCK_COLOR);
  580. this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP);
  581. this.setHelpUrl("");
  582. }
  583. };
  584. Blockly.Python['esp32_motor_setup'] = function (block) {
  585. // TODO: Assemble Python into code variable.
  586. Blockly.Python.definitions_['esp32_controller_set_motor'] = '' +
  587. 'import pca9685, machine, motor\n' +
  588. '\n' +
  589. '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' +
  590. '_iot_motor = motor.DCMotors(_iot_motor_i2c)';
  591. var code = '';
  592. return code;
  593. };
  594. Blockly.Blocks['esp32_motor_run'] = {
  595. init: function () {
  596. this.appendDummyInput()
  597. .appendField(Blockly.Msg.x_motor_set_motor_turn)
  598. .appendField(new Blockly.FieldDropdown([
  599. ["A", "0"],
  600. ["B", "1"],
  601. ["C", "3"],
  602. ["D", "2"]
  603. ]), "motor_type")
  604. .appendField(Blockly.Msg.x_motor_set_motor_speed);
  605. this.appendValueInput("speed")
  606. .setCheck(null);
  607. this.appendDummyInput()
  608. .appendField(Blockly.Msg.x_motor_exec)
  609. // .appendField(new Blockly.FieldDropdown([
  610. // [Blockly.Msg.x_motor_cw, "pos"],
  611. // [Blockly.Msg.x_motor_acw, "neg"]
  612. // ]), "direction")
  613. .appendField(Blockly.Msg.x_motor_set_motor_turns);
  614. this.setInputsInline(true);
  615. this.setPreviousStatement(true, null);
  616. this.setNextStatement(true, null);
  617. this.setColour(MOTOR_BLOCK_COLOR);
  618. this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP);
  619. this.setHelpUrl("");
  620. }
  621. };
  622. Blockly.Python['esp32_motor_run'] = function (block) {
  623. var dropdown_motor_type = block.getFieldValue('motor_type');
  624. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  625. // var dropdown_direction = block.getFieldValue('direction');
  626. var code;
  627. if (value_speed.indexOf("-") == -1) {
  628. code = '_iot_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
  629. } else {
  630. code = '_iot_motor.speed(' + dropdown_motor_type + ', -(abs' + value_speed + '*16))\n';
  631. }
  632. return code;
  633. };
  634. Blockly.Blocks['ai_motor_setup'] = {
  635. init: function () {
  636. this.appendDummyInput()
  637. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  638. this.appendDummyInput()
  639. .appendField(Blockly.Msg.x_motor_set_motor_ai)
  640. this.setInputsInline(false);
  641. this.setPreviousStatement(true, null);
  642. this.setNextStatement(true, null);
  643. this.setColour(servoCloor);
  644. this.setTooltip(Blockly.Msg.ai_motor_setup_TOOLTIP);
  645. this.setHelpUrl("");
  646. }
  647. };
  648. Blockly.Python['ai_motor_setup'] = function (block) {
  649. // TODO: Assemble Python into code variable.
  650. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  651. Blockly.Python.definitions_['v831_import_time'] = `import time`
  652. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  653. sys.path.append("/root/")`
  654. Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
  655. iic_slaver=stm8s()
  656. iic_slaver.clear()
  657. del iic_slaver`;
  658. Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
  659. // Blockly.Python.definitions_["v831_motor_init"] = `M1=dcMotor(1)
  660. // M2=dcMotor(2)`
  661. var code = '';
  662. return code;
  663. };
  664. Blockly.Blocks['ai_motor_run'] = {
  665. init: function () {
  666. this.appendDummyInput()
  667. .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
  668. .appendField(new Blockly.FieldDropdown([
  669. ["M1", "M1"],
  670. ["M2", "M2"]
  671. ]), "motor_type")
  672. .appendField(Blockly.Msg.x_motor_set_motor_speed_ai);
  673. this.appendValueInput("speed")
  674. .setCheck(null);
  675. this.appendDummyInput()
  676. .appendField(Blockly.Msg.x_motor_exec_ai)
  677. .appendField(new Blockly.FieldDropdown([
  678. [Blockly.Msg.x_motor_cw_ai, "pos"],
  679. [Blockly.Msg.x_motor_acw_ai, "neg"]
  680. ]), "direction")
  681. .appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
  682. this.setInputsInline(true);
  683. this.setPreviousStatement(true, null);
  684. this.setNextStatement(true, null);
  685. this.setColour(servoCloor);
  686. this.setTooltip(Blockly.Msg.ai_motor_run_TOOLTIP);
  687. this.setHelpUrl("");
  688. }
  689. };
  690. Blockly.Python['ai_motor_run'] = function (block) {
  691. var dropdown_motor_type = block.getFieldValue('motor_type');
  692. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  693. var dropdown_direction = block.getFieldValue('direction');
  694. var code = ''
  695. Blockly.Python.definitions_["v831dcMotorCtrlFun"+dropdown_motor_type] =`${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1,2)})`
  696. if (dropdown_direction == 'pos') {
  697. code = `${dropdown_motor_type}.dcMotorCtrl(1,${value_speed})
  698. `
  699. }
  700. else {
  701. code = `${dropdown_motor_type}.dcMotorCtrl(0,${value_speed})
  702. `
  703. }
  704. return code;
  705. };
  706. /*
  707. _ _____ ____ __ __ _ _
  708. | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __
  709. | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ /
  710. | |___| |___| |_| | | | | | (_| | |_| | | |> <
  711. |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\
  712. */
  713. let AILEDCOLOR = "#e8795b"
  714. Blockly.Blocks['ai_led_matrix_setup'] = {
  715. init: function () {
  716. this.appendDummyInput()
  717. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  718. this.appendDummyInput()
  719. .appendField(Blockly.Msg.x_led_matrix_setup_ai);
  720. this.appendDummyInput()
  721. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_1)
  722. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  723. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  724. this.setInputsInline(false);
  725. this.setPreviousStatement(true, null);
  726. this.setNextStatement(true, null);
  727. this.setColour(AILEDCOLOR);
  728. this.setTooltip(Blockly.Msg.ai_led_matrix_setup_TOOLTIP);
  729. this.setHelpUrl("");
  730. }
  731. };
  732. Blockly.Python['ai_led_matrix_setup'] = function (block) {
  733. var number_brightness = block.getFieldValue('brightness');
  734. Blockly.Python.definitions_['ai_controller_led_setup'] = '' +
  735. 'from modules import ws2812\n' +
  736. '\n' +
  737. '_ai_led_matrix_total = 36\n' +
  738. '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' +
  739. '_ai_brightness = ' + number_brightness + '\n' +
  740. '\n' +
  741. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  742. ' if _ai_brightness <= 255:\n' +
  743. ' alpha = _ai_brightness / 255\n' +
  744. ' elif _ai_brightness > 255:\n' +
  745. ' alpha = 255 / 255\n' +
  746. ' elif _ai_brightness < 0:\n' +
  747. ' alpha = 0 / 255\n' +
  748. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  749. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  750. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  751. ' final = (final_red, final_green, final_blue)\n' +
  752. ' return final\n' +
  753. '';
  754. // TODO: Assemble Python into code variable.
  755. var code = '';
  756. return code;
  757. };
  758. Blockly.Blocks['ai_led_matrix_set_bright'] = {
  759. init: function () {
  760. this.appendValueInput("brightness")
  761. .setCheck(null)
  762. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  763. this.appendDummyInput()
  764. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  765. this.setInputsInline(true);
  766. this.setPreviousStatement(true, null);
  767. this.setNextStatement(true, null);
  768. this.setColour(AILEDCOLOR);
  769. this.setTooltip(Blockly.Msg.ai_led_matrix_set_bright_TOOLTIP);
  770. this.setHelpUrl("");
  771. }
  772. };
  773. Blockly.Python['ai_led_matrix_set_bright'] = function (block) {
  774. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  775. // TODO: Assemble Python into code variable.
  776. var code = '_ai_brightness = ' + value_brightness + '\n';
  777. return code;
  778. };
  779. Blockly.Blocks['ai_led_matrix_xy'] = {
  780. init: function () {
  781. this.appendValueInput("x")
  782. .setCheck(null)
  783. .appendField(Blockly.Msg.x_led_matrix_width_ai_x);
  784. this.appendValueInput("y")
  785. .setCheck(null)
  786. .appendField(Blockly.Msg.x_led_matrix_width_ai_y);
  787. this.setInputsInline(true);
  788. this.setOutput(true, null);
  789. this.setColour(AILEDCOLOR);
  790. this.setTooltip(Blockly.Msg.ai_led_matrix_xy_TOOLTIP);
  791. this.setHelpUrl("");
  792. }
  793. };
  794. Blockly.Python['ai_led_matrix_xy'] = function (block) {
  795. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  796. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  797. // TODO: Assemble Python into code variable.
  798. var code = value_x + ',' + value_y;
  799. // TODO: Change ORDER_NONE to the correct strength.
  800. return [code, Blockly.Python.ORDER_NONE];
  801. };
  802. Blockly.Blocks['ai_led_matrix_wh'] = {
  803. init: function () {
  804. this.appendValueInput("w")
  805. .setCheck(null)
  806. .appendField(Blockly.Msg.x_led_matrix_width_ai);
  807. this.appendValueInput("h")
  808. .setCheck(null)
  809. .appendField(Blockly.Msg.x_led_matrix_height_ai);
  810. this.setInputsInline(true);
  811. this.setOutput(true, null);
  812. this.setColour(AILEDCOLOR);
  813. this.setTooltip(Blockly.Msg.ai_led_matrix_wh_TOOLTIP);
  814. this.setHelpUrl("");
  815. }
  816. };
  817. Blockly.Python['ai_led_matrix_wh'] = function (block) {
  818. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  819. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  820. // TODO: Assemble Python into code variable.
  821. var code = value_w + ',' + value_h;
  822. // TODO: Change ORDER_NONE to the correct strength.
  823. return [code, Blockly.Python.ORDER_NONE];
  824. };
  825. Blockly.Blocks['ai_led_matrix_color_picker'] = {
  826. init: function () {
  827. this.appendDummyInput()
  828. .appendField(Blockly.Msg.x_led_matrix_color_ai)
  829. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  830. this.setInputsInline(true);
  831. this.setOutput(true, null);
  832. this.setColour(AILEDCOLOR);
  833. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  834. this.setHelpUrl("");
  835. }
  836. };
  837. Blockly.Python['ai_led_matrix_color_picker'] = function (block) {
  838. var color = block.getFieldValue('color');
  839. var d = 0,
  840. e = 0,
  841. f = 0;
  842. try {
  843. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  844. e = parseInt(color.substring(3, 5), 16),
  845. f = parseInt(color.substring(5, 7), 16))
  846. } catch (g) { }
  847. // TODO: Assemble Python into code variable.
  848. var code = d + ',' + e + ',' + f;
  849. // TODO: Change ORDER_NONE to the correct strength.
  850. return [code, Blockly.Python.ORDER_NONE];
  851. };
  852. Blockly.Blocks['ai_led_matrix_draw_pixel'] = {
  853. init: function () {
  854. this.appendDummyInput()
  855. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  856. this.appendDummyInput()
  857. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  858. this.appendDummyInput()
  859. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai);
  860. this.appendValueInput("color")
  861. .setCheck(null)
  862. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  863. this.appendValueInput("coordinate")
  864. .setCheck(null)
  865. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  866. this.setInputsInline(false);
  867. this.setPreviousStatement(true, null);
  868. this.setNextStatement(true, null);
  869. this.setColour(AILEDCOLOR);
  870. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_pixel_TOOLTIP);
  871. this.setHelpUrl("");
  872. }
  873. };
  874. Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) {
  875. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  876. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  877. // TODO: Assemble Python into code variable.
  878. var code = 'ai_pixel_value_coordinate = ' + value_coordinate + '\n' +
  879. '_a = _ai_led_matrix.set_led((((ai_pixel_value_coordinate[1]*6)//6)-1)*6+(ai_pixel_value_coordinate[0]-1),rgba_to_rgb_conversion' + value_color + ')\n';
  880. return code;
  881. };
  882. Blockly.Blocks['ai_led_matrix_draw_rectangle'] = {
  883. init: function () {
  884. this.appendDummyInput()
  885. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  886. this.appendDummyInput()
  887. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  888. this.appendDummyInput()
  889. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  890. this.appendValueInput("color")
  891. .setCheck(null)
  892. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  893. this.appendValueInput("coordinate")
  894. .setCheck(null)
  895. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  896. this.appendValueInput("size")
  897. .setCheck(null)
  898. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  899. this.setInputsInline(false);
  900. this.setPreviousStatement(true, null);
  901. this.setNextStatement(true, null);
  902. this.setColour(AILEDCOLOR);
  903. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_rectangle_TOOLTIP);
  904. this.setHelpUrl("");
  905. }
  906. };
  907. Blockly.Python['ai_led_matrix_draw_rectangle'] = function (block) {
  908. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  909. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  910. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  911. Blockly.Python.codeFunctions_['ai_led_matrix_draw_rectangle_setup'] = '' +
  912. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  913. ' for i in range(x, x+ w, 1):\n' +
  914. ' for j in range(y, y+h, 1):\n' +
  915. ' _a = _ai_led_matrix.set_led((((j*6)//6)-1)*6+(i-1),(rgb))\n' +
  916. '\n';
  917. // TODO: Assemble Python into code variable.
  918. // var comma_location = value_coordinate.indexOf(",");
  919. // var last_bracket_location = value_coordinate.length;
  920. // var cord_x = value_coordinate.substr(1, comma_location - 1);
  921. // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  922. // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  923. // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  924. // var comma_location_size = value_size.indexOf(",");
  925. // var last_bracket_location_size = value_size.length;
  926. // var size_width = value_size.substr(1, comma_location_size - 1);
  927. // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  928. // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  929. // console.log("Size string length: " + last_bracket_location_size);
  930. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  931. var code = 'ai_rectangle_value_coordinate = ' + value_coordinate + '\n' +
  932. 'ai_rectangle_value_size = ' + value_size + '\n' +
  933. '_ai_led_draw_rectangle(ai_rectangle_value_coordinate[0], ai_rectangle_value_coordinate[1], ai_rectangle_value_size[0], ai_rectangle_value_size[1], rgba_to_rgb_conversion' + value_color + ')\n';
  934. return code;
  935. };
  936. Blockly.Blocks['ai_led_pattern_6x6'] = {
  937. init: function () {
  938. this.appendDummyInput()
  939. .appendField(
  940. new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
  941. .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
  942. this.appendDummyInput()
  943. .appendField(" ")
  944. .appendField((new Blockly.FieldColour("#000000")), "6_1")
  945. .appendField((new Blockly.FieldColour("#000000")), "6_2")
  946. .appendField((new Blockly.FieldColour("#000000")), "6_3")
  947. .appendField((new Blockly.FieldColour("#000000")), "6_4")
  948. .appendField((new Blockly.FieldColour("#000000")), "6_5")
  949. .appendField((new Blockly.FieldColour("#000000")), "6_6")
  950. .appendField(" ");
  951. this.appendDummyInput()
  952. .appendField(" ")
  953. .appendField((new Blockly.FieldColour("#000000")), "5_1")
  954. .appendField((new Blockly.FieldColour("#000000")), "5_2")
  955. .appendField((new Blockly.FieldColour("#000000")), "5_3")
  956. .appendField((new Blockly.FieldColour("#000000")), "5_4")
  957. .appendField((new Blockly.FieldColour("#000000")), "5_5")
  958. .appendField((new Blockly.FieldColour("#000000")), "5_6")
  959. .appendField(" ");
  960. this.appendDummyInput()
  961. .appendField(" ")
  962. .appendField((new Blockly.FieldColour("#000000")), "4_1")
  963. .appendField((new Blockly.FieldColour("#000000")), "4_2")
  964. .appendField((new Blockly.FieldColour("#000000")), "4_3")
  965. .appendField((new Blockly.FieldColour("#000000")), "4_4")
  966. .appendField((new Blockly.FieldColour("#000000")), "4_5")
  967. .appendField((new Blockly.FieldColour("#000000")), "4_6")
  968. .appendField(" ");
  969. this.appendDummyInput()
  970. .appendField(" ")
  971. .appendField((new Blockly.FieldColour("#000000")), "3_1")
  972. .appendField((new Blockly.FieldColour("#000000")), "3_2")
  973. .appendField((new Blockly.FieldColour("#000000")), "3_3")
  974. .appendField((new Blockly.FieldColour("#000000")), "3_4")
  975. .appendField((new Blockly.FieldColour("#000000")), "3_5")
  976. .appendField((new Blockly.FieldColour("#000000")), "3_6")
  977. .appendField(" ");
  978. this.appendDummyInput()
  979. .appendField(" ")
  980. .appendField((new Blockly.FieldColour("#000000")), "2_1")
  981. .appendField((new Blockly.FieldColour("#000000")), "2_2")
  982. .appendField((new Blockly.FieldColour("#000000")), "2_3")
  983. .appendField((new Blockly.FieldColour("#000000")), "2_4")
  984. .appendField((new Blockly.FieldColour("#000000")), "2_5")
  985. .appendField((new Blockly.FieldColour("#000000")), "2_6")
  986. .appendField(" ");
  987. this.appendDummyInput()
  988. .appendField(" ")
  989. .appendField((new Blockly.FieldColour("#000000")), "1_1")
  990. .appendField((new Blockly.FieldColour("#000000")), "1_2")
  991. .appendField((new Blockly.FieldColour("#000000")), "1_3")
  992. .appendField((new Blockly.FieldColour("#000000")), "1_4")
  993. .appendField((new Blockly.FieldColour("#000000")), "1_5")
  994. .appendField((new Blockly.FieldColour("#000000")), "1_6")
  995. .appendField(" ");
  996. this.setPreviousStatement(true);
  997. this.setNextStatement(true);
  998. this.setColour(AILEDCOLOR);
  999. this.setTooltip('');
  1000. this.setHelpUrl('');
  1001. }
  1002. };
  1003. var ai_led_pattern_6x6_length = 0;
  1004. Blockly.Python['ai_led_pattern_6x6'] = function (block) {
  1005. // TODO: Assemble Python into code variable.
  1006. var rgb = [];
  1007. ai_led_pattern_6x6_length = ai_led_pattern_6x6_length + 1;
  1008. for (var i = 6; i >= 1; i--) {
  1009. for (var j = 1; j < 7; j++) {
  1010. var color = block.getFieldValue(i + '_' + j);
  1011. var d = 0,
  1012. e = 0,
  1013. f = 0;
  1014. try {
  1015. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1016. e = parseInt(color.substring(3, 5), 16),
  1017. f = parseInt(color.substring(5, 7), 16))
  1018. } catch (g) { }
  1019. rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
  1020. }
  1021. }
  1022. Blockly.Python.codeFunctions_['ai_led_pattern_6x6_setup'] = '' +
  1023. 'def _ai_led_draw_picture(rgb):\n' +
  1024. ' for i in range(6, 0, -1):\n' +
  1025. ' for j in range(1, 7):\n' +
  1026. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb[(((i*6)//6)-1)*6+(j-1)]))\n' +
  1027. '\n';
  1028. Blockly.Python.codeFunctions_['ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + '_setup'] = '' +
  1029. 'ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ' = [' + rgb + ']' +
  1030. '\n';
  1031. var code = '_ai_led_draw_picture(ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ')\n';
  1032. return code;
  1033. };
  1034. Blockly.Blocks['ai_led_matrix_show_above'] = {
  1035. init: function () {
  1036. this.appendDummyInput()
  1037. .appendField(Blockly.Msg.x_led_matrix_show_above_ai);
  1038. this.setInputsInline(false);
  1039. this.setPreviousStatement(true, null);
  1040. this.setNextStatement(true, null);
  1041. this.setColour(AILEDCOLOR);
  1042. this.setTooltip(Blockly.Msg.ai_led_matrix_show_above_TOOLTIP);
  1043. this.setHelpUrl("");
  1044. }
  1045. };
  1046. Blockly.Python['ai_led_matrix_show_above'] = function (block) {
  1047. // TODO: Assemble Python into code variable.
  1048. var code = '_ai_led_matrix.display()\n';
  1049. return code;
  1050. };
  1051. Blockly.Blocks['ai_led_matrix_clear_screen'] = {
  1052. init: function () {
  1053. this.appendDummyInput()
  1054. .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
  1055. this.setInputsInline(false);
  1056. this.setPreviousStatement(true, null);
  1057. this.setNextStatement(true, null);
  1058. this.setColour(AILEDCOLOR);
  1059. this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
  1060. this.setHelpUrl("");
  1061. }
  1062. };
  1063. Blockly.Python['ai_led_matrix_clear_screen'] = function (block) {
  1064. // TODO: Assemble Python into code variable.
  1065. Blockly.Python.definitions_['ai_led_matrix_clear_screen_setup'] = '' +
  1066. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1067. ' for i in range(x, x+ w, 1):\n' +
  1068. ' for j in range(y, y+h, 1):\n' +
  1069. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb))\n' +
  1070. '';
  1071. var code = '' +
  1072. '_ai_led_draw_rectangle(1, 1, 6, 6, rgba_to_rgb_conversion(0, 0, 0))\n' +
  1073. '_ai_led_matrix.display()\n' +
  1074. '';
  1075. return code;
  1076. };
  1077. Blockly.Blocks['ai_led_matrix_rgb_value_input'] = {
  1078. init: function () {
  1079. this.appendDummyInput()
  1080. .appendField(Blockly.Msg.image_process_rgb_r);
  1081. this.appendValueInput("rgb_value_r")
  1082. .setCheck(null)
  1083. .appendField("");
  1084. this.appendDummyInput()
  1085. .appendField(Blockly.Msg.image_process_rgb_g);
  1086. this.appendValueInput("rgb_value_g")
  1087. .setCheck(null)
  1088. .appendField("");
  1089. this.appendDummyInput()
  1090. .appendField(Blockly.Msg.image_process_rgb_b);
  1091. this.appendValueInput("rgb_value_b")
  1092. .setCheck(null)
  1093. .appendField("");
  1094. this.setInputsInline(true);
  1095. this.setOutput(true, null);
  1096. this.setColour(AILEDCOLOR);
  1097. this.setTooltip(Blockly.Msg.ai_led_matrix_rgb_value_input_TOOLTIP);
  1098. this.setHelpUrl("");
  1099. }
  1100. };
  1101. Blockly.Python['ai_led_matrix_rgb_value_input'] = function (block) {
  1102. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1103. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1104. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1105. // TODO: Assemble Python into code variable.
  1106. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1107. // TODO: Change ORDER_NONE to the correct strength.
  1108. return [code, Blockly.Python.ORDER_NONE];
  1109. };
  1110. Blockly.Blocks['iot_led_matrix_rgb_value_input'] = {
  1111. init: function () {
  1112. this.appendDummyInput()
  1113. .appendField(Blockly.Msg.image_process_rgb_r);
  1114. this.appendValueInput("rgb_value_r")
  1115. .setCheck(null)
  1116. .appendField("");
  1117. this.appendDummyInput()
  1118. .appendField(Blockly.Msg.image_process_rgb_g);
  1119. this.appendValueInput("rgb_value_g")
  1120. .setCheck(null)
  1121. .appendField("");
  1122. this.appendDummyInput()
  1123. .appendField(Blockly.Msg.image_process_rgb_b);
  1124. this.appendValueInput("rgb_value_b")
  1125. .setCheck(null)
  1126. .appendField("");
  1127. this.setInputsInline(true);
  1128. this.setOutput(true, null);
  1129. this.setColour(AILEDCOLOR);
  1130. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Rgb_Value_Input_TOOLTIP);
  1131. this.setHelpUrl("");
  1132. }
  1133. };
  1134. Blockly.Python['iot_led_matrix_rgb_value_input'] = function (block) {
  1135. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1136. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1137. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1138. // TODO: Assemble Python into code variable.
  1139. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1140. // TODO: Change ORDER_NONE to the correct strength.
  1141. return [code, Blockly.Python.ORDER_NONE];
  1142. };
  1143. Blockly.Blocks['iot_led_matrix_setup'] = {
  1144. init: function () {
  1145. this.appendDummyInput()
  1146. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  1147. this.appendDummyInput()
  1148. .appendField(Blockly.Msg.x_led_matrix_setup_iot);
  1149. this.appendDummyInput()
  1150. .appendField(Blockly.Msg.x_led_matrix_setup_iot_io)
  1151. .appendField(new Blockly.FieldDropdown([
  1152. ["25", "25"],
  1153. ["15", "15"]
  1154. ]), "io");
  1155. this.appendDummyInput()
  1156. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_1)
  1157. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  1158. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_range);
  1159. this.setInputsInline(false);
  1160. this.setPreviousStatement(true, null);
  1161. this.setNextStatement(true, null);
  1162. this.setColour(AILEDCOLOR);
  1163. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP);
  1164. this.setHelpUrl("");
  1165. }
  1166. };
  1167. Blockly.Python['iot_led_matrix_setup'] = function (block) {
  1168. var number_brightness = block.getFieldValue('brightness');
  1169. var dropdown_io = block.getFieldValue('io');
  1170. Blockly.Python.definitions_['iot_controller_led_setup'] = '' +
  1171. 'import machine, neopixel, time\n' +
  1172. '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(' + dropdown_io + '), 36)\n' +
  1173. '_iot_brightness = ' + number_brightness + '\n' +
  1174. '\n' +
  1175. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  1176. ' if _iot_brightness <= 255:\n' +
  1177. ' alpha = _iot_brightness / 255\n' +
  1178. ' elif _iot_brightness > 255:\n' +
  1179. ' alpha = 255 / 255\n' +
  1180. ' elif _iot_brightness < 0:\n' +
  1181. ' alpha = 0 / 255\n' +
  1182. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  1183. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  1184. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  1185. ' final = (final_red, final_green, final_blue)\n' +
  1186. ' return final\n' +
  1187. '\n';
  1188. // TODO: Assemble Python into code variable.
  1189. var code = '';
  1190. return code;
  1191. };
  1192. Blockly.Blocks['iot_led_matrix_set_bright'] = {
  1193. init: function () {
  1194. this.appendValueInput("brightness")
  1195. .setCheck(null)
  1196. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  1197. this.appendDummyInput()
  1198. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  1199. this.setInputsInline(true);
  1200. this.setPreviousStatement(true, null);
  1201. this.setNextStatement(true, null);
  1202. this.setColour(AILEDCOLOR);
  1203. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Set_Bright_TOOLTIP);
  1204. this.setHelpUrl("");
  1205. }
  1206. };
  1207. Blockly.Python['iot_led_matrix_set_bright'] = function (block) {
  1208. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1209. // TODO: Assemble Python into code variable.
  1210. var code = '_iot_brightness = ' + value_brightness + '\n';
  1211. return code;
  1212. };
  1213. Blockly.Blocks['iot_led_matrix_xy'] = {
  1214. init: function () {
  1215. this.appendValueInput("x")
  1216. .setCheck(null)
  1217. .appendField(Blockly.Msg.x_led_matrix_width_iot_x);
  1218. this.appendValueInput("y")
  1219. .setCheck(null)
  1220. .appendField(Blockly.Msg.x_led_matrix_width_iot_y);
  1221. this.setInputsInline(true);
  1222. this.setOutput(true, null);
  1223. this.setColour(AILEDCOLOR);
  1224. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP);
  1225. this.setHelpUrl("");
  1226. }
  1227. };
  1228. Blockly.Python['iot_led_matrix_xy'] = function (block) {
  1229. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  1230. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  1231. // TODO: Assemble Python into code variable.
  1232. var code = value_x + ',' + value_y;
  1233. // TODO: Change ORDER_NONE to the correct strength.
  1234. return [code, Blockly.Python.ORDER_NONE];
  1235. };
  1236. Blockly.Blocks['iot_led_matrix_wh'] = {
  1237. init: function () {
  1238. this.appendValueInput("w")
  1239. .setCheck(null)
  1240. .appendField(Blockly.Msg.x_led_matrix_width_iot);
  1241. this.appendValueInput("h")
  1242. .setCheck(null)
  1243. .appendField(Blockly.Msg.x_led_matrix_height_iot);
  1244. this.setInputsInline(true);
  1245. this.setOutput(true, null);
  1246. this.setColour(AILEDCOLOR);
  1247. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP);
  1248. this.setHelpUrl("");
  1249. }
  1250. };
  1251. Blockly.Python['iot_led_matrix_wh'] = function (block) {
  1252. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  1253. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  1254. // TODO: Assemble Python into code variable.
  1255. var code = value_w + ',' + value_h;
  1256. // TODO: Change ORDER_NONE to the correct strength.
  1257. return [code, Blockly.Python.ORDER_NONE];
  1258. };
  1259. Blockly.Blocks['iot_led_matrix_color_picker'] = {
  1260. init: function () {
  1261. this.appendDummyInput()
  1262. .appendField(Blockly.Msg.x_led_matrix_color_iot)
  1263. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  1264. this.setInputsInline(true);
  1265. this.setOutput(true, null);
  1266. this.setColour(AILEDCOLOR);
  1267. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  1268. this.setHelpUrl("");
  1269. }
  1270. };
  1271. Blockly.Python['iot_led_matrix_color_picker'] = function (block) {
  1272. var color = block.getFieldValue('color');
  1273. var d = 0,
  1274. e = 0,
  1275. f = 0;
  1276. try {
  1277. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1278. e = parseInt(color.substring(3, 5), 16),
  1279. f = parseInt(color.substring(5, 7), 16))
  1280. } catch (g) { }
  1281. // TODO: Assemble Python into code variable.
  1282. var code = d + ',' + e + ',' + f;
  1283. // TODO: Change ORDER_NONE to the correct strength.
  1284. return [code, Blockly.Python.ORDER_MEMBER];
  1285. };
  1286. Blockly.Blocks['iot_led_matrix_draw_pixel'] = {
  1287. init: function () {
  1288. this.appendDummyInput()
  1289. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1290. this.appendDummyInput()
  1291. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1292. this.appendDummyInput()
  1293. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot);
  1294. this.appendValueInput("color")
  1295. .setCheck(null)
  1296. .appendField(Blockly.Msg.x_led_matrix_parameter_color_iot);
  1297. this.appendValueInput("coordinate")
  1298. .setCheck(null)
  1299. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot);
  1300. this.setInputsInline(false);
  1301. this.setPreviousStatement(true, null);
  1302. this.setNextStatement(true, null);
  1303. this.setColour(AILEDCOLOR);
  1304. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP);
  1305. this.setHelpUrl("");
  1306. }
  1307. };
  1308. Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) {
  1309. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1310. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1311. // TODO: Assemble Python into code variable.
  1312. // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255)
  1313. var code = 'iot_pixel_value_coordinate = ' + value_coordinate + '\n' +
  1314. '_6x6_led_matrix[(((iot_pixel_value_coordinate[1]*6)//6)-1)*6+(iot_pixel_value_coordinate[0]-1)] = rgba_to_rgb_conversion' + value_color + '\n';
  1315. return code;
  1316. };
  1317. Blockly.Blocks['iot_led_matrix_draw_rectangle'] = {
  1318. init: function () {
  1319. this.appendDummyInput()
  1320. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1321. this.appendDummyInput()
  1322. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1323. this.appendDummyInput()
  1324. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  1325. this.appendValueInput("color")
  1326. .setCheck(null)
  1327. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  1328. this.appendValueInput("coordinate")
  1329. .setCheck(null)
  1330. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  1331. this.appendValueInput("size")
  1332. .setCheck(null)
  1333. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  1334. this.setInputsInline(false);
  1335. this.setPreviousStatement(true, null);
  1336. this.setNextStatement(true, null);
  1337. this.setColour(AILEDCOLOR);
  1338. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Rectangle_TOOLTIP);
  1339. this.setHelpUrl("");
  1340. }
  1341. };
  1342. Blockly.Python['iot_led_matrix_draw_rectangle'] = function (block) {
  1343. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1344. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1345. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1346. Blockly.Python.definitions_['iot_led_matrix_draw_rectangle_setup'] = '' +
  1347. 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1348. ' for i in range(x, x+ w, 1):\n' +
  1349. ' for j in range(y, y+h, 1):\n' +
  1350. ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
  1351. '\n';
  1352. // TODO: Assemble Python into code variable.
  1353. // var comma_location = value_coordinate.indexOf(",");
  1354. // var last_bracket_location = value_coordinate.length;
  1355. // var cord_x = value_coordinate.substr(1, comma_location - 1);
  1356. // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  1357. // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  1358. // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  1359. // var comma_location_size = value_size.indexOf(",");
  1360. // var last_bracket_location_size = value_size.length;
  1361. // var size_width = value_size.substr(1, comma_location_size - 1);
  1362. // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  1363. // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  1364. // console.log("Size string length: " + last_bracket_location_size);
  1365. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  1366. var code = 'iot_rectangle_value_coordinate = ' + value_coordinate + '\n' +
  1367. 'iot_rectangle_value_size = ' + value_size + '\n' +
  1368. '_iot_led_draw_rectangle(iot_rectangle_value_coordinate[0], iot_rectangle_value_coordinate[1], iot_rectangle_value_size[0], iot_rectangle_value_size[1], rgba_to_rgb_conversion' + value_color + ')\n';
  1369. return code;
  1370. };
  1371. Blockly.Blocks['iot_led_pattern_6x6'] = {
  1372. init: function () {
  1373. this.appendDummyInput()
  1374. .appendField(
  1375. new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
  1376. .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
  1377. this.appendDummyInput()
  1378. .appendField(" ")
  1379. .appendField((new Blockly.FieldColour("#000000")), "6_1")
  1380. .appendField((new Blockly.FieldColour("#000000")), "6_2")
  1381. .appendField((new Blockly.FieldColour("#000000")), "6_3")
  1382. .appendField((new Blockly.FieldColour("#000000")), "6_4")
  1383. .appendField((new Blockly.FieldColour("#000000")), "6_5")
  1384. .appendField((new Blockly.FieldColour("#000000")), "6_6")
  1385. .appendField(" ");
  1386. this.appendDummyInput()
  1387. .appendField(" ")
  1388. .appendField((new Blockly.FieldColour("#000000")), "5_1")
  1389. .appendField((new Blockly.FieldColour("#000000")), "5_2")
  1390. .appendField((new Blockly.FieldColour("#000000")), "5_3")
  1391. .appendField((new Blockly.FieldColour("#000000")), "5_4")
  1392. .appendField((new Blockly.FieldColour("#000000")), "5_5")
  1393. .appendField((new Blockly.FieldColour("#000000")), "5_6")
  1394. .appendField(" ");
  1395. this.appendDummyInput()
  1396. .appendField(" ")
  1397. .appendField((new Blockly.FieldColour("#000000")), "4_1")
  1398. .appendField((new Blockly.FieldColour("#000000")), "4_2")
  1399. .appendField((new Blockly.FieldColour("#000000")), "4_3")
  1400. .appendField((new Blockly.FieldColour("#000000")), "4_4")
  1401. .appendField((new Blockly.FieldColour("#000000")), "4_5")
  1402. .appendField((new Blockly.FieldColour("#000000")), "4_6")
  1403. .appendField(" ");
  1404. this.appendDummyInput()
  1405. .appendField(" ")
  1406. .appendField((new Blockly.FieldColour("#000000")), "3_1")
  1407. .appendField((new Blockly.FieldColour("#000000")), "3_2")
  1408. .appendField((new Blockly.FieldColour("#000000")), "3_3")
  1409. .appendField((new Blockly.FieldColour("#000000")), "3_4")
  1410. .appendField((new Blockly.FieldColour("#000000")), "3_5")
  1411. .appendField((new Blockly.FieldColour("#000000")), "3_6")
  1412. .appendField(" ");
  1413. this.appendDummyInput()
  1414. .appendField(" ")
  1415. .appendField((new Blockly.FieldColour("#000000")), "2_1")
  1416. .appendField((new Blockly.FieldColour("#000000")), "2_2")
  1417. .appendField((new Blockly.FieldColour("#000000")), "2_3")
  1418. .appendField((new Blockly.FieldColour("#000000")), "2_4")
  1419. .appendField((new Blockly.FieldColour("#000000")), "2_5")
  1420. .appendField((new Blockly.FieldColour("#000000")), "2_6")
  1421. .appendField(" ");
  1422. this.appendDummyInput()
  1423. .appendField(" ")
  1424. .appendField((new Blockly.FieldColour("#000000")), "1_1")
  1425. .appendField((new Blockly.FieldColour("#000000")), "1_2")
  1426. .appendField((new Blockly.FieldColour("#000000")), "1_3")
  1427. .appendField((new Blockly.FieldColour("#000000")), "1_4")
  1428. .appendField((new Blockly.FieldColour("#000000")), "1_5")
  1429. .appendField((new Blockly.FieldColour("#000000")), "1_6")
  1430. .appendField(" ");
  1431. this.setPreviousStatement(true);
  1432. this.setNextStatement(true);
  1433. this.setColour(AILEDCOLOR);
  1434. this.setTooltip('');
  1435. this.setHelpUrl('');
  1436. }
  1437. };
  1438. var iot_led_pattern_6x6_length = 0;
  1439. Blockly.Python['iot_led_pattern_6x6'] = function (block) {
  1440. // TODO: Assemble Python into code variable.
  1441. var rgb = [];
  1442. iot_led_pattern_6x6_length = iot_led_pattern_6x6_length + 1;
  1443. for (var i = 6; i >= 1; i--) {
  1444. for (var j = 1; j < 7; j++) {
  1445. var color = block.getFieldValue(i + '_' + j);
  1446. var d = 0,
  1447. e = 0,
  1448. f = 0;
  1449. try {
  1450. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1451. e = parseInt(color.substring(3, 5), 16),
  1452. f = parseInt(color.substring(5, 7), 16))
  1453. } catch (g) { }
  1454. rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
  1455. }
  1456. }
  1457. Blockly.Python.codeFunctions_['iot_led_pattern_6x6_setup'] = '' +
  1458. 'def _iot_led_draw_picture(rgb):\n' +
  1459. ' for i in range(6, 0, -1):\n' +
  1460. ' for j in range(1, 7):\n' +
  1461. ' _6x6_led_matrix[(((i*6)//6)-1)*6+(j-1)] = (rgb[(((i*6)//6)-1)*6+(j-1)])\n' +
  1462. '\n';
  1463. Blockly.Python.codeFunctions_['iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + '_setup'] = '' +
  1464. 'iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ' = [' + rgb + ']' +
  1465. '\n';
  1466. var code = '_iot_led_draw_picture(iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ')\n';
  1467. return code;
  1468. };
  1469. Blockly.Blocks['iot_led_matrix_show_above'] = {
  1470. init: function () {
  1471. this.appendDummyInput()
  1472. .appendField(Blockly.Msg.x_led_matrix_show_above_iot);
  1473. this.setInputsInline(false);
  1474. this.setPreviousStatement(true, null);
  1475. this.setNextStatement(true, null);
  1476. this.setColour(AILEDCOLOR);
  1477. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP);
  1478. this.setHelpUrl("");
  1479. }
  1480. };
  1481. Blockly.Python['iot_led_matrix_show_above'] = function (block) {
  1482. // TODO: Assemble Python into code variable.
  1483. var code = '_6x6_led_matrix.write()\n';
  1484. return code;
  1485. };
  1486. Blockly.Blocks['iot_led_matrix_clear_screen'] = {
  1487. init: function () {
  1488. this.appendDummyInput()
  1489. .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
  1490. this.setInputsInline(false);
  1491. this.setPreviousStatement(true, null);
  1492. this.setNextStatement(true, null);
  1493. this.setColour(AILEDCOLOR);
  1494. this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
  1495. this.setHelpUrl("");
  1496. }
  1497. };
  1498. Blockly.Python['iot_led_matrix_clear_screen'] = function (block) {
  1499. // TODO: Assemble Python into code variable.
  1500. Blockly.Python.definitions_['iot_led_matrix_clear_screen_setup'] = '' +
  1501. 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1502. ' for i in range(x, x+ w, 1):\n' +
  1503. ' for j in range(y, y+h, 1):\n' +
  1504. ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
  1505. '\n';
  1506. var code = '' +
  1507. '_iot_led_draw_rectangle(1,1,6,6,rgba_to_rgb_conversion(0,0,0))\n' +
  1508. '_6x6_led_matrix.write()\n' +
  1509. '';
  1510. return code;
  1511. };
  1512. /*
  1513. _ _____ ____ ____ _ _
  1514. | | | ____| _ \ / ___|| |_ _ __(_)_ __
  1515. | | | _| | | | | \___ \| __| '__| | '_ \
  1516. | |___| |___| |_| | ___) | |_| | | | |_) |
  1517. |_____|_____|____/ |____/ \__|_| |_| .__/
  1518. |_|
  1519. */
  1520. var ESP32_LED_STRIP_COLOR = AILEDCOLOR;
  1521. Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
  1522. init: function () {
  1523. this.appendDummyInput()
  1524. .appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1525. this.appendDummyInput()
  1526. .appendField(Blockly.Msg.ledstrip_setup_text)
  1527. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1528. .appendField(Blockly.Msg.ledstrip_setup);
  1529. this.appendDummyInput()
  1530. .appendField(Blockly.Msg.ledstrip_set_gpio)
  1531. .appendField(new Blockly.FieldDropdown([
  1532. ["S1", "1"],
  1533. ["S2", "2"]
  1534. ]), "value_io");
  1535. this.appendValueInput("count")
  1536. .setCheck(null)
  1537. .appendField(Blockly.Msg.ledstrip_set_total_led);
  1538. this.setInputsInline(false);
  1539. this.setPreviousStatement(true, null);
  1540. this.setNextStatement(true, null);
  1541. this.setColour(ESP32_LED_STRIP_COLOR);
  1542. this.setHelpUrl("");
  1543. this.setTooltip();
  1544. }
  1545. };
  1546. Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
  1547. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1548. var value_io = block.getFieldValue('value_io');
  1549. var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1550. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1551. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1552. sys.path.append("/root/")`
  1553. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1554. Blockly.Python.addVariable(variable_name, `${variable_name} = multiFuncGpio(${value_io - 1},7)`, true);
  1555. var code = ""
  1556. return code;
  1557. };
  1558. Blockly.Blocks['esp32_main_controller_led_strip_set_bright'] = {
  1559. init: function () {
  1560. this.appendValueInput("brightness")
  1561. .setCheck(null)
  1562. .appendField(Blockly.Msg.ledstrip_set_brightness_1);
  1563. this.appendDummyInput()
  1564. .appendField(Blockly.Msg.ledstrip_set_brightness_after);
  1565. this.setInputsInline(true);
  1566. this.setPreviousStatement(true, null);
  1567. this.setNextStatement(true, null);
  1568. this.setColour(AILEDCOLOR);
  1569. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_strip_set_bright_TOOLTIP);
  1570. this.setHelpUrl("");
  1571. }
  1572. };
  1573. Blockly.Python['esp32_main_controller_led_strip_set_bright'] = function (block) {
  1574. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1575. // TODO: Assemble Python into code variable.
  1576. var code = '_ai_brightness = ' + value_brightness + '\n';
  1577. return code;
  1578. };
  1579. Blockly.Blocks['esp32_main_controller_led_strip_set'] = {
  1580. init: function () {
  1581. this.appendDummyInput()
  1582. .appendField(Blockly.Msg.ledstrip_set_light_up)
  1583. .appendField(new Blockly.FieldVariable("STRIP"), "varitem");
  1584. this.appendValueInput("location")
  1585. .setCheck(null)
  1586. .appendField(Blockly.Msg.ledstrip_set_no);
  1587. this.appendValueInput("color")
  1588. .setCheck(null)
  1589. .appendField(Blockly.Msg.ledstrip_set_color);
  1590. this.appendValueInput("brightness")
  1591. .setCheck(null)
  1592. .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
  1593. this.setInputsInline(false);
  1594. this.setPreviousStatement(true, null);
  1595. this.setNextStatement(true, null);
  1596. this.setColour(ESP32_LED_STRIP_COLOR);
  1597. this.setHelpUrl("");
  1598. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
  1599. }
  1600. };
  1601. Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
  1602. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1603. var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
  1604. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1605. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1606. Blockly.Python.addVariable("setLedColor",'setLedColor = ""',true)
  1607. // TODO: Assemble Python into code variable.
  1608. console.log(value_color)
  1609. var code = `${variable_name}.setBrightness(${value_brightness})
  1610. setLedColor = ${value_color}
  1611. ${variable_name}.setPixelColor(${value_location} ,setLedColor[0],setLedColor[1],setLedColor[2])
  1612. `
  1613. return code;
  1614. };
  1615. Blockly.Blocks['led_strip_show_above'] = {
  1616. init: function () {
  1617. this.appendDummyInput()
  1618. .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
  1619. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1620. .appendField(Blockly.Msg.ledstrip_setup_text_show_above);
  1621. this.setInputsInline(false);
  1622. this.setPreviousStatement(true, null);
  1623. this.setNextStatement(true, null);
  1624. this.setColour(AILEDCOLOR);
  1625. this.setHelpUrl("");
  1626. this.setTooltip(Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', "CocoPi"));
  1627. }
  1628. };
  1629. Blockly.Python['led_strip_show_above'] = function (block) {
  1630. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1631. // TODO: Assemble Python into code variable.
  1632. var code = `${variable_name}.pixelShow()
  1633. `
  1634. return code;
  1635. };
  1636. Blockly.Blocks['led_strip_color_rgb'] = {
  1637. init: function () {
  1638. this.appendDummyInput()
  1639. .appendField(Blockly.Msg.image_process_rgb_r);
  1640. this.appendValueInput("rgb_value_r")
  1641. .setCheck(null)
  1642. .appendField("");
  1643. this.appendDummyInput()
  1644. .appendField(Blockly.Msg.image_process_rgb_g);
  1645. this.appendValueInput("rgb_value_g")
  1646. .setCheck(null)
  1647. .appendField("");
  1648. this.appendDummyInput()
  1649. .appendField(Blockly.Msg.image_process_rgb_b);
  1650. this.appendValueInput("rgb_value_b")
  1651. .setCheck(null)
  1652. .appendField("");
  1653. this.setInputsInline(true);
  1654. this.setOutput(true, null);
  1655. this.setColour(AILEDCOLOR);
  1656. this.setTooltip(Blockly.Msg.Led_Strip_Color_Rgb_TOOLTIP);
  1657. this.setHelpUrl("");
  1658. }
  1659. };
  1660. Blockly.Python['led_strip_color_rgb'] = function (block) {
  1661. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1662. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1663. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1664. // TODO: Assemble Python into code variable.
  1665. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1666. // TODO: Change ORDER_NONE to the correct strength.
  1667. return [code, Blockly.Python.ORDER_NONE];
  1668. };
  1669. /*
  1670. ____ ____ _
  1671. / ___| __ _ _ __ ___ ___| _ \ __ _ __| |
  1672. | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` |
  1673. | |_| | (_| | | | | | | __/ __/ (_| | (_| |
  1674. \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_|
  1675. */
  1676. var GAMEPAD_COLOR = "#183895";
  1677. Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = {
  1678. init: function () {
  1679. this.appendDummyInput()
  1680. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1681. .appendField(new Blockly.FieldDropdown([
  1682. ["X", "x"],
  1683. ["Y", "y"],
  1684. ["A", "a"],
  1685. ["B", "b"],
  1686. ["L", "l"],
  1687. ["R", "r"]
  1688. ]), "type")
  1689. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot);
  1690. this.setInputsInline(true);
  1691. this.setOutput(true, null);
  1692. this.setColour(GAMEPAD_COLOR);
  1693. var thisBlock = this;
  1694. this.setTooltip(function () {
  1695. var mode = thisBlock.getFieldValue('type');
  1696. var TOOLTIPS = {
  1697. 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  1698. 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  1699. 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  1700. 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  1701. 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  1702. 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  1703. };
  1704. return TOOLTIPS[mode];
  1705. });
  1706. this.setHelpUrl("");
  1707. }
  1708. };
  1709. Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) {
  1710. var dropdown_type = block.getFieldValue('type');
  1711. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1712. 'from machine import ADC, Pin\n' +
  1713. 'import time\n' +
  1714. 'from machine import I2C\n' +
  1715. 'from ads1x15 import ADS1115\n' +
  1716. '\n';
  1717. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1718. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1719. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1720. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1721. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1722. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1723. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1724. '\n';
  1725. // TODO: Assemble Python into code variable.
  1726. var code = '_p' + dropdown_type + '.value() == 1';
  1727. // TODO: Change ORDER_NONE to the correct strength.
  1728. return [code, Blockly.Python.ORDER_NONE];
  1729. };
  1730. Blockly.Blocks['x_iot_read_gamepad_button_released'] = {
  1731. init: function () {
  1732. this.appendDummyInput()
  1733. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1734. .appendField(new Blockly.FieldDropdown([
  1735. ["X", "x"],
  1736. ["Y", "y"],
  1737. ["A", "a"],
  1738. ["B", "b"],
  1739. ["L", "l"],
  1740. ["R", "r"]
  1741. ]), "type")
  1742. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot);
  1743. this.setInputsInline(true);
  1744. this.setOutput(true, null);
  1745. this.setColour(GAMEPAD_COLOR);
  1746. var thisBlock = this;
  1747. this.setTooltip(function () {
  1748. var mode = thisBlock.getFieldValue('type');
  1749. var TOOLTIPS = {
  1750. 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  1751. 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  1752. 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  1753. 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  1754. 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  1755. 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  1756. };
  1757. return TOOLTIPS[mode];
  1758. });
  1759. this.setHelpUrl("");
  1760. }
  1761. };
  1762. Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) {
  1763. var dropdown_type = block.getFieldValue('type');
  1764. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1765. 'from machine import ADC, Pin\n' +
  1766. 'import time\n' +
  1767. 'from machine import I2C\n' +
  1768. 'from ads1x15 import ADS1115\n' +
  1769. '\n';
  1770. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1771. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1772. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1773. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1774. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1775. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1776. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1777. '\n';
  1778. // TODO: Assemble Python into code variable.
  1779. var code = '_p' + dropdown_type + '.value() == 0';
  1780. // TODO: Change ORDER_NONE to the correct strength.
  1781. return [code, Blockly.Python.ORDER_NONE];
  1782. };
  1783. Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = {
  1784. init: function () {
  1785. this.appendDummyInput()
  1786. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot)
  1787. .appendField(new Blockly.FieldDropdown([
  1788. ["X", "x"],
  1789. ["Y", "y"],
  1790. ["A", "a"],
  1791. ["B", "b"],
  1792. ["L", "l"],
  1793. ["R", "r"]
  1794. ]), "type")
  1795. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot);
  1796. this.setInputsInline(true);
  1797. this.setOutput(true, null);
  1798. this.setColour(GAMEPAD_COLOR);
  1799. var thisBlock = this;
  1800. this.setTooltip(function () {
  1801. var mode = thisBlock.getFieldValue('type');
  1802. var TOOLTIPS = {
  1803. 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  1804. 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  1805. 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  1806. 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  1807. 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  1808. 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  1809. };
  1810. return TOOLTIPS[mode];
  1811. });
  1812. this.setHelpUrl("");
  1813. }
  1814. };
  1815. Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) {
  1816. var dropdown_type = block.getFieldValue('type');
  1817. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1818. 'from machine import ADC, Pin\n' +
  1819. 'import time\n' +
  1820. 'from machine import I2C\n' +
  1821. 'from ads1x15 import ADS1115\n' +
  1822. '\n';
  1823. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1824. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1825. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1826. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1827. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1828. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1829. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1830. '\n';
  1831. // TODO: Assemble Python into code variable.
  1832. var code = '_p' + dropdown_type + '.value()';
  1833. // TODO: Change ORDER_NONE to the correct strength.
  1834. return [code, Blockly.Python.ORDER_NONE];
  1835. };
  1836. Blockly.Blocks['x_iot_read_gamepad_joystick'] = {
  1837. init: function () {
  1838. this.appendDummyInput()
  1839. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot)
  1840. .appendField(new Blockly.FieldDropdown([
  1841. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"],
  1842. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]
  1843. ]), "type")
  1844. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot);
  1845. this.setInputsInline(true);
  1846. this.setOutput(true, null);
  1847. this.setColour(GAMEPAD_COLOR);
  1848. var thisBlock = this;
  1849. this.setTooltip(function () {
  1850. var mode = thisBlock.getFieldValue('type');
  1851. var TOOLTIPS = {
  1852. 'X': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  1853. 'Y': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  1854. };
  1855. return TOOLTIPS[mode];
  1856. });
  1857. this.setHelpUrl("");
  1858. }
  1859. };
  1860. Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) {
  1861. var dropdown_type = block.getFieldValue('type');
  1862. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1863. 'from machine import ADC, Pin\n' +
  1864. 'import time\n' +
  1865. 'from machine import I2C\n' +
  1866. 'from ads1x15 import ADS1115\n' +
  1867. '\n';
  1868. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  1869. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1870. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1871. '\n';
  1872. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  1873. 'def map_number_scale(val):\n' +
  1874. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  1875. '\n' +
  1876. 'def _read_direction():\n' +
  1877. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  1878. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  1879. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  1880. ' return "still"\n' +
  1881. ' elif _adcX > 968:\n' +
  1882. ' return "left"\n' +
  1883. ' elif _adcX < 38:\n' +
  1884. ' return "right"\n' +
  1885. ' elif _adcY > 968:\n' +
  1886. ' return "top"\n' +
  1887. ' elif _adcY < 38:\n' +
  1888. ' return "bottom"\n' +
  1889. '\n';
  1890. // TODO: Assemble Python into code variable.
  1891. if (dropdown_type == "X") {
  1892. var code = 'map_number_scale(_joystick_adc.read(channel1=1))';
  1893. } else if (dropdown_type == "Y") {
  1894. var code = 'map_number_scale(_joystick_adc.read(channel1=0))';
  1895. }
  1896. // TODO: Change ORDER_NONE to the correct strength.
  1897. return [code, Blockly.Python.ORDER_NONE];
  1898. };
  1899. Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = {
  1900. init: function () {
  1901. this.appendDummyInput()
  1902. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot)
  1903. .appendField(new Blockly.FieldDropdown([
  1904. [Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"],
  1905. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"],
  1906. [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"],
  1907. [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"],
  1908. [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"],
  1909. ]), "type");
  1910. this.setInputsInline(true);
  1911. this.setOutput(true, null);
  1912. this.setColour(GAMEPAD_COLOR);
  1913. var thisBlock = this;
  1914. this.setTooltip(function () {
  1915. var mode = thisBlock.getFieldValue('type');
  1916. var TOOLTIPS = {
  1917. 'top': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_iot),
  1918. 'bottom': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot),
  1919. 'left': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_iot),
  1920. 'right': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_iot),
  1921. 'still': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_iot)
  1922. };
  1923. return TOOLTIPS[mode];
  1924. });
  1925. this.setHelpUrl("");
  1926. }
  1927. };
  1928. Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) {
  1929. var dropdown_type = block.getFieldValue('type');
  1930. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1931. 'from machine import ADC, Pin\n' +
  1932. 'import time\n' +
  1933. 'from machine import I2C\n' +
  1934. 'from ads1x15 import ADS1115\n' +
  1935. '\n';
  1936. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  1937. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1938. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1939. '\n';
  1940. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  1941. 'def map_number_scale(val):\n' +
  1942. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  1943. '\n' +
  1944. 'def _read_direction():\n' +
  1945. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  1946. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  1947. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  1948. ' return "still"\n' +
  1949. ' elif _adcX > 968:\n' +
  1950. ' return "left"\n' +
  1951. ' elif _adcX < 38:\n' +
  1952. ' return "right"\n' +
  1953. ' elif _adcY > 968:\n' +
  1954. ' return "top"\n' +
  1955. ' elif _adcY < 38:\n' +
  1956. ' return "bottom"\n' +
  1957. '\n';
  1958. // TODO: Assemble Python into code variable.
  1959. var code = '_read_direction() == "' + dropdown_type + '"';
  1960. // TODO: Change ORDER_NONE to the correct strength.
  1961. return [code, Blockly.Python.ORDER_NONE];
  1962. };
  1963. Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = {
  1964. init: function () {
  1965. this.appendDummyInput()
  1966. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  1967. .appendField(new Blockly.FieldDropdown([
  1968. ["X", "x"],
  1969. ["Y", "y"],
  1970. ["A", "a"],
  1971. ["B", "b"],
  1972. ["L", "l"],
  1973. ["R", "r"]
  1974. ]), "type")
  1975. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai);
  1976. this.setInputsInline(true);
  1977. this.setOutput(true, null);
  1978. this.setColour(GAMEPAD_COLOR);
  1979. var thisBlock = this;
  1980. this.setTooltip(function () {
  1981. var mode = thisBlock.getFieldValue('type');
  1982. var TOOLTIPS = {
  1983. 'x': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  1984. 'y': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  1985. 'a': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  1986. 'b': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  1987. 'l': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  1988. 'r': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  1989. };
  1990. return TOOLTIPS[mode];
  1991. });
  1992. this.setHelpUrl("");
  1993. }
  1994. };
  1995. Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) {
  1996. var dropdown_type = block.getFieldValue('type');
  1997. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  1998. 'from fpioa_manager import *\n' +
  1999. 'from Maix import FPIOA, GPIO\n' +
  2000. '\n';
  2001. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2002. '_gp_side_button = [13, 14]\n' +
  2003. '_gp_right_button = [22, 21, 15, 17]\n' +
  2004. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2005. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2006. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2007. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2008. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2009. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2010. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2011. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2012. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2013. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2014. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2015. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2016. '\n';
  2017. // TODO: Assemble Python into code variable.
  2018. var code = '_gp_button_' + dropdown_type + '.value() == 1';
  2019. // TODO: Change ORDER_NONE to the correct strength.
  2020. return [code, Blockly.Python.ORDER_NONE];
  2021. };
  2022. Blockly.Blocks['x_ai_read_gamepad_button_released'] = {
  2023. init: function () {
  2024. this.appendDummyInput()
  2025. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  2026. .appendField(new Blockly.FieldDropdown([
  2027. ["X", "x"],
  2028. ["Y", "y"],
  2029. ["A", "a"],
  2030. ["B", "b"],
  2031. ["L", "l"],
  2032. ["R", "r"]
  2033. ]), "type")
  2034. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai);
  2035. this.setInputsInline(true);
  2036. this.setOutput(true, null);
  2037. this.setColour(GAMEPAD_COLOR);
  2038. var thisBlock = this;
  2039. this.setTooltip(function () {
  2040. var mode = thisBlock.getFieldValue('type');
  2041. var TOOLTIPS = {
  2042. 'x': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  2043. 'y': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  2044. 'a': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  2045. 'b': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  2046. 'l': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  2047. 'r': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  2048. };
  2049. return TOOLTIPS[mode];
  2050. });
  2051. this.setHelpUrl("");
  2052. }
  2053. };
  2054. Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) {
  2055. var dropdown_type = block.getFieldValue('type');
  2056. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  2057. 'from fpioa_manager import *\n' +
  2058. 'from Maix import FPIOA, GPIO\n' +
  2059. '\n';
  2060. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2061. '_gp_side_button = [13, 14]\n' +
  2062. '_gp_right_button = [15, 17, 22, 21]\n' +
  2063. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2064. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2065. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2066. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2067. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2068. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2069. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2070. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2071. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2072. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2073. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2074. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2075. '\n';
  2076. // TODO: Assemble Python into code variable.
  2077. var code = '_gp_button_' + dropdown_type + '.value() == 0';
  2078. // TODO: Change ORDER_NONE to the correct strength.
  2079. return [code, Blockly.Python.ORDER_NONE];
  2080. };
  2081. Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = {
  2082. init: function () {
  2083. this.appendDummyInput()
  2084. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai)
  2085. .appendField(new Blockly.FieldDropdown([
  2086. ["X", "x"],
  2087. ["Y", "y"],
  2088. ["A", "a"],
  2089. ["B", "b"],
  2090. ["L", "l"],
  2091. ["R", "r"]
  2092. ]), "type")
  2093. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai);
  2094. this.setInputsInline(true);
  2095. this.setOutput(true, null);
  2096. this.setColour(GAMEPAD_COLOR);
  2097. var thisBlock = this;
  2098. this.setTooltip(function () {
  2099. var mode = thisBlock.getFieldValue('type');
  2100. var TOOLTIPS = {
  2101. 'x': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  2102. 'y': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  2103. 'a': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  2104. 'b': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  2105. 'l': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  2106. 'r': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  2107. };
  2108. return TOOLTIPS[mode];
  2109. });
  2110. this.setHelpUrl("");
  2111. }
  2112. };
  2113. Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) {
  2114. var dropdown_type = block.getFieldValue('type');
  2115. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  2116. 'from fpioa_manager import *\n' +
  2117. 'from Maix import FPIOA, GPIO\n' +
  2118. '\n';
  2119. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2120. '_gp_side_button = [13, 14]\n' +
  2121. '_gp_right_button = [15, 17, 22, 21]\n' +
  2122. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2123. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2124. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2125. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2126. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2127. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2128. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2129. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2130. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2131. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2132. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2133. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2134. '\n';
  2135. // TODO: Assemble Python into code variable.
  2136. var code = '_gp_button_' + dropdown_type + '.value()';
  2137. // TODO: Change ORDER_NONE to the correct strength.
  2138. return [code, Blockly.Python.ORDER_NONE];
  2139. };
  2140. Blockly.Blocks['x_ai_read_gamepad_joystick'] = {
  2141. init: function () {
  2142. this.appendDummyInput()
  2143. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai)
  2144. .appendField(new Blockly.FieldDropdown([
  2145. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"],
  2146. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]
  2147. ]), "type")
  2148. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai);
  2149. this.setInputsInline(true);
  2150. this.setOutput(true, null);
  2151. this.setColour(GAMEPAD_COLOR);
  2152. var thisBlock = this;
  2153. this.setTooltip(function () {
  2154. var mode = thisBlock.getFieldValue('type');
  2155. var TOOLTIPS = {
  2156. '0': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  2157. '1': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  2158. };
  2159. return TOOLTIPS[mode];
  2160. });
  2161. this.setHelpUrl("");
  2162. }
  2163. };
  2164. Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) {
  2165. var dropdown_type = block.getFieldValue('type');
  2166. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  2167. 'from machine import I2C\n' +
  2168. 'from ads1x15 import ADS1115\n' +
  2169. '\n' +
  2170. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  2171. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2172. '\n';
  2173. // TODO: Assemble Python into code variable.
  2174. var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)';
  2175. // TODO: Change ORDER_NONE to the correct strength.
  2176. return [code, Blockly.Python.ORDER_NONE];
  2177. };
  2178. Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = {
  2179. init: function () {
  2180. this.appendDummyInput()
  2181. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai)
  2182. .appendField(new Blockly.FieldDropdown([
  2183. [Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"],
  2184. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"],
  2185. [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"],
  2186. [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"],
  2187. [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"],
  2188. ]), "type");
  2189. this.setInputsInline(true);
  2190. this.setOutput(true, null);
  2191. this.setColour(GAMEPAD_COLOR);
  2192. var thisBlock = this;
  2193. this.setTooltip(function () {
  2194. var mode = thisBlock.getFieldValue('type');
  2195. var TOOLTIPS = {
  2196. 'top': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_ai),
  2197. 'bottom': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai),
  2198. 'left': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_ai),
  2199. 'right': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_ai),
  2200. 'still': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_ai)
  2201. };
  2202. return TOOLTIPS[mode];
  2203. });
  2204. this.setHelpUrl("");
  2205. }
  2206. };
  2207. Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) {
  2208. var dropdown_type = block.getFieldValue('type');
  2209. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  2210. 'from machine import I2C\n' +
  2211. 'from ads1x15 import ADS1115\n' +
  2212. '\n' +
  2213. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  2214. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2215. '\n';
  2216. // TODO: Assemble Python into code variable.
  2217. '\n';
  2218. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  2219. 'def _read_direction():\n' +
  2220. ' if (int(_joystick_adc.read(channel1=0)/25.75) > 400) and (int(_joystick_adc.read(channel1=0)/25.75) < 600) and (int(_joystick_adc.read(channel1=1)/25.75) > 400) and (int(_joystick_adc.read(channel1=1)/25.75) < 600):\n' +
  2221. ' return "still" \n' +
  2222. ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' +
  2223. ' return "left"\n' +
  2224. ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' +
  2225. ' return "right"\n' +
  2226. ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' +
  2227. ' return "top"\n' +
  2228. ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' +
  2229. ' return "bottom"\n' +
  2230. '\n';
  2231. // TODO: Assemble Python into code variable.
  2232. var code = '_read_direction() == "' + dropdown_type + '"';
  2233. // TODO: Change ORDER_NONE to the correct strength.
  2234. return [code, Blockly.Python.ORDER_NONE];
  2235. };
  2236. /*
  2237. __ ___ _____ _____
  2238. /\ \ \/ __\ \_ \___/__ \
  2239. / \/ /__\//_____ / /\/ _ \ / /\/
  2240. / /\ / \/ \_____/\/ /_| (_) / /
  2241. \_\ \/\_____/ \____/ \___/\/
  2242. Blockly.Python.addVariable('', '', true);
  2243. Blockly.Python.definitions_['definition'] = '';
  2244. Blockly.Python.addSetup("setup", '');
  2245. */
  2246. var ESP32_NBIOT_COLOR = "#629978";
  2247. Blockly.Blocks['esp32_nbiot_setup'] = {
  2248. init: function () {
  2249. let type = getLocalStorage("type")
  2250. this.appendDummyInput()
  2251. .appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, { alt: "*", flipRtl: "FALSE" }));
  2252. type == 1 ? this.appendDummyInput()
  2253. .appendField(new Blockly.FieldDropdown([
  2254. [Blockly.Msg.nbiot_module_which_module_ai, "ai"],
  2255. [Blockly.Msg.nbiot_module_which_module_iot, "iot"]
  2256. ]), "module_type") : this.appendDummyInput()
  2257. .appendField(new Blockly.FieldDropdown([
  2258. [Blockly.Msg.nbiot_module_which_module_iot, "iot"],
  2259. [Blockly.Msg.nbiot_module_which_module_ai, "ai"]
  2260. ]), "module_type");
  2261. this.appendDummyInput()
  2262. .appendField(Blockly.Msg.nbiot_module_init);
  2263. this.setPreviousStatement(true, null);
  2264. this.setNextStatement(true, null);
  2265. this.setColour(ESP32_NBIOT_COLOR);
  2266. var thisBlock = this;
  2267. this.setTooltip(function () {
  2268. var mode = thisBlock.getFieldValue('module_type');
  2269. var TOOLTIPS = {
  2270. 'ai': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_ai),
  2271. 'iot': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_iot)
  2272. };
  2273. return TOOLTIPS[mode];
  2274. });
  2275. this.setHelpUrl("");
  2276. }
  2277. };
  2278. Blockly.Python['esp32_nbiot_setup'] = function (block) {
  2279. // TODO: Assemble Python into code variable.
  2280. var dropdown_module_type = block.getFieldValue('module_type');
  2281. if (dropdown_module_type == "iot") {
  2282. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2283. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2284. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2285. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2286. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2287. Blockly.Python.definitions_['import_definition'] = '' +
  2288. 'from machine import UART\n' +
  2289. 'from machine import Timer\n' +
  2290. 'import ubinascii as binascii\n' +
  2291. 'import time\n' +
  2292. '\n' +
  2293. '_nbiot_serial = UART(2,rx=16, tx=17, baudrate=115200, timeout=10)\n' +
  2294. '\n' +
  2295. '_nbiot_serial_timer = Timer(1)\n' +
  2296. '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' +
  2297. '\n' +
  2298. 'def _read_nbiot_serial(uart):\n' +
  2299. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2300. ' if _nbiot_serial.any():\n' +
  2301. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2302. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2303. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2304. '\n' +
  2305. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2306. ' _nbiot_power_on_status = True\n' +
  2307. ' print("NB-IoT Module Initiated!\\n")\n' +
  2308. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2309. '\n' +
  2310. ' if "+IP" in _nbiot_serial_read:\n' +
  2311. ' _nbiot_internet_status = True\n' +
  2312. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2313. '';
  2314. var code = '';
  2315. } else if (dropdown_module_type == "ai") {
  2316. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2317. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2318. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2319. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2320. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2321. Blockly.Python.definitions_['import_definition'] = '' +
  2322. 'import machine, time\n' +
  2323. 'from fpioa_manager import fm\n' +
  2324. 'from machine import Timer\n' +
  2325. '\n' +
  2326. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  2327. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  2328. '\n' +
  2329. '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' +
  2330. '\n' +
  2331. 'def _on_timer(timer):\n' +
  2332. ' _read_nbiot_serial(_nbiot_serial)\n' +
  2333. '\n' +
  2334. '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' +
  2335. '_periodic_tim.start()\n' +
  2336. '\n' +
  2337. 'def _read_nbiot_serial(uart):\n' +
  2338. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2339. ' if _nbiot_serial.any():\n' +
  2340. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2341. ' print(_nbiot_serial_read)\n' +
  2342. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2343. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2344. '\n' +
  2345. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2346. ' _nbiot_power_on_status = True\n' +
  2347. ' print("NB-IoT Module Initiated!\\n")\n' +
  2348. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2349. '\n' +
  2350. ' if "+IP" in _nbiot_serial_read:\n' +
  2351. ' _nbiot_internet_status = True\n' +
  2352. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2353. ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' +
  2354. '\n' +
  2355. '_nbiot_serial_read = ""\n' +
  2356. '_nbiot_readings = ""\n' +
  2357. '_nbiot_power_on_status = False\n' +
  2358. '_nbiot_internet_status = False\n' +
  2359. '_nbiot_internet_ip_address = ""\n' +
  2360. '';
  2361. var code = '';
  2362. }
  2363. return code;
  2364. };
  2365. Blockly.Blocks['esp32_nbiot_get_power_status'] = {
  2366. init: function () {
  2367. this.appendDummyInput()
  2368. .appendField(Blockly.Msg.nbiot_module_power);
  2369. this.setOutput(true, null);
  2370. this.setColour(ESP32_NBIOT_COLOR);
  2371. this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
  2372. this.setHelpUrl("");
  2373. }
  2374. };
  2375. Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
  2376. // TODO: Assemble Python into code variable.
  2377. var code = '_nbiot_power_on_status';
  2378. // TODO: Change ORDER_NONE to the correct strength.
  2379. return [code, Blockly.Python.ORDER_NONE];
  2380. };
  2381. Blockly.Blocks['esp32_nbiot_get_network_status'] = {
  2382. init: function () {
  2383. this.appendDummyInput()
  2384. .appendField(Blockly.Msg.nbiot_module_connectivity);
  2385. this.setOutput(true, null);
  2386. this.setColour(ESP32_NBIOT_COLOR);
  2387. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_status_TOOLTIP);
  2388. this.setHelpUrl("");
  2389. }
  2390. };
  2391. Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
  2392. // TODO: Assemble Python into code variable.
  2393. var code = '_nbiot_internet_status';
  2394. // TODO: Change ORDER_NONE to the correct strength.
  2395. return [code, Blockly.Python.ORDER_NONE];
  2396. };
  2397. Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
  2398. init: function () {
  2399. this.appendDummyInput()
  2400. .appendField(Blockly.Msg.nbiot_module_ip);
  2401. this.setOutput(true, null);
  2402. this.setColour(ESP32_NBIOT_COLOR);
  2403. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_ip_TOOLTIP);
  2404. this.setHelpUrl("");
  2405. }
  2406. };
  2407. Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
  2408. // TODO: Assemble Python into code variable.
  2409. var code = '_nbiot_internet_ip_address';
  2410. // TODO: Change ORDER_NONE to the correct strength.
  2411. return [code, Blockly.Python.ORDER_NONE];
  2412. };
  2413. Blockly.Blocks['esp32_nbiot_onenet_init'] = {
  2414. init: function () {
  2415. this.appendDummyInput()
  2416. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, { alt: "*", flipRtl: "FALSE" }));
  2417. this.appendDummyInput()
  2418. .appendField(Blockly.Msg.nbiot_onenet_title);
  2419. this.appendValueInput("product_id")
  2420. .setCheck(null)
  2421. .appendField(Blockly.Msg.nbiot_onenet_connect_productid);
  2422. this.appendValueInput("device_id")
  2423. .setCheck(null)
  2424. .appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
  2425. this.appendValueInput("device_apikey")
  2426. .setCheck(null)
  2427. .appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
  2428. this.setPreviousStatement(true, null);
  2429. this.setNextStatement(true, null);
  2430. this.setColour(ONENET_BLOCK_COLOR);
  2431. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_init_TOOLTIP);
  2432. this.setHelpUrl("");
  2433. }
  2434. };
  2435. Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
  2436. var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
  2437. var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
  2438. var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC);
  2439. // TODO: Assemble Python into code variable.
  2440. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
  2441. Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
  2442. Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
  2443. Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
  2444. Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
  2445. Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
  2446. Blockly.Python.addSetup("setup", '' +
  2447. '_nbiot_onenet_init_state = False\n' +
  2448. '_nbiot_onenet_connection_status = False\n' +
  2449. '_nbiot_onenet_read_anything = False\n' +
  2450. '_nbiot_onenet_product_id = ' + value_product_id + '\n' +
  2451. '_nbiot_onenet_device_id = ' + value_device_id + '\n' +
  2452. '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' +
  2453. '\n' +
  2454. 'def _nbiot_send_onenet_data(data):\n' +
  2455. ' _nbiot_send_message = data\n' +
  2456. ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' +
  2457. ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' +
  2458. ' _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' +
  2459. ' return _nbiot_send_at_content\n' +
  2460. '\n' +
  2461. 'def _nbiot_read_onenet_data(raw_response):\n' +
  2462. ' _nbiot_receive = raw_response\n' +
  2463. ' if "+MQTTPUBLISH" not in raw_response:\n' +
  2464. ' return ""\n' +
  2465. ' elif "+MQTTPUBLISH" in raw_response:\n' +
  2466. ' _nbiot_occur = -1\n' +
  2467. ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' +
  2468. ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' +
  2469. '\n' +
  2470. 'def _read_nbiot_onenet(_incoming_data):\n' +
  2471. ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' +
  2472. '\n' +
  2473. ' if "+MQTTOPEN: OK" in _incoming_data:\n' +
  2474. ' _nbiot_onenet_connection_status = True\n' +
  2475. ' # print("OneNET Connected!")\n' +
  2476. ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' +
  2477. ' _nbiot_onenet_connection_status = False\n' +
  2478. ' # print("OneNET Lost Connection!")\n' +
  2479. '\n' +
  2480. ' if "+MQTTPUBLISH" in _incoming_data:\n' +
  2481. ' _nbiot_onenet_read_anything = True\n' +
  2482. ' else:\n' +
  2483. ' _nbiot_onenet_read_anything = False\n' +
  2484. '\n' +
  2485. ' if "+MQTTDISC" in _incoming_data:\n' +
  2486. ' _nbiot_onenet_read_anything = False\n' +
  2487. ' _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' +
  2488. ' print("Enabling OneNET Connection...")\n' +
  2489. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2490. ' time.sleep_ms(100)\n' +
  2491. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2492. '\n' +
  2493. 'def _init_onenet_connection():\n' +
  2494. ' _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' +
  2495. ' print("Enabling OneNET Connection...")\n' +
  2496. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2497. ' time.sleep_ms(100)\n' +
  2498. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2499. ' time.sleep_ms(100)\n' +
  2500. '');
  2501. var code = '' +
  2502. 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' +
  2503. ' _init_onenet_connection()\n' +
  2504. ' _nbiot_onenet_init_state = True\n' +
  2505. '\n' +
  2506. '_read_nbiot_onenet(_nbiot_readings)\n' +
  2507. '';
  2508. return code;
  2509. };
  2510. Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
  2511. init: function () {
  2512. this.appendDummyInput()
  2513. .appendField(Blockly.Msg.nbiot_onenet_connectivity);
  2514. this.setOutput(true, null);
  2515. this.setColour(ONENET_BLOCK_COLOR);
  2516. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_connection_status_TOOLTIP);
  2517. this.setHelpUrl("");
  2518. }
  2519. };
  2520. Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
  2521. // TODO: Assemble Python into code variable.
  2522. var code = '_nbiot_onenet_connection_status';
  2523. // TODO: Change ORDER_NONE to the correct strength.
  2524. return [code, Blockly.Python.ORDER_NONE];
  2525. };
  2526. Blockly.Blocks['iot_service_nbiot_onenet'] = {
  2527. init: function () {
  2528. this.appendDummyInput()
  2529. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, { alt: "*", flipRtl: "FALSE" }));
  2530. this.appendDummyInput()
  2531. .appendField(Blockly.Msg.nbiot_onenet_send_title);
  2532. /*this.appendValueInput("first_input")
  2533. .setCheck(null)
  2534. .appendField("Field 0");*/
  2535. this.itemCount_ = 1;
  2536. this.updateShape_();
  2537. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
  2538. this.setColour(ONENET_BLOCK_COLOR);
  2539. this.setPreviousStatement(true, null);
  2540. this.setNextStatement(true, null);
  2541. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_TOOLTIP);
  2542. this.setHelpUrl("");
  2543. },
  2544. mutationToDom: function () {
  2545. var container = document.createElement('mutation');
  2546. container.setAttribute('items', this.itemCount_);
  2547. return container;
  2548. },
  2549. domToMutation: function (xmlElement) {
  2550. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2551. this.updateShape_();
  2552. },
  2553. decompose: function (workspace) {
  2554. var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
  2555. containerBlock.initSvg();
  2556. var connection = containerBlock.getInput('STACK').connection;
  2557. for (var i = 0; i < this.itemCount_; i++) {
  2558. var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
  2559. itemBlock.initSvg();
  2560. connection.connect(itemBlock.previousConnection);
  2561. connection = itemBlock.nextConnection;
  2562. }
  2563. return containerBlock;
  2564. },
  2565. compose: function (containerBlock) {
  2566. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2567. // Count number of inputs.
  2568. var connections = [];
  2569. while (itemBlock) {
  2570. connections.push(itemBlock.valueConnection_);
  2571. itemBlock = itemBlock.nextConnection &&
  2572. itemBlock.nextConnection.targetBlock();
  2573. }
  2574. // Disconnect any children that don't belong.
  2575. for (var i = 0; i < this.itemCount_; i++) {
  2576. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2577. if (connection && connections.indexOf(connection) == -1) {
  2578. connection.disconnect();
  2579. }
  2580. }
  2581. this.itemCount_ = connections.length;
  2582. this.updateShape_();
  2583. // Reconnect any child blocks.
  2584. for (var i = 0; i < this.itemCount_; i++) {
  2585. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2586. }
  2587. },
  2588. saveConnections: function (containerBlock) {
  2589. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2590. var i = 0;
  2591. while (itemBlock) {
  2592. var input = this.getInput('ADD' + i);
  2593. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2594. i++;
  2595. itemBlock = itemBlock.nextConnection &&
  2596. itemBlock.nextConnection.targetBlock();
  2597. }
  2598. },
  2599. updateShape_: function () {
  2600. for (var i = 0; i < this.itemCount_; i++) {
  2601. if (!this.getInput('ADD' + i)) {
  2602. var input = this.appendValueInput('ADD' + i);
  2603. input.appendField(Blockly.Msg.nbiot_onenet_send_property)
  2604. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2605. //input.appendField("資料 " + (i + 1) + ":");
  2606. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2607. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2608. }
  2609. }
  2610. while (this.getInput('ADD' + i)) {
  2611. this.removeInput('ADD' + i);
  2612. i++;
  2613. }
  2614. },
  2615. };
  2616. Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
  2617. init: function () {
  2618. this.setColour(ONENET_BLOCK_COLOR);
  2619. this.appendDummyInput()
  2620. .appendField("Items");
  2621. this.appendStatementInput('STACK');
  2622. this.setTooltip('');
  2623. this.contextMenu = false;
  2624. }
  2625. };
  2626. Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
  2627. init: function () {
  2628. this.setColour(ONENET_BLOCK_COLOR);
  2629. this.appendDummyInput()
  2630. .appendField("Field");
  2631. this.setPreviousStatement(true);
  2632. this.setNextStatement(true);
  2633. this.setTooltip('');
  2634. this.contextMenu = false;
  2635. }
  2636. };
  2637. Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
  2638. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2639. var item_field = '',
  2640. item_value = '';
  2641. var nbiot_onenet_data = "'{";
  2642. for (var n = 0; n < this.itemCount_; n++) {
  2643. item_field = block.getFieldValue("field" + n);
  2644. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2645. nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2646. }
  2647. var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
  2648. var code = '' +
  2649. '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' +
  2650. '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' +
  2651. '_data_for_at = _data_add_rn.encode()\n' +
  2652. '_nbiot_serial.write(_data_for_at)\n' +
  2653. // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
  2654. '\n' +
  2655. '';
  2656. return code;
  2657. };
  2658. Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
  2659. init: function () {
  2660. this.appendDummyInput()
  2661. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
  2662. this.appendValueInput("message")
  2663. .setCheck(null);
  2664. this.appendDummyInput()
  2665. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
  2666. this.appendStatementInput("exec")
  2667. .setCheck(null)
  2668. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
  2669. this.setPreviousStatement(true, null);
  2670. this.setNextStatement(true, null);
  2671. this.setColour(ONENET_BLOCK_COLOR);
  2672. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_read_TOOLTIP);
  2673. this.setHelpUrl("");
  2674. }
  2675. };
  2676. Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
  2677. var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
  2678. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  2679. // TODO: Assemble Python into code variable.
  2680. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' +
  2681. statements_exec +
  2682. '\n';
  2683. return code;
  2684. };
  2685. Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
  2686. init: function () {
  2687. this.appendDummyInput()
  2688. .appendField(Blockly.Msg.nbiot_onenet_receive_command);
  2689. this.setOutput(true, null);
  2690. this.setColour(ONENET_BLOCK_COLOR);
  2691. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_read_value_TOOLTIP);
  2692. this.setHelpUrl("");
  2693. }
  2694. };
  2695. Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
  2696. // TODO: Assemble Python into code variable.
  2697. var code = '_nbiot_read_onenet_data(_nbiot_readings)';
  2698. // TODO: Change ORDER_NONE to the correct strength.
  2699. return [code, Blockly.Python.ORDER_NONE];
  2700. };
  2701. Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
  2702. init: function () {
  2703. this.appendDummyInput()
  2704. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2705. this.appendDummyInput()
  2706. .appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
  2707. this.appendDummyInput()
  2708. .appendField(Blockly.Msg.nbiot_cococloud_get_event_api)
  2709. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  2710. this.setPreviousStatement(true, null);
  2711. this.setNextStatement(true, null);
  2712. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2713. this.setTooltip("");
  2714. this.setHelpUrl("");
  2715. }
  2716. };
  2717. Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
  2718. var api = block.getFieldValue('t_api');
  2719. // TODO: Assemble Python into code variable.
  2720. var code = '\n';
  2721. return code;
  2722. };
  2723. Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
  2724. init: function () {
  2725. this.appendDummyInput()
  2726. .appendField(Blockly.Msg.nbiot_cococloud_get_property);
  2727. this.appendValueInput("property")
  2728. .setCheck(null);
  2729. this.appendDummyInput()
  2730. .appendField(Blockly.Msg.nbiot_cococloud_get_data);
  2731. this.setInputsInline(true);
  2732. this.setOutput(true, null);
  2733. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2734. this.setTooltip("");
  2735. this.setHelpUrl("");
  2736. }
  2737. };
  2738. Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
  2739. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  2740. // TODO: Assemble Python into code variable.
  2741. var code = '\n';
  2742. // TODO: Change ORDER_NONE to the correct strength.
  2743. return [code, Blockly.Python.ORDER_NONE];
  2744. };
  2745. Blockly.Blocks['iot_service_nbiot_cococloud'] = {
  2746. init: function () {
  2747. this.appendDummyInput()
  2748. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2749. this.appendDummyInput()
  2750. .appendField(Blockly.Msg.nbiot_cococloud_send_title);
  2751. this.appendDummyInput()
  2752. .appendField(Blockly.Msg.nbiot_cococloud_send_api)
  2753. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  2754. /*this.appendValueInput("first_input")
  2755. .setCheck(null)
  2756. .appendField("Field 0");*/
  2757. this.itemCount_ = 1;
  2758. this.updateShape_();
  2759. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
  2760. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2761. this.setPreviousStatement(true, null);
  2762. this.setNextStatement(true, null);
  2763. this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
  2764. this.setHelpUrl("");
  2765. },
  2766. mutationToDom: function () {
  2767. var container = document.createElement('mutation');
  2768. container.setAttribute('items', this.itemCount_);
  2769. return container;
  2770. },
  2771. domToMutation: function (xmlElement) {
  2772. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2773. this.updateShape_();
  2774. },
  2775. decompose: function (workspace) {
  2776. var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
  2777. containerBlock.initSvg();
  2778. var connection = containerBlock.getInput('STACK').connection;
  2779. for (var i = 0; i < this.itemCount_; i++) {
  2780. var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
  2781. itemBlock.initSvg();
  2782. connection.connect(itemBlock.previousConnection);
  2783. connection = itemBlock.nextConnection;
  2784. }
  2785. return containerBlock;
  2786. },
  2787. compose: function (containerBlock) {
  2788. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2789. // Count number of inputs.
  2790. var connections = [];
  2791. while (itemBlock) {
  2792. connections.push(itemBlock.valueConnection_);
  2793. itemBlock = itemBlock.nextConnection &&
  2794. itemBlock.nextConnection.targetBlock();
  2795. }
  2796. // Disconnect any children that don't belong.
  2797. for (var i = 0; i < this.itemCount_; i++) {
  2798. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2799. if (connection && connections.indexOf(connection) == -1) {
  2800. connection.disconnect();
  2801. }
  2802. }
  2803. this.itemCount_ = connections.length;
  2804. this.updateShape_();
  2805. // Reconnect any child blocks.
  2806. for (var i = 0; i < this.itemCount_; i++) {
  2807. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2808. }
  2809. },
  2810. saveConnections: function (containerBlock) {
  2811. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2812. var i = 0;
  2813. while (itemBlock) {
  2814. var input = this.getInput('ADD' + i);
  2815. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2816. i++;
  2817. itemBlock = itemBlock.nextConnection &&
  2818. itemBlock.nextConnection.targetBlock();
  2819. }
  2820. },
  2821. updateShape_: function () {
  2822. for (var i = 0; i < this.itemCount_; i++) {
  2823. if (!this.getInput('ADD' + i)) {
  2824. var input = this.appendValueInput('ADD' + i);
  2825. input.appendField(Blockly.Msg.nbiot_cococloud_send_property)
  2826. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2827. //input.appendField("資料 " + (i + 1) + ":");
  2828. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2829. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2830. }
  2831. }
  2832. while (this.getInput('ADD' + i)) {
  2833. this.removeInput('ADD' + i);
  2834. i++;
  2835. }
  2836. },
  2837. };
  2838. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
  2839. init: function () {
  2840. this.setColour(ESP32_NBIOT_COLOR);
  2841. this.appendDummyInput()
  2842. .appendField("Items");
  2843. this.appendStatementInput('STACK');
  2844. this.setTooltip('');
  2845. this.contextMenu = false;
  2846. }
  2847. };
  2848. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
  2849. init: function () {
  2850. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2851. this.appendDummyInput()
  2852. .appendField("Field");
  2853. this.setPreviousStatement(true);
  2854. this.setNextStatement(true);
  2855. this.setTooltip('');
  2856. this.contextMenu = false;
  2857. }
  2858. };
  2859. Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
  2860. var api = block.getFieldValue('t_api');
  2861. Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
  2862. Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
  2863. Blockly.Python.definitions_['import'] = '' +
  2864. '_nbiot_cococloud_send_init_state = False\n' +
  2865. '\n' +
  2866. 'def _nbiot_cococloud_send(data):\n' +
  2867. ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' +
  2868. ' print(_nbiot_cococloud_send_data)\n' +
  2869. ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' +
  2870. ' time.sleep_ms(500)\n' +
  2871. ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.hk/\"\\r\\n\')\n' +
  2872. ' time.sleep_ms(500)\n' +
  2873. ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' +
  2874. ' time.sleep_ms(500)\n' +
  2875. ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' +
  2876. ' time.sleep_ms(500)\n' +
  2877. ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' +
  2878. ' time.sleep_ms(500)\n' +
  2879. '';
  2880. var item_field = '',
  2881. item_value = '';
  2882. var nbiot_cococloud_data = "'{";
  2883. for (var n = 0; n < this.itemCount_; n++) {
  2884. item_field = block.getFieldValue("field" + n);
  2885. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2886. nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2887. }
  2888. var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
  2889. var code = '' +
  2890. '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  2891. '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' +
  2892. '';
  2893. return code;
  2894. };
  2895. Blockly.Blocks['3rd_party_sensor_hcsr04'] = {
  2896. init: function () {
  2897. let types = getLocalStorage("type")
  2898. this.appendDummyInput()
  2899. .appendField(new Blockly.FieldImage("blockly/media/ultrasonic_setup_header.png", 120, 40, { alt: "*", flipRtl: "FALSE" }));
  2900. types == 1 ? this.appendDummyInput()
  2901. .appendField(new Blockly.FieldDropdown([
  2902. [Blockly.Msg.third_party_hcsr04_ai, "ai"],
  2903. [Blockly.Msg.third_party_hcsr04_iot, "iot"]
  2904. ]), "type")
  2905. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  2906. .appendField(new Blockly.FieldDropdown([
  2907. ["1", "1"],
  2908. ["2", "2"],
  2909. ["3", "3"]
  2910. ]), "index") : this.appendDummyInput()
  2911. .appendField(new Blockly.FieldDropdown([
  2912. [Blockly.Msg.third_party_hcsr04_iot, "iot"],
  2913. [Blockly.Msg.third_party_hcsr04_ai, "ai"]
  2914. ]), "type")
  2915. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  2916. .appendField(new Blockly.FieldDropdown([
  2917. ["1", "1"],
  2918. ["2", "2"],
  2919. ["3", "3"]
  2920. ]), "index");
  2921. types == 1 ? this.appendDummyInput()
  2922. .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
  2923. .appendField(new Blockly.FieldNumber(2, 0, 40, 1), "trig")
  2924. .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
  2925. .appendField(new Blockly.FieldNumber(3, 0, 40, 1), "echo") :
  2926. this.appendDummyInput()
  2927. .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
  2928. .appendField(new Blockly.FieldNumber(12, 0, 40, 1), "trig")
  2929. .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
  2930. .appendField(new Blockly.FieldNumber(13, 0, 40, 1), "echo");
  2931. this.setPreviousStatement(true, null);
  2932. this.setNextStatement(true, null);
  2933. this.setColour("#d72bd5");
  2934. this.setHelpUrl("");
  2935. var thisBlock = this;
  2936. this.setTooltip(function () {
  2937. var mode = thisBlock.getFieldValue('type');
  2938. var TOOLTIPS = {
  2939. 'ai': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_ai),
  2940. 'iot': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_iot)
  2941. };
  2942. return TOOLTIPS[mode];
  2943. });
  2944. }
  2945. };
  2946. Blockly.Python['3rd_party_sensor_hcsr04'] = function (block) {
  2947. var dropdown_type = block.getFieldValue('type');
  2948. var dropdown_index = block.getFieldValue('index');
  2949. var number_trig = block.getFieldValue('trig');
  2950. var number_echo = block.getFieldValue('echo');
  2951. // TODO: Assemble Python into code variable.
  2952. if (dropdown_type == "iot") {
  2953. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  2954. 'from HCSR04 import HCSR04\n' +
  2955. 'from machine import Pin\n' +
  2956. 'import time\n' +
  2957. '\n' +
  2958. '_ultrasonic_sensor_trig_' + number_trig + ' = Pin(' + number_trig + ',Pin.OUT)\n' +
  2959. '_ultrasonic_sensor_echo_' + number_echo + ' = Pin(' + number_echo + ',Pin.IN)\n' +
  2960. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  2961. '\n';
  2962. var code = '';
  2963. } else if (dropdown_type == "ai") {
  2964. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  2965. 'from HCSR04 import HCSR04\n' +
  2966. 'from fpioa_manager import *\n' +
  2967. 'from Maix import GPIO\n' +
  2968. 'import time\n' +
  2969. '\n' +
  2970. 'fm.register(' + number_trig + ',fm.fpioa.GPIOHS' + number_trig + ')\n' +
  2971. 'fm.register(' + number_echo + ',fm.fpioa.GPIOHS' + number_echo + ')\n' +
  2972. '\n' +
  2973. '_ultrasonic_sensor_trig_' + number_trig + ' = GPIO(GPIO.GPIOHS' + number_trig + ', GPIO.OUT)\n' +
  2974. '_ultrasonic_sensor_echo_' + number_echo + ' = GPIO(GPIO.GPIOHS' + number_echo + ', GPIO.IN)\n' +
  2975. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  2976. '\n';
  2977. var code = '';
  2978. }
  2979. return code;
  2980. };
  2981. Blockly.Blocks['3rd_party_sensor_hcsr04_read'] = {
  2982. init: function () {
  2983. this.appendDummyInput()
  2984. .appendField(Blockly.Msg.third_party_hcsr04_read_text)
  2985. .appendField(new Blockly.FieldDropdown([
  2986. ["1", "1"],
  2987. ["2", "2"],
  2988. ["3", "3"]
  2989. ]), "index")
  2990. .appendField(Blockly.Msg.third_party_hcsr04_read_text_after);
  2991. this.setOutput(true, null);
  2992. this.setColour("#d72bd5");
  2993. this.setTooltip(Blockly.Msg.Trd_Party_Sensor_Hcsr04_Read_TOOLTIP);
  2994. this.setHelpUrl("");
  2995. }
  2996. };
  2997. Blockly.Python['3rd_party_sensor_hcsr04_read'] = function (block) {
  2998. var dropdown_index = block.getFieldValue('index');
  2999. // TODO: Assemble Python into code variable.
  3000. var code = '_ultrasonic_sensor_read_' + dropdown_index + '.getDistance()';
  3001. // TODO: Change ORDER_NONE to the correct strength.
  3002. return [code, Blockly.Python.ORDER_NONE];
  3003. };
  3004. Blockly.Blocks['Pedometer_Init'] = {
  3005. init: function () {
  3006. this.appendDummyInput()
  3007. .appendField(Blockly.Msg.Pedometer_Init);
  3008. this.appendValueInput("time")
  3009. .setCheck(null)
  3010. .appendField(Blockly.Msg.Pedometer_Init_time);
  3011. this.appendValueInput("sensitivity")
  3012. .setCheck(null)
  3013. .appendField(Blockly.Msg.Pedometer_Init_sensitivity);
  3014. this.setInputsInline(false);
  3015. this.setPreviousStatement(true, null);
  3016. this.setNextStatement(true, null);
  3017. this.setColour("#d42b03");
  3018. this.setTooltip(Blockly.Msg.Pedometer_Init_TOOLTIP);
  3019. this.setHelpUrl("");
  3020. }
  3021. };
  3022. Blockly.Python['Pedometer_Init'] = function (block) {
  3023. var time = Blockly.Python.valueToCode(block, 'time', Blockly.Python.ORDER_ATOMIC);
  3024. var sensitivity = Blockly.Python.valueToCode(block, 'sensitivity', Blockly.Python.ORDER_ATOMIC);
  3025. Blockly.Python.definitions_['Pedometer_Init'] = `from machine import I2C, Pin
  3026. import math
  3027. import QMI8658
  3028. mpu_lib_version_is_latest = "QMI8658" in dir(QMI8658)
  3029. if mpu_lib_version_is_latest == True:
  3030. mpui2c = I2C(scl=Pin(22), sda=Pin(21))
  3031. QMI8658 = QMI8658.QMI8658(mpui2c)
  3032. mpucal = QMI8658.calibrate()
  3033. elif mpu_lib_version_is_latest == False:
  3034. #申明QMI8658传感器IIC通信引脚
  3035. _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))
  3036. _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
  3037. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  3038. _MPU_ACCL_X = _MPU.get_values()['AcX']
  3039. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  3040. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  3041. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  3042. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  3043. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  3044. threshold_time=`+ time + ` #时间筛选阈值
  3045. threshold_hill=`+ sensitivity + ` #波峰波谷差值阈值
  3046. xval=[None]*100 #初始X轴加速度原始数据采集列表
  3047. yval=[None]*100 #初始Y轴加速度原始数据采集列表
  3048. zval=[None]*100 #初始Z轴加速度原始数据采集列表
  3049. xavg=0 #X轴方向初始加速度平均值
  3050. yavg=0 #Y轴方向初始加速度平均值
  3051. zavg=0 #Z轴方向初始加速度平均值
  3052. xaccl=[None]*100 #X轴加速度实时采集列表
  3053. yaccl=[None]*100 #Y轴加速度实时采集列表
  3054. zaccl=[None]*100 #Z轴加速度实时采集列表
  3055. totvect=[0.0]*100 #三个方向总加速度矢量值
  3056. totave=[0.0]*100 #最大和最小加速度矢量值平均值
  3057. peak=0 #波峰数据
  3058. trough=0 #波谷数据
  3059. data=[0]*15 #实时采集数据滚动递进列表
  3060. steps=0 #行走步伐数
  3061. flag0=0 #步数提取标志位
  3062. flag1=0 #波峰提取标志位
  3063. flag2=0 #波谷提取标志位
  3064. hill=0 #波峰波谷差值
  3065. starttime=0 #波峰时间点
  3066. endtime=0 #波谷时间点
  3067. lasttime=0 #波峰波谷间隔时间
  3068. #获取三个方向加速度函数
  3069. def QMI8658_get_accel(type):
  3070. _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
  3071. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  3072. _MPU_ACCL_X = _MPU.get_values()['AcX']
  3073. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  3074. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  3075. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  3076. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  3077. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  3078. if type == 'X':
  3079. return _MPU_ACCL_X
  3080. if type == 'Y':
  3081. return _MPU_ACCL_Y
  3082. if type == 'Z':
  3083. return _MPU_ACCL_Z
  3084. #初始化校准函数
  3085. def calibration():
  3086. sum0=0 #x轴加速度累计值
  3087. sum1=0 #y轴加速度累计值
  3088. sum2=0 #z轴加速度累计值
  3089. for i in range(100):
  3090. xval[i]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
  3091. sum0=xval[i]+sum0
  3092. time.sleep_ms(100)
  3093. xavg=sum0/100
  3094. #print(xavg)
  3095. for i in range(100):
  3096. yval[i]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
  3097. sum1=yval[i]+sum1
  3098. time.sleep_ms(100)
  3099. yavg=sum1/100
  3100. #print(yavg)
  3101. for i in range(100):
  3102. zval[i]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
  3103. sum2=zval[i]+sum2
  3104. time.sleep_ms(100)
  3105. zavg=sum2/100
  3106. #print(zavg)
  3107. def getPedometer():
  3108. global q, xaccl, yaccl, zaccl, totvect, totave, data, peak, flag0, flag1, flag2, starttime, lasttime, endtime, hill, trough, threshold_hill, hill, threshold_time, steps
  3109. for a in range(100):
  3110. xaccl[a]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
  3111. time.sleep_ms(1)
  3112. yaccl[a]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
  3113. time.sleep_ms(1)
  3114. zaccl[a]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
  3115. time.sleep_ms(1)
  3116. #计算X、Y、Z轴三个方向加速度值的平方根来计算总加速度矢量
  3117. totvect[a]=math.sqrt(((xaccl[a]-xavg)*(xaccl[a]-xavg))+((yaccl[a]-yavg)*(yaccl[a]-yavg))+((zaccl[a]-zavg)*(zaccl[a]-zavg)))
  3118. totave[a]=(totvect[a]+totave[a-1])/2
  3119. q=totave[a]
  3120. #数据轮替
  3121. data[2]=data[1]
  3122. data[1]=data[0]
  3123. data[0]=totave[a]
  3124. #检测是否到达一个波峰,如果是,将波峰数据采集标志位置1,并开始计时
  3125. if ((data[1]>data[0])&(data[1]>data[2])&(flag1==0)):
  3126. peak=data[1]
  3127. flag1=1
  3128. starttime=time.ticks_ms()
  3129. #检测是否到达一个波谷,如果是,将波谷数据采集标志位置1,并读取当前时间
  3130. if ((data[1]<data[0])&(data[1]<data[2])&(flag2==0)):
  3131. trough=data[1]
  3132. flag2=1
  3133. endtime=time.ticks_ms()
  3134. #如果采集到一组波峰波谷数据
  3135. if((flag1==1)&(flag2==1)):
  3136. flag1=0 #将波峰数据采集标志位置0
  3137. flag2=0 #将波谷数据采集标指位置1
  3138. hill=peak-trough #计算波峰波谷差值
  3139. lasttime=endtime-starttime #计算波峰波谷间隔时间
  3140. if(hill>threshold_hill)&(lasttime>threshold_time): #如果波峰波谷差值大于threshold_hill且间隔时间大于threshold_time
  3141. steps=steps+1 #步数加一
  3142. flag0=1 #将步数数据提取标志位置1
  3143. lasttime=0 #将波峰波谷间隔时间清零
  3144. hill=0 #将波峰波谷差值清零
  3145. time.sleep_ms(20)
  3146. return steps
  3147. `;
  3148. var code = "";
  3149. return code;
  3150. };
  3151. Blockly.Blocks['Pedometer_Run'] = {
  3152. init: function () {
  3153. this.appendDummyInput()
  3154. .appendField(Blockly.Msg.Pedometer_Run);
  3155. this.setInputsInline(false);
  3156. this.setPreviousStatement(true, null);
  3157. this.setNextStatement(true, null);
  3158. this.setColour("#d42b03");
  3159. this.setTooltip(Blockly.Msg.Pedometer_Run_TOOLTIP);
  3160. this.setHelpUrl("");
  3161. }
  3162. };
  3163. Blockly.Python['Pedometer_Run'] = function (block) {
  3164. var code = `
  3165. calibration()
  3166. `;
  3167. return code;
  3168. };
  3169. Blockly.Blocks['Pedometer_Get'] = {
  3170. init: function () {
  3171. this.appendDummyInput()
  3172. .appendField(Blockly.Msg.Pedometer_Get);
  3173. this.setInputsInline(true);
  3174. this.setOutput(true, null);
  3175. this.setColour("#d42b03");
  3176. this.setTooltip(Blockly.Msg.Pedometer_Get_TOOLTIP);
  3177. this.setHelpUrl("");
  3178. }
  3179. };
  3180. Blockly.Python['Pedometer_Get'] = function (block) {
  3181. var code = `getPedometer()`;
  3182. return [code, Blockly.Python.ORDER_NONE];
  3183. };
  3184. /* mlx90614 */
  3185. Blockly.Blocks['mlx90614_setup'] = {
  3186. init: function () {
  3187. this.appendDummyInput()
  3188. .appendField(new Blockly.FieldImage("blockly/media/body_temp_mlx.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
  3189. let types = getLocalStorage("type")
  3190. types == 1 ? this.appendDummyInput()
  3191. .appendField("")
  3192. .appendField(new Blockly.FieldDropdown([
  3193. [Blockly.Msg.time_ai_module, "2"],
  3194. [Blockly.Msg.time_iot_module, "1"]
  3195. ]), "digital_get_type")
  3196. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1) :
  3197. this.appendDummyInput()
  3198. .appendField("")
  3199. .appendField(new Blockly.FieldDropdown([
  3200. [Blockly.Msg.time_iot_module, "1"],
  3201. [Blockly.Msg.time_ai_module, "2"]
  3202. ]), "digital_get_type")
  3203. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1);
  3204. this.appendDummyInput()
  3205. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_2);
  3206. this.setPreviousStatement(true, null);
  3207. this.setNextStatement(true, null);
  3208. this.setInputsInline(false);
  3209. this.setColour("#5c42bd");
  3210. this.setHelpUrl("");
  3211. var thisBlock = this;
  3212. this.setTooltip(function () {
  3213. var mode = thisBlock.getFieldValue('digital_get_type');
  3214. var TOOLTIPS = {
  3215. '2': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3216. '1': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3217. };
  3218. return TOOLTIPS[mode];
  3219. });
  3220. }
  3221. };
  3222. Blockly.Python['mlx90614_setup'] = function (block) {
  3223. var dropdown_type = block.getFieldValue('digital_get_type');
  3224. if (dropdown_type == "1") {
  3225. Blockly.Python.definitions_['import_iot_mlx90614_setup'] = '' +
  3226. 'import mlx90614\n' +
  3227. 'from machine import I2C, Pin\n' +
  3228. 'import time\n' +
  3229. '\n' +
  3230. 'def mlx90614_init():\n' +
  3231. ' global sensor_mlx90614\n' +
  3232. ' i2c = I2C(scl=Pin(22), sda=Pin(21),freq=100000)\n' +
  3233. ' while True:\n' +
  3234. ' init_success = False\n' +
  3235. ' try:\n' +
  3236. ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
  3237. ' init_success = True\n' +
  3238. ' except BaseException as e:\n' +
  3239. ' print(e)\n' +
  3240. ' if init_success == True:\n' +
  3241. ' break\n' +
  3242. '\n' +
  3243. 'def mlx90614_read(datatype):\n' +
  3244. ' read_from_mlx90614 = None\n' +
  3245. ' try:\n' +
  3246. ' if datatype == "read_ambient_tempC":\n' +
  3247. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
  3248. ' elif datatype == "read_ambient_tempF":\n' +
  3249. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
  3250. ' elif datatype == "read_object_tempC":\n' +
  3251. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
  3252. ' elif datatype == "read_object_tempF":\n' +
  3253. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
  3254. ' except BaseException as e:\n' +
  3255. ' read_from_mlx90614 = None\n' +
  3256. ' print(e)\n' +
  3257. ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
  3258. ' # print(type(read_from_mlx90614))\n' +
  3259. ' return read_from_mlx90614\n' +
  3260. '\n';
  3261. var code = 'mlx90614_init()\n';
  3262. } else if (dropdown_type == "2") {
  3263. Blockly.Python.definitions_['import_ai_mlx90614_setup'] = '' +
  3264. 'import mlx90614\n' +
  3265. 'from machine import I2C\n' +
  3266. 'import time\n' +
  3267. '\n' +
  3268. 'def mlx90614_init():\n' +
  3269. ' global sensor_mlx90614\n' +
  3270. ' i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  3271. ' while True:\n' +
  3272. ' init_success = False\n' +
  3273. ' try:\n' +
  3274. ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
  3275. ' init_success = True\n' +
  3276. ' except BaseException as e:\n' +
  3277. ' print(e)\n' +
  3278. ' if init_success == True:\n' +
  3279. ' break\n' +
  3280. '\n' +
  3281. 'def mlx90614_read(datatype):\n' +
  3282. ' read_from_mlx90614 = None\n' +
  3283. ' try:\n' +
  3284. ' if datatype == "read_ambient_tempC":\n' +
  3285. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
  3286. ' elif datatype == "read_ambient_tempF":\n' +
  3287. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
  3288. ' elif datatype == "read_object_tempC":\n' +
  3289. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
  3290. ' elif datatype == "read_object_tempF":\n' +
  3291. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
  3292. ' except BaseException as e:\n' +
  3293. ' read_from_mlx90614 = None\n' +
  3294. ' print(e)\n' +
  3295. ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
  3296. ' # print(type(read_from_mlx90614))\n' +
  3297. ' return read_from_mlx90614\n' +
  3298. '\n';
  3299. var code = 'mlx90614_init()\n';
  3300. }
  3301. return code;
  3302. };
  3303. Blockly.Blocks['mlx90614_read_ambient'] = {
  3304. init: function () {
  3305. this.appendDummyInput()
  3306. .appendField(Blockly.Msg.MLX90614_READ_AMBIENT);
  3307. this.appendDummyInput()
  3308. .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
  3309. .appendField(new Blockly.FieldDropdown([
  3310. [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
  3311. [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
  3312. ]), "type");
  3313. this.setInputsInline(false);
  3314. this.setOutput(true, null);
  3315. this.setColour("#5c42bd");
  3316. this.setTooltip(Blockly.Msg.MLX90614_READ_AMBIENT_TOOLTIP);
  3317. this.setHelpUrl("");
  3318. }
  3319. };
  3320. Blockly.Python['mlx90614_read_ambient'] = function (block) {
  3321. var dropdown_type = block.getFieldValue('type');
  3322. if (dropdown_type == "C") {
  3323. var code = `mlx90614_read("read_ambient_tempC")`;
  3324. } else if (dropdown_type == "F") {
  3325. var code = `mlx90614_read("read_ambient_tempF")`;
  3326. }
  3327. return [code, Blockly.Python.ORDER_NONE];
  3328. };
  3329. Blockly.Blocks['mlx90614_read_object'] = {
  3330. init: function () {
  3331. this.appendDummyInput()
  3332. .appendField(Blockly.Msg.MLX90614_READ_OBJECT);
  3333. this.appendDummyInput()
  3334. .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
  3335. .appendField(new Blockly.FieldDropdown([
  3336. [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
  3337. [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
  3338. ]), "type");
  3339. this.setInputsInline(false);
  3340. this.setOutput(true, null);
  3341. this.setColour("#5c42bd");
  3342. this.setTooltip(Blockly.Msg.MLX90614_READ_OBJECT_TOOLTIP);
  3343. this.setHelpUrl("");
  3344. }
  3345. };
  3346. Blockly.Python['mlx90614_read_object'] = function (block) {
  3347. var dropdown_type = block.getFieldValue('type');
  3348. if (dropdown_type == "C") {
  3349. var code = `mlx90614_read("read_object_tempC")`;
  3350. } else if (dropdown_type == "F") {
  3351. var code = `mlx90614_read("read_object_tempF")`;
  3352. }
  3353. return [code, Blockly.Python.ORDER_NONE];
  3354. };
  3355. /* DHT11 or DHT22 */
  3356. Blockly.Blocks['DHT_setup'] = {
  3357. init: function () {
  3358. this.appendDummyInput()
  3359. .appendField(new Blockly.FieldImage("blockly/media/dht_header.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
  3360. let types = getLocalStorage("type")
  3361. types == 1 ? this.appendDummyInput()
  3362. .appendField("")
  3363. .appendField(new Blockly.FieldDropdown([
  3364. [Blockly.Msg.time_ai_module, "2"],
  3365. [Blockly.Msg.time_iot_module, "1"]
  3366. ]), "digital_get_type")
  3367. .appendField(Blockly.Msg.DHT_SETUP_TEXT_1) :
  3368. this.appendDummyInput()
  3369. .appendField("")
  3370. .appendField(new Blockly.FieldDropdown([
  3371. [Blockly.Msg.time_iot_module, "1"],
  3372. [Blockly.Msg.time_ai_module, "2"]
  3373. ]), "digital_get_type")
  3374. .appendField(Blockly.Msg.DHT_SETUP_TEXT_1);
  3375. this.appendDummyInput()
  3376. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3377. .appendField(new Blockly.FieldDropdown([
  3378. ["DHT11", "1"],
  3379. ["DHT22", "2"]
  3380. ]), "type")
  3381. .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
  3382. types == 1 ? this.appendDummyInput()
  3383. .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
  3384. .appendField(new Blockly.FieldDropdown([
  3385. ["1", "0"],
  3386. ["2", "1"],
  3387. ["3", "2"],
  3388. ["13", "3"],
  3389. ["14", "4"],
  3390. ["15", "5"],
  3391. ["17", "6"],
  3392. ["21", "7"],
  3393. ["22", "8"],
  3394. ["23", "9"],
  3395. ["24", "10"],
  3396. ["29", "11"],
  3397. ["30", "12"]
  3398. ]), "gpio") :
  3399. this.appendDummyInput()
  3400. .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
  3401. .appendField(new Blockly.FieldDropdown([
  3402. ["4", "0"],
  3403. ["12", "1"],
  3404. ["13", "2"],
  3405. ["14", "3"],
  3406. ["15", "4"],
  3407. ["16", "5"],
  3408. ["17", "6"],
  3409. ["21", "7"],
  3410. ["22", "8"],
  3411. ["25", "9"],
  3412. ["27", "10"]
  3413. ]), "gpio");
  3414. this.setPreviousStatement(true, null);
  3415. this.setNextStatement(true, null);
  3416. this.setInputsInline(false);
  3417. this.setColour("#629978");
  3418. this.setHelpUrl("");
  3419. var thisBlock = this;
  3420. this.setTooltip(function () {
  3421. var mode = thisBlock.getFieldValue('digital_get_type');
  3422. var TOOLTIPS = {
  3423. '2': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3424. '1': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3425. };
  3426. return TOOLTIPS[mode];
  3427. });
  3428. }
  3429. };
  3430. Blockly.Python['DHT_setup'] = function (block) {
  3431. var dropdown_type = block.getFieldValue('digital_get_type');
  3432. var type = block.getFieldValue('type');
  3433. var gpio = block.getFieldValue('gpio');
  3434. var code = '';
  3435. if (dropdown_type == "1") {
  3436. if (type == "1") {
  3437. Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
  3438. 'import dht\n' +
  3439. 'import machine\n' +
  3440. 'import time\n' +
  3441. '\n' +
  3442. 'd = dht.DHT11(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
  3443. '\n';
  3444. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3445. 'def GetDHTTemperatureValue():\n' +
  3446. ' try:\n' +
  3447. ' d.measure()\n' +
  3448. ' return d.temperature()\n' +
  3449. ' except:\n' +
  3450. ' return d.temperature()\n' +
  3451. '\n' +
  3452. 'def GetDHTHumidityValue():\n' +
  3453. ' try:\n' +
  3454. ' d.measure()\n' +
  3455. ' return d.humidity()\n' +
  3456. ' except:\n' +
  3457. ' return d.humidity()\n' +
  3458. '\n' +
  3459. '';
  3460. }
  3461. else {
  3462. Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
  3463. 'import dht\n' +
  3464. 'import machine\n' +
  3465. 'import time\n' +
  3466. '\n' +
  3467. 'd = dht.DHT22(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
  3468. '\n';
  3469. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3470. 'def GetDHTTemperatureValue():\n' +
  3471. ' try:\n' +
  3472. ' d.measure()\n' +
  3473. ' return d.temperature()\n' +
  3474. ' except:\n' +
  3475. ' return d.temperature()\n' +
  3476. '\n' +
  3477. 'def GetDHTHumidityValue():\n' +
  3478. ' try:\n' +
  3479. ' d.measure()\n' +
  3480. ' return d.humidity()\n' +
  3481. ' except:\n' +
  3482. ' return d.humidity()\n' +
  3483. '\n' +
  3484. '';
  3485. }
  3486. } else if (dropdown_type == "2") {
  3487. if (type == "1") {
  3488. Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
  3489. 'import dhtsensor\n' +
  3490. 'import time\n' +
  3491. '\n' +
  3492. 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
  3493. 'time.sleep_ms(100)\n' +
  3494. '\n' +
  3495. 'temperature = None\n' +
  3496. 'humidity = None\n' +
  3497. '\n';
  3498. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3499. 'def GetDHTTemperatureValue():\n' +
  3500. ' global temperature,humidity\n' +
  3501. ' if dhtsensor.measure() and temperature == None:\n' +
  3502. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3503. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3504. ' time.sleep_ms(1000)\n' +
  3505. ' return temperature\n' +
  3506. ' elif temperature != None:\n' +
  3507. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3508. ' humidity = None\n' +
  3509. ' return temperature\n' +
  3510. '\n' +
  3511. 'def GetDHTHumidityValue():\n' +
  3512. ' global temperature,humidity\n' +
  3513. ' if dhtsensor.measure() and humidity == None:\n' +
  3514. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3515. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3516. ' time.sleep_ms(1000)\n' +
  3517. ' return humidity\n' +
  3518. ' elif humidity != None:\n' +
  3519. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3520. ' temperature = None\n' +
  3521. ' return humidity\n' +
  3522. '\n' +
  3523. '';
  3524. }
  3525. else {
  3526. Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
  3527. 'import dhtsensor\n' +
  3528. 'import time\n' +
  3529. '\n' +
  3530. 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
  3531. 'time.sleep_ms(100)\n' +
  3532. '\n' +
  3533. 'temperature = None\n' +
  3534. 'humidity = None\n' +
  3535. '\n';
  3536. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3537. 'def GetDHTTemperatureValue():\n' +
  3538. ' global temperature,humidity\n' +
  3539. ' if dhtsensor.measure() and temperature == None:\n' +
  3540. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3541. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3542. ' time.sleep_ms(1000)\n' +
  3543. ' return temperature * 0.1\n' +
  3544. ' elif temperature != None:\n' +
  3545. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3546. ' humidity = None\n' +
  3547. ' return temperature * 0.1\n' +
  3548. '\n' +
  3549. 'def GetDHTHumidityValue():\n' +
  3550. ' global temperature,humidity\n' +
  3551. ' if dhtsensor.measure() and humidity == None:\n' +
  3552. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3553. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3554. ' time.sleep_ms(1000)\n' +
  3555. ' return humidity * 0.1\n' +
  3556. ' elif humidity != None:\n' +
  3557. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3558. ' temperature = None\n' +
  3559. ' return humidity * 0.1\n' +
  3560. '\n' +
  3561. '';
  3562. }
  3563. }
  3564. return code;
  3565. };
  3566. Blockly.Blocks['DHT_read_data'] = {
  3567. init: function () {
  3568. // this.appendDummyInput()
  3569. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  3570. this.appendDummyInput()
  3571. .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
  3572. .appendField(new Blockly.FieldDropdown([
  3573. [Blockly.Msg.basic_temperature, "temperature"],
  3574. [Blockly.Msg.basic_humidity, "humidity"]
  3575. // [Blockly.Msg.basic_pressure, "pressure"]
  3576. ]), "env_get_type")
  3577. .appendField(Blockly.Msg.basic_env_value);
  3578. this.setInputsInline(false);
  3579. this.setOutput(true, null);
  3580. this.setColour("#629978");
  3581. this.setHelpUrl("");
  3582. var thisBlock = this;
  3583. this.setTooltip(function () {
  3584. var mode = thisBlock.getFieldValue('env_get_type');
  3585. var TOOLTIPS = {
  3586. 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
  3587. 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
  3588. };
  3589. return TOOLTIPS[mode];
  3590. });
  3591. }
  3592. };
  3593. Blockly.Python['DHT_read_data'] = function (block) {
  3594. var env_get_type = block.getFieldValue('env_get_type');
  3595. var code = "";
  3596. if (env_get_type == "temperature") {
  3597. code = 'GetDHTTemperatureValue()';
  3598. } else if (env_get_type == "humidity") {
  3599. code = 'GetDHTHumidityValue()';
  3600. }
  3601. return [code, Blockly.Python.ORDER_NONE];
  3602. };
  3603. /* PM2.5 */
  3604. Blockly.Blocks['PM25_setup'] = {
  3605. init: function () {
  3606. this.appendDummyInput()
  3607. .appendField(new Blockly.FieldImage("blockly/media/pm25sensor_setup.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
  3608. let types = getLocalStorage("type")
  3609. types == 1 ? this.appendDummyInput()
  3610. .appendField("")
  3611. .appendField(new Blockly.FieldDropdown([
  3612. [Blockly.Msg.time_ai_module, "2"],
  3613. [Blockly.Msg.time_iot_module, "1"]
  3614. ]), "digital_get_type")
  3615. .appendField(Blockly.Msg.PM25_SETUP_TEXT) :
  3616. this.appendDummyInput()
  3617. .appendField("")
  3618. .appendField(new Blockly.FieldDropdown([
  3619. [Blockly.Msg.time_iot_module, "1"],
  3620. [Blockly.Msg.time_ai_module, "2"]
  3621. ]), "digital_get_type")
  3622. .appendField(Blockly.Msg.PM25_SETUP_TEXT);
  3623. this.appendDummyInput()
  3624. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3625. .appendField(new Blockly.FieldDropdown([
  3626. ["GP2Y10", "1"]
  3627. ]), "type")
  3628. .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
  3629. this.setPreviousStatement(true, null);
  3630. this.setNextStatement(true, null);
  3631. this.setInputsInline(false);
  3632. this.setColour("#386dc8");
  3633. this.setHelpUrl("");
  3634. var thisBlock = this;
  3635. this.setTooltip(function () {
  3636. var mode = thisBlock.getFieldValue('digital_get_type');
  3637. var TOOLTIPS = {
  3638. '2': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3639. '1': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3640. };
  3641. return TOOLTIPS[mode];
  3642. });
  3643. }
  3644. };
  3645. Blockly.Python['PM25_setup'] = function (block) {
  3646. var dropdown_type = block.getFieldValue('digital_get_type');
  3647. var type = block.getFieldValue('type');
  3648. var code = '';
  3649. if (dropdown_type == "1") {
  3650. if (type == "1") {
  3651. Blockly.Python.definitions_['import_iot_PM25_setup'] = '' +
  3652. 'from machine import UART, Pin\n' +
  3653. 'import pm25\n' +
  3654. '\n' +
  3655. 'uart = UART(2, baudrate=2400, rx=16, tx=17, timeout=10)\n' +
  3656. '\n';
  3657. }
  3658. } else if (dropdown_type == "2") {
  3659. if (type == "1") {
  3660. Blockly.Python.definitions_['import_ai_PM25_setup'] = '' +
  3661. 'import machine, time\n' +
  3662. 'from fpioa_manager import fm\n' +
  3663. 'import pm25\n' +
  3664. '\n' +
  3665. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  3666. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  3667. '\n' +
  3668. 'uart = machine.UART(machine.UART.UART2,2400,bits=8,parity=None,stop=1)\n' +
  3669. '\n';
  3670. }
  3671. }
  3672. return code;
  3673. };
  3674. Blockly.Blocks['PM25_read_data'] = {
  3675. init: function () {
  3676. // this.appendDummyInput()
  3677. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  3678. this.appendDummyInput()
  3679. .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
  3680. .appendField("PM 2.5")
  3681. .appendField(Blockly.Msg.basic_env_value);
  3682. this.setInputsInline(false);
  3683. this.setOutput(true, null);
  3684. this.setTooltip(Blockly.Msg.PM25_REDD_DATA_TOOLTIP);
  3685. this.setColour("#386dc8");
  3686. this.setHelpUrl("");
  3687. }
  3688. };
  3689. Blockly.Python['PM25_read_data'] = function (block) {
  3690. var code = "pm25.getPM25Value(uart)";
  3691. return [code, Blockly.Python.ORDER_NONE];
  3692. };
  3693. /* RTC */
  3694. var rtcColor = "#3c7683";
  3695. Blockly.Blocks['RTC_setup'] = {
  3696. init: function () {
  3697. this.appendDummyInput()
  3698. .appendField(new Blockly.FieldImage("blockly/media/sensors-rtc.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
  3699. this.appendDummyInput()
  3700. .appendField("")
  3701. .appendField(Blockly.Msg.time_ai_module)
  3702. .appendField(Blockly.Msg.RTC_SETUP);
  3703. this.appendDummyInput()
  3704. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3705. .appendField(new Blockly.FieldDropdown([
  3706. ["DS3231", "1"]
  3707. ]), "type");
  3708. this.setPreviousStatement(true, null);
  3709. this.setNextStatement(true, null);
  3710. this.setInputsInline(false);
  3711. this.setColour(rtcColor);
  3712. this.setHelpUrl("");
  3713. this.setTooltip(Blockly.Msg.RTC_setup_TOOLTIP);
  3714. }
  3715. };
  3716. Blockly.Python['RTC_setup'] = function (block) {
  3717. var type = block.getFieldValue('type');
  3718. var code = '';
  3719. if (type == "1") {
  3720. Blockly.Python.definitions_['import_iot_RTC_setup'] = '' +
  3721. 'import machine\n' +
  3722. 'import DS3231micro\n' +
  3723. '\n' +
  3724. 'rtc = DS3231micro.DS3231(30, 29)\n' +
  3725. '\n';
  3726. }
  3727. return code;
  3728. };
  3729. Blockly.Blocks['RTC_setDateTime'] = {
  3730. init: function () {
  3731. this.appendDummyInput()
  3732. .appendField(Blockly.Msg.RTC_SETDATETIME);
  3733. this.appendDummyInput()
  3734. .appendField(Blockly.Msg.RTC_SETDATE)
  3735. .appendField(new Blockly.FieldNumber(18, 0, 50), "year")
  3736. .appendField("-")
  3737. .appendField(new Blockly.FieldNumber(1, 0, 12), "month")
  3738. .appendField("-")
  3739. .appendField(new Blockly.FieldNumber(1, 0, 31), "day")
  3740. this.appendDummyInput()
  3741. .appendField(Blockly.Msg.RTC_SETTIME)
  3742. .appendField(new Blockly.FieldNumber(12, 0, 24), "hour")
  3743. .appendField(":")
  3744. .appendField(new Blockly.FieldNumber(30, 0, 60), "minute")
  3745. .appendField(":")
  3746. .appendField(new Blockly.FieldNumber(10, 0, 60), "second")
  3747. this.appendDummyInput()
  3748. .appendField(Blockly.Msg.RTC_SETWEEK)
  3749. .appendField(new Blockly.FieldDropdown([
  3750. [Blockly.Msg.Monday, "1"],
  3751. [Blockly.Msg.Tuesday, "2"],
  3752. [Blockly.Msg.Wednesday, "3"],
  3753. [Blockly.Msg.Thursday, "4"],
  3754. [Blockly.Msg.Friday, "5"],
  3755. [Blockly.Msg.Saturday, "6"],
  3756. [Blockly.Msg.Sunday, "7"]
  3757. ]), "week")
  3758. this.setPreviousStatement(true);
  3759. this.setNextStatement(true);
  3760. this.setColour(rtcColor);
  3761. this.setTooltip(Blockly.Msg.RTC_setDateTime_TOOLTIP);
  3762. this.setHelpUrl("");
  3763. },
  3764. /**
  3765. * Called whenever anything on the workspace changes.
  3766. * It checks the number of data and time settting
  3767. * @this Blockly.Block
  3768. */
  3769. onchange: function () {
  3770. if (this.getFieldValue("year") > 50) {
  3771. this.setFieldValue("19", "year");
  3772. }
  3773. if (this.getFieldValue("month") > 12) {
  3774. this.setFieldValue("12", "month");
  3775. } else if (this.getFieldValue("month") < 0) {
  3776. this.setFieldValue("1", "month");
  3777. }
  3778. if (this.getFieldValue("day") > 31) {
  3779. this.setFieldValue("31", "day");
  3780. } else if (this.getFieldValue("day") < 0) {
  3781. this.setFieldValue("1", "day");
  3782. }
  3783. if (this.getFieldValue("hour") > 24) {
  3784. this.setFieldValue("24", "hour");
  3785. } else if (this.getFieldValue("hour") < 0) {
  3786. this.setFieldValue("1", "hour");
  3787. }
  3788. if (this.getFieldValue("minute") > 60) {
  3789. this.setFieldValue("60", "minute");
  3790. } else if (this.getFieldValue("minute") < 0) {
  3791. this.setFieldValue("1", "minute");
  3792. }
  3793. if (this.getFieldValue("second") > 60) {
  3794. this.setFieldValue("60", "second");
  3795. } else if (this.getFieldValue("second") < 0) {
  3796. this.setFieldValue("1", "second");
  3797. }
  3798. }
  3799. };
  3800. Blockly.Python['RTC_setDateTime'] = function (block) {
  3801. var year = block.getFieldValue("year");
  3802. var month = block.getFieldValue("month");
  3803. var day = block.getFieldValue("day");
  3804. var hour = block.getFieldValue("hour");
  3805. var minute = block.getFieldValue("minute");
  3806. var second = block.getFieldValue("second");
  3807. var week = block.getFieldValue('week');
  3808. var code = 'rtc.setDateTime(' + year + ',' + month + ',' + day + ',' + week + ',' + hour + ',' + minute + ',' + second + ')\n';
  3809. return code;
  3810. };
  3811. Blockly.Blocks['RTC_getDate'] = {
  3812. init: function () {
  3813. this.appendDummyInput()
  3814. .appendField(Blockly.Msg.RTC_GETDATE)
  3815. .appendField(new Blockly.FieldDropdown([
  3816. [Blockly.Msg.RTC_YEAR, "year"],
  3817. [Blockly.Msg.RTC_MONTH, "month"],
  3818. [Blockly.Msg.RTC_DAY, "day"],
  3819. ]), "date_detail")
  3820. this.setColour(rtcColor);
  3821. this.setOutput(true, null);
  3822. this.setHelpUrl("");
  3823. var thisBlock = this;
  3824. this.setTooltip(function () {
  3825. var mode = thisBlock.getFieldValue('date_detail');
  3826. var TOOLTIPS = {
  3827. "year": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
  3828. "month": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
  3829. "day": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
  3830. };
  3831. return TOOLTIPS[mode];
  3832. });
  3833. }
  3834. };
  3835. Blockly.Python['RTC_getDate'] = function (block) {
  3836. var date_detail = block.getFieldValue("date_detail");
  3837. var code = '';
  3838. if (date_detail == "year") {
  3839. code = 'rtc.getYear()';
  3840. }
  3841. else if (date_detail == "month") {
  3842. code = 'rtc.getMonth()';
  3843. }
  3844. else {
  3845. code = 'rtc.getDay()';
  3846. }
  3847. return [code, Blockly.Python.ORDER_NONE];
  3848. };
  3849. Blockly.Blocks['RTC_getTime'] = {
  3850. init: function () {
  3851. this.appendDummyInput()
  3852. .appendField(Blockly.Msg.RTC_GETTIME)
  3853. .appendField(new Blockly.FieldDropdown([
  3854. [Blockly.Msg.RTC_HOUR, "hour"],
  3855. [Blockly.Msg.RTC_MINUTE, "minute"],
  3856. [Blockly.Msg.RTC_SECOND, "second"],
  3857. ]), "time_detail")
  3858. this.setColour(rtcColor);
  3859. this.setOutput(true, null);
  3860. this.setHelpUrl("");
  3861. var thisBlock = this;
  3862. this.setTooltip(function () {
  3863. var mode = thisBlock.getFieldValue('time_detail');
  3864. var TOOLTIPS = {
  3865. "hour": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
  3866. "minute": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
  3867. "second": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
  3868. };
  3869. return TOOLTIPS[mode];
  3870. });
  3871. }
  3872. };
  3873. Blockly.Python['RTC_getTime'] = function (block) {
  3874. var time_detail = block.getFieldValue("time_detail");
  3875. var code = '';
  3876. if (time_detail == "hour") {
  3877. code = 'rtc.getHour()';
  3878. }
  3879. else if (time_detail == "minute") {
  3880. code = 'rtc.getMinutes()';
  3881. }
  3882. else {
  3883. code = 'rtc.getSeconds()';
  3884. }
  3885. return [code, Blockly.Python.ORDER_NONE];
  3886. };
  3887. Blockly.Blocks['RTC_getWeek'] = {
  3888. init: function () {
  3889. this.appendDummyInput()
  3890. .appendField(Blockly.Msg.RTC_GETWEEK);
  3891. this.setColour(rtcColor);
  3892. this.setOutput(true, null);
  3893. this.setTooltip(Blockly.Msg.RTC_getWeek_TOOLTIP);
  3894. this.setHelpUrl("");
  3895. }
  3896. };
  3897. Blockly.Python['RTC_getWeek'] = function (block) {
  3898. var code = 'rtc.getDayOfWeek()';
  3899. return [code, Blockly.Python.ORDER_NONE];
  3900. };
  3901. Blockly.Blocks['RTC_getDateTime'] = {
  3902. init: function () {
  3903. this.appendDummyInput()
  3904. .appendField(Blockly.Msg.RTC_getDateTime);
  3905. this.setColour(rtcColor);
  3906. this.setOutput(true, null);
  3907. this.setTooltip(Blockly.Msg.RTC_getDateTime_TOOLTIP);
  3908. this.setHelpUrl("");
  3909. }
  3910. };
  3911. Blockly.Python['RTC_getDateTime'] = function (block) {
  3912. var code = 'rtc.getDateTime()';
  3913. return [code, Blockly.Python.ORDER_NONE];
  3914. };
  3915. Blockly.Blocks['RTC_setDate'] = {
  3916. init: function () {
  3917. this.appendDummyInput()
  3918. .appendField(Blockly.Msg.RTC_setDATE)
  3919. .appendField(new Blockly.FieldDropdown([
  3920. [Blockly.Msg.RTC_YEAR, "year"],
  3921. [Blockly.Msg.RTC_MONTH, "month"],
  3922. [Blockly.Msg.RTC_DAY, "day"],
  3923. ]), "date_detail")
  3924. .appendField(Blockly.Msg.RTC_WEI)
  3925. .appendField(new Blockly.FieldNumber(0, null, null), "date")
  3926. this.setOutput(false, null);
  3927. this.setPreviousStatement(true);
  3928. this.setNextStatement(true);
  3929. this.setColour(rtcColor);
  3930. this.setHelpUrl("");
  3931. var thisBlock = this;
  3932. this.setTooltip(function () {
  3933. var mode = thisBlock.getFieldValue('date_detail');
  3934. var TOOLTIPS = {
  3935. "year": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
  3936. "month": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
  3937. "day": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
  3938. };
  3939. return TOOLTIPS[mode];
  3940. });
  3941. }
  3942. };
  3943. Blockly.Python['RTC_setDate'] = function (block) {
  3944. var date_detail = block.getFieldValue("date_detail");
  3945. var date = block.getFieldValue("date");
  3946. var code = '';
  3947. if (date_detail == "year") {
  3948. code = 'rtc.setYear(' + date + ')\n';
  3949. }
  3950. else if (date_detail == "month") {
  3951. code = 'rtc.setMonth(' + date + ')\n';
  3952. }
  3953. else {
  3954. code = 'rtc.setDay(' + date + ')\n';
  3955. }
  3956. return code;
  3957. };
  3958. Blockly.Blocks['RTC_setTime'] = {
  3959. init: function () {
  3960. this.appendDummyInput()
  3961. .appendField(Blockly.Msg.RTC_setTIME)
  3962. .appendField(new Blockly.FieldDropdown([
  3963. [Blockly.Msg.RTC_HOUR, "hour"],
  3964. [Blockly.Msg.RTC_MINUTE, "minute"],
  3965. [Blockly.Msg.RTC_SECOND, "second"],
  3966. ]), "time_detail")
  3967. .appendField(Blockly.Msg.RTC_WEI)
  3968. .appendField(new Blockly.FieldNumber(0, 0, null), "time")
  3969. this.setColour(rtcColor);
  3970. this.setPreviousStatement(true);
  3971. this.setNextStatement(true);
  3972. this.setHelpUrl("");
  3973. var thisBlock = this;
  3974. this.setTooltip(function () {
  3975. var mode = thisBlock.getFieldValue('time_detail');
  3976. var TOOLTIPS = {
  3977. "hour": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
  3978. "minute": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
  3979. "second": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
  3980. };
  3981. return TOOLTIPS[mode];
  3982. });
  3983. }
  3984. };
  3985. Blockly.Python['RTC_setTime'] = function (block) {
  3986. var time_detail = block.getFieldValue("time_detail");
  3987. var time = block.getFieldValue("time");
  3988. var code = '';
  3989. if (time_detail == "hour") {
  3990. code = 'rtc.setHour(' + time + ')\n';
  3991. }
  3992. else if (time_detail == "minute") {
  3993. code = 'rtc.setMinutes(' + time + ')\n';
  3994. }
  3995. else {
  3996. code = 'rtc.setSeconds(' + time + ')\n';
  3997. }
  3998. return code;
  3999. };
  4000. Blockly.Blocks['RTC_setWeek'] = {
  4001. init: function () {
  4002. this.appendDummyInput()
  4003. .appendField(Blockly.Msg.RTC_setWEEK)
  4004. .appendField(new Blockly.FieldNumber(1, 0, null), "week");
  4005. this.setColour(rtcColor);
  4006. this.setPreviousStatement(true);
  4007. this.setNextStatement(true);
  4008. this.setTooltip(Blockly.Msg.RTC_setWeek_TOOLTIP);
  4009. this.setHelpUrl("");
  4010. }
  4011. };
  4012. Blockly.Python['RTC_setWeek'] = function (block) {
  4013. var week = block.getFieldValue("week");
  4014. var code = 'rtc.setDayOfWeek(' + week + ')\n';
  4015. return code;
  4016. };