Tarek Lule / ToF-Only-Tryout

Dependencies:   VL53L0X_Condensed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Tarek Lule, based on code by CLab
00005  * @version V1.0.0
00006  * @date    13-March-2019
00007  * @brief   Simple Example application for using TOF  Sensors
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */ 
00037 
00038 /* Includes */
00039 #include "mbed.h"
00040 #include "VL53L0X.h"
00041 
00042 int Ionisch  [] = {0, 2, 4, 5, 7, 9, 11};
00043 int Dorisch  [] = {0, 2, 3, 5, 7, 9, 10};
00044 int Phrygisch[] = {0, 1, 3, 5, 7, 8, 10};
00045 int Lydisch  [] = {0, 2, 4, 6, 7, 9, 11};
00046 int Mixolydi [] = {0, 2, 4, 5, 7, 9, 10};
00047 int Eolisch  [] = {0, 2, 3, 5, 7, 8, 10};
00048 int Lokrisch [] = {0, 1, 3, 5, 6, 8, 10};
00049 int PhrygDomi[] = {0, 1, 4, 5, 7, 8, 10};
00050 int MiShebera[] = {0, 2, 3, 6, 7, 9, 10};
00051 int TsigaDur [] = {0, 2, 3, 6, 7, 8, 11}; // https://de.wikipedia.org/wiki/Zigeunertonleiter
00052 int TsigaMoll[] = {0, 2, 3, 5, 7, 8, 11}; // https://de.wikipedia.org/wiki/Zigeunertonleiter
00053 
00054 // Maquam: arabische leiter mit 3/4 Tonschritten, kann nur über CV Ausang funzen
00055 // siehe: https://de.wikipedia.org/wiki/Maqam_(Musik)
00056 
00057 /* Interface definition */
00058 static DevI2C devI2c(PB_11,PB_10);
00059 Serial midi_uart(PA_0, PA_1);  // PA_0 = Tx, PA_1 = Rx
00060 
00061 /* Range sensor - B-L475E-IOT01A2 only */
00062 static DigitalOut shutdown_pin(PC_6);
00063 static VL53L0X tof1(&devI2c, &shutdown_pin, PC_7);
00064 
00065 /* Simple main function */
00066 int main() {
00067   uint32_t distance, NewDist, OldDist;
00068   int status;
00069   char NoteNr, Octave, ScaleNote;
00070   char OldKey, NewKey;
00071   VL53L0X_RangingMeasurementData_t p_ranging_measurement_data;
00072     
00073   /* Init all sensors with default params */
00074   tof1.init_sensor(VL53L0X_DEFAULT_ADDRESS);
00075   status = tof1.start_measurement(range_continuous_polling, NULL);
00076   status = status | tof1.VL53L0X_set_measurement_timing_budget_us(100000);
00077   if (status) 
00078      {  printf("\n\r--- Error Initializing the TOF sensor ---\n\r"); 
00079         //while(1) { }; // consider to hang up the device here
00080      }
00081   printf("\n\r--- Read Init of TOF ---\n\r"); ;
00082          
00083   OldKey = 0x00; // Remembers the old key, start with value thats normally unused 
00084   midi_uart.baud( 31250 );
00085   midi_uart.format( 8, SerialBase::None, 2);
00086   wait(0.1); // make sure all is settled
00087   midi_uart.putc(0xB0);  // Midi Channel 0, Channel Mode Message
00088   midi_uart.putc(123);   // Byte 2: All Notes Off
00089   midi_uart.putc(0x00);  // Byte 3: obligatory zero 
00090   
00091  
00092   while(1)
00093     { status =  tof1.get_measurement(range_continuous_polling, &p_ranging_measurement_data);
00094         
00095     if ( (status == VL53L0X_ERROR_NONE) && (p_ranging_measurement_data.RangeStatus == 0) )
00096         { // we have a valid range.
00097         NewDist = p_ranging_measurement_data.RangeMilliMeter;
00098         // 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. 
00099         distance = (int) ( NewDist / 30 + OldDist / 20 ) + 20; // scaling factor 10 between distance and key note
00100         if (distance >= 0x7F) NoteNr = 0x00; // cap the New Key Value on the top, beyond 127 we discard the note as 'no Note'
00101             else NoteNr = distance; // pass through everything below; remember: distance is uint, so no negatives can occur
00102         Octave = (int) (NoteNr / 7);
00103         ScaleNote = NoteNr - (Octave * 7);
00104         NewKey = Octave * 12 + TsigaMoll[ScaleNote];
00105         OldDist = NewDist;
00106         } 
00107       else 
00108         { // printf("  --\r");
00109         NewKey = 0x00;
00110         }
00111     if ( OldKey!=NewKey )
00112       {
00113         if (OldKey != 0x00) 
00114             { // turn off the old note first, if there was one
00115             midi_uart.putc(0x80);   // Midi Channel 0, Note OFF, 
00116             midi_uart.putc(OldKey); // Byte 2: Old Key Number [0 - 127]   
00117             midi_uart.putc(0x3F);   // Byte 3: Velocity [0 - 127]; here fixed=63 
00118             }
00119         if (NewKey != 0x00)
00120             { // turn on the new note now, if there is a valid one
00121             printf("%6ld\r\n", NewKey);
00122             midi_uart.putc(0x90);  // Midi Channel 0, Note ON, 
00123             midi_uart.putc(NewKey);// Byte 2: New Key Number [0 - 127]   
00124             midi_uart.putc(0x3F);  // Byte 3: Velocity [0 - 127]; here fixed=63 
00125             wait(0.1); // leave a little pause before a new note gets played ...
00126             }
00127         OldKey = NewKey;
00128         // wait(0.1);
00129       } // if ( OldKey!=NewKey )
00130   } // while(1)
00131 } // int main()