1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846 |
- /* additional text */
- Blockly.Blocks['text_split_string_by_delimiter'] = {
- init: function() {
- this.appendValueInput("string_input")
- .setCheck(null)
- .appendField(Blockly.Msg.text_additional_delimiter_string);
- this.appendDummyInput()
- .appendField(Blockly.Msg.text_additional_delimiter_deli)
- .appendField(new Blockly.FieldTextInput(","), "delimiter")
- .appendField(Blockly.Msg.text_additional_delimiter_end);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#33cc99");
- this.setTooltip(Blockly.Msg.Text_Split_String_By_Delimiter_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['text_split_string_by_delimiter'] = function(block) {
- var value_string_input = Blockly.Python.valueToCode(block, 'string_input', Blockly.Python.ORDER_ATOMIC);
- var text_delimiter = block.getFieldValue('delimiter');
- // TODO: Assemble Python into code variable.
- var code = value_string_input + '.split("' + text_delimiter + '")';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* additional math */
- Blockly.Blocks['math_degrad'] = {
- init: function() {
- this.appendValueInput("convert")
- .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.math_degrad_rad, "radians"],
- [Blockly.Msg.math_degrad_deg, "degrees"]
- ]), "rad_deg");
- this.setOutput(true, null);
- this.setColour("#5472ea");
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('rad_deg');
- var TOOLTIPS = {
- 'radians': Blockly.Msg.Math_Degrad_RAD_TOOLTIP,
- 'degrees': Blockly.Msg.Math_Degrad_DEG_TOOLTIP
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['math_degrad'] = function(block) {
- var dropdown_rad_deg = block.getFieldValue('rad_deg');
- var value_convert = Blockly.Python.valueToCode(block, 'convert', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_math'] = 'import math\n';
- var code = 'math.' + dropdown_rad_deg + '(' + value_convert + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* Serial Comm. */
- Blockly.Blocks['serial_comm_print'] = {
- init: function() {
- this.appendValueInput("serial_comm_input")
- .setCheck("String")
- .appendField(Blockly.Msg.serialcomm_print);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#22b845");
- this.setTooltip(Blockly.Msg.serial_Comm_Print_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['serial_comm_print'] = function(block) {
- var value_name = Blockly.Python.valueToCode(block, 'serial_comm_input', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'print(' + value_name + ')\n';
- return code;
- };
- /*
- INPUT & OUTPUT
- */
- var ESP32_IO_COLOR = "#ff8027";
- Blockly.Blocks['esp32_main_controller_text_print'] = {
- init: function() {
- this.jsonInit({
- "message0": Blockly.Msg.TEXT_PRINT_TITLE,
- "args0": [{
- "type": "input_value",
- "name": "TEXT"
- }],
- "previousStatement": null,
- "nextStatement": null,
- "colour": "#33cc99",
- "tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
- "helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
- });
- }
- };
- Blockly.Python['esp32_main_controller_text_print'] = function(block) {
- // Print statement.
- var msg = Blockly.Python.valueToCode(block, 'TEXT',
- Blockly.Python.ORDER_NONE) || '___';
- return 'print(' + msg + ')\n';
- };
- Blockly.Blocks['esp32_main_controller_text'] = {
- init: function() {
- this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
- this.setColour("#33cc99");
- this.appendDummyInput()
- .appendField(this.newQuote_(true))
- .appendField(new Blockly.FieldTextInput(''), 'TEXT')
- .appendField(this.newQuote_(false));
- this.setOutput(true, 'String');
- // Assign 'this' to a variable for use in the tooltip closure below.
- var thisBlock = this;
- // Text block is trivial. Use tooltip of parent block if it exists.
- this.setTooltip(function() {
- var parent = thisBlock.getParent();
- return (parent && parent.getInputsInline() && parent.tooltip) ||
- Blockly.Msg.TEXT_TEXT_TOOLTIP;
- });
- },
- newQuote_: function(open) {
- if (open == this.RTL) {
- var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
- } else {
- var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
- }
- return new Blockly.FieldImage(file, 12, 12, '"');
- }
- };
- Blockly.Python['esp32_main_controller_text'] = function(block) {
- // Text value.
- var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
- init: function() {
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.digital_get) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.digital_get);
- this.appendValueInput("digital_pin_input")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('digital_get_type');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- '2': Blockly.Msg.Esp32_Main_Controller_Io_From_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_from_digital_pin'] = function(block) {
- var value_name = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
- var timer_type = block.getFieldValue('digital_get_type');
- // TODO: Assemble Python into code variable.
- // TODO: Assemble Python into code variable.
- var code = '';
- if (timer_type == 2) {
- Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA,GPIO\n' +
- '';
- let aa = 'FPIOA().set_function(' + value_name + ',fm.fpioa.GPIOHS' + value_name + ')\n' +
- '_read_ai_ext_pin' + value_name + ' = GPIO(GPIO.GPIOHS' + value_name + ', GPIO.IN,GPIO.PULL_UP)\n'
- Blockly.Python.addVariable('_read_ai_ext_pin' + value_name, aa, true);
- code = '_read_ai_ext_pin' + value_name + '.value()';
- } else if (timer_type == 1) {
- Blockly.Python.definitions_['import_from_digital_pin_setup'] = '' +
- 'from machine import Pin\n' +
- '';
- let aa = '_PIN_IN_' + value_name + ' = Pin(' + value_name + ', Pin.IN)\n';
- Blockly.Python.addVariable('_PIN_IN_' + value_name, aa, true);
- code = '_PIN_IN_' + value_name + '.value()'
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_io_high_low'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.io_high, "1"],
- [Blockly.Msg.io_low, "0"]
- ]), "high_low");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('high_low');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_high),
- '0': Blockly.Msg.Esp32_Main_Controller_Io_High_Low_TOOLTIP.replace('%1', Blockly.Msg.io_low)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_high_low'] = function(block) {
- var dropdown_name = block.getFieldValue('high_low');
- // TODO: Assemble Python into code variable.
- var code = dropdown_name;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
- init: function() {
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_set_type")
- .appendField(Blockly.Msg.digital_set) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_set_type")
- .appendField(Blockly.Msg.digital_set);
- this.appendValueInput("digital_pin_input")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.digital_set_as);
- this.appendValueInput("high_low")
- .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('digital_set_type');
- var TOOLTIPS = {
- '1': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- '2': Blockly.Msg.Esp32_Main_Controller_Io_Set_Digital_Pin_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function(block) {
- var value_name1 = Blockly.Python.valueToCode(block, 'digital_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_name2 = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC);
- var timer_type = block.getFieldValue('digital_set_type');
- var code = "";
- if (timer_type == 1) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_set_digital_pin_setup'] = '' +
- 'from machine import Pin\n' +
- '';
- let variable = '_PIN_OUT_' + value_name1 + ' = Pin(' + value_name1 + ', Pin.OUT)\n'
- Blockly.Python.addVariable('_PIN_OUT_' + value_name1, variable, true);
- code = '_PIN_OUT_' + value_name1 + '.value' + value_name2 + '\n';
- } else if (timer_type == 2) {
- Blockly.Python.definitions_['import_set_digital_pin_setup'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- '';
- let variable = 'fm.register(' + value_name1 + ',fm.fpioa.GPIOHS' + value_name1 + ')\n' +
- '_set_ai_ext_pin' + value_name1 + ' = GPIO(GPIO.GPIOHS' + value_name1 + ', GPIO.OUT)\n'
- Blockly.Python.addVariable('_set_ai_ext_pin' + value_name1, variable, true);
- code = '_set_ai_ext_pin' + value_name1 + '.value(' + value_name2 + ')\n';
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_io_from_analog_pin'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.analog_get);
- this.appendValueInput("analog_pin_input")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.analog_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Io_From_Analog_Pin_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_io_from_analog_pin'] = function(block) {
- var value_name = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.addVariable('_PWM_PIN_IN_' + value_name, '', true);
- Blockly.Python.definitions_['import_setup'] = '' +
- 'from machine import Pin, ADC\n\n' +
- '_PWM_PIN_IN_' + value_name + ' = ADC(Pin(' + value_name + '))\n' +
- '_PWM_PIN_IN_' + value_name + '.atten(ADC.ATTN_11DB)\n' +
- '';
- var code = '_PWM_PIN_IN_' + value_name + '.read()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_io_set_analog_pin'] = {
- init: function() {
- this.appendValueInput("analog_pin_input")
- .appendField(Blockly.Msg.analog_set)
- .setCheck(null);
- // this.appendValueInput("frequency")
- // .appendField(Blockly.Msg.analog_set_freq)
- // .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.control)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.led, "LED"]
- ]), "LED");
- this.appendValueInput("pwm")
- .appendField(Blockly.Msg.set_pwm)
- .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('LED');
- var TOOLTIPS = {
- 'LED': Blockly.Msg.Esp32_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_io_set_analog_pin'] = function(block) {
- var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
- // var value_name3 = Blockly.Python.valueToCode(block, 'frequency', Blockly.Python.ORDER_ATOMIC);
- // var module_type = block.getFieldValue('digital_set_type');
- Blockly.Python.addVariable('_pwm_gpio_' + value_name1, '', true);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_analog_set_setup'] = 'from machine import Pin, PWM\n';
- var code = '_pwm_gpio_' + value_name1 + ' = PWM(Pin(' + value_name1 + '), freq=300, duty=' + value_name2 + ')\n';
- return code;
- };
- Blockly.Blocks['ai_main_controller_io_set_analog_pin'] = {
- init: function() {
- var _en = true;
- for (var i = 0; i < ids.length; i++) {
- if (!ids[i]) {
- _en = false;
- }
- }
- this.disabled = ids.length > 11 && _en ? true : false;
- this.appendValueInput("analog_pin_input")
- .appendField(Blockly.Msg.analog_set_ai)
- .setCheck(null);
- // this.appendValueInput("frequency")
- // .appendField(Blockly.Msg.analog_set_freq)
- // .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.control)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.led, "LED"]
- ]), "LED");
- this.appendValueInput("pwm")
- .appendField(Blockly.Msg.set_pwm)
- .setCheck(null);
- // this.appendValueInput("timer")
- // .appendField(Blockly.Msg.analog_set_timer)
- // .setCheck(null);
- // this.appendValueInput("channel")
- // .appendField(Blockly.Msg.analog_set_timer_channel)
- // .setCheck(null);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IO_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('LED');
- var TOOLTIPS = {
- 'LED': Blockly.Msg.Ai_Main_Controller_Io_Set_Analog_Pin_TOOLTIP.replace('%1', Blockly.Msg.led)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- var ids = [];
- Blockly.Python['ai_main_controller_io_set_analog_pin'] = function(block) {
- var value_name1 = Blockly.Python.valueToCode(block, 'analog_pin_input', Blockly.Python.ORDER_ATOMIC);
- var value_name2 = Blockly.Python.valueToCode(block, 'pwm', Blockly.Python.ORDER_ATOMIC);
- var str = '';
- var j = 0;
- var returnvalue = '';
- for (var i = 0; i < ids.length; i++) {
- if (ids[i] && !Blockly.getMainWorkspace().getBlockById(ids[i].id)) {
- ids[i] = null;
- }
- }
- for (var i = 0; i < ids.length; i++) {
- if (ids[i] && ids[i].id == block.id) {
- j = i;
- break;
- } else {
- if (j == 0) {
- if (!ids[i]) {
- j = i;
- } else if (i == ids.length - 1) {
- j = ids.length;
- }
- }
- }
- }
- if (!ids[j]) {
- ids[j] = { id: block.id, index: j };
- }
- for (var i = 0; i < ids.length; i++) {
- if (ids[i]) {
- Blockly.Python.addVariable('_pwm_gpio_tim_' + ids[i].index, '', true);
- Blockly.Python.addVariable('_pwm_gpio_control_' + ids[i].index, '', true);
- 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';
- str += '_pwm_gpio_control_' + ids[i].index + ' = PWM(_pwm_gpio_tim_' + ids[i].index + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- }
- }
- returnvalue += '_pwm_gpio_control_' + ids[j].index + '.duty(int(' + value_name2 + '/10.23))\n';
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
- 'from machine import Pin,PWM,Timer\n' +
- '\n' +
- '_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;
- return returnvalue;
- // var module_type = block.getFieldValue('digital_set_type');
- // var str = '';
- // var ss = '';
- // if (counts.length != 0) {
- // if (ids.indexOf(block.id) == -1) {
- // ids.push(block.id);
- // count = count + 1;
- // counts.push(count);
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
- // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // else {
- // for (var i = 0; i < counts.length; i++) {
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + counts[i], '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + counts[i], '', true);
- // str += '_pwm_gpio_tim_' + counts[i] + ' = Timer(_io_timer_use[' + counts[i] + '][0],_io_timer_use[' + counts[i] + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + counts[i] + ' = PWM(_pwm_gpio_tim_' + counts[i] + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + counts[i] + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // }
- // }
- // else {
- // if (ids.indexOf(block.id) == -1) {
- // ids.push(block.id);
- // counts.push(count);
- // Blockly.Python.addVariable('_pwm_gpio_tim_' + count, '', true);
- // Blockly.Python.addVariable('_pwm_gpio_control_' + count, '', true);
- // str += '_pwm_gpio_tim_' + count + ' = Timer(_io_timer_use[' + count + '][0],_io_timer_use[' + count + '][1], mode=Timer.MODE_PWM)\n';
- // str += '_pwm_gpio_control_' + count + ' = PWM(_pwm_gpio_tim_' + count + ', freq=500000, duty=0, pin=' + value_name1 + ')\n';
- // ss += '_pwm_gpio_control_' + count + '.duty(int(' + value_name2 + '/10.23))\n';
- // }
- // }
- // // TODO: Assemble Python into code variable.
- // Blockly.Python.definitions_['import_analog_ai_set_setup'] = '' +
- // 'from machine import Pin,PWM,Timer\n' +
- // '\n' +
- // '_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;
- // // code = ss;
- // return ss;
- };
- /*
- Button
- */
- Blockly.Blocks['esp32_main_controller_button_read_pressed'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/button_pressed.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32BUTTON)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.ESP32PRESSED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Pressed_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_button_read_pressed'] = function(block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(_' + _type + '.value() == ' + 1 + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_button_read_released'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/button_released.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32BUTTON)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.ESP32RELEASED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Released_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_button_read_released'] = function(block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(_' + _type + '.value() == ' + 0 + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_button_read_value'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_button_read_value)
- .appendField(new Blockly.FieldDropdown([
- ["P1", "button_type_1"],
- ["P2", "button_type_2"]
- ]), "button_type")
- .appendField(Blockly.Msg.basic_button_read_value_after);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'button_type_1': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P1"),
- 'button_type_2': Blockly.Msg.Esp32_Main_Controller_Button_Read_Value_TOOLTIP.replace('%1', "P2")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_button_read_value'] = function(block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- // TODO: Assemble Python into code variable.
- var _num = _type == "P1" ? "34" : "35";
- var _upstr = "";
- if (_type == "P2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('_' + _type, '_' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '_' + _type + '.value()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Python['esp32_main_controller_button_read'] = function (block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getField('button_type').text_;
- var _button_state = block.getFieldValue('button_state');
- var _num = _type == "1" ? "36" : "22";
- var _upstr = "";
- if (_type == "2") { _upstr = ",Pin.PULL_UP"; }
- Blockly.Python.addVariable('p' + _type, 'p' + _type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')', true);
- var code = '(p' + _type + '.value() == ' + (_button_state == "button_state_pressed" ? "1" : "0") + ')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- */
- /*
- Dialer
- */
- Blockly.Blocks['esp32_main_controller_dial_switch_read_pressed'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32DIALSWITCH)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
- [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
- [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
- ]), "switch_type")
- .appendField(Blockly.Msg.ESP32PRESSED)
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_dial_switch_read_pressed'] = function(block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getFieldValue('switch_type');
- var _num = "34";
- var _upstr = "";
- var _button_type = "";
- if (_type == "switch_type_left") {
- var _num = "34";
- _button_type = "Left";
- } else if (_type == "switch_type_middle") {
- var _num = "19";
- _upstr = ", Pin.PULL_UP";
- _button_type = "Middle";
- } else if (_type == "switch_type_right") {
- var _num = "35";
- _button_type = "Right";
- }
- // Blockly.Python.addVariable("dialer_type", 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
- Blockly.Python.addVariable("pLeft", 'pLeft = Pin(38, Pin.IN)');
- Blockly.Python.addVariable("pMiddle", 'pMiddle = Pin(19, Pin.IN)');
- Blockly.Python.addVariable("pRight", 'pRight = Pin(35, Pin.IN)');
- var code = '(p' + _button_type + '.value() == 1)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_dial_switch_read_released'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32DIALSWITCH)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ESP32DIALER_LEFT, "switch_type_left"],
- [Blockly.Msg.ESP32DIALER_MIDDLE, "switch_type_middle"],
- [Blockly.Msg.ESP32DIALER_RIGHT, "switch_type_right"]
- ]), "switch_type")
- .appendField(Blockly.Msg.ESP32RELEASED)
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_dial_switch_read_released'] = function(block) {
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = block.getFieldValue('switch_type');
- var _state = block.getFieldValue('switch_state');
- var _num = "34";
- var _upstr = "";
- var _button_type = "";
- if (_type == "switch_type_left") {
- var _num = "34";
- _button_type = "Left";
- } else if (_type == "switch_type_middle") {
- var _num = "19";
- _upstr = ", Pin.PULL_UP";
- _button_type = "Middle";
- } else if (_type == "switch_type_right") {
- var _num = "35";
- _button_type = "Right";
- }
- Blockly.Python.addVariable('p' + _button_type, 'p' + _button_type + ' = Pin(' + _num + ', Pin.IN' + _upstr + ')');
- var code = '(p' + _button_type + '.value() == 0)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- LED
- */
- Blockly.Blocks['esp32_main_controller_set_led_turnon'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/led_on.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
- this.appendValueInput("led_type")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNON);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnon_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_turnon'] = function(block) {
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
- var _state = block.getFieldValue('led_state');
- var _num = "2";
- if (_type == "1") {
- var _num = "2";
- } else if (_type == "2") {
- var _num = "12";
- } else if (_type == "3") {
- var _num = "5";
- } else if (_type == "4") {
- var _num = "13";
- } else if (_type == "5") {
- var _num = "14";
- } else if (_type == "6") {
- var _num = "15";
- } else if (_type == "7") {
- var _num = "18";
- } else if (_type == "8") {
- var _num = "19";
- } else if (_type == "9") {
- var _num = "23";
- } else {
- var _num = "ERROR";
- }
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- var code = '_led_light(True,' + _type + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_turnoff'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/led_off.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TURN + Blockly.Msg.ESP32LED_MONO_TEXT_NO);
- this.appendValueInput("led_type")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_MONO_TEXT_AFTER + Blockly.Msg.ESP32LED + Blockly.Msg.ESP32LED_TURNOFF);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Turnoff_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_turnoff'] = function(block) {
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- var _type = Blockly.Python.valueToCode(block, 'led_type', Blockly.Python.ORDER_ATOMIC);
- var _state = block.getFieldValue('led_state');
- var _num = "2";
- if (_type == "1") {
- var _num = "2";
- } else if (_type == "2") {
- var _num = "12";
- } else if (_type == "3") {
- var _num = "5";
- } else if (_type == "4") {
- var _num = "13";
- } else if (_type == "5") {
- var _num = "14";
- } else if (_type == "6") {
- var _num = "15";
- } else if (_type == "7") {
- var _num = "18";
- } else if (_type == "8") {
- var _num = "19";
- } else if (_type == "9") {
- var _num = "23";
- } else {
- var _num = "ERROR";
- }
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- var code = '_led_light(False,' + _type + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_drawpixel_on'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTUP + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
- this.appendValueInput("x")
- .setCheck(null)
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_On_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_drawpixel_on'] = function(block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- Blockly.Python.addSetup('drawpixel_setup', '' +
- 'def _led_draw_pixel(state, x, y):\n' +
- ' real_pin = (3*y-3+x)\n' +
- ' _led_light(state,real_pin)\n' +
- '\n')
- var code = '_led_draw_pixel(True,' + value_x + ',' + value_y + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_set_led_drawpixel_off'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHTOFF + Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_INIT_TEXT);
- this.appendValueInput("x")
- .setCheck(null)
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_MIDDLE_TEXT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32LED_CORD_DRAWPIXEL_LIGHT_END_TEXT);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Led_Drawpixel_Off_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_led_drawpixel_off'] = function(block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_led_Pin'] = 'from machine import Pin';
- Blockly.Python.addSetup('iot_led_light_setup', '' +
- '_light = [0,2,12,5,13,14,15,18,19,23]\n' +
- '\n' +
- '_light_1 = Pin(_light[1], Pin.OUT, value=0)\n' +
- '_light_2 = Pin(_light[2], Pin.OUT, value=0)\n' +
- '_light_3 = Pin(_light[3], Pin.OUT, value=0)\n' +
- '_light_4 = Pin(_light[4], Pin.OUT, value=0)\n' +
- '_light_5 = Pin(_light[5], Pin.OUT, value=0)\n' +
- '_light_6 = Pin(_light[6], Pin.OUT, value=0)\n' +
- '_light_7 = Pin(_light[7], Pin.OUT, value=0)\n' +
- '_light_8 = Pin(_light[8], Pin.OUT, value=0)\n' +
- '_light_9 = Pin(_light[9], Pin.OUT, value=0)\n' +
- '\n' +
- 'def _led_light(state, pin):\n' +
- ' if (state == True):\n' +
- ' if pin == 1: _light_1.value(1)\n' +
- ' if pin == 2: _light_2.value(1)\n' +
- ' if pin == 3: _light_3.value(1)\n' +
- ' if pin == 4: _light_4.value(1)\n' +
- ' if pin == 5: _light_5.value(1)\n' +
- ' if pin == 6: _light_6.value(1)\n' +
- ' if pin == 7: _light_7.value(1)\n' +
- ' if pin == 8: _light_8.value(1)\n' +
- ' if pin == 9: _light_9.value(1)\n' +
- ' elif (state == False):\n' +
- ' if pin == 1: _light_1.value(0)\n' +
- ' if pin == 2: _light_2.value(0)\n' +
- ' if pin == 3: _light_3.value(0)\n' +
- ' if pin == 4: _light_4.value(0)\n' +
- ' if pin == 5: _light_5.value(0)\n' +
- ' if pin == 6: _light_6.value(0)\n' +
- ' if pin == 7: _light_7.value(0)\n' +
- ' if pin == 8: _light_8.value(0)\n' +
- ' if pin == 9: _light_9.value(0)\n' +
- '');
- Blockly.Python.addSetup('drawpixel_setup', '' +
- 'def _led_draw_pixel(state, x, y):\n' +
- ' real_pin = (3*y-3+x)\n' +
- ' _led_light(state,real_pin)\n' +
- '\n')
- var code = '_led_draw_pixel(False,' + value_x + ',' + value_y + ')\n';
- return code;
- };
- /*
- BUZZER
- */
- Blockly.Blocks['esp32_main_controller_set_buzzer'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/buzzer.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField( /*Blockly.Msg.ESP32TURNOBAF*/ );
- //.appendField(new Blockly.FieldNumber(31, 31, 65535, 1), "buzzer_frequency");
- this.appendValueInput("esp32_var_num")
- .setCheck("Number")
- .appendField(Blockly.Msg.basic_buzzer_set);
- this.setInputsInline(true);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Set_Buzzer_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_set_buzzer'] = function(block) {
- Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
- Blockly.Python.definitions_['import_Pin'] = 'from machine import Pin';
- var _type = Blockly.Python.valueToCode(
- block, 'esp32_var_num', Blockly.Python.ORDER_ATOMIC) || '0';
- Blockly.Python.definitions_['buzzer_operation'] = '' +
- 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' +
- 'beeper.deinit()\n' +
- /*
- 'def _beeper_control(state, freq=31):\n' +
- ' if freq <= 31: freq = 31\n' +
- ' elif freq >= 67108863: freq = 67108863\n' +
- ' if state == True:\n' +
- ' beeper.init()\n' +
- ' beeper.freq(freq)\n' +
- ' time.sleep_ms(20)\n' +
- ' elif state == False:\n' +
- ' time.sleep_ms(20)\n' +
- ' beeper.deinit()\n' +
- */
- '\n';
- var code = 'beeper.init()\nbeeper.freq(' + _type + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_turn_off_buzzer'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TURNOB);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Turn_Off_Buzzer_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_turn_off_buzzer'] = function(block) {
- Blockly.Python.definitions_['import_PWM'] = 'from machine import PWM';
- Blockly.Python.definitions_['buzzer_operation'] = '' +
- 'beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512)\n' +
- 'beeper.deinit()\n' +
- /*
- 'def _beeper_control(state, freq=31):\n' +
- ' if freq <= 31: freq = 31\n' +
- ' elif freq >= 67108863: freq = 67108863\n' +
- ' if state == True:\n' +
- ' beeper.init()\n' +
- ' beeper.freq(freq)\n' +
- ' time.sleep_ms(20)\n' +
- ' elif state == False:\n' +
- ' beeper.deinit()\n' +
- ' time.sleep_ms(20)\n' +
- */
- '\n';
- var code = 'beeper.deinit()\n';
- return code;
- //return [code, Blockly.Python.ORDER_FUNCTION_CALL];
- };
- /*
- TOUCH AREA
- */
- Blockly.Blocks['esp32_main_controller_touch_read_touched'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/touched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TA)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_TOUCHED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Touched_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_touched'] = function(block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") < 100';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_touch_read_untouched'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/untouched.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32TA)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_UNTOUCHED)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Untouched_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_untouched'] = function(block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '") >= 100';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_touch_read_value'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_READ)
- .appendField(new Blockly.FieldDropdown([
- ["A", "touch_area_type_a"],
- ["B", "touch_area_type_b"]
- ]), "touch_area_type")
- .appendField(Blockly.Msg.ESP32_READ_VALUE);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('touch_area_type');
- var TOOLTIPS = {
- 'touch_area_type_a': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "A"),
- 'touch_area_type_b': Blockly.Msg.Esp32_Main_Controller_Touch_Read_Value_TOOLTIP.replace('%1', "B")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_touch_read_value'] = function(block) {
- Blockly.Python.definitions_['import_touch_esp32'] = 'from machine import TouchPad\n' +
- 'from machine import Pin\n' +
- '\n' +
- '_TOUCH_A = TouchPad(Pin(4))\n' +
- '_TOUCH_B = TouchPad(Pin(27))\n' +
- '\n' +
- 'def GetTouchValue(area):\n' +
- ' try:\n' +
- ' if area == "A":\n' +
- ' return _TOUCH_A.read()\n' +
- ' elif area == "B":\n' +
- ' return _TOUCH_B.read()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- // var code = '_TOUCH_' + block.getField('touch_area_type').text_ + '.read() ' + "< 100";
- var code = 'GetTouchValue("' + block.getField('touch_area_type').text_ + '")';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- Env Sensor: BME280
- */
- Blockly.Blocks['esp32_main_controller_get_environmental_value'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_temperature, "temperature"],
- [Blockly.Msg.basic_humidity, "humidity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('env_get_type');
- var TOOLTIPS = {
- 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
- 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_get_environmental_value'] = function(block) {
- var _type_selected = block.getFieldValue('env_get_type');
- /*
- from machine import Pin, I2C
- import bme280 as BME280
- _SENSOR_BME_PINS = I2C(scl=Pin(17), sda=Pin(16), freq=10000)
- _BME = BME280.BME280(i2c=_SENSOR_BME_PINS)
- */
- Blockly.Python.definitions_['import_bme280'] = 'from machine import Pin, I2C\n' +
- 'from aht10 import AHT10\n\n' +
- '_aht10_i2c = I2C(sda=Pin(32), scl=Pin(33))\n' +
- '_aht10_sensor = AHT10(_aht10_i2c)\n' +
- '_aht10_sensor.sensor_init()\n' +
- '_aht10_sensor.is_calibration_enabled()\n' +
- '\n' +
- 'def GetTemperatureValue():\n' +
- ' try:\n' +
- ' if _aht10_sensor.read_temperature() < 0:\n' +
- ' return 0\n' +
- ' elif _aht10_sensor.read_temperature() >= 0:\n' +
- ' return _aht10_sensor.read_temperature()\n' +
- ' except BaseException as e:\n' +
- ' return False\n' +
- '';
- Blockly.Python.addVariable('_BME', '');
- if (_type_selected == 'temperature') {
- var code = 'GetTemperatureValue()';
- } else if (_type_selected == 'humidity') {
- var code = '_aht10_sensor.read_' + _type_selected + '()';
- } else {
- var code = _type_selected;
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- Motion Sensor: MPU6050
- */
- Blockly.Blocks['esp32_main_controller_motion_init'] = {
- init: function() {
- //this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/motion_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_use);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_init'] = function(block) {
- // Blockly.Python.addVariable('_SENSOR_MPU_PINS', '');
- Blockly.Python.definitions_['import_mpu6050'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import mpu6050\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- var code = '' +
- '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_when_shaking'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_shaking);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_shaking'] = function(block) {
- Blockly.Python.definitions_['shaking_def'] = '' +
- 'def _CHECK_SHAKING():\n' +
- ' if _MPU_SHAKING_OFFSET < 0 and _SSTATE == 0:\n' +
- ' _SSTATE = 1\n' +
- ' return True\n' +
- ' elif _MPU_SHAKING_OFFSET > 0 and _SSTATE == 1:\n' +
- ' _SSTATE = 0\n' +
- ' return False\n' +
- '';
- var code = '(_CHECK_SHAKING() == True)';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_when_shaking_STATEMENT'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_shaking_when_module)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_shaking_when_is_in, "True"],
- [Blockly.Msg.basic_when_shaking_when_is_not_in, "False"]
- ]), "status")
- .appendField(Blockly.Msg.basic_when_shaking_when_shaking);
- this.appendStatementInput("input")
- .setCheck(null)
- .appendField(Blockly.Msg.basic_motion_statement_exec);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('status');
- var TOOLTIPS = {
- 'True': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_in),
- 'False': Blockly.Msg.Esp32_Main_Controller_Motion_When_Shaking_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_shaking_when_is_not_in)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_shaking_STATEMENT'] = function(block) {
- var dropdown_status = block.getFieldValue('status');
- var statements_input = Blockly.Python.statementToCode(block, 'input');
- Blockly.Python.definitions_['import_mpu6050'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import mpu6050\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
- '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '';
- Blockly.Python.definitions_['shaking_def'] = '' +
- 'def _CHECK_SHAKING():\n' +
- ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- ' _MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- ' _MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- ' _SSTATE = 0\n' +
- ' if _MPU_SHAKING_OFFSET < 0:\n' +
- ' _SSTATE = 1\n' +
- ' return True\n' +
- ' elif _MPU_SHAKING_OFFSET > 0:\n' +
- ' _SSTATE = 0\n' +
- ' return False\n' +
- '';
- var code = '' +
- 'if (_CHECK_SHAKING() == ' + dropdown_status + '):\n' +
- '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '' + statements_input + '\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_when_tilting'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_tilting_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_tilting_get_left, "L"],
- [Blockly.Msg.basic_when_tilting_get_right, "R"],
- [Blockly.Msg.basic_when_tilting_get_forward, "F"],
- [Blockly.Msg.basic_when_tilting_get_backward, "B"]
- ]), "tilt_type")
- .appendField(Blockly.Msg.basic_when_tilting_get_when);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#e8795b");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_tilting'] = function(block) {
- var _type_selected = block.getFieldValue('tilt_type');
- Blockly.Python.definitions_['tilting_def'] = '' +
- 'def _CHECK_TILT_LR():\n' +
- ' if _MPU_OFFSET_TILT_X < 160 and _MPU_OFFSET_TILT_X > 0:\n' +
- ' return "R"\n' +
- ' elif _MPU_OFFSET_TILT_X > 200 and _MPU_OFFSET_TILT_X < 360:\n' +
- ' return "L"\n' +
- ' else:\n' +
- ' return None\n' +
- '\n' +
- 'def _CHECK_TILT_FB():\n' +
- ' if _MPU_OFFSET_TILT_Y < 160 and _MPU_OFFSET_TILT_Y > 0:\n' +
- ' return "F"\n' +
- ' elif _MPU_OFFSET_TILT_Y > 200 and _MPU_OFFSET_TILT_Y < 360:\n' +
- ' return "B"\n' +
- ' else:\n' +
- ' return None\n' +
- '';
- if (_type_selected == "L" || _type_selected == "R") {
- _check_function = '(_CHECK_TILT_LR() == "';
- } else if (_type_selected == "F" || _type_selected == "B") {
- _check_function = '(_CHECK_TILT_FB() == "';
- }
- var code = _check_function + _type_selected + '")';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_when_tilting_STATEMENT'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_when_tilting_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_when_tilting_get_left, "L"],
- [Blockly.Msg.basic_when_tilting_get_right, "R"],
- [Blockly.Msg.basic_when_tilting_get_forward, "F"],
- [Blockly.Msg.basic_when_tilting_get_backward, "B"],
- [Blockly.Msg.basic_when_tilting_not_tilting, "N"]
- ]), "tilt_type")
- .appendField(Blockly.Msg.basic_when_tilting_get_when);
- this.appendStatementInput("input")
- .setCheck(null)
- .appendField(Blockly.Msg.basic_motion_statement_exec);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null)
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('tilt_type');
- var TOOLTIPS = {
- 'L': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_left),
- 'R': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_right),
- 'F': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_forward),
- 'B': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_get_backward),
- 'N': Blockly.Msg.Esp32_Main_Controller_Motion_When_Tilting_STATEMENT_TOOLTIP.replace('%1', Blockly.Msg.basic_when_tilting_not_tilting)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_when_tilting_STATEMENT'] = function(block) {
- var _type_selected = block.getFieldValue('tilt_type');
- var statements_input = Blockly.Python.statementToCode(block, 'input');
- Blockly.Python.definitions_['import_mpu6050'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import mpu6050\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
- '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n'
- /* +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- ''*/
- ;
- Blockly.Python.definitions_['tilting_def'] = '' +
- 'def _CHECK_TILT_DIRECTION():\n' +
- ' _MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- ' _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- ' _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- ' _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- ' if _MPU_OFFSET_TILT_X < 150 and _MPU_OFFSET_TILT_X > 0:\n' +
- ' return "L"\n' +
- ' elif _MPU_OFFSET_TILT_X > 210 and _MPU_OFFSET_TILT_X < 360:\n' +
- ' return "R"\n' +
- ' if _MPU_OFFSET_TILT_Y < 150 and _MPU_OFFSET_TILT_Y > 0:\n' +
- ' return "B"\n' +
- ' elif _MPU_OFFSET_TILT_Y > 210 and _MPU_OFFSET_TILT_Y < 360:\n' +
- ' return "F"\n' +
- ' else:\n' +
- ' return "N"\n' +
- '';
- var _check_function = "(_CHECK_TILT_DIRECTION() == \"";
- var code = '' +
- 'if ' + _check_function + _type_selected + '"):\n' +
- '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '' + statements_input + '\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_motion_acceleration'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_get)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "accel_type")
- .appendField(Blockly.Msg.basic_motion_accel);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('accel_type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'X'),
- 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Y'),
- 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Acceleration_TOOLTIP.replace('%1', 'Z')
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_acceleration'] = function(block) {
- var _type_selected = block.getFieldValue('accel_type');
- Blockly.Python.definitions_['import_mpu6050'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import mpu6050\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
- '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '';
- Blockly.Python.definitions_['mpu6050_get_accel'] = '' +
- 'def mpu6050_get_accel(type):\n' +
- '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '\tif type == \'X\':\n' +
- '\t\treturn _MPU_ACCL_X\n' +
- '\tif type == \'Y\':\n' +
- '\t\treturn _MPU_ACCL_Y\n' +
- '\tif type == \'Z\':\n' +
- '\t\treturn _MPU_ACCL_Z\n' +
- '';
- // Blockly.Python.addVariable('_MPU_ACCL_' + _type_selected, '');
- var code = 'mpu6050_get_accel(\'' + _type_selected + '\')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_motion_tilt_angle'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_motion_get)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "tilt_angle_type")
- .appendField(Blockly.Msg.basic_motion_angle);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('tilt_angle_type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'X'),
- 'Y': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Y'),
- 'Z': Blockly.Msg.Esp32_Main_Controller_Motion_Tilt_Angle_TOOLTIP.replace('%1', 'Z')
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_motion_tilt_angle'] = function(block) {
- var _type_selected = block.getFieldValue('tilt_angle_type');
- Blockly.Python.definitions_['import_mpu6050'] = '' +
- 'from machine import I2C, Pin\n' +
- 'import math\n' +
- 'import mpu6050\n' +
- '_SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '';
- Blockly.Python.definitions_['mpu6050_standard_def'] = '' +
- '_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '';
- Blockly.Python.definitions_['mpu6050_get_angle'] = '' +
- 'def mpu6050_get_tilt_angle(type):\n' +
- '\t_MPU = mpu6050.accel(_SENSOR_MPU_PINS)\n' +
- '\t_MPU_SHAKING_OFFSET = _MPU.get_values()[\'AcZ\']\n' +
- '\t_MPU_ACCL_X = _MPU.get_values()[\'AcX\']\n' +
- '\t_MPU_ACCL_Y = _MPU.get_values()[\'AcY\']\n' +
- '\t_MPU_ACCL_Z = _MPU.get_values()[\'AcZ\']\n' +
- '\t_MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE(\'X\')\n' +
- '\t_MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE(\'Y\')\n' +
- '\t_MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE(\'Z\')\n' +
- '\tif type == \'X\':\n' +
- '\t\treturn _MPU_OFFSET_TILT_X\n' +
- '\tif type == \'Y\':\n' +
- '\t\treturn _MPU_OFFSET_TILT_Y\n' +
- '\tif type == \'Z\':\n' +
- '\t\treturn _MPU_OFFSET_TILT_Z\n' +
- '';
- // Blockly.Python.addVariable('_MPU_OFFSET_TILT_' + _type_selected, '');
- var code = 'mpu6050_get_tilt_angle(\'' + _type_selected + '\')';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- LIGHT & SOUND
- */
- Blockly.Blocks['esp32_main_controller_get_light'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/light_sensor.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.basic_light_get);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Light_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_light'] = function(block) {
- Blockly.Python.definitions_['light_import_def'] = '' +
- 'from machine import Pin, ADC\n' +
- '_PHOTONRESISTOR = machine.ADC(Pin(39))\n' +
- '_PHOTONRESISTOR.atten(ADC.ATTN_11DB)\n' +
- '_PHOTONRESISTOR.width(ADC.WIDTH_10BIT)\n' +
- '';
- Blockly.Python.addVariable('_PHOTONRESISTOR');
- var code = '_PHOTONRESISTOR.read()';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /* time */
- var ESP32_TIME_COLOR = "#fabe23";
- Blockly.Blocks['esp32_main_controller_time_timer_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "on_esp32"],
- [Blockly.Msg.time_ai_module, "on_ai"]
- ]), "type")
- .appendField(Blockly.Msg.time_init_timer);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Init_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_init'] = function(block) {
- var timer_type = block.getFieldValue('type');
- if (timer_type == "on_esp32") {
- Blockly.Python.definitions_['import_def'] = '' +
- 'import machine, time\n' +
- '_TIMER = machine.Timer(0)\n' +
- '';
- var code = '_TIMER.init()\n';
- } else if (timer_type == "on_ai") {
- Blockly.Python.definitions_['import_def'] = '' +
- 'import time\n' +
- 'from machine import Timer\n' +
- '\n' +
- 'def _timer_on_timer(timer):\n' +
- ' global _timer_current_time_elapsed\n' +
- ' _timer_current_time_elapsed = _timer_current_time_elapsed + 1\n' +
- '\n' +
- '_timer_current_time_elapsed = 0\n' +
- '_timer_tim = Timer(Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PERIODIC, period=1, callback=_timer_on_timer)\n' +
- '';
- var code = '_timer_tim.start()\n';
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_timer_get_current'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "on_esp32"],
- [Blockly.Msg.time_ai_module, "on_ai"]
- ]), "type")
- .appendField(Blockly.Msg.time_timer_get_time);
- this.setOutput(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Get_Current_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_get_current'] = function(block) {
- var timer_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (timer_type == "on_esp32") {
- var code = '_TIMER.value()';
- } else if (timer_type == "on_ai") {
- var code = '_timer_current_time_elapsed';
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_time_timer_clear'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "on_esp32"],
- [Blockly.Msg.time_ai_module, "on_ai"]
- ]), "type")
- .appendField(Blockly.Msg.time_timer_clear);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_timer_clear'] = function(block) {
- var timer_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (timer_type == "on_esp32") {
- var code = '_TIMER.deinit()\n';
- } else if (timer_type == "on_ai") {
- var code = '_timer_tim.stop()\n';
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_period_timer'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "on_esp32"],
- [Blockly.Msg.time_ai_module, "on_ai"]
- ]), "type")
- .appendField(Blockly.Msg.time_timer_periodically);
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_timer_every)
- .appendField(new Blockly.FieldTextInput("500"), "period_timer_count")
- .appendField(Blockly.Msg.time_timer_ms);
- this.appendStatementInput("exec_period_timer")
- .setCheck(null)
- .appendField(Blockly.Msg.time_execution);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_period_timer'] = function(block) {
- var timer_type = block.getFieldValue('type');
- var text_period_timer_count = block.getFieldValue('period_timer_count');
- var statements_name = Blockly.Python.statementToCode(block, 'exec_period_timer');
- if (timer_type == "on_esp32") {
- Blockly.Python.definitions_['import_def'] = '' +
- 'from machine import Timer\n' +
- '\n' +
- 'def _PRD_EXEC_tick(_):\n' +
- ' global _ControlTopic, _ReportTopic\n' +
- statements_name +
- '\n' +
- '_PRD_EXEC_TIMER = Timer(0)\n' +
- '_PRD_EXEC_TIMER.deinit()\n' +
- '_PRD_EXEC_TIMER.init(period=' + text_period_timer_count + ', mode=Timer.PERIODIC, callback=_PRD_EXEC_tick)\n' +
- '';
- } else if (timer_type == "on_ai") {
- Blockly.Python.definitions_['import_def'] = '' +
- 'import time\n' +
- 'from machine import Timer\n' +
- '\n' +
- 'def _on_timer(timer):\n' +
- statements_name +
- '\n' +
- 'from machine import Timer\n' +
- '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=' + text_period_timer_count + ', callback=_on_timer, arg=_on_timer)\n\n' +
- '_periodic_tim.start()\n' +
- '';
- }
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_time_period_timer_clear'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "on_esp32"],
- [Blockly.Msg.time_ai_module, "on_ai"]
- ]), "type")
- .appendField(Blockly.Msg.time_timer_periodically_clear);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_TIME_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'on_esp32': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module),
- 'on_ai': Blockly.Msg.Esp32_Main_Controller_Time_Period_Timer_Clear_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_time_period_timer_clear'] = function(block) {
- var timer_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (timer_type == "on_esp32") {
- var code = '_PRD_EXEC_TIMER.deinit()\n';
- } else if (timer_type == "on_ai") {
- var code = '_periodic_tim.stop()\n';
- }
- return code;
- };
- Blockly.Blocks['esp32_get_current_date'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.time_get_current_date_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_get_current_date_y, "0"],
- [Blockly.Msg.time_get_current_date_m, "1"],
- [Blockly.Msg.time_get_current_date_d, "2"],
- [Blockly.Msg.time_get_current_date_hour, "3"],
- [Blockly.Msg.time_get_current_date_min, "4"],
- [Blockly.Msg.time_get_current_date_sec, "5"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(ESP32_TIME_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '0': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_y),
- '1': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_m),
- '2': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_d),
- '3': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_hour),
- '4': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_min),
- '5': Blockly.Msg.Esp32_Get_Current_Date_TOOLTIP.replace('%1', Blockly.Msg.time_get_current_date_sec)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_get_current_date'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['get_date_current_ntp'] = '' +
- 'import time\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = 'time.localtime()[' + dropdown_type + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- LCD
- */
- var ESP32_SCREEN_COLOR = "#5bb2d6";
- Blockly.Blocks['esp32_main_controller_lcd_rotation'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_SETROTATION)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- }
- Blockly.Python['esp32_main_controller_lcd_rotation'] = function(block) {
- var degree = block.getFieldValue("DEGREE");
- var code = "tft.rotation(" + degree + ")\n";
- // Blockly.Python.addSetup("setRotation", code);
- return code;
- }
- Blockly.Blocks['esp32_main_controller_clear_screen'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_clear_screen);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_clear_screen'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'tft.fill(TFT.BLACK)\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_screen_fill'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_fill_screen);
- this.appendValueInput("COLOR")
- .setCheck(null)
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_lcd_screen_fill'] = function(block) {
- var value_lcd_rgb_value = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- if (value_lcd_rgb_value.charAt(0) != '#') {
- var code = 'tft.fill(_TFTColor' + value_lcd_rgb_value + ')\n';
- } else if (value_lcd_rgb_value.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) {}
- var code = "tft.fill(_TFTColor" + d + "," + e + "," + f + ")\n";
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawpixel'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_pixel.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_pixel);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POS")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COORDINATE);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawpixel'] = function(block) {
- // var drawrectcolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var drawrectcoord = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) {}
- code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + d + "," + e + "," + f + ")\n"
- } else if (color.charAt(0) != '#') {
- code = "tft.pixel((" + drawrectcoord + "), _TFTColor" + color + ")\n"
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawline'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.OLCD_LINE);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_LINE_START);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_LINE_END);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawline'] = function(block) {
- // var drawlinecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) {}
- code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + d + "," + e + "," + f + ")\n"
- } else if (color.charAt(0) != '#') {
- code = "tft.line((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
- }
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawlinelen'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.display_draw_vertical, "vline"],
- [Blockly.Msg.display_draw_horizontal, "hline"]
- ]), "rect_type")
- .appendField(Blockly.Msg.OLCD_LINE)
- this.appendValueInput("COLOR")
- .setCheck(null)
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POS")
- .setCheck(null)
- .appendField(Blockly.Msg.display_start_cord);
- this.appendValueInput("LENGTH")
- .setCheck(null)
- .appendField(Blockly.Msg.display_length);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawlinelen'] = function(block) {
- var _type = block.getFieldValue("rect_type");
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- var drawlinestart = Blockly.Python.valueToCode(block, 'POS', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'LENGTH', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = "tft." + _type + "((" + drawlinestart + "), " + drawlineend + ", _TFTColor" + color + ")\n"
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawrect'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.OLCD_FULL, "fillrect"],
- [Blockly.Msg.OLCD_CIRCLE, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.display_draw_rectangle);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COORDINATE);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_SIZE);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawrect'] = function(block) {
- // var drawlinecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var _type = block.getFieldValue("rect_type");
- var drawlinestart = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var drawlineend = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = "tft." + _type + "((" + drawlinestart + "), (" + drawlineend + "), _TFTColor" + color + ")\n"
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawcircle'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.OLCD_FULL, "fillcircle"],
- [Blockly.Msg.OLCD_STROKED, "circle"]
- ]), "circle_type")
- .appendField(Blockly.Msg.OLCD_CIRCLE);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_CENTER_POS);
- this.appendValueInput("RADIUS")
- .setCheck(null)
- .appendField(Blockly.Msg.OLCD_RADIUS);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawcircle'] = function(block) {
- // var drawcirclecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var circletype = block.getFieldValue('circle_type');
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var radius = Blockly.Python.valueToCode(block, 'RADIUS', Blockly.Python.ORDER_ATOMIC);
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = 'tft.' + circletype + '((' + position + '), ' + radius + ', _TFTColor' + color + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawqrcode'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_qr.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_drawqr_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_SET_POS);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.display_drawqr_content);
- this.appendValueInput("WIDTH")
- .setCheck("Number")
- .appendField(Blockly.Msg.display_drawqr_padding);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawqrcode'] = function(block) {
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
- var code = 'tft.drawqrcode(' + '(' + position + '), ' + content + ', ' + width + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_lcd_drawtext'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 42, 42, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/circle.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.OLCD_DRAW + Blockly.Msg.display_draw_text);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.OLCD_COLOR);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.display_start_cord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.display_draw_text_content);
- this.appendValueInput("SIZE")
- .setCheck("Number")
- .appendField(Blockly.Msg.display_draw_text_size);
- this.appendDummyInput()
- .appendField(Blockly.Msg.display_draw_text_return)
- .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_drawtext'] = function(block) {
- // var drawcirclecolor = sharpTo565(
- // Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC));
- var position = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'SIZE', Blockly.Python.ORDER_ATOMIC);
- var box = block.getFieldValue('TextCheckBox') == "TRUE" ? "True" : "False";
- var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
- code = 'tft.text((' + position + '), ' + content + ', _TFTColor' + color + ', sysfont, ' + size + ', nowrap=' + box + ')\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_rgb_value'] = {
- init: function() {
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField(Blockly.Msg.display_red);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField(Blockly.Msg.display_green);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField(Blockly.Msg.display_blue);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(ESP32_SCREEN_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_rgb_value'] = function(block) {
- var r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- var original_r = parseInt(r, 10);
- var original_g = parseInt(g, 10);
- var original_b = parseInt(b, 10);
- _r = original_r.toString(16);
- _g = original_g.toString(16);
- _b = original_b.toString(16);
- if (_r.length == 1)
- _r = "0" + _r;
- if (_g.length == 1)
- _g = "0" + _g;
- if (_b.length == 1)
- _b = "0" + _b;
- // TODO: Assemble Python into code variable.
- var code = '' + r + ',' + g + ',' + b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_main_controller_lcd_color_hex_to_rgb'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.COLOR)
- .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
- this.setOutput(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['esp32_main_controller_lcd_color_hex_to_rgb'] = function(a) {
- a = a.getFieldValue("COLOR");
- // var d = 0
- // , e = 0
- // , f = 0;
- // try {
- // 7 == a.length && (d = a.substring(1, 3),
- // e = a.substring(3, 5),
- // f = a.substring(5, 7));
- // }
- // catch (g) { }
- var gethex = a;
- function hexToRGB(h) {
- let r = 0,
- g = 0,
- b = 0;
- // 3 digits
- if (h.length == 4) {
- r = "0x" + h[1] + h[1];
- g = "0x" + h[2] + h[2];
- b = "0x" + h[3] + h[3];
- // 6 digits
- } else if (h.length == 7) {
- r = "0x" + h[1] + h[2];
- g = "0x" + h[3] + h[4];
- b = "0x" + h[5] + h[6];
- }
- return "" + +r + "," + +g + "," + +b + "";
- }
- var _code = hexToRGB(gethex);
- //_code += "tft.fill(TFT.TFTColor(FS_COLOR))\n";
- return [_code, Blockly.Python.ORDER_CONDITIONAL];
- };
- /*
- WiFi
- */
- var ESP32_WIFI_COLOR = "#3062c1";
- Blockly.Blocks['esp32_main_controller_wifi_enable_hotspot_mode'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/hotspot_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_setup_hotspot);
- this.appendValueInput("SSID")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_hotspot_ssid);
- this.appendValueInput("PASSWORD")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_hotspot_pass);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Enable_Hotspot_Mode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_enable_hotspot_mode'] = function(block) {
- var text_ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC);
- var text_password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC);
- var _code = '' +
- '_WIFI = network.WLAN(network.STA_IF)\n' +
- '_WIFI.active(False)\n' +
- '_WIFI = network.WLAN(network.AP_IF)\n' +
- '_WIFI.active(True)\n';
- Blockly.Python.definitions_['import_network'] = 'import network\n' +
- _code +
- '_WIFI.config(essid=' + text_ssid + ', password=' + text_password + ', authmode=2, channel=11, hidden=0)\n' +
- '\n';
- code = '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_wifi_connect_internet'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_connect_router);
- this.appendValueInput("ssid")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_router_ssid);
- this.appendValueInput("password")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_router_pass);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Connect_Internet_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_connect_internet'] = function(block) {
- var text_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
- var text_password = Blockly.Python.valueToCode(block, 'password', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['esp32_import_network'] = '' +
- 'import network\n' +
- 'if (network.WLAN(network.STA_IF).isconnected()) == False:\n' +
- ' _WIFI = network.WLAN(network.AP_IF)\n' +
- ' _WIFI.active(False)\n' +
- ' _WIFI = network.WLAN(network.STA_IF)\n' +
- ' _WIFI.active(True)\n' +
- ' _WIFI.connect(' + text_ssid + ', ' + text_password + ')\n' +
- 'else:\n' +
- ' _WIFI = network.WLAN(network.STA_IF)\n' +
- ' pass\n' +
- '\n';
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_wifi_disconnect'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_disconnect);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Disconnect_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_disconnect'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.active(False)\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_get_wifi_devices_number'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_hotspot_get_clients);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Devices_Number_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_wifi_devices_number'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'len(_WIFI.status("stations"))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_acquiring_equipment'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_hotspot_get_list);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Acquiring_Equipment_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_acquiring_equipment'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.status("stations")';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_get_wifi_connection_status'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_connection_status);
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Get_Wifi_Connection_Status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_get_wifi_connection_status'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.isconnected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_close'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_close_hotspot);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Close_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_close'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.active(False)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_get_info'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_router_get_info)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_router_device_ip, "0"],
- [Blockly.Msg.wifi_router_device_mask, "1"],
- [Blockly.Msg.wifi_router_device_gateway, "2"],
- [Blockly.Msg.wifi_router_device_dns, "3"]
- ]), "wifi_info");
- this.setOutput(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('wifi_info');
- var TOOLTIPS = {
- '0': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_ip),
- '1': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_mask),
- '2': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_gateway),
- '3': Blockly.Msg.Esp32_Main_Controller_Wifi_Get_Info_TOOLTIP.replace('%1', Blockly.Msg.wifi_router_device_dns)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_get_info'] = function(block) {
- var dropdown_name = block.getFieldValue('wifi_info');
- // TODO: Assemble Python into code variable.
- var code = '_WIFI.ifconfig()[' + dropdown_name + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['esp32_main_controller_wifi_ntptime'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ntptime_header.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_ntptime_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_ntptime_set_timezone)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_ntptime_utc8, "8"],
- [Blockly.Msg.wifi_ntptime_utc9, "9"],
- [Blockly.Msg.wifi_ntptime_utc10, "10"],
- [Blockly.Msg.wifi_ntptime_utc11, "11"],
- [Blockly.Msg.wifi_ntptime_utc12, "12"],
- [Blockly.Msg.wifi_ntptime_utcm12, "-12"],
- [Blockly.Msg.wifi_ntptime_utcm11, "-11"],
- [Blockly.Msg.wifi_ntptime_utcm10, "-10"],
- [Blockly.Msg.wifi_ntptime_utcm9, "-9"],
- [Blockly.Msg.wifi_ntptime_utcm8, "-8"],
- [Blockly.Msg.wifi_ntptime_utcm7, "-7"],
- [Blockly.Msg.wifi_ntptime_utcm6, "-6"],
- [Blockly.Msg.wifi_ntptime_utcm5, "-5"],
- [Blockly.Msg.wifi_ntptime_utcm4, "-4"],
- [Blockly.Msg.wifi_ntptime_utcm3, "-3"],
- [Blockly.Msg.wifi_ntptime_utcm2, "-2"],
- [Blockly.Msg.wifi_ntptime_utcm1, "-1"],
- [Blockly.Msg.wifi_ntptime_utc0, "0"],
- [Blockly.Msg.wifi_ntptime_utc1, "1"],
- [Blockly.Msg.wifi_ntptime_utc2, "2"],
- [Blockly.Msg.wifi_ntptime_utc3, "3"],
- [Blockly.Msg.wifi_ntptime_utc4, "4"],
- [Blockly.Msg.wifi_ntptime_utc5, "5"],
- [Blockly.Msg.wifi_ntptime_utc6, "6"],
- [Blockly.Msg.wifi_ntptime_utc7, "7"]
- ]), "timezone");
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_ntptime_time_server)
- .appendField(new Blockly.FieldTextInput("ntp.ntsc.ac.cn"), "website");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Wifi_Ntptime_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_wifi_ntptime'] = function(block) {
- var dropdown_timezone = block.getFieldValue('timezone');
- var text_website = block.getFieldValue('website');
- Blockly.Python.definitions_['esp32_main_controller_wifi_ntptime_def'] = '' +
- 'import network\n' +
- 'import ntptime\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = 'ntptime.settime(' + dropdown_timezone + ', "' + text_website + '")\n';
- return code;
- };
- /*
- IoT Service
- */
- Blockly.Blocks['esp32_main_controller_ThingSpeak_send_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 190, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_api_set)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_API_KEY"), "key");
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_thingspeak_send_data_1);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_IOT_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ThingSpeak_send_data'] = function(block) {
- var text_key = block.getFieldValue('key');
- var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- var code = '_THINGSPEAK_CHANNEL_API = "' + text_key + '"\n'
- Blockly.Python.addSetup("esp32_main_controller_ThingSpeak_send_data", code);
- var code = '_THINGSPEAK_DATA1 = ' + data + '\n';
- code += '_THINGSPEAK_ENDPOINT = "http://api.thingspeak.com/update?api_key=" + _THINGSPEAK_CHANNEL_API + "&field1=" + _THINGSPEAK_DATA1\n';
- code += '_THINGSPEAK_REQEUST = urequests.get(_THINGSPEAK_ENDPOINT)\n';
- return code;
- };
- Blockly.Blocks['CocoRobo_set_max'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_set_max)
- .appendField(new Blockly.FieldTextInput("5"), "number");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#183895");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['CocoRobo_set_max'] = function(block) {
- var number = block.getFieldValue('number');
- Blockly.Python.definitions_['import_socket'] = 'import usocket as socket';
- var code = '_S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n';
- code += '_S.bind((_WIFI.ifconfig()[0], 80))\n';
- code += '_S.listen(' + number + ')\n';
- Blockly.Python.addSetup("CocoRobo_set_max", code);
- var code = '_SERVER_CONN, _SERVER_ADDR = _S.accept()\n';
- code += '_SERVER_REQUEST = _CONN.recv(1024)\n';
- return code;
- };
- Blockly.Blocks['CocoRobo_get_source'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_server_set_source);
- this.setOutput(true, null);
- this.setColour("#183895");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['CocoRobo_get_source'] = function(block) {
- var code = 'str(_SERVER_ADDR)\n';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['CocoRobo_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_response_http_content_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_response_text, "text"],
- [Blockly.Msg.internet_response_content, "content"],
- [Blockly.Msg.internet_response_status, "state"],
- [Blockly.Msg.internet_response_json, "json"],
- [Blockly.Msg.internet_response_encode, "code"],
- [Blockly.Msg.internet_response_reason, "reason"]
- ]), "op");
- this.setOutput(true, null);
- this.setColour("#183895");
- // this.setTooltip(Blockly.Msg.CocoRobo_get_TOOLTIP);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('op');
- var TOOLTIPS = {
- 'text': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_text),
- 'content': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_content),
- 'state': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_status),
- 'json': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_json),
- 'code': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_encode),
- 'reason': Blockly.Msg.CocoRobo_get_TOOLTIP.replace('%1', Blockly.Msg.internet_response_reason)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['CocoRobo_get'] = function(block) {
- var op = block.getFieldValue('op');
- var code = '_SEND_HTTP_REQUEST.text';
- if (op == "text") {
- code = '_SEND_HTTP_REQUEST.text';
- } else if (op == "content") {
- code = '_SEND_HTTP_REQUEST.content';
- } else if (op == "state") {
- code = '_SEND_HTTP_REQUEST.status_code';
- } else if (op == "json") {
- code = '_SEND_HTTP_REQUEST.json()';
- } else if (op == "code") {
- code = '_SEND_HTTP_REQUEST.encoding';
- } else if (op == "reason") {
- code = '_SEND_HTTP_REQUEST.reason';
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['iot_system_run'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_RUN);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.system_code_path);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setTooltip(Blockly.Msg.Iot_System_Run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_run = function(block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _code = "exec(open(" + _path + ").read())\n";
- return _code;
- }
- Blockly.Blocks['iot_system_create'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_CREATE);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_CREATE_F);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setTooltip(Blockly.Msg.Iot_System_Create_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_create = function(block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _code = "def _CREATE_TEXT_FILE(_path):\n"
- _code += " f = open(_path, 'w')\n"
- _code += " f.close()\n"
- Blockly.Python.addFunction("_CREATE_TEXT_FILE", _code);
- _code = "_CREATE_TEXT_FILE(" + _path + ")\n";
- return _code;
- }
- Blockly.Blocks['iot_system_write'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendValueInput("WPATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_W);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('separator');
- var TOOLTIPS = {
- 'newline': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
- 'comma': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
- 'space': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
- 'semicolon': Blockly.Msg.Iot_System_Write_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python.iot_system_write = function(block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _wpath = Blockly.Python.valueToCode(block, 'WPATH', Blockly.Python.ORDER_ATOMIC);
- var _separator = block.getFieldValue('separator');
- var _code = "def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):\n"
- _code += " f = open(_path, 'a')\n"
- _code += " f.write(_data + _sep)\n"
- _code += " f.close()\n"
- Blockly.Python.addFunction("_CREATE_TEXT_FILE_WITH_CONTENT", _code);
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
- if (_separator == "newline") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", '\\r\\n')\n";
- } else if (_separator == "comma") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ',')\n";
- } else if (_separator == "space") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ' ')\n";
- } else if (_separator == "semicolon") {
- _code = "_CREATE_TEXT_FILE_WITH_CONTENT(" + _path + ", " + _wpath + ", ';')\n";
- }
- return _code;
- }
- Blockly.Blocks['iot_system_input'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_INPUT);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.SYSTEM_WRITE_F);
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM_WRITE_S)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SYSTEM_WRITE_S_N, "newline"],
- [Blockly.Msg.SYSTEM_WRITE_S_C, "comma"],
- [Blockly.Msg.SYSTEM_WRITE_S_S, "space"],
- [Blockly.Msg.SYSTEM_WRITE_S_SE, "semicolon"]
- ]), "separator");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(32);
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('separator');
- var TOOLTIPS = {
- 'newline': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_N),
- 'comma': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_C),
- 'space': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_S),
- 'semicolon': Blockly.Msg.Iot_System_Input_TOOLTIP.replace('%1', Blockly.Msg.SYSTEM_WRITE_S_SE)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python.iot_system_input = function(block) {
- var _path = Blockly.Python.valueToCode(block, 'PATH', Blockly.Python.ORDER_ATOMIC);
- var _separator = block.getFieldValue('separator');
- var _code = "def _GET_LIST_FROM_FILE(_path, _sep):\n"
- _code += " f = open(_path, 'r')\n"
- _code += " result = f.read().split(_sep)\n"
- _code += " f.close()\n"
- _code += " return result\n"
- Blockly.Python.addFunction("_GET_LIST_FROM_FILE", _code);
- _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
- if (_separator == "newline") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", '\\r\\n')";
- } else if (_separator == "comma") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ',')";
- } else if (_separator == "space") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ' ')";
- } else if (_separator == "semicolon") {
- _code = "_GET_LIST_FROM_FILE(" + _path + ", ';')";
- }
- return _code;
- }
- Blockly.Blocks['iot_system_restart'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.SYSTEM);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Restart_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_restart = function(block) {
- Blockly.Python.definitions_.import_machine = "import machine";
- var _code = "machine.reset()\n"
- return _code;
- }
- Blockly.Blocks['iot_system_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_size);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_get = function(block) {
- var code = "_MCU_FREESPACE"
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- }
- Blockly.Blocks['iot_system_mac'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_mac);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Mac_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.iot_system_mac = function(block) {
- Blockly.Python.definitions_.import_machine_ub = "import machine, ubinascii";
- var _code = "ubinascii.hexlify(machine.unique_id()).decode().upper()"
- return [_code, Blockly.Python.ORDER_CONDITIONAL];
- }
- Blockly.Blocks['iot_system_get_cocorobo_id'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.system_get_cocorobo_cid);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#22398e");
- this.setTooltip(Blockly.Msg.Iot_System_Get_Cocorobo_Id_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_system_get_cocorobo_id'] = function(block) {
- Blockly.Python.definitions_['import_cocorobo_cid'] = 'from cocorobo import cid\n';
- // TODO: Assemble Python into code variable.
- var code = 'cid._GET_MACHINE_ID().strip(\'\\n\')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _____ _____ __ _
- \_ \___/__ \ / _\ ___ _ ____ _(_) ___ ___
- / /\/ _ \ / /\/ \ \ / _ \ '__\ \ / / |/ __/ _ \
- /\/ /_| (_) / / _\ \ __/ | \ V /| | (_| __/
- \____/ \___/\/ \__/\___|_| \_/ |_|\___\___|
-
- */
- var ESP32_IOT_COLOR = "#3c7683";
- var IFTTT_BLOCK_COLOR = "#ff3b00";
- var ONENET_BLOCK_COLOR = "#00b5ff";
- Blockly.Blocks['esp32_main_controller_onenet_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 130, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_server)
- .appendField(new Blockly.FieldTextInput("183.230.40.39"), "server_ip");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_productid)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_ID"), "product_id");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_productkey)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_PRODUCT_KEY"), "product_api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_deviceid)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_DEVICE_ID"), "device_id");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_setup'] = function(block) {
- var text_server_ip = block.getFieldValue('server_ip');
- var text_product_id = block.getFieldValue('product_id');
- var text_product_api_key = block.getFieldValue('product_api_key');
- var text_device_id = block.getFieldValue('device_id');
- // TODO: Assemble Python into code variable.
- var code = '...\n';
- return code;
- };
- Blockly.Blocks['iot_service_onenet'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/onenet_setup_logo.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_send);
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_onenet_create_with_item']));
- this.setColour(ONENET_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip("");
- this.setHelpUrl("");
- },
- mutationToDom: function() {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function(xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function(workspace) {
- var containerBlock = workspace.newBlock('iot_service_onenet_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_onenet_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function() {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_onenet_mqtt_send_item)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_onenet_create_with_container'] = {
- init: function() {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_onenet_create_with_item'] = {
- init: function() {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_onenet'] = function(block) {
- var code = "";
- // onenet_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['esp32_main_controller_onenet_when_receive_msg'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive);
- this.appendValueInput("content")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_when);
- this.appendStatementInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_receive_exec);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_when_receive_msg'] = function(block) {
- var value_content = Blockly.Python.valueToCode(block, 'content', Blockly.Python.ORDER_ATOMIC);
- var statements_exec = Blockly.Python.statementToCode(block, 'exec');
- // TODO: Assemble Python into code variable.
- var code = '...\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_onenet_disconnect'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_onenet_mqtt_disconnect);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_onenet_disconnect'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_ifttt_send_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_webhook)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "ifttt_api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_send_eventname)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "ifttt_event");
- this.appendValueInput("ifttt_data_1")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_1);
- this.appendValueInput("ifttt_data_2")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_2);
- this.appendValueInput("ifttt_data_3")
- .setCheck(null)
- .appendField(Blockly.Msg.iotservice_ifttt_send_3);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IFTTT_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Send_Data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ifttt_send_data'] = function(block) {
- var text_ifttt_api_key = block.getFieldValue('ifttt_api_key');
- var text_ifttt_event = block.getFieldValue('ifttt_event');
- var value_ifttt_data_1 = Blockly.Python.valueToCode(block, 'ifttt_data_1', Blockly.Python.ORDER_ATOMIC);
- var value_ifttt_data_2 = Blockly.Python.valueToCode(block, 'ifttt_data_2', Blockly.Python.ORDER_ATOMIC);
- var value_ifttt_data_3 = Blockly.Python.valueToCode(block, 'ifttt_data_3', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- var code = '' +
- '_IFTTT_POST_API_KEY = "' + text_ifttt_api_key + '"\n' +
- '_IFTTT_POST_EVENT_NAME = "' + text_ifttt_event + '"\n' +
- '_IFTTT_POST_ENDPOINT = "http://maker.ifttt.com/trigger/"+ _IFTTT_POST_EVENT_NAME + "/with/key/" + _IFTTT_POST_API_KEY\n' +
- '_IFTTT_POST_DATA = \'{"value1":"\'+ str(' + value_ifttt_data_1 + ') +\'","value2":"\'+ str(' + value_ifttt_data_2 + ') +\'","value3":"\'+ str(' + value_ifttt_data_3 + ') +\'"}\'\n' +
- '_IFTTT_POST_REQUEST = urequests.post(_IFTTT_POST_ENDPOINT, data = _IFTTT_POST_DATA , headers = { "Content-type": "application/json" }, timeout=60)\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_ifttt_touched'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ifttt_webhook.png", 110, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_webhook)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_API_KEY"), "key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_ifttt_trigger_eventname)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_NAME"), "timename");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IFTTT_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Ifttt_Touched_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_ifttt_touched'] = function(block) {
- var text_ifttt_api_key = block.getFieldValue('key');
- var text_timename = block.getFieldValue('timename');
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- var code = '_IFTTT_TRIGGER_EVENT_NAME = "' + text_timename + '"\n'
- code += '_IFTTT_TRIGGER_API_KEY = "' + text_ifttt_api_key + '"\n'
- code += '_IFTTT_TRIGGER_ENDPOINT = "https://maker.ifttt.com/trigger/" + _IFTTT_TRIGGER_EVENT_NAME + "/with/key/" + _IFTTT_TRIGGER_API_KEY\n'
- Blockly.Python.addSetup("esp32_main_controller_ifttt_touched", code);
- var code = '_IFTTT_GET_REQUEST = urequests.get(_IFTTT_TRIGGER_ENDPOINT, timeout=60)\n'
- return code;
- };
- IOT_THINGSPEAK_COLOR = "#1b379f";
- // IOT_THINGSPEAK_COLOR = ESP32_IOT_COLOR;
- Blockly.Blocks['iot_service_thingspeak'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_send_api)
- .appendField(new Blockly.FieldTextInput("ENTET_YOUT_CHANNEL_WRITE_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_thingspeak_create_with_item']));
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function() {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function(xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function(workspace) {
- var containerBlock = workspace.newBlock('iot_service_thingspeak_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_thingspeak_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function() {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField("Field " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("field" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_thingspeak_create_with_container'] = {
- init: function() {
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_thingspeak_create_with_item'] = {
- init: function() {
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_thingspeak'] = function(block) {
- var first_input = Blockly.Python.valueToCode(block, 'first_input', Blockly.Python.ORDER_ATOMIC);
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_THINGSPEAK_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + api;
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = "field" + (n + 1);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
- Blockly.Python.ORDER_NONE) || '';
- thingspeak_url += '&' + item_field + '=\"+str(' + item_value + ')+\"';
- }
- var code = '' +
- '_THINGSPEAK_ENDPOINT = "' + thingspeak_url + '"\n' +
- '_THINGSPEAK_REQUEST = urequests.get(_THINGSPEAK_ENDPOINT,timeout=60)\n' +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_thingspeak_read'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/thingspeak.png", 170, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_query_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_query_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_READ_API_KEY"), "api_key");
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_channel_id)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_CHANNEL_ID"), "id");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read'] = function(block) {
- var text_api_key = block.getFieldValue('api_key');
- var text_id = block.getFieldValue('id');
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- Blockly.Python.addVariable('_THINGSPEAK_READ_REQUEST', '', true);
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST = urequests.get("https://api.thingspeak.com/channels/" + "' + text_id + '" + "/feeds.json?api_key=" + "' + text_api_key + '",timeout=60)\n';
- return code;
- };
- Blockly.Blocks['iot_service_thingspeak_read_total'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_read_total);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Total_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read_total'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST.json()[\'channel\'][\'last_entry_id\']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_thingspeak_read_specific'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeak_get_title);
- this.appendValueInput("entry")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeaK_get_field);
- this.appendValueInput("field")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_thingspeaK_get_value);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(IOT_THINGSPEAK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Thingspeak_Read_Specific_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_thingspeak_read_specific'] = function(block) {
- var value_entry = Blockly.Python.valueToCode(block, 'entry', Blockly.Python.ORDER_ATOMIC);
- var value_field = Blockly.Python.valueToCode(block, 'field', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_THINGSPEAK_READ_REQUEST.json()[\'feeds\'][' + value_entry + '][\'field' + value_field + '\']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- // cococloud
- var COCOCLOUD_BLOCK_COLOR = "#4085de";
- Blockly.Blocks['iot_service_cococloud'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
- .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_cococloud_create_with_item']));
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function() {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function(xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function(workspace) {
- var containerBlock = workspace.newBlock('iot_service_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function(containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function() {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_cococloud_create_with_container'] = {
- init: function() {
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_cococloud_create_with_item'] = {
- init: function() {
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_cococloud'] = function(block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var cococloud_data = "'{";
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var cococloud_data_final = cococloud_data + "}'";
- var code = '' +
- '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
- '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
- 'try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)\n' +
- ' print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '';
- // cococloud_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['iot_service_cococloud_read'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_api)
- .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "api");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_cococloud_read'] = function(block) {
- var api = block.getFieldValue('api');
- Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
- return code;
- };
- Blockly.Blocks['iot_service_cococloud_read_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_property);
- this.appendValueInput("property")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_data);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Iot_Service_Cococloud_Read_Data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_cococloud_read_data'] = function(block) {
- var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- __ _ _ __ _
- /\ \ \___| |___ _____ _ __| | __ /__\ ___ __ _ _ _ ___ ___| |_
- / \/ / _ \ __\ \ /\ / / _ \| '__| |/ / / \/// _ \/ _` | | | |/ _ \/ __| __|
- / /\ / __/ |_ \ V V / (_) | | | < / _ \ __/ (_| | |_| | __/\__ \ |_
- \_\ \/ \___|\__| \_/\_/ \___/|_| |_|\_\ \/ \_/\___|\__, |\__,_|\___||___/\__|
- |_|
- */
- var NETWORK_BLOCK_COLOR = "#16318a";
- Blockly.Blocks['esp32_network_http_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_header_get.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_title);
- this.appendValueInput("http_get_url")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_get_url);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_get'] = function(block) {
- var url = Blockly.Python.valueToCode(block, 'http_get_url', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_SEND_HTTP_GET_ENDPOINT', '', true);
- Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests as requests\n' +
- 'import ujson as json\n' +
- '\n' +
- '_SEND_HTTP_GET_ENDPOINT = ' + url + '\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '_SEND_HTTP_REQUEST = requests.get(_SEND_HTTP_GET_ENDPOINT)\n';
- return code;
- };
- Blockly.Blocks['esp32_network_http_post'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_header_post.png", 180, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_post_title);
- this.appendValueInput("http_post_url")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_post_url);
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_http_post_json);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Post_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_post'] = function(block) {
- var url = Blockly.Python.valueToCode(block, 'http_post_url', Blockly.Python.ORDER_ATOMIC);
- var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('_SEND_HTTP_POST_ENDPOINT', '', true);
- Blockly.Python.addVariable('_SEND_HTTP_REQUEST', '', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests as requests\n' +
- 'import ujson as json\n' +
- '\n' +
- '_SEND_HTTP_POST_ENDPOINT = ' + url + '\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '' +
- '_SEND_HTTP_POST_DATA = \'' + data + '\'\n' +
- '_SEND_HTTP_REQUEST = requests.post(_SEND_HTTP_POST_ENDPOINT, data = _SEND_HTTP_POST_DATA , headers = { "Content-type": "application/json" })\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_get_data_from_local_server'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_server_data_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_http_get_server_data_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_http_get_server_data_json, "json"],
- [Blockly.Msg.internet_http_get_server_data_text, "text"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'json': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_json),
- 'text': Blockly.Msg.Esp32_Network_Http_Get_Data_From_Local_Server_TOOLTIP.replace('%1', Blockly.Msg.internet_http_get_server_data_text)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_get_data_from_local_server'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "text") {
- var request_code = "_SEND_HTTP_REQUEST.text";
- } else if (dropdown_type == "json") {
- var request_code = "eval(_SEND_HTTP_REQUEST.text)";
- }
- var code = request_code;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_network_http_server_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/http_server_header.png", 130, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Network_Http_Server_Setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_setup'] = function(block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['esp32_network_http_server_setup_def_setup'] = '' +
- 'from microWebSrv import MicroWebSrv\n' +
- 'import time\n' +
- '';
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_route'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_path)
- .appendField(new Blockly.FieldTextInput("/"), "path")
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_being_requested);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_property)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_route_setting_get, "get"],
- [Blockly.Msg.wifi_web_http_server_route_setting_post, "post"]
- ]), "response_type");
- this.appendStatementInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_web_http_server_route_setting_do);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- // this.setTooltip("");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('response_type');
- var TOOLTIPS = {
- 'get': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_get),
- 'post': Blockly.Msg.Esp32_Network_Http_Server_Route_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_route_setting_post)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_network_http_server_route'] = function(block) {
- var dropdown_response_type = block.getFieldValue('response_type');
- var text_path = block.getFieldValue('path');
- var statements_exec = Blockly.Python.statementToCode(block, 'exec');
- var response_type = "";
- var response_writeok = "";
- if (dropdown_response_type == "get") {
- response_type = "";
- } else if (dropdown_response_type == "post") {
- response_type = ", 'POST'";
- }
- /*Blockly.Python.addVariable('', '' +
- '' +
- '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
- 'def _httpHandlerTestGet(httpClient, httpResponse):\n' +
- statements_exec +
- '', true);*/
- Blockly.Python.definitions_['esp32_network_http_server_route_def_' + text_path.substring(1)] = '' +
- '@MicroWebSrv.route(\'' + text_path + '\'' + response_type + ')\n' +
- 'def _httpHandler' + text_path.substring(1) + 'Get(httpClient, httpResponse):\n' +
- statements_exec +
- '\n';
- Blockly.Python.addSetup("server_route_setup", '' +
- 'def _acceptWebSocketCallback(webSocket, httpClient) :\n' +
- ' print("WS ACCEPT")\n' +
- ' webSocket.RecvTextCallback = _recvTextCallback\n' +
- ' webSocket.RecvBinaryCallback = _recvBinaryCallback\n' +
- ' webSocket.ClosedCallback = _closedCallback\n' +
- '\n' +
- 'def _recvTextCallback(webSocket, msg) :\n' +
- ' print("WS RECV TEXT : %s" % msg)\n' +
- ' webSocket.SendText("Reply for %s" % msg)\n' +
- '\n' +
- 'def _recvBinaryCallback(webSocket, data) :\n' +
- ' print("WS RECV DATA : %s" % data)\n' +
- '\n' +
- 'def _closedCallback(webSocket) :\n' +
- ' print("WS CLOSED")\n' +
- '\n' +
- 'try:\n' +
- ' srv = MicroWebSrv()\n' +
- ' srv.Stop()\n' +
- ' srv.MaxWebSocketRecvLen = 1024\n' +
- ' srv.WebSocketThreaded = False\n' +
- ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
- ' srv.Start(threaded=True)\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' srv = MicroWebSrv()\n' +
- ' srv.MaxWebSocketRecvLen = 1024\n' +
- ' srv.WebSocketThreaded = False\n' +
- ' srv.AcceptWebSocketCallback = _acceptWebSocketCallback\n' +
- ' srv.Start(threaded=True)\n' +
- '');
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_route_respond'] = {
- init: function() {
- this.appendValueInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.wifi_web_http_server_respond_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_respond_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_respond_text, "text/plain"],
- [Blockly.Msg.wifi_web_http_server_respond_json, "application/json"],
- [Blockly.Msg.wifi_web_http_server_respond_html, "text/html"]
- ]), "response_type");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('response_type');
- var TOOLTIPS = {
- 'text/plain': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_text),
- 'application/json': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_json),
- 'text/html': Blockly.Msg.Esp32_Network_Http_Server_Route_Respond_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_respond_html)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_route_respond'] = function(block) {
- var dropdown_response_type = block.getFieldValue('response_type');
- var statements_exec = Blockly.Python.valueToCode(block, 'exec', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'content = ' + statements_exec + '\n' +
- 'httpResponse.WriteResponseOk( headers = None,\n' +
- ' contentType = "' + dropdown_response_type + '",\n' +
- ' contentCharset = "UTF-8",\n' +
- ' content = str(content) )\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_network_http_server_get_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.wifi_web_http_server_get_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.wifi_web_http_server_get_as_json, "json"],
- [Blockly.Msg.wifi_web_http_server_get_as_text, "text"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(NETWORK_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'json': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_json),
- 'text': Blockly.Msg.Esp32_Network_Http_Server_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.wifi_web_http_server_get_as_text)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_network_http_server_get_data'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- if (dropdown_type == "json") {
- var as_type = "ReadRequestContentAsJSON()";
- } else if (dropdown_type == "text") {
- var as_type = "ReadRequestContent(size=None)";
- }
- // TODO: Assemble Python into code variable.
- var code = 'httpClient.' + as_type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- var WS_BLOCK_COLOR = "#83178a";
- Blockly.Blocks['esp32_web_ws_setup_send'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ws_header_send.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_send_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_send_ip)
- .appendField(new Blockly.FieldTextInput("192.168.4.1"), "ip");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Send_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_setup_send'] = function(block) {
- var text_ip = block.getFieldValue('ip');
- Blockly.Python.definitions_['ws_setup_send'] = '' +
- 'import uwebsockets.client\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_client_websocket = uwebsockets.client.connect("ws://' + text_ip + ':80")\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_send'] = {
- init: function() {
- this.appendValueInput("command")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_ws_send_msg);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Send_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_send'] = function(block) {
- var value_command = Blockly.Python.valueToCode(block, 'command', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'message = ' + value_command + '\n' +
- '_client_websocket.send(message)\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_setup_receive'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ws_header_receive.png", 220, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_recv);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Setup_Receive_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_setup_receive'] = function(block) {
- Blockly.Python.definitions_['ws_setup_receive'] = '' +
- 'import uasyncio, logging\n' +
- 'from uasyncio.websocket.server import WSReader, WSWriter\n' +
- 'logging.basicConfig(level=logging.DEBUG)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_receive_statement'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_receiving);
- this.appendStatementInput("command")
- .setCheck(null)
- .appendField(Blockly.Msg.internet_ws_setup_recv_exec);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(WS_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Web_Ws_Receive_Statement_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_receive_statement'] = function(block) {
- var statements_command = Blockly.Python.statementToCode(block, 'command');
- Blockly.Python.definitions_['ws_setup_receive_statement'] = '' +
- 'def _ws_exec(msg):\n' +
- '' + statements_command +
- '\n' +
- 'def _ws_echo(reader, writer):\n' +
- ' yield from reader.readline()\n' +
- '\n' +
- ' reader = yield from WSReader(reader, writer)\n' +
- ' writer = WSWriter(reader, writer)\n' +
- '\n' +
- ' while 1:\n' +
- ' _ws_l = yield from reader.read(1024)\n' +
- ' print(_ws_l)\n' +
- ' _ws_exec(_ws_l)\n' +
- '\n' +
- ' if _ws_l == b"\\r":\n' +
- ' await writer.awrite(b"\\r\\n")\n' +
- ' else:\n' +
- ' await writer.awrite(_ws_l)\n' +
- '\n' +
- '_ws_loop = uasyncio.get_event_loop()\n' +
- '_ws_loop.create_task(uasyncio.start_server(_ws_echo, "0.0.0.0", 80))\n' +
- '_ws_loop.run_forever()\n' +
- '_ws_loop.close()\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_web_ws_receive_get_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_get_msg);
- this.appendDummyInput()
- .appendField(Blockly.Msg.internet_ws_setup_get_msg_type)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.internet_ws_setup_get_msg_text, "text"],
- [Blockly.Msg.internet_ws_setup_get_msg_json, "json"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(WS_BLOCK_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'text': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_text),
- 'json': Blockly.Msg.Esp32_Web_Ws_Receive_Get_Data_TOOLTIP.replace('%1', Blockly.Msg.internet_ws_setup_get_msg_json)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_web_ws_receive_get_data'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "text") {
- var code = "msg.decode(\"utf-8\")";
- } else if (dropdown_type == "json") {
- var code = "eval(msg.decode(\"utf-8\"))";
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- ____ _ _____
- | __ )| | | ____|
- | _ \| | | _|
- | |_) | |___| |___
- |____/|_____|_____|
- */
- var BLE_BLOCK_COLOR = "#629978";
- Blockly.Blocks['esp32_ble_setup_sender'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_sender_msg);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_sender);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_setup_sender_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_setup_sender'] = function(block) {
- Blockly.Python.definitions_['esp32_ble_setup_sender_def'] = '' +
- 'import ble_simple_central\n' +
- 'from ble_simple_central import BLESimpleCentral\n' +
- 'import bluetooth, time\n' +
- '\n' +
- 'ble = bluetooth.BLE()\n' +
- 'central = BLESimpleCentral(ble)\n' +
- 'ble_read = \'\'\n' +
- '\n' +
- 'not_found = False\n' +
- '\n' +
- 'def ble_on_rx(v):\n' +
- ' global ble_read\n' +
- ' ble_read = v.decode("utf-8")\n' +
- '\n' +
- 'def ble_on_scan(addr_type, addr, name):\n' +
- ' if addr_type is not None:\n' +
- ' print("Found peripheral:", addr_type, addr, name)\n' +
- ' central.connect()\n' +
- ' else:\n' +
- ' global not_found\n' +
- ' not_found = True\n' +
- ' print("No peripheral found.")\n' +
- '\n' +
- 'def ble_scan_and_connect():\n' +
- ' central.scan(callback=ble_on_scan)\n' +
- ' # Wait for connection...\n' +
- ' print("Connecting...")\n' +
- ' failed = 0\n' +
- '\n' +
- ' while not central.is_connected():\n' +
- ' time.sleep_ms(100)\n' +
- ' if not_found: return\n' +
- '\n' +
- ' print("Connected")\n' +
- '\n' +
- 'central.on_notify(ble_on_rx)\n' +
- 'with_response = False\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = 'ble_scan_and_connect()\n';
- return code;
- };
- Blockly.Blocks['esp32_ble_send_data'] = {
- init: function() {
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.esp32_ble_send_data_msg);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_send_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_send_data'] = function(block) {
- var value_data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'central_write_value = str(' + value_data + ')\n' +
- 'central.write(central_write_value, with_response)\n' +
- 'time.sleep_ms(400 if with_response else 30)\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_ble_setup_receiver'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_ble_setup.png", 100, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_receiver_msg);
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_setup_receiver);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_setup_receiver_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_setup_receiver'] = function(block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['esp32_ble_setup_receiver_def'] = '' +
- 'import ble_simple_peripheral\n' +
- 'from ble_simple_peripheral import BLESimplePeripheral\n' +
- 'import bluetooth, time\n' +
- '\n' +
- 'ble = bluetooth.BLE()\n' +
- 'ble_p = BLESimplePeripheral(ble)\n' +
- 'ble_read = \'\'\n' +
- '\n' +
- 'def ble_on_rx(v):\n' +
- ' global ble_read\n' +
- ' ble_read = v.decode("utf-8")\n' +
- '\n' +
- '';
- var code = 'ble_p.on_write(ble_on_rx)\n';
- return code;
- };
- Blockly.Blocks['esp32_ble_get_data'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_get_data_msg);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_get_data_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_get_data'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'ble_read';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_ble_sender_connected'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_sender_connected_msg);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_sender_connected_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_sender_connected'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'central.is_connected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_ble_receiver_connected'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.esp32_ble_receiver_connected_msg);
- this.setOutput(true, null);
- this.setColour(BLE_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_ble_receiver_connected_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_ble_receiver_connected'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'ble_p.is_connected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
|