1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228 |
- /*
- _ ___ ____ _
- / \ |_ _| | __ ) __ _ ___(_) ___
- / _ \ | | | _ \ / _` / __| |/ __|
- / ___ \ | | | |_) | (_| \__ \ | (__
- /_/ \_\___| |____/ \__,_|___/_|\___|
- */
- Blockly.Blocks['ai_k210_button_read_pressed'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_button_when)
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type")
- .appendField(Blockly.Msg.ai_basic_pressed)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('button_type');
- var TOOLTIPS = {
- 'a': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
- 'c': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_button_read_pressed'] = function(block) {
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- var _type = block.getFieldValue('button_type');
- var code = '_gp_side_' + _type + '.value() == 1';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['ai_k210_button_read_released'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_button_when)
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type_released")
- .appendField(Blockly.Msg.ai_basic_released)
- this.setOutput(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('button_type_released');
- var TOOLTIPS = {
- 'a': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "B"),
- 'c': Blockly.Msg.ai_k210_button_read_released_TOOLTIP.replace('%1', "C")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_button_read_released'] = function(block) {
- Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- '\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
- '\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
- '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
- '\n';
- var _type = block.getFieldValue('button_type_released');
- var code = '_gp_side_' + _type + '.value() == 0';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['ai_k210_led_light_up'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_blue, "blue"],
- [Blockly.Msg.ai_basic_led_red, "red"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_on);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'blue': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
- 'red': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_led_light_up'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['k210_led_light_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- '\n' +
- 'fm.register(31, fm.fpioa.GPIO3)\n' +
- 'fm.register(32, fm.fpioa.GPIO4)\n' +
- '\n' +
- '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
- '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_led_' + dropdown_type + '.value(1)\n';
- return code;
- };
- Blockly.Blocks['ai_k210_led_light_off'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_blue, "blue"],
- [Blockly.Msg.ai_basic_led_red, "red"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_off);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'blue': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
- 'red': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_k210_led_light_off'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['k210_led_light_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- '\n' +
- 'fm.register(31, fm.fpioa.GPIO3)\n' +
- 'fm.register(32, fm.fpioa.GPIO4)\n' +
- '\n' +
- '_led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n' +
- '_led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_led_' + dropdown_type + '.value(0)\n';
- return code;
- };
- Blockly.Blocks['ai_basic_light_up_led_analog'] = {
- init: function() {
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_basic_led_analog_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_basic_led_analog_led1, "1"],
- [Blockly.Msg.ai_basic_led_analog_led2, "2"]
- ]), "type")
- .appendField(Blockly.Msg.ai_basic_led_analog_brightness_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_basic_led_analog_range_text);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '1': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led1),
- '2': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led2)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_basic_light_up_led_analog'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['ai_basic_light_up_led_analog_def'] = '' +
- 'from machine import Timer,PWM\n' +
- 'import time\n' +
- '\n' +
- 'led_left_tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM)\n' +
- 'led_right_tim = Timer(Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM)\n' +
- 'led_left_ctrl = PWM(led_left_tim, freq=500000, duty=50, pin=31)\n' +
- 'led_right_ctrl = PWM(led_right_tim, freq=500000, duty=50, pin=32)\n' +
- '\n' +
- 'def analog_light(which, brightness):\n' +
- ' if which == 1:\n' +
- ' led_left_ctrl.duty(brightness)\n' +
- ' if which == 2:\n' +
- ' led_right_ctrl.duty(brightness)\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = 'analog_light(' + dropdown_type + ',' + value_brightness + ')\n';
- return code;
- };
- /*
- _ ___ ___ ____
- / \ |_ _| |_ _|_ __ ___ __ _ __ _ ___ | _ \ _ __ ___ ___ ___ ___ ___
- / _ \ | | | || '_ ` _ \ / _` |/ _` |/ _ \ | |_) | '__/ _ \ / __/ _ \/ __/ __|
- / ___ \ | | | || | | | | | (_| | (_| | __/ | __/| | | (_) | (_| __/\__ \__ \
- /_/ \_\___| |___|_| |_| |_|\__,_|\__, |\___| |_| |_| \___/ \___\___||___/___/
- |___/
- */
- Blockly.Blocks["ai_lcd_screeninit"] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/screen_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_init);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_screeninit_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_screeninit = function(block) {
- var checkbox_name = block.getFieldValue('DEGREE');
- //var dropdown_name = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
- Blockly.Python.definitions_.import_lcd = "import lcd";
- Blockly.Python.definitions_.import_image = "import image";
- Blockly.Python.definitions_['import_define_x_ai_screeninit'] = '' +
- 'try:from cocorobo import display_cjk_string\n' +
- 'except:pass\n' +
- '\n' +
- 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n' +
- ' try: \n' +
- ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n' +
- ' return canvas\n'+
- ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n' +
- '\n';
- var _code = "lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n";
- _code += "lcd.rotation(1)\n";
- _code += "lcd.clear(lcd.BLACK)\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_rotation'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_lcd_rotation)
- .appendField(new Blockly.FieldDropdown([
- ["0°", "1"],
- ["90°", "2"],
- ["180°", "3"],
- ["270°", "0"]
- ]), "DEGREE")
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_rotation_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_rotation = function(block) {
- var dropdown_name = block.getFieldValue('DEGREE');
- var _code = "lcd.rotation(" + dropdown_name + ")\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_filp'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_lcd_mirror_flip)
- .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_filp_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_filp = function(block) {
- var _tf = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
- var _code = "lcd.mirror(" + _tf + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_screen'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_filled_screen_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_set_filled_screen_color_red, "RED"],
- [Blockly.Msg.image_process_set_filled_screen_color_green, "GREEN"],
- [Blockly.Msg.image_process_set_filled_screen_color_blue, "BLUE"],
- [Blockly.Msg.image_process_set_filled_screen_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_set_filled_screen_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_set_filled_screen_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_set_filled_screen_color_yellow, "YELLOW"],
- [Blockly.Msg.image_process_set_filled_screen_color_black, "BLACK"],
- [Blockly.Msg.image_process_set_filled_screen_color_white, "WHITE"]
- ]), "COLOR");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('COLOR');
- var TOOLTIPS = {
- 'RED': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_red),
- 'GREEN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_green),
- 'BLUE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_blue),
- 'PURPLE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_purple),
- 'CYAN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_cyan),
- 'ORANGE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_orange),
- 'YELLOW': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_yellow),
- 'BLACK': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_black),
- 'WHITE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_white)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_screen = function(block) {
- var colour_name = block.getFieldValue('COLOR');
- var _code = "lcd.clear(lcd." + colour_name + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_fill_screen_with_rgb'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_set_filled_screen_color);
- this.appendValueInput("rgb_value")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_fill_screen_with_rgb_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_fill_screen_with_rgb'] = function(block) {
- var value_name = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'lcd.clear(' + value_name + ')\n';
- return code;
- };
- Blockly.Blocks['ai_lcd_clear'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_clear_lcd);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_clear_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_clear = function(block) {
- var _code = "lcd.clear(lcd.BLACK)\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_rgb_value_input'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_r);
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_g);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_b);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_rgb_value_input_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_rgb_value_input'] = function(block) {
- var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks.ai_lcd_XY = {
- init: function() {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setTooltip(Blockly.Msg.ai_lcd_XY_TOOLTIP);
- this.setColour("#5bb2d6");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.image_process_xy_x);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.image_process_xy_y)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_XY = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks.ai_lcd_WH = {
- init: function() {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setTooltip(Blockly.Msg.ai_lcd_WH_TOOLTIP);
- this.setColour("#5bb2d6");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.image_process_wh_width);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.image_process_wh_height)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_WH = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_lcd_colortext'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
- [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
- [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
- [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
- [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
- [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
- ]), "COLOR");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lcd_show_text_bg_color)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
- [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
- [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
- [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
- [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
- [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
- [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
- [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
- [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
- ]), "BCOLOR");
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- /* this.appendDummyInput()
- .appendField(Blockly.Msg.LCDDT); */
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python['ai_lcd_colortext'] = function(block) {
- var color = block.getFieldValue("COLOR");
- var bcolor = block.getFieldValue("BCOLOR");
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = "lcd.draw_string(" + xy + ", " + content + ", lcd." + color + ", lcd." + bcolor + ")\n"; //, scale=" + size + "
- // return [_code, Blockly.Python.ORDER_ATOMIC]
- return _code;
- };
- Blockly.Blocks["ai_lcd_createnonecarvas"] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_blank_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_size);
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.LCDDCC_F)
- // .appendField(new Blockly.FieldCheckbox("TRUE"), "check")
- // .appendField(Blockly.Msg.LCDDCC_I)
- // .appendField(new Blockly.FieldTextInput(""), "text");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_createnonecarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_createnonecarvas = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + " = image.Image(size=(" + value_wh + "))\n";
- return _code;
- }
- Blockly.Blocks["ai_lcd_fillcanvas"] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_let_canvas_filled_with_color);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_fillcanvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_fillcanvas = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + '.draw_rectangle(0,0, ' + variable_name + '.width(),' + variable_name + '.height(), color=' + value_color + ', thickness=1, fill=True)\n';
- return _code;
- }
- Blockly.Blocks["ai_lcd_createcarvas"] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_image_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_create_image_canvas_path)
- .appendField(new Blockly.FieldTextInput("/sd/preset/images/splash_bg.jpg"), "text");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_createcarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_createcarvas = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var checkbox_name = block.getFieldValue('check') == 'TRUE' ? "True" : "False";
- var text_name = block.getFieldValue('text');
- var _code = variable_name + " = image.Image(\"" + text_name + "\")\n";
- return _code;
- }
- Blockly.Blocks['ai_lcd_showcarvas_set_cord'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_set_canvas_setup)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord_as);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_cord_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas_set_cord = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = "_" + variable_name + "_x, _" + variable_name + "_y = " + value_name + "\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_showcarvas_set_display'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_show_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_display_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas_set_display = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.definitions_.ai_lcd_display_def = "" +
- "_" + variable_name + "_x, _" + variable_name + "_y = 0, 0" +
- "";
- // TODO: Assemble Python into code variable.
- var code = "lcd.display(" + variable_name + ", oft=(_" + variable_name + "_x,_" + variable_name + "_y))\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_showcarvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_show_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem");
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5cb2d6");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_lcd_showcarvas = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = "lcd.display(" + variable_name + ", oft=(" + value_name + "))\n";
- return code;
- };
- Blockly.Blocks['ai_lcd_clearcanvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_clear_canvas_1)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_clear_canvas_2);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_clearcanvas_TOOLTIP);
- this.setHelpUrl('');
- }
- }
- Blockly.Python.ai_lcd_clearcanvas = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".clear()\n";
- return _code;;
- }
- Blockly.Blocks['ai_lcd_textcarvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_draw_text_font_size);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_draw_text_if_monospace)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "bool");
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_textcarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_textcarvas = function(block) {
- 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) {}
- var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=(" + d + "," + e + "," + f + "), scale=" + size + ", mono_space=" + bool + ")\n";
- } else 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) {}
- var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=" + color + ", scale=" + size + ", mono_space=" + bool + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_characterscarvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_hanzi_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_characters);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("CONTENT")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_content);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_draw_text_font_size + ":")
- // .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.SMALL, "1"],
- [Blockly.Msg.MIDDLE, "2"],
- [Blockly.Msg.LARGE, "3"]
- ]), "fonts");
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.Set_Auto_Wrap + ":")
- // // .setCheck(null)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.LED_SCROLL_YES, "True"],
- // [Blockly.Msg.LED_SCROLL_NO, "False"]
- // ]), "wraps");
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_characterscarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_characterscarvas = function(block) {
- 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) {}
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = block.getFieldValue('fonts');;
- // var wraps = block.getFieldValue('wraps');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=(" + d + "," + e + "," + f + "))\n";
- } else 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) {}
- // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
- var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = block.getFieldValue('fonts');;
- // var wraps = block.getFieldValue('wraps');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", font_size=" + size + ", color=" + color + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_linecarvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_line);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_end_coord);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_linecarvas_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_linecarvas = function(block) {
- 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) {}
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=(" + d + "," + e + "," + f + "), thickness=" + size + ")\n";
- } else 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) {}
- var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = variable_name + ".draw_line(" + xy + ", " + xy2 + ", color=" + color + ", thickness=" + size + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_ractanglecarvas'] = {
- init: function() {
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_text_filled, "fillrect"],
- [Blockly.Msg.image_process_text_stroked, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.image_process_draw_rectangle_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("POSB")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_size);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('rect_type');
- var TOOLTIPS = {
- 'fillrect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
- 'rect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_ractanglecarvas = function(block) {
- 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) {}
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=(" + d + "," + e + "," + f + "), thickness=" + th + ", fill=" + type + ")\n";
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_rectangle(" + xy + ", " + size + ", color=" + color + ", thickness=" + th + ", fill=" + type + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_drawcirclecarvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(
- // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_text_on_draw)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_text_filled, "fillrect"],
- [Blockly.Msg.image_process_text_stroked, "rect"]
- ]), "rect_type")
- .appendField(Blockly.Msg.image_process_draw_circle_text);
- this.appendValueInput("POSA")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_draw_circle_text_start);
- this.appendValueInput("Scale")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_radius);
- this.appendValueInput("COLOR")
- .setCheck("String")
- .appendField(Blockly.Msg.image_process_text_color);
- this.appendValueInput("Scale_0")
- .setCheck("Number")
- .appendField(Blockly.Msg.image_process_text_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5bb2d6");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('rect_type');
- var TOOLTIPS = {
- 'fillrect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
- 'rect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_lcd_drawcirclecarvas = function(block) {
- 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) {}
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=(" + d + ", " + e + ", " + f + "), thickness=" + th0 + ", fill=" + type + ")\n";
- } else 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) {}
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('rect_type') == "fillrect" ? "True" : "False";
- var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
- var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
- var _code = variable_name + ".draw_circle(" + xy + ", " + th + ", color=" + color + ", thickness=" + th0 + ", fill=" + type + ")\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_lcd_draw_cross_sign'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_cross.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_cross_title);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_centerpos);
- this.appendValueInput("size")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_length);
- this.appendValueInput("thickness")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_thick);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_cross_color);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_cross_sign_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_cross_sign'] = function(block) {
- var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- 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) {}
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_cross(' + value_xy + ', color=(' + d + ', ' + e + ', ' + f + '), size=' + value_size + ', thickness=' + value_thickness + ')\n';
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_cross(' + value_xy + ', color=' + color + ', size=' + value_size + ', thickness=' + value_thickness + ')\n';
- }
- return code;
- };
- Blockly.Blocks['ai_lcd_draw_arrow_sign'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_arrow.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_arrow_title);
- this.appendValueInput("xy1")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_start);
- this.appendValueInput("xy2")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_end);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_color);
- this.appendValueInput("thickness")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_arrow_thick);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_arrow_sign_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_arrow_sign'] = function(block) {
- // TODO: Assemble Python into code variable.
- var color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- if (color.charAt(0) == '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
- var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- 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) {}
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + d + ', ' + e + ', ' + f + '), thickness=' + value_thickness + ')\n';
- } else if (color.charAt(0) != '#') {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy1 = Blockly.Python.valueToCode(block, 'xy1', Blockly.Python.ORDER_ATOMIC);
- var value_xy2 = Blockly.Python.valueToCode(block, 'xy2', Blockly.Python.ORDER_ATOMIC);
- var value_thickness = Blockly.Python.valueToCode(block, 'thickness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.draw_arrow(' + value_xy1 + ',' + value_xy2 + ', color=(' + color + '), thickness=' + value_thickness + ')\n';
- }
- return code;
- };
- Blockly.Blocks['ai_lcd_draw_image_on_canvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_image.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_on_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_draw_image_title);
- this.appendValueInput("image_path")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_title_image_path);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_start_coord);
- this.appendValueInput("scale_x")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_scale_x);
- this.appendValueInput("scale_y")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_draw_image_scale_y);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5bb2d6");
- this.setTooltip(Blockly.Msg.ai_lcd_draw_image_on_canvas_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_draw_image_on_canvas'] = function(block) {
- // TODO: Assemble Python into code variable.
- // nav_up = image.Image("/sd/preset/images/arrow_up_filled.jpg")
- // splash.draw_image(nav_up, 5, 5)
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_image_path = Blockly.Python.valueToCode(block, 'image_path', Blockly.Python.ORDER_ATOMIC);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_scale_x = Blockly.Python.valueToCode(block, 'scale_x', Blockly.Python.ORDER_ATOMIC);
- var value_scale_y = Blockly.Python.valueToCode(block, 'scale_y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' +
- variable_name + '.draw_image(image.Image(' + value_image_path + '), ' + value_xy + ', x_scale=' + value_scale_x + ', y_scale=' + value_scale_y + ' )' +
- '\n';
- return code;
- };
- Blockly.Blocks['ai_img_save'] = {
- init: function() {
- this.appendValueInput("save_path")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_img_save);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_img_save_quality)
- .appendField(new Blockly.FieldNumber(90, 0, 100), "compression")
- .appendField("%");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_img_save_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_img_save'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
- var compression_rate = block.getFieldValue('compression');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.save(' + save_path_location + ', quality=' + compression_rate + ')\n';
- return code;
- };
- Blockly.Blocks['ai_lcd_invert_color'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_color_invert);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_invert_color'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.invert()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_resizecanvas'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_resize_to);
- this.appendValueInput("wh")
- .setCheck(null);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_resizecanvas_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_resizecanvas'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.resize(' + wh + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_color_scheme'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_color_scheme_convert)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_color_scheme_bw, "to_grayscale(1)"],
- [Blockly.Msg.image_process_color_scheme_rgb, "to_rgb565(1)"]
- ]), "color_scheme");
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('color_scheme');
- var TOOLTIPS = {
- 'to_grayscale(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_bw),
- 'to_rgb565(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_rgb)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_color_scheme'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_vari = block.getFieldValue('color_scheme');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_vari;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_lcd_ai_pix'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_pixtovec_text_first)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_pixtovec_v2p, ".ai_to_pix()"],
- [Blockly.Msg.image_process_pixtovec_p2v, ".pix_to_ai()"]
- ]), "ai_pix_type")
- .appendField(Blockly.Msg.image_process_pixtovec_conversion);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#f0983e");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_ai_pix'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_vari = block.getFieldValue('ai_pix_type');
- // TODO: Assemble Python into code variable.
- var code = 'a = ' + variable_name + dropdown_vari + '\n';
- // TODO: Change ORDER_NONE to the correct strength.
- return code;
- };
- Blockly.Blocks['ai_lcd_img_cut'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_text_let_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_imgcut_cutting);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_imgcut_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_imgcut_size);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_lcd_img_cut_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_lcd_img_cut'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- // TODO: Change ORDER_NONE to the correct strength.
- var code = variable_name + '.cut(' + xy + ',' + wh + ')';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_statistics'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_get_statistics_to_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.image_process_get_statistics_get_analysis);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_size);
- this.appendValueInput("bins")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_get_statistics_boxes);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_statistics_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_statistics'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- var bins = Blockly.Python.valueToCode(block, 'bins', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_statistics(bins=' + bins + ',roi=(' + xy + ',' + wh + '))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_get)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_histogram_compute);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_region);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_get_histogram_region_xy);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_get_histogram_region_wh);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_histogram_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_histogram'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_histogram(roi=(' + value_xy + ',' + value_wh + '))';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram_percentile'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_details_1)
- .appendField(new Blockly.FieldVariable("hist"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_histogram_details_get);
- this.appendValueInput("perc")
- .setCheck(null);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_histogram_percentile_TOOLTIP);
- this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
- }
- };
- Blockly.Python['ai_vision_get_histogram_percentile'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_perc = Blockly.Python.valueToCode(block, 'perc', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.get_percentile(' + value_perc + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_histogram_percentile_detail'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_1)
- .appendField(new Blockly.FieldVariable("percentile"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_get_histogram_percentile_3, "value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_4, "l_value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_5, "a_value()"],
- [Blockly.Msg.ai_vision_get_histogram_percentile_6, "b_value()"]
- ]), "option");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('option');
- var TOOLTIPS = {
- 'value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_3),
- 'l_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_4),
- 'a_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_5),
- 'b_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_6)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
- }
- };
- Blockly.Python['ai_vision_get_histogram_percentile_detail'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_option = block.getFieldValue('option');
- var code = variable_name + '.' + dropdown_option;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_lab_rgb_conversion'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_lab_rgb_conversion_exec)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_lab_rgb_conversion_torgb, "lab_to_rgb"],
- [Blockly.Msg.image_process_lab_rgb_conversion_tolab, "rgb_to_lab"]
- ]), "rgblab");
- this.appendValueInput("color_tuple")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_lab_rgb_conversion_tuple);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('rgblab');
- var TOOLTIPS = {
- 'lab_to_rgb': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_torgb),
- 'rgb_to_lab': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_tolab)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_lab_rgb_conversion'] = function(block) {
- var type = block.getFieldValue('rgblab');
- var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'image.' + type + '(' + tuple + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_rgb_grayscale_conversion'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_exec)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.image_process_rgb_grayscale_conversion_torgb, "grayscale_to_rgb"],
- [Blockly.Msg.image_process_rgb_grayscale_conversion_togray, "rgb_to_grayscale"]
- ]), "rgb_g");
- this.appendValueInput("color_tuple")
- .setCheck(null)
- .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_set);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('rgb_g');
- var TOOLTIPS = {
- 'grayscale_to_rgb': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_torgb),
- 'rgb_to_grayscale': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_togray)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_rgb_grayscale_conversion'] = function(block) {
- var type = block.getFieldValue('rgb_g');
- var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addVariable('rgb_tuple', "", true);
- // TODO: Assemble Python into code variable.
- var code = 'image.' + type + '(' + tuple + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ ____
- / \ |_ _| / ___|__ _ _ __ ___ ___ _ __ __ _
- / _ \ | | | | / _` | '_ ` _ \ / _ \ '__/ _` |
- / ___ \ | | | |__| (_| | | | | | | __/ | | (_| |
- /_/ \_\___| \____\__,_|_| |_| |_|\___|_| \__,_|
- */
- Blockly.Blocks.ai_camera_wh = {
- init: function() {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#60c1bb");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.W);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.H)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_camera_wh = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_camera_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/camera_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_init);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_set_size)
- .appendField(new Blockly.FieldDropdown([
- ["QVGA (320*240)", "QVGA"],
- ["QQVGA (160*120)", "QQVGA"]
- ]), "DEGREE");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_color_mode)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_camera_color_mode_rgb, "RGB565"],
- [Blockly.Msg.ai_camera_camera_color_mode_bw, "GRAYSCALE"]
- ]), "DEGREE1");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_init_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_init = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var degee1 = block.getFieldValue('DEGREE1');
- var checkbox = block.getFieldValue('checkbox') == 'TRUE';
- var w = block.getFieldValue('w');
- var h = block.getFieldValue('h');
- var degee2 = block.getFieldValue('DEGREE2') || 1;
- var degee3 = block.getFieldValue('DEGREE3') || 1;
- var degee4 = block.getFieldValue('DEGREE4') || 1;
- var degee5 = block.getFieldValue('DEGREE5') || 1;
- var input1 = block.getFieldValue('input1') || 1;
- var input2 = block.getFieldValue('input2') || 1;
- var input3 = block.getFieldValue('input3') || 1;
- Blockly.Python.definitions_.import_sensor = "import sensor";
- var _code = "sensor.reset()\n";
- _code += "sensor.set_pixformat(sensor." + degee1 + ")\n";
- _code += "sensor.set_framesize(sensor." + degee + ")\n";
- _code += "sensor.set_vflip(0)\n";
- _code += "sensor.set_hmirror(0)\n";
- _code += "sensor.skip_frames(30)\n";
- _code += "sensor.run(1)\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_windows'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_windowing_resize);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_windowing_w)
- .appendField(new Blockly.FieldNumber(224), "window_w")
- .appendField(Blockly.Msg.ai_camera_windowing_h)
- .appendField(new Blockly.FieldNumber(224), "window_h")
- .appendField(Blockly.Msg.ai_camera_windowing_unit);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_windows_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_windows'] = function(block) {
- var w = block.getFieldValue('window_w');
- var h = block.getFieldValue('window_h');
- // var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
- var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
- return code;
- };
- Blockly.Blocks['ai_camera_fit_to_screen_size'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_fit_set);
- this.appendDummyInput()
- .appendField(new Blockly.FieldVariable("camera"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_camera_fit_to)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_camera_fit_1, "240"],
- [Blockly.Msg.ai_camera_camera_fit_2, "224"]
- ]), "size");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_fit_to_screen_size_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_fit_to_screen_size'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_size = block.getFieldValue('size');
- // TODO: Assemble Python into code variable.
- if (dropdown_size == "240") {
- var code = 'camera = sensor.snapshot()\n' +
- variable_name + ' = ' + variable_name + '.cut(40,0,240,240)\n';
- } else if (dropdown_size == "224") {
- var code = 'camera = sensor.snapshot()\n' +
- variable_name + ' = ' + variable_name + '.cut(48,8,224,224)\n' +
- 'a = ' + variable_name + '.pix_to_ai()\n' +
- '';
- }
- return code;
- };
- Blockly.Blocks['ai_camera_snapshot'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_snapshot);
- this.setOutput(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_snapshot_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_snapshot = function(block) {
- Blockly.Python.definitions_.import_sensor = "import sensor";
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.snapshot()";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_camera_lens_corr'] = {
- init: function() {
- this.appendValueInput("lens_corr")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_camera_set_lens_corr);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_camera_lens_corr_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_camera_lens_corr'] = function(block) {
- var value_lens_corr = Blockly.Python.valueToCode(block, 'lens_corr', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_lens_corr + '.lens_corr(strength = 1.8, zoom = 1.0)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_camera_image_property_change'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_bcs_set_text_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_bcs_vflip, "set_vflip"],
- [Blockly.Msg.ai_camera_bcs_hmirror, "set_hmirror"],
- [Blockly.Msg.ai_camera_bcs_wb, "set_auto_whitebal"],
- [Blockly.Msg.ai_camera_bcs_g, "set_auto_gain"],
- [Blockly.Msg.ai_camera_bcs_e, "set_auto_exposure"]
- ]), "image_property")
- .appendField(Blockly.Msg.ai_camera_bcs_as_2)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable + Blockly.Msg.ai_camera_bcs_status, "True"],
- [Blockly.Msg.ai_camera_disable + Blockly.Msg.ai_camera_bcs_status, "False"]
- ]), "DEGREE");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('image_property');
- var TOOLTIPS = {
- 'set_vflip': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_vflip),
- 'set_hmirror': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_hmirror),
- 'set_auto_whitebal': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_wb),
- 'set_auto_gain': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_g),
- 'set_auto_exposure': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_e)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_image_property_change = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var dropdown_image_property = block.getFieldValue('image_property');
- var _code = "sensor." + dropdown_image_property + "(" + degee + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_vflip'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_vflip)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_vflip = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_vflip(" + degee + ")\n";
- return _code;
- }
- Blockly.Blocks['ai_camera_hmirror'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_hmirror)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_hmirror = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_hmirror(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_whitebal'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_whitebal)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_whitebal = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_whitebal(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_gain'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_gain)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_gain = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_gain(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_auto_exposure'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_auto_exposure)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_enable, "True"],
- [Blockly.Msg.ai_camera_disable, "False"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_auto_exposure = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "sensor.set_auto_exposure(" + degee + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_bcs_operation'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_bcs_set_text)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_camera_bcs_contrast, "contrast"],
- [Blockly.Msg.ai_camera_bcs_brightness, "brightness"],
- [Blockly.Msg.ai_camera_bcs_saturation, "saturation"]
- ]), "image_property")
- .appendField(Blockly.Msg.ai_camera_bcs_as);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('image_property');
- var TOOLTIPS = {
- 'contrast': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_contrast),
- 'brightness': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_brightness),
- 'saturation': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_saturation)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_bcs_operation = function(block) {
- var dropdown_image_property = block.getFieldValue('image_property');
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_" + dropdown_image_property + "(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_contrast'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_contrast_to_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_contrast = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_contrast(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_brightness'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_brightness_to_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_brightness = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_brightness(" + _x + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_camera_saturation'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_saturation_value);
- this.appendValueInput("A").setCheck("Number");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_camera_set_range);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(!0);
- this.setColour("#60c1bb");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_camera_saturation = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _code = "sensor.set_saturation(" + _x + ")\n"
- return _code;
- }
- /*
- _ ___ __ ___ _
- / \ |_ _| \ \ / (_)___(_) ___ _ __
- / _ \ | | \ \ / /| / __| |/ _ \| '_ \
- / ___ \ | | \ V / | \__ \ | (_) | | | |
- /_/ \_\___| \_/ |_|___/_|\___/|_| |_|
-
- */
- Blockly.Blocks.ai_lcd_XY_vision = {
- init: function() {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#f0983e");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.X);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.Y)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_XY_vision = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks.ai_lcd_WH_vision = {
- init: function() {
- var _input = this.appendDummyInput();
- this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
- this.setColour("#f0983e");
- this.setOutput(!0, "Boolean");
- _input.appendField(Blockly.Msg.W);
- this.appendValueInput("A").setCheck("Number");
- _input = this.appendDummyInput();
- _input.appendField(Blockly.Msg.H)
- this.appendValueInput("B").setCheck("Number");
- this.setInputsInline(!0);
- }
- };
- Blockly.Python.ai_lcd_WH_vision = function(block) {
- var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
- var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
- var code = "" + _x + ", " + _y + "";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_vision_color_analyze'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_analyze_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_color_analyze_get_result);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_color_analyze_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_color_analyze_size);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_color_analyze_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_color_analyze'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addFunction("function", '' +
- 'def _get_regional_color_analyze_rgb(x, y, w, h):\n' +
- ' _crd_bounding_box_size = (w, h)\n' +
- ' _crd_r = [x, y, _crd_bounding_box_size[0], _crd_bounding_box_size[1]] # 50x50 center of QQVGA.\n' +
- ' _crd_hist = ' + variable_name + '.get_statistics(bins=8,roi=_crd_r)\n' +
- ' return image.lab_to_rgb((_crd_hist.l_mean(),_crd_hist.a_mean(),_crd_hist.b_mean()))\n' +
- '');
- // TODO: Assemble Python into code variable.
- var code = '_get_regional_color_analyze_rgb(' + xy + ', ' + wh + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_blobs'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_blobs_color_region);
- this.appendValueInput("color_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_color_threshold);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_size);
- /*this.appendValueInput("area_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_region_threshold)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_area");
- this.appendValueInput("pixels_threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_pixel_threshold)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_pixels");
- this.appendValueInput("margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_blobs_margin)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "margin");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_find_blobs_filtered_region)
- .appendField(new Blockly.FieldCheckbox("FALSE"), "check_merge");*/
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_blobs_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_blobs'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_color_threshold = Blockly.Python.valueToCode(block, 'color_threshold', Blockly.Python.ORDER_ATOMIC);
- var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- /* var value_area_threshold = Blockly.Python.valueToCode(block, 'area_threshold', Blockly.Python.ORDER_ATOMIC);
- var value_pixels_threshold = Blockly.Python.valueToCode(block, 'pixels_threshold', Blockly.Python.ORDER_ATOMIC);
- var checkbox_check_area = block.getFieldValue('check_area') == 'TRUE';
- var checkbox_check_pixels = block.getFieldValue('check_pixels') == 'TRUE';
- var checkbox_check_merge = block.getFieldValue('check_merge') == 'TRUE';
- var value_margin = Blockly.Python.valueToCode(block, 'margin', Blockly.Python.ORDER_ATOMIC);
- var checkbox_margin = block.getFieldValue('margin') == 'TRUE';
- var area = checkbox_check_area;
- var pixels = checkbox_check_pixels;
- var margin = checkbox_margin;
- var merge = checkbox_check_merge.toString();
- var merge_state = merge.charAt(0).toUpperCase() + merge.slice(1);
- if (margin == false) {
- if (area == false && pixels == false) {
- var addtional_parameters = ' ';
- } else if (area == true && pixels == false) {
- var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold;
- } else if (area == false && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold;
- } else if (area == true && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold;
- }
- } else if (margin == true) {
- if (area == false && pixels == false) {
- var addtional_parameters = ', margin=' + value_margin;
- } else if (area == true && pixels == false) {
- var addtional_parameters = ', ' + 'area_threshold=' + value_area_threshold + ', margin=' + value_margin;
- } else if (area == false && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', margin=' + value_margin;
- } else if (area == true && pixels == true) {
- var addtional_parameters = ', ' + 'pixels_threshold=' + value_area_threshold + ', area_threshold=' + value_area_threshold + ', margin=' + value_margin;
- }
- }
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=' + merge_state + addtional_parameters + ')';*/
- var code = variable_name + '.find_blobs(' + value_color_threshold + ', roi=(' + value_xy + ',' + value_wh + '), merge=False, area_threshold=150)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_blobs'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_get_blobs_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_detection_box_x, "x()"],
- [Blockly.Msg.ai_vision_detection_box_y, "y()"],
- [Blockly.Msg.ai_vision_detection_box_width, "w()"],
- [Blockly.Msg.ai_vision_detection_box_height, "h()"],
- [Blockly.Msg.ai_vision_detection_box_pixels, "pixels()"],
- [Blockly.Msg.ai_vision_detection_box_cx, "cx()"],
- [Blockly.Msg.ai_vision_detection_box_cy, "cy()"],
- [Blockly.Msg.ai_vision_detection_box_degree, "rotation()"],
- [Blockly.Msg.ai_vision_detection_box_total, "count()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_blobs_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_blobs'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_threshold_default'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_detection_color_threshold_set)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_detection_color_threshold_set_black, "black"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_white, "white"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_red, "red"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_green, "green"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_blue, "blue"],
- [Blockly.Msg.ai_vision_detection_color_threshold_set_orange, "orange"]
- ]), "color");
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('color');
- var TOOLTIPS = {
- 'black': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_black),
- 'white': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_white),
- 'red': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_red),
- 'green': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_green),
- 'blue': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_blue),
- 'orange': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_orange)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_get_threshold_default'] = function(block) {
- var dropdown_color = block.getFieldValue('color');
- // TODO: Assemble Python into code variable.
- if (dropdown_color == "black") {
- var code = '[(0,40)]';
- } else if (dropdown_color == "white") {
- var code = '[(128,255)]';
- } else if (dropdown_color == "red") {
- var code = '[(45, 65, 40, 80, 40, 60)]';
- } else if (dropdown_color == "green") {
- var code = '[(45, 65, -50, -30, 0, 40)]';
- } else if (dropdown_color == "blue") {
- var code = '[(45, 65, -20, 30, -60, -20)]';
- } else if (dropdown_color == "orange") {
- var code = '[(77, 55, 19, 61, 14, 108)]';
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_qrcode'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_qrcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_qrcode_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_qrcode'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_qrcodes()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_qrcode_get_info'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_qrcode_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type, "data_type"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'data_type': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
- 'x': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_qrcode_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_qrcode_get_info'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_apriltag'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_apriltag_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_apriltag_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_apriltag_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_apriltag'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_apriltags()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_apriltag_get_info'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_apriltag_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type_id, "id"],
- [Blockly.Msg.ai_vision_pattern_detection_type_family, "family"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'id': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_id),
- 'family': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_family),
- 'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_apriltag_get_info'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_datamatrices'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_datamatrix_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_datamatrices_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_datamatrices_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_datamatrices'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_datamatrices()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_datamatrices_get_info'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_data_matrices_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_rows, "rows"],
- [Blockly.Msg.ai_vision_pattern_detection_cols, "columns"],
- [Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count, "capacity"],
- [Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count, "padding"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'rows': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_rows),
- 'columns': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_cols),
- 'capacity': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count),
- 'padding': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count),
- 'x': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_datamatrices_get_info'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_barcodes'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_barcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_get_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_barcode_detected_result);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_barcodes_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_find_barcodes'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_barcodes()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_barcodes_get_info'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_barcode_matrices_get_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
- [Blockly.Msg.ai_vision_pattern_detection_type, "type"],
- [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
- [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
- [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
- [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
- ]), "get_info");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('get_info');
- var TOOLTIPS = {
- 'payload': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
- 'type': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type),
- 'x': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
- 'y': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
- 'w': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
- 'h': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_vision_barcodes_get_info'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_name = block.getFieldValue('get_info');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + dropdown_name + '()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_circles'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_circle.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_circle);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_region_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_hoffman_threshold);
- this.appendValueInput("x_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_x);
- this.appendValueInput("y_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_y);
- this.appendValueInput("r_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_merge_r);
- this.appendValueInput("r_min")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_min_radius);
- this.appendValueInput("r_max")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_max_radius);
- this.appendValueInput("r_step")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_circle_step_radius);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_circles_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_circles'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- /*var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- var x_margin = Blockly.Python.valueToCode(block, 'x_margin', Blockly.Python.ORDER_ATOMIC);
- var y_margin = Blockly.Python.valueToCode(block, 'y_margin', Blockly.Python.ORDER_ATOMIC);
- var r_margin = Blockly.Python.valueToCode(block, 'r_margin', Blockly.Python.ORDER_ATOMIC);
- var r_min = Blockly.Python.valueToCode(block, 'r_min', Blockly.Python.ORDER_ATOMIC);
- var r_max = Blockly.Python.valueToCode(block, 'r_max', Blockly.Python.ORDER_ATOMIC);
- var r_step = Blockly.Python.valueToCode(block, 'r_step', Blockly.Python.ORDER_ATOMIC);*/
- // TODO: Assemble Python into code variable.
- // var code = variable_name + '.find_circles(roi=(' + xy + ',' + wh + '), threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 2, r_max = 100, r_step = 2)';
- var code = variable_name + '.find_circles(roi=(' + xy + ',' + wh + '), threshold = 2500, x_margin = 10, y_margin = 10, r_margin = 10, r_min = 10, r_max = 100, r_step = 2)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_circles'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_circle_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_circle_detected_x, "x()"],
- [Blockly.Msg.ai_vision_find_circle_detected_y, "y()"],
- [Blockly.Msg.ai_vision_find_circle_detected_radius, "r()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_circles_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_circles'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- var code = variable_name + '.' + type;
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_rects'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_rect.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_rect);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_region_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_rect_bounding_threshold);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_rects_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_rects'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_rects(roi=(' + xy + ',' + wh + '), threshold = 10000)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_rects'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_rect_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_rect_detected_x, "x()"],
- [Blockly.Msg.ai_vision_find_rect_detected_y, "y()"],
- [Blockly.Msg.ai_vision_find_rect_detected_w, "w()"],
- [Blockly.Msg.ai_vision_find_rect_detected_h, "h()"],
- [Blockly.Msg.ai_vision_find_rect_detected_tuple, "rect()"],
- [Blockly.Msg.ai_vision_find_rect_detected_corners, "corners()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_rects_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_rects'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_find_lines'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_line.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_line);
- this.appendValueInput("xy")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_start_coord);
- this.appendValueInput("wh")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_size);
- /*this.appendValueInput("threshold")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_hoffman_threshold);
- this.appendValueInput("theta_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_merge_theta);
- this.appendValueInput("rho_margin")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_vision_find_line_merge_rho);*/
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_find_lines_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_find_lines'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- //var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
- //var theta_margin = Blockly.Python.valueToCode(block, 'theta_margin', Blockly.Python.ORDER_ATOMIC);
- //var rho_margin = Blockly.Python.valueToCode(block, 'rho_margin', Blockly.Python.ORDER_ATOMIC);
- var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
- var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.find_lines(roi=(' + xy + ',' + wh + '), threshold = 1000, theta_margin = 25, rho_margin = 25)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_vision_get_lines'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_vision_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_vision_find_line_detected_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_vision_find_line_detected_start_x, "x1()"],
- [Blockly.Msg.ai_vision_find_line_detected_start_y, "y1()"],
- [Blockly.Msg.ai_vision_find_line_detected_end_x, "x2()"],
- [Blockly.Msg.ai_vision_find_line_detected_end_y, "y2()"],
- [Blockly.Msg.ai_vision_find_line_detected_length, "length()"],
- [Blockly.Msg.ai_vision_find_line_detected_angle, "theta()"]
- ]), "blob_type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#f0983e");
- this.setTooltip(Blockly.Msg.ai_vision_get_lines_TOOLTIP);
- this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
- }
- };
- Blockly.Python['ai_vision_get_lines'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var type = block.getFieldValue('blob_type');
- // TODO: Assemble Python into code variable.
- var code = variable_name + '.' + type;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ __ __ _ _
- / \ |_ _| | \/ (_) ___ _ __ ___ _ __ | |__ ___ _ __ ___
- / _ \ | | | |\/| | |/ __| '__/ _ \| '_ \| '_ \ / _ \| '_ \ / _ \
- / ___ \ | | | | | | | (__| | | (_) | |_) | | | | (_) | | | | __/
- /_/ \_\___| |_| |_|_|\___|_| \___/| .__/|_| |_|\___/|_| |_|\___|
- |_|
- */
- Blockly.Blocks['ai_mphone_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_init);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.ai_mphone_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_mphone_init = function(block) {
- Blockly.Python.definitions_.import_all_mphone = "" +
- "from Maix import GPIO, I2S, FFT\n" +
- "from board import board_info\n" +
- "from fpioa_manager import fm\n" +
- "\n" +
- "sample_rate = 38640\n" +
- "sample_points = 1024\n" +
- "fft_points = 512\n" +
- "hist_x_num = 50\n" +
- "\n" +
- "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
- "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
- "\n" +
- "_recorder_rx = I2S(I2S.DEVICE_0)\n" +
- "_recorder_rx.channel_config(_recorder_rx.CHANNEL_0, _recorder_rx.RECEIVER, align_mode = I2S.STANDARD_MODE)\n" +
- "_recorder_rx.set_sample_rate(sample_rate)\n" +
- "def _microphone_read_average(lst): \n" +
- " return int((sum(lst)/len(lst))*100)\n" +
- "";
- Blockly.Python.addVariable('sample_rate', "sample_rate = 38640", true);
- Blockly.Python.addVariable('sample_points', "sample_points = 1024", true);
- Blockly.Python.addVariable('fft_points', "fft_points = 512", true);
- Blockly.Python.addVariable('hist_x_num', "hist_x_num = 50", true);
- Blockly.Python.addVariable('_recorder_rx', "", true);
- Blockly.Python.addVariable('fft_res', "", true);
- Blockly.Python.addVariable('fft_amp', "", true);
- Blockly.Python.addVariable('read_all_channel', "read_all_channel = [0, 0, 0, 0, 0, 0, 0, 0]", true);
- // Blockly.Python.addVariable('_recorder_rx', "", true);
- var _code = "" +
- "";
- return _code;
- }
- Blockly.Blocks['mphone_audio_spectrum_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ai_audio_spectrum.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_start_spectrum_analyze);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.mphone_audio_spectrum_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['mphone_audio_spectrum_init'] = function(block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_.mphone_audio_spectrum_init_def = "" +
- "import time\n" +
- "";
- var code = "" +
- "time.sleep_ms(10)\n" +
- "_audio = _recorder_rx.record(sample_points)\n" +
- "fft_res = FFT.run(_audio.to_bytes(),fft_points)\n" +
- "fft_amp = FFT.amplitude(fft_res)\n\n" +
- "read_all_channel[0] = int(_microphone_read_average(fft_amp[0:63]))\n" +
- "read_all_channel[1] = int(_microphone_read_average(fft_amp[64:127]))\n" +
- "read_all_channel[2] = int(_microphone_read_average(fft_amp[127:191]))\n" +
- "read_all_channel[3] = int(_microphone_read_average(fft_amp[192:255]))\n" +
- "read_all_channel[4] = int(_microphone_read_average(fft_amp[256:319]))\n" +
- "read_all_channel[5] = int(_microphone_read_average(fft_amp[320:383]))\n" +
- "read_all_channel[6] = int(_microphone_read_average(fft_amp[384:447]))\n" +
- "read_all_channel[7] = int(_microphone_read_average(fft_amp[448:514]))\n" +
- "";
- return code;
- };
- Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_microphone_get_spectrum_analyze_result)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_1, "0"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_2, "1"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_3, "2"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_4, "3"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_5, "4"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_6, "5"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_7, "6"],
- [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_8, "7"]
- ]), "level");
- this.setOutput(true, null);
- this.setColour("#5fcd8e");
- this.setTooltip(Blockly.Msg.ai_mphone_read_sound_sensitivity_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_mphone_read_sound_sensitivity'] = function(block) {
- var dropdown_level = block.getFieldValue('level');
- // TODO: Assemble Python into code variable.
- var code = 'read_all_channel[' + dropdown_level + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- _ ___ _ _ _
- / \ |_ _| / \ _ _ __| (_) ___
- / _ \ | | / _ \| | | |/ _` | |/ _ \
- / ___ \ | | / ___ \ |_| | (_| | | (_) |
- /_/ \_\___| /_/ \_\__,_|\__,_|_|\___/
- */
- Blockly.Blocks['ai_audio_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_init);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- }
- };
- Blockly.Python.ai_audio_init = function(block) {
- Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
- Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
- Blockly.Python.definitions_.import_audio = "import audio";
- var _code = "";
- return _code;
- }
- Blockly.Blocks['ai_audio_play_song'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song'] = function(block) {
- var number_play_vol = block.getFieldValue('play_vol');
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "def _speaker_play_song(path, volume):\n" +
- " global _audio_play_state\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " print(\"Playing.\")\n" +
- " elif (_audio_play_state) == False:\n" +
- " print(\"Done.\")\n" +
- " break\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_state', _in, true);
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
- return _code;
- };
- Blockly.Blocks['ai_audio_play_state'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_get_play_status);
- this.setOutput(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_state'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_audio_play_state';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_play_finish'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_end_play);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_finish'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_player.finish()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_set'] = {
- init: function() {
- this.appendValueInput("Num")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_set_music_vol);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#603ea0");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_audio_set = function(block) {
- var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
- var _code = "_player.volume(" + _num + ")\n"
- return _code;
- }
- var SPEECH_RECOGNITION_BLOCK_COLOR = "230";
- Blockly.Blocks['speech_recognition_new_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_setup_set_sensi)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_normal, "10000"],
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_high, "20000"],
- [Blockly.Msg.ai_speech_recognition_setup_set_sensi_ehigh, "30000"]
- ]), "sensitivity");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_setup'] = function(block) {
- var dropdown_sensitivity = block.getFieldValue('sensitivity');
- Blockly.Python.definitions_.speech_recognition_new_setup_def = "" +
- "import os, json, time, utime, struct\n" +
- "from Maix import I2S, GPIO\n" +
- "from modules import SpeechRecognizer\n" +
- "from fpioa_manager import *\n" +
- "fm.register(31, fm.fpioa.GPIO3)\n" +
- "fm.register(32, fm.fpioa.GPIO4)\n" +
- "\n" +
- "_led_red = GPIO(GPIO.GPIO3, GPIO.OUT)\n" +
- "_led_blue = GPIO(GPIO.GPIO4, GPIO.OUT)\n" +
- "\n" +
- "def _sr_data_save(s,content,keyword_num, model_num, path):\n" +
- " data = _s_daemon.get_model_data(keyword_num, model_num)\n" +
- " with open(path,'w') as f:\n" +
- " f.write(data)\n" +
- "\n" +
- "def _sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
- " print(path)\n" +
- " with open(path,'r') as f:\n" +
- " data = f.read()\n" +
- " _s_daemon.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
- "\n" +
- "def _sr_init_remove_old_recording():\n" +
- " global _voice_record\n" +
- " try:\n" +
- " for i in os.listdir(\"/sd/sr\"):\n" +
- " print(\"deleting \" + str(i) + \"...\")\n" +
- " os.remove(\"/sd/sr/\"+str(i))\n" +
- " print(\"file deleting done.\")\n" +
- " os.rmdir(\"/sd/sr\")\n" +
- " print(\"directory deleting done.\")\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- " except:\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- " _voice_record = True\n" +
- "\n" +
- "# Enable Microphone and Disable Wifi Feature\n" +
- "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
- "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
- "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
- "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
- "wifi_en.value(0)\n" +
- "\n" +
- "# Init recording device parameteres\n" +
- "sample_rate = 8000\n" +
- "i2s_dev = I2S(I2S.DEVICE_0)\n" +
- "# config i2s according to speechrecognizer\n" +
- "i2s_dev.channel_config(i2s_dev.CHANNEL_0,I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT,cycles = I2S.SCLK_CYCLES_32,align_mode = I2S.STANDARD_MODE)\n" +
- "i2s_dev.set_sample_rate(sample_rate)\n" +
- "_s_daemon = SpeechRecognizer(i2s_dev)\n" +
- "_s_daemon.set_threshold(0,0," + dropdown_sensitivity + ")\n" +
- "";
- var code = '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recording_sample_start'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recording_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_text)
- .appendField(new Blockly.FieldNumber(2, 1, 6, 1), "clip")
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_note);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_text)
- .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "take")
- .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_note);
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_interface)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
- [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
- [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
- ]), "interface")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recording_sample_start_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recording_sample_start'] = function(block) {
- var number_clip = block.getFieldValue('clip');
- var number_take = block.getFieldValue('take');
- var interface = block.getFieldValue('interface');
- var code = '' +
- '# Remove old recordings\n' +
- '_sr_init_remove_old_recording()\n' +
- '\n' +
- 'import lcd\n' +
- 'import image\n' +
- 'try:from cocorobo import display_cjk_string\n' +
- 'except:pass\n'+
- '\n' +
- 'def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):\n'+
- ' try:\n'+
- ' display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)\n'+
- ' return canvas\n'+
- ' except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)\n'+
- '\n' +
- '_canvas_x, _canvas_y = 0, 0\n' +
- 'lcd.init(type=2,freq=15000000,width=240,height=240,color=(0,0,0))\n' +
- 'lcd.rotation(1)\n' +
- 'lcd.clear(lcd.BLACK)\n' +
- 'canvas = image.Image(size=(240, 240))\n' +
- 'canvas.clear()\n' +
- 'canvas.draw_rectangle(0,0, 240, 240, color=(0,0,0), thickness=1, fill=True)\n';
- if (interface == "English") {
- code += 'lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
- } else {
- code += 'lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
- }
- code += '\n' +
- '_voice_keyword_num = ' + number_clip + '\n' +
- '_voice_model_num = ' + number_take + '\n' +
- 'for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- 'lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- '\n' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- 'import time\n' +
- 'from machine import Timer\n' +
- '\n' +
- '_gp_side_buttons = [9, 10, 11]\n' +
- 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
- '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
- '_voice_button_status = False\n' +
- '\n' +
- 'def _on_read_voice_recog_button_timer(timer):\n' +
- ' global _voice_button_status\n' +
- ' if _gp_side_a.value() == True: _voice_button_status = True\n' +
- ' elif _gp_side_a.value() == False: _voice_button_status = False\n' +
- '\n' +
- '_on_read_voice_recog_periodic_tim = Timer(Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PERIODIC, period=1, callback=_on_read_voice_recog_button_timer, arg=_on_read_voice_recog_button_timer)\n' +
- '_on_read_voice_recog_periodic_tim.start()\n' +
- '\n' +
- 'try:\n' +
- ' while _voice_record == True:\n' +
- '\n' +
- ' for i in range(_voice_keyword_num):\n' +
- ' for j in range(_voice_model_num):\n' +
- ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
- '\n' +
- ' while True:\n' +
- ' if _voice_button_status == True:\n' +
- ' break\n' +
- ' else:\n' +
- ' print(".", end="")\n' +
- ' _led_red.value(1)\n' +
- ' time.sleep_ms(100)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep_ms(100)\n' +
- '\n' +
- ' # Start recoding procedure\n' +
- ' _led_red.value(0)\n' +
- ' _s_daemon.record(i, j)\n' +
- ' time.sleep_ms(500)\n' +
- '\n' +
- ' # Check recording state\n' +
- ' while (_s_daemon.get_status() != 2):\n' +
- ' if (_s_daemon.get_status() == 1):\n' +
- ' print("[CocoRobo] 现在开始说!")\n' +
- ' _led_red.value(1)\n' +
- ' _led_blue.value(1)\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 65,0, "Speak Now", color=(255,0,0), scale=2, mono_space=False)\n';
- } else if (interface == 'Simplified Chinese') {
- code += ' lcd_draw_string(canvas, 75,0, "现在开始说", font_size=1, color=(255,0,0))\n';
- } else {
- code += ' lcd_draw_string(canvas, 75,0, "現在開始說", font_size=1, color=(255,0,0))\n';
- }
- code += '\n' +
- ' if (_s_daemon.get_status() == 5):\n' +
- ' print("[CocoRobo] 收集噪音...")\n' +
- ' _led_red.value(1)\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 45,0, "Gathering Noise...", color=(255,0,0), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 75,0, "收集噪音...", font_size=1, color=(255,0,0))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' time.sleep_ms(500)\n' +
- ' if (_s_daemon.get_status() == 2):\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 95,0, "OK", color=(10,255,0), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 95,0, "成功 ", font_size=1, color=(10,255,0))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 4:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' for t in range(0,6,1):\n' +
- ' _led_blue.value(1)\n' +
- ' _led_red.value(1)\n' +
- ' time.sleep_ms(100)\n' +
- ' _led_blue.value(0)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep_ms(100)\n' +
- ' time.sleep(1)\n' +
- '\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas, 85,0, "press A", color=(255,255,255), scale=2, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas, 95,0, "按下A ", font_size=1, color=(255,255,255))\n';
- }
- code += '\n' +
- ' if i == 0:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 1:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 2:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 3:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k == 0 and l < 4:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 1:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 1 or (k == 1 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 2:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 2 or (k == 2 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 3:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 3 or (k == 3 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' elif i == 4:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 4 or (k == 4 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' else:\n' +
- ' if j == 0:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 1):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 1:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 2):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' elif j == 2:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 3):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- ' else:\n' +
- ' for k in range(_voice_keyword_num):\n' +
- ' lcd_draw_string(canvas, 30,30 + (k % _voice_keyword_num) * 35, str(k+1), color=(255,255,255), scale=2, mono_space=False)\n' +
- ' for l in range(_voice_model_num):\n' +
- ' if k < 5 or (k == 5 and l < 4):\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=True)\n' +
- ' else:\n' +
- ' canvas.draw_rectangle(70 + (l % _voice_model_num) * 35,30 + (k % _voice_keyword_num) * 35, 25, 25, color=(255,255,255), thickness=3, fill=False)\n' +
- '\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- '\n' +
- ' # Done Recording\n' +
- ' content = str(i) + \'_\' + str(j)\n' +
- ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+"_" + str(_s_daemon.get_model_info(i,j)).strip("[]") +".sr"\n' +
- ' _sr_data_save(_s_daemon, content, i, j, file_name)\n' +
- ' print("frm_num is: " + str(_s_daemon.get_model_info(i,j)))\n' +
- '\n' +
- ' print("Record successful! Next, Run your speech recognition code!")\n' +
- ' _voice_record = False\n' +
- '\n' +
- ' canvas.clear()\n' +
- ' canvas.draw_image(image.Image("/sd/preset/images/tick.jpg"), 70,70, x_scale=0.5, y_scale=0.5)\n' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' _led_blue.value(1)\n' +
- ' _led_red.value(0)\n' +
- ' time.sleep(2)\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' canvas.clear()\n';
- if (interface == "English") {
- code += ' lcd_draw_string(canvas,30,40, "Voice Sample", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,70, "Recording", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,100, "Failed!", color=(255,0,0), scale=1, mono_space=False)\n' +
- ' lcd_draw_string(canvas,30,140, "Reason: " + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- } else if (interface == 'Simplified Chinese') {
- code += ' lcd_draw_string(canvas,30,40, "语音样本",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,70, "正在记录中",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,100, "失败!",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- } else {
- code += ' lcd_draw_string(canvas,30,40, "語音樣本",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,70, "正在記錄中",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,100, "失敗!",font_size=1, color=(255,0,0))\n' +
- ' lcd_draw_string(canvas,30,140, "原因:" + str(e), color=(255,0,0), scale=1, mono_space=False)\n';
- }
- code += '' +
- ' lcd.display(canvas, oft=(_canvas_x,_canvas_y))\n' +
- ' _led_blue.value(0)\n' +
- ' _led_red.value(1)\n' +
- '\n' +
- 'import machine\n' +
- 'machine.reset()\n' +
- '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_start_recognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_start_recognizing);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_start_recognition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_start_recognition'] = function(block) {
- Blockly.Python.definitions_.speech_recognition_new_start_recognition_def = "" +
- "import time\n\n" +
- "_voice_recording_load = True\n" +
- "_voice_recording_ready = False\n" +
- "\n" +
- "if _voice_recording_load == True:\n" +
- " _sr_filelist = os.listdir(\"/sd/sr\")\n" +
- " for _sr_file in _sr_filelist: \n" +
- " _sr_frm_num = int(_sr_file[4:_sr_file.find(\".\")])\n" +
- " print(_sr_frm_num)\n" +
- " print(\"/sd/sr/\" + str(_sr_file))\n" +
- " _sr_data_load(_s_daemon, int(_sr_file[0]), int(_sr_file[2]), _sr_frm_num, \"/sd/sr/\" + str(_sr_file))\n" +
- " print(\"load successful!\")\n" +
- "";
- var code = '_s_recognition_state = 0\n' +
- '_s_daemon.recognize()\n' +
- 'time.sleep_ms(500)\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recognition_get_any_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_any_result_text);
- this.appendStatementInput("NAME")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_any_result_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recognition_get_any_result'] = function(block) {
- var statements_name = Blockly.Python.statementToCode(block, 'NAME');
- // TODO: Assemble Python into code variable.
- var code = '' +
- 'while _s_recognition_state == 0:\n' +
- ' if (_s_daemon.get_status() == 5):\n' +
- ' print("", end="")\n' +
- ' elif (_s_daemon.get_status() == 3):\n' +
- ' print("", end="")\n' +
- ' elif (_s_daemon.get_status() == 4):\n' +
- ' print("[CocoRobo] Record OK, Proceed!")\n' +
- ' _s_recognition_state = 1\n' +
- ' else:\n' +
- ' print("[CocoRobo] Current state: " + str(s.get_status()))\n' +
- '\n' +
- 'time.sleep_ms(800)\n' +
- '_s_ret = _s_daemon.get_result()\n' +
- 'print("[CocoRobo] Result: " + str(_s_ret))\n' +
- '\n' +
- 'if (_s_ret > 0):\n' +
- statements_name +
- '';
- return code;
- };
- Blockly.Blocks['speech_recognition_new_recognition_get_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_get_result)
- .appendField(new Blockly.FieldNumber(1, 1, 6, 1), "index")
- .appendField(Blockly.Msg.ai_speech_recognition_get_result_after);
- this.setOutput(true, null);
- this.setColour(SPEECH_RECOGNITION_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_result_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['speech_recognition_new_recognition_get_result'] = function(block) {
- var number_index = block.getFieldValue('index');
- // TODO: Assemble Python into code variable.
- var code = '(_s_ret == ' + number_index + ')';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Blocks['ai_audio_voice_recognition_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/voice_recognition_header.png", 120, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField("Voice Recognition Setup");
- this.appendDummyInput()
- .appendField("Set the Total Number of Voice Clip(s): ")
- .appendField(new Blockly.FieldNumber(3, 1, 6, 1), "word_count")
- .appendField(" (No more than 6)");
- this.appendDummyInput()
- .appendField("Set the Total Count of Sample(s) for each clip: ")
- .appendField(new Blockly.FieldNumber(3, 1, 4, 1), "repeat_count")
- .appendField(" (No more than 4)");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- var global_voice_recognition_word_count = 0;
- var global_voice_recognition_sample_count = 0;
- Blockly.Python['ai_audio_voice_recognition_setup'] = function(block) {
- var number_word_count = block.getFieldValue('word_count');
- var number_repeat_count = block.getFieldValue('repeat_count');
- // TODO: Assemble Python into code variable.
- global_voice_recognition_word_count = number_word_count;
- global_voice_recognition_sample_count = number_repeat_count;
- console.log(global_voice_recognition_word_count + "," + global_voice_recognition_sample_count);
- Blockly.Python.definitions_.voice_recognition_def = "" +
- "import lcd\n" +
- "import image\n" +
- "\n" +
- "lcd.init(type=1,freq=15000000,width=240,height=240,color=(0,0,0))\n" +
- "lcd.rotation(1)\n" +
- "lcd.clear(lcd.BLACK)\n" +
- "\n" +
- "import os, json, time, utime, struct\n" +
- "from Maix import I2S, GPIO\n" +
- "from modules import SpeechRecognizer\n" +
- "from fpioa_manager import *\n" +
- "\n" +
- "fm.register(31, fm.fpioa.GPIO3)\n" +
- "fm.register(32, fm.fpioa.GPIO4)\n" +
- "\n" +
- "led_red=GPIO(GPIO.GPIO3,GPIO.OUT)\n" +
- "led_blue=GPIO(GPIO.GPIO4,GPIO.OUT)\n" +
- "\n" +
- "def sr_data_save(s,content,keyword_num, model_num, path):\n" +
- " data = s.get_model_data(keyword_num, model_num)\n" +
- " with open(path,'w') as f:\n" +
- " f.write(data)\n" +
- "\n" +
- "def sr_data_load(s, keyword_num, model_num,frame_num, path):\n" +
- " print(path)\n" +
- " with open(path,'r') as f:\n" +
- " data = f.read()\n" +
- " # print(data)\n" +
- " s.add_voice_model(keyword_num, model_num, frame_num, data)\n" +
- "\n" +
- "def sr_led_blinking(type):\n" +
- " if type == \"waitfor\":\n" +
- " led_red.value(1)\n" +
- " time.sleep_ms(100)\n" +
- " led_red.value(0)\n" +
- " time.sleep_ms(100)\n" +
- " elif type == \"recorded\":\n" +
- " for i in range(0,5,1):\n" +
- " led_blue.value(1)\n" +
- " time.sleep_ms(100)\n" +
- " led_blue.value(0)\n" +
- " time.sleep_ms(100)\n" +
- "\n" +
- "fm.register(20, fm.fpioa.I2S0_IN_D0, force=True)\n" +
- "fm.register(18, fm.fpioa.I2S0_SCLK, force=True)\n" +
- "fm.register(19, fm.fpioa.I2S0_WS, force=True)\n" +
- "\n" +
- "fm.register(8, fm.fpioa.GPIO5, force=True)\n" +
- "wifi_en=GPIO(GPIO.GPIO5,GPIO.OUT)\n" +
- "wifi_en.value(0)\n" +
- "\n" +
- "sample_rate = 8000\n" +
- "i2s_dev = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "# config i2s according to speechrecognizer\n" +
- "i2s_dev.channel_config(i2s_dev.CHANNEL_0,I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT,cycles = I2S.SCLK_CYCLES_32,align_mode = I2S.STANDARD_MODE)\n" +
- "i2s_dev.set_sample_rate(sample_rate)\n" +
- "s = SpeechRecognizer(i2s_dev)\n" +
- "try:\n" +
- " print(\"Setting get_noise_threshold to less sensitive.\")\n" +
- " s.set_threshold(0,0,20000)\n" +
- "except BaseException as e:\n" +
- " print(\"Failed to set threshold, please check if your firmware is right, error info:\")\n" +
- " print(str(e))\n" +
- "";
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_start_record'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Start Recording Samples (By Pressing Button ")
- .appendField(new Blockly.FieldDropdown([
- ["A", "a"],
- ["B", "b"],
- ["C", "c"]
- ]), "button_type")
- .appendField(")");
- this.appendDummyInput()
- .appendField("Note 1: Recorded Samples Will Stored in SD Card.");
- this.appendDummyInput()
- .appendField("Note 2: Write down the frm_num for each sample.");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_start_record'] = function(block) {
- var dropdown_button_type = block.getFieldValue('button_type');
- // TODO: Assemble Python into code variable.
- Blockly.Python.addVariable('_voice_on_recoding_state', '', true);
- Blockly.Python.definitions_.voice_recognition_start_record_def = "" +
- "try:\n" +
- " for i in os.listdir(\"/sd/sr\"):\n" +
- " print(\"deleting \" + str(i) + \"...\")\n" +
- " os.remove(\"/sd/sr/\"+str(i))\n" +
- " print(\"file deleting done.\")\n" +
- " os.rmdir(\"/sd/sr\")\n" +
- " print(\"directory deleting done.\")\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- "except:\n" +
- " os.mkdir(\"/sd/sr\")\n" +
- " print(\"directory creating done.\")\n" +
- "\n" +
- "_voice_record = True\n" +
- "\n" +
- "from fpioa_manager import *\n" +
- "from Maix import FPIOA, GPIO\n" +
- "\n" +
- "_gp_side_buttons = [9, 10, 11]\n" +
- "\n" +
- "FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n" +
- "FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n" +
- "FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n" +
- "\n" +
- "_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n" +
- "_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n" +
- "_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n" +
- "\n" +
- "import time\n" +
- "from machine import Timer\n" +
- "\n" +
- "_voice_button_status = False\n" +
- "\n" +
- "def _on_read_voice_recog_button_timer(timer):\n" +
- " global _voice_button_status\n" +
- " if _gp_side_" + dropdown_button_type + ".value() == True: _voice_button_status = True\n" +
- " elif _gp_side_" + dropdown_button_type + ".value() == False: _voice_button_status = False\n" +
- "\n" +
- "_on_read_voice_recog_periodic_tim = Timer(Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PERIODIC, period=1, callback=_on_read_voice_recog_button_timer, arg=_on_read_voice_recog_button_timer)\n" +
- "_on_read_voice_recog_periodic_tim.start()\n" +
- "";
- var code = '' +
- 'while _voice_record == True:\n' +
- ' print(str(_voice_button_status))\n' +
- '\n' +
- ' _voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords, \n' +
- ' _voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
- '\n' +
- ' for i in range(_voice_keyword_num):\n' +
- ' for j in range(_voice_model_num):\n' +
- ' print("Press the button to record the {} keyword, the {}".format(i+1, j+1))\n' +
- ' while True:\n' +
- ' if _voice_button_status == True:\n' +
- ' break\n' +
- ' else:\n' +
- ' print(\'.\', end="")\n' +
- ' sr_led_blinking("waitfor")\n' +
- ' led_red.value(1)\n' +
- ' s.record(i, j)\n' +
- ' led_red.value(0)\n' +
- ' sr_led_blinking("recorded")\n' +
- ' content = str(i) + \'_\' + str(j)\n' +
- ' print(content)\n' +
- ' file_name = "/sd/sr/" + str(i) + \'_\' + str(j)+".sr"\n' +
- ' b = sr_data_save(s, content, i, j, file_name)\n' +
- ' print(b)\n' +
- ' print("Record successful! Next, Run your speech recognition code!")\n' +
- '\n' +
- ' _voice_record = False\n' +
- '';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_start_recognizing'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Start Recognizing");
- this.appendValueInput("frm_num_set")
- .setCheck(null)
- .appendField("Set Frame Number for Each Sample:");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_start_recognizing'] = function(block) {
- var value_frm_num_set = Blockly.Python.valueToCode(block, 'frm_num_set', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var sr_data_load_content = '';
- var count = 0;
- for (var i = 0; i < global_voice_recognition_word_count; i++) {
- console.log(i);
- for (var j = 0; j < global_voice_recognition_sample_count; j++) {
- console.log(j);
- sr_data_load_content += ' sr_data_load(s, ' + i + ', ' + j + ', _voice_recording_load_frm_num[' + i + '][' + j + '], "/sd/sr/' + i + '_' + j + '.sr")\n'
- count += 1;
- }
- }
- Blockly.Python.definitions_.voice_recognition_start_recognizing_def = '' +
- '_voice_recording_load = True\n' +
- '_voice_recording_ready = False\n' +
- '\n' +
- '_voice_keyword_num = ' + global_voice_recognition_word_count + ' # Currently supports a maximum of 10 keywords\n' +
- '_voice_model_num = ' + global_voice_recognition_sample_count + ' # each recording a maximum of 4 templates\n' +
- '\n' +
- '_voice_recording_load_frm_num = [0] * (_voice_keyword_num*_voice_model_num)\n' +
- '_voice_recording_load_frm_num = ' + value_frm_num_set + '\n' +
- '\n' +
- 'if _voice_recording_load == True:\n' +
- sr_data_load_content +
- // ' sr_data_load(s, 0, 0, 86, "/sd/sr/0_0.sr")\n' +
- // ' sr_data_load(s, 0, 1, 43, "/sd/sr/0_1.sr")\n' +
- // ' sr_data_load(s, 0, 2, 45, "/sd/sr/0_2.sr")\n' +
- // ' sr_data_load(s, 1, 0, 39, "/sd/sr/1_0.sr")\n' +
- // ' sr_data_load(s, 1, 1, 82, "/sd/sr/1_1.sr")\n' +
- // ' sr_data_load(s, 1, 2, 134, "/sd/sr/1_2.sr")\n' +
- // ' sr_data_load(s, 2, 0, 35, "/sd/sr/2_0.sr")\n' +
- // ' sr_data_load(s, 2, 1, 158, "/sd/sr/2_1.sr")\n' +
- // ' sr_data_load(s, 2, 2, 87, "/sd/sr/2_2.sr")\n' +
- ' print("load successful!")\n' +
- '';
- var code = '_voice_recognition_result = s.recognize()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_status'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("When Get Noise is Done");
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_status'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '...';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_any_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("Get Any Result from Voice Recogntion")
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_any_result'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_voice_recognition_result > 0';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_voice_recognition_get_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField("When Recognized Clip #")
- .appendField(new Blockly.FieldNumber(0, 1, 6), "clip_no");
- this.setOutput(true, null);
- this.setColour(230);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_voice_recognition_get_result'] = function(block) {
- var dropdown_clip_no = block.getFieldValue('clip_no');
- // TODO: Assemble Python into code variable.
- var code = '_voice_recognition_result == ' + dropdown_clip_no;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- */
- /*
- _ ___ ____ _
- / \ |_ _| / ___| _ __ ___ __ _| | _____ _ __
- / _ \ | | \___ \| '_ \ / _ \/ _` | |/ / _ \ '__|
- / ___ \ _ | | _ ___) | |_) | __/ (_| | < __/ |
- /_/ \_(_)___(_) |____/| .__/ \___|\__,_|_|\_\___|_|
- |_|
- */
- Blockly.Blocks['ai_audio_init_speaker'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_init);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_init_speaker_TOOLTIP);
- this.setHelpUrl("");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- }
- };
- Blockly.Python.ai_audio_init_speaker = function(block) {
- Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
- Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
- Blockly.Python.definitions_.import_audio = "import audio";
- var _code = "";
- return _code;
- }
- Blockly.Blocks['ai_audio_play_song_speaker'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
- .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_song_speaker'] = function(block) {
- var number_play_vol = block.getFieldValue('play_vol');
- var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var _in = "" +
- "_audio_rx = I2S(I2S.DEVICE_0)\n" +
- "\n" +
- "def _speaker_play_song(path, volume):\n" +
- " global _audio_play_state\n" +
- " _player = audio.Audio(path = path)\n" +
- " _player.volume(volume)\n" +
- " _wav_info = _player.play_process(_audio_rx)\n" +
- " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
- " _audio_rx.set_sample_rate(_wav_info[1])\n" +
- " _audio_play_state = 0\n" +
- "\n" +
- " fm.register(34,fm.fpioa.I2S0_OUT_D1)\n" +
- " fm.register(35,fm.fpioa.I2S0_SCLK)\n" +
- " fm.register(33,fm.fpioa.I2S0_WS)\n" +
- "\n" +
- " while True:\n" +
- " _audio_play_state = _player.play()\n" +
- " if (_audio_play_state) == True:\n" +
- " print(\"Playing.\")\n" +
- " elif (_audio_play_state) == False:\n" +
- " print(\"Done.\")\n" +
- " break\n" +
- "\n";
- // Blockly.Python.definitions_.import_audio = _in;
- Blockly.Python.addVariable('_audio_play_state', _in, true);
- // Blockly.Python.addSetup("ai_audio_read", _in);
- var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
- return _code;
- };
- Blockly.Blocks['ai_audio_play_state_speaker'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_get_play_status);
- this.setOutput(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_state_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_state_speaker'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_audio_play_state';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_audio_play_finish_speaker'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_audio_end_play);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_play_finish_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_audio_play_finish_speaker'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_player.finish()\n';
- return code;
- };
- Blockly.Blocks['ai_audio_set_speaker'] = {
- init: function() {
- this.appendValueInput("Num")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_audio_set_music_vol);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5f7fc1");
- this.setTooltip(Blockly.Msg.ai_audio_set_speaker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_audio_set_speaker = function(block) {
- var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
- var _code = "_player.volume(" + _num + ")\n"
- return _code;
- }
- /*
- _ ___ __ ___ _
- / \ |_ _| \ \ / (_) __| | ___ ___
- / _ \ | | \ \ / /| |/ _` |/ _ \/ _ \
- / ___ \ | | \ V / | | (_| | __/ (_) |
- /_/ \_\___| \_/ |_|\__,_|\___|\___/
- */
- Blockly.Blocks['ai_video_record_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_start_recording);
- this.appendValueInput("save_path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_file_path);
- this.appendValueInput("w")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_width);
- this.appendValueInput("h")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_start_recording_height);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_record_setup'] = function(block) {
- var value_save_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
- var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
- var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.import_video = "import video";
- Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
- Blockly.Python.addVariable('record_vid', 'record_vid = None', true);
- // TODO: Assemble Python into code variable.
- var code = "_record_vid = video.open(" + value_save_path + ", record=1, width=" + value_w + ", height=" + value_h + ", interval=20000, quality=80)\n";
- return code;
- };
- Blockly.Blocks['ai_video_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_to_canvas)
- .appendField(new Blockly.FieldVariable("canvas"), "varitem")
- .appendField(Blockly.Msg.ai_video_record);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_get = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "_record_vid.record(" + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_video_stop'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_stop_recording);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#60c1bb");
- this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_stop = function(block) {
- var _code = "_record_vid.record_finish()\n"
- return _code;
- }
- Blockly.Blocks['ai_video_set'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_play_video);
- this.appendValueInput("path")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_video_play_video_file_path);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_play_video_2);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_set_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_set'] = function(block) {
- Blockly.Python.definitions_.import_video = "import video";
- Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
- Blockly.Python.addVariable('_video_play', '', true);
- var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
- var code = '_video_play = video.open(' + value_path + ')\n';
- return code;
- };
- Blockly.Blocks['ai_video_stop_play'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_stop_video_playing);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_stop_play_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_video_stop_play'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_video_play.__del__()\n';
- return code;
- };
- Blockly.Blocks['ai_video_status'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_video_get_video_playing_status);
- this.setOutput(true, null);
- this.setColour("#4d9994");
- this.setTooltip(Blockly.Msg.ai_video_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_video_status = function(block) {
- var code = "_video_play.play()";
- return [code, Blockly.Python.ORDER_NONE];
- }
- /*
- _ ___ __ __ _ _
- / \ |_ _| | \/ | ___ __| | ___| |___
- / _ \ | | | |\/| |/ _ \ / _` |/ _ \ / __|
- / ___ \ | | | | | | (_) | (_| | __/ \__ \
- /_/ \_\___| |_| |_|\___/ \__,_|\___|_|___/
- */
- Blockly.Blocks['ai_model_allinit'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_init_title)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
- [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
- [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
- ]), "DEGREE");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_init_source)
- .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('DEGREE');
- var TOOLTIPS = {
- 'Single Digit Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
- 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
- 'Human Face Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_allinit = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var degee = block.getFieldValue('DEGREE');
- var _code = ""
- if (degee == "Single Digit Recognition") {
- Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
- // Blockly.Python.addVariable('img_mnist1', '', true);
- // Blockly.Python.addVariable('img_mnist_resized', '', true);
- _code += "img_mnist1=img_mnist.to_grayscale(1)\n";
- _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
- _code += "a=img_mnist_resized.invert()\n";
- _code += "a=img_mnist_resized.strech_char(1)\n";
- _code += "a=img_mnist_resized.pix_to_ai()\n";
- _code += "fmap_mnist=kpu.forward(task_mnist, " + variable_name + ")\n";
- _code += "plist_mnist=fmap_mnist[:]\n";
- _code += "pmax_mnist=max(plist_mnist)\n";
- _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
- } else if (degee == "Common Object Recognition") {
- Blockly.Python.addVariable('task_objectrecognition', '', true);
- Blockly.Python.addVariable('img_objectrecognition', '' +
- 'classes_objectrecognition = [\'aeroplane\', \'bicycle\', \'bird\', \'boat\', \'bottle\', \'bus\', \'car\', \'cat\', \'chair\', \'cow\', \'diningtable\', \'dog\', \'horse\', \'motorbike\', \'person\', \'pottedplant\', \'sheep\', \'sofa\', \'train\', \'tvmonitor\']\n' +
- 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
- 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
- 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
- '', true);
- // Blockly.Python.addVariable('code_objectrecognition', '', true);
- _code += "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n";
- _code += variable_name + ".ai_to_pix()\n";
- } else {
- Blockly.Python.addVariable('task_facerecognition', '', true);
- // Blockly.Python.addVariable('task_facerecognition', '', true);
- Blockly.Python.addVariable('img_facerecognition', "" +
- "task_facerecognition = kpu.load(\"/sd/preset/models/preset/face-recognition.kmodel\")\n" +
- "anchor_face = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n" +
- "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor_face)\n" +
- "\n", true);
- // Blockly.Python.addVariable('code_facerecognition', '', true);
- _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
- _code += variable_name + ".ai_to_pix()\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_model_index_mnist'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_hand_written_model_result);
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_hand_written_model_number, "c"],
- [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"]
- ]), "value");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_index_mnist_TOOLTIP);
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_index_mnist = function(block) {
- var value = block.getFieldValue('value');
- var _code = "max_index_mnist";
- if (value == "confidence") {
- _code = "int(pmax_mnist*100)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_look'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_object_model_if);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_look_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_look = function(block) {
- var _code = "code_objectrecognition";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_object_model_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_object_model_name, "Object"],
- [Blockly.Msg.ai_models_object_model_x, "X"],
- [Blockly.Msg.ai_models_object_model_y, "Y"],
- [Blockly.Msg.ai_models_object_model_w, "W"],
- [Blockly.Msg.ai_models_object_model_h, "H"],
- [Blockly.Msg.ai_models_object_model_x_center, "CX"],
- [Blockly.Msg.ai_models_object_model_y_center, "CY"],
- [Blockly.Msg.ai_models_object_model_count, "Number"],
- [Blockly.Msg.ai_models_object_model_index, "Serial"],
- [Blockly.Msg.ai_models_object_model_confidence, "Confidence"]
- ]), "TYPE");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('TYPE');
- var TOOLTIPS = {
- 'Object': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_name),
- 'X': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x),
- 'Y': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y),
- 'W': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_w),
- 'H': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_h),
- 'CX': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x_center),
- 'CY': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y_center),
- 'Number': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_count),
- 'Serial': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_index),
- 'Confidence': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_get = function(block) {
- var type = block.getFieldValue('TYPE');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "";
- if (type == "Object") {
- _code = "classes_objectrecognition[" + variable_name + ".classid()]";
- } else if (type == "X") {
- _code = variable_name + ".x()";
- } else if (type == "Y") {
- _code = variable_name + ".y()";
- } else if (type == "W") {
- _code = variable_name + ".w()";
- } else if (type == "H") {
- _code = variable_name + ".h()";
- } else if (type == "Number") {
- _code = variable_name + ".objnum()";
- } else if (type == "Serial") {
- _code = variable_name + ".index()";
- } else if (type == "Confidence") {
- _code = variable_name + ".value()";
- } else if (type == "CX") {
- _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (type == "CY") {
- _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_face'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_face_model_if);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_face_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_face = function(block) {
- var _code = "code_facerecognition";
- return [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Blocks['ai_model_xy'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_face_model_get)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_face_model_x, "X"],
- [Blockly.Msg.ai_models_face_model_y, "Y"],
- [Blockly.Msg.ai_models_face_model_w, "W"],
- [Blockly.Msg.ai_models_face_model_h, "H"],
- [Blockly.Msg.ai_models_face_model_x_center, "CX"],
- [Blockly.Msg.ai_models_face_model_y_center, "CY"],
- [Blockly.Msg.ai_models_face_model_count, "Number"],
- [Blockly.Msg.ai_models_face_model_index, "Serial"],
- [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
- ]), "TYPE");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('TYPE');
- var TOOLTIPS = {
- 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
- 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
- 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
- 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
- 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
- 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
- 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
- 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
- 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_model_xy = function(block) {
- var type = block.getFieldValue('TYPE');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "";
- if (type == "X") {
- _code = variable_name + ".x()";
- } else if (type == "Y") {
- _code = variable_name + ".y()";
- } else if (type == "W") {
- _code = variable_name + ".w()";
- } else if (type == "H") {
- _code = variable_name + ".h()";
- } else if (type == "Number") {
- _code = variable_name + ".objnum()";
- } else if (type == "Serial") {
- _code = variable_name + ".index()";
- } else if (type == "Confidence") {
- _code = variable_name + ".value()";
- } else if (type == "CX") {
- _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (type == "CY") {
- _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- return [_code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Blocks['ai_model_customized_init_setup_definition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_customized_init_setup_definition);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_init_setup_definition'] = function(block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.includes_.import_amxisd = '' +
- 'print("Clearing Cached Variables...", end="")\n' +
- 'for name in dir(): \n' +
- ' if not name.startswith(\'_\'): \n' +
- ' del globals()[name]\n' +
- 'print(" Done")\n' +
- 'import KPU as kpu\n' +
- 'kpu.memtest()\n' +
- 'from Maix import utils\n' +
- 'import gc\n' +
- 'gc.enable()\n' +
- 'utils.gc_heap_size()\n' +
- '\n' +
- '################# Done Init #################\n' +
- '';
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_model_customized_init_setup'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
- .appendField(new Blockly.FieldTextInput("/sd/user/mymodel.kmodel"), "model_path");
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
- .appendField(new Blockly.FieldVariable("camera"), "varitem");
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_init_setup'] = function(block) {
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var text_model_path = block.getFieldValue('model_path');
- Blockly.Python.definitions_.import_KPU_customized = "import KPU as kpu";
- Blockly.Python.addVariable('task_customized_model', '' +
- 'task_customized_model = kpu.load("' + text_model_path + '")\n' +
- 'anchor_customized_model = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828)\n' +
- 'a = kpu.init_yolo2(task_customized_model, 0.6, 0.3, 5, anchor_customized_model)\n' +
- '', true);
- Blockly.Python.addVariable('classes_customized_model', '' +
- 'classes_customized_model = ' + value_class_input + '\n' +
- '', true);
- var code = 'code_customized_model = kpu.run_yolo2(task_customized_model, ' + variable_name + ')\n';
- return code;
- };
- Blockly.Blocks['ai_model_face_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_face);
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_model_face_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_face_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_face_init'] = function(block) {
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.includes_.import_modelface = `
- for name in dir():
- if not name.startswith('_'):
- del globals()[name]
- import gc
- import KPU as kpu
- from Maix import utils
- utils.gc_heap_size(0xAF000)
- import sensor
- import image
- import lcd
- import time
- from Maix import FPIOA, GPIO
- import gc
- from fpioa_manager import fm
- from board import board_info
- try:from cocorobo import display_cjk_string
- except:pass
- def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
- try:
- display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
- return canvas
- except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
- Blockly.Python.addFunction("set_key_state", `def set_key_state(*_):
- global start_processing
- start_processing = True`);
- Blockly.Python.addVariable('task_f', `
- gc.enable()
- gc.collect()
- # kpu.memtest()
- task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel")
- task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel")
- task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel")
- #kpu.set_outputs(task, 0, 1,1,1) # 配置输出形状
- kpu.set_outputs(task_fe, 0, 1,1,512)
- # kpu.memtest()
- kpu.set_outputs(task_fe, 0,1,1, 512)
- fm.register(10, fm.fpioa.GPIOHS0)
- key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
- start_processing = False
- key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
- lcd.init(type=2)
- lcd.rotation(1)
- sensor.reset()
- sensor.set_pixformat(sensor.RGB565)
- sensor.set_framesize(sensor.QVGA)
- sensor.set_vflip(0)
- sensor.set_hmirror(0)
- sensor.run(1)
- anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,6.92275, 6.718375, 9.01025) # anchor for face detect
- dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)] # standard face key point position
- a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
- img_lcd = image.Image()
- img_face = image.Image(size=(128, 128))
- index = -1
- a = img_face.pix_to_ai()
- record_ftr = []
- record_ftrs = []
- gc.collect()
- clock = time.clock()
- names = ` + value_class_input + `
- `, true);
- var code = `img = sensor.snapshot()
- clock.tick()
- code = kpu.run_yolo2(task_fd, img)
- index = -1
- if code:
- for i in code:
- # Cut face and resize to 128x128
- a = img.draw_rectangle(i.rect())
- face_cut = img.cut(i.x(), i.y(), i.w(), i.h())
- face_cut_128 = face_cut.resize(128, 128)
- a = face_cut_128.pix_to_ai()
- #a = img.draw_image(face_cut_128, (0,0))
- # Landmark for face 5 points
- fmap = kpu.forward(task_ld, face_cut_128)
- plist = fmap[:]
- le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
- re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
- nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
- lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
- rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
- a = img.draw_circle(le[0], le[1], 4)
- a = img.draw_circle(re[0], re[1], 4)
- a = img.draw_circle(nose[0], nose[1], 4)
- a = img.draw_circle(lm[0], lm[1], 4)
- a = img.draw_circle(rm[0], rm[1], 4)
- # align face to standard position
- src_point = [le, re, nose, lm, rm]
- T = image.get_affine_transform(src_point, dst_point)
- a = image.warp_affine_ai(img, img_face, T)
- a = img_face.ai_to_pix()
- #a = img.draw_image(img_face, (128,0))
- del(face_cut_128)
- # calculate face feature vector
- fmap = kpu.forward(task_fe, img_face)
- feature = kpu.face_encode(fmap[:])
- reg_flag = False
- scores = []
- for j in range(len(record_ftrs)):
- score = kpu.face_compare(record_ftrs[j], feature)
- scores.append(score)
- max_score = 0
- index = -1
- for k in range(len(scores)):
- if max_score < scores[k]:
- max_score = scores[k]
- index = k
- if(len(names) > index):
- if max_score > 85:
- a = lcd_draw_string(img, i.x(), i.y(), ("%s :%2.1f" % (names[index], max_score)), color=(0, 255, 0), scale=2, mono_space=False)
- else:
- index = -1
- a = lcd_draw_string(img, i.x(), i.y(), ("X :%2.1f" % (max_score)), color=(255, 0, 0), scale=2, mono_space=False)
- if start_processing:
- record_ftr = feature
- record_ftrs.append(record_ftr)
- start_processing = False
- break
- fps = clock.fps()
- img = img.cut(40,0,240,240)
- a = lcd.display(img, oft=(0,0))
- gc.collect()
- # kpu.memtest()
- `;
- return code;
- };
- Blockly.Blocks['ai_model_class'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_class);
- this.appendValueInput("class_input")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_model_class_name);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_class_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_class'] = function(block) {
- var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_.import_modelface = `import KPU as kpu
- import sensor
- import lcd
- from Maix import FPIOA, GPIO
- from fpioa_manager import fm, board_info
- import time
- import gc
- try:from cocorobo import display_cjk_string
- except:pass
- def lcd_draw_string(canvas, x, y, text, color=(255,255,255), font_size=1, scale=1, mono_space=False, auto_wrap=True):
- try:
- display_cjk_string(canvas, x, y, text, font_size=font_size, color=color)
- return canvas
- except: return canvas.draw_string(x, y, text, color=color, scale=scale, mono_space=mono_space)`;
- Blockly.Python.addFunction("draw_string", `def draw_string(img, x, y, text, color, scale, bg=None ):
- if bg:
- img.draw_rectangle(x-2,y-2, len(text)*10*scale+4 , 24*scale, fill=True, color=bg)
- image = lcd_draw_string(img, x, y, text, color=color,scale=scale*2,mono_space=False)
- return image
- `);
- Blockly.Python.addVariable('class_', `############### config #################
- class_names = ` + value_class_input + `
- class_num = len(class_names)
- sample_num = len(class_names) * 5
- THRESHOLD = 11
- board_cube = 0
- button_state = False
- ########################################
- lcd.init(type=2)
- lcd.rotation(1)
- sensor.reset()
- sensor.set_pixformat(sensor.RGB565)
- sensor.set_framesize(sensor.QVGA)
- sensor.set_vflip(0)
- sensor.set_hmirror(0)
- sensor.run(1)
- #sensor.reset()
- #sensor.set_pixformat(sensor.RGB565)
- #sensor.set_framesize(sensor.QVGA)
- #sensor.set_windowing((224, 224))
- #if board_cube == 1:
- # sensor.set_vflip(True)
- # sensor.set_hmirror(True)
- # lcd.init(type=2)
- # lcd.rotation(2)
- #else:
- # lcd.init()
- #fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
- #key = GPIO(GPIO.GPIOHS0, GPIO.PULL_UP)
- FPIOA().set_function(10, FPIOA.GPIO1)
- key1 = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)
- try:
- del model
- except Exception:
- pass
- try:
- del classifier
- except Exception:
- pass
- gc.collect()
- model = kpu.load("/sd/preset/models/object_classifier.emodel")
- classifier = kpu.classifier(model, class_num, sample_num)
- cap_num = 0
- train_status = 0
- last_cap_time = 0
- last_btn_status = 0
- res_index = -1
- `)
- var code = `img = sensor.snapshot()
- if board_cube:
- img = img.rotation_corr(z_rotation=90)
- img.pix_to_ai()
- # capture img
- if train_status == 0:
- if key1.value() == 0 and last_btn_status == 1:
- #time.sleep_ms(30)
- #if key1.value() == 1 and (last_btn_status == 1) and (time.ticks_ms() - last_cap_time > 500):
- last_btn_status = 0
- last_cap_time = time.ticks_ms()
- if cap_num < class_num:
- index = classifier.add_class_img(img)
- cap_num += 1
- elif cap_num < class_num + sample_num:
- index = classifier.add_sample_img(img)
- cap_num += 1
- #else:
- # img = draw_string(img, 2, 200, "release boot key please", color=lcd.WHITE,scale=1, bg=lcd.RED)
- else:
- #time.sleep_ms(30)
- if key1.value() == 1 and (last_btn_status == 0):
- last_btn_status = 1
- if cap_num < class_num:
- img = draw_string(img, 0, 2, "press right key to cap "+class_names[cap_num], color=lcd.WHITE,scale=1, bg=lcd.RED)
- elif cap_num < class_num + sample_num:
- img = draw_string(img, 0, 2, "right key to cap sample{}".format(cap_num-class_num), color=lcd.WHITE,scale=1, bg=lcd.RED)
- # train and predict
- if train_status == 0:
- if cap_num >= class_num + sample_num:
- img = draw_string(img, 30, 100, "training...", color=lcd.WHITE,scale=1, bg=lcd.RED)
- lcd.display(img)
- classifier.train()
- train_status = 1
- else:
- res_index = -1
- try:
- res_index, min_dist = classifier.predict(img)
- except Exception as e:
- print("predict err:", e)
- if res_index >= 0 and min_dist < THRESHOLD :
- img = draw_string(img, 2, 2, class_names[res_index], color=lcd.WHITE,scale=1, bg=lcd.RED)
- else:
- img = draw_string(img, 2, 2, 'maybe {}'.format(class_names[res_index]), color=lcd.WHITE,scale=1, bg=lcd.RED)
- img = img.cut(0,0,240,240)
- lcd.display(img, oft=(0,0))
- `;
- return code;
- };
- Blockly.Blocks['ai_model_getface'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_getface);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_getface_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_getface'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'names[index] if (index != -1 and len(names) > index) else ""';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_getclass'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_model_getclass);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_getclass_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_getclass'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'class_names[res_index] if (res_index != -1 and len(class_names) > res_index) else ""';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_customized_if_got'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_customized_if_got);
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip(Blockly.Msg.ai_model_customized_if_got_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_if_got'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = 'code_customized_model';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_model_customized_get_result'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_models_from)
- .appendField(new Blockly.FieldVariable("i"), "varitem")
- .appendField(Blockly.Msg.ai_models_customized_get_result_any)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_models_customized_get_result_name, "classid"],
- [Blockly.Msg.ai_models_customized_get_result_x, "x"],
- [Blockly.Msg.ai_models_customized_get_result_y, "y"],
- [Blockly.Msg.ai_models_customized_get_result_w, "w"],
- [Blockly.Msg.ai_models_customized_get_result_h, "h"],
- [Blockly.Msg.ai_models_customized_get_result_x_center, "cx"],
- [Blockly.Msg.ai_models_customized_get_result_y_center, "cy"],
- [Blockly.Msg.ai_models_customized_get_result_count, "objnum"],
- [Blockly.Msg.ai_models_customized_get_result_index, "index"],
- [Blockly.Msg.ai_models_customized_get_result_confidence, "value"]
- ]), "get_results");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('get_results');
- var TOOLTIPS = {
- 'classid': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_name),
- 'x': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x),
- 'y': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y),
- 'w': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_w),
- 'h': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_h),
- 'cx': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x_center),
- 'cy': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y_center),
- 'objnum': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_count),
- 'index': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_index),
- 'value': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_confidence)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_model_customized_get_result'] = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var dropdown_get_results = block.getFieldValue('get_results');
- // TODO: Assemble Python into code variable.
- if (dropdown_get_results == "classid") {
- var code = 'classes_customized_model[' + variable_name + '.classid()]';
- } else if ((dropdown_get_results != "classid") && (dropdown_get_results != "cx") && (dropdown_get_results != "cy")) {
- var code = variable_name + '.' + dropdown_get_results + '()';
- } else if (dropdown_get_results == "cx") {
- var code = variable_name + ".x()+(" + variable_name + ".w()/2)";
- } else if (dropdown_get_results == "cy") {
- var code = variable_name + ".y()+(" + variable_name + ".h()/2)";
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- Blockly.Blocks['ai_model_init'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.MODELLPM)
- .appendField(new Blockly.FieldDropdown([
- ["Single Digit Recognition", "Single Digit Recognition"],
- ["Common Object Recognition", "Common Object Recognition"],
- ["Animal Recognition", "Animal Recognition"],
- ["Human Face Recognition", "Human Face Recognition"]
- ]), "DEGREE")
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_init = function(block) {
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var degee = block.getFieldValue('DEGREE');
- var _code = ""
- if (degee == "Single Digit Recognition") {
- _code += "task_mnist = kpu.load(\"/sd/models/mnist.kmodel\")\n";
- } else if (degee == "Common Object Recognition") {
- _code += "classes_animalrecognition = ['aeroplane', 'bicycle', '', 'boat', 'bottle', 'bus', 'car', '', 'chair', '', 'diningtable', '', '', 'motorbike', 'person', 'pottedplant', '', 'sofa', 'train', 'tvmonitor']\n";
- _code += "task_animalrecognition = kpu.load(\"/sd/models/object-recognition.kmodel\")\n";
- _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
- _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
- } else if (degee == "Animal Recognition") {
- _code += "classes_animalrecognition = ['', '', 'bird', '', '', '', '', 'cat', '', 'cow', '', 'dog', 'horse', '', '', '', 'sheep', '', '', '']\n";
- _code += "task_animalrecognition = kpu.load(\"/sd/models/animal-recognition.kmodel\")\n";
- _code += "anchor_animalrecognition = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n";
- _code += "a = kpu.init_yolo2(task_animalrecognition, 0.5, 0.3, 5, anchor_animalrecognition)\n";
- } else {
- _code += "task_facerecognition = kpu.load(\"/sd/models/face-recognition.kmodel\")\n";
- _code += "anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n";
- _code += "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor)\n";
- }
- return _code;
- }
- Blockly.Blocks['ai_model_img_mnist'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_SISF)
- .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_img_mnist = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "img_mnist1=" + variable_name + ".to_grayscale(1)\n"
- _code += "img_mnist2=img_mnist1.resize(28,28)\n"
- _code += "a=img_mnist2.invert()\n"
- _code += "a=img_mnist2.strech_char(1)\n"
- _code += "a=img_mnist2.pix_to_ai()\n"
- _code += "fmap_mnist=kpu.forward(task_mnist,img_mnist2)\n"
- _code += "plist_mnist=fmap_mnist[:]\n"
- _code += "pmax_mnist=max(plist_mnist)\n"
- _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n"
- return _code;
- }
- */
- /*
- Blockly.Blocks['ai_model_pmax_mnist'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIT_GTCS)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_pmax_mnist = function(block) {
- var _code = "int(pmax_mnist*100)\n"
- return _code;
- }
- Blockly.Blocks['ai_model_objectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
- .appendField(new Blockly.FieldVariable("img_objectrecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_objectrecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_aobjectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_ACOID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_aobjectrecognition = function(block) {
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_ifaobjectrecognition'] = {
- 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("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_ifaobjectrecognition = function(block) {
- // if code_objectrecognition: # if object is detected then do
- // for i_objectrecognition in code_objectrecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyobjectreocgnition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPM_GIOTCOD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyobjectreocgnition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_objectreocgnition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_cobjectrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_DCOIA)
- .appendField(new Blockly.FieldDropdown([
- ["Aeroplane", "Aeroplane"],
- ["Bicycle", "Bicycle"],
- ["Boat", "Boat"],
- ["Bus", "Bus"],
- ["Car", "Car"],
- ["Chair", "Chair"],
- ["Dining", "Dining"],
- ["Table", "Table"],
- ["Motorbike", "Motorbike"],
- ["Person", "Person"],
- ["Potted", "Potted"],
- ["Plant", "Plant"],
- ["Sofa", "Sofa"],
- ["Train", "Train"],
- ["Monitor", "Monitor"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_cobjectrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "classes_objectrecognition[i.classid()]\n"
- return _code;
- }
- Blockly.Blocks['ai_model_animalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_SMSFIOR)
- .appendField(new Blockly.FieldVariable("img_animalrecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_animalrecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_animalrecognition = kpu.run_yolo2(task_animalrecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_aanimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_AAOID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_aanimalrecognition = function(block) {
- var _code = "code_animalrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_ifanimalrecognition'] = {
- 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("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_ifanimalrecognition = function(block) {
- // if code_animalrecognition: # if object is detected then do
- // for i_animalrecognition in code_animalrecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyanimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_FPMA)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPMA_GIOTCAD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyanimalrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_animalrecognition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_canimalrecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMA_DAOIA)
- .appendField(new Blockly.FieldDropdown([
- ["Bird", "Bird"],
- ["Cat", "Cat"],
- ["Cow", "Cow"],
- ["Dog", "Dog"],
- ["Horse", "Horse"],
- ["Sheep", "Sheep"]
- ]), "DEGREE")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_canimalrecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "classes_animalrecognition[i.classid()]\n"
- return _code;
- }
- Blockly.Blocks['ai_model_facerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_SISFIF)
- .appendField(new Blockly.FieldVariable("img_facerecognition"), "varitem");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_facerecognition = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_afacerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_AFID)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_afacerecognition = function(block) {
- var _code = "code_facerecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_iffacerecognition'] = {
- 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("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_iffacerecognition = function(block) {
- // if code_facerecognition: # if face is detected then do
- // for i_facerecognition in code_facerecognition:
- // # ...
- var _code = "code_objectrecognition\n"
- return _code;
- }
- Blockly.Blocks['ai_model_xyfacerecognition'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_FPM)
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPM_G)
- .appendField(new Blockly.FieldDropdown([
- ["X-Coordinate", "x"],
- ["Y-Coordinate", "y"],
- ["Width of the Detection Box", "w"],
- ["Height of the Detection Box", "h"],
- ["Number of Objects Detected", "objnum"],
- ["Index of Object Detected", "index"],
- ["Confidence Score (%)", "value"]
- ]), "DEGREE")
- .appendField(Blockly.Msg.CAMERINITPMF_GIOTFD)
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_xyfacerecognition = function(block) {
- var degee = block.getFieldValue('DEGREE');
- var _code = "i_facerecognition." + degee + "()\n"
- return _code;
- }
- Blockly.Blocks['ai_model_kpuload'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_LCMLM);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_FSC)
- .appendField(new Blockly.FieldTextInput("/sd/models/facedetect.kmodel"), "input")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_kpuload = function(block) {
- var input = block.getFieldValue('input');
- Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
- var _code = "kpu.load(\"" + input + "\")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_inityolo2'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIINNYFNOT)
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_F)
- .appendField(new Blockly.FieldVariable("task"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT)
- .appendField(new Blockly.FieldTextInput("0"), "input")
- .appendField(Blockly.Msg.CAMERINIINNYFNOT_SPT_S)
- .appendField(new Blockly.FieldTextInput("0"), "input1");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERINITPMF_SNOAA)
- .appendField(new Blockly.FieldTextInput("5"), "input2")
- .appendField(Blockly.Msg.CAMERINITPMF_SNOAA_S)
- .appendField(new Blockly.FieldVariable("anchor"), "varitem1");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_inityolo2 = function(block) {
- var varitem = block.getFieldValue('varitem');
- var varitem1 = block.getFieldValue('varitem1');
- var input = block.getFieldValue('input');
- var input1 = block.getFieldValue('input1');
- var input2 = block.getFieldValue('input2');
- var _code = "kpu.init_yolo2(" + varitem + ", " + input + ", " + input1 + ", " + input2 + ", " + varitem1 + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_runyolo2'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY);
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY_FNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGRFRNNY_SISA)
- .appendField(new Blockly.FieldVariable("camera"), "varitem1");
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_runyolo2 = function(block) {
- var varitem = block.getFieldValue('varitem');
- var varitem1 = block.getFieldValue('varitem1');
- var _code = "kpu.run_yolo2(" + varitem + ", " + varitem1 + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_forward'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem")
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
- .appendField(new Blockly.FieldTextInput("3"), "input")
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERGFMOFNOT_SISAC)
- .appendField(new Blockly.FieldVariable("task"), "varitem1")
- this.setOutput(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_forward = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var variable_name1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
- var input = block.getFieldValue('input');
- var _code = "kpu.forward(" + variable_name + "," + variable_name1 + "," + input + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_model_deinit'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CAMERDNOT)
- .appendField(new Blockly.FieldVariable("task"), "varitem")
- .appendField(Blockly.Msg.OLCD_SETROTATION_DEGREE)
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#ee783a");
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- Blockly.Python.ai_model_deinit = function(block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var _code = "kpu.deinit(" + variable_name + ")\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_if'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF)
- .appendField(new Blockly.FieldDropdown([
- ["Red", "fcr_threshold_red"],
- ["Green", "fcr_threshold_green"],
- ["Blue", "fcr_threshold_blue"]
- ]), "color")
- .appendField(Blockly.Msg.VISION_IF_R)
- .appendField(new Blockly.FieldVariable("img"), "varitem");
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF_S)
- .appendField(new Blockly.FieldTextInput("60"), "input")
- .appendField(Blockly.Msg.HID_MOUSE_MOVE_DISTANCE);
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_if = function(block) {
- var input = block.getFieldValue('input');
- var varitem = block.getFieldValue('varitem');
- var color = block.getFieldValue('color');
- Blockly.Python.addVariable('fcr_threshold_red', 'fcr_threshold_red = (30, 100, 15, 127, 15, 127)', true);
- Blockly.Python.addVariable('fcr_threshold_green', 'fcr_threshold_green = (30, 100, -64, -8, -32, 32)', true);
- Blockly.Python.addVariable('fcr_threshold_blue', 'fcr_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
- Blockly.Python.addVariable('fcr_current_threshold', 'fcr_current_threshold = ' + color, true);
- Blockly.Python.addVariable('fcr_max_region_size_detected', 'fcr_max_region_size_detected = ' + input, true);
- var _code = "fcr_blobs = " + varitem + ".find_blobs([fcr_current_threshold], area_threshold=150)\n"
- _code += " if fcr_blobs: \n"
- _code += " for b in fcr_blobs:\n"
- _code += " if (b[2] > fcr_max_region_size_detected) or ((b[3] > fcr_max_region_size_detected)):\n"
- _code += " " + varitem + ".draw_rectangle(b[0:4])\n"
- _code += " " + varitem + ".draw_cross(b[5], b[6])\n"
- _code += " " + varitem + ".draw_rectangle(b[0], b[1]-20, b[2], 20, color=(255,255,255), fill=True)\n"
- _code += " " + varitem + ".draw_string(b[0]+2, b[1]-20+2, \"x:\"+ str(b[0]) + \", y:\"+ str(b[1]), mono_space=False, color=(0,0,0), scale=2)\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_gd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_IF_C)
- .appendField(new Blockly.FieldDropdown([
- ["X Coordinate", "0"],
- ["Y Coordinate", "1"],
- ["Width", "2"],
- ["Height", "3"],
- ]), "dropdown");
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_gd = function(block) {
- var d = block.getFieldValue('dropdown');
- var _code = "b[" + d + "]\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_dif'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.CONTROLS_IF_MSG_IF)
- .appendField(new Blockly.FieldDropdown([
- ["Red", "fcr_threshold_red"],
- ["Green", "fcr_threshold_green"],
- ["Blue", "fcr_threshold_blue"]
- ]), "color")
- .appendField(Blockly.Msg.VISION_CIF)
- .appendField(new Blockly.FieldVariable("img"), "varitem");
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_dif = function(block) {
- var color = block.getFieldValue('color');
- var varitem = block.getFieldValue('varitem');
- Blockly.Python.addVariable('fc_threshold_red', 'fc_threshold_red = (30, 100, 15, 127, 15, 127)', true);
- Blockly.Python.addVariable('fc_threshold_green', 'fc_threshold_green = (30, 100, -64, -8, -32, 32)', true);
- Blockly.Python.addVariable('fc_threshold_blue', 'fc_threshold_blue = (45, 65, -20, 30, -60, -20)', true);
- Blockly.Python.addVariable('fc_color_threshold', 'fc_color_threshold = ' + color, true);
- Blockly.Python.addVariable('fc_bounding_box_color', 'fc_bounding_box_color = (200,200,200)', true);
- var _code = "def fc_expand_roi(roi):\n"
- _code += " extra = 5\n"
- _code += " win_size = (224, 224)\n"
- _code += " (x, y, width, height) = roi\n"
- _code += " fc_new_roi = [x-extra, y-extra, width+2*extra, height+2*extra]\n"
- _code += "\n"
- _code += " if fc_new_roi[0] < 0:\n"
- _code += " fc_new_roi[0] = 0\n"
- _code += " if fc_new_roi[1] < 0:\n"
- _code += " fc_new_roi[1] = 0\n"
- _code += " if fc_new_roi[2] > win_size[0]:\n"
- _code += " fc_new_roi[2] = win_size[0]\n"
- _code += " if fc_new_roi[3] > win_size[1]:\n"
- _code += " fc_new_roi[3] = win_size[1]\n"
- _code += "\n"
- _code += " return tuple(fc_new_roi)\n"
- Blockly.Python.addFunction("fc_expand_roi", _code);
- _code = "fc_blobs = " + varitem + ".find_blobs([fc_color_threshold], area_threshold=150)\n"
- " if fc_blobs:\n"
- " for fc_blob in fc_blobs:\n"
- " fc_is_circle = False\n"
- " fc_max_circle = None\n"
- " fc_max_radius = -1\n"
- " fc_new_roi = fc_expand_roi(fc_blob.rect())\n"
- "\n"
- " for fc_c in " + varitem + ".find_circles(threshold = 2000, x_margin = 20, y_margin = 20, r_margin = 10, roi=fc_new_roi):\n"
- " fc_is_circle = True\n"
- " # " + varitem + ".draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255))\n"
- " if fc_c.r() > fc_max_radius:\n"
- " fc_max_radius = fc_c.r()\n"
- " fc_max_circle = fc_c\n"
- " if fc_is_circle:\n"
- " " + varitem + ".draw_rectangle(fc_new_roi, thickness=1)\n"
- " " + varitem + ".draw_rectangle(fc_new_roi[0], fc_new_roi[1]-20, fc_new_roi[2], 20, color=(255,255,255), fill=True)\n"
- " " + varitem + ".draw_string(fc_new_roi[0]+2, fc_new_roi[1]-20+2, \"x:\"+ str(fc_new_roi[0]) + \", y:\"+ str(fc_new_roi[1]), mono_space=False, color=(0,0,0), scale=2)\n"
- " " + varitem + ".draw_cross(fc_blob[5], fc_blob[6])\n"
- " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r(), color = fc_bounding_box_color)\n"
- " " + varitem + ".draw_circle(fc_max_circle.x(), fc_max_circle.y(), fc_max_circle.r() + 1, color = fc_bounding_box_color)\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_dgd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_CIF_D)
- .appendField(new Blockly.FieldDropdown([
- ["X Coordinate", "0"],
- ["Y Coordinate", "1"],
- ["Width", "2"],
- ["Height", "3"],
- ]), "dropdown");
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_dgd = function(block) {
- var n = block.getFieldValue('dropdown');
- var _code = "fc_new_roi[" + n + "]\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_sif'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF);
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_D)
- .appendField(new Blockly.FieldDropdown([
- ["Black", "DETECT_SINGLE_BLACK_LINE"],
- ["White", "DETECT_SINGLE_WHITE_LINE"]
- ]), "color")
- this.appendStatementInput('DO0')
- .appendField(Blockly.Msg.TOUCH_DO);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#f2a247");
- }
- };
- Blockly.Python.ai_vision_sif = function(block) {
- var color = block.getFieldValue('color');
- Blockly.Python.addVariable('ROI', "ROI = (0, 92, 224, 40)", true);
- Blockly.Python.addVariable('DETECT_SINGLE_BLACK_LINE', "[(0, 40)]", true);
- Blockly.Python.addVariable('DETECT_SINGLE_WHITE_LINE', "[(128, 255)]", true);
- Blockly.Python.addVariable('GRAYSCALE_THRESHOLD', "ROI = " + color, true);
- var _code = "def get_blob_value(blob):\n";
- _code += " return blob.w()\n";
- Blockly.Python.addFunction("get_blob_value", _code);
- _code = "def compare_blob(blob1, blob2):\n"
- _code += " comp_result = get_blob_value(blob1) - get_blob_value(blob2)\n"
- _code += " if comp_result > 3:\n"
- _code += " return 1\n"
- _code += " elif comp_result < -3:\n"
- _code += " return -1\n"
- _code += " else:\n"
- _code += " return 0\n"
- Blockly.Python.addFunction("compare_blob", _code);
- _code = "def get_direction(left_blob, right_blob):\n"
- _code += " MAX_WIDTH = 224\n"
- _code += " theta = 0.01\n"
- _code += " b = 3\n"
- _code += " x1 = left_blob.x() - int(0.5 * left_blob.w())\n"
- _code += " x2 = right_blob.x() + int(0.5 * right_blob.w())\n"
- _code += " w_left = x1\n"
- _code += " w_center = math.fabs(x2 - x1)\n"
- _code += " w_right = math.fabs(MAX_WIDTH - x2)\n"
- _code += " direct_ratio = (w_left + b + theta * w_center) / (w_left + w_right + 2 * b + 2 * theta * w_center) - 0.5\n"
- _code += " return direct_ratio\n"
- Blockly.Python.addFunction("get_direction", _code);
- _code = "def get_top2_blobs(blobs):\n"
- _code += " for blob in blobs:\n"
- _code += " pass\n"
- _code += " #print(blob)\n"
- _code += " # img.draw_rectangle(blob.rect())\n"
- _code += " if len(blobs) < 2:\n"
- _code += " return (None, None)\n"
- _code += " top_blob1 = blobs[0]\n"
- _code += " top_blob2 = blobs[1]\n"
- _code += " if compare_blob(top_blob1, top_blob2) == -1:\n"
- _code += " top_blob1, top_blob2 = top_blob2, top_blob1\n"
- _code += " for i in range(2, len(blobs)):\n"
- _code += " if compare_blob(blobs[i], top_blob1) == 1:\n"
- _code += " top_blob2 = top_blob1\n"
- _code += " top_blob1 = blobs[i]\n"
- _code += " elif compare_blob(blobs[i], top_blob2) == 1:\n"
- _code += " top_blob2 = blobs[i]\n"
- _code += " if top_blob1.cx() > top_blob2.cx():\n"
- _code += " return (top_blob2, top_blob1)\n"
- _code += " else:\n"
- _code += " return (top_blob1, top_blob2)\n"
- Blockly.Python.addFunction("get_top2_blobs", _code);
- _code = "def draw_direct(img, direct_ratio):\n"
- _code += " img.draw_circle(112, 112, 5)\n"
- _code += " img.draw_line((112, 112, int(112 + direct_ratio * 20), 112))\n"
- Blockly.Python.addFunction("draw_direct", _code);
- // # b 積木主體代碼
- _code = "img = sensor.snapshot()";
- _code += "blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi = ROI, merge = True)";
- _code += "if blobs:\n"
- _code += " left_blob, right_blob = get_top2_blobs(blobs)\n"
- _code += " if(left_blob == None or right_blob == None):\n"
- _code += " print(\"Out Of Range\")\n"
- _code += " lcd.display(img, oft=(8,8))\n"
- _code += " continue\n"
- _code += " else:\n"
- _code += " print(\"left blob\")\n"
- _code += " print(left_blob)\n"
- _code += " print(\"right blob\")\n"
- _code += " print(right_blob)\n"
- _code += " img.draw_circle(left_blob.cx(), left_blob.cy(), 3, thickness=2)\n"
- _code += " img.draw_circle(right_blob.cx(), right_blob.cy(), 3, thickness=2)\n"
- _code += " img.draw_line(left_blob.cx(), left_blob.cy(), right_blob.cx(), right_blob.cy(), thickness=2)\n"
- _code += " direct_ratio = get_direction(left_blob, right_blob)\n"
- _code += " img.draw_string(10, 33, \"%.2f\"%direct_ratio, mono_space=False)\n"
- _code += " rect_color = (0,0,255)\n"
- _code += " arrow_color = (255,255,255)\n"
- _code += " if float(direct_ratio) < -0.1:\n"
- _code += " img.draw_string(10, 10, \"Turn Left\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(207, 30,180, 30, color=arrow_color, thickness=3)\n"
- _code += " elif float(direct_ratio) > 0.1:\n"
- _code += " img.draw_string(10, 10, \"Turn Right\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(180, 30, 207, 30, color=arrow_color, thickness=3)\n"
- _code += " elif int(direct_ratio) == 0:\n"
- _code += " img.draw_string(10, 10, \"Go Straight\", scale=2, mono_space=False)\n"
- _code += " img.draw_rectangle(174,10,40,40,color=rect_color,fill=True)\n"
- _code += " img.draw_arrow(194, 40, 194, 20, color=arrow_color, thickness=3)\n"
- _code += " # 將條件積木中放置的代碼放在此處...\n"
- return _code;
- }
- Blockly.Blocks['ai_vision_sgd'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_G);
- this.appendDummyInput()
- .appendField(Blockly.Msg.VISION_SIF_GF);
- this.setOutput(true, null);
- this.setColour("#f2a247");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_vision_sgd = function(block) {
- var _code = "direct_ratio\n"
- return _code;
- }
- */
- /*
- _ ___ ____ _
- / \ |_ _| / ___| _ _ ___| |_ ___ _ __ ___
- / _ \ | | \___ \| | | / __| __/ _ \ '_ ` _ \
- / ___ \ | | ___) | |_| \__ \ || __/ | | | | |
- /_/ \_\___| |____/ \__, |___/\__\___|_| |_| |_|
- |___/
- */
- Blockly.Blocks['ai_system_restart'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_system_restart);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.ai_system_restart_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_system_restart = function(block) {
- Blockly.Python.definitions_.import_machine = "import machine";
- var _code = "machine.reset()\n"
- return _code;
- }
- // Blockly.Blocks['ai_system_gc'] = {
- // init: function() {
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.ai_system_enable_gc);
- // this.setInputsInline(false);
- // this.setPreviousStatement(true, null);
- // this.setNextStatement(true, null);
- // this.setColour("#d42b03");
- // this.setTooltip("");
- // this.setHelpUrl("");
- // }
- // };
- // Blockly.Python.ai_system_gc = function(block) {
- // Blockly.Python.definitions_.import_gc = "import gc";
- // Blockly.Python.addSetup("ai_system_gc", "gc.enable()");
- // var _code = "gc.collect()\n"
- // return _code;
- // }
- Blockly.Blocks['ai_system_run'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_system_run_python_code);
- this.appendValueInput("PATH")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_system_run_python_code_file_path);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.ai_system_run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_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['ai_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("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_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['ai_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("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_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['ai_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("#1e3283");
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python.ai_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;
- }
- /*
- __ ___ _____ _
- \ \ / (_) ___(_)
- \ \ /\ / /| | |_ | |
- \ V V / | | _| | |
- \_/\_/ |_|_| |_|
-
- */
- var K210_WIFI_COLOR = "#3062c1";
- Blockly.Blocks['k210_wifi_enable_disable'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_set_wifi)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_enabled, "1"],
- [Blockly.Msg.ai_wifi_disabled, "0"]
- ]), "de");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- var thisBlock = this;
- this.setTooltip(function() {
- var mode = thisBlock.getFieldValue('de');
- var TOOLTIPS = {
- '1': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_enabled),
- '0': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_disabled)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_enable_disable'] = function(block) {
- var dropdown_de = block.getFieldValue('de');
- // Blockly.Python.addVariable('k210_wifi_enable_disable', '', true);
- Blockly.Python.definitions_['k210_wifi_enable_disable_def'] = '' +
- 'from machine import UART\n' +
- 'import network, time\n' +
- 'from machine import UART\n' +
- 'from Maix import GPIO\n' +
- 'from fpioa_manager import fm, board_info\n' +
- '\n' +
- '# En SEP8285\n' +
- 'fm.register(8, fm.fpioa.GPIOHS0, force=True)\n' +
- '_k210_wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)\n' +
- '\n' +
- 'fm.register(0, fm.fpioa.GPIOHS1, force=True)\n' +
- '_k210wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)\n' +
- '_k210wifi_io0_en.value(0)\n' +
- '\n' +
- 'fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)\n' +
- 'fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)\n' +
- '\n' +
- '_k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- '\n' +
- 'def _k210_wifi_enable(en):\n' +
- ' global _k210_wifi_en\n' +
- ' _k210_wifi_en.value(en)\n' +
- '';
- // TODO: Assemble Python into code variable.
- if (dropdown_de == "1") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- 'time.sleep(2)\n' +
- '_k210_wifi = network.ESP8285(_k210_uart)\n' +
- '\n';
- } else if (dropdown_de == "0") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- '\n';
- }
- return code;
- };
- Blockly.Blocks['k210_wifi_scan'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_scan.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_start_wifi_scan);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan'] = function(block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_scan_def'] = '' +
- 'def wifi_deal_ap_info(info):\n' +
- ' res = []\n' +
- ' for ap_str in info:\n' +
- ' ap_str = ap_str.split(",")\n' +
- ' info_one = []\n' +
- ' for node in ap_str:\n' +
- ' if node.startswith(\'\"\'):\n' +
- ' info_one.append(node[1:-1])\n' +
- ' else:\n' +
- ' info_one.append(int(node))\n' +
- ' res.append(info_one)\n' +
- ' return res\n' +
- '';
- var code = '' +
- '_k210_ap_scan_info = _k210_wifi.scan()\n' +
- '_k210_ap_scan_info = wifi_deal_ap_info(_k210_ap_scan_info)\n' +
- '_k210_ap_scan_info.sort(key=lambda x:x[2], reverse=True) # sort by rssi\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_scan_get'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_wifi_scan_result);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan_get'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_ap_scan_info';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_connect_hotspot'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_connect_wifi);
- this.appendValueInput("ssid")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_ssid);
- this.appendValueInput("pass")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_pass);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_connect_hotspot_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_connect_hotspot'] = function(block) {
- var value_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
- var value_pass = Blockly.Python.valueToCode(block, 'pass', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_connect_hotspot_def'] = '' +
- 'def _k210_wifi_reset():\n' +
- ' global _k210_uart\n' +
- ' _k210_wifi_enable(0)\n' +
- ' time.sleep_ms(200)\n' +
- ' _k210_wifi_enable(1)\n' +
- ' time.sleep(2)\n' +
- ' _k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- ' tmp = _k210_uart.read()\n' +
- ' _k210_uart.write("AT+UART_CUR=921600,8,1,0,0\\r\\n")\n' +
- ' print(_k210_uart.read())\n' +
- ' _k210_uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240)\n' +
- ' _k210_uart.write("AT\\r\\n")\n' +
- ' tmp = _k210_uart.read()\n' +
- ' print(tmp)\n' +
- ' if not tmp.endswith("OK\\r\\n"):\n' +
- ' print("reset fail")\n' +
- ' return None\n' +
- ' try:\n' +
- ' _k210_wifi = network.ESP8285(_k210_uart)\n' +
- ' except Exception:\n' +
- ' return None\n' +
- ' return _k210_wifi\n' +
- '';
- var code = '' +
- '_k210_wifi = _k210_wifi_reset()\n' +
- 'if not _k210_wifi:\n' +
- ' raise Exception("WiFi init fail")\n' +
- '\n' +
- '_k210_wifi.connect(' + value_ssid + ',' + value_pass + ')\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_get_connection_status'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_connection_status);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_connection_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_connection_status'] = function(block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.isconnected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_get_config'] = {
- init: function() {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_wifi_info)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_get_wifi_info_name, "6"],
- [Blockly.Msg.ai_wifi_get_wifi_info_ip, "0"],
- [Blockly.Msg.ai_wifi_get_wifi_info_dns, "2"],
- [Blockly.Msg.ai_wifi_get_wifi_info_mac, "5"]
- ]), "type");
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_config_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_config'] = function(block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.ifconfig()[' + dropdown_type + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_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(['k210_wifi_cococloud_create_with_item']));
- this.setColour(K210_WIFI_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('k210_wifi_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('k210_wifi_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['k210_wifi_cococloud_create_with_container'] = {
- init: function() {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['k210_wifi_cococloud_create_with_item'] = {
- init: function() {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['k210_wifi_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 = 30)\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n'
- '';
- // cococloud_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['k210_wifi_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(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_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['k210_wifi_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(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_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];
- };
|