Tryout the TOF sensor Only

Dependencies:   VL53L0X_Condensed

Committer:
sepp_nepp
Date:
Sun Mar 24 22:25:09 2019 +0000
Revision:
15:e0fbd2ddd2a9
Parent:
11:3695280eeffd
Child:
16:6a5a62b00ed1
Now condensed and compiles

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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
sepp_nepp 15:e0fbd2ddd2a9 42 int Ionisch [] = {0, 2, 4, 5, 7, 9, 11};
sepp_nepp 15:e0fbd2ddd2a9 43 int Dorisch [] = {0, 2, 3, 5, 7, 9, 10};
sepp_nepp 15:e0fbd2ddd2a9 44 int Phrygisch[] = {0, 1, 3, 5, 7, 8, 10};
sepp_nepp 15:e0fbd2ddd2a9 45 int Lydisch [] = {0, 2, 4, 6, 7, 9, 11};
sepp_nepp 15:e0fbd2ddd2a9 46 int Mixolydi [] = {0, 2, 4, 5, 7, 9, 10};
sepp_nepp 15:e0fbd2ddd2a9 47 int Eolisch [] = {0, 2, 3, 5, 7, 8, 10};
sepp_nepp 15:e0fbd2ddd2a9 48 int Lokrisch [] = {0, 1, 3, 5, 6, 8, 10};
sepp_nepp 15:e0fbd2ddd2a9 49 int PhrygDomi[] = {0, 1, 4, 5, 7, 8, 10};
sepp_nepp 15:e0fbd2ddd2a9 50 int MiShebera[] = {0, 2, 3, 6, 7, 9, 10};
sepp_nepp 15:e0fbd2ddd2a9 51 int TsigaDur [] = {0, 2, 3, 6, 7, 8, 11}; // https://de.wikipedia.org/wiki/Zigeunertonleiter
sepp_nepp 15:e0fbd2ddd2a9 52 int TsigaMoll[] = {0, 2, 3, 5, 7, 8, 11}; // https://de.wikipedia.org/wiki/Zigeunertonleiter
sepp_nepp 15:e0fbd2ddd2a9 53
sepp_nepp 15:e0fbd2ddd2a9 54 // Maquam: arabische leiter mit 3/4 Tonschritten, kann nur über CV Ausang funzen
sepp_nepp 15:e0fbd2ddd2a9 55 // siehe: https://de.wikipedia.org/wiki/Maqam_(Musik)
sepp_nepp 15:e0fbd2ddd2a9 56
nikapov 8:39ecd15538f9 57 /* Interface definition */
mapellil 4:a876fbe13688 58 static DevI2C devI2c(PB_11,PB_10);
sepp_nepp 11:3695280eeffd 59 Serial midi_uart(PA_0, PA_1); // PA_0 = Tx, PA_1 = Rx
nikapov 0:6a97487a0ab3 60
nikapov 8:39ecd15538f9 61 /* Range sensor - B-L475E-IOT01A2 only */
nikapov 8:39ecd15538f9 62 static DigitalOut shutdown_pin(PC_6);
sepp_nepp 11:3695280eeffd 63 static VL53L0X tof1(&devI2c, &shutdown_pin, PC_7);
nikapov 8:39ecd15538f9 64
nikapov 0:6a97487a0ab3 65 /* Simple main function */
nikapov 0:6a97487a0ab3 66 int main() {
sepp_nepp 11:3695280eeffd 67 uint32_t distance;
sepp_nepp 11:3695280eeffd 68 int status;
sepp_nepp 15:e0fbd2ddd2a9 69 char NoteNr, Octave, ScaleNote;
sepp_nepp 11:3695280eeffd 70 char OldKey, NewKey;
sepp_nepp 11:3695280eeffd 71 VL53L0X_RangingMeasurementData_t p_ranging_measurement_data;
sepp_nepp 11:3695280eeffd 72
nikapov 0:6a97487a0ab3 73 /* Init all sensors with default params */
sepp_nepp 11:3695280eeffd 74 tof1.init_sensor(VL53L0X_DEFAULT_ADDRESS);
sepp_nepp 11:3695280eeffd 75 status = tof1.start_measurement(range_continuous_polling, NULL);
sepp_nepp 11:3695280eeffd 76 if (status)
sepp_nepp 11:3695280eeffd 77 { printf("\n\r--- Error Initializing the TOF sensor ---\n\r");
sepp_nepp 11:3695280eeffd 78 //while(1) { }; // consider to hang up the device here
sepp_nepp 11:3695280eeffd 79 }
sepp_nepp 11:3695280eeffd 80 printf("\n\r--- Read Init of TOF ---\n\r"); ;
sepp_nepp 11:3695280eeffd 81
sepp_nepp 11:3695280eeffd 82 OldKey = 0x00; // Remembers the old key, start with value thats normallz unused
sepp_nepp 11:3695280eeffd 83 midi_uart.baud( 31250 );
sepp_nepp 11:3695280eeffd 84 midi_uart.format( 8, SerialBase::None, 2);
sepp_nepp 15:e0fbd2ddd2a9 85 wait(0.1); // make sure all is settled
sepp_nepp 15:e0fbd2ddd2a9 86 midi_uart.putc(0xB0); // Midi Channel 0, Channel Mode Message
sepp_nepp 15:e0fbd2ddd2a9 87 midi_uart.putc(123); // Byte 2: All Notes Off
sepp_nepp 15:e0fbd2ddd2a9 88 midi_uart.putc(0x00); // Byte 3: obligatory zero
sepp_nepp 15:e0fbd2ddd2a9 89
nikapov 0:6a97487a0ab3 90
sepp_nepp 11:3695280eeffd 91 while(1)
sepp_nepp 11:3695280eeffd 92 { status = tof1.get_measurement(range_continuous_polling, &p_ranging_measurement_data);
sepp_nepp 11:3695280eeffd 93
sepp_nepp 11:3695280eeffd 94 if ( (status == VL53L0X_ERROR_NONE) && (p_ranging_measurement_data.RangeStatus == 0) )
sepp_nepp 11:3695280eeffd 95 { // we have a valid range.
sepp_nepp 11:3695280eeffd 96 distance = p_ranging_measurement_data.RangeMilliMeter;
sepp_nepp 11:3695280eeffd 97 // 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 98 distance = (int) (distance / 10) + 20; // scaling factor 10 between distance and key note
sepp_nepp 15:e0fbd2ddd2a9 99 if (distance >= 0x7F) NoteNr = 0x00; // cap the New Key Value on the top, beyond 127 we discard the note as 'no Note'
sepp_nepp 15:e0fbd2ddd2a9 100 else NoteNr = distance; // pass through everything below; remember: distance is uint, so no negatives can occur
sepp_nepp 15:e0fbd2ddd2a9 101 Octave = (int) (NoteNr / 7);
sepp_nepp 15:e0fbd2ddd2a9 102 ScaleNote = NoteNr - (Octave * 7);
sepp_nepp 15:e0fbd2ddd2a9 103 NewKey = Octave * 12 + MiShebera[ScaleNote];
sepp_nepp 11:3695280eeffd 104 }
sepp_nepp 11:3695280eeffd 105 else
sepp_nepp 11:3695280eeffd 106 { // printf(" --\r");
sepp_nepp 11:3695280eeffd 107 NewKey = 0x00;
sepp_nepp 11:3695280eeffd 108 }
sepp_nepp 11:3695280eeffd 109 if ( OldKey!=NewKey )
sepp_nepp 11:3695280eeffd 110 {
sepp_nepp 11:3695280eeffd 111 if (OldKey != 0x00)
sepp_nepp 11:3695280eeffd 112 { // turn off the old note first, if there was one
sepp_nepp 11:3695280eeffd 113 midi_uart.putc(0x80); // Midi Channel 0, Note OFF,
sepp_nepp 11:3695280eeffd 114 midi_uart.putc(OldKey); // Byte 2: Old Key Number [0 - 127]
sepp_nepp 11:3695280eeffd 115 midi_uart.putc(0x3F); // Byte 3: Velocity [0 - 127]; here fixed=63
sepp_nepp 11:3695280eeffd 116 }
sepp_nepp 11:3695280eeffd 117 if (NewKey != 0x00)
sepp_nepp 11:3695280eeffd 118 { // turn on the new note now, if there is a valid one
sepp_nepp 15:e0fbd2ddd2a9 119 printf("%6ld\r\n", NewKey);
sepp_nepp 11:3695280eeffd 120 midi_uart.putc(0x90); // Midi Channel 0, Note ON,
sepp_nepp 11:3695280eeffd 121 midi_uart.putc(NewKey);// Byte 2: New Key Number [0 - 127]
sepp_nepp 11:3695280eeffd 122 midi_uart.putc(0x3F); // Byte 3: Velocity [0 - 127]; here fixed=63
sepp_nepp 15:e0fbd2ddd2a9 123 wait(0.1); // leave a little pause before a new note gets played ...
sepp_nepp 11:3695280eeffd 124 }
sepp_nepp 11:3695280eeffd 125 OldKey = NewKey;
sepp_nepp 11:3695280eeffd 126 // wait(0.1);
sepp_nepp 11:3695280eeffd 127 } // if ( OldKey!=NewKey )
sepp_nepp 11:3695280eeffd 128 } // while(1)
sepp_nepp 11:3695280eeffd 129 } // int main()