Library to allow the use of Arduino specific language with the mbed
Dependents: mbed_shiftreg mbed_shiftreg mbed_shiftreg_4 mbed_shiftreg_3
Fork of Arduino by
arduino.c@1:b22cf6a5957d, 2012-10-21 (annotated)
- Committer:
- TheChrisyd
- Date:
- Sun Oct 21 14:47:26 2012 +0000
- Revision:
- 1:b22cf6a5957d
- Parent:
- 0:8d785e80d3ba
moved defines to arduino.h
Who changed what in which revision?
User | Revision | Line number | New 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 | |
TheChrisyd | 0:8d785e80d3ba | 119 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 120 | Maths |
TheChrisyd | 0:8d785e80d3ba | 121 | |
TheChrisyd | 0:8d785e80d3ba | 122 | min() - done by Macro |
TheChrisyd | 0:8d785e80d3ba | 123 | max() - done by Macro |
TheChrisyd | 0:8d785e80d3ba | 124 | abs() - done by Macro |
TheChrisyd | 0:8d785e80d3ba | 125 | constrain() - TODO |
TheChrisyd | 0:8d785e80d3ba | 126 | map() - TODO |
TheChrisyd | 0:8d785e80d3ba | 127 | pow() - implemented by including math.h |
TheChrisyd | 0:8d785e80d3ba | 128 | sqrt() - implemented by including math.h |
TheChrisyd | 0:8d785e80d3ba | 129 | |
TheChrisyd | 0:8d785e80d3ba | 130 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 131 | |
TheChrisyd | 0:8d785e80d3ba | 132 | |
TheChrisyd | 0:8d785e80d3ba | 133 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 134 | Trigonometry |
TheChrisyd | 0:8d785e80d3ba | 135 | |
TheChrisyd | 0:8d785e80d3ba | 136 | sin() - implemented by including math.h |
TheChrisyd | 0:8d785e80d3ba | 137 | cos() - implemented by including math.h |
TheChrisyd | 0:8d785e80d3ba | 138 | tan() - implemented by including math.h |
TheChrisyd | 0:8d785e80d3ba | 139 | |
TheChrisyd | 0:8d785e80d3ba | 140 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 141 | |
TheChrisyd | 0:8d785e80d3ba | 142 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 143 | Random Numbers |
TheChrisyd | 0:8d785e80d3ba | 144 | |
TheChrisyd | 0:8d785e80d3ba | 145 | randomSeed() - done by Macro |
TheChrisyd | 0:8d785e80d3ba | 146 | random() - function below |
TheChrisyd | 0:8d785e80d3ba | 147 | |
TheChrisyd | 0:8d785e80d3ba | 148 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 149 | |
TheChrisyd | 0:8d785e80d3ba | 150 | /** generates a random number from 0 to defined number |
TheChrisyd | 0:8d785e80d3ba | 151 | * |
TheChrisyd | 0:8d785e80d3ba | 152 | * @param number maximum value for random number |
TheChrisyd | 0:8d785e80d3ba | 153 | */ |
TheChrisyd | 0:8d785e80d3ba | 154 | int random(int number) { |
TheChrisyd | 0:8d785e80d3ba | 155 | return (rand()%number); |
TheChrisyd | 0:8d785e80d3ba | 156 | } |
TheChrisyd | 0:8d785e80d3ba | 157 | /** generates a random number between two numbers |
TheChrisyd | 0:8d785e80d3ba | 158 | * |
TheChrisyd | 0:8d785e80d3ba | 159 | * @param numberone minimum value for random number |
TheChrisyd | 0:8d785e80d3ba | 160 | * @param numbertwo maximum value for random number |
TheChrisyd | 0:8d785e80d3ba | 161 | */ |
TheChrisyd | 0:8d785e80d3ba | 162 | int random(int numberone, int numbertwo) { |
TheChrisyd | 0:8d785e80d3ba | 163 | int random = 0; |
TheChrisyd | 0:8d785e80d3ba | 164 | if ((numberone < 0) && (numbertwo < 0)) { |
TheChrisyd | 0:8d785e80d3ba | 165 | numberone = numberone * -1; |
TheChrisyd | 0:8d785e80d3ba | 166 | numbertwo = numbertwo * -1; |
TheChrisyd | 0:8d785e80d3ba | 167 | random = -1 * (rand()%(numberone + numbertwo)); |
TheChrisyd | 0:8d785e80d3ba | 168 | } |
TheChrisyd | 0:8d785e80d3ba | 169 | if ((numbertwo < 0) && (numberone >= 0)) { |
TheChrisyd | 0:8d785e80d3ba | 170 | numbertwo = numbertwo * -1; |
TheChrisyd | 0:8d785e80d3ba | 171 | random = (rand()%(numberone + numbertwo)) - numbertwo; |
TheChrisyd | 0:8d785e80d3ba | 172 | } |
TheChrisyd | 0:8d785e80d3ba | 173 | if ((numberone < 0) && (numbertwo >= 0)) { |
TheChrisyd | 0:8d785e80d3ba | 174 | numberone = numberone * -1; |
TheChrisyd | 0:8d785e80d3ba | 175 | random = (rand()%(numberone + numbertwo)) - numberone; |
TheChrisyd | 0:8d785e80d3ba | 176 | } else { |
TheChrisyd | 0:8d785e80d3ba | 177 | random = (rand()%(numberone + numbertwo)) - min(numberone, numbertwo); |
TheChrisyd | 0:8d785e80d3ba | 178 | } |
TheChrisyd | 0:8d785e80d3ba | 179 | return random; |
TheChrisyd | 0:8d785e80d3ba | 180 | } |
TheChrisyd | 0:8d785e80d3ba | 181 | |
TheChrisyd | 0:8d785e80d3ba | 182 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 183 | Bits and Bytes |
TheChrisyd | 0:8d785e80d3ba | 184 | |
TheChrisyd | 0:8d785e80d3ba | 185 | lowByte() - function below |
TheChrisyd | 0:8d785e80d3ba | 186 | highByte() - function below |
TheChrisyd | 0:8d785e80d3ba | 187 | bitRead() - TODO |
TheChrisyd | 0:8d785e80d3ba | 188 | bitWrite() - TODO |
TheChrisyd | 0:8d785e80d3ba | 189 | bitSet() - TODO |
TheChrisyd | 0:8d785e80d3ba | 190 | bitClear() - TODO |
TheChrisyd | 0:8d785e80d3ba | 191 | bit() - TODO |
TheChrisyd | 0:8d785e80d3ba | 192 | |
TheChrisyd | 0:8d785e80d3ba | 193 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 194 | |
TheChrisyd | 0:8d785e80d3ba | 195 | /** returns the lower nibble of first byte |
TheChrisyd | 0:8d785e80d3ba | 196 | * |
TheChrisyd | 0:8d785e80d3ba | 197 | * @param high byte of which the high nibble is required |
TheChrisyd | 0:8d785e80d3ba | 198 | */ |
TheChrisyd | 0:8d785e80d3ba | 199 | byte lowByte(short int low) { |
TheChrisyd | 0:8d785e80d3ba | 200 | byte bytelow = 0; |
TheChrisyd | 0:8d785e80d3ba | 201 | bytelow = (low & 0xFF); |
TheChrisyd | 0:8d785e80d3ba | 202 | return bytelow; |
TheChrisyd | 0:8d785e80d3ba | 203 | } |
TheChrisyd | 0:8d785e80d3ba | 204 | /** returns the higher nibble of first byte |
TheChrisyd | 0:8d785e80d3ba | 205 | * |
TheChrisyd | 0:8d785e80d3ba | 206 | * @param high byte of which the high nibble is required |
TheChrisyd | 0:8d785e80d3ba | 207 | */ |
TheChrisyd | 0:8d785e80d3ba | 208 | byte highByte(short int high) { |
TheChrisyd | 0:8d785e80d3ba | 209 | byte bytehigh = 0; |
TheChrisyd | 0:8d785e80d3ba | 210 | bytehigh = ((high >> 8) & 0xFF); |
TheChrisyd | 0:8d785e80d3ba | 211 | return bytehigh; |
TheChrisyd | 0:8d785e80d3ba | 212 | } |
TheChrisyd | 0:8d785e80d3ba | 213 | |
TheChrisyd | 0:8d785e80d3ba | 214 | |
TheChrisyd | 0:8d785e80d3ba | 215 | |
TheChrisyd | 0:8d785e80d3ba | 216 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 217 | External Interrupts |
TheChrisyd | 0:8d785e80d3ba | 218 | |
TheChrisyd | 0:8d785e80d3ba | 219 | attachInterrupt() - TODO |
TheChrisyd | 0:8d785e80d3ba | 220 | detachInterrupt() - TODO |
TheChrisyd | 0:8d785e80d3ba | 221 | |
TheChrisyd | 0:8d785e80d3ba | 222 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 223 | |
TheChrisyd | 0:8d785e80d3ba | 224 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 225 | Interrupts |
TheChrisyd | 0:8d785e80d3ba | 226 | |
TheChrisyd | 0:8d785e80d3ba | 227 | interrupts() - TODO |
TheChrisyd | 0:8d785e80d3ba | 228 | noInterrupts() - TODO |
TheChrisyd | 0:8d785e80d3ba | 229 | |
TheChrisyd | 0:8d785e80d3ba | 230 | *****************************************************/ |
TheChrisyd | 0:8d785e80d3ba | 231 | |
TheChrisyd | 0:8d785e80d3ba | 232 | /***************************************************** |
TheChrisyd | 0:8d785e80d3ba | 233 | Communication |
TheChrisyd | 0:8d785e80d3ba | 234 | |
TheChrisyd | 0:8d785e80d3ba | 235 | Serial - TODO |
TheChrisyd | 0:8d785e80d3ba | 236 | Stream - TODO |
TheChrisyd | 0:8d785e80d3ba | 237 | |
TheChrisyd | 0:8d785e80d3ba | 238 | *****************************************************/ |