Git Product home page Git Product logo

arduinolist's Introduction

Arduino List

Status GitHub Issues GitHub Pull Requests License


A Simple List Implementation for Arduino

About

This is a very simple implementation of Linked Lists on C++ for Arduino. It was created for those who need to have a more flexible way to work with multiple data than regular arrays.

Arrays sometimes can be dificult to have its size increased. Using Lists you can have any number of items as your SRAM allows.

Getting Started

Here you can find how to install and know more about what is supported or not.

Installing

  1. Download the zip file
  2. Open Arduino IDE
  3. Go to "Sketch => Include Library => Add .ZIP Library..."
  4. Select the Downloaded zip file

Data types supported

  • array - LIMITED support
  • bool - Supported
  • boolean - Supported
  • byte - Supported
  • char - Supported
  • double - Supported
  • float - Supported
  • int - Supported
  • long - Supported
  • short - Supported
  • size_t - Supported
  • string - NOT supported (yet)
  • String() - NOT supported (yet)
  • unsigned char - Supported
  • unsigned int - Supported
  • unsigned long - Supported
  • void - NOT supported
  • word - Supported
  • class - ??? Not tested
  • struct - ??? Not tested

Usage

After adding the library to your Arduino IDE, include it as follow:

Basic usage

#include <List.h>

//List of ints
List<int> ids;

//List of floats
List<float> prices;

//List of strings
List<char*> names;

void setup() {
  //Adding an id
  ids.add(14);

  //adding a price
  prices.add(12.34);

  //adding a name
  names.add("John Doe");

}

void loop() {
  // put your main code here, to run repeatedly:

}

Methods

  • List: Class constructor

    • params:
      • any class type
    • return: nothing
    //Instantiating a new int List
    List<int> ids;
    
    //Instantiating a new float List
    List<float> prices;
    
    //Instantiating a new char array List
    List<char*> ids;
    
    //Instantiating a new int array List
    List<int*> ids;
    
  • add: Adding a new element to the end of the List

    • params:
      • item any - The element to insert to the list
    • return: nothing
    //Adding 123 to the end of the list
    ids.add(123);
    
  • remove: Remove the first occurence of the item in the List

    • params:
      • item any - The element to be removed from the list
    • return: nothing
    //Removing the first occurence of 123
    ids.remove(123);
    
  • removeAll: Remove all occurences of the item in the List

    • params:
      • item any - The element to be removed from the list
    • return: nothing
    //Removing all ocurrences of 123
    ids.removeAll(123);
    
  • removeAt: Remove the item on the position defined by the provided index from the List

    • params:
      • index unsigned long - The index to remove from the list
    • return: nothing
    //Removing the first element
    ids.removeAt(0);
    
  • get: Getting the element at the provided index

    • params:
      • index unsigned long - The index to retrieve the value. Must be greater than 0 and lass than the number of items in the list
    • return:
      • any - The item
    //Getting the first element
    int element = ids.get(0);
    
  • find: Find the index for the given element

    • params:
      • item any - The element to find in the list
    • return:
      • unsigned long - The index of the element. Returns -1 if not found;
    //Finding the index of 123
    int index = ids.find(123);
    
  • count: Counting the number of items in the list

    • params: none
    • return:
      • unsigned long - Number of items
    //Counting the number of itens in the list
    int size = ids.count();
    
  • toArray: Converts the list into an array

    • params:
      • &array - The array with all list items
    • return: none
    //Converting to array
    int *idsArray = new int [ids.count()];
    ids.toArray(idsArray);
    //Remember do dispose your array after use
    delete [] idsArray;
    
  • dispose: Dispose the List to free memory

    • params: none
    • return: none
    //Free memory
    ids.dispose();
    

Examples

#include "List.h"

List<int> ids;

void setup()
{
    //Starting the serial communication
    Serial.begin(9600);

    //Adding 20 numbers to the list
    for (int i = 0; i < 20; i++)
    {
        ids.add(i);
    }
    //Adding more 20 numbers to the list
    for (int i = 0; i < 20; i++)
    {
        ids.add(i);
    }
}

void loop()
{
    //Adding a single item
    ids.add(157);

    //Removing the first number 12 of the list
    ids.remove(12);

    //Removing all number 12 from the list
    ids.removeAll(12);

    //Removing the element on the position 7
    ids.removeAt(7);

    //Getting the size of the list
    int size = ids.count();

    //Looping all elements
    for (int i = 0; i < size; i++)
    {
        Serial.println(ids.get(i));
    }

    //Converting the list to an array
    int *array = ids.toArray();

    delay(10000);
}

TODO

  • Support to Strings
  • Test with struct
  • Test with class

Authors

arduinolist's People

Contributors

heliobentes avatar

Stargazers

 avatar

Watchers

 avatar

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.