Tarek Lule / ToF-Only-Tryout

Dependencies:   VL53L0X_Condensed

main.cpp

Committer:
sepp_nepp
Date:
2019-03-23
Revision:
11:3695280eeffd
Parent:
8:39ecd15538f9
Child:
15:e0fbd2ddd2a9

File content as of revision 11:3695280eeffd:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  Tarek Lule, based on code by CLab
 * @version V1.0.0
 * @date    13-March-2019
 * @brief   Simple Example application for using TOF  Sensors
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of STMicroelectronics nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
*/ 

/* Includes */
#include "mbed.h"
#include "VL53L0X.h"

/* Interface definition */
static DevI2C devI2c(PB_11,PB_10);
Serial midi_uart(PA_0, PA_1);  // PA_0 = Tx, PA_1 = Rx

/* Range sensor - B-L475E-IOT01A2 only */
static DigitalOut shutdown_pin(PC_6);
static VL53L0X tof1(&devI2c, &shutdown_pin, PC_7);

/* Simple main function */
int main() {
  uint32_t distance;
  int status;
  char OldKey, NewKey;
  VL53L0X_RangingMeasurementData_t p_ranging_measurement_data;
    
  /* Init all sensors with default params */
  tof1.init_sensor(VL53L0X_DEFAULT_ADDRESS);
  status = tof1.start_measurement(range_continuous_polling, NULL);
  if (status) 
     {  printf("\n\r--- Error Initializing the TOF sensor ---\n\r"); 
        //while(1) { }; // consider to hang up the device here
     }
  printf("\n\r--- Read Init of TOF ---\n\r"); ;
         
  OldKey = 0x00; // Remembers the old key, start with value thats normallz unused 
  midi_uart.baud( 31250 );
  midi_uart.format( 8, SerialBase::None, 2);
 
  while(1)
    { status =  tof1.get_measurement(range_continuous_polling, &p_ranging_measurement_data);
        
    if ( (status == VL53L0X_ERROR_NONE) && (p_ranging_measurement_data.RangeStatus == 0) )
        { // we have a valid range.
        distance = p_ranging_measurement_data.RangeMilliMeter;
        // 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. 
        distance = (int) (distance / 10) + 20; // scaling factor 10 between distance and key note
        if (distance >= 0x6F) NewKey = 0x00; // cap the New Key Value on the top, beyond 127 we discard the note as 'no Note'
            else NewKey = distance; // pass through everything below; remember: distance is uint, so no negatives can occur
        } 
      else 
        { // printf("  --\r");
        NewKey = 0x00;
        }
    printf("%6ld\r", NewKey);
    if ( OldKey!=NewKey )
      {
        if (OldKey != 0x00) 
            { // turn off the old note first, if there was one
            midi_uart.putc(0x80);   // Midi Channel 0, Note OFF, 
            midi_uart.putc(OldKey); // Byte 2: Old Key Number [0 - 127]   
            midi_uart.putc(0x3F);   // Byte 3: Velocity [0 - 127]; here fixed=63 
            }
        // wait(0.1);
        if (NewKey != 0x00)
            { // turn on the new note now, if there is a valid one
            midi_uart.putc(0x90);  // Midi Channel 0, Note ON, 
            midi_uart.putc(NewKey);// Byte 2: New Key Number [0 - 127]   
            midi_uart.putc(0x3F);  // Byte 3: Velocity [0 - 127]; here fixed=63 
            }
        OldKey = NewKey;
        // wait(0.1);
      } // if ( OldKey!=NewKey )
  } // while(1)
} // int main()