Library to allow the use of Arduino specific language with the mbed

Dependents:   Gameduino Factory_monitor_0v1 mbed_mindflex NEW_NFC_TEST

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

Committer:
TheChrisyd
Date:
Sun Oct 21 14:26:18 2012 +0000
Revision:
0:8d785e80d3ba
Child:
1:b22cf6a5957d
commit for publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 0:8d785e80d3ba 1 /**
TheChrisyd 0:8d785e80d3ba 2 * @section LICENSE
TheChrisyd 0:8d785e80d3ba 3 * Copyright (c) 2012
TheChrisyd 0:8d785e80d3ba 4 *
TheChrisyd 0:8d785e80d3ba 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
TheChrisyd 0:8d785e80d3ba 6 * of this software and associated documentation files (the "Software"), to deal
TheChrisyd 0:8d785e80d3ba 7 * in the Software without restriction, including without limitation the rights
TheChrisyd 0:8d785e80d3ba 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
TheChrisyd 0:8d785e80d3ba 9 * copies of the Software, and to permit persons to whom the Software is
TheChrisyd 0:8d785e80d3ba 10 * furnished to do so, subject to the following conditions:
TheChrisyd 0:8d785e80d3ba 11 *
TheChrisyd 0:8d785e80d3ba 12 * The above copyright notice and this permission notice shall be included in
TheChrisyd 0:8d785e80d3ba 13 * all copies or substantial portions of the Software.
TheChrisyd 0:8d785e80d3ba 14 *
TheChrisyd 0:8d785e80d3ba 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
TheChrisyd 0:8d785e80d3ba 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
TheChrisyd 0:8d785e80d3ba 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
TheChrisyd 0:8d785e80d3ba 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
TheChrisyd 0:8d785e80d3ba 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
TheChrisyd 0:8d785e80d3ba 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
TheChrisyd 0:8d785e80d3ba 21 * THE SOFTWARE.
TheChrisyd 0:8d785e80d3ba 22 *
TheChrisyd 0:8d785e80d3ba 23 * @section DESCRIPTION
TheChrisyd 0:8d785e80d3ba 24 * This is a library for allowing the use of programs written for the Arduino
TheChrisyd 0:8d785e80d3ba 25 * with the mbed. This was started for use with the Gameduino shield Library.
TheChrisyd 0:8d785e80d3ba 26 * It is currently uncomplete and not fully tested, but I don't see myself spending time on it.
TheChrisyd 0:8d785e80d3ba 27 * See TODOs and check http://arduino.cc/en/Reference/HomePage for missing parts
TheChrisyd 0:8d785e80d3ba 28 *
TheChrisyd 0:8d785e80d3ba 29 * @file arduino.c
TheChrisyd 0:8d785e80d3ba 30 *
TheChrisyd 0:8d785e80d3ba 31 * Example:
TheChrisyd 0:8d785e80d3ba 32 * @code
TheChrisyd 0:8d785e80d3ba 33 *
TheChrisyd 0:8d785e80d3ba 34 * #include "mbed.h"
TheChrisyd 0:8d785e80d3ba 35 * #include "arduino.h"
TheChrisyd 0:8d785e80d3ba 36 *
TheChrisyd 0:8d785e80d3ba 37 * void setup(){
TheChrisyd 0:8d785e80d3ba 38 * // setup code
TheChrisyd 0:8d785e80d3ba 39 * }
TheChrisyd 0:8d785e80d3ba 40 *
TheChrisyd 0:8d785e80d3ba 41 *void loop(){
TheChrisyd 0:8d785e80d3ba 42 * // loop code
TheChrisyd 0:8d785e80d3ba 43 *}
TheChrisyd 0:8d785e80d3ba 44 *
TheChrisyd 0:8d785e80d3ba 45 * int main() {
TheChrisyd 0:8d785e80d3ba 46 * timer_start();
TheChrisyd 0:8d785e80d3ba 47 * setup();
TheChrisyd 0:8d785e80d3ba 48 * while(1) {
TheChrisyd 0:8d785e80d3ba 49 * loop();
TheChrisyd 0:8d785e80d3ba 50 * }
TheChrisyd 0:8d785e80d3ba 51 * }
TheChrisyd 0:8d785e80d3ba 52 * @endcode
TheChrisyd 0:8d785e80d3ba 53 */
TheChrisyd 0:8d785e80d3ba 54 #include "arduino.h"
TheChrisyd 0:8d785e80d3ba 55
TheChrisyd 0:8d785e80d3ba 56 /*****************************************************
TheChrisyd 0:8d785e80d3ba 57 Digital I/O
TheChrisyd 0:8d785e80d3ba 58
TheChrisyd 0:8d785e80d3ba 59 pinMode()- TODO
TheChrisyd 0:8d785e80d3ba 60 digitalWrite()- TODO
TheChrisyd 0:8d785e80d3ba 61 digitalRead()- TODO
TheChrisyd 0:8d785e80d3ba 62
TheChrisyd 0:8d785e80d3ba 63 ******************************************************/
TheChrisyd 0:8d785e80d3ba 64
TheChrisyd 0:8d785e80d3ba 65 /*****************************************************
TheChrisyd 0:8d785e80d3ba 66 Analog I/O
TheChrisyd 0:8d785e80d3ba 67
TheChrisyd 0:8d785e80d3ba 68 analogReference() - TODO
TheChrisyd 0:8d785e80d3ba 69 analogRead() - TODO
TheChrisyd 0:8d785e80d3ba 70 analogWrite()-(PWM) - TODO
TheChrisyd 0:8d785e80d3ba 71
TheChrisyd 0:8d785e80d3ba 72 *****************************************************/
TheChrisyd 0:8d785e80d3ba 73
TheChrisyd 0:8d785e80d3ba 74 /*****************************************************
TheChrisyd 0:8d785e80d3ba 75 Advanced I/O
TheChrisyd 0:8d785e80d3ba 76
TheChrisyd 0:8d785e80d3ba 77 tone() - TODO
TheChrisyd 0:8d785e80d3ba 78 noTone() - TODO
TheChrisyd 0:8d785e80d3ba 79 shiftOut() - TODO
TheChrisyd 0:8d785e80d3ba 80 shiftIn() - TODO
TheChrisyd 0:8d785e80d3ba 81 pulseIn() - TODO
TheChrisyd 0:8d785e80d3ba 82
TheChrisyd 0:8d785e80d3ba 83 *****************************************************/
TheChrisyd 0:8d785e80d3ba 84
TheChrisyd 0:8d785e80d3ba 85 /*****************************************************
TheChrisyd 0:8d785e80d3ba 86 Time
TheChrisyd 0:8d785e80d3ba 87
TheChrisyd 0:8d785e80d3ba 88 millis() - need to start timer first
TheChrisyd 0:8d785e80d3ba 89 micros() - need to start timer first
TheChrisyd 0:8d785e80d3ba 90 delay() - done by Macro
TheChrisyd 0:8d785e80d3ba 91 delayMicroseconds() - Done byMacro
TheChrisyd 0:8d785e80d3ba 92
TheChrisyd 0:8d785e80d3ba 93 *****************************************************/
TheChrisyd 0:8d785e80d3ba 94
TheChrisyd 0:8d785e80d3ba 95 Timer arduino_timer;
TheChrisyd 0:8d785e80d3ba 96 /** start the arduino_timer timer for millis() and micros() running.
TheChrisyd 0:8d785e80d3ba 97 *
TheChrisyd 0:8d785e80d3ba 98 * @param void
TheChrisyd 0:8d785e80d3ba 99 */
TheChrisyd 0:8d785e80d3ba 100 void timer_start(void) {
TheChrisyd 0:8d785e80d3ba 101 arduino_timer.start();
TheChrisyd 0:8d785e80d3ba 102 }
TheChrisyd 0:8d785e80d3ba 103 /** return a long for the amount of time since the timer was started in milliseconds.
TheChrisyd 0:8d785e80d3ba 104 *
TheChrisyd 0:8d785e80d3ba 105 * @param void
TheChrisyd 0:8d785e80d3ba 106 */
TheChrisyd 0:8d785e80d3ba 107 long millis(void) {
TheChrisyd 0:8d785e80d3ba 108 return arduino_timer.read_ms();
TheChrisyd 0:8d785e80d3ba 109 }
TheChrisyd 0:8d785e80d3ba 110 /** return a long for the amount of time since the timer was started in microseconds.
TheChrisyd 0:8d785e80d3ba 111 *
TheChrisyd 0:8d785e80d3ba 112 * @param void
TheChrisyd 0:8d785e80d3ba 113 */
TheChrisyd 0:8d785e80d3ba 114 long micros(void) {
TheChrisyd 0:8d785e80d3ba 115 return arduino_timer.read_us();
TheChrisyd 0:8d785e80d3ba 116 }
TheChrisyd 0:8d785e80d3ba 117
TheChrisyd 0:8d785e80d3ba 118 /** Macro for delay()
TheChrisyd 0:8d785e80d3ba 119 *
TheChrisyd 0:8d785e80d3ba 120 * @param void
TheChrisyd 0:8d785e80d3ba 121 */
TheChrisyd 0:8d785e80d3ba 122 #define delay(x) (wait_ms(x))
TheChrisyd 0:8d785e80d3ba 123 /** Macro for delayMicroseconds()
TheChrisyd 0:8d785e80d3ba 124 *
TheChrisyd 0:8d785e80d3ba 125 * @param void
TheChrisyd 0:8d785e80d3ba 126 */
TheChrisyd 0:8d785e80d3ba 127 #define delayMicroseconds(x) (wait_us(x))
TheChrisyd 0:8d785e80d3ba 128
TheChrisyd 0:8d785e80d3ba 129 /*****************************************************
TheChrisyd 0:8d785e80d3ba 130 Maths
TheChrisyd 0:8d785e80d3ba 131
TheChrisyd 0:8d785e80d3ba 132 min() - done by Macro
TheChrisyd 0:8d785e80d3ba 133 max() - done by Macro
TheChrisyd 0:8d785e80d3ba 134 abs() - done by Macro
TheChrisyd 0:8d785e80d3ba 135 constrain() - TODO
TheChrisyd 0:8d785e80d3ba 136 map() - TODO
TheChrisyd 0:8d785e80d3ba 137 pow() - implemented by including math.h
TheChrisyd 0:8d785e80d3ba 138 sqrt() - implemented by including math.h
TheChrisyd 0:8d785e80d3ba 139
TheChrisyd 0:8d785e80d3ba 140 *****************************************************/
TheChrisyd 0:8d785e80d3ba 141
TheChrisyd 0:8d785e80d3ba 142 /** Macro for min()
TheChrisyd 0:8d785e80d3ba 143 *
TheChrisyd 0:8d785e80d3ba 144 * @param any
TheChrisyd 0:8d785e80d3ba 145 */
TheChrisyd 0:8d785e80d3ba 146 #define min(a,b) ((a)<(b)?(a):(b))
TheChrisyd 0:8d785e80d3ba 147 /** Macro for max()
TheChrisyd 0:8d785e80d3ba 148 *
TheChrisyd 0:8d785e80d3ba 149 * @param any
TheChrisyd 0:8d785e80d3ba 150 */
TheChrisyd 0:8d785e80d3ba 151 #define max(a,b) ((a)>(b)?(a):(b))
TheChrisyd 0:8d785e80d3ba 152 /** Macro for abs()
TheChrisyd 0:8d785e80d3ba 153 *
TheChrisyd 0:8d785e80d3ba 154 * @param any
TheChrisyd 0:8d785e80d3ba 155 */
TheChrisyd 0:8d785e80d3ba 156 #define abs(x) ((x)>0?(x):(x*-1))
TheChrisyd 0:8d785e80d3ba 157
TheChrisyd 0:8d785e80d3ba 158 /*****************************************************
TheChrisyd 0:8d785e80d3ba 159 Trigonometry
TheChrisyd 0:8d785e80d3ba 160
TheChrisyd 0:8d785e80d3ba 161 sin() - implemented by including math.h
TheChrisyd 0:8d785e80d3ba 162 cos() - implemented by including math.h
TheChrisyd 0:8d785e80d3ba 163 tan() - implemented by including math.h
TheChrisyd 0:8d785e80d3ba 164
TheChrisyd 0:8d785e80d3ba 165 *****************************************************/
TheChrisyd 0:8d785e80d3ba 166
TheChrisyd 0:8d785e80d3ba 167 /*****************************************************
TheChrisyd 0:8d785e80d3ba 168 Random Numbers
TheChrisyd 0:8d785e80d3ba 169
TheChrisyd 0:8d785e80d3ba 170 randomSeed() - done by Macro
TheChrisyd 0:8d785e80d3ba 171 random() - function below
TheChrisyd 0:8d785e80d3ba 172
TheChrisyd 0:8d785e80d3ba 173 *****************************************************/
TheChrisyd 0:8d785e80d3ba 174
TheChrisyd 0:8d785e80d3ba 175 /** Macro for randomSeed()
TheChrisyd 0:8d785e80d3ba 176 *
TheChrisyd 0:8d785e80d3ba 177 * @param int
TheChrisyd 0:8d785e80d3ba 178 */
TheChrisyd 0:8d785e80d3ba 179 #define randomSeed(x) srand(x)
TheChrisyd 0:8d785e80d3ba 180 /** generates a random number from 0 to defined number
TheChrisyd 0:8d785e80d3ba 181 *
TheChrisyd 0:8d785e80d3ba 182 * @param number maximum value for random number
TheChrisyd 0:8d785e80d3ba 183 */
TheChrisyd 0:8d785e80d3ba 184 int random(int number) {
TheChrisyd 0:8d785e80d3ba 185 return (rand()%number);
TheChrisyd 0:8d785e80d3ba 186 }
TheChrisyd 0:8d785e80d3ba 187 /** generates a random number between two numbers
TheChrisyd 0:8d785e80d3ba 188 *
TheChrisyd 0:8d785e80d3ba 189 * @param numberone minimum value for random number
TheChrisyd 0:8d785e80d3ba 190 * @param numbertwo maximum value for random number
TheChrisyd 0:8d785e80d3ba 191 */
TheChrisyd 0:8d785e80d3ba 192 int random(int numberone, int numbertwo) {
TheChrisyd 0:8d785e80d3ba 193 int random = 0;
TheChrisyd 0:8d785e80d3ba 194 if ((numberone < 0) && (numbertwo < 0)) {
TheChrisyd 0:8d785e80d3ba 195 numberone = numberone * -1;
TheChrisyd 0:8d785e80d3ba 196 numbertwo = numbertwo * -1;
TheChrisyd 0:8d785e80d3ba 197 random = -1 * (rand()%(numberone + numbertwo));
TheChrisyd 0:8d785e80d3ba 198 }
TheChrisyd 0:8d785e80d3ba 199 if ((numbertwo < 0) && (numberone >= 0)) {
TheChrisyd 0:8d785e80d3ba 200 numbertwo = numbertwo * -1;
TheChrisyd 0:8d785e80d3ba 201 random = (rand()%(numberone + numbertwo)) - numbertwo;
TheChrisyd 0:8d785e80d3ba 202 }
TheChrisyd 0:8d785e80d3ba 203 if ((numberone < 0) && (numbertwo >= 0)) {
TheChrisyd 0:8d785e80d3ba 204 numberone = numberone * -1;
TheChrisyd 0:8d785e80d3ba 205 random = (rand()%(numberone + numbertwo)) - numberone;
TheChrisyd 0:8d785e80d3ba 206 } else {
TheChrisyd 0:8d785e80d3ba 207 random = (rand()%(numberone + numbertwo)) - min(numberone, numbertwo);
TheChrisyd 0:8d785e80d3ba 208 }
TheChrisyd 0:8d785e80d3ba 209 return random;
TheChrisyd 0:8d785e80d3ba 210 }
TheChrisyd 0:8d785e80d3ba 211
TheChrisyd 0:8d785e80d3ba 212 /*****************************************************
TheChrisyd 0:8d785e80d3ba 213 Bits and Bytes
TheChrisyd 0:8d785e80d3ba 214
TheChrisyd 0:8d785e80d3ba 215 lowByte() - function below
TheChrisyd 0:8d785e80d3ba 216 highByte() - function below
TheChrisyd 0:8d785e80d3ba 217 bitRead() - TODO
TheChrisyd 0:8d785e80d3ba 218 bitWrite() - TODO
TheChrisyd 0:8d785e80d3ba 219 bitSet() - TODO
TheChrisyd 0:8d785e80d3ba 220 bitClear() - TODO
TheChrisyd 0:8d785e80d3ba 221 bit() - TODO
TheChrisyd 0:8d785e80d3ba 222
TheChrisyd 0:8d785e80d3ba 223 *****************************************************/
TheChrisyd 0:8d785e80d3ba 224
TheChrisyd 0:8d785e80d3ba 225 /** returns the lower nibble of first byte
TheChrisyd 0:8d785e80d3ba 226 *
TheChrisyd 0:8d785e80d3ba 227 * @param high byte of which the high nibble is required
TheChrisyd 0:8d785e80d3ba 228 */
TheChrisyd 0:8d785e80d3ba 229 byte lowByte(short int low) {
TheChrisyd 0:8d785e80d3ba 230 byte bytelow = 0;
TheChrisyd 0:8d785e80d3ba 231 bytelow = (low & 0xFF);
TheChrisyd 0:8d785e80d3ba 232 return bytelow;
TheChrisyd 0:8d785e80d3ba 233 }
TheChrisyd 0:8d785e80d3ba 234 /** returns the higher nibble of first byte
TheChrisyd 0:8d785e80d3ba 235 *
TheChrisyd 0:8d785e80d3ba 236 * @param high byte of which the high nibble is required
TheChrisyd 0:8d785e80d3ba 237 */
TheChrisyd 0:8d785e80d3ba 238 byte highByte(short int high) {
TheChrisyd 0:8d785e80d3ba 239 byte bytehigh = 0;
TheChrisyd 0:8d785e80d3ba 240 bytehigh = ((high >> 8) & 0xFF);
TheChrisyd 0:8d785e80d3ba 241 return bytehigh;
TheChrisyd 0:8d785e80d3ba 242 }
TheChrisyd 0:8d785e80d3ba 243
TheChrisyd 0:8d785e80d3ba 244
TheChrisyd 0:8d785e80d3ba 245
TheChrisyd 0:8d785e80d3ba 246 /*****************************************************
TheChrisyd 0:8d785e80d3ba 247 External Interrupts
TheChrisyd 0:8d785e80d3ba 248
TheChrisyd 0:8d785e80d3ba 249 attachInterrupt() - TODO
TheChrisyd 0:8d785e80d3ba 250 detachInterrupt() - TODO
TheChrisyd 0:8d785e80d3ba 251
TheChrisyd 0:8d785e80d3ba 252 *****************************************************/
TheChrisyd 0:8d785e80d3ba 253
TheChrisyd 0:8d785e80d3ba 254 /*****************************************************
TheChrisyd 0:8d785e80d3ba 255 Interrupts
TheChrisyd 0:8d785e80d3ba 256
TheChrisyd 0:8d785e80d3ba 257 interrupts() - TODO
TheChrisyd 0:8d785e80d3ba 258 noInterrupts() - TODO
TheChrisyd 0:8d785e80d3ba 259
TheChrisyd 0:8d785e80d3ba 260 *****************************************************/
TheChrisyd 0:8d785e80d3ba 261
TheChrisyd 0:8d785e80d3ba 262 /*****************************************************
TheChrisyd 0:8d785e80d3ba 263 Communication
TheChrisyd 0:8d785e80d3ba 264
TheChrisyd 0:8d785e80d3ba 265 Serial - TODO
TheChrisyd 0:8d785e80d3ba 266 Stream - TODO
TheChrisyd 0:8d785e80d3ba 267
TheChrisyd 0:8d785e80d3ba 268 *****************************************************/