arduino library for mbed

Dependents:   Gameduino2 MemoryGame arduino2

Files at this revision

API Documentation at this revision

Comitter:
aluqard
Date:
Fri Apr 11 07:21:59 2014 +0000
Commit message:
Initial release

Changed in this revision

arduino.cpp Show annotated file Show diff for this revision Revisions of this file
arduino.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arduino.cpp	Fri Apr 11 07:21:59 2014 +0000
@@ -0,0 +1,238 @@
+/**
+ * @section LICENSE
+ *  Copyright (c) 2012
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *  This is a library for allowing the use of programs written for the Arduino
+ *  with the mbed. This was started for use with the Gameduino shield Library.
+ *  It is currently uncomplete and not fully tested, but I don't see myself spending time on it. 
+ *  See TODOs and check http://arduino.cc/en/Reference/HomePage for missing parts
+ *
+ * @file arduino.c
+ *
+ * Example:
+ * @code
+ * 
+ * #include "mbed.h"
+ * #include "arduino.h"
+ * 
+ * void setup(){
+ * // setup code
+ * }
+ *
+ *void loop(){
+ * // loop code
+ *}
+ *
+ * int main() {
+ *   timer_start();
+ *   setup();
+ *   while(1) {
+ *       loop();
+ *   }
+ * }
+ * @endcode
+ */
+ #include "arduino.h"
+ 
+/*****************************************************  
+  Digital I/O
+ 
+    pinMode()- TODO
+    digitalWrite()- TODO
+    digitalRead()- TODO
+    
+******************************************************/
+ 
+/*****************************************************  
+  Analog I/O
+ 
+    analogReference() - TODO
+    analogRead() - TODO
+    analogWrite()-(PWM) - TODO
+    
+*****************************************************/
+ 
+/*****************************************************
+  Advanced I/O
+ 
+    tone() - TODO
+    noTone() - TODO
+    shiftOut() - TODO
+    shiftIn() - TODO
+    pulseIn() - TODO
+    
+*****************************************************/ 
+ 
+/*****************************************************  
+  Time
+    
+    millis() - need to start timer first
+    micros() - need to start timer first
+    delay() - done by Macro
+    delayMicroseconds() - Done byMacro
+    
+*****************************************************/
+ 
+Timer arduino_timer;
+/** start the arduino_timer timer for millis() and micros() running.
+ *
+ * @param void
+ */
+void timer_start(void) {
+    arduino_timer.start();
+}
+/** return a long for the amount of time since the timer was started in milliseconds.
+ *
+ * @param void
+ */
+long millis(void) {
+    return arduino_timer.read_ms();
+}
+/** return a long for the amount of time since the timer was started in microseconds.
+ *
+ * @param void
+ */
+long micros(void) {
+    return arduino_timer.read_us();
+}
+ 
+ 
+/*****************************************************
+  Maths
+ 
+    min()  - done by Macro
+    max()  - done by Macro
+    abs()  - done by Macro
+    constrain() - TODO
+    map() - TODO
+    pow() - implemented by including math.h
+    sqrt() - implemented by including math.h
+    
+*****************************************************/
+ 
+ 
+/*****************************************************
+  Trigonometry
+ 
+    sin() - implemented by including math.h
+    cos() - implemented by including math.h
+    tan() - implemented by including math.h
+    
+*****************************************************/
+ 
+/*****************************************************
+  Random Numbers
+ 
+    randomSeed()  - done by Macro
+    random() - function below
+    
+*****************************************************/
+ 
+/** generates a random number from 0 to defined number
+ *
+ * @param number maximum value for random number
+ */
+int random(int number) {
+    return (rand()%number);
+}
+/** generates a random number between two numbers
+ *
+ * @param numberone minimum value for random number
+ * @param numbertwo maximum value for random number
+ */
+int random(int numberone, int numbertwo) {
+    int random = 0;
+    if ((numberone < 0) && (numbertwo < 0)) {
+        numberone = numberone * -1;
+        numbertwo = numbertwo * -1;
+        random = -1 * (rand()%(numberone + numbertwo));
+    }
+    if ((numbertwo < 0) && (numberone >= 0)) {
+        numbertwo = numbertwo * -1;
+        random = (rand()%(numberone + numbertwo)) - numbertwo;
+    }
+    if ((numberone < 0) && (numbertwo >= 0)) {
+        numberone = numberone * -1;
+        random = (rand()%(numberone + numbertwo)) - numberone;
+    } else {
+        random = (rand()%(numberone + numbertwo)) - min(numberone, numbertwo);
+    }
+    return random;
+}
+ 
+/*****************************************************
+  Bits and Bytes
+     
+     lowByte() - function below
+     highByte() - function below
+     bitRead() - TODO
+     bitWrite() - TODO
+     bitSet() - TODO
+     bitClear() - TODO
+     bit() - TODO
+     
+*****************************************************/
+ 
+/** returns the lower nibble of first byte
+ *
+ * @param high byte of which the high nibble is required
+ */
+byte lowByte(short int low) {
+    byte bytelow = 0;
+    bytelow = (low & 0xFF);
+    return bytelow;
+}
+/** returns the higher nibble of first byte
+ *
+ * @param high byte of which the high nibble is required
+ */
+byte highByte(short int high) {
+    byte bytehigh = 0;
+    bytehigh = ((high >> 8) & 0xFF);
+    return bytehigh;
+}
+ 
+ 
+ 
+/*****************************************************
+  External Interrupts
+    
+    attachInterrupt() - TODO
+    detachInterrupt() - TODO
+    
+*****************************************************/
+    
+/*****************************************************
+  Interrupts
+ 
+    interrupts() - TODO
+    noInterrupts() - TODO
+    
+*****************************************************/
+ 
+/*****************************************************
+  Communication
+ 
+    Serial - TODO
+    Stream - TODO
+    
+*****************************************************/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arduino.h	Fri Apr 11 07:21:59 2014 +0000
@@ -0,0 +1,100 @@
+#ifndef _ARDUINO_H_
+#define _ARDUINO_H_
+ 
+#include "mbed.h"
+#include "math.h"
+// Macros
+ 
+#define PI 3.1415926535897932384626433832795
+#define HALF_PI 1.5707963267948966192313216916398
+#define TWO_PI 6.283185307179586476925286766559
+#define DEG_TO_RAD 0.017453292519943295769236907684886
+#define RAD_TO_DEG 57.295779513082320876798154814105
+
+#define radians(deg) ((deg)*DEG_TO_RAD)
+#define degrees(rad) ((rad)*RAD_TO_DEG)
+#define sq(x) ((x)*(x))
+
+#define pgm_read_word(x)          (*(const short int*)x) 
+#define pgm_read_dword_near(x)    (*(const int*)x)
+#define pgm_read_word_near(x)     (*(const unsigned int*)x)
+#define pgm_read_int_near(x)      (*(const int*)x)
+#define pgm_read_int(x)           (*(const int*)x)
+#define pgm_read_byte(x)          (*(const char*)x)
+#define pgm_read_byte_near(x)     (*(const char*)x)
+#define PROGMEM                   const
+#define char(x)                   ((char)x)
+#define byte(x)                   ((byte)x)
+#define int(x)                    ((int)x)
+#define word(x)                   ((word)x)
+#define long(x)                   ((long)x)
+#define float(x)                  ((float)x)
+
+#define in_range(c, lo, up)  ((uint8_t)c >= lo && (uint8_t)c <= up)
+#define isprint(c)           in_range(c, 0x20, 0x7f)
+#define isdigit(c)           in_range(c, '0', '9')
+#define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
+#define islower(c)           in_range(c, 'a', 'z')
+#define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
+ 
+/** Macro for delay() 
+ *
+ * @param void
+ */
+#define delay(x)                  (wait_ms(x))
+/** Macro for delayMicroseconds()  
+ *
+ * @param void
+ */
+#define delayMicroseconds(x)      (wait_us(x))
+ 
+/** Macro for min() 
+ *
+ * @param any
+ */
+#define min(a,b)                  ((a)<(b)?(a):(b))
+/** Macro for max()
+ *
+ * @param any
+ */
+#define max(a,b)                  ((a)>(b)?(a):(b))
+/** Macro for abs()
+ *
+ * @param any
+ */
+#define abs(x)                    ((x)>0?(x):(x*-1))
+ 
+/** Macro for randomSeed()
+ *
+ * @param int
+ */
+#define randomSeed(x)             srand(x)
+ 
+#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
+#define bitSet(value, bit) ((value) |= (1UL << (bit)))
+#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
+#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
+ 
+// typedefs
+ 
+typedef unsigned char prog_uchar;
+typedef unsigned char prog_uint8_t;
+typedef unsigned int prog_uint16_t;
+typedef unsigned int prog_uint32_t;
+typedef unsigned char byte;
+typedef bool boolean;
+typedef unsigned char prog_uchar;
+typedef signed char prog_char;
+typedef signed long int word;
+ 
+// function prototypes
+ 
+void timer_start(void);
+long millis(void);
+long micros(void);
+byte lowByte(short int low);
+byte highByte(short int high);
+
+int random(int number);
+int random(int numberone, int numbertwo);
+#endif
\ No newline at end of file