Giter Club home page Giter Club logo

stratifyapi's Introduction

Stratify API

Build Coverity

Overview

  • API Documentation
  • Naming Conventions
  • Building and Installing the Stratify API

If you want to write programs using the Stratify API, you should read through the naming conventions then dive into the API documentation. Also, please look at the Stratify OS programs that are published on Github as they make use of the API.

API Documenation

If you want to write applications using the Stratify API, please see the API documentation.

Naming Conventions

Stratify API uses the following naming conventions

Classes and Namespaces

Classes use upper CamelCase with a strong preference for a single word. Namespaces use all lowercase letters and are preferably concise and a single word.

File and Directory Names

File names follow the class names and namespace names with hpp appended to header files and cpp appended to C++ source files. Namespaces have their own directories which contain the header files of all the classes in the namespace. The namespaces include a header file named after the namespace which includes all classes and declares 'using namespace '.

/*! \brief Standard Calculation and Software Routines
*
*/
namespace calc {}
#include "calc/Base64.hpp"
#include "calc/Ema.hpp"
#include "calc/Lookup.hpp"
#include "calc/Pid.hpp"
#include "calc/Rle.hpp"

using namespace calc;

In an application, #include <sapi/calc.hpp> will allow you to declare Pid objects without using calc::Pid. If you don't want to use the namespace (that is you need to avoid scope conflicts), just #include <sapi/calc/Pid.hpp> instead. All library headers (including those in Stratify API) should use this approach and should never include a namespace header (or 'using namespace') in any header file other than the namespace's hpp file. The application developer can then decide to include <sapi/calc.hpp> or the individual classes <sapi/calc/Pid.hpp>.

Methods and Functions

Methods and functions follow traditional C and C++ naming styles with lowercase and underscores to separate words. Methods and functions should start with a verb except when directly accessing a variable.

Class Object {
public:
  int amount() const { return m_amount; } //this just returns the amount (no calculating, no fetching)
  int set_amount(int v){ m_amount = v; } //this sets the value of amount
  int calculate_amount() const; //this is an action that does something
  int get_amount() const; //since this starts with a verb it needs to do something--like load amount from a file
private:
  int m_amount; //member variables have an m_ prefix
}

The above code uses set_* and get_* but not perhaps in the traditional way. If get_ is used, it implies the value is not immediately available and must be loaded from somewhere. The convention used by the method amount() (no action word) is used if the value is immediately ready or ready with a trivial calculation such as

area(){ return m_width*m_height; }

References and Pointers

Parameters passed to methods and functions should be passed as references unless a pointer is more appropriate. For example, if the item is a member of an array then a pointer would be appropriate. However, if the item will be operated on as a stand alone entity, a reference is preferred. Also if the object uses only read-only methods, a const reference is best.

void copy_memory(const char * src, char * dest, int nbytes); //a pointer here is best because src points to the data
void print_string(const String & str); //this is better as a reference because we are just reading str

Pointers are also appropriate if nullptr is a valid value even if there is only one item.

Variables and Member Variables

Variables are named the same way functions are except they don't start with an action verb. Member variables have m_ prefixed to the description variable name. See the amount() and m_amount example above.

Type Definitions

To maintain compatibility and interoperability with C (i.e. Stratify OS), type definitions are declared in .h files and follow C naming conventions. Whenever typedef is used, _t should be appended to the type name like size_t or pthread_t.

Enums and Macros

Macros are written in all caps with words separated by underscores. Enums are written in all lower case separated by underscores and prefixed the the singular of the enum name (enum names should be the plural version). Enums are preferred to macros when defining constants because this allows them to auto-populate when using code completion software. Here is an example from the fs::File class:

enum flags {
    flag_read_only /*! Open as read-only */ = LINK_O_RDONLY,
    flag_write_only /*! Open as write-only */ = LINK_O_WRONLY,
    flag_create /*! Create when opening (files) */ = LINK_O_CREAT,
    flag_truncate /*! Truncate when opening (files) */ = LINK_O_TRUNC
};

Building the Stratify API

The latest API is built and distributed with the Stratify Labs SDK. You only need to build and install the source code if you want to debug, contribute new features, or equip your local SDK with a previous or customized version.

Here are the steps to build and install the API using the Stratify Labs command line tool sl.

sl sdk.install # install the SDK (if it isn't already installed)
sl sdk.bootstrap
sl sdk.update # this will pull, build, and install StratifyOS and StratifyAPI

stratifyapi's People

Contributors

charanyaarvind avatar shomagan avatar tyler-gilbert avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

stratifyapi's Issues

Add ui::ButtonDriver Class

This will be a button class that will use a /dev/button device. This will allow the kernel to have greater control over the buttons.

Inconsistent naming of inherited objects

The last word should be the definitive/dominant interface. The first word should be a modifier.

Like "elephant" vs "elephant seal". In "elephant seal", the animal is much more seal than elephant.

This allows the class to be read more naturally.

Are these right?

class DeviceSignal : public sys::Signal //This is a Signal used for devices
class DisplayDevice: public Display, public Device //this is both but probably OK as Device being dominant
class ElementLinked : public Element //should probably be LinkedElement since this is an Element
class MenuList : public ui::List //this is right
class TabProgress : public Tab //this should be ProgressTab
class TabText : public Tab //this should be TextTab
class TabTime : public Tab //this should be TimeTab

These changes can be made without breaking the API by having the old name be a direct descendant of the new name.

Add msec, usec, or sec to setting time values, keep backward compatiblity

Maybe even create an elapsed time class.

ElapsedTime {
public:
    void set_msec(u32 msec);
    void set_usec(u32 usec);
    void set_sec(u32 sec);

   u32 usec() const { return m_value_usec; }
   u32 msec() const { return m_value_msec; }

private:
   u32 m_value_usec;

Example in sm::EventLoopAttr

	u16 hibernation_threshold() const { return m_attr.hibernation_threshold; }

should be

	u16 hibernation_threshold_msec() const { return m_attr.hibernation_threshold_msec; }
	u16 hibernation_threshold() const { return m_attr.hibernation_threshold_msec; } //not documented -- deprecated

Or

	ElapsedTime hibernation_threshold() const { return ElapsedTime(m_attr.hibernation_threshold_msec); }

Add ui::Led Class

This class will work similar to button but be used to turn LEDs on/off and flash. It will use an update() method and internal timers.

The class will be pure virtual and require sub-classes to provide a set_active(bool) method to turn the LED on and off.

Add a simple state machine in sm namespace

The eventloop is for more complex operations where each event handler is it's own class.

The state machine is much simpler where the machine just runs private methods to execute the state machine.

Add chrono namespace

ClockTime, MicroTime, ClockTimer, MicroTimer, and Time will all be in this namespace.

sys::Time and sys::Timer will be typedef to chrono::Time chrono::MicroTimer respectively.

Add base class for all objects

Use namespace sapi and object Api.

namespace sapi {

class Api {
public:
  Api(); //debug when objects are created
  ~Api(); //debug when objects are destroyed
  const char * get_error(); //return "none" if error is never set
  //add some debugging of every object
  const char * name(); //return a "unknown" if name is not set

protected:
   void set_name(const char * name);
   void set_error(const char * error);
private:
  char * m_error; //dynamically allocate as needed
  char * m_name; //dynamically allocate as needed

};


}

Add Classes for Hardware Attributes

Add classes for hardware attributes. For example:

class UartAttr {
public:
	u8 port() const { return m_port; }
	const uart_attr_t & attr() const { return m_attr; }
	mcu_pin_t tx() const { return m_attr.pin_assignment.tx; }
	mcu_pin_t rx() const { return m_attr.pin_assignment.rx; }
	mcu_pin_t cts() const { return m_attr.pin_assignment.cts; }
	mcu_pin_t rts() const { return m_attr.pin_assignment.rts; }


	void set_tx(const mcu_pin_t & pin){ m_attr.pin_assignment.tx = pin;}
	void set_rx(const mcu_pin_t & pin){ m_attr.pin_assignment.rx = pin;}
	void set_cts(const mcu_pin_t & pin){ m_attr.pin_assignment.cts = pin;}
	void set_rts(const mcu_pin_t & pin){ m_attr.pin_assignment.rts = pin;}
	void set_port(u8 p){ m_port = p; }
	void set_flags(u32 flags){ m_attr.o_flags = flags; }
	void set_freq(u32 f){ m_attr.freq = f; }
	void set_width(u32 w){ m_attr.width = w; }
private:
	u8 m_port;
	uart_attr_t m_attr;
};

Then add methods to sys::Cli to parse and handle attributes for each hardware type.

Create MicroTimer and ClockTimer classes

MicroTimer will be the same as Timer (based on microseconds). Timer will be deprecated.

ClockTimer will be based on a new class called ClockTime (64-bit with accuracy down to the microsecond).

ClockTime will have a similar API to MicroTime but will work with days, weeks, and months.

Time will still be based on time_t. 32-bit second accurate timer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.