blob: 1da2acf8b6edcc512463f9b874c80878d4c0926f [file] [log] [blame]
<!DOCTYPE html>
<html class="no-js">
<head lang="en-us">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<title>Introduction - Eclipse Mita</title>
<meta name="generator" content="Hugo 0.54.0" />
<meta name="description" content="The documentation of Eclipse Mita.">
<link rel="canonical" href="../../language/introduction/">
<meta property="og:url" content="/language/introduction/">
<meta property="og:title" content="Eclipse Mita">
<meta property="og:image" content="/images/mita.svg">
<meta name="apple-mobile-web-app-title" content="Eclipse Mita">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="shortcut icon" type="image/x-icon" href="../../images/favicon.ico">
<link rel="icon" type="image/x-icon" href="../../images/favicon.ico">
<style>
@font-face {
font-family: 'Icon';
src: url('/fonts/icon.eot');
src: url('/fonts/icon.eot')
format('embedded-opentype'),
url('/fonts/icon.woff')
format('woff'),
url('/fonts/icon.ttf')
format('truetype'),
url('/fonts/icon.svg')
format('svg');
font-weight: normal;
font-style: normal;
}
</style>
<link rel="stylesheet" href="../../stylesheets/application.css">
<link rel="stylesheet" href="../../stylesheets/temporary.css">
<link rel="stylesheet" href="../../stylesheets/palettes.css">
<link rel="stylesheet" href="../../stylesheets/highlight/highlight.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ubuntu:400,700|Ubuntu&#43;Mono">
<style>
body, input {
font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
}
pre, code {
font-family: 'Ubuntu Mono', 'Courier New', 'Courier', monospace;
}
</style>
<link rel="stylesheet" href="../../css/custom.css">
<script src="../../javascripts/modernizr.js"></script>
</head>
<body class="palette-primary-blue-grey palette-accent-light-blue">
<div class="backdrop">
<div class="backdrop-paper"></div>
</div>
<input class="toggle" type="checkbox" id="toggle-drawer">
<input class="toggle" type="checkbox" id="toggle-search">
<label class="toggle-button overlay" for="toggle-drawer"></label>
<header class="header">
<nav aria-label="Header">
<div class="bar default">
<div class="button button-menu" role="button" aria-label="Menu">
<label class="toggle-button icon icon-menu" for="toggle-drawer">
<span></span>
</label>
</div>
<div class="stretch">
<div class="title">
Introduction
</div>
</div>
<div class="button button-twitter" role="button" aria-label="Twitter">
<a href="https://twitter.com/eclipse_mita" title="@eclipse_mita on Twitter" target="_blank" class="toggle-button icon icon-twitter"></a>
</div>
<div class="button button-github" role="button" aria-label="GitHub">
<a href="https://github.com/eclipse/mita" title="@eclipse/mita on GitHub" target="_blank" class="toggle-button icon icon-github"></a>
</div>
</div>
<div class="bar search">
<div class="button button-close" role="button" aria-label="Close">
<label class="toggle-button icon icon-back" for="toggle-search"></label>
</div>
<div class="stretch">
<div class="field">
<input class="query" type="text" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck>
</div>
</div>
<div class="button button-reset" role="button" aria-label="Search">
<button class="toggle-button icon icon-close" id="reset-search"></button>
</div>
</div>
</nav>
</header>
<main class="main">
<div class="drawer">
<nav aria-label="Navigation">
<a href="../../" class="project">
<div class="banner">
<div class="logo">
<img src="../../images/mita.svg">
</div>
<div class="name">
<strong>Eclipse Mita </strong>
</div>
</div>
</a>
<div class="scrollable">
<div class="wrapper">
<div class="toc">
<ul>
<li>
<a title="Download" href="http://github.com/eclipse/mita">
Download
</a>
</li>
<li>
<a title="Concepts" href="../../concepts/">
Concepts
</a>
</li>
<li>
<span class="section">Language</span>
<ul>
<a class="current" title="Introduction" href="../../language/introduction/">
Introduction
</a>
<ul id="scrollspy">
</ul>
<a title="Basics" href="../../language/basics/">
Basics
</a>
<a title="Packages" href="../../language/packages/">
Packages
</a>
<a title="System Setup" href="../../language/setup/">
System Setup
</a>
<a title="Types" href="../../language/types/">
Types
</a>
<a title="Arrays" href="../../language/arrays/">
Arrays
</a>
<a title="Functions" href="../../language/functions/">
Functions
</a>
<a title="Foreign Function Interface" href="../../language/foreignfunctioninterface/">
Foreign Function Interface
</a>
<a title="Events" href="../../language/events/">
Events
</a>
<a title="Exceptions" href="../../language/exceptions/">
Exceptions
</a>
</ul>
</li>
<li>
<span class="section">Platforms</span>
<ul>
<a title="Integrators Guide" href="../../platforms/integratorsguide/">
Integrators Guide
</a>
<a title="Arduino Uno" href="../../platforms/arduinouno/">
Arduino Uno
</a>
<a title="XDK110" href="../../platforms/xdk110/">
XDK110
</a>
<a title="x86" href="../../platforms/x86/">
x86
</a>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
<article class="article">
<div class="wrapper">
<p>Mita is a programming language that is focused on making Internet-Of-Things things easier to program, especially for developers without embedded development background.
Its language design and feature-set are meant to make anyone coming from a world of Javascript, Typescript, Java, Swift or Go feel right at home.
Compared to C you do not have to care about allocating memory, strings behave naturally, as do arrays.
Featuring powerful abstractions and a model-driven system configuration approach we can often tell at compile time if something will not work.
At the core of Mita is a platform model which describes what resources (think connectivity, sensors or low-level hardware interfaces) you have to work with.</p>
<p>All Mita code compiles to C code. This is a very powerful feature.
It allows developers to inspect the generated code, learn from it and when they hit that inevitable glass ceiling continue where they left off, but in C.
Further, once you leave the prototyping phase or feel that you need more control over your system, you can always fall back to the C level and go from there.</p>
<h2 id="anatomy-of-an-mita-program">Anatomy of an Mita program</h2>
<p>Mita programs live in files ending with <code>.x</code>, for example <code>application.x</code>. Let&rsquo;s have a look at an example which implements a simple Bluetooth enabled shock detector:</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="kr">package</span> <span class="nx">main</span><span class="p">;</span>
<span class="kr">import</span> <span class="nx">platforms</span><span class="p">.</span><span class="nx">xdk110</span><span class="p">;</span>
<span class="nx">setup</span> <span class="nx">smartphone</span> : <span class="kt">BLE</span> <span class="p">{</span>
<span class="nx">deviceName</span> <span class="o">=</span> <span class="s2">&#34;HelloWorld&#34;</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">shockDetected</span> <span class="o">=</span> <span class="nx">bool_characteristic</span><span class="p">(</span><span class="nx">UUID</span><span class="o">=</span><span class="mh">0xCAFE</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">every</span> <span class="nx">accelerometer</span><span class="p">.</span><span class="nx">activity</span> <span class="p">{</span>
<span class="k">if</span><span class="p">(</span><span class="nx">accelerometer</span><span class="p">.</span><span class="nx">magnitude</span><span class="p">.</span><span class="nx">read</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">5000</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">smartphone</span><span class="p">.</span><span class="nx">shockDetected</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></div>
<p>Now let&rsquo;s go over this code and understand it bit by bit.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="kr">package</span> <span class="nx">main</span><span class="p">;</span></code></pre></div>
<p>All Mita code has a package associated with it, which means that the first line of any <code>.x</code> file is always a <em>package</em> statement which tells the compiler to which package the subsequent code belongs to.
There is no fixed naming that you would need to adhere to, merely conventions. For example, the main application logic is typically located in a package named <em>main</em>. More details about packages can be found in the [language section].</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="kr">import</span> <span class="nx">platforms</span><span class="p">.</span><span class="nx">xdk110</span><span class="p">;</span></code></pre></div>
<p>Much like packages are defined, they need to be imported so that they can be used. This import is a special one, and the only import that every <code>.x</code> file needs to have. It imports the platform for which we are writing our program.
By convention platforms are located in the <em>platforms</em> package.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="nx">setup</span> <span class="nx">smartphone</span> : <span class="kt">BLE</span> <span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span></code></pre></div>
<p>Setup blocks configure the resources we have at our disposal, in this case a Bluetooth Low Energy connectivity. Which resources exist depends on the platform you have previously imported - not every hardware has the same radio or sensors installed, for example. The content of the setup block depends on the resource being set up. Whenever you are inside a setup block, you can use auto-complete (<code>Ctrl+Space</code>)
to find out what you can configure or do next.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="nx">every</span> <span class="nx">accelerometer</span><span class="p">.</span><span class="nx">activity</span> <span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span></code></pre></div>
<p>Most of their time IoT devices spend sleeping in some low-power mode. Every now and then, triggered by time or some external event, they wake up do some sensing, computation and send data using wireless connectivity.
Mita is an event based language which maps nicely to the way such IoT devices work. Using the <em>every</em> keyword we can react to events or time. Instead of <code>every accelerometer.activity { ... }</code> we could also write <code>every 100 milliseconds { ... }</code> which would simply execute every 100 milliseconds.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="k">if</span><span class="p">(</span><span class="nx">accelerometer</span><span class="p">.</span><span class="nx">magnitude</span><span class="p">.</span><span class="nx">read</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">5000</span><span class="p">)</span> <span class="p">{</span>
<span class="p">...</span>
<span class="p">}</span></code></pre></div>
<p>Sensor data and other resources providing data at runtime (we call them <em>modalities</em>) can be read at any time. We did not have to set up the accelerometer, the platform told the compiler that such a sensor exists and has a magnitude that can be read. Should you want to change the default values of the accelerometer after all, you can always use a <code>setup accelerometer { ... }</code> block to do that.</p>
<div class="highlight"><pre class="chroma"><code class="language-TypeScript" data-lang="TypeScript"><span class="nx">smartphone</span><span class="p">.</span><span class="nx">shockDetected</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span></code></pre></div>
<p>Earlier in the setup block for the Bluetooth Low Energy resource we configured a <a href="https://en.wikipedia.org/wiki/Bluetooth_Low_Energy#Software_model">BLE GATT characteristic</a>, which we named <code>shockDetected</code>. To send out data using Bluetooth all we need to do is to write to this characteristic using the built-in <code>write</code> function. All the nitty details of how to use the underlying Bluetooth stack are handled by the platform, not by you, the developer.</p>
<p>That concludes our first tour of an Mita program. There is a lot more to learn though. The following sections give an overview of the basic concepts, followed by more in-depth chapters of specific language elements.</p>
<aside class="copyright" role="note">
<div class="incubation">
<a href="https://www.eclipse.org/projects/what-is-incubation.php" target="_blank">
<img src="../../images/incubating.png" />
</a>
</div>
<div class="logo">
<a href="https://www.eclipse.org" target="_blank">
<img src="../../images/eclipse_foundation_logo.svg" />
</a>
</div>
<p class="notice">
Documentation built with
<a href="https://www.gohugo.io" target="_blank">Hugo</a>
using the
<a href="http://github.com/digitalcraftsman/hugo-material-docs" target="_blank">Material</a> theme.
</p>
<p class="quickLinks">
<a href="http://www.eclipse.org/legal/privacy.php" target="_blank">
&gt; Privacy Policy
</a>
<a href="http://www.eclipse.org/legal/termsofuse.php" target="_blank">
&gt; Terms of Use
</a>
<a href="http://www.eclipse.org/legal/copyright.php" target="_blank">
&gt; Copyright Agent
</a>
<a href="http://www.eclipse.org/legal" target="_blank">
&gt; Legal
</a>
<a href="https://eclipse.org/security" target="_blank">
&gt; Report a Vulnerability
</a>
</p>
</aside>
</div>
</article>
<div class="results" role="status" aria-live="polite">
<div class="scrollable">
<div class="wrapper">
<div class="meta"></div>
<div class="list"></div>
</div>
</div>
</div>
</main>
<script>
var base_url = '';
var repo_id = '';
</script>
<script src="../../javascripts/application.js"></script>
<script>
/* Add headers to scrollspy */
var headers = document.getElementsByTagName("h2");
var scrollspy = document.getElementById('scrollspy');
if(scrollspy) {
if(headers.length > 0) {
for(var i = 0; i < headers.length; i++) {
var li = document.createElement("li");
li.setAttribute("class", "anchor");
var a = document.createElement("a");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", headers[i].innerHTML);
a.innerHTML = headers[i].innerHTML;
li.appendChild(a)
scrollspy.appendChild(li);
}
} else {
scrollspy.parentElement.removeChild(scrollspy)
}
/* Add permanent link next to the headers */
var headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
for(var i = 0; i < headers.length; i++) {
var a = document.createElement("a");
a.setAttribute("class", "headerlink");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", "Permanent link")
a.innerHTML = "#";
headers[i].appendChild(a);
}
}
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>