├── ButtonEvent
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ ├── SimpleButton
│ │ └── SimpleButton.ino
│ └── AnalogButton
│ │ └── AnalogButton.ino
└── src
│ ├── ButtonEvent.h
│ └── ButtonEvent.cpp
├── AnalogEvent
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ └── Potentiometer
│ │ └── Potentiometer.ino
└── src
│ ├── AnalogEvent.h
│ └── AnalogEvent.cpp
├── AdvancedSerial
├── clientapi
│ └── dotnet
│ │ ├── dotnet.suo
│ │ ├── app.config
│ │ ├── Release
│ │ ├── dotnet.dll
│ │ ├── LCDWrite.exe
│ │ └── dotnet.XML
│ │ ├── examples
│ │ └── LCDWrite
│ │ │ ├── app.config
│ │ │ ├── LCDWrite.suo
│ │ │ ├── LCDWrite.csproj.user
│ │ │ ├── Properties
│ │ │ └── AssemblyInfo.cs
│ │ │ ├── LCDWrite.csproj
│ │ │ └── LCDWrite.cs
│ │ ├── dotnet.sln
│ │ ├── Properties
│ │ └── AssemblyInfo.cs
│ │ ├── AdvancedSerialClient.cs
│ │ ├── dotnet.csproj
│ │ └── SerialProtocol.cs
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ └── LCDWriter
│ │ └── LCDWriter.ino
└── src
│ ├── AdvancedSerial.h
│ └── AdvancedSerial.cpp
├── TimedEvent
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ ├── Timers
│ │ └── Timers.ino
│ └── RandomBlink
│ │ └── RandomBlink.ino
└── src
│ ├── TimedEvent.h
│ └── TimedEvent.cpp
├── LedControl
├── examples
│ └── SimpleBlinker
│ │ └── SimpleBlinker.ino
├── keywords.txt
├── library.properties
├── README.adoc
└── src
│ ├── LedControl.h
│ └── LedControl.cpp
├── Properties
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ └── SimpleVariables
│ │ └── SimpleVariables.ino
└── src
│ ├── Properties.h
│ └── Properties.cpp
├── RTCTimedEvent
├── keywords.txt
├── library.properties
├── README.adoc
├── examples
│ └── RTCTimer
│ │ └── RTCTimer.ino
└── src
│ ├── RTCTimedEvent.h
│ └── RTCTimedEvent.cpp
└── README.md
/ButtonEvent/keywords.txt:
--------------------------------------------------------------------------------
1 | ButtonEvent KEYWORD3
2 | ButtonEventClass KEYWORD3
3 | ButtonInformation KEYWORD2
4 | addButton KEYWORD2
5 | loop KEYWORD2
--------------------------------------------------------------------------------
/AnalogEvent/keywords.txt:
--------------------------------------------------------------------------------
1 | AnalogEvent KEYWORD3
2 | AnalogEventClass KEYWORD3
3 | AnalogPortInformation KEYWORD2
4 | addAnalogPort KEYWORD2
5 | loop KEYWORD2
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/dotnet.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/renatoferreirarenatoferreira/ebl-arduino/HEAD/AdvancedSerial/clientapi/dotnet/dotnet.suo
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/Release/dotnet.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/renatoferreirarenatoferreira/ebl-arduino/HEAD/AdvancedSerial/clientapi/dotnet/Release/dotnet.dll
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/Release/LCDWrite.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/renatoferreirarenatoferreira/ebl-arduino/HEAD/AdvancedSerial/clientapi/dotnet/Release/LCDWrite.exe
--------------------------------------------------------------------------------
/AdvancedSerial/keywords.txt:
--------------------------------------------------------------------------------
1 | AdvancedSerial KEYWORD3
2 | AdvancedSerialClass KEYWORD3
3 | AdvancedSerialMessage KEYWORD2
4 | setReceiver KEYWORD2
5 | send KEYWORD2
6 | loop KEYWORD2
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/LCDWrite.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/renatoferreirarenatoferreira/ebl-arduino/HEAD/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/LCDWrite.suo
--------------------------------------------------------------------------------
/TimedEvent/keywords.txt:
--------------------------------------------------------------------------------
1 | TimedEvent KEYWORD3
2 | TimedEventClass KEYWORD3
3 | TimerInformation KEYWORD2
4 | addTimer KEYWORD2
5 | start KEYWORD2
6 | stop KEYWORD2
7 | loop KEYWORD2
8 | setInterval KEYWORD2
9 |
--------------------------------------------------------------------------------
/LedControl/examples/SimpleBlinker/SimpleBlinker.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void setup() {
4 | LedControl.startBlink(9,1000,500);
5 | LedControl.startBlink(8,200);
6 | }
7 |
8 | void loop() {
9 | LedControl.loop();
10 | }
11 |
--------------------------------------------------------------------------------
/Properties/keywords.txt:
--------------------------------------------------------------------------------
1 | Properties KEYWORD3
2 | PropertiesClass KEYWORD3
3 | PropertyInformation KEYWORD2
4 | load KEYWORD2
5 | save KEYWORD2
6 | flush KEYWORD2
7 | set KEYWORD2
8 | getInt KEYWORD2
9 | getLong KEYWORD2
10 | get KEYWORD2
--------------------------------------------------------------------------------
/LedControl/keywords.txt:
--------------------------------------------------------------------------------
1 | LedControl KEYWORD3
2 | LedControlClass KEYWORD3
3 | LedInformation KEYWORD2
4 | turnOn KEYWORD2
5 | turnOff KEYWORD2
6 | addButton KEYWORD2
7 | turnPercent KEYWORD2
8 | blink KEYWORD2
9 | startBlink KEYWORD2
10 | stopBlink KEYWORD2
--------------------------------------------------------------------------------
/RTCTimedEvent/keywords.txt:
--------------------------------------------------------------------------------
1 | RTCTimedEvent KEYWORD3
2 | RTCTimedEventClass KEYWORD3
3 | RTCTimerInformation KEYWORD2
4 | addTimer KEYWORD2
5 | loop KEYWORD2
6 | clear KEYWORD2
7 | readRTC KEYWORD2
8 | writeRTC KEYWORD2
9 | initialCapacity KEYWORD2
10 | time KEYWORD2
--------------------------------------------------------------------------------
/LedControl/library.properties:
--------------------------------------------------------------------------------
1 | name=LedControl
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Asynchronous led controller.
6 | paragraph=
7 | category=Display
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/Properties/library.properties:
--------------------------------------------------------------------------------
1 | name=Properties
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Data storage handler.
6 | paragraph=
7 | category=Data Storage
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/TimedEvent/library.properties:
--------------------------------------------------------------------------------
1 | name=TimedEvent
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Event scheduler based on time intervals.
6 | paragraph=
7 | category=Timing
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/LCDWrite.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 | -p com23 -off
4 |
5 |
--------------------------------------------------------------------------------
/Properties/README.adoc:
--------------------------------------------------------------------------------
1 | = Properties Library for Arduino =
2 |
3 | Data storage handler.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/AnalogEvent/library.properties:
--------------------------------------------------------------------------------
1 | name=AnalogEvent
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Event handler to listen for analog changes.
6 | paragraph=
7 | category=Device Control
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/ButtonEvent/library.properties:
--------------------------------------------------------------------------------
1 | name=ButtonEvent
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Event handler for analog and digital buttons.
6 | paragraph=
7 | category=Device Control
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/LedControl/README.adoc:
--------------------------------------------------------------------------------
1 | = LedControl Library for Arduino =
2 |
3 | Asynchronous led controller.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/AdvancedSerial/library.properties:
--------------------------------------------------------------------------------
1 | name=AdvancedSerial
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Serial communication library with a C# .Net client.
6 | paragraph=
7 | category=Communication
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/TimedEvent/README.adoc:
--------------------------------------------------------------------------------
1 | = TimedEvent Library for Arduino =
2 |
3 | Event scheduler based on time intervals.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/AnalogEvent/README.adoc:
--------------------------------------------------------------------------------
1 | = AnalogEvent Library for Arduino =
2 |
3 | Event handler to listen for analog changes.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/ButtonEvent/README.adoc:
--------------------------------------------------------------------------------
1 | = ButtonEvent Library for Arduino =
2 |
3 | Event handler for analog and digital buttons.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/RTCTimedEvent/library.properties:
--------------------------------------------------------------------------------
1 | name=RTCTimedEvent
2 | version=0.1.0
3 | author=Renato A. Ferreira
4 | maintainer=Renato A. Ferreira
5 | sentence=Event scheduler based on real time clocks, like unix cron scheduler.
6 | paragraph=
7 | category=Timing
8 | url=https://github.com/renatoferreirarenatoferreira/ebl-arduino
9 | architectures=*
10 |
--------------------------------------------------------------------------------
/AdvancedSerial/README.adoc:
--------------------------------------------------------------------------------
1 | = AdvancedSerial Library for Arduino =
2 |
3 | Serial communication library with a C# .Net client.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/RTCTimedEvent/README.adoc:
--------------------------------------------------------------------------------
1 | = RTCTimedEvent Library for Arduino =
2 |
3 | Event scheduler based on real time clocks, like unix cron scheduler.
4 |
5 | Part of ebl-arduino project.
6 |
7 | Project URL on GitHub: https://github.com/renatoferreirarenatoferreira/ebl-arduino
8 |
9 | == License ==
10 |
11 | Copyright (c) 2011, Renato A. Ferreira
12 | All rights reserved.
--------------------------------------------------------------------------------
/AnalogEvent/examples/Potentiometer/Potentiometer.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void setup() {
4 | AnalogEvent.addAnalogPort(1, //potentiometer pin
5 | onChange, //onChange event function
6 | 3); //hysteresis
7 |
8 | Serial.begin(9600);
9 | }
10 |
11 | void loop() {
12 | AnalogEvent.loop();
13 | }
14 |
15 | void onChange(AnalogPortInformation* Sender) {
16 | Serial.print("Analog (pin:");
17 | Serial.print(Sender->pin);
18 | Serial.print(") changed to: ");
19 | Serial.print(Sender->value);
20 | Serial.println("!");
21 | }
22 |
--------------------------------------------------------------------------------
/TimedEvent/examples/Timers/Timers.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define CONTROLLED_TIMER 1
4 |
5 | bool flag = true;
6 |
7 | void setup() {
8 | Serial.begin(9600);
9 |
10 | //event 1
11 | TimedEvent.addTimer(5000, event1);
12 | //event 2
13 | TimedEvent.addTimer(CONTROLLED_TIMER, 500, event2);
14 | TimedEvent.start(CONTROLLED_TIMER);
15 | }
16 |
17 | void loop() {
18 | TimedEvent.loop();
19 | }
20 |
21 | void event1(TimerInformation* Sender) {
22 | Serial.print("event1: ");
23 | if (flag=!flag) {
24 | TimedEvent.start(CONTROLLED_TIMER);
25 | Serial.println("START!");
26 | } else {
27 | TimedEvent.stop(CONTROLLED_TIMER);
28 | Serial.println("STOP!");
29 | }
30 | }
31 |
32 | void event2(TimerInformation* Sender) {
33 | Serial.println("event2!!");
34 | }
35 |
--------------------------------------------------------------------------------
/TimedEvent/examples/RandomBlink/RandomBlink.ino:
--------------------------------------------------------------------------------
1 | /**
2 | * Blink an LED with random on and off times between MIN_TIMEOUT and MAX_TIMEOUT milliseconds.
3 | * Demonstrates setInterval() call.
4 | */
5 | #include
6 |
7 | #define MAX_TIMEOUT 200
8 | #define MIN_TIMEOUT 20
9 | #define INCREMENT 10
10 |
11 | #define LED_PIN 13
12 | #define BLINK_TIMER_ID 1
13 |
14 | // LED state
15 | bool on = false;
16 |
17 | void setup() {
18 | pinMode( LED_PIN, OUTPUT );
19 | TimedEvent.addTimer( BLINK_TIMER_ID, MIN_TIMEOUT, onTimeout );
20 | TimedEvent.start( BLINK_TIMER_ID );
21 | toggleLed();
22 | }
23 |
24 | void loop() {
25 | TimedEvent.loop();
26 | }
27 |
28 | void onTimeout( TimerInformation* info ){
29 |
30 | toggleLed();
31 |
32 | // Adjust the timeout interval
33 | TimedEvent.setInterval( BLINK_TIMER_ID, random( MIN_TIMEOUT, MAX_TIMEOUT ) );
34 | }
35 |
36 | void toggleLed(){
37 | if ( on ){
38 | digitalWrite( LED_PIN, LOW );
39 | on = false;
40 | } else {
41 | digitalWrite( LED_PIN, HIGH );
42 | on = true;
43 | }
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/AdvancedSerial/examples/LCDWriter/LCDWriter.ino:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #define BACKLIGHT_ON_MESSAGE 0
5 | #define BACKLIGHT_OFF_MESSAGE 1
6 | #define TEXT_MESSAGE 2
7 |
8 | //pins
9 | #define PIN_BACKLIGHT 7
10 |
11 | //configure lcd
12 | LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
13 |
14 | void setup() {
15 | //configure backlight pin
16 | pinMode(PIN_BACKLIGHT, OUTPUT);
17 | //configure LCD
18 | lcd.begin(16, 2);
19 | //begin serial port with a desirable speed
20 | Serial.begin(115200);
21 | //configure receiver callback
22 | AdvancedSerial.setReceiver(onMessage);
23 | }
24 |
25 | void loop() {
26 | //AdvancedSerial job
27 | AdvancedSerial.loop();
28 | }
29 |
30 | void onMessage(AdvancedSerialMessage* message) {
31 | switch (message->id) {
32 | case BACKLIGHT_ON_MESSAGE:
33 | digitalWrite(PIN_BACKLIGHT, HIGH);
34 | break;
35 |
36 | case BACKLIGHT_OFF_MESSAGE:
37 | digitalWrite(PIN_BACKLIGHT, LOW);
38 | break;
39 |
40 | case TEXT_MESSAGE:
41 | lcd.clear();
42 | for (int i=0; isize; i++) {
43 | if (i==16) lcd.setCursor(0, 1);
44 | lcd.write((char)message->payload[i]);
45 | }
46 | break;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/ButtonEvent/examples/SimpleButton/SimpleButton.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void setup() {
4 | ButtonEvent.addButton(12, //button pin
5 | onDown, //onDown event function
6 | onUp, //onUp event function
7 | onHold, //onHold event function
8 | 1000, //hold time in milliseconds
9 | onDouble, //double event function
10 | 200); //double time interval
11 |
12 | Serial.begin(9600);
13 | }
14 |
15 | void loop() {
16 | ButtonEvent.loop();
17 | }
18 |
19 | void onDown(ButtonInformation* Sender) {
20 | Serial.print("Button (pin:");
21 | Serial.print(Sender->pin);
22 | Serial.println(") down!");
23 | }
24 |
25 | void onUp(ButtonInformation* Sender) {
26 | Serial.print("Button (pin:");
27 | Serial.print(Sender->pin);
28 | Serial.println(") up!");
29 | }
30 |
31 | void onHold(ButtonInformation* Sender) {
32 | Serial.print("Button (pin:");
33 | Serial.print(Sender->pin);
34 | Serial.print(") hold for ");
35 | Serial.print(Sender->holdMillis);
36 | Serial.println("ms!");
37 | }
38 |
39 | void onDouble(ButtonInformation* Sender) {
40 | Serial.print("Button (pin:");
41 | Serial.print(Sender->pin);
42 | Serial.print(") double click in ");
43 | Serial.print(Sender->doubleMillis);
44 | Serial.println("ms!");
45 | }
46 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ebl-arduino
2 | Automatically exported from code.google.com/p/ebl-arduino
3 | ### Event-Based Library for Arduino
4 |
5 | * AdvancedSerial - Serial communication library with a C# .Net client. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/AdvancedSerial.md)
6 | * AnalogEvent - Event handler to listen for analog changes. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/AnalogEvent.md)
7 | * ButtonEvent - Event handler for analog and digital buttons. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/ButtonEvent.md)
8 | * LedControl - Asynchronous led controller. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/LedControl.md)
9 | * Properties - Data storage handler. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/Properties.md)
10 | * RTCTimedEvent - Event scheduler based on real time clocks, like unix cron scheduler. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/RTCTimedEvent.md)
11 | * TimedEvent - Event scheduler based on time intervals. [Usage](https://github.com/renatoferreirarenatoferreira/ebl-arduino/blob/wiki/TimedEvent.md)
12 |
13 | **P.S.:** *These modules are outdated and I don't think it will work with newer versions of Arduino's API or boards. I can try to help someone interested in get them working, but I don't have the newers boards to test it.*
14 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/dotnet.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 10.00
3 | # Visual Studio 2008
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet", "dotnet.csproj", "{12D43696-C6CA-4B65-9339-828D481BEF52}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LCDWrite", "examples\LCDWrite\LCDWrite.csproj", "{9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {12D43696-C6CA-4B65-9339-828D481BEF52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {12D43696-C6CA-4B65-9339-828D481BEF52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {12D43696-C6CA-4B65-9339-828D481BEF52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {12D43696-C6CA-4B65-9339-828D481BEF52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | {9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}.Release|Any CPU.Build.0 = Release|Any CPU
22 | EndGlobalSection
23 | GlobalSection(SolutionProperties) = preSolution
24 | HideSolutionNode = FALSE
25 | EndGlobalSection
26 | EndGlobal
27 |
--------------------------------------------------------------------------------
/Properties/examples/SimpleVariables/SimpleVariables.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define PROPERTY_BOOTS 0
4 | #define PROPERTY_TEXT 1
5 |
6 | void setup() {
7 | Serial.begin(9600);
8 |
9 | if (Properties.load()) {
10 | if (Properties.getInt(PROPERTY_BOOTS)<=15) {
11 | //print data
12 | Serial.print("PROPERTY_BOOTS: ");
13 | Serial.println(Properties.getInt(PROPERTY_BOOTS));
14 | Serial.print("PROPERTY_TEXT (");
15 | Serial.print(Properties.get(PROPERTY_TEXT)->valueSize);
16 | Serial.print("): ");
17 | Serial.write((byte*)Properties.get(PROPERTY_TEXT)->value,Properties.get(PROPERTY_TEXT)->valueSize);
18 | Serial.println();
19 |
20 | //sum PROPERTY_BOOTS and update text
21 | Properties.set(PROPERTY_BOOTS,Properties.getInt(PROPERTY_BOOTS)+1);
22 | char* text = (char*)malloc(32);
23 | sprintf(text,"Boot times: %d...", Properties.getInt(PROPERTY_BOOTS));
24 | Properties.set(PROPERTY_TEXT,(void*)text,strlen(text));
25 | Properties.save();
26 | } else {
27 | Serial.println("Flushing!");
28 |
29 | //flush data
30 | Properties.flush();
31 | Properties.save();
32 | }
33 | } else {
34 | Serial.println("Starting!");
35 |
36 | //create data
37 | Properties.set(PROPERTY_BOOTS,1);
38 | char* text = (char*)malloc(32);
39 | sprintf(text,"Boot times: %d...", Properties.getInt(PROPERTY_BOOTS));
40 | Properties.set(PROPERTY_TEXT,(void*)text,strlen(text));
41 | Properties.save();
42 | }
43 | }
44 |
45 | void loop() {
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("dotnet")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("ebl-arduino")]
12 | [assembly: AssemblyProduct("dotnet")]
13 | [assembly: AssemblyCopyright("Copyright © Renato A. Ferreira 2011")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("700d1b16-79fc-4408-8186-05a7ec473e28")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("0.1.1.0")]
36 | [assembly: AssemblyFileVersion("0.1.1.0")]
37 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("LCDWrite")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("ebl-arduino")]
12 | [assembly: AssemblyProduct("LCDWrite")]
13 | [assembly: AssemblyCopyright("Copyright © Renato A. Ferreira 2011")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("06428de9-4183-4101-a9a5-95b9bcaf7407")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("0.1.1.0")]
36 | [assembly: AssemblyFileVersion("0.1.1.0")]
37 |
--------------------------------------------------------------------------------
/RTCTimedEvent/examples/RTCTimer/RTCTimer.ino:
--------------------------------------------------------------------------------
1 | #include
#include //needed for I2C
void setup() {
Serial.begin(9600);
//set time to 31/12/2010 23:58:50
RTCTimedEvent.time.second = 50;
RTCTimedEvent.time.minute = 58;
RTCTimedEvent.time.hour = 23;
RTCTimedEvent.time.dayOfWeek = 6;
RTCTimedEvent.time.day = 31;
RTCTimedEvent.time.month = 12;
RTCTimedEvent.time.year = 2010;
RTCTimedEvent.writeRTC();
//initial buffer for 3 timers
RTCTimedEvent.initialCapacity = sizeof(RTCTimerInformation)*3;
//event for every day
RTCTimedEvent.addTimer(0, //minute
0, //hour
TIMER_ANY, //day fo week
TIMER_ANY, //day
TIMER_ANY, //month
dayCall);
//event for every hour
RTCTimedEvent.addTimer(0, //minute
TIMER_ANY, //hour
TIMER_ANY, //day fo week
TIMER_ANY, //day
TIMER_ANY, //month
hourCall);
//event for every minute
RTCTimedEvent.addTimer(TIMER_ANY, //minute
TIMER_ANY, //hour
TIMER_ANY, //day fo week
TIMER_ANY, //day
TIMER_ANY, //month
minuteCall);
}
void loop() {
RTCTimedEvent.loop();
}
void minuteCall(RTCTimerInformation* Sender) {
Serial.print("New minute: ");
Serial.print(RTCTimedEvent.time.hour, DEC);
Serial.print(":");
Serial.print(RTCTimedEvent.time.minute, DEC);
Serial.print(":");
Serial.print(RTCTimedEvent.time.second, DEC);
Serial.print(" ");
Serial.print(RTCTimedEvent.time.month, DEC);
Serial.print("/");
Serial.print(RTCTimedEvent.time.day, DEC);
Serial.print("/");
Serial.println(RTCTimedEvent.time.year, DEC);
}
void hourCall(RTCTimerInformation* Sender) {
Serial.print("New hour! ");
}
void dayCall(RTCTimerInformation* Sender) {
Serial.print("New day! ");
}
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/AdvancedSerialClient.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO.Ports;
3 |
4 | namespace ebl_arduino
5 | {
6 | ///
7 | /// Serial client for AdvancedSerial.
8 | ///
9 | public class AdvancedSerialClient : SerialProtocol
10 | {
11 | ///
12 | /// Serial port.
13 | ///
14 | private SerialPort Port;
15 |
16 | ///
17 | /// Default constructor.
18 | ///
19 | public AdvancedSerialClient()
20 | {
21 |
22 | }
23 |
24 | ///
25 | /// Verify transport connection.
26 | ///
27 | /// Return true if connected.
28 | public override bool IsConnected()
29 | {
30 | if (this.Port != null)
31 | return this.Port.IsOpen;
32 | else
33 | return false;
34 | }
35 |
36 | ///
37 | /// Close serial port.
38 | ///
39 | public override void Close()
40 | {
41 | this.ConnectionStream.Close();
42 | this.Port.Close();
43 | }
44 |
45 | ///
46 | /// Starts device communication through a serial port.
47 | ///
48 | /// Port name.
49 | /// Port baud rate.
50 | public void Open(String Port, int BaudRate)
51 | {
52 | if (this.Port != null && this.Port.IsOpen)
53 | this.Port.Close();
54 |
55 | this.Port = new SerialPort(Port);
56 | this.Port.BaudRate = BaudRate;
57 | this.Port.Open();
58 | this.ConnectionStream = this.Port.BaseStream;
59 | this.Receive();
60 |
61 | this.Flush();
62 |
63 | this.Send(MessageTypes.DISCOVERY_REQUEST);
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/dotnet.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 9.0.30729
7 | 2.0
8 | {12D43696-C6CA-4B65-9339-828D481BEF52}
9 | Library
10 | Properties
11 | dotnet
12 | dotnet
13 | v2.0
14 | 512
15 |
16 |
17 |
18 |
19 | true
20 | full
21 | false
22 | Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | pdbonly
29 | true
30 | Release\
31 | TRACE
32 | prompt
33 | 4
34 | Release\dotnet.XML
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
57 |
--------------------------------------------------------------------------------
/AnalogEvent/src/AnalogEvent.h:
--------------------------------------------------------------------------------
1 | /*
2 | AnalogEvent.h - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the author nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #ifndef AnalogEvent_h
30 | #define AnalogEvent_h
31 |
32 | #include
33 |
34 | #if defined(ARDUINO) && ARDUINO >= 100
35 | #include "Arduino.h"
36 | #else
37 | #include "WProgram.h"
38 | #endif
39 |
40 | struct AnalogPortInformation {
41 | short pin;
42 | int value;
43 | int hysteresis;
44 | void (*onChange)(AnalogPortInformation* Sender);
45 | };
46 |
47 | class AnalogEventClass
48 | {
49 | public:
50 | AnalogEventClass();
51 | void addAnalogPort(short pin, void (*onChange)(AnalogPortInformation* Sender), int hysteresis);
52 | void loop();
53 |
54 | private:
55 | short nextValue;
56 | short count;
57 | short index;
58 | AnalogPortInformation* ports;
59 | AnalogPortInformation* currentPort;
60 | void setPosition(short Position);
61 | };
62 |
63 | //global instance
64 | extern AnalogEventClass AnalogEvent;
65 |
66 | #endif
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/LCDWrite.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 9.0.30729
7 | 2.0
8 | {9BF24F86-B43E-4F61-8A3D-D093DEB97DC7}
9 | Exe
10 | Properties
11 | LCDWrite
12 | LCDWrite
13 | v2.0
14 | 512
15 |
16 |
17 |
18 |
19 | true
20 | full
21 | false
22 | ..\..\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | pdbonly
29 | true
30 | ..\..\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | {12D43696-C6CA-4B65-9339-828D481BEF52}
47 | dotnet
48 |
49 |
50 |
51 |
52 |
53 |
54 |
61 |
--------------------------------------------------------------------------------
/Properties/src/Properties.h:
--------------------------------------------------------------------------------
1 | /*
Properties.h - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef Properties_h
#define Properties_h
#include
#include
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define DEFAULT_POSITION 0
#define MAX_VARIABLES 30
struct PropertyInformation {
short propertyId;
short valueSize;
void* value;
};
class PropertiesClass
{
public:
short count;
short size;
PropertiesClass();
bool load();
bool load(short position);
void save();
void save(short position);
void flush();
void set(short propertyId, int value);
void set(short propertyId, long value);
void set(short propertyId, void* data, short dataSize);
int getInt(short propertyId);
long getLong(short propertyId);
PropertyInformation* get(short propertyId);
private:
short mallocSize;
short index;
short mainPosition;
short lastInt;
short lastLong;
PropertyInformation* moveProperty;
PropertyInformation* properties;
PropertyInformation* currentProperty;
void remove(short propertyId);
void addProperty(short propertyId, short dataSize);
void setPosition(short position);
bool findProperty(short propertyId);
void fixPointers();
void readEEPROM(short position, byte* data, short size);
void writeEEPROM(short position, byte* data, short size);
};
//global instance
extern PropertiesClass Properties;
#endif
--------------------------------------------------------------------------------
/LedControl/src/LedControl.h:
--------------------------------------------------------------------------------
1 | /*
LedControl.h - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LedControl_h
#define LedControl_h
#include
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define MODE_NONE 0
#define MODE_BLINK_ON 1
#define MODE_BLINK_OFF 2
#define MODE_FADE_IN 3
#define MODE_FADE_OUT 4
#define MODE_FADE_WAIT 4
struct LedInformation {
short pin;
short mode;
int intervalMilliseconds;
int delayMilliseconds;
int syncDelay;
unsigned long lastEventMicroseconds;
short pwmLevel;
};
class LedControlClass
{
public:
LedControlClass();
void loop();
void turnOn(short pin);
void turnOff(short pin);
void turnOn(short pin, int delayMilliseconds);
void turnOff(short pin, int delayMilliseconds);
void turnPercent(short pin, short percent);
void blink(short pin, int times, int intervalMilliseconds);
void blink(short pin, int times, int intervalMilliseconds, int delayMilliseconds);
void startBlink(short pin, int intervalMilliseconds);
void startBlink(short pin, int intervalMilliseconds, int delayMilliseconds);
void stopBlink(short pin);
private:
short count;
short index;
short index2;
int syncDelay;
bool syncMicros;
unsigned long lastMicros;
LedInformation* leds;
LedInformation* currentLed;
void addLed(short pin);
void setPosition(short position);
void findPin(short pin);
};
//global instance
extern LedControlClass LedControl;
#endif
--------------------------------------------------------------------------------
/TimedEvent/src/TimedEvent.h:
--------------------------------------------------------------------------------
1 | /*
2 | TimedEvent.h - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #ifndef TimedEvent_h
30 | #define TimedEvent_h
31 |
32 | #include
33 |
34 | #if defined(ARDUINO) && ARDUINO >= 100
35 | #include "Arduino.h"
36 | #else
37 | #include "WProgram.h"
38 | #endif
39 |
40 | #define DEFAULT_TIMER_ID -99
41 |
42 | struct TimerInformation {
43 | short eventId;
44 | bool enabled;
45 | unsigned long intervalMillis;
46 | unsigned long lastEventMillis;
47 | void (*onEvent)(TimerInformation* Sender);
48 | };
49 |
50 | class TimedEventClass
51 | {
52 | public:
53 | TimedEventClass();
54 | void addTimer(short eventId, unsigned long intervalMillis, void (*onEvent)(TimerInformation* Sender));
55 | void addTimer(unsigned long intervalMillis, void (*onEvent)(TimerInformation* Sender));
56 | void setInterval(short eventId, unsigned long intervalMillis);
57 | void start(short eventId);
58 | void stop(short eventId);
59 | void loop();
60 |
61 | private:
62 | short count;
63 | short index;
64 | unsigned long lastMillis;
65 | TimerInformation* timers;
66 | TimerInformation* currentTimer;
67 | void setPosition(short Position);
68 | bool findTimer(short eventId);
69 | };
70 |
71 | //global instance
72 | extern TimedEventClass TimedEvent;
73 |
74 | #endif
75 |
--------------------------------------------------------------------------------
/RTCTimedEvent/src/RTCTimedEvent.h:
--------------------------------------------------------------------------------
1 | /*
RTCTimedEvent.h - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TimedEvent_h
#define TimedEvent_h
#include
#include
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define RTC_ADDRESS_DS1307 0x68
#define RTC_HALT_DS1307 0x80
#define RTC_BASEYR_DS1307 2000
#define TIMER_ANY 0xFF
#define DEFAULT_TIMER_ID -99
struct TimeInformation {
byte second;
byte minute;
byte hour;
byte dayOfWeek;
byte day;
byte month;
short year;
};
struct RTCTimerInformation {
short eventId;
byte minute;
byte hour;
byte dayOfWeek;
byte day;
byte month;
void (*onEvent)(RTCTimerInformation* Sender);
};
class RTCTimedEventClass
{
public:
RTCTimedEventClass();
short initialCapacity;
TimeInformation time;
void readRTC();
void writeRTC();
void addTimer(byte minute, byte hour, byte day, byte month, byte dayOfWeek, void (*onEvent)(RTCTimerInformation* Sender));
void addTimer(short eventId, byte minute, byte hour, byte day, byte month, byte dayOfWeek, void (*onEvent)(RTCTimerInformation* Sender));
void clear();
void loop();
private:
short count;
short mallocSize;
short index;
short lastMinute;
short currMinute;
unsigned long nextMillis;
unsigned long lastMillis;
unsigned long currMillis;
RTCTimerInformation* timers;
RTCTimerInformation* currentTimer;
byte decToBcd(byte value);
byte bcdToDec(byte value);
void switchRTC(bool turnOn);
};
//global instance
extern RTCTimedEventClass RTCTimedEvent;
#endif
--------------------------------------------------------------------------------
/ButtonEvent/src/ButtonEvent.h:
--------------------------------------------------------------------------------
1 | /*
ButtonEvent.h - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ButtonEvent_h
#define ButtonEvent_h
#include
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define NOT_ANALOG -99
struct ButtonInformation {
short pin;
short analogValue;
byte deviation;
bool pressed;
bool hold;
unsigned long startMillis;
unsigned long holdMillis;
unsigned long holdMillisWait;
unsigned long doubleMillis;
unsigned long doubleMillisWait;
void (*onDown)(ButtonInformation* Sender);
void (*onUp)(ButtonInformation* Sender);
void (*onHold)(ButtonInformation* Sender);
void (*onDouble)(ButtonInformation* Sender);
};
class ButtonEventClass
{
public:
ButtonEventClass();
short initialCapacity;
void addButton(short pin, void (*onDown)(ButtonInformation* Sender), void (*onUp)(ButtonInformation* Sender), void (*onHold)(ButtonInformation* Sender), unsigned long holdMillisWait, void (*onDouble)(ButtonInformation* Sender), unsigned long doubleMillisWait);
void addButton(short pin, short analogValue, byte deviation, void (*onDown)(ButtonInformation* Sender), void (*onUp)(ButtonInformation* Sender), void (*onHold)(ButtonInformation* Sender), unsigned long holdMillisWait, void (*onDouble)(ButtonInformation* Sender), unsigned long doubleMillisWait);
void loop();
private:
bool nextPressed;
short nextAnalogRead;
short count;
short mallocSize;
short index;
unsigned long lastMillis;
ButtonInformation* buttons;
ButtonInformation* currentButton;
void setPosition(short Position);
};
//global instance
extern ButtonEventClass ButtonEvent;
#endif
--------------------------------------------------------------------------------
/AdvancedSerial/src/AdvancedSerial.h:
--------------------------------------------------------------------------------
1 | /*
2 | AdvancedSerial.h - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #ifndef AdvancedSerial_h
30 | #define AdvancedSerial_h
31 |
32 | #define MESSAGE_HEADER_SIZE 3
33 | #define MESSAGE_MAX_PAYLOAD_SIZE 32
34 |
35 | #define READING_STX 0x01
36 | #define READING_HEADER 0x02
37 | #define READING_PAYLOAD 0x03
38 | #define READING_ETX 0x04
39 |
40 | #define DELIMITER_STX 0x02
41 | #define DELIMITER_ETX 0x03
42 |
43 | #define DEBUG 0x01
44 | #define MESSAGE 0x02
45 | #define MESSAGE_ACKNOWLEDGE 0x03
46 | #define DISCOVERY_REQUEST 0x04
47 | #define DISCOVERY_RESPONSE 0x05
48 |
49 | #include
50 |
51 | #if defined(ARDUINO) && ARDUINO >= 100
52 | #include "Arduino.h"
53 | #else
54 | #include "WProgram.h"
55 | #endif
56 |
57 | struct AdvancedSerialMessage {
58 | byte type;
59 | byte id;
60 | byte size;
61 | byte* payload;
62 | };
63 |
64 | class AdvancedSerialClass
65 | {
66 | public:
67 | AdvancedSerialClass();
68 | void setReceiver(void (*onReceive)(AdvancedSerialMessage* Message));
69 | void send(byte id, byte size, byte* payload);
70 | void loop();
71 |
72 | private:
73 | byte bufferCondition;
74 | byte bufferPosition;
75 | byte messageBuffer[sizeof(AdvancedSerialMessage)+MESSAGE_MAX_PAYLOAD_SIZE];
76 | AdvancedSerialMessage* message;
77 | void (*onReceive)(AdvancedSerialMessage* Message);
78 | void send(byte type, byte id, byte size, byte* payload);
79 |
80 | };
81 |
82 | //global instance
83 | extern AdvancedSerialClass AdvancedSerial;
84 |
85 | #endif
--------------------------------------------------------------------------------
/AnalogEvent/src/AnalogEvent.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AnalogEvent.cpp - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the author nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #include "AnalogEvent.h"
30 |
31 | AnalogEventClass::AnalogEventClass() {
32 | this->count = 0;
33 | }
34 |
35 | void AnalogEventClass::addAnalogPort(short pin, void (*onChange)(AnalogPortInformation* Sender), int hysteresis) {
36 | if (this->count > 0) {
37 | this->ports = (AnalogPortInformation*) realloc(this->ports, sizeof(AnalogPortInformation)*(this->count+1));
38 | } else {
39 | this->ports = (AnalogPortInformation*) malloc(sizeof(AnalogPortInformation));
40 | }
41 |
42 | this->setPosition(this->count);
43 | this->currentPort->pin = pin;
44 | this->currentPort->value = -99; //force the first event change
45 | this->currentPort->onChange = onChange;
46 | this->currentPort->hysteresis = hysteresis;
47 |
48 | this->count++;
49 | }
50 |
51 | void AnalogEventClass::setPosition(short Position) {
52 | this->currentPort = this->ports+Position;
53 | }
54 |
55 | void AnalogEventClass::loop() {
56 | for (this->index = 0; this->index < this->count; this->index++) {
57 | this->setPosition(this->index);
58 | this->nextValue = analogRead(this->currentPort->pin);
59 |
60 | if (this->currentPort->value != this->nextValue) {
61 | if (this->currentPort->hysteresis > 0) {
62 | if (this->currentPort->value-this->nextValue >= this->currentPort->hysteresis ||
63 | this->nextValue-this->currentPort->value >= this->currentPort->hysteresis) {
64 | this->currentPort->value = this->nextValue;
65 | if (this->currentPort->onChange != NULL)
66 | this->currentPort->onChange(this->currentPort); //call event
67 | }
68 | } else {
69 | this->currentPort->value = this->nextValue;
70 | if (this->currentPort->onChange != NULL)
71 | this->currentPort->onChange(this->currentPort); //call event
72 | }
73 | }
74 | }
75 | }
76 |
77 | AnalogEventClass AnalogEvent;
--------------------------------------------------------------------------------
/ButtonEvent/examples/AnalogButton/AnalogButton.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | short Button1 = 230;
4 | short Button2 = 365;
5 | short Button3 = 460;
6 |
7 | void setup() {
8 | //initial buffer for 3 buttons
9 | ButtonEvent.initialCapacity = sizeof(ButtonInformation)*3;
10 |
11 | ButtonEvent.addButton(0, //analog button pin
12 | Button1, //analog value
13 | 20, //deviation
14 | onDown, //onDown event function
15 | onUp, //onUp event function
16 | onHold, //onHold event function
17 | 1000, //hold time in milliseconds
18 | onDouble, //onDouble event function
19 | 200); //double time interval
20 |
21 | ButtonEvent.addButton(0, //analog button pin
22 | Button2, //analog value
23 | 20, //deviation
24 | onDown, //onDown event function
25 | onUp, //onUp event function
26 | onHold, //onHold event function
27 | 1000, //hold time in milliseconds
28 | onDouble, //onDouble event function
29 | 200); //double time interval
30 |
31 | ButtonEvent.addButton(0, //analog button pin
32 | Button3, //analog value
33 | 20, //deviation
34 | onDown, //onDown event function
35 | onUp, //onUp event function
36 | onHold, //onHold event function
37 | 1000, //hold time in milliseconds
38 | onDouble, //onDouble event function
39 | 200); //double time interval
40 |
41 | Serial.begin(9600);
42 | }
43 |
44 |
45 | void loop() {
46 | ButtonEvent.loop();
47 | }
48 |
49 | void onDown(ButtonInformation* Sender) {
50 | Serial.print("Button ");
51 |
52 | if (Sender->analogValue == Button1)
53 | Serial.print(1);
54 | else if (Sender->analogValue == Button2)
55 | Serial.print(2);
56 | else if (Sender->analogValue == Button3)
57 | Serial.print(3);
58 |
59 | Serial.println(" down!");
60 | }
61 |
62 | void onUp(ButtonInformation* Sender) {
63 | Serial.print("Button ");
64 |
65 | if (Sender->analogValue == Button1)
66 | Serial.print(1);
67 | else if (Sender->analogValue == Button2)
68 | Serial.print(2);
69 | else if (Sender->analogValue == Button3)
70 | Serial.print(3);
71 |
72 | Serial.println(" up!");
73 | }
74 |
75 | void onHold(ButtonInformation* Sender) {
76 | Serial.print("Button ");
77 |
78 | if (Sender->analogValue == Button1)
79 | Serial.print(1);
80 | else if (Sender->analogValue == Button2)
81 | Serial.print(2);
82 | else if (Sender->analogValue == Button3)
83 | Serial.print(3);
84 |
85 | Serial.print(" hold for ");
86 | Serial.print(Sender->holdMillis);
87 | Serial.println("ms!");
88 | }
89 |
90 | void onDouble(ButtonInformation* Sender) {
91 | Serial.print("Button ");
92 |
93 | if (Sender->analogValue == Button1)
94 | Serial.print(1);
95 | else if (Sender->analogValue == Button2)
96 | Serial.print(2);
97 | else if (Sender->analogValue == Button3)
98 | Serial.print(3);
99 |
100 | Serial.print(" double click in ");
101 | Serial.print(Sender->doubleMillis);
102 | Serial.println("ms!");
103 | }
104 |
--------------------------------------------------------------------------------
/AdvancedSerial/src/AdvancedSerial.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | AdvancedSerial.cpp - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #include "AdvancedSerial.h"
30 |
31 | AdvancedSerialClass::AdvancedSerialClass() {
32 | this->bufferCondition = READING_STX;
33 | this->bufferPosition = 0;
34 | this->message = (AdvancedSerialMessage*) &this->messageBuffer;
35 | this->message->payload = (byte*)this->message+sizeof(AdvancedSerialMessage);
36 | }
37 |
38 | void AdvancedSerialClass::setReceiver(void (*onReceive)(AdvancedSerialMessage* Message)) {
39 | this->onReceive = onReceive;
40 | }
41 |
42 | void AdvancedSerialClass::send(byte type, byte id, byte size, byte* payload) {
43 | if (size >= 0 && size <= MESSAGE_MAX_PAYLOAD_SIZE) {
44 | Serial.write((byte)DELIMITER_STX);
45 | Serial.write(type);
46 | Serial.write(id);
47 | Serial.write(size);
48 | if (size > 0) Serial.write(payload, size);
49 | Serial.write((byte)DELIMITER_ETX);
50 | }
51 | }
52 |
53 | void AdvancedSerialClass::send(byte id, byte size, byte* payload) {
54 | this->send(MESSAGE, id, size, payload);
55 | }
56 |
57 | void AdvancedSerialClass::loop() {
58 | while(Serial.available() > 0) {
59 | switch(bufferCondition) {
60 | case READING_STX:
61 | if ( Serial.read() == DELIMITER_STX) {
62 | this->bufferCondition = READING_HEADER;
63 | }
64 | break;
65 |
66 | case READING_HEADER:
67 | this->messageBuffer[this->bufferPosition] = Serial.read();
68 | this->bufferPosition++;
69 | if ( this->bufferPosition == MESSAGE_HEADER_SIZE ) {
70 | if (this->message->size == 0) {
71 | //payload is empty
72 | this->bufferCondition = READING_ETX;
73 | } else if (this->message->size > 0 && this->message->size <= MESSAGE_MAX_PAYLOAD_SIZE) {
74 | //read
75 | this->bufferPosition += sizeof(this->message->payload);
76 | this->bufferCondition = READING_PAYLOAD;
77 | } else {
78 | //avoid wrong sized messages
79 | this->bufferCondition = READING_STX;
80 | }
81 | }
82 | break;
83 |
84 | case READING_PAYLOAD:
85 | this->messageBuffer[this->bufferPosition] = Serial.read();
86 | this->bufferPosition++;
87 | if ( this->bufferPosition == (this->message->size+sizeof(AdvancedSerialMessage)) ) {
88 | this->bufferCondition = READING_ETX;
89 | }
90 | break;
91 |
92 | case READING_ETX:
93 | if ( Serial.read() == DELIMITER_ETX) {
94 | if (this->message->type == DISCOVERY_REQUEST) {
95 | this->send(DISCOVERY_RESPONSE, 0, 0, NULL);
96 | } else if (this->message->type == MESSAGE) {
97 | this->send(MESSAGE_ACKNOWLEDGE, 0, 0, NULL);
98 |
99 | if (this->onReceive != NULL)
100 | this->onReceive(this->message);
101 | }
102 | }
103 | this->bufferCondition = READING_STX;
104 | this->bufferPosition = 0;
105 | break;
106 | }
107 |
108 | }
109 | }
110 |
111 | AdvancedSerialClass AdvancedSerial;
--------------------------------------------------------------------------------
/TimedEvent/src/TimedEvent.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | TimedEvent.cpp - Event-Based Library for Arduino.
3 | Copyright (c) 2011, Renato A. Ferreira
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | * Neither the name of the nor the
14 | names of its contributors may be used to endorse or promote products
15 | derived from this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 | */
28 |
29 | #include "TimedEvent.h"
30 |
31 | TimedEventClass::TimedEventClass() {
32 | this->count = 0;
33 | }
34 |
35 | void TimedEventClass::addTimer(unsigned long intervalMillis, void (*onEvent)(TimerInformation* Sender)) {
36 | this->addTimer(DEFAULT_TIMER_ID, intervalMillis, onEvent);
37 | this->currentTimer->enabled = true;
38 | this->currentTimer->lastEventMillis = millis();
39 | }
40 |
41 | void TimedEventClass::addTimer(short eventId, unsigned long intervalMillis, void (*onEvent)(TimerInformation* Sender)) {
42 | if (this->count > 0) {
43 | this->timers = (TimerInformation*) realloc(this->timers, sizeof(TimerInformation)*(this->count+1));
44 | } else {
45 | this->timers = (TimerInformation*) malloc(sizeof(TimerInformation));
46 | }
47 |
48 | this->setPosition(this->count);
49 | this->currentTimer->eventId = eventId;
50 | this->currentTimer->intervalMillis = intervalMillis;
51 | this->currentTimer->onEvent = onEvent;
52 | this->currentTimer->enabled = false;
53 |
54 | this->count++;
55 | }
56 |
57 | void TimedEventClass::setPosition(short Position) {
58 | this->currentTimer = this->timers+Position;
59 | }
60 |
61 | bool TimedEventClass::findTimer(short eventId) {
62 | for (this->index = 0; this->index < this->count; this->index++) {
63 | this->setPosition(this->index);
64 |
65 | if (this->currentTimer->eventId == eventId)
66 | return true;
67 | }
68 | return false;
69 | }
70 |
71 | void TimedEventClass::loop() {
72 | for (this->index = 0; this->index < this->count; this->index++) {
73 | this->setPosition(this->index);
74 |
75 | if (this->currentTimer->enabled) {
76 | this->lastMillis = millis();
77 |
78 | //check for millis() overflow
79 | if (this->lastMillis < this->currentTimer->lastEventMillis)
80 | {
81 | //calculate the time before the overflow plus the current time to obtain the invertal
82 | if (((((unsigned long)-1) - this->currentTimer->lastEventMillis) + this->lastMillis) >= this->currentTimer->intervalMillis) {
83 | this->currentTimer->lastEventMillis = this->lastMillis;
84 | this->currentTimer->onEvent(this->currentTimer);
85 | }
86 | } else {
87 | //proceed as a normal increment
88 | if (this->currentTimer->lastEventMillis+this->currentTimer->intervalMillis <= this->lastMillis) {
89 | this->currentTimer->lastEventMillis = this->lastMillis;
90 | this->currentTimer->onEvent(this->currentTimer);
91 | }
92 | }
93 | }
94 | }
95 | }
96 |
97 | void TimedEventClass::start(short eventId) {
98 | if (this->findTimer(eventId)) {
99 | this->currentTimer->enabled = true;
100 | this->currentTimer->lastEventMillis = millis();
101 | }
102 | }
103 |
104 | void TimedEventClass::stop(short eventId) {
105 | if (this->findTimer(eventId)) {
106 | this->currentTimer->enabled = false;
107 | }
108 | }
109 |
110 | void TimedEventClass::setInterval(short eventId, unsigned long intervalMillis) {
111 | if (this->findTimer(eventId)) {
112 | this->currentTimer->intervalMillis = intervalMillis;
113 | }
114 | }
115 |
116 | TimedEventClass TimedEvent;
117 |
--------------------------------------------------------------------------------
/ButtonEvent/src/ButtonEvent.cpp:
--------------------------------------------------------------------------------
1 | /*
ButtonEvent.cpp - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ButtonEvent.h"
ButtonEventClass::ButtonEventClass() {
this->count = 0;
this->mallocSize = 0;
this->initialCapacity = sizeof(ButtonInformation);
}
void ButtonEventClass::addButton(short pin, void (*onDown)(ButtonInformation* Sender), void (*onUp)(ButtonInformation* Sender), void (*onHold)(ButtonInformation* Sender), unsigned long holdMillisWait, void (*onDouble)(ButtonInformation* Sender), unsigned long doubleMillisWait) {
if (this->count > 0) {
//determine if the buffer free space fits the next object
if (this->mallocSize < (sizeof(ButtonInformation)*(this->count+1))) {
this->mallocSize = sizeof(ButtonInformation)*(this->count+1);
//alocate more memory space
this->buttons = (ButtonInformation*) realloc(this->buttons, this->mallocSize);
}
} else {
//determine if initial capacity parameter fits the first object
if (this->initialCapacity >= sizeof(ButtonInformation)) {
this->mallocSize = this->initialCapacity;
} else {
this->mallocSize = sizeof(ButtonInformation);
}
//create the buffer size
this->buttons = (ButtonInformation*) malloc(this->mallocSize);
}
this->setPosition(this->count);
this->currentButton->pin = pin;
this->currentButton->analogValue = NOT_ANALOG;
this->currentButton->onDown = onDown;
this->currentButton->onUp = onUp;
this->currentButton->onHold = onHold;
this->currentButton->holdMillisWait = holdMillisWait;
this->currentButton->onDouble = onDouble;
this->currentButton->doubleMillisWait = doubleMillisWait;
pinMode(this->currentButton->pin, INPUT);
this->count++;
}
void ButtonEventClass::addButton(short pin, short analogValue, byte deviation, void (*onDown)(ButtonInformation* Sender), void (*onUp)(ButtonInformation* Sender), void (*onHold)(ButtonInformation* Sender), unsigned long holdMillisWait, void (*onDouble)(ButtonInformation* Sender), unsigned long doubleMillisWait) {
if (this->count > 0) {
//determine if the buffer free space fits the next object
if (this->mallocSize < (sizeof(ButtonInformation)*(this->count+1))) {
this->mallocSize = sizeof(ButtonInformation)*(this->count+1);
//alocate more memory space
this->buttons = (ButtonInformation*) realloc(this->buttons, this->mallocSize);
}
} else {
//determine if initial capacity parameter fits the first object
if (this->initialCapacity >= sizeof(ButtonInformation)) {
this->mallocSize = this->initialCapacity;
} else {
this->mallocSize = sizeof(ButtonInformation);
}
//create the buffer size
this->buttons = (ButtonInformation*) malloc(this->mallocSize);
}
this->setPosition(this->count);
this->currentButton->pin = pin;
this->currentButton->analogValue = analogValue;
this->currentButton->deviation = deviation;
this->currentButton->onDown = onDown;
this->currentButton->onUp = onUp;
this->currentButton->onHold = onHold;
this->currentButton->holdMillisWait = holdMillisWait;
this->currentButton->onDouble = onDouble;
this->currentButton->doubleMillisWait = doubleMillisWait;
pinMode(14+this->currentButton->pin, INPUT);
digitalWrite((14+this->currentButton->pin), HIGH);
this->count++;
}
void ButtonEventClass::setPosition(short Position) {
this->currentButton = this->buttons+Position;
}
void ButtonEventClass::loop() {
for (this->index = 0; this->index < this->count; this->index++) {
this->setPosition(this->index);
if (this->currentButton->analogValue == NOT_ANALOG) {
this->nextPressed = (digitalRead(this->currentButton->pin) == HIGH);
} else {
this->nextAnalogRead = analogRead(this->currentButton->pin);
this->nextPressed = ((this->nextAnalogRead >= (this->currentButton->analogValue-this->currentButton->deviation)) && (this->nextAnalogRead <= (this->currentButton->analogValue+this->currentButton->deviation)));
}
//down event
if (this->nextPressed) {
if (this->currentButton->pressed) {
//hold event
if (!this->currentButton->hold && this->currentButton->onHold != NULL && this->currentButton->holdMillisWait > 0) {
this->currentButton->holdMillis = millis() - this->currentButton->startMillis; //calculate time
if (this->currentButton->holdMillis >= this->currentButton->holdMillisWait) {
this->currentButton->onHold(this->currentButton); //call event
this->currentButton->hold = true;
}
}
} else {
//double event
if (this->currentButton->onDouble != NULL && this->currentButton->doubleMillisWait > 0) {
this->lastMillis = millis();
this->currentButton->doubleMillis = this->lastMillis - this->currentButton->startMillis; //calculate time
this->currentButton->startMillis = this->lastMillis;
if (this->currentButton->doubleMillis <= this->currentButton->doubleMillisWait)
this->currentButton->onDouble(this->currentButton); //call event
else
if (this->currentButton->onDown != NULL)
//down event
this->currentButton->onDown(this->currentButton); //call event
} else {
//down event
this->currentButton->startMillis = millis();
if (this->currentButton->onDown != NULL)
this->currentButton->onDown(this->currentButton); //call event
}
}
}
//up event
if (!this->nextPressed && this->currentButton->pressed) {
if (this->currentButton->onUp != NULL) {
this->currentButton->holdMillis = millis() - this->currentButton->startMillis; //calculate time
this->currentButton->onUp(this->currentButton); //call event
}
this->currentButton->hold = false;
}
this->currentButton->pressed = this->nextPressed;
}
}
ButtonEventClass ButtonEvent;
--------------------------------------------------------------------------------
/LedControl/src/LedControl.cpp:
--------------------------------------------------------------------------------
1 | /*
LedControl.cpp - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "LedControl.h"
LedControlClass::LedControlClass() {
this->count = 0;
}
void LedControlClass::addLed(short pin) {
if (this->count > 0) {
this->leds = (LedInformation*) realloc(this->leds, sizeof(LedInformation)*(this->count+1));
} else {
this->leds = (LedInformation*) malloc(sizeof(LedInformation));
}
this->setPosition(this->count);
this->currentLed->pin = pin;
this->currentLed->lastEventMicroseconds = 0;
pinMode(this->currentLed->pin, OUTPUT);
this->count++;
}
void LedControlClass::setPosition(short position) {
this->currentLed = this->leds+position;
}
void LedControlClass::findPin(short pin) {
for (this->index = 0; this->index < this->count; this->index++) {
this->setPosition(this->index);
if (this->currentLed->pin == pin)
return;
}
this->addLed(pin);
}
void LedControlClass::loop() {
for (this->index = 0; this->index < this->count; this->index++) {
this->setPosition(this->index);
if (this->currentLed->mode != MODE_NONE) {
this->lastMicros = micros();
if (this->currentLed->lastEventMicroseconds+(this->currentLed->intervalMilliseconds*1000L) <= this->lastMicros) {
if (this->currentLed->mode == MODE_BLINK_OFF) {
this->turnOn(this->currentLed->pin);
this->currentLed->mode = MODE_BLINK_ON;
this->currentLed->lastEventMicroseconds = this->lastMicros;
} else if (this->currentLed->mode == MODE_BLINK_ON) {
this->turnOff(this->currentLed->pin);
this->currentLed->mode = MODE_BLINK_OFF;
this->currentLed->lastEventMicroseconds = this->lastMicros;
} else if (this->currentLed->mode == MODE_FADE_IN) {
if (this->currentLed->pwmLevel <= 255) {
this->currentLed->lastEventMicroseconds += this->currentLed->syncDelay;
analogWrite(this->currentLed->pin, this->currentLed->pwmLevel);
this->currentLed->pwmLevel++;
} else {
this->currentLed->mode = MODE_FADE_OUT;
this->currentLed->pwmLevel = 255;
this->currentLed->lastEventMicroseconds = this->lastMicros;
}
} else if (this->currentLed->mode == MODE_FADE_OUT) {
if (this->currentLed->pwmLevel >= 0) {
this->currentLed->lastEventMicroseconds += this->currentLed->syncDelay;
analogWrite(this->currentLed->pin, this->currentLed->pwmLevel);
this->currentLed->pwmLevel--;
} else {
this->currentLed->mode = MODE_FADE_IN;
this->currentLed->pwmLevel = 0;
this->currentLed->lastEventMicroseconds = this->lastMicros;
}
}
} else if (this->lastMicros < this->currentLed->lastEventMicroseconds) {
//avoid micros() overflow problem
this->currentLed->lastEventMicroseconds = this->lastMicros;
}
}
}
}
void LedControlClass::turnOn(short pin) {
digitalWrite(pin, HIGH);
}
void LedControlClass::turnOff(short pin) {
digitalWrite(pin, LOW);
}
void LedControlClass::turnPercent(short pin, short percent) {
if (percent <= 0)
digitalWrite(pin, LOW);
else if (percent >= 100)
digitalWrite(pin, HIGH);
else {
analogWrite(pin, map(percent, 0, 100, 0, 255));
}
}
void LedControlClass::turnOn(short pin, int delayMilliseconds) {
if (delayMilliseconds>4000) {
this->syncMicros = false;
this->syncDelay = delayMilliseconds/255;
} else {
this->syncMicros = true;
this->syncDelay = (delayMilliseconds*1000L)/255;
}
for (this->index = 0; this->index <= 255; this->index++) {
analogWrite(pin, this->index);
if (this->syncMicros)
delayMicroseconds(this->syncDelay);
else
delay(this->syncDelay);
}
}
void LedControlClass::turnOff(short pin, int delayMilliseconds) {
if (delayMilliseconds>4000) {
this->syncMicros = false;
this->syncDelay = delayMilliseconds/255;
} else {
this->syncMicros = true;
this->syncDelay = (delayMilliseconds*1000L)/255;
}
for (this->index = 255; this->index >= 0; this->index--) {
analogWrite(pin, this->index);
if (this->syncMicros)
delayMicroseconds(this->syncDelay);
else
delay(this->syncDelay);
}
}
void LedControlClass::blink(short pin, int times, int intervalMilliseconds) {
for (this->index = 0; this->index < times; this->index++) {
if (this->index != 0)
delay(intervalMilliseconds);
this->turnOn(pin);
delay(intervalMilliseconds);
this->turnOff(pin);
}
}
void LedControlClass::blink(short pin, int times, int intervalMilliseconds, int delayMilliseconds) {
for (this->index2 = 0; this->index2 < times; this->index2++) {
if (this->index2 != 0)
delay(intervalMilliseconds);
this->turnOn(pin, delayMilliseconds);
delay(intervalMilliseconds);
this->turnOff(pin, delayMilliseconds);
}
}
void LedControlClass::startBlink(short pin, int intervalMilliseconds) {
this->findPin(pin);
this->turnOn(this->currentLed->pin);
this->currentLed->mode = MODE_BLINK_ON;
this->currentLed->intervalMilliseconds = intervalMilliseconds;
//starts immediately
this->currentLed->lastEventMicroseconds = micros()-(this->currentLed->intervalMilliseconds*1000L);
}
void LedControlClass::startBlink(short pin, int intervalMilliseconds, int delayMilliseconds) {
this->findPin(pin);
this->turnOff(this->currentLed->pin);
this->currentLed->mode = MODE_FADE_IN;
this->currentLed->intervalMilliseconds = intervalMilliseconds;
this->currentLed->delayMilliseconds = delayMilliseconds;
this->currentLed->syncDelay = (delayMilliseconds*1000L)/255;
this->currentLed->pwmLevel = 0;
//starts immediately
this->currentLed->lastEventMicroseconds = micros()-(this->currentLed->intervalMilliseconds*1000L);
}
void LedControlClass::stopBlink(short pin) {
this->findPin(pin);
this->turnOff(this->currentLed->pin);
this->currentLed->mode = MODE_NONE;
}
LedControlClass LedControl;
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/examples/LCDWrite/LCDWrite.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Diagnostics;
4 | using System.Text;
5 |
6 | namespace ebl_arduino.examples
7 | {
8 | ///
9 | /// Client application for LCDWriter sketch.
10 | ///
11 | class LCDWrite
12 | {
13 | //types of parameters
14 | private const int PARAMETER_BACKLIGHT = 0;
15 | private const int PARAMETER_PORT = 1;
16 | private const int PARAMETER_TEXT = 2;
17 |
18 | //types of messages
19 | private const byte BACKLIGHT_ON_MESSAGE = 0;
20 | private const byte BACKLIGHT_OFF_MESSAGE = 1;
21 | private const byte TEXT_MESSAGE = 2;
22 |
23 | ///
24 | /// Applications entry point.
25 | ///
26 | ///
27 | static void Main(string[] args)
28 | {
29 | Hashtable Arguments = ArgumentParse(args);
30 |
31 | if (Arguments.ContainsKey(PARAMETER_PORT) &&
32 | (Arguments.ContainsKey(PARAMETER_BACKLIGHT) || Arguments.ContainsKey(PARAMETER_TEXT)))
33 | {
34 | Console.Write("Port: ");
35 | Console.WriteLine(Arguments[PARAMETER_PORT]);
36 | if (Arguments.ContainsKey(PARAMETER_BACKLIGHT))
37 | {
38 | Console.Write("Backlight: ");
39 | if ((bool)Arguments[PARAMETER_BACKLIGHT])
40 | Console.WriteLine("On");
41 | else
42 | Console.WriteLine("Off");
43 | }
44 | if (Arguments.ContainsKey(PARAMETER_TEXT))
45 | {
46 | Console.Write("Message: '");
47 | Console.Write(Arguments[PARAMETER_TEXT]);
48 | Console.WriteLine("'");
49 | }
50 |
51 | try
52 | {
53 | //create object
54 | AdvancedSerialClient ArduinoClient = new AdvancedSerialClient();
55 | //connect to device
56 | ArduinoClient.Open(Arguments[PARAMETER_PORT].ToString(), 115200);
57 |
58 | //command for LED
59 | if (Arguments.ContainsKey(PARAMETER_BACKLIGHT))
60 | {
61 | SerialProtocol.AdvancedSerialMessage BacklightMessage = new SerialProtocol.AdvancedSerialMessage();
62 |
63 | if ((bool)Arguments[PARAMETER_BACKLIGHT])
64 | BacklightMessage.ID = BACKLIGHT_ON_MESSAGE;
65 | else
66 | BacklightMessage.ID = BACKLIGHT_OFF_MESSAGE;
67 |
68 | ArduinoClient.Send(BacklightMessage);
69 | }
70 |
71 | //command for text
72 | if (Arguments.ContainsKey(PARAMETER_TEXT))
73 | {
74 | SerialProtocol.AdvancedSerialMessage TextMessage = new SerialProtocol.AdvancedSerialMessage();
75 | TextMessage.ID = TEXT_MESSAGE;
76 | TextMessage.Payload = new System.Text.ASCIIEncoding().GetBytes(Arguments[PARAMETER_TEXT].ToString());
77 | TextMessage.Size = (byte)TextMessage.Payload.Length;
78 | ArduinoClient.Send(TextMessage);
79 | }
80 | }
81 | catch (Exception ex)
82 | {
83 | Console.Write("Error: " + ex.Message);
84 | }
85 | }
86 | else
87 | {
88 | Console.WriteLine("Invalid parameters!");
89 | Console.WriteLine();
90 | Console.WriteLine("Write a message to LCD display. It's a serial client for LCDWriter sketch from abl-arduino lirary.");
91 | Console.WriteLine();
92 | Console.WriteLine("usage: " + Process.GetCurrentProcess().ProcessName + " /P port [/On | /Off] [message]");
93 | Console.WriteLine();
94 | Console.WriteLine(" message Message to be written on LCD display (16x2).");
95 | Console.WriteLine();
96 | Console.WriteLine(" /P Serial port.");
97 | Console.WriteLine(" /On Turn backlight led on.");
98 | Console.WriteLine(" /Off Turn backlight led off.");
99 | }
100 | }
101 |
102 | ///
103 | /// Parse arguments.
104 | ///
105 | /// Argument array from Main() method.
106 | /// Hashtable of parsed arguments.
107 | private static Hashtable ArgumentParse(string[] args)
108 | {
109 | Hashtable Return = new Hashtable();
110 |
111 | string Key;
112 | bool ReadPort = false;
113 | bool ResetMessage = false;
114 | foreach (String arg in args)
115 | {
116 | if (arg.StartsWith("-") || arg.StartsWith("/"))
117 | {
118 | Key = arg.Substring(1, arg.Length - 1).ToLower();
119 |
120 | if (Key.Equals("on"))
121 | {
122 | if (!Return.ContainsKey(PARAMETER_BACKLIGHT))
123 | Return.Add(PARAMETER_BACKLIGHT, true);
124 | }
125 | else if (Key.Equals("off"))
126 | {
127 | if (!Return.ContainsKey(PARAMETER_BACKLIGHT))
128 | Return.Add(PARAMETER_BACKLIGHT, false);
129 | }
130 | else if (Key.StartsWith("p"))
131 | {
132 | if (!Return.ContainsKey(PARAMETER_PORT))
133 | ReadPort = true;
134 | }
135 |
136 | ResetMessage = true;
137 | }
138 | else if (ReadPort)
139 | {
140 | Return.Add(PARAMETER_PORT, arg);
141 | ReadPort = false;
142 | }
143 | else
144 | {
145 | if (ResetMessage)
146 | {
147 | if (Return.ContainsKey(PARAMETER_TEXT))
148 | Return.Remove(PARAMETER_TEXT);
149 |
150 | ResetMessage = false;
151 | }
152 |
153 | if (!Return.ContainsKey(PARAMETER_TEXT))
154 | {
155 | Return.Add(PARAMETER_TEXT, new StringBuilder(arg));
156 | }
157 | else
158 | {
159 | ((StringBuilder)Return[PARAMETER_TEXT]).Append(" ").Append(arg);
160 | }
161 | }
162 | }
163 |
164 | return Return;
165 | }
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/Properties/src/Properties.cpp:
--------------------------------------------------------------------------------
1 | /*
Properties.cpp - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Properties.h"
PropertiesClass::PropertiesClass() {
this->mainPosition = DEFAULT_POSITION;
this->count = 0;
this->size = 0;
this->mallocSize = 0;
}
bool PropertiesClass::load() {
//free any previous loaded data
this->flush();
//load variable count
this->readEEPROM(this->mainPosition, (byte*)&this->count, sizeof(this->count));
//validate variable count
if ( this->count > 0 && this->count < MAX_VARIABLES ) {
//load data size
this->readEEPROM(this->mainPosition+sizeof(this->count), (byte*)&this->size, sizeof(this->size));
//copy data
if (this->size > 0) {
this->properties = (PropertyInformation*) malloc(this->size);
this->readEEPROM(this->mainPosition+sizeof(this->count)+sizeof(this->size), (byte*)this->properties, this->size);
this->fixPointers();
}
return true;
} else {
this->count = 0;
return false;
}
}
bool PropertiesClass::load(short position) {
this->mainPosition = position;
return this->load();
}
void PropertiesClass::save() {
this->writeEEPROM(this->mainPosition, (byte*)&this->count, sizeof(this->count));
this->writeEEPROM(this->mainPosition+sizeof(this->count), (byte*)&this->size, this->mainPosition+sizeof(this->size));
this->writeEEPROM(this->mainPosition+sizeof(this->count)+sizeof(this->size), (byte*)this->properties, this->size);
}
void PropertiesClass::save(short position) {
this->mainPosition = position;
this->save();
}
void PropertiesClass::readEEPROM(short position, byte* data, short size) {
for (this->index = 0; this->index < size; this->index++) {
*data++ = eeprom_read_byte((unsigned char *) position++);
}
}
void PropertiesClass::writeEEPROM(short position, byte* data, short size) {
for (this->index = 0; this->index < size; this->index++) {
eeprom_write_byte((unsigned char *) position++, *data++);
}
}
void PropertiesClass::addProperty(short propertyId, short dataSize) {
if ( this->count < MAX_VARIABLES ) {
if (this->count > 0) {
this->size += sizeof(PropertyInformation)+dataSize;
this->properties = (PropertyInformation*) realloc(this->properties, this->size);
this->mallocSize = this->size;
} else {
this->size = sizeof(PropertyInformation)+dataSize;
if ( this->mallocSize < this->size ) {
this->properties = (PropertyInformation*) malloc(this->size);
this->mallocSize = this->size;
}
}
this->setPosition(this->count);
this->currentProperty->propertyId = propertyId;
this->currentProperty->valueSize = dataSize;
this->currentProperty->value = (this->currentProperty)+1; //the value is at end of structure
this->count++;
this->fixPointers();
}
}
void PropertiesClass::flush() {
if (this->mallocSize > 0)
free(this->properties);
this->count = 0;
this->size = 0;
this->mallocSize = 0;
}
void PropertiesClass::setPosition(short position) {
this->currentProperty = this->properties;
if (position > 0)
for (this->index = 1; this->index <= position; this->index++)
this->currentProperty = (PropertyInformation*) (((unsigned short)this->currentProperty)+sizeof(PropertyInformation)+this->currentProperty->valueSize);
}
bool PropertiesClass::findProperty(short propertyId) {
if (this->count > 0) {
//try first one
this->index = 0;
this->currentProperty = this->properties;
if (this->currentProperty->propertyId == propertyId)
return true;
//the remaining
for (this->index = 1; this->index < this->count; this->index++) {
this->currentProperty = (PropertyInformation*) (((unsigned short)this->currentProperty)+sizeof(PropertyInformation)+this->currentProperty->valueSize);
if (this->currentProperty->propertyId == propertyId)
return true;
}
}
//not found
return false;
}
void PropertiesClass::remove(short propertyId) {
if (this->findProperty(propertyId)) {
this->size -= (this->currentProperty->valueSize+sizeof(PropertyInformation));
if (this->count > 1) {
//move data if needed
if (this->index < this->count-1) {
//copy destination location
this->moveProperty = this->currentProperty;
//calculate data size to move
short moveSize=0;
for (;this->index < (this->count-1); this->index++) {
this->currentProperty = (PropertyInformation*) (((unsigned short)this->currentProperty)+sizeof(PropertyInformation)+this->currentProperty->valueSize);
moveSize += sizeof(PropertyInformation)+this->currentProperty->valueSize;
}
memmove(this->moveProperty,(void*) ((unsigned short)this->moveProperty+sizeof(PropertyInformation)+this->moveProperty->valueSize),moveSize);
}
}
this->count--;
if (this->size <= 0)
this->flush();
this->fixPointers();
}
}
void PropertiesClass::fixPointers() {
for (this->index = 0; this->index < this->count; this->index++) {
if (this->index == 0)
this->currentProperty = this->properties;
else
this->currentProperty = (PropertyInformation*) (((unsigned short)this->currentProperty)+sizeof(PropertyInformation)+this->currentProperty->valueSize);
this->currentProperty->value = (this->currentProperty)+1; //the value is at end of structure
}
}
void PropertiesClass::set(short propertyId, int value) {
this->set(propertyId,(void*)&value,sizeof(int));
}
void PropertiesClass::set(short propertyId, long value) {
this->set(propertyId,(void*)&value,sizeof(long));
}
void PropertiesClass::set(short propertyId, void* data, short dataSize) {
if (this->findProperty(propertyId)) {
//verify if size was changed
if (dataSize != this->currentProperty->valueSize) {
this->remove(propertyId);
this->addProperty(propertyId, dataSize);
}
} else {
this->addProperty(propertyId, dataSize);
}
memcpy(this->currentProperty->value,data,dataSize);
}
int PropertiesClass::getInt(short propertyId) {
if (this->findProperty(propertyId)) {
memcpy(&this->lastInt,this->currentProperty->value,this->currentProperty->valueSize);
return this->lastInt;
} else {
return -1;
}
}
long PropertiesClass::getLong(short propertyId) {
if (this->findProperty(propertyId)) {
memcpy(&this->lastLong,this->currentProperty->value,this->currentProperty->valueSize);
return this->lastLong;
} else {
return -1;
}
}
PropertyInformation* PropertiesClass::get(short propertyId) {
if (this->findProperty(propertyId)) {
return this->currentProperty;
} else {
return NULL;
}
}
PropertiesClass Properties;
--------------------------------------------------------------------------------
/RTCTimedEvent/src/RTCTimedEvent.cpp:
--------------------------------------------------------------------------------
1 | /*
RTCTimedEvent.cpp - Event-Based Library for Arduino.
Copyright (c) 2011, Renato A. Ferreira
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "RTCTimedEvent.h"
RTCTimedEventClass::RTCTimedEventClass() {
this->count = 0;
this->mallocSize = 0;
this->nextMillis = -1;
this->initialCapacity = sizeof(RTCTimerInformation);
Wire.begin();
}
void RTCTimedEventClass::readRTC() {
//register
Wire.beginTransmission(RTC_ADDRESS_DS1307);
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write((byte)0x00);
#else
Wire.send(0x00);
#endif
Wire.endTransmission();
//request data
Wire.requestFrom(RTC_ADDRESS_DS1307, 7);
//read RTC data
#if defined(ARDUINO) && ARDUINO >= 100
this->time.second = bcdToDec(Wire.read() & 0x7f);
this->time.minute = bcdToDec(Wire.read());
this->time.hour = bcdToDec(Wire.read() & 0x3f);
this->time.dayOfWeek = bcdToDec(Wire.read());
this->time.day = bcdToDec(Wire.read());
this->time.month = bcdToDec(Wire.read());
this->time.year = bcdToDec(Wire.read())+RTC_BASEYR_DS1307;
#else
this->time.second = bcdToDec(Wire.receive() & 0x7f);
this->time.minute = bcdToDec(Wire.receive());
this->time.hour = bcdToDec(Wire.receive() & 0x3f);
this->time.dayOfWeek = bcdToDec(Wire.receive());
this->time.day = bcdToDec(Wire.receive());
this->time.month = bcdToDec(Wire.receive());
this->time.year = bcdToDec(Wire.receive())+RTC_BASEYR_DS1307;
#endif
}
void RTCTimedEventClass::writeRTC() {
//stop RTC
this->switchRTC(false);
//register
Wire.beginTransmission(RTC_ADDRESS_DS1307);
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write((byte)0x00);
#else
Wire.send(0x00);
#endif
//set RTC data
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(decToBcd(this->time.second));
Wire.write(decToBcd(this->time.minute));
Wire.write(decToBcd(this->time.hour));
Wire.write(decToBcd(this->time.dayOfWeek));
Wire.write(decToBcd(this->time.day));
Wire.write(decToBcd(this->time.month));
Wire.write(decToBcd(this->time.year-RTC_BASEYR_DS1307));
#else
Wire.send(decToBcd(this->time.second));
Wire.send(decToBcd(this->time.minute));
Wire.send(decToBcd(this->time.hour));
Wire.send(decToBcd(this->time.dayOfWeek));
Wire.send(decToBcd(this->time.day));
Wire.send(decToBcd(this->time.month));
Wire.send(decToBcd(this->time.year-RTC_BASEYR_DS1307));
#endif
Wire.endTransmission();
//start RTC
this->switchRTC(true);
}
void RTCTimedEventClass::switchRTC(bool turnOn) {
//register
Wire.beginTransmission(RTC_ADDRESS_DS1307);
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write((byte)0x00);
#else
Wire.send(0x00);
#endif
//set RTC data
#if defined(ARDUINO) && ARDUINO >= 100
if (turnOn) {
Wire.write(decToBcd(this->time.second));
} else {
Wire.write(decToBcd(this->time.second) | RTC_HALT_DS1307);
}
Wire.write(decToBcd(this->time.minute));
Wire.write(decToBcd(this->time.hour));
Wire.write(decToBcd(this->time.dayOfWeek));
Wire.write(decToBcd(this->time.day));
Wire.write(decToBcd(this->time.month));
Wire.write(decToBcd(this->time.year-RTC_BASEYR_DS1307));
#else
if (turnOn) {
Wire.send(decToBcd(this->time.second));
} else {
Wire.send(decToBcd(this->time.second) | RTC_HALT_DS1307);
}
Wire.send(decToBcd(this->time.minute));
Wire.send(decToBcd(this->time.hour));
Wire.send(decToBcd(this->time.dayOfWeek));
Wire.send(decToBcd(this->time.day));
Wire.send(decToBcd(this->time.month));
Wire.send(decToBcd(this->time.year-RTC_BASEYR_DS1307));
#endif
Wire.endTransmission();
}
void RTCTimedEventClass::addTimer(byte minute, byte hour, byte day, byte month, byte dayOfWeek, void (*onEvent)(RTCTimerInformation* Sender)) {
this->addTimer(DEFAULT_TIMER_ID, minute, hour, day, month, dayOfWeek, onEvent);
}
void RTCTimedEventClass::addTimer(short eventId, byte minute, byte hour, byte day, byte month, byte dayOfWeek, void (*onEvent)(RTCTimerInformation* Sender)) {
if (this->count > 0) {
//determine if the buffer free space fits the next object
if (this->mallocSize < (sizeof(RTCTimerInformation)*(this->count+1))) {
this->mallocSize = sizeof(RTCTimerInformation)*(this->count+1);
//alocate more memory space
this->timers = (RTCTimerInformation*) realloc(this->timers, this->mallocSize);
}
} else {
//determine if initial capacity parameter fits the first object
if (this->initialCapacity >= sizeof(RTCTimerInformation)) {
this->mallocSize = this->initialCapacity;
} else {
this->mallocSize = sizeof(RTCTimerInformation);
}
//create the buffer size
this->timers = (RTCTimerInformation*) malloc(this->mallocSize);
}
this->currentTimer = this->timers+this->count; //array index
this->currentTimer->eventId = eventId;
this->currentTimer->minute = minute;
this->currentTimer->hour = hour;
this->currentTimer->dayOfWeek = dayOfWeek;
this->currentTimer->day = day;
this->currentTimer->month = month;
this->currentTimer->onEvent = onEvent;
this->count++;
}
void RTCTimedEventClass::clear() {
if (this->mallocSize > 0)
free(this->timers);
this->count = 0;
this->mallocSize = 0;
}
void RTCTimedEventClass::loop() {
this->currMillis = millis();
if (this->nextMillis == -1 || this->currMillis < this->lastMillis) { //prepare first step and handle millis() overflow
//prepare next step
this->readRTC(); //read current time
this->nextMillis = 60-this->time.second; //seconds to next minute
this->nextMillis *= 1000; //convert to milliseconds
this->nextMillis += this->currMillis; //add current millis
this->lastMinute = this->time.minute; //copy minute to avoid repetitions
} else if (this->currMillis>=this->nextMillis) { //execute
this->readRTC();
this->currMinute = this->time.minute;
if ( this->lastMinute != this->currMinute ) {
for (this->index = 0; this->index < this->count; this->index++) {
this->currentTimer = this->timers+this->index;
if (this->currentTimer->minute == this->currMinute || this->currentTimer->minute == TIMER_ANY) {
if (this->currentTimer->hour == this->time.hour || this->currentTimer->hour == TIMER_ANY) {
if (this->currentTimer->dayOfWeek == this->time.dayOfWeek || this->currentTimer->dayOfWeek == TIMER_ANY) {
if (this->currentTimer->day == this->time.day || this->currentTimer->day == TIMER_ANY) {
if (this->currentTimer->month == this->time.month || this->currentTimer->month == TIMER_ANY) {
this->currentTimer->onEvent(this->currentTimer);
}
}
}
}
}
}
}
//prepare next step
this->readRTC(); //read current time
this->nextMillis = 60-this->time.second; //seconds to next minute
this->nextMillis *= 1000; //convert to milliseconds
this->nextMillis += this->currMillis; //add current millis
this->lastMinute = this->currMinute; //copy minute to avoid repetitions
}
this->lastMillis = this->currMillis;
}
byte RTCTimedEventClass::decToBcd(byte value)
{
return (value/10*16) + (value%10);
}
byte RTCTimedEventClass::bcdToDec(byte value)
{
return (value/16*10) + (value%16);
}
RTCTimedEventClass RTCTimedEvent;
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/Release/dotnet.XML:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | dotnet
5 |
6 |
7 |
8 |
9 | Serial client for AdvancedSerial.
10 |
11 |
12 |
13 |
14 | Protocol implementation for AdvancedSerial.
15 |
16 |
17 |
18 |
19 | Size of message header.
20 |
21 |
22 |
23 |
24 | Size of message delimiter.
25 |
26 |
27 |
28 |
29 | Maximum size of message payload.
30 |
31 |
32 |
33 |
34 | Maximum size of message.
35 |
36 |
37 |
38 |
39 | Start of message.
40 |
41 |
42 |
43 |
44 | End of message.
45 |
46 |
47 |
48 |
49 | Protocol stream.
50 |
51 |
52 |
53 |
54 | Verify transport connection.
55 |
56 | Return true if connected.
57 |
58 |
59 |
60 | Close transport connection.
61 |
62 |
63 |
64 |
65 | Timeout for message confirmation.
66 |
67 |
68 |
69 |
70 | Send message.
71 |
72 | Message to send.
73 |
74 |
75 |
76 | Send message.
77 |
78 | Message type.
79 | Message ID.
80 | Payload size.
81 | Message payload
82 |
83 |
84 |
85 | Send simple message without payload.
86 |
87 | Message type.
88 |
89 |
90 |
91 | Clear communication stream.
92 |
93 |
94 |
95 |
96 | Receive next amount of data.
97 |
98 |
99 |
100 |
101 | Occurs when the a message is received.
102 |
103 |
104 |
105 |
106 | Type of AdvancedSerial messages.
107 |
108 |
109 |
110 |
111 | Common message.
112 |
113 |
114 |
115 |
116 | Common message.
117 |
118 |
119 |
120 |
121 | Message acknowledge.
122 |
123 |
124 |
125 |
126 | Discovery request message.
127 |
128 |
129 |
130 |
131 | Discovery response message.
132 |
133 |
134 |
135 |
136 | Message class.
137 |
138 |
139 |
140 |
141 | Type of message.
142 |
143 |
144 |
145 |
146 | Message ID.
147 |
148 |
149 |
150 |
151 | Payload size.
152 |
153 |
154 |
155 |
156 | Message payload.
157 |
158 |
159 |
160 |
161 | Connection states.
162 |
163 |
164 |
165 |
166 | Receiving start of text.
167 |
168 |
169 |
170 |
171 | Receiving end of text.
172 |
173 |
174 |
175 |
176 | Receiving header.
177 |
178 |
179 |
180 |
181 | Receiving payload.
182 |
183 |
184 |
185 |
186 | Delegate method for MessageReceived.
187 |
188 |
189 |
190 |
191 | Serial port.
192 |
193 |
194 |
195 |
196 | Default constructor.
197 |
198 |
199 |
200 |
201 | Verify transport connection.
202 |
203 | Return true if connected.
204 |
205 |
206 |
207 | Close serial port.
208 |
209 |
210 |
211 |
212 | Starts device communication through a serial port.
213 |
214 | Port name.
215 | Port baud rate.
216 |
217 |
218 |
219 |
--------------------------------------------------------------------------------
/AdvancedSerial/clientapi/dotnet/SerialProtocol.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Threading;
4 |
5 | namespace ebl_arduino
6 | {
7 | ///
8 | /// Protocol implementation for AdvancedSerial.
9 | ///
10 | public abstract class SerialProtocol
11 | {
12 | ///
13 | /// Type of AdvancedSerial messages.
14 | ///
15 | public enum MessageTypes
16 | {
17 | ///
18 | /// Common message.
19 | ///
20 | DEBUG = 0x01,
21 |
22 | ///
23 | /// Common message.
24 | ///
25 | MESSAGE = 0x02,
26 |
27 | ///
28 | /// Message acknowledge.
29 | ///
30 | MESSAGE_ACKNOWLEDGE = 0x03,
31 |
32 | ///
33 | /// Discovery request message.
34 | ///
35 | DISCOVERY_REQUEST = 0x04,
36 |
37 | ///
38 | /// Discovery response message.
39 | ///
40 | DISCOVERY_RESPONSE = 0x05
41 | }
42 |
43 | ///
44 | /// Message class.
45 | ///
46 | public class AdvancedSerialMessage
47 | {
48 | ///
49 | /// Type of message.
50 | ///
51 | public MessageTypes Type = MessageTypes.MESSAGE;
52 |
53 | ///
54 | /// Message ID.
55 | ///
56 | public byte ID;
57 |
58 | ///
59 | /// Payload size.
60 | ///
61 | public byte Size = 0;
62 |
63 | ///
64 | /// Message payload.
65 | ///
66 | public byte[] Payload;
67 | }
68 |
69 | ///
70 | /// Connection states.
71 | ///
72 | public enum ConnectionState
73 | {
74 | ///
75 | /// Receiving start of text.
76 | ///
77 | ReadingSTX,
78 |
79 | ///
80 | /// Receiving end of text.
81 | ///
82 | ReadingETX,
83 |
84 | ///
85 | /// Receiving header.
86 | ///
87 | ReadingHeader,
88 |
89 | ///
90 | /// Receiving payload.
91 | ///
92 | ReadingPayload
93 | }
94 |
95 | ///
96 | /// Size of message header.
97 | ///
98 | public const int MESSAGE_HEADER_SIZE = 3;
99 |
100 | ///
101 | /// Size of message delimiter.
102 | ///
103 | public const int MESSAGE_DELIMITER_SIZE = 1;
104 |
105 | ///
106 | /// Maximum size of message payload.
107 | ///
108 | public const int MESSAGE_MAX_PAYLOAD_SIZE = 32;
109 |
110 | ///
111 | /// Maximum size of message.
112 | ///
113 | public const int MESSAGE_SIZE = MESSAGE_MAX_PAYLOAD_SIZE + MESSAGE_HEADER_SIZE;
114 |
115 | ///
116 | /// Start of message.
117 | ///
118 | public const byte STX = 0x02;
119 |
120 | ///
121 | /// End of message.
122 | ///
123 | public const byte ETX = 0x03;
124 |
125 | ///
126 | /// Delegate method for MessageReceived.
127 | ///
128 | public delegate void MessageReceivedCallback(AdvancedSerialMessage Message);
129 |
130 | ///
131 | /// Occurs when the a message is received.
132 | ///
133 | public event MessageReceivedCallback MessageReceived;
134 |
135 | ///
136 | /// Protocol stream.
137 | ///
138 | protected Stream ConnectionStream;
139 |
140 | ///
141 | /// Verify transport connection.
142 | ///
143 | /// Return true if connected.
144 | public abstract bool IsConnected();
145 |
146 | ///
147 | /// Close transport connection.
148 | ///
149 | public abstract void Close();
150 |
151 | ///
152 | /// Timeout for message confirmation.
153 | ///
154 | public int MessageTimeout = 2000;
155 |
156 | private AdvancedSerialMessage InputMessage = new AdvancedSerialMessage();
157 | private byte[] InputBuffer = new byte[MESSAGE_SIZE];
158 | private byte[] InputDelimiter = new byte[1];
159 | private ConnectionState State = ConnectionState.ReadingSTX;
160 | private int ReceivedBytes;
161 | private int RequestedBytes;
162 |
163 | ///
164 | /// Send message.
165 | ///
166 | /// Message to send.
167 | public void Send(AdvancedSerialMessage Message)
168 | {
169 | this.Send(Message.Type, Message.ID, Message.Size, Message.Payload);
170 | }
171 |
172 | ///
173 | /// Send message.
174 | ///
175 | /// Message type.
176 | /// Message ID.
177 | /// Payload size.
178 | /// Message payload
179 | public void Send(MessageTypes Type, byte ID, byte Size, byte[] Payload)
180 | {
181 | if (Size >= 0 && Size <= MESSAGE_MAX_PAYLOAD_SIZE)
182 | {
183 | this.ConnectionStream.WriteByte(STX);
184 | this.ConnectionStream.WriteByte((byte)Type);
185 | this.ConnectionStream.WriteByte(ID);
186 | this.ConnectionStream.WriteByte(Size);
187 | if (Size > 0) this.ConnectionStream.Write(Payload, 0, Size);
188 | this.ConnectionStream.WriteByte(ETX);
189 |
190 | //wait for confirmations
191 | if (Type == MessageTypes.MESSAGE ||
192 | Type == MessageTypes.DISCOVERY_REQUEST)
193 | {
194 | lock (this.ConnectionStream)
195 | {
196 | if (!Monitor.Wait(this.ConnectionStream, this.MessageTimeout))
197 | throw new TimeoutException("Device not responding.");
198 | }
199 | }
200 | }
201 | else
202 | {
203 | throw new TimeoutException("Accepted message's payload size between 0 and " + MESSAGE_MAX_PAYLOAD_SIZE + ".");
204 | }
205 | }
206 |
207 | ///
208 | /// Send simple message without payload.
209 | ///
210 | /// Message type.
211 | public void Send(MessageTypes Type)
212 | {
213 | this.Send(Type, 0, 0, null);
214 | }
215 |
216 | ///
217 | /// Clear communication stream.
218 | ///
219 | public void Flush()
220 | {
221 | byte[] flushdata = new byte[MESSAGE_DELIMITER_SIZE+MESSAGE_SIZE+MESSAGE_DELIMITER_SIZE];
222 | for (int i = 0; i < flushdata.Length; i++)
223 | {
224 | flushdata[i] = (byte)0x00;
225 | }
226 | this.ConnectionStream.Write(flushdata, 0, flushdata.Length);
227 | this.ConnectionStream.Flush();
228 | }
229 |
230 | ///
231 | /// Receive next amount of data.
232 | ///
233 | protected void Receive()
234 | {
235 | this.ReceivedBytes = 0;
236 |
237 | if (this.DataReceivingCallback == null)
238 | this.DataReceivingCallback = new AsyncCallback(DataReceiving);
239 |
240 | try
241 | {
242 | switch (this.State)
243 | {
244 | case ConnectionState.ReadingSTX:
245 | case ConnectionState.ReadingETX:
246 | this.ConnectionStream.BeginRead(this.InputDelimiter, 0, MESSAGE_DELIMITER_SIZE, this.DataReceivingCallback, null);
247 | this.RequestedBytes = MESSAGE_DELIMITER_SIZE;
248 | break;
249 |
250 | case ConnectionState.ReadingHeader:
251 | this.ConnectionStream.BeginRead(this.InputBuffer, 0, MESSAGE_HEADER_SIZE, this.DataReceivingCallback, null);
252 | this.RequestedBytes = MESSAGE_HEADER_SIZE;
253 | break;
254 |
255 | case ConnectionState.ReadingPayload:
256 | this.ConnectionStream.BeginRead(this.InputBuffer, 0, this.InputMessage.Size, this.DataReceivingCallback, null);
257 | this.RequestedBytes = this.InputMessage.Size;
258 | break;
259 | }
260 | }
261 | catch (Exception)
262 | {
263 | this.Close();
264 | }
265 | }
266 |
267 | private AsyncCallback DataReceivingCallback;
268 | private void DataReceiving(IAsyncResult ar)
269 | {
270 | int ReceivedSize = 0;
271 | try
272 | {
273 | ReceivedSize = this.ConnectionStream.EndRead(ar);
274 | }
275 | catch (Exception)
276 | {
277 | this.Close();
278 | return;
279 | }
280 |
281 | //verify if buffer was completelly received
282 | this.ReceivedBytes += ReceivedSize;
283 | if (this.ReceivedBytes < this.RequestedBytes)
284 | {
285 | try
286 | {
287 | this.ConnectionStream.BeginRead(this.InputBuffer, ReceivedBytes, RequestedBytes - ReceivedBytes, this.DataReceivingCallback, null);
288 | }
289 | catch (Exception)
290 | {
291 | this.Close();
292 | }
293 | return;
294 | }
295 |
296 | switch (this.State)
297 | {
298 | case ConnectionState.ReadingSTX:
299 | if (this.InputDelimiter[0] == STX)
300 | {
301 | this.State = ConnectionState.ReadingHeader;
302 | }
303 | break;
304 |
305 | case ConnectionState.ReadingHeader:
306 | this.InputMessage.Type = (MessageTypes)this.InputBuffer[0];
307 | this.InputMessage.Size = this.InputBuffer[1];
308 | if (this.InputMessage.Size == 0)
309 | {
310 | //payload is empty
311 | this.State = ConnectionState.ReadingETX;
312 | }
313 | else if (this.InputMessage.Size > 0 && this.InputMessage.Size <= MESSAGE_MAX_PAYLOAD_SIZE)
314 | {
315 | //read
316 | this.State = ConnectionState.ReadingPayload;
317 | }
318 | else
319 | {
320 | //avoid wrong sized messages
321 | this.State = ConnectionState.ReadingSTX;
322 | }
323 | break;
324 |
325 | case ConnectionState.ReadingPayload:
326 | Array.Copy(this.InputBuffer, this.InputMessage.Payload, this.InputMessage.Size);
327 | this.State = ConnectionState.ReadingETX;
328 | break;
329 |
330 | case ConnectionState.ReadingETX:
331 | if (this.InputDelimiter[0] == ETX)
332 | {
333 | if (this.InputMessage.Type == MessageTypes.DISCOVERY_RESPONSE ||
334 | this.InputMessage.Type == MessageTypes.MESSAGE_ACKNOWLEDGE)
335 | {
336 | lock (this.ConnectionStream)
337 | {
338 | Monitor.Pulse(this.ConnectionStream);
339 | }
340 | }
341 | else if (this.InputMessage.Type == MessageTypes.MESSAGE)
342 | {
343 | this.Send(MessageTypes.MESSAGE_ACKNOWLEDGE);
344 | this.MessageReceived(this.InputMessage);
345 | }
346 | else if (this.InputMessage.Type == MessageTypes.DISCOVERY_REQUEST)
347 | {
348 | this.Send(MessageTypes.DISCOVERY_RESPONSE);
349 | }
350 | }
351 |
352 | this.State = ConnectionState.ReadingSTX;
353 | break;
354 | }
355 |
356 | this.Receive();
357 | }
358 | }
359 | }
360 |
--------------------------------------------------------------------------------