How do libraries work and specifically what are they?

28 Feb 2014

What is a library and how would I make one work in my code, for example a webserver with JavaScript.

10 Mar 2014

It's really hard to digest your question in it's current form.

Webserver is backend and JavaScript is frontend, something is messed up here...

14 Mar 2014

This is in the wrong section, but I suppose if you don't know what a library is, then you can't be blamed :D

A library is a related collection of functions and data structures, that provide a coherent building block for further use.

So for example, each of the components in mbed's "Components" database is likely to come with a library for its use.

A touchscreen might come with functions such as:

   // drawing commands
   void drawCircle(uint16_t xPos, uint16_t yPos, uint16_t radius, uint16_t color);
   void drawPixel(uint16_t xPos, uint16_t yPos, uint16_t color);
   void clearScreen();
   void setBackgroundColor(uint16_t color);

This is from a touchscreen library I wrote for 4D systems touchscreens. The header file can be observed here:

http://mbed.org/users/ashleymills/code/TouchScreen/file/edf5f3d8645e/TouchScreen.h

So you can see that the library contains all the things that you might expect to do with a touchscreen. By abstracting them into a library, and in this case a class, it makes logical sense and I can just "include" this library as a building block whenever I need to use the touchscreen.

Most of the component libraries are actually "drivers" as well, in that they are used to drive a piece of hardware, but a library does not have to be a driver.

Here are some standard c libraries:

  1. math.h - floating point mathematical library
  2. fcntl.h - file control
  3. stdio.h - provides printf, fprintf and so on with formatted out conversion

These libraries are usually bundled into a one big library called the standard c library, but the individual library definitions are still in separate header files for orginisational efficiency.

To give an example of where you might create a library, imagine you are writing a program to compute various statistics, and you have some special functions for computing the mean, std, and so on for your data.

Perhaps you would create a new "stats" library to provide these functions, because you want to keep them logically separate, but also because you might want to re-use such functionality in the future.

Going back to your orginal question. The webserver and javasript bit.

Well, mbed certainly has the ability to be a webserver, and here is a library that provides functionality for serving HTTP:

Import libraryHTTPD

HTTP Server, WebSocket support

You might need to use a bunch of other libraries in combination with this (for example the library for the ethernet controller and so on), but this is why the person usually writes an example program too, such as:

Import programHTTPD_sample

HTTP Server, WebSocket support

So you can see which other libraries are needed, and how to use them together to create a webserver.

As for javascript, someone has actually ported a tiny javascript interpreter to mbed:

Import programTinyJS

TinyJS on mbed. TinyJS is very simple JavaScript engine.

Of course, normally, javascript is used on the client side, unless you want to use a web application framework like "nodejs" which is based on javascript. But there isn't going to be support for a full V8 engine or something like that. There simply isn't enough resources.

You have to bear in mind that the functionality of an embedded controller is very limited in terms of memory and CPU, so you shouldn't expect to be able to run the kind of webserver you would run on a PC.

But with some ingenuity, I bet you can achieve a lot more than people think you can.

Ashley

You need to log in to post a reply