test
Dependencies: mbed AsyncBuzzerLib X_NUCLEO_53L1A1_mbed WS2812
Revision 0:89b87662e64b, committed 2020-09-29
- Comitter:
- elab
- Date:
- Tue Sep 29 09:08:55 2020 +0000
- Commit message:
- first version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AsyncBuzzerLib.lib Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/CreaLab/code/AsyncBuzzerLib/#91de3af62626
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/LED_WS2812.cpp Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,209 @@ +#include "LED_WS2812.h" + + + +LED_WS2812::LED_WS2812(PinName _PinOut, int _nbLeds) { + nbLeds = _nbLeds; + double period_ns; + Timer tuneTimings; + int sum = 0; + int nopRun; + + for(int kavg = 0; kavg<20;kavg++) { + tuneTimings.reset(); + tuneTimings.start(); + for(int nopCount=0; nopCount < 10000; nopCount ++) { + __nop(); + } + tuneTimings.stop(); + nopRun = tuneTimings.read_us(); + sum=nopRun+sum; + } + period_ns = sum/200; /* *1000 for nanoseconds /20 average /10000 count */ + + int zero_high = ZERO_HIGH/period_ns; + int zero_low = ZERO_LOW/period_ns; + int one_high = ONE_HIGH/period_ns; + int one_low = ONE_LOW/period_ns; + + ws = new WS2812(_PinOut, nbLeds, zero_high, zero_low, one_high, one_low); + ws->useII(WS2812::PER_PIXEL); + + pxArray = new PixelArray(nbLeds); + pxInsert = new PixelArray(nbLeds); + ResetColor(); + rotationState = false; + rotationPosition = nbLeds-1; + blinkState = false; + blinkONOFF = false; + intensity = 0xff; +}; + +LED_WS2812::~LED_WS2812() { + delete(ws); + delete(pxArray); +} + + + +void LED_WS2812::SetColor(LED_COLORS _color, int position) { + SetColor((unsigned int) _color, position); +}; + +void LED_WS2812::SetColor(unsigned int _color, int position) { + if(position < nbLeds && position >=0 ) { + pxArray->Set(position, _color); + pxArray->SetI(position,intensity); + + } + __writeBuf(0); + nbInsert = 0; + if(rotationState) StopRotation(); + rotationPosition = nbLeds; +}; + +void LED_WS2812::SetColor(LED_COLORS _color) { + SetColor((unsigned int) _color); +}; + +void LED_WS2812::SetColor(unsigned int _color) { + for(int i=0;i<nbLeds;i++) { + pxArray->Set(i, _color); + pxArray->SetI(i,intensity); + } + __writeBuf(0); + nbInsert = 0; + if(rotationState) StopRotation(); + rotationPosition = nbLeds; +}; + + +void LED_WS2812::ResetColor() { + SetColor(BLACK); + + } + +void LED_WS2812::__writeBuf(int z) { + ws->write_offsets(pxArray->getBuf(),z,z,z); + wait(0.01); + } + +void LED_WS2812::__insert2buf() { + for(int i=0;i<nbLeds;i++) { + pxArray->Set(i, pxInsert->Get(i%nbInsert)); + } + __writeBuf(0); + rotationPosition = nbLeds; +} + +void LED_WS2812::__insertColor(unsigned int _color, int _intensity) { + pxInsert->Set(nbInsert%nbLeds,_color); + pxInsert->SetI(nbInsert%nbLeds,_intensity); + nbInsert++; + +}; + +void LED_WS2812::InsertColor(unsigned int _color) { + InsertColor(_color,intensity*100/0xFF); +}; + + +void LED_WS2812::InsertColor(unsigned int _color, float brightness) { + int pixelIntensity = brightness*0xFF/100; + __insertColor(_color, pixelIntensity); + __insert2buf(); +}; + + +void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) { + for(int i=0;i<N;i++) { + InsertColor(_color, brightness); + } +}; + +void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) { + InsertColorNtimes(N, _color, intensity*100/0xFF); +}; + + +void LED_WS2812::InsertColor(LED_COLORS _color) { + InsertColor((unsigned int)_color); +}; + + +void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) { + InsertColor((unsigned int)_color, brightness); +}; + + +void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) { + InsertColorNtimes(N, (unsigned int)_color, brightness); +}; + +void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) { + InsertColorNtimes(N, _color, intensity*100/0xFF); +}; + + +void LED_WS2812::SetIntensity(float perCent) { + intensity = perCent*0xFF/100; + ws->setII(intensity); + for(int i=0;i<nbLeds;i++) { + pxArray->SetI(i,intensity); + } +} + +void LED_WS2812::StartRotation(float interval) { + if(rotationState==false) { + rotationState = true; + LEDSystemTick.attach_us(callback(this, &LED_WS2812::Rotate), interval*1000000); + } +} + + +void LED_WS2812::StopRotation() { + if(rotationState==true) { + rotationState = false; + rotationPosition = 0; + LEDSystemTick.detach(); + } +} + +void LED_WS2812::Rotate() { + rotationPosition--; + if (rotationPosition == -1) + rotationPosition = nbLeds-1; + if(!blinkState) __writeBuf(rotationPosition); +} + + +void LED_WS2812::StartBlink(float interval) { + StopBlink(); + if(blinkState==false) { + blinkState = true; + LEDBlinkSystemTick.attach_us(callback(this, &LED_WS2812::Blink), interval*1000000); + } +} + + +void LED_WS2812::StopBlink() { + if(blinkState==true) { + blinkState = false; + LEDBlinkSystemTick.detach(); + } +} + +void LED_WS2812::Blink() { + blinkONOFF = !blinkONOFF; + if (blinkONOFF) + __writeBuf(rotationPosition); + else { + ws->useII(WS2812::GLOBAL); + ws->setII(0); + __writeBuf(rotationPosition); + ws->useII(WS2812::PER_PIXEL); + ws->setII(intensity); + + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/LED_WS2812.h Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,84 @@ + +#ifndef LED_WS2812_H +#define LED_WS2812_H + +#include "mbed.h" +#include "WS2812.h" +#include "PixelArray.h" + +/* +#define ZERO_HIGH 250.0 +#define ZERO_LOW 1000.0 +#define ONE_HIGH 1000.0 +#define ONE_LOW 250.0 +*/ +#define ZERO_HIGH 200.0 +#define ZERO_LOW 800.0 +#define ONE_HIGH 800.0 +#define ONE_LOW 200.0 + + typedef enum _LED_COLORS { + BLUE = 0x0000FF, + LIGHTBLUE = 0x00FFF6, + RED = 0xFF0000, + ORANGE = 0xFF3500, + GREEN = 0X00FF00, + BLACK = 0X000000, + WHITE = 0XFFFFFF, + PURPLE = 0XFF00FF, + PINK = 0XFF84A3, + YELLOW = 0XFFFF00, + DARK_YELLOW = 0X555500, + DEFAULT = 0x000000 + } LED_COLORS; + +class LED_WS2812 +{ +public: + LED_WS2812(PinName _PinOut, int _nbLeds); + ~LED_WS2812(); + void SetColor(LED_COLORS _color, int position); + void SetColor(unsigned int _color, int position); + void SetColor(unsigned int _color); + void SetColor(LED_COLORS _color); + void ResetColor(); + void SetIntensity(float perCent); + + void InsertColor(unsigned int _color); + void InsertColor(unsigned int _color, float brightness); + void InsertColorNtimes(int N, unsigned int _color, float brightness); + void InsertColorNtimes(int N, unsigned int _color); + + void InsertColor(LED_COLORS _color); + void InsertColor(LED_COLORS _color, float brightness); + void InsertColorNtimes(int N, LED_COLORS _color, float brightness); + void InsertColorNtimes(int N, LED_COLORS _color); + + void StartRotation(float interval); // interval in s + void StopRotation(); // + void Rotate(); // One Rotation + + void StartBlink(float interval); // interval in s + void StopBlink(); // + void Blink(); // One Rotation + +private: + void __writeBuf(int z); + void __insert2buf(); + void __insertColor(unsigned int _color, int _intensity); + + WS2812 *ws; + int nbLeds; + PixelArray *pxArray; + int nbInsert; + PixelArray *pxInsert; + Ticker LEDSystemTick; // System Callback for Rotation + Ticker LEDBlinkSystemTick; // System Callback for Rotation + bool rotationState; + bool blinkState; + int rotationPosition; + bool blinkONOFF; // ON = true, OFF = false + int intensity; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/Pixel/PixelArray.cpp Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,173 @@ +#include "PixelArray.h" + +PixelArray::PixelArray(int size) +{ + pbufsize = size; + pbuf = new int[pbufsize]; + SetAll(0x0); // initialise memory to zeros + +} + +PixelArray::~PixelArray() +{ + delete[] pbuf; +} + +void PixelArray::SetAll(unsigned int value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel(i,value); + } +} + + +void PixelArray::SetAllI(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,3,value); + } +} + + + +void PixelArray::SetAllR(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,2,value); + } +} + +void PixelArray::SetAllG(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,1,value); + } +} + +void PixelArray::SetAllB(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,0,value); + } +} + + + + + +void PixelArray::Set(int i, unsigned int value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel(i,value); + } +} + + + +void PixelArray::SetI(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,3,value); + } +} + + +void PixelArray::SetR(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,2,value); + } +} + +void PixelArray::SetG(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,1,value); + } +} + +void PixelArray::SetB(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,0,value); + } +} + + +unsigned int PixelArray::Get(int i) +{ + return __get_pixel(i); +} + +unsigned int PixelArray::GetI(int i) +{ + return __get_pixel_component(i,3); +} + +unsigned int PixelArray::GetR(int i) +{ + return __get_pixel_component(i,2); +} + +unsigned int PixelArray::GetG(int i) +{ + return __get_pixel_component(i,1); +} + +unsigned int PixelArray::GetB(int i) +{ + return __get_pixel_component(i,0); +} + + + +int* PixelArray::getBuf() +{ + return (pbuf); +} + + +// set either the I,R,G,B value of specific pixel channel +void PixelArray::__set_pixel_component(int index, int channel, int value) +{ + + // AND with 0x00 shifted to the right location to clear the bits + pbuf[index] &= ~(0xFF << (8 * channel)); + + // Set the bits with an OR + pbuf[index] |= (value << (8 * channel)); +} + + +// set either the I,R,G,B value of specific pixel channel +void PixelArray::__set_pixel(int index, int value) +{ + // AND with 0x00 shifted to the right location to clear the bits + pbuf[index] = value; +} + + +// get specific pixel +int PixelArray::__get_pixel(int index) +{ + if ((index >= 0) && (index < pbufsize)) { + return pbuf[index]; + } else { + return 0; + } +} + + +// get specific pixel +int PixelArray::__get_pixel_component(int index, int channel) +{ + // AND with 0xFF shifted to the right location to get the bits + return ( (__get_pixel(index) & (0xFF << (8 * channel))) >> (8*channel) ); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/Pixel/PixelArray.h Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) 2012 cstyles, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef PixelArray_H +#define PixelArray_H + +#include "mbed.h" + +//!Library for the WS2812 RGB LED with integrated controller +/*! +PixelArray +*/ +class PixelArray +{ +public: + //!Creates an instance of the class. + /*! + Pixel Array + */ + PixelArray(int); + + /*! + Destroys instance. + */ + ~PixelArray(); + + int* getBuf(); + + void SetAll(unsigned int); + void SetAllI(unsigned char); + void SetAllR(unsigned char); + void SetAllG(unsigned char); + void SetAllB(unsigned char); + + // location, value + void Set(int, unsigned int); + void SetI(int, unsigned char); + void SetR(int, unsigned char); + void SetG(int, unsigned char); + void SetB(int, unsigned char); + + unsigned int Get(int); + unsigned int GetI(int); + unsigned int GetR(int); + unsigned int GetG(int); + unsigned int GetB(int); + +private: + + int *pbuf; + int pbufsize; + + void __set_pixel_component(int index, int channel, int value); + void __set_pixel(int index, int value); + + int __get_pixel_component(int index, int channel); + int __get_pixel(int index); + +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/WS2812.lib Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/bridadan/code/WS2812/#6e647820f587
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X_NUCLEO_53L1A1_mbed.lib Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/ST-Expansion-SW-Team/code/X_NUCLEO_53L1A1_mbed/#0e99f593daa9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,183 @@ +/* + * This VL53L1X satellite board sample application performs range measurements + * with interrupts enabled to generate a hardware interrupt each time a new + * measurement is ready to be read. + * + * Measured ranges are output on the Serial Port, running at 115200 baud. + * + * On STM32-Nucleo boards : + * The User Blue button stops the current measurement and entire program, + * releasing all resources. + * + * The Black Reset button is used to restart the program. + * + * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of + * the X-NUCELO-53L1A1 expansion board are not made/OFF. + * These links must be made to allow interrupts from the Satellite boards + * to be received. + * U11 and U18 must be made/ON to allow interrupts to be received from the + * INT_L & INT_R positions; or + * U10 and U15 must be made/ON to allow interrupts to be received from the + * Alternate INT_L & INT_R positions. + * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R + * positions. + * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. + * Alternate INT_L is on CN5 Connector pin 2 as D8. + * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. + * Alternate INT_R is on CN9 Connector pin 5 as D2. + * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ + */ + +#include <stdio.h> + +#include "mbed.h" +#include "VL53L1X_I2C.h" +#include "VL53L1X_Class.h" +#include "LED_WS2812.h" + +#define VL53L1_I2C_SDA D14 +#define VL53L1_I2C_SCL D15 + +static VL53L1X *sensor = NULL; +Serial pc(SERIAL_TX, SERIAL_RX); +PwmOut servo(D6); + +/* flags that handle interrupt request for sensor and user blue button*/ +volatile bool int_sensor = false; +volatile bool int_stop = false; + + +/* ISR callback function of the sensor */ +void sensor_irq(void) +{ + int_sensor = true; + sensor->disable_interrupt_measure_detection_irq(); +} + +/* ISR callback function of the user blue button to switch measuring sensor. */ +void measuring_stop_irq(void) +{ + int_stop = true; +} + +/* Start the sensor ranging */ +int start_ranging() +{ + int status = 0; + /* start the measure on the sensor */ + if (NULL != sensor) { + status = sensor->stop_measurement(); + if (status != 0) { + return status; + } + + status = sensor->start_measurement(&sensor_irq); + if (status != 0) { + return status; + } + } + return status; +} + +int range_measure(VL53L1X_DevI2C *device_i2c) +{ + int status = 0; + uint16_t distance = 0; + int distance_close = 0; + + LED_WS2812 LED(A0,12); + LED.SetIntensity(5); + LED.SetColor(BLACK); + + /* Create a xshutdown pin */ + DigitalOut xshutdown(D7); + + /* create instance of sensor class */ + sensor = new VL53L1X(device_i2c, &xshutdown, A2); + + sensor->vl53l1_off(); + /* initialise sensor */ + sensor->init_sensor(0x52); + + if (status) { + delete sensor; + sensor= NULL; + printf("Sensor centre not present\n\r"); + } + + status = sensor->vl53l1x_set_inter_measurement_in_ms(1000); + status = sensor->vl53l1x_set_distance_threshold(50, + 50, 0, 0); + status = start_ranging(); + if (status != 0) { + printf("Failed to start ranging!\r\n"); + return status; + } + if (NULL != sensor) { + printf("Entering loop mode\r\n"); + /* Main ranging interrupt loop */ + while (true) { + if (int_sensor) { + + servo.period_ms(20); + + servo.pulsewidth_us(1150); + wait_ms(100); + servo.pulsewidth_us(1100); + wait_ms(100); + servo.pulsewidth_us(1050); + wait_ms(100); + servo.pulsewidth_us(1000); + wait_ms(100); + servo.pulsewidth_us(950); + wait_ms(100); + servo.pulsewidth_us(900); + wait_ms(100); + + servo.pulsewidth_us(850); + wait_ms(100); + servo.pulsewidth_us(800); + wait_ms(100); + servo.pulsewidth_us(750); + wait_ms(100); + servo.pulsewidth_us(700); +// wait_ms(100); +// servo.pulsewidth_us(650); +// wait_ms(100); +// servo.pulsewidth_us(600); +// wait_ms(100); +// servo.pulsewidth_us(550); +// wait_ms(100); +// servo.pulsewidth_us(500); + + wait_ms(500); + servo.pulsewidth_us(1150); + + int_sensor = false; + status = sensor->handle_irq(&distance); + printf("distance: %d\r\n", distance); + } + + } + } + + return status; + +} + + +/*=================================== Main ================================== +=============================================================================*/ +int main() +{ +#if TARGET_STM // we are cross compiling for an STM32-Nucleo + InterruptIn stop_button(USER_BUTTON); + stop_button.rise(&measuring_stop_irq); +#endif +#if TARGET_Freescale // we are cross-compiling for NXP FRDM boards. + InterruptIn stop_button(SW2); + stop_button.rise(&measuring_stop_irq); +#endif + VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); + range_measure(device_i2c); // start continuous measures +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Sep 29 09:08:55 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file