Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: VL53L0X_Condensed
main.cpp@11:3695280eeffd, 2019-03-23 (annotated)
- Committer:
- sepp_nepp
- Date:
- Sat Mar 23 21:30:31 2019 +0000
- Revision:
- 11:3695280eeffd
- Parent:
- 8:39ecd15538f9
- Child:
- 15:e0fbd2ddd2a9
My Tryout
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nikapov | 0:6a97487a0ab3 | 1 | /** |
| nikapov | 0:6a97487a0ab3 | 2 | ****************************************************************************** |
| nikapov | 0:6a97487a0ab3 | 3 | * @file main.cpp |
| sepp_nepp | 11:3695280eeffd | 4 | * @author Tarek Lule, based on code by CLab |
| nikapov | 0:6a97487a0ab3 | 5 | * @version V1.0.0 |
| sepp_nepp | 11:3695280eeffd | 6 | * @date 13-March-2019 |
| sepp_nepp | 11:3695280eeffd | 7 | * @brief Simple Example application for using TOF Sensors |
| nikapov | 0:6a97487a0ab3 | 8 | ****************************************************************************** |
| nikapov | 0:6a97487a0ab3 | 9 | * @attention |
| nikapov | 0:6a97487a0ab3 | 10 | * |
| nikapov | 0:6a97487a0ab3 | 11 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
| nikapov | 0:6a97487a0ab3 | 12 | * |
| nikapov | 0:6a97487a0ab3 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
| nikapov | 0:6a97487a0ab3 | 14 | * are permitted provided that the following conditions are met: |
| nikapov | 0:6a97487a0ab3 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
| nikapov | 0:6a97487a0ab3 | 16 | * this list of conditions and the following disclaimer. |
| nikapov | 0:6a97487a0ab3 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| nikapov | 0:6a97487a0ab3 | 18 | * this list of conditions and the following disclaimer in the documentation |
| nikapov | 0:6a97487a0ab3 | 19 | * and/or other materials provided with the distribution. |
| nikapov | 0:6a97487a0ab3 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| nikapov | 0:6a97487a0ab3 | 21 | * may be used to endorse or promote products derived from this software |
| nikapov | 0:6a97487a0ab3 | 22 | * without specific prior written permission. |
| nikapov | 0:6a97487a0ab3 | 23 | * |
| nikapov | 0:6a97487a0ab3 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| nikapov | 0:6a97487a0ab3 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| nikapov | 0:6a97487a0ab3 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| nikapov | 0:6a97487a0ab3 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| nikapov | 0:6a97487a0ab3 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| nikapov | 0:6a97487a0ab3 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| nikapov | 0:6a97487a0ab3 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| nikapov | 0:6a97487a0ab3 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| nikapov | 0:6a97487a0ab3 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| nikapov | 0:6a97487a0ab3 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| nikapov | 0:6a97487a0ab3 | 34 | * |
| nikapov | 0:6a97487a0ab3 | 35 | ****************************************************************************** |
| nikapov | 0:6a97487a0ab3 | 36 | */ |
| nikapov | 0:6a97487a0ab3 | 37 | |
| nikapov | 0:6a97487a0ab3 | 38 | /* Includes */ |
| nikapov | 0:6a97487a0ab3 | 39 | #include "mbed.h" |
| nikapov | 8:39ecd15538f9 | 40 | #include "VL53L0X.h" |
| nikapov | 8:39ecd15538f9 | 41 | |
| nikapov | 8:39ecd15538f9 | 42 | /* Interface definition */ |
| mapellil | 4:a876fbe13688 | 43 | static DevI2C devI2c(PB_11,PB_10); |
| sepp_nepp | 11:3695280eeffd | 44 | Serial midi_uart(PA_0, PA_1); // PA_0 = Tx, PA_1 = Rx |
| nikapov | 0:6a97487a0ab3 | 45 | |
| nikapov | 8:39ecd15538f9 | 46 | /* Range sensor - B-L475E-IOT01A2 only */ |
| nikapov | 8:39ecd15538f9 | 47 | static DigitalOut shutdown_pin(PC_6); |
| sepp_nepp | 11:3695280eeffd | 48 | static VL53L0X tof1(&devI2c, &shutdown_pin, PC_7); |
| nikapov | 8:39ecd15538f9 | 49 | |
| nikapov | 0:6a97487a0ab3 | 50 | /* Simple main function */ |
| nikapov | 0:6a97487a0ab3 | 51 | int main() { |
| sepp_nepp | 11:3695280eeffd | 52 | uint32_t distance; |
| sepp_nepp | 11:3695280eeffd | 53 | int status; |
| sepp_nepp | 11:3695280eeffd | 54 | char OldKey, NewKey; |
| sepp_nepp | 11:3695280eeffd | 55 | VL53L0X_RangingMeasurementData_t p_ranging_measurement_data; |
| sepp_nepp | 11:3695280eeffd | 56 | |
| nikapov | 0:6a97487a0ab3 | 57 | /* Init all sensors with default params */ |
| sepp_nepp | 11:3695280eeffd | 58 | tof1.init_sensor(VL53L0X_DEFAULT_ADDRESS); |
| sepp_nepp | 11:3695280eeffd | 59 | status = tof1.start_measurement(range_continuous_polling, NULL); |
| sepp_nepp | 11:3695280eeffd | 60 | if (status) |
| sepp_nepp | 11:3695280eeffd | 61 | { printf("\n\r--- Error Initializing the TOF sensor ---\n\r"); |
| sepp_nepp | 11:3695280eeffd | 62 | //while(1) { }; // consider to hang up the device here |
| sepp_nepp | 11:3695280eeffd | 63 | } |
| sepp_nepp | 11:3695280eeffd | 64 | printf("\n\r--- Read Init of TOF ---\n\r"); ; |
| sepp_nepp | 11:3695280eeffd | 65 | |
| sepp_nepp | 11:3695280eeffd | 66 | OldKey = 0x00; // Remembers the old key, start with value thats normallz unused |
| sepp_nepp | 11:3695280eeffd | 67 | midi_uart.baud( 31250 ); |
| sepp_nepp | 11:3695280eeffd | 68 | midi_uart.format( 8, SerialBase::None, 2); |
| nikapov | 0:6a97487a0ab3 | 69 | |
| sepp_nepp | 11:3695280eeffd | 70 | while(1) |
| sepp_nepp | 11:3695280eeffd | 71 | { status = tof1.get_measurement(range_continuous_polling, &p_ranging_measurement_data); |
| sepp_nepp | 11:3695280eeffd | 72 | |
| sepp_nepp | 11:3695280eeffd | 73 | if ( (status == VL53L0X_ERROR_NONE) && (p_ranging_measurement_data.RangeStatus == 0) ) |
| sepp_nepp | 11:3695280eeffd | 74 | { // we have a valid range. |
| sepp_nepp | 11:3695280eeffd | 75 | distance = p_ranging_measurement_data.RangeMilliMeter; |
| sepp_nepp | 11:3695280eeffd | 76 | // printf("%6ld\r", distance); // Distance is ~1700 when looking at the ceiling, so approximately 1 per mm. A really usable distance range is only up to 1270. |
| sepp_nepp | 11:3695280eeffd | 77 | distance = (int) (distance / 10) + 20; // scaling factor 10 between distance and key note |
| sepp_nepp | 11:3695280eeffd | 78 | if (distance >= 0x6F) NewKey = 0x00; // cap the New Key Value on the top, beyond 127 we discard the note as 'no Note' |
| sepp_nepp | 11:3695280eeffd | 79 | else NewKey = distance; // pass through everything below; remember: distance is uint, so no negatives can occur |
| sepp_nepp | 11:3695280eeffd | 80 | } |
| sepp_nepp | 11:3695280eeffd | 81 | else |
| sepp_nepp | 11:3695280eeffd | 82 | { // printf(" --\r"); |
| sepp_nepp | 11:3695280eeffd | 83 | NewKey = 0x00; |
| sepp_nepp | 11:3695280eeffd | 84 | } |
| sepp_nepp | 11:3695280eeffd | 85 | printf("%6ld\r", NewKey); |
| sepp_nepp | 11:3695280eeffd | 86 | if ( OldKey!=NewKey ) |
| sepp_nepp | 11:3695280eeffd | 87 | { |
| sepp_nepp | 11:3695280eeffd | 88 | if (OldKey != 0x00) |
| sepp_nepp | 11:3695280eeffd | 89 | { // turn off the old note first, if there was one |
| sepp_nepp | 11:3695280eeffd | 90 | midi_uart.putc(0x80); // Midi Channel 0, Note OFF, |
| sepp_nepp | 11:3695280eeffd | 91 | midi_uart.putc(OldKey); // Byte 2: Old Key Number [0 - 127] |
| sepp_nepp | 11:3695280eeffd | 92 | midi_uart.putc(0x3F); // Byte 3: Velocity [0 - 127]; here fixed=63 |
| sepp_nepp | 11:3695280eeffd | 93 | } |
| sepp_nepp | 11:3695280eeffd | 94 | // wait(0.1); |
| sepp_nepp | 11:3695280eeffd | 95 | if (NewKey != 0x00) |
| sepp_nepp | 11:3695280eeffd | 96 | { // turn on the new note now, if there is a valid one |
| sepp_nepp | 11:3695280eeffd | 97 | midi_uart.putc(0x90); // Midi Channel 0, Note ON, |
| sepp_nepp | 11:3695280eeffd | 98 | midi_uart.putc(NewKey);// Byte 2: New Key Number [0 - 127] |
| sepp_nepp | 11:3695280eeffd | 99 | midi_uart.putc(0x3F); // Byte 3: Velocity [0 - 127]; here fixed=63 |
| sepp_nepp | 11:3695280eeffd | 100 | } |
| sepp_nepp | 11:3695280eeffd | 101 | OldKey = NewKey; |
| sepp_nepp | 11:3695280eeffd | 102 | // wait(0.1); |
| sepp_nepp | 11:3695280eeffd | 103 | } // if ( OldKey!=NewKey ) |
| sepp_nepp | 11:3695280eeffd | 104 | } // while(1) |
| sepp_nepp | 11:3695280eeffd | 105 | } // int main() |