extension_module.js 189 KB

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