esp32.js 187 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846
  1. /* additional text */
  2. Blockly.Blocks['text_split_string_by_delimiter'] = {
  3. init: function() {
  4. this.appendValueInput("string_input")
  5. .setCheck(null)
  6. .appendField(Blockly.Msg.text_additional_delimiter_string);
  7. this.appendDummyInput()
  8. .appendField(Blockly.Msg.text_additional_delimiter_deli)
  9. .appendField(new Blockly.FieldTextInput(","), "delimiter")
  10. .appendField(Blockly.Msg.text_additional_delimiter_end);
  11. this.setInputsInline(true);
  12. this.setOutput(true, null);
  13. this.setColour("#33cc99");
  14. this.setTooltip(Blockly.Msg.Text_Split_String_By_Delimiter_TOOLTIP);
  15. this.setHelpUrl("");
  16. }
  17. };
  18. Blockly.Python['text_split_string_by_delimiter'] = function(block) {
  19. var value_string_input = Blockly.Python.valueToCode(block, 'string_input', Blockly.Python.ORDER_ATOMIC);
  20. var text_delimiter = block.getFieldValue('delimiter');
  21. // TODO: Assemble Python into code variable.
  22. var code = value_string_input + '.split("' + text_delimiter + '")';
  23. // TODO: Change ORDER_NONE to the correct strength.
  24. return [code, Blockly.Python.ORDER_NONE];
  25. };
  26. /* additional math */
  27. Blockly.Blocks['math_degrad'] = {
  28. init: function() {
  29. this.appendValueInput("convert")
  30. .setCheck(null)
  31. .appendField(new Blockly.FieldDropdown([
  32. [Blockly.Msg.math_degrad_rad, "radians"],
  33. [Blockly.Msg.math_degrad_deg, "degrees"]
  34. ]), "rad_deg");
  35. this.setOutput(true, null);
  36. this.setColour("#5472ea");
  37. // this.setTooltip("");
  38. this.setHelpUrl("");
  39. var thisBlock = this;
  40. this.setTooltip(function() {
  41. var mode = thisBlock.getFieldValue('rad_deg');
  42. var TOOLTIPS = {
  43. 'radians': Blockly.Msg.Math_Degrad_RAD_TOOLTIP,
  44. 'degrees': Blockly.Msg.Math_Degrad_DEG_TOOLTIP
  45. };
  46. return TOOLTIPS[mode];
  47. });
  48. }
  49. };
  50. Blockly.Python['math_degrad'] = function(block) {
  51. var dropdown_rad_deg = block.getFieldValue('rad_deg');
  52. var value_convert = Blockly.Python.valueToCode(block, 'convert', Blockly.Python.ORDER_ATOMIC);
  53. // TODO: Assemble Python into code variable.
  54. Blockly.Python.definitions_['import_math'] = 'import math\n';
  55. var code = 'math.' + dropdown_rad_deg + '(' + value_convert + ')';
  56. // TODO: Change ORDER_NONE to the correct strength.
  57. return [code, Blockly.Python.ORDER_NONE];
  58. };
  59. /* Serial Comm. */
  60. Blockly.Blocks['serial_comm_print'] = {
  61. init: function() {
  62. this.appendValueInput("serial_comm_input")
  63. .setCheck("String")
  64. .appendField(Blockly.Msg.serialcomm_print);
  65. this.setInputsInline(false);
  66. this.setPreviousStatement(true, null);
  67. this.setNextStatement(true, null);
  68. this.setColour("#22b845");
  69. this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
  70. this.setHelpUrl("");
  71. }
  72. };
  73. Blockly.Python['serial_comm_print'] = function(block) {
  74. var value_name = Blockly.Python.valueToCode(block, 'serial_comm_input', Blockly.Python.ORDER_ATOMIC);
  75. // TODO: Assemble Python into code variable.
  76. var code = 'print(' + value_name + ')\n';
  77. return code;
  78. };
  79. /*
  80. INPUT & OUTPUT
  81. */
  82. var ESP32_IO_COLOR = "#ff8027";
  83. Blockly.Blocks['esp32_main_controller_text_print'] = {
  84. init: function() {
  85. this.jsonInit({
  86. "message0": Blockly.Msg.TEXT_PRINT_TITLE,
  87. "args0": [{
  88. "type": "input_value",
  89. "name": "TEXT"
  90. }],
  91. "previousStatement": null,
  92. "nextStatement": null,
  93. "colour": "#33cc99",
  94. "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
  95. "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
  96. });
  97. }
  98. };
  99. Blockly.Python['esp32_main_controller_text_print'] = function(block) {
  100. // Print statement.
  101. var msg = Blockly.Python.valueToCode(block, 'TEXT',
  102. Blockly.Python.ORDER_NONE) || '___';
  103. return 'print(' + msg + ')\n';
  104. };
  105. Blockly.Blocks['esp32_main_controller_text'] = {
  106. init: function() {
  107. this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
  108. this.setColour("#33cc99");
  109. this.appendDummyInput()
  110. .appendField(this.newQuote_(true))
  111. .appendField(new Blockly.FieldTextInput(''), 'TEXT')
  112. .appendField(this.newQuote_(false));
  113. this.setOutput(true, 'String');
  114. // Assign 'this' to a variable for use in the tooltip closure below.
  115. var thisBlock = this;
  116. // Text block is trivial. Use tooltip of parent block if it exists.
  117. this.setTooltip(function() {
  118. var parent = thisBlock.getParent();
  119. return (parent && parent.getInputsInline() && parent.tooltip) ||
  120. Blockly.Msg.TEXT_TEXT_TOOLTIP;
  121. });
  122. },
  123. newQuote_: function(open) {
  124. if (open == this.RTL) {
  125. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
  126. } else {
  127. var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
  128. }
  129. return new Blockly.FieldImage(file, 12, 12, '"');
  130. }
  131. };
  132. Blockly.Python['esp32_main_controller_text'] = function(block) {
  133. // Text value.
  134. var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
  135. return [code, Blockly.Python.ORDER_ATOMIC];
  136. };
  137. Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
  138. init: function() {
  139. let types = getLocalStorage("type")
  140. types == 1 ? this.appendDummyInput()
  141. .appendField("")
  142. .appendField(new Blockly.FieldDropdown([
  143. [Blockly.Msg.time_ai_module, "2"],
  144. [Blockly.Msg.time_iot_module, "1"]
  145. ]), "digital_get_type")
  146. .appendField(Blockly.Msg.digital_get) :
  147. this.appendDummyInput()
  148. .appendField("")
  149. .appendField(new Blockly.FieldDropdown([
  150. [Blockly.Msg.time_iot_module, "1"],
  151. [Blockly.Msg.time_ai_module, "2"]
  152. ]), "digital_get_type")
  153. .appendField(Blockly.Msg.digital_get);
  154. this.appendValueInput("digital_pin_input")
  155. .setCheck(null);
  156. this.appendDummyInput()
  157. .appendField(Blockly.Msg.digital_get_value);
  158. this.setInputsInline(true);
  159. this.setOutput(true, null);
  160. this.setColour(ESP32_IO_COLOR);
  161. this.setHelpUrl("");
  162. var thisBlock = this;
  163. this.setTooltip(function() {
  164. var mode = thisBlock.getFieldValue('digital_get_type');
  165. var TOOLTIPS = {
  166. '1': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  167. '2': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  168. };
  169. return TOOLTIPS[mode];
  170. });
  171. }
  172. };
  173. Blockly.Python['esp32_main_controller_io_from_digital_pin'] = function(block) {
  174. var value_name = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
  175. var timer_type = block.getFieldValue('digital_get_type');
  176. // TODO: Assemble Python into code variable.
  177. // TODO: Assemble Python into code variable.
  178. var code = '';
  179. if (timer_type == 2) {
  180. Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
  181. 'from fpioa_manager import *\n' +
  182. 'from Maix import FPIOA,GPIO\n' +
  183. '';
  184. let aa = 'FPIOA().set_function(' + value_name + ',fm.fpioa.GPIOHS' + value_name + ')\n' +
  185. '_read_ai_ext_pin' + value_name + ' = GPIO(GPIO.GPIOHS' + value_name + ', GPIO.IN,GPIO.PULL_UP)\n'
  186. Blockly.Python.addVariable('_read_ai_ext_pin' + value_name, aa, true);
  187. code = '_read_ai_ext_pin' + value_name + '.value()';
  188. } else if (timer_type == 1) {
  189. Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
  190. 'from machine import Pin\n' +
  191. '';
  192. let aa = '_PIN_IN_' + value_name + ' = Pin(' + value_name + ', Pin.IN)\n';
  193. Blockly.Python.addVariable('_PIN_IN_' + value_name, aa, true);
  194. code = '_PIN_IN_' + value_name + '.value()'
  195. }
  196. // TODO: Change ORDER_NONE to the correct strength.
  197. return [code, Blockly.Python.ORDER_CONDITIONAL];
  198. };
  199. Blockly.Blocks['esp32_main_controller_io_high_low'] = {
  200. init: function() {
  201. this.appendDummyInput()
  202. .appendField(new Blockly.FieldDropdown([
  203. [Blockly.Msg.io_high, "1"],
  204. [Blockly.Msg.io_low, "0"]
  205. ]), "high_low");
  206. this.setInputsInline(true);
  207. this.setOutput(true, null);
  208. this.setColour(ESP32_IO_COLOR);
  209. this.setHelpUrl("");
  210. var thisBlock = this;
  211. this.setTooltip(function() {
  212. var mode = thisBlock.getFieldValue('high_low');
  213. var TOOLTIPS = {
  214. '1': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_high),
  215. '0': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_low)
  216. };
  217. return TOOLTIPS[mode];
  218. });
  219. }
  220. };
  221. Blockly.Python['esp32_main_controller_io_high_low'] = function(block) {
  222. var dropdown_name = block.getFieldValue('high_low');
  223. // TODO: Assemble Python into code variable.
  224. var code = dropdown_name;
  225. // TODO: Change ORDER_NONE to the correct strength.
  226. return [code, Blockly.Python.ORDER_NONE];
  227. };
  228. Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
  229. init: function() {
  230. let types = getLocalStorage("type")
  231. types == 1 ? this.appendDummyInput()
  232. .appendField("")
  233. .appendField(new Blockly.FieldDropdown([
  234. [Blockly.Msg.time_ai_module, "2"],
  235. [Blockly.Msg.time_iot_module, "1"]
  236. ]), "digital_set_type")
  237. .appendField(Blockly.Msg.digital_set) :
  238. this.appendDummyInput()
  239. .appendField("")
  240. .appendField(new Blockly.FieldDropdown([
  241. [Blockly.Msg.time_iot_module, "1"],
  242. [Blockly.Msg.time_ai_module, "2"]
  243. ]), "digital_set_type")
  244. .appendField(Blockly.Msg.digital_set);
  245. this.appendValueInput("digital_pin_input")
  246. .setCheck(null);
  247. this.appendDummyInput()
  248. .appendField(Blockly.Msg.digital_set_as);
  249. this.appendValueInput("high_low")
  250. .setCheck(null);
  251. this.setInputsInline(true);
  252. this.setPreviousStatement(true, null);
  253. this.setNextStatement(true, null);
  254. this.setColour(ESP32_IO_COLOR);
  255. this.setHelpUrl("");
  256. var thisBlock = this;
  257. this.setTooltip(function() {
  258. var mode = thisBlock.getFieldValue('digital_set_type');
  259. var TOOLTIPS = {
  260. '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  261. '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  262. };
  263. return TOOLTIPS[mode];
  264. });
  265. }
  266. };
  267. Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function(block) {
  268. var value_name1 = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
  269. var value_name2 = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC);
  270. var timer_type = block.getFieldValue('digital_set_type');
  271. var code = "";
  272. if (timer_type == 1) {
  273. // TODO: Assemble Python into code variable.
  274. Blockly.Python.definitions_['import_set_digital_pin_setup'] = '' +
  275. 'from machine import Pin\n' +
  276. '';
  277. let variable = '_PIN_OUT_' + value_name1 + ' = Pin(' + value_name1 + ', Pin.OUT)\n'
  278. Blockly.Python.addVariable('_PIN_OUT_' + value_name1, variable, true);
  279. code = '_PIN_OUT_' + value_name1 + '.value' + value_name2 + '\n';
  280. } else if (timer_type == 2) {
  281. Blockly.Python.definitions_['import_set_digital_pin_setup'] = '' +
  282. 'from fpioa_manager import *\n' +
  283. 'from Maix import GPIO\n' +
  284. '';
  285. let variable = 'fm.register(' + value_name1 + ',fm.fpioa.GPIOHS' + value_name1 + ')\n' +
  286. '_set_ai_ext_pin' + value_name1 + ' = GPIO(GPIO.GPIOHS' + value_name1 + ', GPIO.OUT)\n'
  287. Blockly.Python.addVariable('_set_ai_ext_pin' + value_name1, variable, true);
  288. code = '_set_ai_ext_pin' + value_name1 + '.value(' + value_name2 + ')\n';
  289. }
  290. return code;
  291. };
  292. Blockly.Blocks['esp32_main_controller_io_from_analog_pin'] = {
  293. init: function() {
  294. this.appendDummyInput()
  295. .appendField(Blockly.Msg.analog_get);
  296. this.appendValueInput("analog_pin_input")
  297. .setCheck(null);
  298. this.appendDummyInput()
  299. .appendField(Blockly.Msg.analog_get_value);
  300. this.setInputsInline(true);
  301. this.setOutput(true, null);
  302. this.setColour(ESP32_IO_COLOR);
  303. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Io_From_Analog_Pin_TOOLTIP);
  304. this.setHelpUrl("");
  305. }
  306. };
  307. Blockly.Python['esp32_main_controller_io_from_analog_pin'] = function(block) {
  308. var value_name = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
  309. // TODO: Assemble Python into code variable.
  310. Blockly.Python.addVariable('_PWM_PIN_IN_' + value_name, '', true);
  311. Blockly.Python.definitions_['import_setup'] = '' +
  312. 'from machine import Pin, ADC\n\n' +
  313. '_PWM_PIN_IN_' + value_name + ' = ADC(Pin(' + value_name + '))\n' +
  314. '_PWM_PIN_IN_' + value_name + '.atten(ADC.ATTN_11DB)\n' +
  315. '';
  316. var code = '_PWM_PIN_IN_' + value_name + '.read()';
  317. // TODO: Change ORDER_NONE to the correct strength.
  318. return [code, Blockly.Python.ORDER_CONDITIONAL];
  319. };
  320. Blockly.Blocks['esp32_main_controller_io_set_analog_pin'] = {
  321. init: function() {
  322. this.appendValueInput("analog_pin_input")
  323. .appendField(Blockly.Msg.analog_set)
  324. .setCheck(null);
  325. // this.appendValueInput("frequency")
  326. // .appendField(Blockly.Msg.analog_set_freq)
  327. // .setCheck(null);
  328. this.appendDummyInput()
  329. .appendField(Blockly.Msg.control)
  330. .appendField(new Blockly.FieldDropdown([
  331. [Blockly.Msg.led, "LED"]
  332. ]), "LED");
  333. this.appendValueInput("pwm")
  334. .appendField(Blockly.Msg.set_pwm)
  335. .setCheck(null);
  336. this.setInputsInline(true);
  337. this.setPreviousStatement(true, null);
  338. this.setNextStatement(true, null);
  339. this.setColour(ESP32_IO_COLOR);
  340. this.setHelpUrl("");
  341. var thisBlock = this;
  342. this.setTooltip(function() {
  343. var mode = thisBlock.getFieldValue('LED');
  344. var TOOLTIPS = {
  345. 'LED': Blockly.Msg.Esp32_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
  346. };
  347. return TOOLTIPS[mode];
  348. });
  349. }
  350. };
  351. Blockly.Python['esp32_main_controller_io_set_analog_pin'] = function(block) {
  352. var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
  353. var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
  354. // var value_name3 = Blockly.Python.valueToCode(block, 'frequency', Blockly.Python.ORDER_ATOMIC);
  355. // var module_type = block.getFieldValue('digital_set_type');
  356. Blockly.Python.addVariable('_pwm_gpio_' + value_name1, '', true);
  357. // TODO: Assemble Python into code variable.
  358. Blockly.Python.definitions_['import_analog_set_setup'] = 'from machine import Pin, PWM\n';
  359. var code = '_pwm_gpio_' + value_name1 + ' = PWM(Pin(' + value_name1 + '), freq=300, duty=' + value_name2 + ')\n';
  360. return code;
  361. };
  362. Blockly.Blocks['ai_main_controller_io_set_analog_pin'] = {
  363. init: function() {
  364. var _en = true;
  365. for (var i = 0; i < ids.length; i++) {
  366. if (!ids[i]) {
  367. _en = false;
  368. }
  369. }
  370. this.disabled = ids.length > 11 && _en ? true : false;
  371. this.appendValueInput("analog_pin_input")
  372. .appendField(Blockly.Msg.analog_set_ai)
  373. .setCheck(null);
  374. // this.appendValueInput("frequency")
  375. // .appendField(Blockly.Msg.analog_set_freq)
  376. // .setCheck(null);
  377. this.appendDummyInput()
  378. .appendField(Blockly.Msg.control)
  379. .appendField(new Blockly.FieldDropdown([
  380. [Blockly.Msg.led, "LED"]
  381. ]), "LED");
  382. this.appendValueInput("pwm")
  383. .appendField(Blockly.Msg.set_pwm)
  384. .setCheck(null);
  385. // this.appendValueInput("timer")
  386. // .appendField(Blockly.Msg.analog_set_timer)
  387. // .setCheck(null);
  388. // this.appendValueInput("channel")
  389. // .appendField(Blockly.Msg.analog_set_timer_channel)
  390. // .setCheck(null);
  391. this.setInputsInline(true);
  392. this.setPreviousStatement(true, null);
  393. this.setNextStatement(true, null);
  394. this.setColour(ESP32_IO_COLOR);
  395. this.setHelpUrl("");
  396. var thisBlock = this;
  397. this.setTooltip(function() {
  398. var mode = thisBlock.getFieldValue('LED');
  399. var TOOLTIPS = {
  400. 'LED': Blockly.Msg.Ai_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
  401. };
  402. return TOOLTIPS[mode];
  403. });
  404. }
  405. };
  406. var ids = [];
  407. Blockly.Python['ai_main_controller_io_set_analog_pin'] = function(block) {
  408. var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
  409. var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
  410. var str = '';
  411. var j = 0;
  412. var returnvalue = '';
  413. for (var i = 0; i < ids.length; i++) {
  414. if (ids[i] && !Blockly.getMainWorkspace().getBlockById(ids[i].id)) {
  415. ids[i] = null;
  416. }
  417. }
  418. for (var i = 0; i < ids.length; i++) {
  419. if (ids[i] && ids[i].id == block.id) {
  420. j = i;
  421. break;
  422. } else {
  423. if (j == 0) {
  424. if (!ids[i]) {
  425. j = i;
  426. } else if (i == ids.length - 1) {
  427. j = ids.length;
  428. }
  429. }
  430. }
  431. }
  432. if (!ids[j]) {
  433. ids[j] = { id: block.id, index: j };
  434. }
  435. for (var i = 0; i < ids.length; i++) {
  436. if (ids[i]) {
  437. Blockly.Python.addVariable('_pwm_gpio_tim_' + ids[i].index, '', true);
  438. Blockly.Python.addVariable('_pwm_gpio_control_' + ids[i].index, '', true);
  439. str += '_pwm_gpio_tim_' + ids[i].index + ' = Timer(_io_timer_use[' + ids[i].index + '][0],_io_timer_use[' + ids[i].index + '][1], mode=Timer.MODE_PWM)\n';
  440. str += '_pwm_gpio_control_' + ids[i].index + ' = PWM(_pwm_gpio_tim_' + ids[i].index + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
  441. }
  442. }
  443. returnvalue += '_pwm_gpio_control_' + ids[j].index + '.duty(int(' + value_name2 + '/10.23))\n';
  444. // TODO: Assemble Python into code variable.
  445. Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
  446. 'from machine import Pin,PWM,Timer\n' +
  447. '\n' +
  448. '_io_timer_use = [[Timer.TIMER0, Timer.CHANNEL0],[Timer.TIMER0, Timer.CHANNEL1],[Timer.TIMER0, Timer.CHANNEL2],[Timer.TIMER1, Timer.CHANNEL0],[Timer.TIMER1, Timer.CHANNEL1],[Timer.TIMER1, Timer.CHANNEL2],[Timer.TIMER2, Timer.CHANNEL0],[Timer.TIMER2, Timer.CHANNEL1],[Timer.TIMER2, Timer.CHANNEL2]]\n' + str;
  449. return returnvalue;
  450. // var module_type = block.getFieldValue('digital_set_type');
  451. // var str = '';
  452. // var ss = '';
  453. // if (counts.length != 0) {
  454. // if (ids.indexOf(block.id) == -1) {
  455. // ids.push(block.id);
  456. // count = count + 1;
  457. // counts.push(count);
  458. // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
  459. // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
  460. // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
  461. // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
  462. // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
  463. // }
  464. // else {
  465. // for (var i = 0; i < counts.length; i++) {
  466. // Blockly.Python.addVariable('_pwm_gpio_tim_' + counts[i], '', true);
  467. // Blockly.Python.addVariable('_pwm_gpio_control_' + counts[i], '', true);
  468. // str += '_pwm_gpio_tim_' + counts[i] + ' = Timer(_io_timer_use[' + counts[i] + '][0],_io_timer_use[' + counts[i] + '][1], mode=Timer.MODE_PWM)\n';
  469. // str += '_pwm_gpio_control_' + counts[i] + ' = PWM(_pwm_gpio_tim_' + counts[i] + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
  470. // ss += '_pwm_gpio_control_' + counts[i] + '.duty(int(' + value_name2 + '/10.23))\n';
  471. // }
  472. // }
  473. // }
  474. // else {
  475. // if (ids.indexOf(block.id) == -1) {
  476. // ids.push(block.id);
  477. // counts.push(count);
  478. // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
  479. // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
  480. // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
  481. // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
  482. // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
  483. // }
  484. // }
  485. // // TODO: Assemble Python into code variable.
  486. // Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
  487. // 'from machine import Pin,PWM,Timer\n' +
  488. // '\n' +
  489. // '_io_timer_use = [[Timer.TIMER0, Timer.CHANNEL0],[Timer.TIMER0, Timer.CHANNEL1],[Timer.TIMER0, Timer.CHANNEL2],[Timer.TIMER1, Timer.CHANNEL0],[Timer.TIMER1, Timer.CHANNEL1],[Timer.TIMER1, Timer.CHANNEL2],[Timer.TIMER2, Timer.CHANNEL0],[Timer.TIMER2, Timer.CHANNEL1],[Timer.TIMER2, Timer.CHANNEL2]]\n' + str;
  490. // // code = ss;
  491. // return ss;
  492. };
  493. /*
  494. Button
  495. */
  496. Blockly.Blocks['esp32_main_controller_button_read_pressed'] = {
  497. init: function() {
  498. // this.appendDummyInput()
  499. // .appendField(new Blockly.FieldImage("blockly/media/button_pressed.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  500. this.appendDummyInput()
  501. .appendField(Blockly.Msg.ESP32BUTTON)
  502. .appendField(new Blockly.FieldDropdown([
  503. ["P1", "button_type_1"],
  504. ["P2", "button_type_2"]
  505. ]), "button_type")
  506. .appendField(Blockly.Msg.ESP32PRESSED)
  507. this.setOutput(true, null);
  508. this.setColour("#d42b03");
  509. this.setHelpUrl("");
  510. var thisBlock = this;
  511. this.setTooltip(function() {
  512. var mode = thisBlock.getFieldValue('button_type');
  513. var TOOLTIPS = {
  514. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P1"),
  515. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P2")
  516. };
  517. return TOOLTIPS[mode];
  518. });
  519. }
  520. };
  521. Blockly.Python['esp32_main_controller_button_read_pressed'] = function(block) {
  522. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  523. var _type = block.getField('button_type').text_;
  524. var _button_state = block.getFieldValue('button_state');
  525. var _num = _type == "P1" ? "34" : "35";
  526. var _upstr = "";
  527. if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
  528. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  529. var code = '(_' + _type + '.value() == ' + 1 + ')';
  530. return [code, Blockly.Python.ORDER_CONDITIONAL];
  531. };
  532. Blockly.Blocks['esp32_main_controller_button_read_released'] = {
  533. init: function() {
  534. // this.appendDummyInput()
  535. // .appendField(new Blockly.FieldImage("blockly/media/button_released.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  536. this.appendDummyInput()
  537. .appendField(Blockly.Msg.ESP32BUTTON)
  538. .appendField(new Blockly.FieldDropdown([
  539. ["P1", "button_type_1"],
  540. ["P2", "button_type_2"]
  541. ]), "button_type")
  542. .appendField(Blockly.Msg.ESP32RELEASED)
  543. this.setOutput(true, null);
  544. this.setColour("#d42b03");
  545. var thisBlock = this;
  546. this.setTooltip(function() {
  547. var mode = thisBlock.getFieldValue('button_type');
  548. var TOOLTIPS = {
  549. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P1"),
  550. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P2")
  551. };
  552. return TOOLTIPS[mode];
  553. });
  554. this.setHelpUrl("");
  555. }
  556. };
  557. Blockly.Python['esp32_main_controller_button_read_released'] = function(block) {
  558. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  559. var _type = block.getField('button_type').text_;
  560. var _button_state = block.getFieldValue('button_state');
  561. var _num = _type == "P1" ? "34" : "35";
  562. var _upstr = "";
  563. if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
  564. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  565. var code = '(_' + _type + '.value() == ' + 0 + ')';
  566. return [code, Blockly.Python.ORDER_CONDITIONAL];
  567. };
  568. Blockly.Blocks['esp32_main_controller_button_read_value'] = {
  569. init: function() {
  570. this.appendDummyInput()
  571. .appendField(Blockly.Msg.basic_button_read_value)
  572. .appendField(new Blockly.FieldDropdown([
  573. ["P1", "button_type_1"],
  574. ["P2", "button_type_2"]
  575. ]), "button_type")
  576. .appendField(Blockly.Msg.basic_button_read_value_after);
  577. this.setInputsInline(true);
  578. this.setOutput(true, null);
  579. this.setColour("#d42b03");
  580. this.setHelpUrl("");
  581. var thisBlock = this;
  582. this.setTooltip(function() {
  583. var mode = thisBlock.getFieldValue('button_type');
  584. var TOOLTIPS = {
  585. 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P1"),
  586. 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P2")
  587. };
  588. return TOOLTIPS[mode];
  589. });
  590. this.setHelpUrl("");
  591. }
  592. };
  593. Blockly.Python['esp32_main_controller_button_read_value'] = function(block) {
  594. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  595. var _type = block.getField('button_type').text_;
  596. // TODO: Assemble Python into code variable.
  597. var _num = _type == "P1" ? "34" : "35";
  598. var _upstr = "";
  599. if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
  600. Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  601. var code = '_' + _type + '.value()';
  602. // TODO: Change ORDER_NONE to the correct strength.
  603. return [code, Blockly.Python.ORDER_NONE];
  604. };
  605. /*
  606. Blockly.Python['esp32_main_controller_button_read'] = function (block) {
  607. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  608. var _type = block.getField('button_type').text_;
  609. var _button_state = block.getFieldValue('button_state');
  610. var _num = _type == "1" ? "36" : "22";
  611. var _upstr = "";
  612. if (_type == "2") { _upstr = ",Pin.PULL_UP"; }
  613. Blockly.Python.addVariable('p' + _type, 'p' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
  614. var code = '(p' + _type + '.value() == ' + (_button_state == "button_state_pressed" ? "1" : "0") + ')';
  615. return [code, Blockly.Python.ORDER_CONDITIONAL];
  616. };
  617. */
  618. /*
  619. Dialer
  620. */
  621. Blockly.Blocks['esp32_main_controller_dial_switch_read_pressed'] = {
  622. init: function() {
  623. this.appendDummyInput()
  624. .appendField(Blockly.Msg.ESP32DIALSWITCH)
  625. .appendField(new Blockly.FieldDropdown([
  626. [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
  627. [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
  628. [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
  629. ]), "switch_type")
  630. .appendField(Blockly.Msg.ESP32PRESSED)
  631. this.setOutput(true, null);
  632. this.setColour("#e8795b");
  633. this.setTooltip("");
  634. this.setHelpUrl("");
  635. }
  636. };
  637. Blockly.Python['esp32_main_controller_dial_switch_read_pressed'] = function(block) {
  638. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  639. var _type = block.getFieldValue('switch_type');
  640. var _num = "34";
  641. var _upstr = "";
  642. var _button_type = "";
  643. if (_type == "switch_type_left") {
  644. var _num = "34";
  645. _button_type = "Left";
  646. } else if (_type == "switch_type_middle") {
  647. var _num = "19";
  648. _upstr = ", Pin.PULL_UP";
  649. _button_type = "Middle";
  650. } else if (_type == "switch_type_right") {
  651. var _num = "35";
  652. _button_type = "Right";
  653. }
  654. // Blockly.Python.addVariable("dialer_type", 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
  655. Blockly.Python.addVariable("pLeft", 'pLeft = Pin(38, Pin.IN)');
  656. Blockly.Python.addVariable("pMiddle", 'pMiddle = Pin(19, Pin.IN)');
  657. Blockly.Python.addVariable("pRight", 'pRight = Pin(35, Pin.IN)');
  658. var code = '(p' + _button_type + '.value() == 1)';
  659. return [code, Blockly.Python.ORDER_CONDITIONAL];
  660. };
  661. Blockly.Blocks['esp32_main_controller_dial_switch_read_released'] = {
  662. init: function() {
  663. this.appendDummyInput()
  664. .appendField(Blockly.Msg.ESP32DIALSWITCH)
  665. .appendField(new Blockly.FieldDropdown([
  666. [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
  667. [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
  668. [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
  669. ]), "switch_type")
  670. .appendField(Blockly.Msg.ESP32RELEASED)
  671. this.setOutput(true, null);
  672. this.setColour("#e8795b");
  673. this.setTooltip("");
  674. this.setHelpUrl("");
  675. }
  676. };
  677. Blockly.Python['esp32_main_controller_dial_switch_read_released'] = function(block) {
  678. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  679. var _type = block.getFieldValue('switch_type');
  680. var _state = block.getFieldValue('switch_state');
  681. var _num = "34";
  682. var _upstr = "";
  683. var _button_type = "";
  684. if (_type == "switch_type_left") {
  685. var _num = "34";
  686. _button_type = "Left";
  687. } else if (_type == "switch_type_middle") {
  688. var _num = "19";
  689. _upstr = ", Pin.PULL_UP";
  690. _button_type = "Middle";
  691. } else if (_type == "switch_type_right") {
  692. var _num = "35";
  693. _button_type = "Right";
  694. }
  695. Blockly.Python.addVariable('p' + _button_type, 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
  696. var code = '(p' + _button_type + '.value() == 0)';
  697. return [code, Blockly.Python.ORDER_CONDITIONAL];
  698. };
  699. /*
  700. LED
  701. */
  702. Blockly.Blocks['esp32_main_controller_set_led_turnon'] = {
  703. init: function() {
  704. // this.appendDummyInput()
  705. // .appendField(new Blockly.FieldImage("blockly/media/led_on.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  706. this.appendDummyInput()
  707. .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
  708. this.appendValueInput("led_type")
  709. .setCheck(null);
  710. this.appendDummyInput()
  711. .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNON);
  712. this.setPreviousStatement(true, null);
  713. this.setNextStatement(true, null);
  714. this.setColour("#d42b03");
  715. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnon_TOOLTIP);
  716. this.setHelpUrl("");
  717. }
  718. };
  719. Blockly.Python['esp32_main_controller_set_led_turnon'] = function(block) {
  720. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  721. var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
  722. var _state = block.getFieldValue('led_state');
  723. var _num = "2";
  724. if (_type == "1") {
  725. var _num = "2";
  726. } else if (_type == "2") {
  727. var _num = "12";
  728. } else if (_type == "3") {
  729. var _num = "5";
  730. } else if (_type == "4") {
  731. var _num = "13";
  732. } else if (_type == "5") {
  733. var _num = "14";
  734. } else if (_type == "6") {
  735. var _num = "15";
  736. } else if (_type == "7") {
  737. var _num = "18";
  738. } else if (_type == "8") {
  739. var _num = "19";
  740. } else if (_type == "9") {
  741. var _num = "23";
  742. } else {
  743. var _num = "ERROR";
  744. }
  745. Blockly.Python.addSetup('iot_led_light_setup', '' +
  746. '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
  747. '\n' +
  748. '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
  749. '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
  750. '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
  751. '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
  752. '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
  753. '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
  754. '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
  755. '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
  756. '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
  757. '\n' +
  758. 'def _led_light(state, pin):\n' +
  759. ' if (state == True):\n' +
  760. ' if pin == 1: _light_1.value(1)\n' +
  761. ' if pin == 2: _light_2.value(1)\n' +
  762. ' if pin == 3: _light_3.value(1)\n' +
  763. ' if pin == 4: _light_4.value(1)\n' +
  764. ' if pin == 5: _light_5.value(1)\n' +
  765. ' if pin == 6: _light_6.value(1)\n' +
  766. ' if pin == 7: _light_7.value(1)\n' +
  767. ' if pin == 8: _light_8.value(1)\n' +
  768. ' if pin == 9: _light_9.value(1)\n' +
  769. ' elif (state == False):\n' +
  770. ' if pin == 1: _light_1.value(0)\n' +
  771. ' if pin == 2: _light_2.value(0)\n' +
  772. ' if pin == 3: _light_3.value(0)\n' +
  773. ' if pin == 4: _light_4.value(0)\n' +
  774. ' if pin == 5: _light_5.value(0)\n' +
  775. ' if pin == 6: _light_6.value(0)\n' +
  776. ' if pin == 7: _light_7.value(0)\n' +
  777. ' if pin == 8: _light_8.value(0)\n' +
  778. ' if pin == 9: _light_9.value(0)\n' +
  779. '');
  780. var code = '_led_light(True,' + _type + ')\n';
  781. return code;
  782. };
  783. Blockly.Blocks['esp32_main_controller_set_led_turnoff'] = {
  784. init: function() {
  785. // this.appendDummyInput()
  786. // .appendField(new Blockly.FieldImage("blockly/media/led_off.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  787. this.appendDummyInput()
  788. .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
  789. this.appendValueInput("led_type")
  790. .setCheck(null);
  791. this.appendDummyInput()
  792. .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNOFF);
  793. this.setPreviousStatement(true, null);
  794. this.setNextStatement(true, null);
  795. this.setColour("#d42b03");
  796. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnoff_TOOLTIP);
  797. this.setHelpUrl("");
  798. }
  799. };
  800. Blockly.Python['esp32_main_controller_set_led_turnoff'] = function(block) {
  801. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  802. var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
  803. var _state = block.getFieldValue('led_state');
  804. var _num = "2";
  805. if (_type == "1") {
  806. var _num = "2";
  807. } else if (_type == "2") {
  808. var _num = "12";
  809. } else if (_type == "3") {
  810. var _num = "5";
  811. } else if (_type == "4") {
  812. var _num = "13";
  813. } else if (_type == "5") {
  814. var _num = "14";
  815. } else if (_type == "6") {
  816. var _num = "15";
  817. } else if (_type == "7") {
  818. var _num = "18";
  819. } else if (_type == "8") {
  820. var _num = "19";
  821. } else if (_type == "9") {
  822. var _num = "23";
  823. } else {
  824. var _num = "ERROR";
  825. }
  826. Blockly.Python.addSetup('iot_led_light_setup', '' +
  827. '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
  828. '\n' +
  829. '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
  830. '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
  831. '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
  832. '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
  833. '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
  834. '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
  835. '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
  836. '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
  837. '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
  838. '\n' +
  839. 'def _led_light(state, pin):\n' +
  840. ' if (state == True):\n' +
  841. ' if pin == 1: _light_1.value(1)\n' +
  842. ' if pin == 2: _light_2.value(1)\n' +
  843. ' if pin == 3: _light_3.value(1)\n' +
  844. ' if pin == 4: _light_4.value(1)\n' +
  845. ' if pin == 5: _light_5.value(1)\n' +
  846. ' if pin == 6: _light_6.value(1)\n' +
  847. ' if pin == 7: _light_7.value(1)\n' +
  848. ' if pin == 8: _light_8.value(1)\n' +
  849. ' if pin == 9: _light_9.value(1)\n' +
  850. ' elif (state == False):\n' +
  851. ' if pin == 1: _light_1.value(0)\n' +
  852. ' if pin == 2: _light_2.value(0)\n' +
  853. ' if pin == 3: _light_3.value(0)\n' +
  854. ' if pin == 4: _light_4.value(0)\n' +
  855. ' if pin == 5: _light_5.value(0)\n' +
  856. ' if pin == 6: _light_6.value(0)\n' +
  857. ' if pin == 7: _light_7.value(0)\n' +
  858. ' if pin == 8: _light_8.value(0)\n' +
  859. ' if pin == 9: _light_9.value(0)\n' +
  860. '');
  861. var code = '_led_light(False,' + _type + ')\n';
  862. return code;
  863. };
  864. Blockly.Blocks['esp32_main_controller_set_led_drawpixel_on'] = {
  865. init: function() {
  866. this.appendDummyInput()
  867. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTUP + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
  868. this.appendValueInput("x")
  869. .setCheck(null)
  870. this.appendValueInput("y")
  871. .setCheck(null)
  872. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
  873. this.appendDummyInput()
  874. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
  875. this.setInputsInline(true);
  876. this.setPreviousStatement(true, null);
  877. this.setNextStatement(true, null);
  878. this.setColour("#d42b03");
  879. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_On_TOOLTIP);
  880. this.setHelpUrl("");
  881. }
  882. };
  883. Blockly.Python['esp32_main_controller_set_led_drawpixel_on'] = function(block) {
  884. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  885. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  886. // TODO: Assemble Python into code variable.
  887. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  888. Blockly.Python.addSetup('iot_led_light_setup', '' +
  889. '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
  890. '\n' +
  891. '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
  892. '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
  893. '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
  894. '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
  895. '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
  896. '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
  897. '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
  898. '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
  899. '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
  900. '\n' +
  901. 'def _led_light(state, pin):\n' +
  902. ' if (state == True):\n' +
  903. ' if pin == 1: _light_1.value(1)\n' +
  904. ' if pin == 2: _light_2.value(1)\n' +
  905. ' if pin == 3: _light_3.value(1)\n' +
  906. ' if pin == 4: _light_4.value(1)\n' +
  907. ' if pin == 5: _light_5.value(1)\n' +
  908. ' if pin == 6: _light_6.value(1)\n' +
  909. ' if pin == 7: _light_7.value(1)\n' +
  910. ' if pin == 8: _light_8.value(1)\n' +
  911. ' if pin == 9: _light_9.value(1)\n' +
  912. ' elif (state == False):\n' +
  913. ' if pin == 1: _light_1.value(0)\n' +
  914. ' if pin == 2: _light_2.value(0)\n' +
  915. ' if pin == 3: _light_3.value(0)\n' +
  916. ' if pin == 4: _light_4.value(0)\n' +
  917. ' if pin == 5: _light_5.value(0)\n' +
  918. ' if pin == 6: _light_6.value(0)\n' +
  919. ' if pin == 7: _light_7.value(0)\n' +
  920. ' if pin == 8: _light_8.value(0)\n' +
  921. ' if pin == 9: _light_9.value(0)\n' +
  922. '');
  923. Blockly.Python.addSetup('drawpixel_setup', '' +
  924. 'def _led_draw_pixel(state, x, y):\n' +
  925. ' real_pin = (3*y-3+x)\n' +
  926. ' _led_light(state,real_pin)\n' +
  927. '\n')
  928. var code = '_led_draw_pixel(True,' + value_x + ',' + value_y + ')\n';
  929. return code;
  930. };
  931. Blockly.Blocks['esp32_main_controller_set_led_drawpixel_off'] = {
  932. init: function() {
  933. this.appendDummyInput()
  934. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTOFF + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
  935. this.appendValueInput("x")
  936. .setCheck(null)
  937. this.appendValueInput("y")
  938. .setCheck(null)
  939. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
  940. this.appendDummyInput()
  941. .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
  942. this.setInputsInline(true);
  943. this.setPreviousStatement(true, null);
  944. this.setNextStatement(true, null);
  945. this.setColour("#d42b03");
  946. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_Off_TOOLTIP);
  947. this.setHelpUrl("");
  948. }
  949. };
  950. Blockly.Python['esp32_main_controller_set_led_drawpixel_off'] = function(block) {
  951. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  952. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  953. // TODO: Assemble Python into code variable.
  954. Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
  955. Blockly.Python.addSetup('iot_led_light_setup', '' +
  956. '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
  957. '\n' +
  958. '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
  959. '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
  960. '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
  961. '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
  962. '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
  963. '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
  964. '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
  965. '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
  966. '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
  967. '\n' +
  968. 'def _led_light(state, pin):\n' +
  969. ' if (state == True):\n' +
  970. ' if pin == 1: _light_1.value(1)\n' +
  971. ' if pin == 2: _light_2.value(1)\n' +
  972. ' if pin == 3: _light_3.value(1)\n' +
  973. ' if pin == 4: _light_4.value(1)\n' +
  974. ' if pin == 5: _light_5.value(1)\n' +
  975. ' if pin == 6: _light_6.value(1)\n' +
  976. ' if pin == 7: _light_7.value(1)\n' +
  977. ' if pin == 8: _light_8.value(1)\n' +
  978. ' if pin == 9: _light_9.value(1)\n' +
  979. ' elif (state == False):\n' +
  980. ' if pin == 1: _light_1.value(0)\n' +
  981. ' if pin == 2: _light_2.value(0)\n' +
  982. ' if pin == 3: _light_3.value(0)\n' +
  983. ' if pin == 4: _light_4.value(0)\n' +
  984. ' if pin == 5: _light_5.value(0)\n' +
  985. ' if pin == 6: _light_6.value(0)\n' +
  986. ' if pin == 7: _light_7.value(0)\n' +
  987. ' if pin == 8: _light_8.value(0)\n' +
  988. ' if pin == 9: _light_9.value(0)\n' +
  989. '');
  990. Blockly.Python.addSetup('drawpixel_setup', '' +
  991. 'def _led_draw_pixel(state, x, y):\n' +
  992. ' real_pin = (3*y-3+x)\n' +
  993. ' _led_light(state,real_pin)\n' +
  994. '\n')
  995. var code = '_led_draw_pixel(False,' + value_x + ',' + value_y + ')\n';
  996. return code;
  997. };
  998. /*
  999. BUZZER
  1000. */
  1001. Blockly.Blocks['esp32_main_controller_set_buzzer'] = {
  1002. init: function() {
  1003. // this.appendDummyInput()
  1004. // .appendField(new Blockly.FieldImage("blockly/media/buzzer.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1005. this.appendDummyInput()
  1006. .appendField( /*Blockly.Msg.ESP32TURNOBAF*/ );
  1007. //.appendField(new Blockly.FieldNumber(31, 31, 65535, 1), "buzzer_frequency");
  1008. this.appendValueInput("esp32_var_num")
  1009. .setCheck("Number")
  1010. .appendField(Blockly.Msg.basic_buzzer_set);
  1011. this.setInputsInline(true);
  1012. this.setPreviousStatement(true);
  1013. this.setNextStatement(true);
  1014. // this.setPreviousStatement(true, null);
  1015. // this.setNextStatement(true, null);
  1016. this.setColour("#d42b03");
  1017. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Buzzer_TOOLTIP);
  1018. this.setHelpUrl("");
  1019. }
  1020. };
  1021. Blockly.Python['esp32_main_controller_set_buzzer'] = function(block) {
  1022. Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
  1023. Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
  1024. var _type = Blockly.Python.valueToCode(
  1025. block, 'esp32_var_num', Blockly.Python.ORDER_ATOMIC) || '0';
  1026. Blockly.Python.definitions_['buzzer_operation'] = '' +
  1027. 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' +
  1028. 'beeper.deinit()\n' +
  1029. /*
  1030. 'def _beeper_control(state, freq=31):\n' +
  1031. ' if freq <= 31: freq = 31\n' +
  1032. ' elif freq >= 67108863: freq = 67108863\n' +
  1033. ' if state == True:\n' +
  1034. ' beeper.init()\n' +
  1035. ' beeper.freq(freq)\n' +
  1036. ' time.sleep_ms(20)\n' +
  1037. ' elif state == False:\n' +
  1038. ' time.sleep_ms(20)\n' +
  1039. ' beeper.deinit()\n' +
  1040. */
  1041. '\n';
  1042. var code = 'beeper.init()\nbeeper.freq(' + _type + ')\n';
  1043. return code;
  1044. };
  1045. Blockly.Blocks['esp32_main_controller_turn_off_buzzer'] = {
  1046. init: function() {
  1047. this.appendDummyInput()
  1048. .appendField(Blockly.Msg.ESP32TURNOB);
  1049. this.setPreviousStatement(true, null);
  1050. this.setNextStatement(true, null);
  1051. this.setColour("#d42b03");
  1052. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Turn_Off_Buzzer_TOOLTIP);
  1053. this.setHelpUrl("");
  1054. }
  1055. };
  1056. Blockly.Python['esp32_main_controller_turn_off_buzzer'] = function(block) {
  1057. Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
  1058. Blockly.Python.definitions_['buzzer_operation'] = '' +
  1059. 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' +
  1060. 'beeper.deinit()\n' +
  1061. /*
  1062. 'def _beeper_control(state, freq=31):\n' +
  1063. ' if freq <= 31: freq = 31\n' +
  1064. ' elif freq >= 67108863: freq = 67108863\n' +
  1065. ' if state == True:\n' +
  1066. ' beeper.init()\n' +
  1067. ' beeper.freq(freq)\n' +
  1068. ' time.sleep_ms(20)\n' +
  1069. ' elif state == False:\n' +
  1070. ' beeper.deinit()\n' +
  1071. ' time.sleep_ms(20)\n' +
  1072. */
  1073. '\n';
  1074. var code = 'beeper.deinit()\n';
  1075. return code;
  1076. //return [code, Blockly.Python.ORDER_FUNCTION_CALL];
  1077. };
  1078. /*
  1079. TOUCH AREA
  1080. */
  1081. Blockly.Blocks['esp32_main_controller_touch_read_touched'] = {
  1082. init: function() {
  1083. // this.appendDummyInput()
  1084. // .appendField(new Blockly.FieldImage("blockly/media/touched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1085. this.appendDummyInput()
  1086. .appendField(Blockly.Msg.ESP32TA)
  1087. .appendField(new Blockly.FieldDropdown([
  1088. ["A", "touch_area_type_a"],
  1089. ["B", "touch_area_type_b"]
  1090. ]), "touch_area_type")
  1091. .appendField(Blockly.Msg.ESP32_TOUCHED)
  1092. this.setOutput(true, null);
  1093. this.setColour("#d42b03");
  1094. this.setHelpUrl("");
  1095. var thisBlock = this;
  1096. this.setTooltip(function() {
  1097. var mode = thisBlock.getFieldValue('touch_area_type');
  1098. var TOOLTIPS = {
  1099. 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "A"),
  1100. 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "B")
  1101. };
  1102. return TOOLTIPS[mode];
  1103. });
  1104. }
  1105. };
  1106. Blockly.Python['esp32_main_controller_touch_read_touched'] = function(block) {
  1107. Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
  1108. 'from machine import Pin\n' +
  1109. '\n' +
  1110. '_TOUCH_A = TouchPad(Pin(4))\n' +
  1111. '_TOUCH_B = TouchPad(Pin(27))\n' +
  1112. '\n' +
  1113. 'def GetTouchValue(area):\n' +
  1114. ' try:\n' +
  1115. ' if area == "A":\n' +
  1116. ' return _TOUCH_A.read()\n' +
  1117. ' elif area == "B":\n' +
  1118. ' return _TOUCH_B.read()\n' +
  1119. ' except BaseException as e:\n' +
  1120. ' return False\n' +
  1121. '';
  1122. // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
  1123. var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") < 100';
  1124. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1125. };
  1126. Blockly.Blocks['esp32_main_controller_touch_read_untouched'] = {
  1127. init: function() {
  1128. // this.appendDummyInput()
  1129. // .appendField(new Blockly.FieldImage("blockly/media/untouched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1130. this.appendDummyInput()
  1131. .appendField(Blockly.Msg.ESP32TA)
  1132. .appendField(new Blockly.FieldDropdown([
  1133. ["A", "touch_area_type_a"],
  1134. ["B", "touch_area_type_b"]
  1135. ]), "touch_area_type")
  1136. .appendField(Blockly.Msg.ESP32_UNTOUCHED)
  1137. this.setOutput(true, null);
  1138. this.setColour("#d42b03");
  1139. this.setHelpUrl("");
  1140. var thisBlock = this;
  1141. this.setTooltip(function() {
  1142. var mode = thisBlock.getFieldValue('touch_area_type');
  1143. var TOOLTIPS = {
  1144. 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "A"),
  1145. 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "B")
  1146. };
  1147. return TOOLTIPS[mode];
  1148. });
  1149. }
  1150. };
  1151. Blockly.Python['esp32_main_controller_touch_read_untouched'] = function(block) {
  1152. Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
  1153. 'from machine import Pin\n' +
  1154. '\n' +
  1155. '_TOUCH_A = TouchPad(Pin(4))\n' +
  1156. '_TOUCH_B = TouchPad(Pin(27))\n' +
  1157. '\n' +
  1158. 'def GetTouchValue(area):\n' +
  1159. ' try:\n' +
  1160. ' if area == "A":\n' +
  1161. ' return _TOUCH_A.read()\n' +
  1162. ' elif area == "B":\n' +
  1163. ' return _TOUCH_B.read()\n' +
  1164. ' except BaseException as e:\n' +
  1165. ' return False\n' +
  1166. '';
  1167. // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
  1168. var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") >= 100';
  1169. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1170. };
  1171. Blockly.Blocks['esp32_main_controller_touch_read_value'] = {
  1172. init: function() {
  1173. this.appendDummyInput()
  1174. .appendField(Blockly.Msg.ESP32_READ)
  1175. .appendField(new Blockly.FieldDropdown([
  1176. ["A", "touch_area_type_a"],
  1177. ["B", "touch_area_type_b"]
  1178. ]), "touch_area_type")
  1179. .appendField(Blockly.Msg.ESP32_READ_VALUE);
  1180. this.setInputsInline(false);
  1181. this.setOutput(true, null);
  1182. this.setColour("#d42b03");
  1183. this.setHelpUrl("");
  1184. var thisBlock = this;
  1185. this.setTooltip(function() {
  1186. var mode = thisBlock.getFieldValue('touch_area_type');
  1187. var TOOLTIPS = {
  1188. 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "A"),
  1189. 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "B")
  1190. };
  1191. return TOOLTIPS[mode];
  1192. });
  1193. }
  1194. };
  1195. Blockly.Python['esp32_main_controller_touch_read_value'] = function(block) {
  1196. Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
  1197. 'from machine import Pin\n' +
  1198. '\n' +
  1199. '_TOUCH_A = TouchPad(Pin(4))\n' +
  1200. '_TOUCH_B = TouchPad(Pin(27))\n' +
  1201. '\n' +
  1202. 'def GetTouchValue(area):\n' +
  1203. ' try:\n' +
  1204. ' if area == "A":\n' +
  1205. ' return _TOUCH_A.read()\n' +
  1206. ' elif area == "B":\n' +
  1207. ' return _TOUCH_B.read()\n' +
  1208. ' except BaseException as e:\n' +
  1209. ' return False\n' +
  1210. '';
  1211. // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
  1212. var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '")';
  1213. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1214. };
  1215. /*
  1216. Env Sensor: BME280
  1217. */
  1218. Blockly.Blocks['esp32_main_controller_get_environmental_value'] = {
  1219. init: function() {
  1220. // this.appendDummyInput()
  1221. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1222. this.appendDummyInput()
  1223. .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
  1224. .appendField(new Blockly.FieldDropdown([
  1225. [Blockly.Msg.basic_temperature, "temperature"],
  1226. [Blockly.Msg.basic_humidity, "humidity"]
  1227. // [Blockly.Msg.basic_pressure, "pressure"]
  1228. ]), "env_get_type")
  1229. .appendField(Blockly.Msg.basic_env_value);
  1230. this.setInputsInline(false);
  1231. this.setOutput(true, null);
  1232. this.setColour("#d42b03");
  1233. this.setHelpUrl("");
  1234. var thisBlock = this;
  1235. this.setTooltip(function() {
  1236. var mode = thisBlock.getFieldValue('env_get_type');
  1237. var TOOLTIPS = {
  1238. 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
  1239. 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
  1240. };
  1241. return TOOLTIPS[mode];
  1242. });
  1243. }
  1244. };
  1245. Blockly.Python['esp32_main_controller_get_environmental_value'] = function(block) {
  1246. var _type_selected = block.getFieldValue('env_get_type');
  1247. /*
  1248. from machine import Pin, I2C
  1249. import bme280 as BME280
  1250. _SENSOR_BME_PINS = I2C(scl=Pin(17), sda=Pin(16), freq=10000)
  1251. _BME = BME280.BME280(i2c=_SENSOR_BME_PINS)
  1252. */
  1253. Blockly.Python.definitions_['import_bme280'] = 'from machine import Pin, I2C\n' +
  1254. 'from aht10 import AHT10\n\n' +
  1255. '_aht10_i2c = I2C(sda=Pin(32), scl=Pin(33))\n' +
  1256. '_aht10_sensor = AHT10(_aht10_i2c)\n' +
  1257. '_aht10_sensor.sensor_init()\n' +
  1258. '_aht10_sensor.is_calibration_enabled()\n' +
  1259. '\n' +
  1260. 'def GetTemperatureValue():\n' +
  1261. ' try:\n' +
  1262. ' if _aht10_sensor.read_temperature() < 0:\n' +
  1263. ' return 0\n' +
  1264. ' elif _aht10_sensor.read_temperature() >= 0:\n' +
  1265. ' return _aht10_sensor.read_temperature()\n' +
  1266. ' except BaseException as e:\n' +
  1267. ' return False\n' +
  1268. '';
  1269. Blockly.Python.addVariable('_BME', '');
  1270. if (_type_selected == 'temperature') {
  1271. var code = 'GetTemperatureValue()';
  1272. } else if (_type_selected == 'humidity') {
  1273. var code = '_aht10_sensor.read_' + _type_selected + '()';
  1274. } else {
  1275. var code = _type_selected;
  1276. }
  1277. // TODO: Change ORDER_NONE to the correct strength.
  1278. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1279. };
  1280. /*
  1281. Motion Sensor: MPU6050
  1282. */
  1283. Blockly.Blocks['esp32_main_controller_motion_init'] = {
  1284. init: function() {
  1285. //this.appendDummyInput()
  1286. // .appendField(new Blockly.FieldImage("blockly/media/motion_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1287. this.appendDummyInput()
  1288. .appendField(Blockly.Msg.basic_motion_use);
  1289. this.setInputsInline(false);
  1290. this.setPreviousStatement(true, null);
  1291. this.setNextStatement(true, null);
  1292. this.setColour("#e8795b");
  1293. this.setTooltip("");
  1294. this.setHelpUrl("");
  1295. }
  1296. };
  1297. Blockly.Python['esp32_main_controller_motion_init'] = function(block) {
  1298. // Blockly.Python.addVariable('_SENSOR_MPU_PINS', '');
  1299. Blockly.Python.definitions_['import_mpu6050'] = '' +
  1300. 'from machine import I2C, Pin\n' +
  1301. 'import math\n' +
  1302. 'import mpu6050\n' +
  1303. '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1304. '';
  1305. var code = '' +
  1306. '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1307. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1308. '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1309. '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1310. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1311. '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1312. '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1313. '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1314. '';
  1315. return code;
  1316. };
  1317. Blockly.Blocks['esp32_main_controller_motion_when_shaking'] = {
  1318. init: function() {
  1319. this.appendDummyInput()
  1320. .appendField(Blockly.Msg.basic_when_shaking);
  1321. this.setInputsInline(false);
  1322. this.setOutput(true, null);
  1323. this.setColour("#e8795b");
  1324. this.setTooltip("");
  1325. this.setHelpUrl("");
  1326. }
  1327. };
  1328. Blockly.Python['esp32_main_controller_motion_when_shaking'] = function(block) {
  1329. Blockly.Python.definitions_['shaking_def'] = '' +
  1330. 'def _CHECK_SHAKING():\n' +
  1331. ' if _MPU_SHAKING_OFFSET < 0 and _SSTATE == 0:\n' +
  1332. ' _SSTATE = 1\n' +
  1333. ' return True\n' +
  1334. ' elif _MPU_SHAKING_OFFSET > 0 and _SSTATE == 1:\n' +
  1335. ' _SSTATE = 0\n' +
  1336. ' return False\n' +
  1337. '';
  1338. var code = '(_CHECK_SHAKING() == True)';
  1339. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1340. };
  1341. Blockly.Blocks['esp32_main_controller_motion_when_shaking_STATEMENT'] = {
  1342. init: function() {
  1343. this.appendDummyInput()
  1344. .appendField(Blockly.Msg.basic_when_shaking_when_module)
  1345. .appendField(new Blockly.FieldDropdown([
  1346. [Blockly.Msg.basic_when_shaking_when_is_in, "True"],
  1347. [Blockly.Msg.basic_when_shaking_when_is_not_in, "False"]
  1348. ]), "status")
  1349. .appendField(Blockly.Msg.basic_when_shaking_when_shaking);
  1350. this.appendStatementInput("input")
  1351. .setCheck(null)
  1352. .appendField(Blockly.Msg.basic_motion_statement_exec);
  1353. this.setPreviousStatement(true, null);
  1354. this.setNextStatement(true, null);
  1355. this.setColour("#d42b03");
  1356. this.setHelpUrl("");
  1357. var thisBlock = this;
  1358. this.setTooltip(function() {
  1359. var mode = thisBlock.getFieldValue('status');
  1360. var TOOLTIPS = {
  1361. 'True': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_in),
  1362. 'False': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_not_in)
  1363. };
  1364. return TOOLTIPS[mode];
  1365. });
  1366. }
  1367. };
  1368. Blockly.Python['esp32_main_controller_motion_when_shaking_STATEMENT'] = function(block) {
  1369. var dropdown_status = block.getFieldValue('status');
  1370. var statements_input = Blockly.Python.statementToCode(block, 'input');
  1371. Blockly.Python.definitions_['import_mpu6050'] = '' +
  1372. 'from machine import I2C, Pin\n' +
  1373. 'import math\n' +
  1374. 'import mpu6050\n' +
  1375. '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1376. '';
  1377. Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
  1378. '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1379. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1380. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1381. '';
  1382. Blockly.Python.definitions_['shaking_def'] = '' +
  1383. 'def _CHECK_SHAKING():\n' +
  1384. ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1385. ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1386. ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1387. ' _SSTATE = 0\n' +
  1388. ' if _MPU_SHAKING_OFFSET < 0:\n' +
  1389. ' _SSTATE = 1\n' +
  1390. ' return True\n' +
  1391. ' elif _MPU_SHAKING_OFFSET > 0:\n' +
  1392. ' _SSTATE = 0\n' +
  1393. ' return False\n' +
  1394. '';
  1395. var code = '' +
  1396. 'if (_CHECK_SHAKING() == ' + dropdown_status + '):\n' +
  1397. '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1398. '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1399. '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1400. '' + statements_input + '\n' +
  1401. '';
  1402. return code;
  1403. };
  1404. Blockly.Blocks['esp32_main_controller_motion_when_tilting'] = {
  1405. init: function() {
  1406. this.appendDummyInput()
  1407. .appendField(Blockly.Msg.basic_when_tilting_get)
  1408. .appendField(new Blockly.FieldDropdown([
  1409. [Blockly.Msg.basic_when_tilting_get_left, "L"],
  1410. [Blockly.Msg.basic_when_tilting_get_right, "R"],
  1411. [Blockly.Msg.basic_when_tilting_get_forward, "F"],
  1412. [Blockly.Msg.basic_when_tilting_get_backward, "B"]
  1413. ]), "tilt_type")
  1414. .appendField(Blockly.Msg.basic_when_tilting_get_when);
  1415. this.setInputsInline(false);
  1416. this.setOutput(true, null);
  1417. this.setColour("#e8795b");
  1418. this.setTooltip("");
  1419. this.setHelpUrl("");
  1420. }
  1421. };
  1422. Blockly.Python['esp32_main_controller_motion_when_tilting'] = function(block) {
  1423. var _type_selected = block.getFieldValue('tilt_type');
  1424. Blockly.Python.definitions_['tilting_def'] = '' +
  1425. 'def _CHECK_TILT_LR():\n' +
  1426. ' if _MPU_OFFSET_TILT_X < 160 and _MPU_OFFSET_TILT_X > 0:\n' +
  1427. ' return "R"\n' +
  1428. ' elif _MPU_OFFSET_TILT_X > 200 and _MPU_OFFSET_TILT_X < 360:\n' +
  1429. ' return "L"\n' +
  1430. ' else:\n' +
  1431. ' return None\n' +
  1432. '\n' +
  1433. 'def _CHECK_TILT_FB():\n' +
  1434. ' if _MPU_OFFSET_TILT_Y < 160 and _MPU_OFFSET_TILT_Y > 0:\n' +
  1435. ' return "F"\n' +
  1436. ' elif _MPU_OFFSET_TILT_Y > 200 and _MPU_OFFSET_TILT_Y < 360:\n' +
  1437. ' return "B"\n' +
  1438. ' else:\n' +
  1439. ' return None\n' +
  1440. '';
  1441. if (_type_selected == "L" || _type_selected == "R") {
  1442. _check_function = '(_CHECK_TILT_LR() == "';
  1443. } else if (_type_selected == "F" || _type_selected == "B") {
  1444. _check_function = '(_CHECK_TILT_FB() == "';
  1445. }
  1446. var code = _check_function + _type_selected + '")';
  1447. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1448. };
  1449. Blockly.Blocks['esp32_main_controller_motion_when_tilting_STATEMENT'] = {
  1450. init: function() {
  1451. this.appendDummyInput()
  1452. .appendField(Blockly.Msg.basic_when_tilting_get)
  1453. .appendField(new Blockly.FieldDropdown([
  1454. [Blockly.Msg.basic_when_tilting_get_left, "L"],
  1455. [Blockly.Msg.basic_when_tilting_get_right, "R"],
  1456. [Blockly.Msg.basic_when_tilting_get_forward, "F"],
  1457. [Blockly.Msg.basic_when_tilting_get_backward, "B"],
  1458. [Blockly.Msg.basic_when_tilting_not_tilting, "N"]
  1459. ]), "tilt_type")
  1460. .appendField(Blockly.Msg.basic_when_tilting_get_when);
  1461. this.appendStatementInput("input")
  1462. .setCheck(null)
  1463. .appendField(Blockly.Msg.basic_motion_statement_exec);
  1464. this.setPreviousStatement(true, null);
  1465. this.setNextStatement(true, null)
  1466. this.setColour("#d42b03");
  1467. var thisBlock = this;
  1468. this.setTooltip(function() {
  1469. var mode = thisBlock.getFieldValue('tilt_type');
  1470. var TOOLTIPS = {
  1471. 'L': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left),
  1472. 'R': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right),
  1473. 'F': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_forward),
  1474. 'B': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_backward),
  1475. 'N': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_not_tilting)
  1476. };
  1477. return TOOLTIPS[mode];
  1478. });
  1479. this.setHelpUrl("");
  1480. }
  1481. };
  1482. Blockly.Python['esp32_main_controller_motion_when_tilting_STATEMENT'] = function(block) {
  1483. var _type_selected = block.getFieldValue('tilt_type');
  1484. var statements_input = Blockly.Python.statementToCode(block, 'input');
  1485. Blockly.Python.definitions_['import_mpu6050'] = '' +
  1486. 'from machine import I2C, Pin\n' +
  1487. 'import math\n' +
  1488. 'import mpu6050\n' +
  1489. '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1490. '';
  1491. Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
  1492. '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n'
  1493. /* +
  1494. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1495. '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1496. '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1497. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1498. '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1499. '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1500. '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1501. ''*/
  1502. ;
  1503. Blockly.Python.definitions_['tilting_def'] = '' +
  1504. 'def _CHECK_TILT_DIRECTION():\n' +
  1505. ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1506. ' _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1507. ' _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1508. ' _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1509. ' if _MPU_OFFSET_TILT_X < 150 and _MPU_OFFSET_TILT_X > 0:\n' +
  1510. ' return "L"\n' +
  1511. ' elif _MPU_OFFSET_TILT_X > 210 and _MPU_OFFSET_TILT_X < 360:\n' +
  1512. ' return "R"\n' +
  1513. ' if _MPU_OFFSET_TILT_Y < 150 and _MPU_OFFSET_TILT_Y > 0:\n' +
  1514. ' return "B"\n' +
  1515. ' elif _MPU_OFFSET_TILT_Y > 210 and _MPU_OFFSET_TILT_Y < 360:\n' +
  1516. ' return "F"\n' +
  1517. ' else:\n' +
  1518. ' return "N"\n' +
  1519. '';
  1520. var _check_function = "(_CHECK_TILT_DIRECTION() == \"";
  1521. var code = '' +
  1522. 'if ' + _check_function + _type_selected + '"):\n' +
  1523. '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1524. '' + statements_input + '\n' +
  1525. '\n';
  1526. return code;
  1527. };
  1528. Blockly.Blocks['esp32_main_controller_motion_acceleration'] = {
  1529. init: function() {
  1530. this.appendDummyInput()
  1531. .appendField(Blockly.Msg.basic_motion_get)
  1532. .appendField(new Blockly.FieldDropdown([
  1533. ["X", "X"],
  1534. ["Y", "Y"],
  1535. ["Z", "Z"]
  1536. ]), "accel_type")
  1537. .appendField(Blockly.Msg.basic_motion_accel);
  1538. this.setInputsInline(false);
  1539. this.setOutput(true, null);
  1540. this.setColour("#d42b03");
  1541. var thisBlock = this;
  1542. this.setTooltip(function() {
  1543. var mode = thisBlock.getFieldValue('accel_type');
  1544. var TOOLTIPS = {
  1545. 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'X'),
  1546. 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Y'),
  1547. 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Z')
  1548. };
  1549. return TOOLTIPS[mode];
  1550. });
  1551. this.setHelpUrl("");
  1552. }
  1553. };
  1554. Blockly.Python['esp32_main_controller_motion_acceleration'] = function(block) {
  1555. var _type_selected = block.getFieldValue('accel_type');
  1556. Blockly.Python.definitions_['import_mpu6050'] = '' +
  1557. 'from machine import I2C, Pin\n' +
  1558. 'import math\n' +
  1559. 'import mpu6050\n' +
  1560. '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1561. '';
  1562. Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
  1563. '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1564. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1565. '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1566. '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1567. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1568. '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1569. '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1570. '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1571. '';
  1572. Blockly.Python.definitions_['mpu6050_get_accel'] = '' +
  1573. 'def mpu6050_get_accel(type):\n' +
  1574. '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1575. '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1576. '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1577. '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1578. '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1579. '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1580. '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1581. '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1582. '\tif type == \'X\':\n' +
  1583. '\t\treturn _MPU_ACCL_X\n' +
  1584. '\tif type == \'Y\':\n' +
  1585. '\t\treturn _MPU_ACCL_Y\n' +
  1586. '\tif type == \'Z\':\n' +
  1587. '\t\treturn _MPU_ACCL_Z\n' +
  1588. '';
  1589. // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
  1590. var code = 'mpu6050_get_accel(\'' + _type_selected + '\')';
  1591. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1592. };
  1593. Blockly.Blocks['esp32_main_controller_motion_tilt_angle'] = {
  1594. init: function() {
  1595. this.appendDummyInput()
  1596. .appendField(Blockly.Msg.basic_motion_get)
  1597. .appendField(new Blockly.FieldDropdown([
  1598. ["X", "X"],
  1599. ["Y", "Y"],
  1600. ["Z", "Z"]
  1601. ]), "tilt_angle_type")
  1602. .appendField(Blockly.Msg.basic_motion_angle);
  1603. this.setInputsInline(false);
  1604. this.setOutput(true, null);
  1605. this.setColour("#d42b03");
  1606. var thisBlock = this;
  1607. this.setTooltip(function() {
  1608. var mode = thisBlock.getFieldValue('tilt_angle_type');
  1609. var TOOLTIPS = {
  1610. 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'X'),
  1611. 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Y'),
  1612. 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Z')
  1613. };
  1614. return TOOLTIPS[mode];
  1615. });
  1616. this.setHelpUrl("");
  1617. }
  1618. };
  1619. Blockly.Python['esp32_main_controller_motion_tilt_angle'] = function(block) {
  1620. var _type_selected = block.getFieldValue('tilt_angle_type');
  1621. Blockly.Python.definitions_['import_mpu6050'] = '' +
  1622. 'from machine import I2C, Pin\n' +
  1623. 'import math\n' +
  1624. 'import mpu6050\n' +
  1625. '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1626. '';
  1627. Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
  1628. '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1629. '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1630. '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1631. '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1632. '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1633. '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1634. '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1635. '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1636. '';
  1637. Blockly.Python.definitions_['mpu6050_get_angle'] = '' +
  1638. 'def mpu6050_get_tilt_angle(type):\n' +
  1639. '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
  1640. '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
  1641. '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
  1642. '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
  1643. '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
  1644. '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
  1645. '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
  1646. '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
  1647. '\tif type == \'X\':\n' +
  1648. '\t\treturn _MPU_OFFSET_TILT_X\n' +
  1649. '\tif type == \'Y\':\n' +
  1650. '\t\treturn _MPU_OFFSET_TILT_Y\n' +
  1651. '\tif type == \'Z\':\n' +
  1652. '\t\treturn _MPU_OFFSET_TILT_Z\n' +
  1653. '';
  1654. // Blockly.Python.addVariable('_MPU_OFFSET_TILT_' + _type_selected, '');
  1655. var code = 'mpu6050_get_tilt_angle(\'' + _type_selected + '\')';
  1656. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1657. };
  1658. /*
  1659. LIGHT & SOUND
  1660. */
  1661. Blockly.Blocks['esp32_main_controller_get_light'] = {
  1662. init: function() {
  1663. // this.appendDummyInput()
  1664. // .appendField(new Blockly.FieldImage("blockly/media/light_sensor.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  1665. this.appendDummyInput()
  1666. .appendField(Blockly.Msg.basic_light_get);
  1667. this.setInputsInline(false);
  1668. this.setOutput(true, null);
  1669. this.setColour("#d42b03");
  1670. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Light_TOOLTIP);
  1671. this.setHelpUrl("");
  1672. }
  1673. };
  1674. Blockly.Python['esp32_main_controller_get_light'] = function(block) {
  1675. Blockly.Python.definitions_['light_import_def'] = '' +
  1676. 'from machine import Pin, ADC\n' +
  1677. '_PHOTONRESISTOR = machine.ADC(Pin(39))\n' +
  1678. '_PHOTONRESISTOR.atten(ADC.ATTN_11DB)\n' +
  1679. '_PHOTONRESISTOR.width(ADC.WIDTH_10BIT)\n' +
  1680. '';
  1681. Blockly.Python.addVariable('_PHOTONRESISTOR');
  1682. var code = '_PHOTONRESISTOR.read()';
  1683. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1684. };
  1685. /* time */
  1686. var ESP32_TIME_COLOR = "#fabe23";
  1687. Blockly.Blocks['esp32_main_controller_time_timer_init'] = {
  1688. init: function() {
  1689. this.appendDummyInput()
  1690. .appendField("")
  1691. .appendField(new Blockly.FieldDropdown([
  1692. [Blockly.Msg.time_iot_module, "on_esp32"],
  1693. [Blockly.Msg.time_ai_module, "on_ai"]
  1694. ]), "type")
  1695. .appendField(Blockly.Msg.time_init_timer);
  1696. this.setPreviousStatement(true, null);
  1697. this.setNextStatement(true, null);
  1698. this.setColour(ESP32_TIME_COLOR);
  1699. var thisBlock = this;
  1700. this.setTooltip(function() {
  1701. var mode = thisBlock.getFieldValue('type');
  1702. var TOOLTIPS = {
  1703. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1704. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1705. };
  1706. return TOOLTIPS[mode];
  1707. });
  1708. this.setHelpUrl("");
  1709. }
  1710. };
  1711. Blockly.Python['esp32_main_controller_time_timer_init'] = function(block) {
  1712. var timer_type = block.getFieldValue('type');
  1713. if (timer_type == "on_esp32") {
  1714. Blockly.Python.definitions_['import_def'] = '' +
  1715. 'import machine, time\n' +
  1716. '_TIMER = machine.Timer(0)\n' +
  1717. '';
  1718. var code = '_TIMER.init()\n';
  1719. } else if (timer_type == "on_ai") {
  1720. Blockly.Python.definitions_['import_def'] = '' +
  1721. 'import time\n' +
  1722. 'from machine import Timer\n' +
  1723. '\n' +
  1724. 'def _timer_on_timer(timer):\n' +
  1725. ' global _timer_current_time_elapsed\n' +
  1726. ' _timer_current_time_elapsed = _timer_current_time_elapsed + 1\n' +
  1727. '\n' +
  1728. '_timer_current_time_elapsed = 0\n' +
  1729. '_timer_tim = Timer(Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PERIODIC, period=1, callback=_timer_on_timer)\n' +
  1730. '';
  1731. var code = '_timer_tim.start()\n';
  1732. }
  1733. return code;
  1734. };
  1735. Blockly.Blocks['esp32_main_controller_time_timer_get_current'] = {
  1736. init: function() {
  1737. this.appendDummyInput()
  1738. .appendField("")
  1739. .appendField(new Blockly.FieldDropdown([
  1740. [Blockly.Msg.time_iot_module, "on_esp32"],
  1741. [Blockly.Msg.time_ai_module, "on_ai"]
  1742. ]), "type")
  1743. .appendField(Blockly.Msg.time_timer_get_time);
  1744. this.setOutput(true, null);
  1745. this.setColour(ESP32_TIME_COLOR);
  1746. var thisBlock = this;
  1747. this.setTooltip(function() {
  1748. var mode = thisBlock.getFieldValue('type');
  1749. var TOOLTIPS = {
  1750. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1751. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1752. };
  1753. return TOOLTIPS[mode];
  1754. });
  1755. this.setHelpUrl("");
  1756. }
  1757. };
  1758. Blockly.Python['esp32_main_controller_time_timer_get_current'] = function(block) {
  1759. var timer_type = block.getFieldValue('type');
  1760. // TODO: Assemble Python into code variable.
  1761. if (timer_type == "on_esp32") {
  1762. var code = '_TIMER.value()';
  1763. } else if (timer_type == "on_ai") {
  1764. var code = '_timer_current_time_elapsed';
  1765. }
  1766. // TODO: Change ORDER_NONE to the correct strength.
  1767. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1768. };
  1769. Blockly.Blocks['esp32_main_controller_time_timer_clear'] = {
  1770. init: function() {
  1771. this.appendDummyInput()
  1772. .appendField("")
  1773. .appendField(new Blockly.FieldDropdown([
  1774. [Blockly.Msg.time_iot_module, "on_esp32"],
  1775. [Blockly.Msg.time_ai_module, "on_ai"]
  1776. ]), "type")
  1777. .appendField(Blockly.Msg.time_timer_clear);
  1778. this.setPreviousStatement(true, null);
  1779. this.setNextStatement(true, null);
  1780. this.setColour(ESP32_TIME_COLOR);
  1781. var thisBlock = this;
  1782. this.setTooltip(function() {
  1783. var mode = thisBlock.getFieldValue('type');
  1784. var TOOLTIPS = {
  1785. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1786. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1787. };
  1788. return TOOLTIPS[mode];
  1789. });
  1790. this.setHelpUrl("");
  1791. }
  1792. };
  1793. Blockly.Python['esp32_main_controller_time_timer_clear'] = function(block) {
  1794. var timer_type = block.getFieldValue('type');
  1795. // TODO: Assemble Python into code variable.
  1796. if (timer_type == "on_esp32") {
  1797. var code = '_TIMER.deinit()\n';
  1798. } else if (timer_type == "on_ai") {
  1799. var code = '_timer_tim.stop()\n';
  1800. }
  1801. return code;
  1802. };
  1803. Blockly.Blocks['esp32_main_controller_time_period_timer'] = {
  1804. init: function() {
  1805. this.appendDummyInput()
  1806. .appendField("")
  1807. .appendField(new Blockly.FieldDropdown([
  1808. [Blockly.Msg.time_iot_module, "on_esp32"],
  1809. [Blockly.Msg.time_ai_module, "on_ai"]
  1810. ]), "type")
  1811. .appendField(Blockly.Msg.time_timer_periodically);
  1812. this.appendDummyInput()
  1813. .appendField(Blockly.Msg.time_timer_every)
  1814. .appendField(new Blockly.FieldTextInput("500"), "period_timer_count")
  1815. .appendField(Blockly.Msg.time_timer_ms);
  1816. this.appendStatementInput("exec_period_timer")
  1817. .setCheck(null)
  1818. .appendField(Blockly.Msg.time_execution);
  1819. this.setPreviousStatement(true, null);
  1820. this.setNextStatement(true, null);
  1821. this.setColour(ESP32_TIME_COLOR);
  1822. var thisBlock = this;
  1823. this.setTooltip(function() {
  1824. var mode = thisBlock.getFieldValue('type');
  1825. var TOOLTIPS = {
  1826. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1827. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1828. };
  1829. return TOOLTIPS[mode];
  1830. });
  1831. this.setHelpUrl("");
  1832. }
  1833. };
  1834. Blockly.Python['esp32_main_controller_time_period_timer'] = function(block) {
  1835. var timer_type = block.getFieldValue('type');
  1836. var text_period_timer_count = block.getFieldValue('period_timer_count');
  1837. var statements_name = Blockly.Python.statementToCode(block, 'exec_period_timer');
  1838. if (timer_type == "on_esp32") {
  1839. Blockly.Python.definitions_['import_def'] = '' +
  1840. 'from machine import Timer\n' +
  1841. '\n' +
  1842. 'def _PRD_EXEC_tick(_):\n' +
  1843. ' global _ControlTopic, _ReportTopic\n' +
  1844. statements_name +
  1845. '\n' +
  1846. '_PRD_EXEC_TIMER = Timer(0)\n' +
  1847. '_PRD_EXEC_TIMER.deinit()\n' +
  1848. '_PRD_EXEC_TIMER.init(period=' + text_period_timer_count + ', mode=Timer.PERIODIC, callback=_PRD_EXEC_tick)\n' +
  1849. '';
  1850. } else if (timer_type == "on_ai") {
  1851. Blockly.Python.definitions_['import_def'] = '' +
  1852. 'import time\n' +
  1853. 'from machine import Timer\n' +
  1854. '\n' +
  1855. 'def _on_timer(timer):\n' +
  1856. statements_name +
  1857. '\n' +
  1858. 'from machine import Timer\n' +
  1859. '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=' + text_period_timer_count + ', callback=_on_timer, arg=_on_timer)\n\n' +
  1860. '_periodic_tim.start()\n' +
  1861. '';
  1862. }
  1863. // TODO: Assemble Python into code variable.
  1864. var code = '';
  1865. return code;
  1866. };
  1867. Blockly.Blocks['esp32_main_controller_time_period_timer_clear'] = {
  1868. init: function() {
  1869. this.appendDummyInput()
  1870. .appendField("")
  1871. .appendField(new Blockly.FieldDropdown([
  1872. [Blockly.Msg.time_iot_module, "on_esp32"],
  1873. [Blockly.Msg.time_ai_module, "on_ai"]
  1874. ]), "type")
  1875. .appendField(Blockly.Msg.time_timer_periodically_clear);
  1876. this.setPreviousStatement(true, null);
  1877. this.setNextStatement(true, null);
  1878. this.setColour(ESP32_TIME_COLOR);
  1879. var thisBlock = this;
  1880. this.setTooltip(function() {
  1881. var mode = thisBlock.getFieldValue('type');
  1882. var TOOLTIPS = {
  1883. 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
  1884. 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
  1885. };
  1886. return TOOLTIPS[mode];
  1887. });
  1888. this.setHelpUrl("");
  1889. }
  1890. };
  1891. Blockly.Python['esp32_main_controller_time_period_timer_clear'] = function(block) {
  1892. var timer_type = block.getFieldValue('type');
  1893. // TODO: Assemble Python into code variable.
  1894. if (timer_type == "on_esp32") {
  1895. var code = '_PRD_EXEC_TIMER.deinit()\n';
  1896. } else if (timer_type == "on_ai") {
  1897. var code = '_periodic_tim.stop()\n';
  1898. }
  1899. return code;
  1900. };
  1901. Blockly.Blocks['esp32_get_current_date'] = {
  1902. init: function() {
  1903. this.appendDummyInput()
  1904. .appendField(Blockly.Msg.time_get_current_date_title)
  1905. .appendField(new Blockly.FieldDropdown([
  1906. [Blockly.Msg.time_get_current_date_y, "0"],
  1907. [Blockly.Msg.time_get_current_date_m, "1"],
  1908. [Blockly.Msg.time_get_current_date_d, "2"],
  1909. [Blockly.Msg.time_get_current_date_hour, "3"],
  1910. [Blockly.Msg.time_get_current_date_min, "4"],
  1911. [Blockly.Msg.time_get_current_date_sec, "5"]
  1912. ]), "type");
  1913. this.setInputsInline(false);
  1914. this.setOutput(true, null);
  1915. this.setColour(ESP32_TIME_COLOR);
  1916. this.setHelpUrl("");
  1917. var thisBlock = this;
  1918. this.setTooltip(function() {
  1919. var mode = thisBlock.getFieldValue('type');
  1920. var TOOLTIPS = {
  1921. '0': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_y),
  1922. '1': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_m),
  1923. '2': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_d),
  1924. '3': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_hour),
  1925. '4': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_min),
  1926. '5': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_sec)
  1927. };
  1928. return TOOLTIPS[mode];
  1929. });
  1930. }
  1931. };
  1932. Blockly.Python['esp32_get_current_date'] = function(block) {
  1933. var dropdown_type = block.getFieldValue('type');
  1934. Blockly.Python.definitions_['get_date_current_ntp'] = '' +
  1935. 'import time\n' +
  1936. '\n';
  1937. // TODO: Assemble Python into code variable.
  1938. var code = 'time.localtime()[' + dropdown_type + ']';
  1939. // TODO: Change ORDER_NONE to the correct strength.
  1940. return [code, Blockly.Python.ORDER_NONE];
  1941. };
  1942. /*
  1943. LCD
  1944. */
  1945. var ESP32_SCREEN_COLOR = "#5bb2d6";
  1946. Blockly.Blocks['esp32_main_controller_lcd_rotation'] = {
  1947. init: function() {
  1948. this.appendDummyInput()
  1949. .appendField(Blockly.Msg.OLCD_SETROTATION)
  1950. .appendField(new Blockly.FieldDropdown([
  1951. ["0°", "1"],
  1952. ["90°", "2"],
  1953. ["180°", "3"],
  1954. ["270°", "0"]
  1955. ]), "DEGREE")
  1956. .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
  1957. this.setPreviousStatement(true, null);
  1958. this.setNextStatement(true, null);
  1959. this.setColour("#5bb2d6");
  1960. this.setTooltip('');
  1961. this.setHelpUrl('');
  1962. }
  1963. }
  1964. Blockly.Python['esp32_main_controller_lcd_rotation'] = function(block) {
  1965. var degree = block.getFieldValue("DEGREE");
  1966. var code = "tft.rotation(" + degree + ")\n";
  1967. // Blockly.Python.addSetup("setRotation", code);
  1968. return code;
  1969. }
  1970. Blockly.Blocks['esp32_main_controller_clear_screen'] = {
  1971. init: function() {
  1972. this.appendDummyInput()
  1973. .appendField(Blockly.Msg.display_clear_screen);
  1974. this.setInputsInline(true);
  1975. this.setPreviousStatement(true, null);
  1976. this.setNextStatement(true, null);
  1977. this.setColour(ESP32_SCREEN_COLOR);
  1978. this.setTooltip("");
  1979. this.setHelpUrl("");
  1980. }
  1981. };
  1982. Blockly.Python['esp32_main_controller_clear_screen'] = function(block) {
  1983. // TODO: Assemble Python into code variable.
  1984. var code = 'tft.fill(TFT.BLACK)\n';
  1985. return code;
  1986. };
  1987. Blockly.Blocks['esp32_main_controller_lcd_screen_fill'] = {
  1988. init: function() {
  1989. this.appendDummyInput()
  1990. .appendField(Blockly.Msg.display_fill_screen);
  1991. this.appendValueInput("COLOR")
  1992. .setCheck(null)
  1993. this.setInputsInline(true);
  1994. this.setPreviousStatement(true, null);
  1995. this.setNextStatement(true, null);
  1996. this.setColour(ESP32_SCREEN_COLOR);
  1997. this.setTooltip("");
  1998. this.setHelpUrl("");
  1999. }
  2000. };
  2001. Blockly.Python['esp32_main_controller_lcd_screen_fill'] = function(block) {
  2002. var value_lcd_rgb_value = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2003. // TODO: Assemble Python into code variable.
  2004. if (value_lcd_rgb_value.charAt(0) != '#') {
  2005. var code = 'tft.fill(_TFTColor' + value_lcd_rgb_value + ')\n';
  2006. } else if (value_lcd_rgb_value.charAt(0) == '#') {
  2007. var d = 0,
  2008. e = 0,
  2009. f = 0;
  2010. try {
  2011. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  2012. e = parseInt(color.substring(3, 5), 16),
  2013. f = parseInt(color.substring(5, 7), 16))
  2014. } catch (g) {}
  2015. var code = "tft.fill(_TFTColor" + d + "," + e + "," + f + ")\n";
  2016. }
  2017. return code;
  2018. };
  2019. Blockly.Blocks['esp32_main_controller_lcd_drawpixel'] = {
  2020. init: function() {
  2021. this.appendDummyInput()
  2022. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_pixel.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2023. this.appendDummyInput()
  2024. .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_pixel);
  2025. this.appendValueInput("COLOR")
  2026. .setCheck("String")
  2027. .appendField(Blockly.Msg.OLCD_COLOR);
  2028. this.appendValueInput("POS")
  2029. .setCheck("String")
  2030. .appendField(Blockly.Msg.OLCD_COORDINATE);
  2031. this.setInputsInline(false);
  2032. this.setPreviousStatement(true);
  2033. this.setNextStatement(true);
  2034. this.setColour("#5bb2d6");
  2035. this.setTooltip('');
  2036. this.setHelpUrl('');
  2037. }
  2038. };
  2039. Blockly.Python['esp32_main_controller_lcd_drawpixel'] = function(block) {
  2040. // var drawrectcolor = sharpTo565(
  2041. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  2042. var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  2043. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2044. if (color.charAt(0) == '#') {
  2045. var d = 0,
  2046. e = 0,
  2047. f = 0;
  2048. try {
  2049. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  2050. e = parseInt(color.substring(3, 5), 16),
  2051. f = parseInt(color.substring(5, 7), 16))
  2052. } catch (g) {}
  2053. code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + d + "," + e + "," + f + ")\n"
  2054. } else if (color.charAt(0) != '#') {
  2055. code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + color + ")\n"
  2056. }
  2057. return code;
  2058. };
  2059. Blockly.Blocks['esp32_main_controller_lcd_drawline'] = {
  2060. init: function() {
  2061. this.appendDummyInput()
  2062. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2063. // this.appendDummyInput()
  2064. // .appendField(
  2065. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  2066. this.appendDummyInput()
  2067. .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_LINE);
  2068. this.appendValueInput("COLOR")
  2069. .setCheck("String")
  2070. .appendField(Blockly.Msg.OLCD_COLOR);
  2071. this.appendValueInput("POSA")
  2072. .setCheck("String")
  2073. .appendField(Blockly.Msg.OLCD_LINE_START);
  2074. this.appendValueInput("POSB")
  2075. .setCheck("String")
  2076. .appendField(Blockly.Msg.OLCD_LINE_END);
  2077. this.setInputsInline(false);
  2078. this.setPreviousStatement(true);
  2079. this.setNextStatement(true);
  2080. this.setColour("#5bb2d6");
  2081. this.setTooltip('');
  2082. this.setHelpUrl('');
  2083. }
  2084. };
  2085. Blockly.Python['esp32_main_controller_lcd_drawline'] = function(block) {
  2086. // var drawlinecolor = sharpTo565(
  2087. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  2088. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  2089. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  2090. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2091. if (color.charAt(0) == '#') {
  2092. var d = 0,
  2093. e = 0,
  2094. f = 0;
  2095. try {
  2096. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  2097. e = parseInt(color.substring(3, 5), 16),
  2098. f = parseInt(color.substring(5, 7), 16))
  2099. } catch (g) {}
  2100. code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + d + "," + e + "," + f + ")\n"
  2101. } else if (color.charAt(0) != '#') {
  2102. code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
  2103. }
  2104. return code;
  2105. };
  2106. Blockly.Blocks['esp32_main_controller_lcd_drawlinelen'] = {
  2107. init: function() {
  2108. this.appendDummyInput()
  2109. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2110. // this.appendDummyInput()
  2111. // .appendField(
  2112. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  2113. this.appendDummyInput()
  2114. .appendField(Blockly.Msg.OLCD_DRAW)
  2115. .appendField(new Blockly.FieldDropdown([
  2116. [Blockly.Msg.display_draw_vertical, "vline"],
  2117. [Blockly.Msg.display_draw_horizontal, "hline"]
  2118. ]), "rect_type")
  2119. .appendField(Blockly.Msg.OLCD_LINE)
  2120. this.appendValueInput("COLOR")
  2121. .setCheck(null)
  2122. .appendField(Blockly.Msg.OLCD_COLOR);
  2123. this.appendValueInput("POS")
  2124. .setCheck(null)
  2125. .appendField(Blockly.Msg.display_start_cord);
  2126. this.appendValueInput("LENGTH")
  2127. .setCheck(null)
  2128. .appendField(Blockly.Msg.display_length);
  2129. this.setInputsInline(false);
  2130. this.setPreviousStatement(true);
  2131. this.setNextStatement(true);
  2132. this.setColour("#5bb2d6");
  2133. this.setTooltip('');
  2134. this.setHelpUrl('');
  2135. }
  2136. };
  2137. Blockly.Python['esp32_main_controller_lcd_drawlinelen'] = function(block) {
  2138. var _type = block.getFieldValue("rect_type");
  2139. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2140. var drawlinestart = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
  2141. var drawlineend = Blockly.Python.valueToCode(block, 'LENGTH', Blockly.Python.ORDER_ATOMIC);
  2142. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2143. code = "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", _TFTColor" + color + ")\n"
  2144. return code;
  2145. };
  2146. Blockly.Blocks['esp32_main_controller_lcd_drawrect'] = {
  2147. init: function() {
  2148. this.appendDummyInput()
  2149. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2150. // this.appendDummyInput()
  2151. // .appendField(
  2152. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  2153. this.appendDummyInput()
  2154. .appendField(Blockly.Msg.OLCD_DRAW)
  2155. .appendField(new Blockly.FieldDropdown([
  2156. [Blockly.Msg.OLCD_FULL, "fillrect"],
  2157. [Blockly.Msg.OLCD_CIRCLE, "rect"]
  2158. ]), "rect_type")
  2159. .appendField(Blockly.Msg.display_draw_rectangle);
  2160. this.appendValueInput("COLOR")
  2161. .setCheck("String")
  2162. .appendField(Blockly.Msg.OLCD_COLOR);
  2163. this.appendValueInput("POSA")
  2164. .setCheck("String")
  2165. .appendField(Blockly.Msg.OLCD_COORDINATE);
  2166. this.appendValueInput("POSB")
  2167. .setCheck("String")
  2168. .appendField(Blockly.Msg.OLCD_SIZE);
  2169. this.setInputsInline(false);
  2170. this.setPreviousStatement(true);
  2171. this.setNextStatement(true);
  2172. this.setColour("#5bb2d6");
  2173. this.setTooltip('');
  2174. this.setHelpUrl('');
  2175. }
  2176. };
  2177. Blockly.Python['esp32_main_controller_lcd_drawrect'] = function(block) {
  2178. // var drawlinecolor = sharpTo565(
  2179. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  2180. var _type = block.getFieldValue("rect_type");
  2181. var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  2182. var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  2183. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2184. code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
  2185. return code;
  2186. };
  2187. Blockly.Blocks['esp32_main_controller_lcd_drawcircle'] = {
  2188. init: function() {
  2189. this.appendDummyInput()
  2190. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2191. this.appendDummyInput()
  2192. .appendField(Blockly.Msg.OLCD_DRAW)
  2193. .appendField(new Blockly.FieldDropdown([
  2194. [Blockly.Msg.OLCD_FULL, "fillcircle"],
  2195. [Blockly.Msg.OLCD_STROKED, "circle"]
  2196. ]), "circle_type")
  2197. .appendField(Blockly.Msg.OLCD_CIRCLE);
  2198. this.appendValueInput("COLOR")
  2199. .setCheck("String")
  2200. .appendField(Blockly.Msg.OLCD_COLOR);
  2201. this.appendValueInput("POSA")
  2202. .setCheck("String")
  2203. .appendField(Blockly.Msg.OLCD_CENTER_POS);
  2204. this.appendValueInput("RADIUS")
  2205. .setCheck(null)
  2206. .appendField(Blockly.Msg.OLCD_RADIUS);
  2207. this.setInputsInline(false);
  2208. this.setPreviousStatement(true);
  2209. this.setNextStatement(true);
  2210. this.setColour("#5bb2d6");
  2211. this.setTooltip('');
  2212. this.setHelpUrl('');
  2213. }
  2214. };
  2215. Blockly.Python['esp32_main_controller_lcd_drawcircle'] = function(block) {
  2216. // var drawcirclecolor = sharpTo565(
  2217. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  2218. var circletype = block.getFieldValue('circle_type');
  2219. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  2220. var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
  2221. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2222. code = 'tft.' + circletype + '((' + position + '), ' + radius + ', _TFTColor' + color + ')\n';
  2223. return code;
  2224. };
  2225. Blockly.Blocks['esp32_main_controller_lcd_drawqrcode'] = {
  2226. init: function() {
  2227. this.appendDummyInput()
  2228. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_qr.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2229. // this.appendDummyInput()
  2230. // .appendField(
  2231. // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
  2232. this.appendDummyInput()
  2233. .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_drawqr_text);
  2234. this.appendValueInput("POSA")
  2235. .setCheck("String")
  2236. .appendField(Blockly.Msg.OLCD_SET_POS);
  2237. this.appendValueInput("CONTENT")
  2238. .setCheck("String")
  2239. .appendField(Blockly.Msg.display_drawqr_content);
  2240. this.appendValueInput("WIDTH")
  2241. .setCheck("Number")
  2242. .appendField(Blockly.Msg.display_drawqr_padding);
  2243. this.setInputsInline(false);
  2244. this.setPreviousStatement(true);
  2245. this.setNextStatement(true);
  2246. this.setColour("#5bb2d6");
  2247. this.setTooltip('');
  2248. this.setHelpUrl('');
  2249. }
  2250. };
  2251. Blockly.Python['esp32_main_controller_lcd_drawqrcode'] = function(block) {
  2252. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  2253. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  2254. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  2255. var code = 'tft.drawqrcode(' + '(' + position + '), ' + content + ', ' + width + ')\n';
  2256. return code;
  2257. };
  2258. Blockly.Blocks['esp32_main_controller_lcd_drawtext'] = {
  2259. init: function() {
  2260. this.appendDummyInput()
  2261. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
  2262. // this.appendDummyInput()
  2263. // .appendField(
  2264. // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
  2265. this.appendDummyInput()
  2266. .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_text);
  2267. this.appendValueInput("COLOR")
  2268. .setCheck("String")
  2269. .appendField(Blockly.Msg.OLCD_COLOR);
  2270. this.appendValueInput("POSA")
  2271. .setCheck("String")
  2272. .appendField(Blockly.Msg.display_start_cord);
  2273. this.appendValueInput("CONTENT")
  2274. .setCheck("String")
  2275. .appendField(Blockly.Msg.display_draw_text_content);
  2276. this.appendValueInput("SIZE")
  2277. .setCheck("Number")
  2278. .appendField(Blockly.Msg.display_draw_text_size);
  2279. this.appendDummyInput()
  2280. .appendField(Blockly.Msg.display_draw_text_return)
  2281. .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
  2282. this.setInputsInline(false);
  2283. this.setPreviousStatement(true);
  2284. this.setNextStatement(true);
  2285. this.setColour("#5bb2d6");
  2286. this.setTooltip('');
  2287. this.setHelpUrl('');
  2288. }
  2289. };
  2290. Blockly.Python['esp32_main_controller_lcd_drawtext'] = function(block) {
  2291. // var drawcirclecolor = sharpTo565(
  2292. // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
  2293. var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  2294. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  2295. var size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
  2296. var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
  2297. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  2298. code = 'tft.text((' + position + '), ' + content + ', _TFTColor' + color + ', sysfont, ' + size + ', nowrap=' + box + ')\n';
  2299. return code;
  2300. };
  2301. Blockly.Blocks['esp32_main_controller_rgb_value'] = {
  2302. init: function() {
  2303. this.appendValueInput("rgb_value_r")
  2304. .setCheck(null)
  2305. .appendField(Blockly.Msg.display_red);
  2306. this.appendValueInput("rgb_value_g")
  2307. .setCheck(null)
  2308. .appendField(Blockly.Msg.display_green);
  2309. this.appendValueInput("rgb_value_b")
  2310. .setCheck(null)
  2311. .appendField(Blockly.Msg.display_blue);
  2312. this.setInputsInline(true);
  2313. this.setOutput(true, null);
  2314. this.setColour(ESP32_SCREEN_COLOR);
  2315. this.setTooltip("");
  2316. this.setHelpUrl("");
  2317. }
  2318. };
  2319. Blockly.Python['esp32_main_controller_rgb_value'] = function(block) {
  2320. var r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  2321. var g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  2322. var b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  2323. var original_r = parseInt(r, 10);
  2324. var original_g = parseInt(g, 10);
  2325. var original_b = parseInt(b, 10);
  2326. _r = original_r.toString(16);
  2327. _g = original_g.toString(16);
  2328. _b = original_b.toString(16);
  2329. if (_r.length == 1)
  2330. _r = "0" + _r;
  2331. if (_g.length == 1)
  2332. _g = "0" + _g;
  2333. if (_b.length == 1)
  2334. _b = "0" + _b;
  2335. // TODO: Assemble Python into code variable.
  2336. var code = '' + r + ',' + g + ',' + b + '';
  2337. // TODO: Change ORDER_NONE to the correct strength.
  2338. return [code, Blockly.Python.ORDER_NONE];
  2339. };
  2340. Blockly.Blocks['esp32_main_controller_lcd_color_hex_to_rgb'] = {
  2341. init: function() {
  2342. this.appendDummyInput()
  2343. .appendField(Blockly.Msg.COLOR)
  2344. .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
  2345. this.setOutput(true, null);
  2346. this.setColour("#5bb2d6");
  2347. this.setTooltip('');
  2348. this.setHelpUrl('');
  2349. }
  2350. };
  2351. Blockly.Python['esp32_main_controller_lcd_color_hex_to_rgb'] = function(a) {
  2352. a = a.getFieldValue("COLOR");
  2353. // var d = 0
  2354. // , e = 0
  2355. // , f = 0;
  2356. // try {
  2357. // 7 == a.length && (d = a.substring(1, 3),
  2358. // e = a.substring(3, 5),
  2359. // f = a.substring(5, 7));
  2360. // }
  2361. // catch (g) { }
  2362. var gethex = a;
  2363. function hexToRGB(h) {
  2364. let r = 0,
  2365. g = 0,
  2366. b = 0;
  2367. // 3 digits
  2368. if (h.length == 4) {
  2369. r = "0x" + h[1] + h[1];
  2370. g = "0x" + h[2] + h[2];
  2371. b = "0x" + h[3] + h[3];
  2372. // 6 digits
  2373. } else if (h.length == 7) {
  2374. r = "0x" + h[1] + h[2];
  2375. g = "0x" + h[3] + h[4];
  2376. b = "0x" + h[5] + h[6];
  2377. }
  2378. return "" + +r + "," + +g + "," + +b + "";
  2379. }
  2380. var _code = hexToRGB(gethex);
  2381. //_code += "tft.fill(TFT.TFTColor(FS_COLOR))\n";
  2382. return [_code, Blockly.Python.ORDER_CONDITIONAL];
  2383. };
  2384. /*
  2385. WiFi
  2386. */
  2387. var ESP32_WIFI_COLOR = "#3062c1";
  2388. Blockly.Blocks['esp32_main_controller_wifi_enable_hotspot_mode'] = {
  2389. init: function() {
  2390. this.appendDummyInput()
  2391. .appendField(new Blockly.FieldImage("blockly/media/hotspot_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  2392. this.appendDummyInput()
  2393. .appendField(Blockly.Msg.wifi_setup_hotspot);
  2394. this.appendValueInput("SSID")
  2395. .setCheck(null)
  2396. .appendField(Blockly.Msg.wifi_hotspot_ssid);
  2397. this.appendValueInput("PASSWORD")
  2398. .setCheck(null)
  2399. .appendField(Blockly.Msg.wifi_hotspot_pass);
  2400. this.setInputsInline(false);
  2401. this.setPreviousStatement(true, null);
  2402. this.setNextStatement(true, null);
  2403. this.setColour(ESP32_WIFI_COLOR);
  2404. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Enable_Hotspot_Mode_TOOLTIP);
  2405. this.setHelpUrl("");
  2406. }
  2407. };
  2408. Blockly.Python['esp32_main_controller_wifi_enable_hotspot_mode'] = function(block) {
  2409. var text_ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC);
  2410. var text_password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC);
  2411. var _code = '' +
  2412. '_WIFI = network.WLAN(network.STA_IF)\n' +
  2413. '_WIFI.active(False)\n' +
  2414. '_WIFI = network.WLAN(network.AP_IF)\n' +
  2415. '_WIFI.active(True)\n';
  2416. Blockly.Python.definitions_['import_network'] = 'import network\n' +
  2417. _code +
  2418. '_WIFI.config(essid=' + text_ssid + ', password=' + text_password + ', authmode=2, channel=11, hidden=0)\n' +
  2419. '\n';
  2420. code = '';
  2421. return code;
  2422. };
  2423. Blockly.Blocks['esp32_main_controller_wifi_connect_internet'] = {
  2424. init: function() {
  2425. this.appendDummyInput()
  2426. .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  2427. this.appendDummyInput()
  2428. .appendField(Blockly.Msg.wifi_connect_router);
  2429. this.appendValueInput("ssid")
  2430. .setCheck(null)
  2431. .appendField(Blockly.Msg.wifi_router_ssid);
  2432. this.appendValueInput("password")
  2433. .setCheck(null)
  2434. .appendField(Blockly.Msg.wifi_router_pass);
  2435. this.setInputsInline(false);
  2436. this.setPreviousStatement(true, null);
  2437. this.setNextStatement(true, null);
  2438. this.setColour(ESP32_WIFI_COLOR);
  2439. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Connect_Internet_TOOLTIP);
  2440. this.setHelpUrl("");
  2441. }
  2442. };
  2443. Blockly.Python['esp32_main_controller_wifi_connect_internet'] = function(block) {
  2444. var text_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
  2445. var text_password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
  2446. Blockly.Python.definitions_['esp32_import_network'] = '' +
  2447. 'import network\n' +
  2448. 'if (network.WLAN(network.STA_IF).isconnected()) == False:\n' +
  2449. ' _WIFI = network.WLAN(network.AP_IF)\n' +
  2450. ' _WIFI.active(False)\n' +
  2451. ' _WIFI = network.WLAN(network.STA_IF)\n' +
  2452. ' _WIFI.active(True)\n' +
  2453. ' _WIFI.connect(' + text_ssid + ', ' + text_password + ')\n' +
  2454. 'else:\n' +
  2455. ' _WIFI = network.WLAN(network.STA_IF)\n' +
  2456. ' pass\n' +
  2457. '\n';
  2458. var code = '';
  2459. return code;
  2460. };
  2461. Blockly.Blocks['esp32_main_controller_wifi_disconnect'] = {
  2462. init: function() {
  2463. this.appendDummyInput()
  2464. .appendField(Blockly.Msg.wifi_router_disconnect);
  2465. this.setInputsInline(false);
  2466. this.setPreviousStatement(true, null);
  2467. this.setNextStatement(true, null);
  2468. this.setColour(ESP32_WIFI_COLOR);
  2469. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Disconnect_TOOLTIP);
  2470. this.setHelpUrl("");
  2471. }
  2472. };
  2473. Blockly.Python['esp32_main_controller_wifi_disconnect'] = function(block) {
  2474. // TODO: Assemble Python into code variable.
  2475. var code = '_WIFI.active(False)\n';
  2476. return code;
  2477. };
  2478. Blockly.Blocks['esp32_main_controller_get_wifi_devices_number'] = {
  2479. init: function() {
  2480. this.appendDummyInput()
  2481. .appendField(Blockly.Msg.wifi_hotspot_get_clients);
  2482. this.setOutput(true, null);
  2483. this.setColour(ESP32_WIFI_COLOR);
  2484. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Devices_Number_TOOLTIP);
  2485. this.setHelpUrl("");
  2486. }
  2487. };
  2488. Blockly.Python['esp32_main_controller_get_wifi_devices_number'] = function(block) {
  2489. // TODO: Assemble Python into code variable.
  2490. var code = 'len(_WIFI.status("stations"))';
  2491. // TODO: Change ORDER_NONE to the correct strength.
  2492. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2493. };
  2494. Blockly.Blocks['esp32_main_controller_wifi_acquiring_equipment'] = {
  2495. init: function() {
  2496. this.appendDummyInput()
  2497. .appendField(Blockly.Msg.wifi_hotspot_get_list);
  2498. this.setOutput(true, null);
  2499. this.setColour(ESP32_WIFI_COLOR);
  2500. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Acquiring_Equipment_TOOLTIP);
  2501. this.setHelpUrl("");
  2502. }
  2503. };
  2504. Blockly.Python['esp32_main_controller_wifi_acquiring_equipment'] = function(block) {
  2505. // TODO: Assemble Python into code variable.
  2506. var code = '_WIFI.status("stations")';
  2507. // TODO: Change ORDER_NONE to the correct strength.
  2508. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2509. };
  2510. Blockly.Blocks['esp32_main_controller_get_wifi_connection_status'] = {
  2511. init: function() {
  2512. this.appendDummyInput()
  2513. .appendField(Blockly.Msg.wifi_router_connection_status);
  2514. this.setOutput(true, null);
  2515. this.setColour(ESP32_WIFI_COLOR);
  2516. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Connection_Status_TOOLTIP);
  2517. this.setHelpUrl("");
  2518. }
  2519. };
  2520. Blockly.Python['esp32_main_controller_get_wifi_connection_status'] = function(block) {
  2521. // TODO: Assemble Python into code variable.
  2522. var code = '_WIFI.isconnected()';
  2523. // TODO: Change ORDER_NONE to the correct strength.
  2524. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2525. };
  2526. Blockly.Blocks['esp32_main_controller_wifi_close'] = {
  2527. init: function() {
  2528. this.appendDummyInput()
  2529. .appendField(Blockly.Msg.wifi_close_hotspot);
  2530. this.setPreviousStatement(true, null);
  2531. this.setNextStatement(true, null);
  2532. this.setColour(ESP32_WIFI_COLOR);
  2533. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Close_TOOLTIP);
  2534. this.setHelpUrl("");
  2535. }
  2536. };
  2537. Blockly.Python['esp32_main_controller_wifi_close'] = function(block) {
  2538. // TODO: Assemble Python into code variable.
  2539. var code = '_WIFI.active(False)';
  2540. // TODO: Change ORDER_NONE to the correct strength.
  2541. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2542. };
  2543. Blockly.Blocks['esp32_main_controller_wifi_get_info'] = {
  2544. init: function() {
  2545. this.appendDummyInput()
  2546. .appendField(Blockly.Msg.wifi_router_get_info)
  2547. .appendField(new Blockly.FieldDropdown([
  2548. [Blockly.Msg.wifi_router_device_ip, "0"],
  2549. [Blockly.Msg.wifi_router_device_mask, "1"],
  2550. [Blockly.Msg.wifi_router_device_gateway, "2"],
  2551. [Blockly.Msg.wifi_router_device_dns, "3"]
  2552. ]), "wifi_info");
  2553. this.setOutput(true, null);
  2554. this.setColour(ESP32_WIFI_COLOR);
  2555. var thisBlock = this;
  2556. this.setTooltip(function() {
  2557. var mode = thisBlock.getFieldValue('wifi_info');
  2558. var TOOLTIPS = {
  2559. '0': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_ip),
  2560. '1': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_mask),
  2561. '2': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_gateway),
  2562. '3': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_dns)
  2563. };
  2564. return TOOLTIPS[mode];
  2565. });
  2566. this.setHelpUrl("");
  2567. }
  2568. };
  2569. Blockly.Python['esp32_main_controller_wifi_get_info'] = function(block) {
  2570. var dropdown_name = block.getFieldValue('wifi_info');
  2571. // TODO: Assemble Python into code variable.
  2572. var code = '_WIFI.ifconfig()[' + dropdown_name + ']';
  2573. // TODO: Change ORDER_NONE to the correct strength.
  2574. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2575. };
  2576. Blockly.Blocks['esp32_main_controller_wifi_ntptime'] = {
  2577. init: function() {
  2578. this.appendDummyInput()
  2579. .appendField(new Blockly.FieldImage("blockly/media/ntptime_header.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  2580. this.appendDummyInput()
  2581. .appendField(Blockly.Msg.wifi_ntptime_title);
  2582. this.appendDummyInput()
  2583. .appendField(Blockly.Msg.wifi_ntptime_set_timezone)
  2584. .appendField(new Blockly.FieldDropdown([
  2585. [Blockly.Msg.wifi_ntptime_utc8, "8"],
  2586. [Blockly.Msg.wifi_ntptime_utc9, "9"],
  2587. [Blockly.Msg.wifi_ntptime_utc10, "10"],
  2588. [Blockly.Msg.wifi_ntptime_utc11, "11"],
  2589. [Blockly.Msg.wifi_ntptime_utc12, "12"],
  2590. [Blockly.Msg.wifi_ntptime_utcm12, "-12"],
  2591. [Blockly.Msg.wifi_ntptime_utcm11, "-11"],
  2592. [Blockly.Msg.wifi_ntptime_utcm10, "-10"],
  2593. [Blockly.Msg.wifi_ntptime_utcm9, "-9"],
  2594. [Blockly.Msg.wifi_ntptime_utcm8, "-8"],
  2595. [Blockly.Msg.wifi_ntptime_utcm7, "-7"],
  2596. [Blockly.Msg.wifi_ntptime_utcm6, "-6"],
  2597. [Blockly.Msg.wifi_ntptime_utcm5, "-5"],
  2598. [Blockly.Msg.wifi_ntptime_utcm4, "-4"],
  2599. [Blockly.Msg.wifi_ntptime_utcm3, "-3"],
  2600. [Blockly.Msg.wifi_ntptime_utcm2, "-2"],
  2601. [Blockly.Msg.wifi_ntptime_utcm1, "-1"],
  2602. [Blockly.Msg.wifi_ntptime_utc0, "0"],
  2603. [Blockly.Msg.wifi_ntptime_utc1, "1"],
  2604. [Blockly.Msg.wifi_ntptime_utc2, "2"],
  2605. [Blockly.Msg.wifi_ntptime_utc3, "3"],
  2606. [Blockly.Msg.wifi_ntptime_utc4, "4"],
  2607. [Blockly.Msg.wifi_ntptime_utc5, "5"],
  2608. [Blockly.Msg.wifi_ntptime_utc6, "6"],
  2609. [Blockly.Msg.wifi_ntptime_utc7, "7"]
  2610. ]), "timezone");
  2611. this.appendDummyInput()
  2612. .appendField(Blockly.Msg.wifi_ntptime_time_server)
  2613. .appendField(new Blockly.FieldTextInput("ntp.ntsc.ac.cn"), "website");
  2614. this.setInputsInline(false);
  2615. this.setPreviousStatement(true, null);
  2616. this.setNextStatement(true, null);
  2617. this.setColour(ESP32_WIFI_COLOR);
  2618. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Ntptime_TOOLTIP);
  2619. this.setHelpUrl("");
  2620. }
  2621. };
  2622. Blockly.Python['esp32_main_controller_wifi_ntptime'] = function(block) {
  2623. var dropdown_timezone = block.getFieldValue('timezone');
  2624. var text_website = block.getFieldValue('website');
  2625. Blockly.Python.definitions_['esp32_main_controller_wifi_ntptime_def'] = '' +
  2626. 'import network\n' +
  2627. 'import ntptime\n' +
  2628. '\n';
  2629. // TODO: Assemble Python into code variable.
  2630. var code = 'ntptime.settime(' + dropdown_timezone + ', "' + text_website + '")\n';
  2631. return code;
  2632. };
  2633. /*
  2634. IoT Service
  2635. */
  2636. Blockly.Blocks['esp32_main_controller_ThingSpeak_send_data'] = {
  2637. init: function() {
  2638. this.appendDummyInput()
  2639. .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 190, 50, { alt: "*", flipRtl: "FALSE" }));
  2640. this.appendDummyInput()
  2641. .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
  2642. this.appendDummyInput()
  2643. .appendField(Blockly.Msg.iotservice_thingspeak_send_api_set)
  2644. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_API_KEY"), "key");
  2645. this.appendValueInput("data")
  2646. .setCheck(null)
  2647. .appendField(Blockly.Msg.iotservice_thingspeak_send_data_1);
  2648. this.setPreviousStatement(true, null);
  2649. this.setNextStatement(true, null);
  2650. this.setColour(ESP32_IOT_COLOR);
  2651. this.setTooltip("");
  2652. this.setHelpUrl("");
  2653. }
  2654. };
  2655. Blockly.Python['esp32_main_controller_ThingSpeak_send_data'] = function(block) {
  2656. var text_key = block.getFieldValue('key');
  2657. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  2658. Blockly.Python.definitions_['import_urequests'] = '' +
  2659. 'import urequests\n' +
  2660. 'import ujson as json\n'
  2661. '';
  2662. var code = '_THINGSPEAK_CHANNEL_API = "' + text_key + '"\n'
  2663. Blockly.Python.addSetup("esp32_main_controller_ThingSpeak_send_data", code);
  2664. var code = '_THINGSPEAK_DATA1 = ' + data + '\n';
  2665. code += '_THINGSPEAK_ENDPOINT = "http://api.thingspeak.com/update?api_key=" + _THINGSPEAK_CHANNEL_API + "&field1=" + _THINGSPEAK_DATA1\n';
  2666. code += '_THINGSPEAK_REQEUST = urequests.get(_THINGSPEAK_ENDPOINT)\n';
  2667. return code;
  2668. };
  2669. Blockly.Blocks['CocoRobo_set_max'] = {
  2670. init: function() {
  2671. this.appendDummyInput()
  2672. .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 50, { alt: "*", flipRtl: "FALSE" }));
  2673. this.appendDummyInput()
  2674. .appendField(Blockly.Msg.internet_server_setup);
  2675. this.appendDummyInput()
  2676. .appendField(Blockly.Msg.internet_server_set_max)
  2677. .appendField(new Blockly.FieldTextInput("5"), "number");
  2678. this.setPreviousStatement(true, null);
  2679. this.setNextStatement(true, null);
  2680. this.setColour("#183895");
  2681. this.setTooltip("");
  2682. this.setHelpUrl("");
  2683. }
  2684. };
  2685. Blockly.Python['CocoRobo_set_max'] = function(block) {
  2686. var number = block.getFieldValue('number');
  2687. Blockly.Python.definitions_['import_socket'] = 'import usocket as socket';
  2688. var code = '_S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n';
  2689. code += '_S.bind((_WIFI.ifconfig()[0], 80))\n';
  2690. code += '_S.listen(' + number + ')\n';
  2691. Blockly.Python.addSetup("CocoRobo_set_max", code);
  2692. var code = '_SERVER_CONN, _SERVER_ADDR = _S.accept()\n';
  2693. code += '_SERVER_REQUEST = _CONN.recv(1024)\n';
  2694. return code;
  2695. };
  2696. Blockly.Blocks['CocoRobo_get_source'] = {
  2697. init: function() {
  2698. this.appendDummyInput()
  2699. .appendField(Blockly.Msg.internet_server_set_source);
  2700. this.setOutput(true, null);
  2701. this.setColour("#183895");
  2702. this.setTooltip("");
  2703. this.setHelpUrl("");
  2704. }
  2705. };
  2706. Blockly.Python['CocoRobo_get_source'] = function(block) {
  2707. var code = 'str(_SERVER_ADDR)\n';
  2708. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2709. };
  2710. Blockly.Blocks['CocoRobo_get'] = {
  2711. init: function() {
  2712. this.appendDummyInput()
  2713. .appendField(Blockly.Msg.internet_response_http_content_title)
  2714. .appendField(new Blockly.FieldDropdown([
  2715. [Blockly.Msg.internet_response_text, "text"],
  2716. [Blockly.Msg.internet_response_content, "content"],
  2717. [Blockly.Msg.internet_response_status, "state"],
  2718. [Blockly.Msg.internet_response_json, "json"],
  2719. [Blockly.Msg.internet_response_encode, "code"],
  2720. [Blockly.Msg.internet_response_reason, "reason"]
  2721. ]), "op");
  2722. this.setOutput(true, null);
  2723. this.setColour("#183895");
  2724. // this.setTooltip(Blockly.Msg.CocoRobo_get_TOOLTIP);
  2725. this.setHelpUrl("");
  2726. var thisBlock = this;
  2727. this.setTooltip(function() {
  2728. var mode = thisBlock.getFieldValue('op');
  2729. var TOOLTIPS = {
  2730. 'text': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_text),
  2731. 'content': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_content),
  2732. 'state': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_status),
  2733. 'json': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_json),
  2734. 'code': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_encode),
  2735. 'reason': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_reason)
  2736. };
  2737. return TOOLTIPS[mode];
  2738. });
  2739. }
  2740. };
  2741. Blockly.Python['CocoRobo_get'] = function(block) {
  2742. var op = block.getFieldValue('op');
  2743. var code = '_SEND_HTTP_REQUEST.text';
  2744. if (op == "text") {
  2745. code = '_SEND_HTTP_REQUEST.text';
  2746. } else if (op == "content") {
  2747. code = '_SEND_HTTP_REQUEST.content';
  2748. } else if (op == "state") {
  2749. code = '_SEND_HTTP_REQUEST.status_code';
  2750. } else if (op == "json") {
  2751. code = '_SEND_HTTP_REQUEST.json()';
  2752. } else if (op == "code") {
  2753. code = '_SEND_HTTP_REQUEST.encoding';
  2754. } else if (op == "reason") {
  2755. code = '_SEND_HTTP_REQUEST.reason';
  2756. }
  2757. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2758. };
  2759. Blockly.Blocks['iot_system_run'] = {
  2760. init: function() {
  2761. this.appendDummyInput()
  2762. .appendField(Blockly.Msg.SYSTEM_RUN);
  2763. this.appendValueInput("PATH")
  2764. .setCheck(null)
  2765. .appendField(Blockly.Msg.system_code_path);
  2766. this.setInputsInline(false);
  2767. this.setPreviousStatement(true, null);
  2768. this.setNextStatement(true, null);
  2769. this.setColour(32);
  2770. this.setTooltip(Blockly.Msg.Iot_System_Run_TOOLTIP);
  2771. this.setHelpUrl("");
  2772. }
  2773. };
  2774. Blockly.Python.iot_system_run = function(block) {
  2775. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  2776. var _code = "exec(open(" + _path + ").read())\n";
  2777. return _code;
  2778. }
  2779. Blockly.Blocks['iot_system_create'] = {
  2780. init: function() {
  2781. this.appendDummyInput()
  2782. .appendField(Blockly.Msg.SYSTEM_CREATE);
  2783. this.appendValueInput("PATH")
  2784. .setCheck(null)
  2785. .appendField(Blockly.Msg.SYSTEM_CREATE_F);
  2786. this.setInputsInline(false);
  2787. this.setPreviousStatement(true, null);
  2788. this.setNextStatement(true, null);
  2789. this.setColour(32);
  2790. this.setTooltip(Blockly.Msg.Iot_System_Create_TOOLTIP);
  2791. this.setHelpUrl("");
  2792. }
  2793. };
  2794. Blockly.Python.iot_system_create = function(block) {
  2795. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  2796. var _code = "def _CREATE_TEXT_FILE(_path):\n"
  2797. _code += " f = open(_path, 'w')\n"
  2798. _code += " f.close()\n"
  2799. Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
  2800. _code = "_CREATE_TEXT_FILE(" + _path + ")\n";
  2801. return _code;
  2802. }
  2803. Blockly.Blocks['iot_system_write'] = {
  2804. init: function() {
  2805. this.appendDummyInput()
  2806. .appendField(Blockly.Msg.SYSTEM_WRITE);
  2807. this.appendValueInput("PATH")
  2808. .setCheck(null)
  2809. .appendField(Blockly.Msg.SYSTEM_WRITE_F);
  2810. this.appendValueInput("WPATH")
  2811. .setCheck(null)
  2812. .appendField(Blockly.Msg.SYSTEM_WRITE_W);
  2813. this.appendDummyInput()
  2814. .appendField(Blockly.Msg.SYSTEM_WRITE_S)
  2815. .appendField(new Blockly.FieldDropdown([
  2816. [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
  2817. [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
  2818. [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
  2819. [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
  2820. ]), "separator");
  2821. this.setInputsInline(false);
  2822. this.setPreviousStatement(true, null);
  2823. this.setNextStatement(true, null);
  2824. this.setColour(32);
  2825. this.setHelpUrl("");
  2826. var thisBlock = this;
  2827. this.setTooltip(function() {
  2828. var mode = thisBlock.getFieldValue('separator');
  2829. var TOOLTIPS = {
  2830. 'newline': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
  2831. 'comma': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
  2832. 'space': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
  2833. 'semicolon': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
  2834. };
  2835. return TOOLTIPS[mode];
  2836. });
  2837. }
  2838. };
  2839. Blockly.Python.iot_system_write = function(block) {
  2840. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  2841. var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
  2842. var _separator = block.getFieldValue('separator');
  2843. var _code = "def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):\n"
  2844. _code += " f = open(_path, 'a')\n"
  2845. _code += " f.write(_data + _sep)\n"
  2846. _code += " f.close()\n"
  2847. Blockly.Python.addFunction("_CREATE_TEXT_FILE_WITH_CONTENT", _code);
  2848. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
  2849. if (_separator == "newline") {
  2850. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
  2851. } else if (_separator == "comma") {
  2852. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ',')\n";
  2853. } else if (_separator == "space") {
  2854. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ' ')\n";
  2855. } else if (_separator == "semicolon") {
  2856. _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ';')\n";
  2857. }
  2858. return _code;
  2859. }
  2860. Blockly.Blocks['iot_system_input'] = {
  2861. init: function() {
  2862. this.appendDummyInput()
  2863. .appendField(Blockly.Msg.SYSTEM_INPUT);
  2864. this.appendValueInput("PATH")
  2865. .setCheck(null)
  2866. .appendField(Blockly.Msg.SYSTEM_WRITE_F);
  2867. this.appendDummyInput()
  2868. .appendField(Blockly.Msg.SYSTEM_WRITE_S)
  2869. .appendField(new Blockly.FieldDropdown([
  2870. [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
  2871. [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
  2872. [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
  2873. [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
  2874. ]), "separator");
  2875. this.setInputsInline(false);
  2876. this.setPreviousStatement(true, null);
  2877. this.setNextStatement(true, null);
  2878. this.setColour(32);
  2879. // this.setTooltip("");
  2880. this.setHelpUrl("");
  2881. var thisBlock = this;
  2882. this.setTooltip(function() {
  2883. var mode = thisBlock.getFieldValue('separator');
  2884. var TOOLTIPS = {
  2885. 'newline': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
  2886. 'comma': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
  2887. 'space': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
  2888. 'semicolon': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
  2889. };
  2890. return TOOLTIPS[mode];
  2891. });
  2892. }
  2893. };
  2894. Blockly.Python.iot_system_input = function(block) {
  2895. var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
  2896. var _separator = block.getFieldValue('separator');
  2897. var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n"
  2898. _code += " f = open(_path, 'r')\n"
  2899. _code += " result = f.read().split(_sep)\n"
  2900. _code += " f.close()\n"
  2901. _code += " return result\n"
  2902. Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
  2903. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  2904. if (_separator == "newline") {
  2905. _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
  2906. } else if (_separator == "comma") {
  2907. _code = "_GET_LIST_FROM_FILE(" + _path + ", ',')";
  2908. } else if (_separator == "space") {
  2909. _code = "_GET_LIST_FROM_FILE(" + _path + ", ' ')";
  2910. } else if (_separator == "semicolon") {
  2911. _code = "_GET_LIST_FROM_FILE(" + _path + ", ';')";
  2912. }
  2913. return _code;
  2914. }
  2915. Blockly.Blocks['iot_system_restart'] = {
  2916. init: function() {
  2917. this.appendDummyInput()
  2918. .appendField(Blockly.Msg.SYSTEM);
  2919. this.setPreviousStatement(true);
  2920. this.setNextStatement(true);
  2921. this.setColour("#22398e");
  2922. this.setTooltip(Blockly.Msg.Iot_System_Restart_TOOLTIP);
  2923. this.setHelpUrl("");
  2924. }
  2925. };
  2926. Blockly.Python.iot_system_restart = function(block) {
  2927. Blockly.Python.definitions_.import_machine = "import machine";
  2928. var _code = "machine.reset()\n"
  2929. return _code;
  2930. }
  2931. Blockly.Blocks['iot_system_get'] = {
  2932. init: function() {
  2933. this.appendDummyInput()
  2934. .appendField(Blockly.Msg.system_get_size);
  2935. this.setOutput(true, null);
  2936. this.setColour("#22398e");
  2937. this.setTooltip(Blockly.Msg.Iot_System_Get_TOOLTIP);
  2938. this.setHelpUrl("");
  2939. }
  2940. };
  2941. Blockly.Python.iot_system_get = function(block) {
  2942. var code = "_MCU_FREESPACE"
  2943. return [code, Blockly.Python.ORDER_CONDITIONAL];
  2944. }
  2945. Blockly.Blocks['iot_system_mac'] = {
  2946. init: function() {
  2947. this.appendDummyInput()
  2948. .appendField(Blockly.Msg.system_get_mac);
  2949. this.setOutput(true, null);
  2950. this.setColour("#22398e");
  2951. this.setTooltip(Blockly.Msg.Iot_System_Mac_TOOLTIP);
  2952. this.setHelpUrl("");
  2953. }
  2954. };
  2955. Blockly.Python.iot_system_mac = function(block) {
  2956. Blockly.Python.definitions_.import_machine_ub = "import machine, ubinascii";
  2957. var _code = "ubinascii.hexlify(machine.unique_id()).decode().upper()"
  2958. return [_code, Blockly.Python.ORDER_CONDITIONAL];
  2959. }
  2960. Blockly.Blocks['iot_system_get_cocorobo_id'] = {
  2961. init: function() {
  2962. this.appendDummyInput()
  2963. .appendField(Blockly.Msg.system_get_cocorobo_cid);
  2964. this.setInputsInline(false);
  2965. this.setOutput(true, null);
  2966. this.setColour("#22398e");
  2967. this.setTooltip(Blockly.Msg.Iot_System_Get_Cocorobo_Id_TOOLTIP);
  2968. this.setHelpUrl("");
  2969. }
  2970. };
  2971. Blockly.Python['iot_system_get_cocorobo_id'] = function(block) {
  2972. Blockly.Python.definitions_['import_cocorobo_cid'] = 'from cocorobo import cid\n';
  2973. // TODO: Assemble Python into code variable.
  2974. var code = 'cid._GET_MACHINE_ID().strip(\'\\n\')';
  2975. // TODO: Change ORDER_NONE to the correct strength.
  2976. return [code, Blockly.Python.ORDER_NONE];
  2977. };
  2978. /*
  2979. _____ _____ __ _
  2980. \_ \___/__ \ / _\ ___ _ ____ _(_) ___ ___
  2981. / /\/ _ \ / /\/ \ \ / _ \ '__\ \ / / |/ __/ _ \
  2982. /\/ /_| (_) / / _\ \ __/ | \ V /| | (_| __/
  2983. \____/ \___/\/ \__/\___|_| \_/ |_|\___\___|
  2984. */
  2985. var ESP32_IOT_COLOR = "#3c7683";
  2986. var IFTTT_BLOCK_COLOR = "#ff3b00";
  2987. var ONENET_BLOCK_COLOR = "#00b5ff";
  2988. Blockly.Blocks['esp32_main_controller_onenet_setup'] = {
  2989. init: function() {
  2990. this.appendDummyInput()
  2991. .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 130, 70, { alt: "*", flipRtl: "FALSE" }));
  2992. this.appendDummyInput()
  2993. .appendField(Blockly.Msg.iotservice_onenet_mqtt_setup);
  2994. this.appendDummyInput()
  2995. .appendField(Blockly.Msg.iotservice_onenet_mqtt_server)
  2996. .appendField(new Blockly.FieldTextInput("183.230.40.39"), "server_ip");
  2997. this.appendDummyInput()
  2998. .appendField(Blockly.Msg.iotservice_onenet_mqtt_productid)
  2999. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_ID"), "product_id");
  3000. this.appendDummyInput()
  3001. .appendField(Blockly.Msg.iotservice_onenet_mqtt_productkey)
  3002. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_KEY"), "product_api_key");
  3003. this.appendDummyInput()
  3004. .appendField(Blockly.Msg.iotservice_onenet_mqtt_deviceid)
  3005. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_DEVICE_ID"), "device_id");
  3006. this.setPreviousStatement(true, null);
  3007. this.setNextStatement(true, null);
  3008. this.setColour(ONENET_BLOCK_COLOR);
  3009. this.setTooltip("");
  3010. this.setHelpUrl("");
  3011. }
  3012. };
  3013. Blockly.Python['esp32_main_controller_onenet_setup'] = function(block) {
  3014. var text_server_ip = block.getFieldValue('server_ip');
  3015. var text_product_id = block.getFieldValue('product_id');
  3016. var text_product_api_key = block.getFieldValue('product_api_key');
  3017. var text_device_id = block.getFieldValue('device_id');
  3018. // TODO: Assemble Python into code variable.
  3019. var code = '...\n';
  3020. return code;
  3021. };
  3022. Blockly.Blocks['iot_service_onenet'] = {
  3023. init: function() {
  3024. this.appendDummyInput()
  3025. .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
  3026. this.appendDummyInput()
  3027. .appendField(Blockly.Msg.iotservice_onenet_mqtt_send);
  3028. /*this.appendValueInput("first_input")
  3029. .setCheck(null)
  3030. .appendField("Field 0");*/
  3031. this.itemCount_ = 1;
  3032. this.updateShape_();
  3033. this.setMutator(new Blockly.Mutator(['iot_service_onenet_create_with_item']));
  3034. this.setColour(ONENET_BLOCK_COLOR);
  3035. this.setPreviousStatement(true, null);
  3036. this.setNextStatement(true, null);
  3037. this.setTooltip("");
  3038. this.setHelpUrl("");
  3039. },
  3040. mutationToDom: function() {
  3041. var container = document.createElement('mutation');
  3042. container.setAttribute('items', this.itemCount_);
  3043. return container;
  3044. },
  3045. domToMutation: function(xmlElement) {
  3046. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  3047. this.updateShape_();
  3048. },
  3049. decompose: function(workspace) {
  3050. var containerBlock = workspace.newBlock('iot_service_onenet_create_with_container');
  3051. containerBlock.initSvg();
  3052. var connection = containerBlock.getInput('STACK').connection;
  3053. for (var i = 0; i < this.itemCount_; i++) {
  3054. var itemBlock = workspace.newBlock('iot_service_onenet_create_with_item');
  3055. itemBlock.initSvg();
  3056. connection.connect(itemBlock.previousConnection);
  3057. connection = itemBlock.nextConnection;
  3058. }
  3059. return containerBlock;
  3060. },
  3061. compose: function(containerBlock) {
  3062. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3063. // Count number of inputs.
  3064. var connections = [];
  3065. while (itemBlock) {
  3066. connections.push(itemBlock.valueConnection_);
  3067. itemBlock = itemBlock.nextConnection &&
  3068. itemBlock.nextConnection.targetBlock();
  3069. }
  3070. // Disconnect any children that don't belong.
  3071. for (var i = 0; i < this.itemCount_; i++) {
  3072. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3073. if (connection && connections.indexOf(connection) == -1) {
  3074. connection.disconnect();
  3075. }
  3076. }
  3077. this.itemCount_ = connections.length;
  3078. this.updateShape_();
  3079. // Reconnect any child blocks.
  3080. for (var i = 0; i < this.itemCount_; i++) {
  3081. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3082. }
  3083. },
  3084. saveConnections: function(containerBlock) {
  3085. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3086. var i = 0;
  3087. while (itemBlock) {
  3088. var input = this.getInput('ADD' + i);
  3089. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3090. i++;
  3091. itemBlock = itemBlock.nextConnection &&
  3092. itemBlock.nextConnection.targetBlock();
  3093. }
  3094. },
  3095. updateShape_: function() {
  3096. for (var i = 0; i < this.itemCount_; i++) {
  3097. if (!this.getInput('ADD' + i)) {
  3098. var input = this.appendValueInput('ADD' + i);
  3099. input.appendField(Blockly.Msg.iotservice_onenet_mqtt_send_item)
  3100. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  3101. //input.appendField("資料 " + (i + 1) + ":");
  3102. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3103. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  3104. }
  3105. }
  3106. while (this.getInput('ADD' + i)) {
  3107. this.removeInput('ADD' + i);
  3108. i++;
  3109. }
  3110. },
  3111. };
  3112. Blockly.Blocks['iot_service_onenet_create_with_container'] = {
  3113. init: function() {
  3114. this.setColour(ONENET_BLOCK_COLOR);
  3115. this.appendDummyInput()
  3116. .appendField("Items");
  3117. this.appendStatementInput('STACK');
  3118. this.setTooltip('');
  3119. this.contextMenu = false;
  3120. }
  3121. };
  3122. Blockly.Blocks['iot_service_onenet_create_with_item'] = {
  3123. init: function() {
  3124. this.setColour(ONENET_BLOCK_COLOR);
  3125. this.appendDummyInput()
  3126. .appendField("Field");
  3127. this.setPreviousStatement(true);
  3128. this.setNextStatement(true);
  3129. this.setTooltip('');
  3130. this.contextMenu = false;
  3131. }
  3132. };
  3133. Blockly.Python['iot_service_onenet'] = function(block) {
  3134. var code = "";
  3135. // onenet_data_final.slice(0, -3) + '}\''
  3136. return code;
  3137. };
  3138. Blockly.Blocks['esp32_main_controller_onenet_when_receive_msg'] = {
  3139. init: function() {
  3140. this.appendDummyInput()
  3141. .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive);
  3142. this.appendValueInput("content")
  3143. .setCheck(null);
  3144. this.appendDummyInput()
  3145. .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_when);
  3146. this.appendStatementInput("exec")
  3147. .setCheck(null)
  3148. .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_exec);
  3149. this.setInputsInline(true);
  3150. this.setPreviousStatement(true, null);
  3151. this.setNextStatement(true, null);
  3152. this.setColour(ONENET_BLOCK_COLOR);
  3153. this.setTooltip("");
  3154. this.setHelpUrl("");
  3155. }
  3156. };
  3157. Blockly.Python['esp32_main_controller_onenet_when_receive_msg'] = function(block) {
  3158. var value_content = Blockly.Python.valueToCode(block, 'content', Blockly.Python.ORDER_ATOMIC);
  3159. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  3160. // TODO: Assemble Python into code variable.
  3161. var code = '...\n';
  3162. return code;
  3163. };
  3164. Blockly.Blocks['esp32_main_controller_onenet_disconnect'] = {
  3165. init: function() {
  3166. this.appendDummyInput()
  3167. .appendField(Blockly.Msg.iotservice_onenet_mqtt_disconnect);
  3168. this.setInputsInline(true);
  3169. this.setPreviousStatement(true, null);
  3170. this.setNextStatement(true, null);
  3171. this.setColour(ONENET_BLOCK_COLOR);
  3172. this.setTooltip("");
  3173. this.setHelpUrl("");
  3174. }
  3175. };
  3176. Blockly.Python['esp32_main_controller_onenet_disconnect'] = function(block) {
  3177. // TODO: Assemble Python into code variable.
  3178. var code = '\n';
  3179. return code;
  3180. };
  3181. Blockly.Blocks['esp32_main_controller_ifttt_send_data'] = {
  3182. init: function() {
  3183. this.appendDummyInput()
  3184. .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
  3185. this.appendDummyInput()
  3186. .appendField(Blockly.Msg.iotservice_ifttt_send_title);
  3187. this.appendDummyInput()
  3188. .appendField(Blockly.Msg.iotservice_ifttt_send_webhook)
  3189. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "ifttt_api_key");
  3190. this.appendDummyInput()
  3191. .appendField(Blockly.Msg.iotservice_ifttt_send_eventname)
  3192. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "ifttt_event");
  3193. this.appendValueInput("ifttt_data_1")
  3194. .setCheck(null)
  3195. .appendField(Blockly.Msg.iotservice_ifttt_send_1);
  3196. this.appendValueInput("ifttt_data_2")
  3197. .setCheck(null)
  3198. .appendField(Blockly.Msg.iotservice_ifttt_send_2);
  3199. this.appendValueInput("ifttt_data_3")
  3200. .setCheck(null)
  3201. .appendField(Blockly.Msg.iotservice_ifttt_send_3);
  3202. this.setPreviousStatement(true, null);
  3203. this.setNextStatement(true, null);
  3204. this.setColour(IFTTT_BLOCK_COLOR);
  3205. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Send_Data_TOOLTIP);
  3206. this.setHelpUrl("");
  3207. }
  3208. };
  3209. Blockly.Python['esp32_main_controller_ifttt_send_data'] = function(block) {
  3210. var text_ifttt_api_key = block.getFieldValue('ifttt_api_key');
  3211. var text_ifttt_event = block.getFieldValue('ifttt_event');
  3212. var value_ifttt_data_1 = Blockly.Python.valueToCode(block, 'ifttt_data_1', Blockly.Python.ORDER_ATOMIC);
  3213. var value_ifttt_data_2 = Blockly.Python.valueToCode(block, 'ifttt_data_2', Blockly.Python.ORDER_ATOMIC);
  3214. var value_ifttt_data_3 = Blockly.Python.valueToCode(block, 'ifttt_data_3', Blockly.Python.ORDER_ATOMIC);
  3215. // TODO: Assemble Python into code variable.
  3216. Blockly.Python.definitions_['import_urequests'] = '' +
  3217. 'import urequests\n' +
  3218. 'import ujson as json\n'
  3219. '';
  3220. var code = '' +
  3221. '_IFTTT_POST_API_KEY = "' + text_ifttt_api_key + '"\n' +
  3222. '_IFTTT_POST_EVENT_NAME = "' + text_ifttt_event + '"\n' +
  3223. '_IFTTT_POST_ENDPOINT = "http://maker.ifttt.com/trigger/"+ _IFTTT_POST_EVENT_NAME + "/with/key/" + _IFTTT_POST_API_KEY\n' +
  3224. '_IFTTT_POST_DATA = \'{"value1":"\'+ str(' + value_ifttt_data_1 + ') +\'","value2":"\'+ str(' + value_ifttt_data_2 + ') +\'","value3":"\'+ str(' + value_ifttt_data_3 + ') +\'"}\'\n' +
  3225. '_IFTTT_POST_REQUEST = urequests.post(_IFTTT_POST_ENDPOINT, data = _IFTTT_POST_DATA , headers = { "Content-type": "application/json" }, timeout=60)\n' +
  3226. '\n';
  3227. return code;
  3228. };
  3229. Blockly.Blocks['esp32_main_controller_ifttt_touched'] = {
  3230. init: function() {
  3231. this.appendDummyInput()
  3232. .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
  3233. this.appendDummyInput()
  3234. .appendField(Blockly.Msg.iotservice_ifttt_trigger_title);
  3235. this.appendDummyInput()
  3236. .appendField(Blockly.Msg.iotservice_ifttt_trigger_webhook)
  3237. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "key");
  3238. this.appendDummyInput()
  3239. .appendField(Blockly.Msg.iotservice_ifttt_trigger_eventname)
  3240. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "timename");
  3241. this.setPreviousStatement(true, null);
  3242. this.setNextStatement(true, null);
  3243. this.setColour(IFTTT_BLOCK_COLOR);
  3244. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Touched_TOOLTIP);
  3245. this.setHelpUrl("");
  3246. }
  3247. };
  3248. Blockly.Python['esp32_main_controller_ifttt_touched'] = function(block) {
  3249. var text_ifttt_api_key = block.getFieldValue('key');
  3250. var text_timename = block.getFieldValue('timename');
  3251. Blockly.Python.definitions_['import_urequests'] = '' +
  3252. 'import urequests\n' +
  3253. 'import ujson as json\n'
  3254. '';
  3255. var code = '_IFTTT_TRIGGER_EVENT_NAME = "' + text_timename + '"\n'
  3256. code += '_IFTTT_TRIGGER_API_KEY = "' + text_ifttt_api_key + '"\n'
  3257. code += '_IFTTT_TRIGGER_ENDPOINT = "https://maker.ifttt.com/trigger/" + _IFTTT_TRIGGER_EVENT_NAME + "/with/key/" + _IFTTT_TRIGGER_API_KEY\n'
  3258. Blockly.Python.addSetup("esp32_main_controller_ifttt_touched", code);
  3259. var code = '_IFTTT_GET_REQUEST = urequests.get(_IFTTT_TRIGGER_ENDPOINT, timeout=60)\n'
  3260. return code;
  3261. };
  3262. IOT_THINGSPEAK_COLOR = "#1b379f";
  3263. // IOT_THINGSPEAK_COLOR = ESP32_IOT_COLOR;
  3264. Blockly.Blocks['iot_service_thingspeak'] = {
  3265. init: function() {
  3266. this.appendDummyInput()
  3267. .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
  3268. this.appendDummyInput()
  3269. .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
  3270. this.appendDummyInput()
  3271. .appendField(Blockly.Msg.iotservice_thingspeak_send_api)
  3272. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_CHANNEL_WRITE_API_KEY"), "t_api");
  3273. /*this.appendValueInput("first_input")
  3274. .setCheck(null)
  3275. .appendField("Field 0");*/
  3276. this.itemCount_ = 1;
  3277. this.updateShape_();
  3278. this.setMutator(new Blockly.Mutator(['iot_service_thingspeak_create_with_item']));
  3279. this.setColour(IOT_THINGSPEAK_COLOR);
  3280. this.setPreviousStatement(true, null);
  3281. this.setNextStatement(true, null);
  3282. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_TOOLTIP);
  3283. this.setHelpUrl("");
  3284. },
  3285. mutationToDom: function() {
  3286. var container = document.createElement('mutation');
  3287. container.setAttribute('items', this.itemCount_);
  3288. return container;
  3289. },
  3290. domToMutation: function(xmlElement) {
  3291. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  3292. this.updateShape_();
  3293. },
  3294. decompose: function(workspace) {
  3295. var containerBlock = workspace.newBlock('iot_service_thingspeak_create_with_container');
  3296. containerBlock.initSvg();
  3297. var connection = containerBlock.getInput('STACK').connection;
  3298. for (var i = 0; i < this.itemCount_; i++) {
  3299. var itemBlock = workspace.newBlock('iot_service_thingspeak_create_with_item');
  3300. itemBlock.initSvg();
  3301. connection.connect(itemBlock.previousConnection);
  3302. connection = itemBlock.nextConnection;
  3303. }
  3304. return containerBlock;
  3305. },
  3306. compose: function(containerBlock) {
  3307. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3308. // Count number of inputs.
  3309. var connections = [];
  3310. while (itemBlock) {
  3311. connections.push(itemBlock.valueConnection_);
  3312. itemBlock = itemBlock.nextConnection &&
  3313. itemBlock.nextConnection.targetBlock();
  3314. }
  3315. // Disconnect any children that don't belong.
  3316. for (var i = 0; i < this.itemCount_; i++) {
  3317. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3318. if (connection && connections.indexOf(connection) == -1) {
  3319. connection.disconnect();
  3320. }
  3321. }
  3322. this.itemCount_ = connections.length;
  3323. this.updateShape_();
  3324. // Reconnect any child blocks.
  3325. for (var i = 0; i < this.itemCount_; i++) {
  3326. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3327. }
  3328. },
  3329. saveConnections: function(containerBlock) {
  3330. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3331. var i = 0;
  3332. while (itemBlock) {
  3333. var input = this.getInput('ADD' + i);
  3334. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3335. i++;
  3336. itemBlock = itemBlock.nextConnection &&
  3337. itemBlock.nextConnection.targetBlock();
  3338. }
  3339. },
  3340. updateShape_: function() {
  3341. for (var i = 0; i < this.itemCount_; i++) {
  3342. if (!this.getInput('ADD' + i)) {
  3343. var input = this.appendValueInput('ADD' + i);
  3344. input.appendField("Field " + (i + 1) + ":");
  3345. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3346. // input.appendField(new Blockly.FieldTextInput("field" + (i + 1)), 'FIELD' + i)
  3347. }
  3348. }
  3349. while (this.getInput('ADD' + i)) {
  3350. this.removeInput('ADD' + i);
  3351. i++;
  3352. }
  3353. },
  3354. };
  3355. Blockly.Blocks['iot_service_thingspeak_create_with_container'] = {
  3356. init: function() {
  3357. this.setColour(IOT_THINGSPEAK_COLOR);
  3358. this.appendDummyInput()
  3359. .appendField("Items");
  3360. this.appendStatementInput('STACK');
  3361. this.setTooltip('');
  3362. this.contextMenu = false;
  3363. }
  3364. };
  3365. Blockly.Blocks['iot_service_thingspeak_create_with_item'] = {
  3366. init: function() {
  3367. this.setColour(IOT_THINGSPEAK_COLOR);
  3368. this.appendDummyInput()
  3369. .appendField("Field");
  3370. this.setPreviousStatement(true);
  3371. this.setNextStatement(true);
  3372. this.setTooltip('');
  3373. this.contextMenu = false;
  3374. }
  3375. };
  3376. Blockly.Python['iot_service_thingspeak'] = function(block) {
  3377. var first_input = Blockly.Python.valueToCode(block, 'first_input', Blockly.Python.ORDER_ATOMIC);
  3378. var api = block.getFieldValue('t_api');
  3379. Blockly.Python.addVariable('_THINGSPEAK_REQUEST', '', true);
  3380. Blockly.Python.definitions_['import_urequests'] = '' +
  3381. 'import urequests\n' +
  3382. 'import ujson as json\n'
  3383. '';
  3384. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  3385. var item_field = '',
  3386. item_value = '';
  3387. var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + api;
  3388. for (var n = 0; n < this.itemCount_; n++) {
  3389. item_field = "field" + (n + 1);
  3390. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  3391. Blockly.Python.ORDER_NONE) || '';
  3392. thingspeak_url += '&' + item_field + '=\"+str(' + item_value + ')+\"';
  3393. }
  3394. var code = '' +
  3395. '_THINGSPEAK_ENDPOINT = "' + thingspeak_url + '"\n' +
  3396. '_THINGSPEAK_REQUEST = urequests.get(_THINGSPEAK_ENDPOINT,timeout=60)\n' +
  3397. '';
  3398. return code;
  3399. };
  3400. Blockly.Blocks['iot_service_thingspeak_read'] = {
  3401. init: function() {
  3402. this.appendDummyInput()
  3403. .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
  3404. this.appendDummyInput()
  3405. .appendField(Blockly.Msg.iotservice_thingspeak_query_title);
  3406. this.appendDummyInput()
  3407. .appendField(Blockly.Msg.iotservice_thingspeak_query_api)
  3408. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_READ_API_KEY"), "api_key");
  3409. this.appendDummyInput()
  3410. .appendField(Blockly.Msg.iotservice_thingspeak_channel_id)
  3411. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_ID"), "id");
  3412. this.setInputsInline(false);
  3413. this.setPreviousStatement(true, null);
  3414. this.setNextStatement(true, null);
  3415. this.setColour(IOT_THINGSPEAK_COLOR);
  3416. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_TOOLTIP);
  3417. this.setHelpUrl("");
  3418. }
  3419. };
  3420. Blockly.Python['iot_service_thingspeak_read'] = function(block) {
  3421. var text_api_key = block.getFieldValue('api_key');
  3422. var text_id = block.getFieldValue('id');
  3423. Blockly.Python.definitions_['import_urequests'] = '' +
  3424. 'import urequests\n' +
  3425. 'import ujson as json\n'
  3426. '';
  3427. Blockly.Python.addVariable('_THINGSPEAK_READ_REQUEST', '', true);
  3428. // TODO: Assemble Python into code variable.
  3429. var code = '_THINGSPEAK_READ_REQUEST = urequests.get("https://api.thingspeak.com/channels/" + "' + text_id + '" + "/feeds.json?api_key=" + "' + text_api_key + '",timeout=60)\n';
  3430. return code;
  3431. };
  3432. Blockly.Blocks['iot_service_thingspeak_read_total'] = {
  3433. init: function() {
  3434. this.appendDummyInput()
  3435. .appendField(Blockly.Msg.iotservice_thingspeak_read_total);
  3436. this.setInputsInline(false);
  3437. this.setOutput(true, null);
  3438. this.setColour(IOT_THINGSPEAK_COLOR);
  3439. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Total_TOOLTIP);
  3440. this.setHelpUrl("");
  3441. }
  3442. };
  3443. Blockly.Python['iot_service_thingspeak_read_total'] = function(block) {
  3444. // TODO: Assemble Python into code variable.
  3445. var code = '_THINGSPEAK_READ_REQUEST.json()[\'channel\'][\'last_entry_id\']';
  3446. // TODO: Change ORDER_NONE to the correct strength.
  3447. return [code, Blockly.Python.ORDER_NONE];
  3448. };
  3449. Blockly.Blocks['iot_service_thingspeak_read_specific'] = {
  3450. init: function() {
  3451. this.appendDummyInput()
  3452. .appendField(Blockly.Msg.iotservice_thingspeak_get_title);
  3453. this.appendValueInput("entry")
  3454. .setCheck(null);
  3455. this.appendDummyInput()
  3456. .appendField(Blockly.Msg.iotservice_thingspeaK_get_field);
  3457. this.appendValueInput("field")
  3458. .setCheck(null);
  3459. this.appendDummyInput()
  3460. .appendField(Blockly.Msg.iotservice_thingspeaK_get_value);
  3461. this.setInputsInline(true);
  3462. this.setOutput(true, null);
  3463. this.setColour(IOT_THINGSPEAK_COLOR);
  3464. this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Specific_TOOLTIP);
  3465. this.setHelpUrl("");
  3466. }
  3467. };
  3468. Blockly.Python['iot_service_thingspeak_read_specific'] = function(block) {
  3469. var value_entry = Blockly.Python.valueToCode(block, 'entry', Blockly.Python.ORDER_ATOMIC);
  3470. var value_field = Blockly.Python.valueToCode(block, 'field', Blockly.Python.ORDER_ATOMIC);
  3471. // TODO: Assemble Python into code variable.
  3472. var code = '_THINGSPEAK_READ_REQUEST.json()[\'feeds\'][' + value_entry + '][\'field' + value_field + '\']';
  3473. // TODO: Change ORDER_NONE to the correct strength.
  3474. return [code, Blockly.Python.ORDER_NONE];
  3475. };
  3476. // cococloud
  3477. var COCOCLOUD_BLOCK_COLOR = "#4085de";
  3478. Blockly.Blocks['iot_service_cococloud'] = {
  3479. init: function() {
  3480. this.appendDummyInput()
  3481. .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
  3482. this.appendDummyInput()
  3483. .appendField(Blockly.Msg.iotservice_cococloud_send_title);
  3484. this.appendDummyInput()
  3485. .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
  3486. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  3487. /*this.appendValueInput("first_input")
  3488. .setCheck(null)
  3489. .appendField("Field 0");*/
  3490. this.itemCount_ = 1;
  3491. this.updateShape_();
  3492. this.setMutator(new Blockly.Mutator(['iot_service_cococloud_create_with_item']));
  3493. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3494. this.setPreviousStatement(true, null);
  3495. this.setNextStatement(true, null);
  3496. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_TOOLTIP);
  3497. this.setHelpUrl("");
  3498. },
  3499. mutationToDom: function() {
  3500. var container = document.createElement('mutation');
  3501. container.setAttribute('items', this.itemCount_);
  3502. return container;
  3503. },
  3504. domToMutation: function(xmlElement) {
  3505. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  3506. this.updateShape_();
  3507. },
  3508. decompose: function(workspace) {
  3509. var containerBlock = workspace.newBlock('iot_service_cococloud_create_with_container');
  3510. containerBlock.initSvg();
  3511. var connection = containerBlock.getInput('STACK').connection;
  3512. for (var i = 0; i < this.itemCount_; i++) {
  3513. var itemBlock = workspace.newBlock('iot_service_cococloud_create_with_item');
  3514. itemBlock.initSvg();
  3515. connection.connect(itemBlock.previousConnection);
  3516. connection = itemBlock.nextConnection;
  3517. }
  3518. return containerBlock;
  3519. },
  3520. compose: function(containerBlock) {
  3521. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3522. // Count number of inputs.
  3523. var connections = [];
  3524. while (itemBlock) {
  3525. connections.push(itemBlock.valueConnection_);
  3526. itemBlock = itemBlock.nextConnection &&
  3527. itemBlock.nextConnection.targetBlock();
  3528. }
  3529. // Disconnect any children that don't belong.
  3530. for (var i = 0; i < this.itemCount_; i++) {
  3531. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3532. if (connection && connections.indexOf(connection) == -1) {
  3533. connection.disconnect();
  3534. }
  3535. }
  3536. this.itemCount_ = connections.length;
  3537. this.updateShape_();
  3538. // Reconnect any child blocks.
  3539. for (var i = 0; i < this.itemCount_; i++) {
  3540. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3541. }
  3542. },
  3543. saveConnections: function(containerBlock) {
  3544. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3545. var i = 0;
  3546. while (itemBlock) {
  3547. var input = this.getInput('ADD' + i);
  3548. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3549. i++;
  3550. itemBlock = itemBlock.nextConnection &&
  3551. itemBlock.nextConnection.targetBlock();
  3552. }
  3553. },
  3554. updateShape_: function() {
  3555. for (var i = 0; i < this.itemCount_; i++) {
  3556. if (!this.getInput('ADD' + i)) {
  3557. var input = this.appendValueInput('ADD' + i);
  3558. input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
  3559. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  3560. //input.appendField("資料 " + (i + 1) + ":");
  3561. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3562. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  3563. }
  3564. }
  3565. while (this.getInput('ADD' + i)) {
  3566. this.removeInput('ADD' + i);
  3567. i++;
  3568. }
  3569. },
  3570. };
  3571. Blockly.Blocks['iot_service_cococloud_create_with_container'] = {
  3572. init: function() {
  3573. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3574. this.appendDummyInput()
  3575. .appendField("Items");
  3576. this.appendStatementInput('STACK');
  3577. this.setTooltip('');
  3578. this.contextMenu = false;
  3579. }
  3580. };
  3581. Blockly.Blocks['iot_service_cococloud_create_with_item'] = {
  3582. init: function() {
  3583. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3584. this.appendDummyInput()
  3585. .appendField("Field");
  3586. this.setPreviousStatement(true);
  3587. this.setNextStatement(true);
  3588. this.setTooltip('');
  3589. this.contextMenu = false;
  3590. }
  3591. };
  3592. Blockly.Python['iot_service_cococloud'] = function(block) {
  3593. var api = block.getFieldValue('t_api');
  3594. Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '', true);
  3595. Blockly.Python.definitions_['import_urequests'] = '' +
  3596. 'import urequests\n' +
  3597. 'import ujson as json\n'
  3598. '';
  3599. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  3600. var item_field = '',
  3601. item_value = '';
  3602. var cococloud_data = "'{";
  3603. for (var n = 0; n < this.itemCount_; n++) {
  3604. item_field = block.getFieldValue("field" + n);
  3605. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  3606. cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  3607. }
  3608. var cococloud_data_final = cococloud_data + "}'";
  3609. var code = '' +
  3610. '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
  3611. '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  3612. 'try:\n' +
  3613. ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)\n' +
  3614. ' print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))\n' +
  3615. 'except BaseException as e:\n' +
  3616. ' print(e)\n' +
  3617. 'pass\n' +
  3618. '';
  3619. // cococloud_data_final.slice(0, -3) + '}\''
  3620. return code;
  3621. };
  3622. Blockly.Blocks['iot_service_cococloud_read'] = {
  3623. init: function() {
  3624. this.appendDummyInput()
  3625. .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
  3626. this.appendDummyInput()
  3627. .appendField(Blockly.Msg.iotservice_cococloud_get_title);
  3628. this.appendDummyInput()
  3629. .appendField(Blockly.Msg.iotservice_cococloud_get_api)
  3630. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "api");
  3631. this.setInputsInline(false);
  3632. this.setPreviousStatement(true, null);
  3633. this.setNextStatement(true, null);
  3634. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3635. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_TOOLTIP);
  3636. this.setHelpUrl("");
  3637. }
  3638. };
  3639. Blockly.Python['iot_service_cococloud_read'] = function(block) {
  3640. var api = block.getFieldValue('api');
  3641. Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '', true);
  3642. Blockly.Python.definitions_['import_urequests'] = '' +
  3643. 'import urequests\n' +
  3644. 'import ujson as json\n'
  3645. '';
  3646. // TODO: Assemble Python into code variable.
  3647. var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
  3648. return code;
  3649. };
  3650. Blockly.Blocks['iot_service_cococloud_read_data'] = {
  3651. init: function() {
  3652. this.appendDummyInput()
  3653. .appendField(Blockly.Msg.iotservice_cococloud_get_property);
  3654. this.appendValueInput("property")
  3655. .setCheck(null);
  3656. this.appendDummyInput()
  3657. .appendField(Blockly.Msg.iotservice_cococloud_get_data);
  3658. this.setInputsInline(true);
  3659. this.setOutput(true, null);
  3660. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3661. this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_Data_TOOLTIP);
  3662. this.setHelpUrl("");
  3663. }
  3664. };
  3665. Blockly.Python['iot_service_cococloud_read_data'] = function(block) {
  3666. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  3667. // TODO: Assemble Python into code variable.
  3668. var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
  3669. // TODO: Change ORDER_NONE to the correct strength.
  3670. return [code, Blockly.Python.ORDER_NONE];
  3671. };
  3672. /*
  3673. __ _ _ __ _
  3674. /\ \ \___| |___ _____ _ __| | __ /__\ ___ __ _ _ _ ___ ___| |_
  3675. / \/ / _ \ __\ \ /\ / / _ \| '__| |/ / / \/// _ \/ _` | | | |/ _ \/ __| __|
  3676. / /\ / __/ |_ \ V V / (_) | | | < / _ \ __/ (_| | |_| | __/\__ \ |_
  3677. \_\ \/ \___|\__| \_/\_/ \___/|_| |_|\_\ \/ \_/\___|\__, |\__,_|\___||___/\__|
  3678. |_|
  3679. */
  3680. var NETWORK_BLOCK_COLOR = "#16318a";
  3681. Blockly.Blocks['esp32_network_http_get'] = {
  3682. init: function() {
  3683. this.appendDummyInput()
  3684. .appendField(new Blockly.FieldImage("blockly/media/http_header_get.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
  3685. this.appendDummyInput()
  3686. .appendField(Blockly.Msg.internet_http_get_title);
  3687. this.appendValueInput("http_get_url")
  3688. .setCheck(null)
  3689. .appendField(Blockly.Msg.internet_http_get_url);
  3690. this.setInputsInline(false);
  3691. this.setPreviousStatement(true, null);
  3692. this.setNextStatement(true, null);
  3693. this.setColour(NETWORK_BLOCK_COLOR);
  3694. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Get_TOOLTIP);
  3695. this.setHelpUrl("");
  3696. }
  3697. };
  3698. Blockly.Python['esp32_network_http_get'] = function(block) {
  3699. var url = Blockly.Python.valueToCode(block, 'http_get_url', Blockly.Python.ORDER_ATOMIC);
  3700. Blockly.Python.addVariable('_SEND_HTTP_GET_ENDPOINT', '', true);
  3701. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  3702. Blockly.Python.definitions_['import_urequests'] = '' +
  3703. 'import urequests as requests\n' +
  3704. 'import ujson as json\n' +
  3705. '\n' +
  3706. '_SEND_HTTP_GET_ENDPOINT = ' + url + '\n' +
  3707. '';
  3708. // TODO: Assemble Python into code variable.
  3709. var code = '_SEND_HTTP_REQUEST = requests.get(_SEND_HTTP_GET_ENDPOINT)\n';
  3710. return code;
  3711. };
  3712. Blockly.Blocks['esp32_network_http_post'] = {
  3713. init: function() {
  3714. this.appendDummyInput()
  3715. .appendField(new Blockly.FieldImage("blockly/media/http_header_post.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
  3716. this.appendDummyInput()
  3717. .appendField(Blockly.Msg.internet_http_post_title);
  3718. this.appendValueInput("http_post_url")
  3719. .setCheck(null)
  3720. .appendField(Blockly.Msg.internet_http_post_url);
  3721. this.appendValueInput("data")
  3722. .setCheck(null)
  3723. .appendField(Blockly.Msg.internet_http_post_json);
  3724. this.setInputsInline(false);
  3725. this.setPreviousStatement(true, null);
  3726. this.setNextStatement(true, null);
  3727. this.setColour(NETWORK_BLOCK_COLOR);
  3728. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Post_TOOLTIP);
  3729. this.setHelpUrl("");
  3730. }
  3731. };
  3732. Blockly.Python['esp32_network_http_post'] = function(block) {
  3733. var url = Blockly.Python.valueToCode(block, 'http_post_url', Blockly.Python.ORDER_ATOMIC);
  3734. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  3735. Blockly.Python.addVariable('_SEND_HTTP_POST_ENDPOINT', '', true);
  3736. Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
  3737. Blockly.Python.definitions_['import_urequests'] = '' +
  3738. 'import urequests as requests\n' +
  3739. 'import ujson as json\n' +
  3740. '\n' +
  3741. '_SEND_HTTP_POST_ENDPOINT = ' + url + '\n' +
  3742. '';
  3743. // TODO: Assemble Python into code variable.
  3744. var code = '' +
  3745. '_SEND_HTTP_POST_DATA = \'' + data + '\'\n' +
  3746. '_SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, data = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })\n' +
  3747. '';
  3748. return code;
  3749. };
  3750. Blockly.Blocks['esp32_network_http_get_data_from_local_server'] = {
  3751. init: function() {
  3752. this.appendDummyInput()
  3753. .appendField(Blockly.Msg.internet_http_get_server_data_title);
  3754. this.appendDummyInput()
  3755. .appendField(Blockly.Msg.internet_http_get_server_data_type)
  3756. .appendField(new Blockly.FieldDropdown([
  3757. [Blockly.Msg.internet_http_get_server_data_json, "json"],
  3758. [Blockly.Msg.internet_http_get_server_data_text, "text"]
  3759. ]), "type");
  3760. this.setInputsInline(false);
  3761. this.setOutput(true, null);
  3762. this.setColour(NETWORK_BLOCK_COLOR);
  3763. var thisBlock = this;
  3764. this.setTooltip(function() {
  3765. var mode = thisBlock.getFieldValue('type');
  3766. var TOOLTIPS = {
  3767. 'json': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_json),
  3768. 'text': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_text)
  3769. };
  3770. return TOOLTIPS[mode];
  3771. });
  3772. this.setHelpUrl("");
  3773. }
  3774. };
  3775. Blockly.Python['esp32_network_http_get_data_from_local_server'] = function(block) {
  3776. var dropdown_type = block.getFieldValue('type');
  3777. // TODO: Assemble Python into code variable.
  3778. if (dropdown_type == "text") {
  3779. var request_code = "_SEND_HTTP_REQUEST.text";
  3780. } else if (dropdown_type == "json") {
  3781. var request_code = "eval(_SEND_HTTP_REQUEST.text)";
  3782. }
  3783. var code = request_code;
  3784. // TODO: Change ORDER_NONE to the correct strength.
  3785. return [code, Blockly.Python.ORDER_NONE];
  3786. };
  3787. Blockly.Blocks['esp32_network_http_server_setup'] = {
  3788. init: function() {
  3789. this.appendDummyInput()
  3790. .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 40, { alt: "*", flipRtl: "FALSE" }));
  3791. this.appendDummyInput()
  3792. .appendField(Blockly.Msg.wifi_web_http_server_setup);
  3793. this.setInputsInline(false);
  3794. this.setPreviousStatement(true, null);
  3795. this.setNextStatement(true, null);
  3796. this.setColour(NETWORK_BLOCK_COLOR);
  3797. this.setTooltip(Blockly.Msg.Esp32_Network_Http_Server_Setup_TOOLTIP);
  3798. this.setHelpUrl("");
  3799. }
  3800. };
  3801. Blockly.Python['esp32_network_http_server_setup'] = function(block) {
  3802. // TODO: Assemble Python into code variable.
  3803. Blockly.Python.definitions_['esp32_network_http_server_setup_def_setup'] = '' +
  3804. 'from microWebSrv import MicroWebSrv\n' +
  3805. 'import time\n' +
  3806. '';
  3807. var code = '';
  3808. return code;
  3809. };
  3810. Blockly.Blocks['esp32_network_http_server_route'] = {
  3811. init: function() {
  3812. this.appendDummyInput()
  3813. .appendField(Blockly.Msg.wifi_web_http_server_route_setting_title);
  3814. this.appendDummyInput()
  3815. .appendField(Blockly.Msg.wifi_web_http_server_route_setting_path)
  3816. .appendField(new Blockly.FieldTextInput("/"), "path")
  3817. .appendField(Blockly.Msg.wifi_web_http_server_route_setting_being_requested);
  3818. this.appendDummyInput()
  3819. .appendField(Blockly.Msg.wifi_web_http_server_route_setting_property)
  3820. .appendField(new Blockly.FieldDropdown([
  3821. [Blockly.Msg.wifi_web_http_server_route_setting_get, "get"],
  3822. [Blockly.Msg.wifi_web_http_server_route_setting_post, "post"]
  3823. ]), "response_type");
  3824. this.appendStatementInput("exec")
  3825. .setCheck(null)
  3826. .appendField(Blockly.Msg.wifi_web_http_server_route_setting_do);
  3827. this.setInputsInline(false);
  3828. this.setPreviousStatement(true, null);
  3829. this.setNextStatement(true, null);
  3830. this.setColour(NETWORK_BLOCK_COLOR);
  3831. // this.setTooltip("");
  3832. this.setHelpUrl("");
  3833. var thisBlock = this;
  3834. this.setTooltip(function() {
  3835. var mode = thisBlock.getFieldValue('response_type');
  3836. var TOOLTIPS = {
  3837. 'get': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_get),
  3838. 'post': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_post)
  3839. };
  3840. return TOOLTIPS[mode];
  3841. });
  3842. }
  3843. };
  3844. Blockly.Python['esp32_network_http_server_route'] = function(block) {
  3845. var dropdown_response_type = block.getFieldValue('response_type');
  3846. var text_path = block.getFieldValue('path');
  3847. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  3848. var response_type = "";
  3849. var response_writeok = "";
  3850. if (dropdown_response_type == "get") {
  3851. response_type = "";
  3852. } else if (dropdown_response_type == "post") {
  3853. response_type = ", 'POST'";
  3854. }
  3855. /*Blockly.Python.addVariable('', '' +
  3856. '' +
  3857. '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
  3858. 'def _httpHandlerTestGet(httpClient, httpResponse):\n' +
  3859. statements_exec +
  3860. '', true);*/
  3861. Blockly.Python.definitions_['esp32_network_http_server_route_def_' + text_path.substring(1)] = '' +
  3862. '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
  3863. 'def _httpHandler' + text_path.substring(1) + 'Get(httpClient, httpResponse):\n' +
  3864. statements_exec +
  3865. '\n';
  3866. Blockly.Python.addSetup("server_route_setup", '' +
  3867. 'def _acceptWebSocketCallback(webSocket, httpClient) :\n' +
  3868. ' print("WS ACCEPT")\n' +
  3869. ' webSocket.RecvTextCallback = _recvTextCallback\n' +
  3870. ' webSocket.RecvBinaryCallback = _recvBinaryCallback\n' +
  3871. ' webSocket.ClosedCallback = _closedCallback\n' +
  3872. '\n' +
  3873. 'def _recvTextCallback(webSocket, msg) :\n' +
  3874. ' print("WS RECV TEXT : %s" % msg)\n' +
  3875. ' webSocket.SendText("Reply for %s" % msg)\n' +
  3876. '\n' +
  3877. 'def _recvBinaryCallback(webSocket, data) :\n' +
  3878. ' print("WS RECV DATA : %s" % data)\n' +
  3879. '\n' +
  3880. 'def _closedCallback(webSocket) :\n' +
  3881. ' print("WS CLOSED")\n' +
  3882. '\n' +
  3883. 'try:\n' +
  3884. ' srv = MicroWebSrv()\n' +
  3885. ' srv.Stop()\n' +
  3886. ' srv.MaxWebSocketRecvLen = 1024\n' +
  3887. ' srv.WebSocketThreaded = False\n' +
  3888. ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
  3889. ' srv.Start(threaded=True)\n' +
  3890. 'except BaseException as e:\n' +
  3891. ' print(str(e))\n' +
  3892. ' srv = MicroWebSrv()\n' +
  3893. ' srv.MaxWebSocketRecvLen = 1024\n' +
  3894. ' srv.WebSocketThreaded = False\n' +
  3895. ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
  3896. ' srv.Start(threaded=True)\n' +
  3897. '');
  3898. // TODO: Assemble Python into code variable.
  3899. var code = '';
  3900. return code;
  3901. };
  3902. Blockly.Blocks['esp32_network_http_server_route_respond'] = {
  3903. init: function() {
  3904. this.appendValueInput("exec")
  3905. .setCheck(null)
  3906. .appendField(Blockly.Msg.wifi_web_http_server_respond_title);
  3907. this.appendDummyInput()
  3908. .appendField(Blockly.Msg.wifi_web_http_server_respond_type)
  3909. .appendField(new Blockly.FieldDropdown([
  3910. [Blockly.Msg.wifi_web_http_server_respond_text, "text/plain"],
  3911. [Blockly.Msg.wifi_web_http_server_respond_json, "application/json"],
  3912. [Blockly.Msg.wifi_web_http_server_respond_html, "text/html"]
  3913. ]), "response_type");
  3914. this.setInputsInline(false);
  3915. this.setPreviousStatement(true, null);
  3916. this.setNextStatement(true, null);
  3917. this.setColour(NETWORK_BLOCK_COLOR);
  3918. var thisBlock = this;
  3919. this.setTooltip(function() {
  3920. var mode = thisBlock.getFieldValue('response_type');
  3921. var TOOLTIPS = {
  3922. 'text/plain': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_text),
  3923. 'application/json': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_json),
  3924. 'text/html': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_html)
  3925. };
  3926. return TOOLTIPS[mode];
  3927. });
  3928. this.setHelpUrl("");
  3929. }
  3930. };
  3931. Blockly.Python['esp32_network_http_server_route_respond'] = function(block) {
  3932. var dropdown_response_type = block.getFieldValue('response_type');
  3933. var statements_exec = Blockly.Python.valueToCode(block, 'exec', Blockly.Python.ORDER_ATOMIC);
  3934. // TODO: Assemble Python into code variable.
  3935. var code = '' +
  3936. 'content = ' + statements_exec + '\n' +
  3937. 'httpResponse.WriteResponseOk( headers = None,\n' +
  3938. ' contentType = "' + dropdown_response_type + '",\n' +
  3939. ' contentCharset = "UTF-8",\n' +
  3940. ' content = str(content) )\n' +
  3941. '';
  3942. return code;
  3943. };
  3944. Blockly.Blocks['esp32_network_http_server_get_data'] = {
  3945. init: function() {
  3946. this.appendDummyInput()
  3947. .appendField(Blockly.Msg.wifi_web_http_server_get_title)
  3948. .appendField(new Blockly.FieldDropdown([
  3949. [Blockly.Msg.wifi_web_http_server_get_as_json, "json"],
  3950. [Blockly.Msg.wifi_web_http_server_get_as_text, "text"]
  3951. ]), "type");
  3952. this.setInputsInline(false);
  3953. this.setOutput(true, null);
  3954. this.setColour(NETWORK_BLOCK_COLOR);
  3955. var thisBlock = this;
  3956. this.setTooltip(function() {
  3957. var mode = thisBlock.getFieldValue('type');
  3958. var TOOLTIPS = {
  3959. 'json': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_json),
  3960. 'text': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_text)
  3961. };
  3962. return TOOLTIPS[mode];
  3963. });
  3964. this.setHelpUrl("");
  3965. }
  3966. };
  3967. Blockly.Python['esp32_network_http_server_get_data'] = function(block) {
  3968. var dropdown_type = block.getFieldValue('type');
  3969. if (dropdown_type == "json") {
  3970. var as_type = "ReadRequestContentAsJSON()";
  3971. } else if (dropdown_type == "text") {
  3972. var as_type = "ReadRequestContent(size=None)";
  3973. }
  3974. // TODO: Assemble Python into code variable.
  3975. var code = 'httpClient.' + as_type;
  3976. // TODO: Change ORDER_NONE to the correct strength.
  3977. return [code, Blockly.Python.ORDER_NONE];
  3978. };
  3979. var WS_BLOCK_COLOR = "#83178a";
  3980. Blockly.Blocks['esp32_web_ws_setup_send'] = {
  3981. init: function() {
  3982. this.appendDummyInput()
  3983. .appendField(new Blockly.FieldImage("blockly/media/ws_header_send.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
  3984. this.appendDummyInput()
  3985. .appendField(Blockly.Msg.internet_ws_setup_send_text);
  3986. this.appendDummyInput()
  3987. .appendField(Blockly.Msg.internet_ws_setup_send_ip)
  3988. .appendField(new Blockly.FieldTextInput("192.168.4.1"), "ip");
  3989. this.setInputsInline(false);
  3990. this.setPreviousStatement(true, null);
  3991. this.setNextStatement(true, null);
  3992. this.setColour(WS_BLOCK_COLOR);
  3993. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Send_TOOLTIP);
  3994. this.setHelpUrl("");
  3995. }
  3996. };
  3997. Blockly.Python['esp32_web_ws_setup_send'] = function(block) {
  3998. var text_ip = block.getFieldValue('ip');
  3999. Blockly.Python.definitions_['ws_setup_send'] = '' +
  4000. 'import uwebsockets.client\n' +
  4001. '\n';
  4002. // TODO: Assemble Python into code variable.
  4003. var code = '_client_websocket = uwebsockets.client.connect("ws://' + text_ip + ':80")\n';
  4004. return code;
  4005. };
  4006. Blockly.Blocks['esp32_web_ws_send'] = {
  4007. init: function() {
  4008. this.appendValueInput("command")
  4009. .setCheck(null)
  4010. .appendField(Blockly.Msg.internet_ws_send_msg);
  4011. this.setInputsInline(true);
  4012. this.setPreviousStatement(true, null);
  4013. this.setNextStatement(true, null);
  4014. this.setColour(WS_BLOCK_COLOR);
  4015. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Send_TOOLTIP);
  4016. this.setHelpUrl("");
  4017. }
  4018. };
  4019. Blockly.Python['esp32_web_ws_send'] = function(block) {
  4020. var value_command = Blockly.Python.valueToCode(block, 'command', Blockly.Python.ORDER_ATOMIC);
  4021. // TODO: Assemble Python into code variable.
  4022. var code = '' +
  4023. 'message = ' + value_command + '\n' +
  4024. '_client_websocket.send(message)\n';
  4025. return code;
  4026. };
  4027. Blockly.Blocks['esp32_web_ws_setup_receive'] = {
  4028. init: function() {
  4029. this.appendDummyInput()
  4030. .appendField(new Blockly.FieldImage("blockly/media/ws_header_receive.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
  4031. this.appendDummyInput()
  4032. .appendField(Blockly.Msg.internet_ws_setup_recv);
  4033. this.setInputsInline(false);
  4034. this.setPreviousStatement(true, null);
  4035. this.setNextStatement(true, null);
  4036. this.setColour(WS_BLOCK_COLOR);
  4037. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Receive_TOOLTIP);
  4038. this.setHelpUrl("");
  4039. }
  4040. };
  4041. Blockly.Python['esp32_web_ws_setup_receive'] = function(block) {
  4042. Blockly.Python.definitions_['ws_setup_receive'] = '' +
  4043. 'import uasyncio, logging\n' +
  4044. 'from uasyncio.websocket.server import WSReader, WSWriter\n' +
  4045. 'logging.basicConfig(level=logging.DEBUG)\n' +
  4046. '\n';
  4047. // TODO: Assemble Python into code variable.
  4048. var code = '';
  4049. return code;
  4050. };
  4051. Blockly.Blocks['esp32_web_ws_receive_statement'] = {
  4052. init: function() {
  4053. this.appendDummyInput()
  4054. .appendField(Blockly.Msg.internet_ws_setup_receiving);
  4055. this.appendStatementInput("command")
  4056. .setCheck(null)
  4057. .appendField(Blockly.Msg.internet_ws_setup_recv_exec);
  4058. this.setInputsInline(false);
  4059. this.setPreviousStatement(true, null);
  4060. this.setNextStatement(true, null);
  4061. this.setColour(WS_BLOCK_COLOR);
  4062. this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Receive_Statement_TOOLTIP);
  4063. this.setHelpUrl("");
  4064. }
  4065. };
  4066. Blockly.Python['esp32_web_ws_receive_statement'] = function(block) {
  4067. var statements_command = Blockly.Python.statementToCode(block, 'command');
  4068. Blockly.Python.definitions_['ws_setup_receive_statement'] = '' +
  4069. 'def _ws_exec(msg):\n' +
  4070. '' + statements_command +
  4071. '\n' +
  4072. 'def _ws_echo(reader, writer):\n' +
  4073. ' yield from reader.readline()\n' +
  4074. '\n' +
  4075. ' reader = yield from WSReader(reader, writer)\n' +
  4076. ' writer = WSWriter(reader, writer)\n' +
  4077. '\n' +
  4078. ' while 1:\n' +
  4079. ' _ws_l = yield from reader.read(1024)\n' +
  4080. ' print(_ws_l)\n' +
  4081. ' _ws_exec(_ws_l)\n' +
  4082. '\n' +
  4083. ' if _ws_l == b"\\r":\n' +
  4084. ' await writer.awrite(b"\\r\\n")\n' +
  4085. ' else:\n' +
  4086. ' await writer.awrite(_ws_l)\n' +
  4087. '\n' +
  4088. '_ws_loop = uasyncio.get_event_loop()\n' +
  4089. '_ws_loop.create_task(uasyncio.start_server(_ws_echo, "0.0.0.0", 80))\n' +
  4090. '_ws_loop.run_forever()\n' +
  4091. '_ws_loop.close()\n' +
  4092. '';
  4093. // TODO: Assemble Python into code variable.
  4094. var code = '\n';
  4095. return code;
  4096. };
  4097. Blockly.Blocks['esp32_web_ws_receive_get_data'] = {
  4098. init: function() {
  4099. this.appendDummyInput()
  4100. .appendField(Blockly.Msg.internet_ws_setup_get_msg);
  4101. this.appendDummyInput()
  4102. .appendField(Blockly.Msg.internet_ws_setup_get_msg_type)
  4103. .appendField(new Blockly.FieldDropdown([
  4104. [Blockly.Msg.internet_ws_setup_get_msg_text, "text"],
  4105. [Blockly.Msg.internet_ws_setup_get_msg_json, "json"]
  4106. ]), "type");
  4107. this.setInputsInline(false);
  4108. this.setOutput(true, null);
  4109. this.setColour(WS_BLOCK_COLOR);
  4110. var thisBlock = this;
  4111. this.setTooltip(function() {
  4112. var mode = thisBlock.getFieldValue('type');
  4113. var TOOLTIPS = {
  4114. 'text': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_text),
  4115. 'json': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_json)
  4116. };
  4117. return TOOLTIPS[mode];
  4118. });
  4119. this.setHelpUrl("");
  4120. }
  4121. };
  4122. Blockly.Python['esp32_web_ws_receive_get_data'] = function(block) {
  4123. var dropdown_type = block.getFieldValue('type');
  4124. // TODO: Assemble Python into code variable.
  4125. if (dropdown_type == "text") {
  4126. var code = "msg.decode(\"utf-8\")";
  4127. } else if (dropdown_type == "json") {
  4128. var code = "eval(msg.decode(\"utf-8\"))";
  4129. }
  4130. // TODO: Change ORDER_NONE to the correct strength.
  4131. return [code, Blockly.Python.ORDER_NONE];
  4132. };
  4133. /*
  4134. ____ _ _____
  4135. | __ )| | | ____|
  4136. | _ \| | | _|
  4137. | |_) | |___| |___
  4138. |____/|_____|_____|
  4139. */
  4140. var BLE_BLOCK_COLOR = "#629978";
  4141. Blockly.Blocks['esp32_ble_setup_sender'] = {
  4142. init: function() {
  4143. this.appendDummyInput()
  4144. .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
  4145. this.appendDummyInput()
  4146. .appendField(Blockly.Msg.esp32_ble_setup_sender_msg);
  4147. this.appendDummyInput()
  4148. .appendField(Blockly.Msg.esp32_ble_setup_sender);
  4149. this.setInputsInline(false);
  4150. this.setPreviousStatement(true, null);
  4151. this.setNextStatement(true, null);
  4152. this.setColour(BLE_BLOCK_COLOR);
  4153. this.setTooltip(Blockly.Msg.esp32_ble_setup_sender_TOOLTIP);
  4154. this.setHelpUrl("");
  4155. }
  4156. };
  4157. Blockly.Python['esp32_ble_setup_sender'] = function(block) {
  4158. Blockly.Python.definitions_['esp32_ble_setup_sender_def'] = '' +
  4159. 'import ble_simple_central\n' +
  4160. 'from ble_simple_central import BLESimpleCentral\n' +
  4161. 'import bluetooth, time\n' +
  4162. '\n' +
  4163. 'ble = bluetooth.BLE()\n' +
  4164. 'central = BLESimpleCentral(ble)\n' +
  4165. 'ble_read = \'\'\n' +
  4166. '\n' +
  4167. 'not_found = False\n' +
  4168. '\n' +
  4169. 'def ble_on_rx(v):\n' +
  4170. ' global ble_read\n' +
  4171. ' ble_read = v.decode("utf-8")\n' +
  4172. '\n' +
  4173. 'def ble_on_scan(addr_type, addr, name):\n' +
  4174. ' if addr_type is not None:\n' +
  4175. ' print("Found peripheral:", addr_type, addr, name)\n' +
  4176. ' central.connect()\n' +
  4177. ' else:\n' +
  4178. ' global not_found\n' +
  4179. ' not_found = True\n' +
  4180. ' print("No peripheral found.")\n' +
  4181. '\n' +
  4182. 'def ble_scan_and_connect():\n' +
  4183. ' central.scan(callback=ble_on_scan)\n' +
  4184. ' # Wait for connection...\n' +
  4185. ' print("Connecting...")\n' +
  4186. ' failed = 0\n' +
  4187. '\n' +
  4188. ' while not central.is_connected():\n' +
  4189. ' time.sleep_ms(100)\n' +
  4190. ' if not_found: return\n' +
  4191. '\n' +
  4192. ' print("Connected")\n' +
  4193. '\n' +
  4194. 'central.on_notify(ble_on_rx)\n' +
  4195. 'with_response = False\n' +
  4196. '';
  4197. // TODO: Assemble Python into code variable.
  4198. var code = 'ble_scan_and_connect()\n';
  4199. return code;
  4200. };
  4201. Blockly.Blocks['esp32_ble_send_data'] = {
  4202. init: function() {
  4203. this.appendValueInput("data")
  4204. .setCheck(null)
  4205. .appendField(Blockly.Msg.esp32_ble_send_data_msg);
  4206. this.setInputsInline(true);
  4207. this.setPreviousStatement(true, null);
  4208. this.setNextStatement(true, null);
  4209. this.setColour(BLE_BLOCK_COLOR);
  4210. this.setTooltip(Blockly.Msg.esp32_ble_send_data_TOOLTIP);
  4211. this.setHelpUrl("");
  4212. }
  4213. };
  4214. Blockly.Python['esp32_ble_send_data'] = function(block) {
  4215. var value_data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  4216. // TODO: Assemble Python into code variable.
  4217. var code = 'central_write_value = str(' + value_data + ')\n' +
  4218. 'central.write(central_write_value, with_response)\n' +
  4219. 'time.sleep_ms(400 if with_response else 30)\n' +
  4220. '';
  4221. return code;
  4222. };
  4223. Blockly.Blocks['esp32_ble_setup_receiver'] = {
  4224. init: function() {
  4225. this.appendDummyInput()
  4226. .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
  4227. this.appendDummyInput()
  4228. .appendField(Blockly.Msg.esp32_ble_setup_receiver_msg);
  4229. this.appendDummyInput()
  4230. .appendField(Blockly.Msg.esp32_ble_setup_receiver);
  4231. this.setInputsInline(false);
  4232. this.setPreviousStatement(true, null);
  4233. this.setNextStatement(true, null);
  4234. this.setColour(BLE_BLOCK_COLOR);
  4235. this.setTooltip(Blockly.Msg.esp32_ble_setup_receiver_TOOLTIP);
  4236. this.setHelpUrl("");
  4237. }
  4238. };
  4239. Blockly.Python['esp32_ble_setup_receiver'] = function(block) {
  4240. // TODO: Assemble Python into code variable.
  4241. Blockly.Python.definitions_['esp32_ble_setup_receiver_def'] = '' +
  4242. 'import ble_simple_peripheral\n' +
  4243. 'from ble_simple_peripheral import BLESimplePeripheral\n' +
  4244. 'import bluetooth, time\n' +
  4245. '\n' +
  4246. 'ble = bluetooth.BLE()\n' +
  4247. 'ble_p = BLESimplePeripheral(ble)\n' +
  4248. 'ble_read = \'\'\n' +
  4249. '\n' +
  4250. 'def ble_on_rx(v):\n' +
  4251. ' global ble_read\n' +
  4252. ' ble_read = v.decode("utf-8")\n' +
  4253. '\n' +
  4254. '';
  4255. var code = 'ble_p.on_write(ble_on_rx)\n';
  4256. return code;
  4257. };
  4258. Blockly.Blocks['esp32_ble_get_data'] = {
  4259. init: function() {
  4260. this.appendDummyInput()
  4261. .appendField(Blockly.Msg.esp32_ble_get_data_msg);
  4262. this.setInputsInline(false);
  4263. this.setOutput(true, null);
  4264. this.setColour(BLE_BLOCK_COLOR);
  4265. this.setTooltip(Blockly.Msg.esp32_ble_get_data_TOOLTIP);
  4266. this.setHelpUrl("");
  4267. }
  4268. };
  4269. Blockly.Python['esp32_ble_get_data'] = function(block) {
  4270. // TODO: Assemble Python into code variable.
  4271. var code = 'ble_read';
  4272. // TODO: Change ORDER_NONE to the correct strength.
  4273. return [code, Blockly.Python.ORDER_NONE];
  4274. };
  4275. Blockly.Blocks['esp32_ble_sender_connected'] = {
  4276. init: function() {
  4277. this.appendDummyInput()
  4278. .appendField(Blockly.Msg.esp32_ble_sender_connected_msg);
  4279. this.setOutput(true, null);
  4280. this.setColour(BLE_BLOCK_COLOR);
  4281. this.setTooltip(Blockly.Msg.esp32_ble_sender_connected_TOOLTIP);
  4282. this.setHelpUrl("");
  4283. }
  4284. };
  4285. Blockly.Python['esp32_ble_sender_connected'] = function(block) {
  4286. // TODO: Assemble Python into code variable.
  4287. var code = 'central.is_connected()';
  4288. // TODO: Change ORDER_NONE to the correct strength.
  4289. return [code, Blockly.Python.ORDER_NONE];
  4290. };
  4291. Blockly.Blocks['esp32_ble_receiver_connected'] = {
  4292. init: function() {
  4293. this.appendDummyInput()
  4294. .appendField(Blockly.Msg.esp32_ble_receiver_connected_msg);
  4295. this.setOutput(true, null);
  4296. this.setColour(BLE_BLOCK_COLOR);
  4297. this.setTooltip(Blockly.Msg.esp32_ble_receiver_connected_TOOLTIP);
  4298. this.setHelpUrl("");
  4299. }
  4300. };
  4301. Blockly.Python['esp32_ble_receiver_connected'] = function(block) {
  4302. // TODO: Assemble Python into code variable.
  4303. var code = 'ble_p.is_connected()';
  4304. // TODO: Change ORDER_NONE to the correct strength.
  4305. return [code, Blockly.Python.ORDER_NONE];
  4306. };