extension_module.js 181 KB

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