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: TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG BUTTON_GROUP
Revision 0:5d88688340fc, committed 2020-06-22
- Comitter:
- darkseb
- Date:
- Mon Jun 22 14:16:28 2020 +0000
- Commit message:
- Code_capteur-APDS9960_ecran-STM32F746G-DISCO
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,2 @@ +BUILD +mbed-os/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_APDS9960.cpp Mon Jun 22 14:16:28 2020 +0000
@@ -0,0 +1,768 @@
+/*!
+ * @file Adafruit_APDS9960.cpp
+ *
+ * @mainpage Adafruit APDS9960 Proximity, Light, RGB, and Gesture Sensor
+ *
+ * @section author Author
+ *
+ * Ladyada, Dean Miller (Adafruit Industries)
+ *
+ * @section license License
+ *
+ * Software License Agreement (BSD License)
+ *
+ * Copyright (c) 2017, Adafruit Industries
+ * All rights reserved.
+ *
+ * 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 the copyright holders 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 ''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 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.
+ */
+
+#ifdef __AVR
+#include <avr/pgmspace.h>
+#elif defined(ESP8266)
+#include <pgmspace.h>
+#endif
+#include <math.h>
+#include <stdlib.h>
+
+#include "Adafruit_APDS9960.h"
+
+/*!
+ * @brief Implements missing powf function
+ * @param x
+ * Base number
+ * @param y
+ * Exponent
+ * @return x raised to the power of y
+ */
+ /*
+float powf(const float x, const float y) {
+ return (float)(pow((double)x, (double)y));
+}
+*/
+
+/*!
+ * @brief Enables the device
+ * Disables the device (putting it in lower power sleep mode)
+ * @param en
+ * Enable (True/False)
+ */
+void Adafruit_APDS9960::enable(boolean en) {
+ _enable.PON = en;
+ this->write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Initializes I2C and configures the sensor
+ * @param iTimeMS
+ * Integration time
+ * @param aGain
+ * Gain
+ * @param addr
+ * I2C address
+ * @param *theWire
+ * Wire object
+ * @return True if initialization was successful, otherwise false.
+ */
+boolean Adafruit_APDS9960::begin(I2C *theWire, uint16_t iTimeMS, apds9960AGain_t aGain,
+ uint8_t addr) {
+ _wire = theWire;
+ _i2c_init();
+ _i2caddr = addr;
+
+ /* Make sure we're actually connected */
+ uint8_t x = read8(APDS9960_ID);
+ if (x != 0xAB) {
+ return false;
+ _pc->printf("TESTCPP %d", x);
+ }
+
+ /* Set default integration time and gain */
+ setADCIntegrationTime(iTimeMS);
+ setADCGain(aGain);
+
+ // disable everything to start
+ enableGesture(false);
+ enableProximity(false);
+ enableColor(false);
+
+ disableColorInterrupt();
+ disableProximityInterrupt();
+ clearInterrupt();
+
+ /* Note: by default, the device is in power down mode on bootup */
+ enable(false);
+ wait_us(10000);
+ enable(true);
+ wait_us(10000);
+
+ // default to all gesture dimensions
+ setGestureDimensions(APDS9960_DIMENSIONS_ALL);
+ setGestureFIFOThreshold(APDS9960_GFIFO_4);
+ setGestureGain(APDS9960_GGAIN_4);
+ setGestureProximityThreshold(50);
+ resetCounts();
+
+ _gpulse.GPLEN = APDS9960_GPULSE_32US;
+ _gpulse.GPULSE = 9; // 10 pulses
+ this->write8(APDS9960_GPULSE, _gpulse.get());
+
+ return true;
+}
+
+/*!
+ * @brief Sets the integration time for the ADC of the APDS9960, in millis
+ * @param iTimeMS
+ * Integration time
+ */
+void Adafruit_APDS9960::setADCIntegrationTime(uint16_t iTimeMS) {
+ float temp;
+
+ // convert ms into 2.78ms increments
+ temp = iTimeMS;
+ temp /= 2.78;
+ temp = 256 - temp;
+ if (temp > 255)
+ temp = 255;
+ if (temp < 0)
+ temp = 0;
+
+ /* Update the timing register */
+ write8(APDS9960_ATIME, (uint8_t)temp);
+}
+
+/*!
+ * @brief Returns the integration time for the ADC of the APDS9960, in millis
+ * @return Integration time
+ */
+float Adafruit_APDS9960::getADCIntegrationTime() {
+ float temp;
+
+ temp = read8(APDS9960_ATIME);
+
+ // convert to units of 2.78 ms
+ temp = 256 - temp;
+ temp *= 2.78;
+ return temp;
+}
+
+/*!
+ * @brief Adjusts the color/ALS gain on the APDS9960 (adjusts the sensitivity
+ * to light)
+ * @param aGain
+ * Gain
+ */
+void Adafruit_APDS9960::setADCGain(apds9960AGain_t aGain) {
+ _control.AGAIN = aGain;
+
+ /* Update the timing register */
+ write8(APDS9960_CONTROL, _control.get());
+}
+
+/*!
+ * @brief Returns the ADC gain
+ * @return ADC gain
+ */
+apds9960AGain_t Adafruit_APDS9960::getADCGain() {
+ return (apds9960AGain_t)(read8(APDS9960_CONTROL) & 0x03);
+}
+
+/*!
+ * @brief Adjusts the Proximity gain on the APDS9960
+ * @param pGain
+ * Gain
+ */
+void Adafruit_APDS9960::setProxGain(apds9960PGain_t pGain) {
+ _control.PGAIN = pGain;
+
+ /* Update the timing register */
+ write8(APDS9960_CONTROL, _control.get());
+}
+
+/*!
+ * @brief Returns the Proximity gain on the APDS9960
+ * @return Proxmity gain
+ */
+apds9960PGain_t Adafruit_APDS9960::getProxGain() {
+ return (apds9960PGain_t)(read8(APDS9960_CONTROL) & 0x0C);
+}
+
+/*!
+ * @brief Sets number of proxmity pulses
+ * @param pLen
+ * Pulse Length
+ * @param pulses
+ * Number of pulses
+ */
+void Adafruit_APDS9960::setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses) {
+ if (pulses < 1)
+ pulses = 1;
+ if (pulses > 64)
+ pulses = 64;
+ pulses--;
+
+ _ppulse.PPLEN = pLen;
+ _ppulse.PPULSE = pulses;
+
+ write8(APDS9960_PPULSE, _ppulse.get());
+}
+
+/*!
+ * @brief Enable proximity readings on APDS9960
+ * @param en
+ * Enable (True/False)
+ */
+void Adafruit_APDS9960::enableProximity(boolean en) {
+ _enable.PEN = en;
+
+ write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Enable proximity interrupts
+ */
+void Adafruit_APDS9960::enableProximityInterrupt() {
+ _enable.PIEN = 1;
+ write8(APDS9960_ENABLE, _enable.get());
+ clearInterrupt();
+}
+
+/*!
+ * @brief Disable proximity interrupts
+ */
+void Adafruit_APDS9960::disableProximityInterrupt() {
+ _enable.PIEN = 0;
+ write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Set proxmity interrupt thresholds
+ * @param low
+ * Low threshold
+ * @param high
+ * High threshold
+ * @param persistance
+ * Persistance
+ */
+void Adafruit_APDS9960::setProximityInterruptThreshold(uint8_t low,
+ uint8_t high,
+ uint8_t persistance) {
+ write8(APDS9960_PILT, low);
+ write8(APDS9960_PIHT, high);
+
+ if (persistance > 7)
+ persistance = 7;
+ _pers.PPERS = persistance;
+ write8(APDS9960_PERS, _pers.get());
+}
+
+/*!
+ * @brief Returns proxmity interrupt status
+ * @return True if enabled, false otherwise.
+ */
+bool Adafruit_APDS9960::getProximityInterrupt() {
+ _status.set(this->read8(APDS9960_STATUS));
+ return _status.PINT;
+};
+
+/*!
+ * @brief Read proximity data
+ * @return Proximity
+ */
+uint8_t Adafruit_APDS9960::readProximity() { return read8(APDS9960_PDATA); }
+
+/*!
+ * @brief Returns validity status of a gesture
+ * @return Status (True/False)
+ */
+bool Adafruit_APDS9960::gestureValid() {
+ _gstatus.set(this->read8(APDS9960_GSTATUS));
+ return _gstatus.GVALID;
+}
+
+/*!
+ * @brief Sets gesture dimensions
+ * @param dims
+ * Dimensions (APDS9960_DIMENSIONS_ALL, APDS9960_DIMENSIONS_UP_DOWM,
+ * APDS9960_DIMENSIONS_UP_DOWN, APGS9960_DIMENSIONS_LEFT_RIGHT)
+ */
+void Adafruit_APDS9960::setGestureDimensions(uint8_t dims) {
+ _gconf3.GDIMS = dims;
+ this->write8(APDS9960_GCONF3, _gconf3.get());
+}
+
+/*!
+ * @brief Sets gesture FIFO Threshold
+ * @param thresh
+ * Threshold (APDS9960_GFIFO_1, APDS9960_GFIFO_4, APDS9960_GFIFO_8,
+ * APDS9960_GFIFO_16)
+ */
+void Adafruit_APDS9960::setGestureFIFOThreshold(uint8_t thresh) {
+ _gconf1.GFIFOTH = thresh;
+ this->write8(APDS9960_GCONF1, _gconf1.get());
+}
+
+/*!
+ * @brief Sets gesture sensor gain
+ * @param gain
+ * Gain (APDS9960_GAIN_1, APDS9960_GAIN_2, APDS9960_GAIN_4,
+ * APDS9960_GAIN_8)
+ */
+void Adafruit_APDS9960::setGestureGain(uint8_t gain) {
+ _gconf2.GGAIN = gain;
+ this->write8(APDS9960_GCONF2, _gconf2.get());
+}
+
+/*!
+ * @brief Sets gesture sensor threshold
+ * @param thresh
+ * Threshold
+ */
+void Adafruit_APDS9960::setGestureProximityThreshold(uint8_t thresh) {
+ this->write8(APDS9960_GPENTH, thresh);
+}
+
+/*!
+ * @brief Sets gesture sensor offset
+ * @param offset_up
+ * Up offset
+ * @param offset_down
+ * Down offset
+ * @param offset_left
+ * Left offset
+ * @param offset_right
+ * Right offset
+ */
+void Adafruit_APDS9960::setGestureOffset(uint8_t offset_up, uint8_t offset_down,
+ uint8_t offset_left,
+ uint8_t offset_right) {
+ this->write8(APDS9960_GOFFSET_U, offset_up);
+ this->write8(APDS9960_GOFFSET_D, offset_down);
+ this->write8(APDS9960_GOFFSET_L, offset_left);
+ this->write8(APDS9960_GOFFSET_R, offset_right);
+}
+
+/*!
+ * @brief Enable gesture readings on APDS9960
+ * @param en
+ * Enable (True/False)
+ */
+void Adafruit_APDS9960::enableGesture(boolean en) {
+ if (!en) {
+ _gconf4.GMODE = 0;
+ write8(APDS9960_GCONF4, _gconf4.get());
+ }
+ _enable.GEN = en;
+ write8(APDS9960_ENABLE, _enable.get());
+ resetCounts();
+}
+
+/*!
+ * @brief Resets gesture counts
+ */
+void Adafruit_APDS9960::resetCounts() {
+ gestCnt = 0;
+ UCount = 0;
+ DCount = 0;
+ LCount = 0;
+ RCount = 0;
+}
+
+/*!
+ * @brief Reads gesture
+ * @return Received gesture (APDS9960_DOWN APDS9960_UP, APDS9960_LEFT
+ * APDS9960_RIGHT)
+ */
+uint8_t Adafruit_APDS9960::readGesture() {
+ uint8_t toRead;
+ uint8_t buf[256];
+ unsigned long t = 0;
+ uint8_t gestureReceived;
+ while (1) {
+ int up_down_diff = 0;
+ int left_right_diff = 0;
+ gestureReceived = 0;
+ if (!gestureValid())
+ return 0;
+
+ wait_us(30000);
+ toRead = this->read8(APDS9960_GFLVL);
+
+ // produces sideffects needed for readGesture to work
+ this->read(APDS9960_GFIFO_U, buf, toRead);
+
+ if (abs((int)buf[0] - (int)buf[1]) > 13)
+ up_down_diff += (int)buf[0] - (int)buf[1];
+
+ if (abs((int)buf[2] - (int)buf[3]) > 13)
+ left_right_diff += (int)buf[2] - (int)buf[3];
+
+ if (up_down_diff != 0) {
+ if (up_down_diff < 0) {
+ if (DCount > 0) {
+ gestureReceived = APDS9960_UP;
+ } else
+ UCount++;
+ } else if (up_down_diff > 0) {
+ if (UCount > 0) {
+ gestureReceived = APDS9960_DOWN;
+ } else
+ DCount++;
+ }
+ }
+
+ if (left_right_diff != 0) {
+ if (left_right_diff < 0) {
+ if (RCount > 0) {
+ gestureReceived = APDS9960_LEFT;
+ } else
+ LCount++;
+ } else if (left_right_diff > 0) {
+ if (LCount > 0) {
+ gestureReceived = APDS9960_RIGHT;
+ } else
+ RCount++;
+ }
+ }
+
+
+ if (up_down_diff != 0 || left_right_diff != 0){
+ t = clock_ms();
+ }
+
+ if (gestureReceived || clock_ms() - t > 300) {
+ resetCounts();
+ return gestureReceived;
+ }
+
+ }
+}
+
+/*!
+ * @brief Set LED brightness for proximity/gesture
+ * @param drive
+ * LED Drive
+ * @param boost
+ * LED Boost
+ */
+void Adafruit_APDS9960::setLED(apds9960LedDrive_t drive,
+ apds9960LedBoost_t boost) {
+ // set BOOST
+ _config2.LED_BOOST = boost;
+ write8(APDS9960_CONFIG2, _config2.get());
+
+ _control.LDRIVE = drive;
+ write8(APDS9960_CONTROL, _control.get());
+}
+
+/*!
+ * @brief Enable color readings on APDS9960
+ * @param en
+ * Enable (True/False)
+ */
+void Adafruit_APDS9960::enableColor(boolean en) {
+ _enable.AEN = en;
+ write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Returns status of color data
+ * @return True if color data ready, False otherwise
+ */
+bool Adafruit_APDS9960::colorDataReady() {
+ _status.set(this->read8(APDS9960_STATUS));
+ return _status.AVALID;
+}
+
+/*!
+ * @brief Reads the raw red, green, blue and clear channel values
+ * @param *r
+ * Red value
+ * @param *g
+ * Green value
+ * @param *b
+ * Blue value
+ * @param *c
+ * Clear channel value
+ */
+void Adafruit_APDS9960::getColorData(uint16_t *r, uint16_t *g, uint16_t *b,
+ uint16_t *c) {
+
+ *c = read16R(APDS9960_CDATAL);
+ *r = read16R(APDS9960_RDATAL);
+ *g = read16R(APDS9960_GDATAL);
+ *b = read16R(APDS9960_BDATAL);
+}
+
+/*!
+ * @brief Converts the raw R/G/B values to color temperature in degrees Kelvin
+ * @param r
+ * Red value
+ * @param g
+ * Green value
+ * @param b
+ * Blue value
+ * @return Color temperature
+ */
+uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g,
+ uint16_t b) {
+ float X, Y, Z; /* RGB to XYZ correlation */
+ float xc, yc; /* Chromaticity co-ordinates */
+ float n; /* McCamy's formula */
+ float cct;
+
+ /* 1. Map RGB values to their XYZ counterparts. */
+ /* Based on 6500K fluorescent, 3000K fluorescent */
+ /* and 60W incandescent values for a wide range. */
+ /* Note: Y = Illuminance or lux */
+ X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
+ Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
+ Z = (-0.68202F * r) + (0.77073F * g) + (0.56332F * b);
+
+ /* 2. Calculate the chromaticity co-ordinates */
+ xc = (X) / (X + Y + Z);
+ yc = (Y) / (X + Y + Z);
+
+ /* 3. Use McCamy's formula to determine the CCT */
+ n = (xc - 0.3320F) / (0.1858F - yc);
+
+ /* Calculate the final CCT */
+ cct =
+ (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
+
+ /* Return the results in degrees Kelvin */
+ return (uint16_t)cct;
+}
+
+/*!
+ * @brief Calculate ambient light values
+ * @param r
+ * Red value
+ * @param g
+ * Green value
+ * @param b
+ * Blue value
+ * @return LUX value
+ */
+uint16_t Adafruit_APDS9960::calculateLux(uint16_t r, uint16_t g, uint16_t b) {
+ float illuminance;
+
+ /* This only uses RGB ... how can we integrate clear or calculate lux */
+ /* based exclusively on clear since this might be more reliable? */
+ illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
+
+ return (uint16_t)illuminance;
+}
+
+/*!
+ * @brief Enables color interrupt
+ */
+void Adafruit_APDS9960::enableColorInterrupt() {
+ _enable.AIEN = 1;
+ write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Disables color interrupt
+ */
+void Adafruit_APDS9960::disableColorInterrupt() {
+ _enable.AIEN = 0;
+ write8(APDS9960_ENABLE, _enable.get());
+}
+
+/*!
+ * @brief Clears interrupt
+ */
+void Adafruit_APDS9960::clearInterrupt() {
+ this->write(APDS9960_AICLEAR, NULL, 0);
+}
+
+/*!
+ * @brief Sets interrupt limits
+ * @param low
+ * Low limit
+ * @param high
+ * High limit
+ */
+void Adafruit_APDS9960::setIntLimits(uint16_t low, uint16_t high) {
+ write8(APDS9960_AILTIL, low & 0xFF);
+ write8(APDS9960_AILTH, low >> 8);
+ write8(APDS9960_AIHTL, high & 0xFF);
+ write8(APDS9960_AIHTH, high >> 8);
+}
+
+/*!
+ * @brief Writes specified value to given register
+ * @param reg
+ * Register to write to
+ * @param value
+ * Value to write
+ */
+void Adafruit_APDS9960::write8(byte reg, byte value) {
+ this->write(reg, &value, 1);
+}
+
+/*!
+ * @brief Reads 8 bits from specified register
+ * @param reg
+ * Register to write to
+ * @return Value in register
+ */
+uint8_t Adafruit_APDS9960::read8(byte reg) {
+ uint8_t ret;
+ this->read(reg, &ret, 1);
+
+ return ret;
+}
+
+/*!
+ * @brief Reads 32 bits from specified register
+ * @param reg
+ * Register to write to
+ * @return Value in register
+ */
+uint32_t Adafruit_APDS9960::read32(uint8_t reg) {
+ uint8_t ret[4];
+ uint32_t ret32;
+ this->read(reg, ret, 4);
+ ret32 = ret[3];
+ ret32 |= (uint32_t)ret[2] << 8;
+ ret32 |= (uint32_t)ret[1] << 16;
+ ret32 |= (uint32_t)ret[0] << 24;
+ return ret32;
+}
+
+/*!
+ * @brief Reads 16 bites from specified register
+ * @param reg
+ * Register to write to
+ * @return Value in register
+ */
+uint16_t Adafruit_APDS9960::read16(uint8_t reg) {
+ uint8_t ret[2];
+ this->read(reg, ret, 2);
+
+ return (ret[0] << 8) | ret[1];
+}
+
+/*!
+ * @brief Reads 16 bites from specified register
+ * @param reg
+ * Register to write to
+ * @return Value in register
+ */
+uint16_t Adafruit_APDS9960::read16R(uint8_t reg) {
+ uint8_t ret[2];
+ this->read(reg, ret, 2);
+
+ return (ret[1] << 8) | ret[0];
+}
+
+/*!
+ * @brief Begins I2C communication
+ */
+void Adafruit_APDS9960::_i2c_init() {
+ //_wire->begin(); // on mbed, we are already master if using the I2C class instead of I2CSlave class
+}
+
+/*!
+ * @brief Reads num bytes from specified register into a given buffer
+ * @param reg
+ * Register
+ * @param *buf
+ * Buffer
+ * @param num
+ * Number of bytes
+ * @return Position after reading
+ */
+uint8_t Adafruit_APDS9960::read(uint8_t reg, uint8_t *buf, uint8_t num) {
+ uint8_t pos = 0;
+ bool eof = false;
+
+ // on arduino we need to read in 32 byte chunks
+ //while (pos < num && !eof) {
+
+ //uint8_t read_now = min(32, num - pos);
+ /*
+ _wire->beginTransmission((uint8_t)_i2caddr);
+ _wire->write((uint8_t)reg + pos);
+ _wire->endTransmission();
+
+ _wire->requestFrom((uint8_t)_i2caddr, read_now);
+ */
+
+ //_wire->start();
+ //_wire->write((uint8_t)_i2caddr);
+ //_wire->write((uint8_t)reg);
+ //_wire->stop();
+
+ _wire->write((uint8_t)_i2caddr, (char *)®, 1, true);
+
+ _wire->read((uint8_t)_i2caddr, (char *) buf, num);
+
+
+/*
+ for (int i = 0; i < read_now; i++) {
+ if (!_wire->available()) {
+ eof = true;
+ break;
+ }
+ buf[pos] = _wire->read();
+ pos++;
+ }
+ */
+ //}
+
+ return num;
+}
+
+/*!
+ * @brief Writes num bytes from specified buffer into a given register
+ * @param reg
+ * Register
+ * @param *buf
+ * Buffer
+ * @param num
+ * Number of bytes
+ */
+void Adafruit_APDS9960::write(uint8_t reg, uint8_t *buf, uint8_t num) {
+ /*
+ _wire->beginTransmission((uint8_t)_i2caddr);
+ _wire->write((uint8_t)reg);
+ _wire->write((uint8_t *)buf, num);
+ _wire->endTransmission();
+ */
+
+ _wire->start();
+ _wire->write((uint8_t)_i2caddr);
+ _wire->write((uint8_t)reg);
+
+ for(int i=0; i < num; i++){ // on parcours le "tableau" buf
+ uint8_t value = *(buf+i); // on récupere la valeur à l'emplacement "i" du tableau
+ _wire->write(value); // j'écris la valeur du tableau à l'emplacement "i" sur le bus i2c.
+ }
+
+ _wire->stop();
+
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_APDS9960.h Mon Jun 22 14:16:28 2020 +0000
@@ -0,0 +1,535 @@
+/*!
+ * @file Adafruit_APDS9960.h
+ *
+ * Software License Agreement (BSD License)
+ *
+ * Copyright (c) 2017, Adafruit Industries
+ * All rights reserved.
+ *
+ * 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 the copyright holders 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 ''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 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.
+ */
+#ifndef _APDS9960_H_
+#define _APDS9960_H_
+
+#include "mbed.h"
+#include "Clock.h"
+
+typedef bool boolean;
+typedef uint8_t byte;
+
+#define APDS9960_ADDRESS (0x39<<1) /**< I2C Address */
+
+/** I2C Registers */
+enum {
+ APDS9960_RAM = 0x00,
+ APDS9960_ENABLE = 0x80,
+ APDS9960_ATIME = 0x81,
+ APDS9960_WTIME = 0x83,
+ APDS9960_AILTIL = 0x84,
+ APDS9960_AILTH = 0x85,
+ APDS9960_AIHTL = 0x86,
+ APDS9960_AIHTH = 0x87,
+ APDS9960_PILT = 0x89,
+ APDS9960_PIHT = 0x8B,
+ APDS9960_PERS = 0x8C,
+ APDS9960_CONFIG1 = 0x8D,
+ APDS9960_PPULSE = 0x8E,
+ APDS9960_CONTROL = 0x8F,
+ APDS9960_CONFIG2 = 0x90,
+ APDS9960_ID = 0x92,
+ APDS9960_STATUS = 0x93,
+ APDS9960_CDATAL = 0x94,
+ APDS9960_CDATAH = 0x95,
+ APDS9960_RDATAL = 0x96,
+ APDS9960_RDATAH = 0x97,
+ APDS9960_GDATAL = 0x98,
+ APDS9960_GDATAH = 0x99,
+ APDS9960_BDATAL = 0x9A,
+ APDS9960_BDATAH = 0x9B,
+ APDS9960_PDATA = 0x9C,
+ APDS9960_POFFSET_UR = 0x9D,
+ APDS9960_POFFSET_DL = 0x9E,
+ APDS9960_CONFIG3 = 0x9F,
+ APDS9960_GPENTH = 0xA0,
+ APDS9960_GEXTH = 0xA1,
+ APDS9960_GCONF1 = 0xA2,
+ APDS9960_GCONF2 = 0xA3,
+ APDS9960_GOFFSET_U = 0xA4,
+ APDS9960_GOFFSET_D = 0xA5,
+ APDS9960_GOFFSET_L = 0xA7,
+ APDS9960_GOFFSET_R = 0xA9,
+ APDS9960_GPULSE = 0xA6,
+ APDS9960_GCONF3 = 0xAA,
+ APDS9960_GCONF4 = 0xAB,
+ APDS9960_GFLVL = 0xAE,
+ APDS9960_GSTATUS = 0xAF,
+ APDS9960_IFORCE = 0xE4,
+ APDS9960_PICLEAR = 0xE5,
+ APDS9960_CICLEAR = 0xE6,
+ APDS9960_AICLEAR = 0xE7,
+ APDS9960_GFIFO_U = 0xFC,
+ APDS9960_GFIFO_D = 0xFD,
+ APDS9960_GFIFO_L = 0xFE,
+ APDS9960_GFIFO_R = 0xFF,
+};
+
+/** ADC gain settings */
+typedef enum {
+ APDS9960_AGAIN_1X = 0x00, /**< No gain */
+ APDS9960_AGAIN_4X = 0x01, /**< 2x gain */
+ APDS9960_AGAIN_16X = 0x02, /**< 16x gain */
+ APDS9960_AGAIN_64X = 0x03 /**< 64x gain */
+} apds9960AGain_t;
+
+/** Proxmity gain settings */
+typedef enum {
+ APDS9960_PGAIN_1X = 0x00, /**< 1x gain */
+ APDS9960_PGAIN_2X = 0x04, /**< 2x gain */
+ APDS9960_PGAIN_4X = 0x08, /**< 4x gain */
+ APDS9960_PGAIN_8X = 0x0C /**< 8x gain */
+} apds9960PGain_t;
+
+/** Pulse length settings */
+typedef enum {
+ APDS9960_PPULSELEN_4US = 0x00, /**< 4uS */
+ APDS9960_PPULSELEN_8US = 0x40, /**< 8uS */
+ APDS9960_PPULSELEN_16US = 0x80, /**< 16uS */
+ APDS9960_PPULSELEN_32US = 0xC0 /**< 32uS */
+} apds9960PPulseLen_t;
+
+/** LED drive settings */
+typedef enum {
+ APDS9960_LEDDRIVE_100MA = 0x00, /**< 100mA */
+ APDS9960_LEDDRIVE_50MA = 0x40, /**< 50mA */
+ APDS9960_LEDDRIVE_25MA = 0x80, /**< 25mA */
+ APDS9960_LEDDRIVE_12MA = 0xC0 /**< 12.5mA */
+} apds9960LedDrive_t;
+
+/** LED boost settings */
+typedef enum {
+ APDS9960_LEDBOOST_100PCNT = 0x00, /**< 100% */
+ APDS9960_LEDBOOST_150PCNT = 0x10, /**< 150% */
+ APDS9960_LEDBOOST_200PCNT = 0x20, /**< 200% */
+ APDS9960_LEDBOOST_300PCNT = 0x30 /**< 300% */
+} apds9960LedBoost_t;
+
+/** Dimensions */
+enum {
+ APDS9960_DIMENSIONS_ALL = 0x00, // All dimensions
+ APDS9960_DIMENSIONS_UP_DOWN = 0x01, // Up/Down dimensions
+ APGS9960_DIMENSIONS_LEFT_RIGHT = 0x02, // Left/Right dimensions
+};
+
+/** FIFO Interrupts */
+enum {
+ APDS9960_GFIFO_1 = 0x00, // Generate interrupt after 1 dataset in FIFO
+ APDS9960_GFIFO_4 = 0x01, // Generate interrupt after 2 datasets in FIFO
+ APDS9960_GFIFO_8 = 0x02, // Generate interrupt after 3 datasets in FIFO
+ APDS9960_GFIFO_16 = 0x03, // Generate interrupt after 4 datasets in FIFO
+};
+
+/** Gesture Gain */
+enum {
+ APDS9960_GGAIN_1 = 0x00, // Gain 1x
+ APDS9960_GGAIN_2 = 0x01, // Gain 2x
+ APDS9960_GGAIN_4 = 0x02, // Gain 4x
+ APDS9960_GGAIN_8 = 0x03, // Gain 8x
+};
+
+/** Pulse Lenghts */
+enum {
+ APDS9960_GPULSE_4US = 0x00, // Pulse 4us
+ APDS9960_GPULSE_8US = 0x01, // Pulse 8us
+ APDS9960_GPULSE_16US = 0x02, // Pulse 16us
+ APDS9960_GPULSE_32US = 0x03, // Pulse 32us
+};
+
+#define APDS9960_UP 0x01 /**< Gesture Up */
+#define APDS9960_DOWN 0x02 /**< Gesture Down */
+#define APDS9960_LEFT 0x03 /**< Gesture Left */
+#define APDS9960_RIGHT 0x04 /**< Gesture Right */
+
+/*!
+ * @brief Class that stores state and functions for interacting with
+ * APDS9960 Sensor
+ */
+class Adafruit_APDS9960 {
+public:
+ Adafruit_APDS9960(Serial *serial){_pc=serial;};
+ ~Adafruit_APDS9960(){};
+
+ boolean begin(I2C *theWire, uint16_t iTimeMS = 10, apds9960AGain_t = APDS9960_AGAIN_4X,
+ uint8_t addr = APDS9960_ADDRESS);
+ void setADCIntegrationTime(uint16_t iTimeMS);
+ float getADCIntegrationTime();
+ void setADCGain(apds9960AGain_t gain);
+ apds9960AGain_t getADCGain();
+ void setLED(apds9960LedDrive_t drive, apds9960LedBoost_t boost);
+
+ // proximity
+ void enableProximity(boolean en = true);
+ void setProxGain(apds9960PGain_t gain);
+ apds9960PGain_t getProxGain();
+ void setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses);
+ void enableProximityInterrupt();
+ void disableProximityInterrupt();
+ uint8_t readProximity();
+ void setProximityInterruptThreshold(uint8_t low, uint8_t high,
+ uint8_t persistance = 4);
+ bool getProximityInterrupt();
+
+ // gesture
+ void enableGesture(boolean en = true);
+ bool gestureValid();
+ void setGestureDimensions(uint8_t dims);
+ void setGestureFIFOThreshold(uint8_t thresh);
+ void setGestureGain(uint8_t gain);
+ void setGestureProximityThreshold(uint8_t thresh);
+ void setGestureOffset(uint8_t offset_up, uint8_t offset_down,
+ uint8_t offset_left, uint8_t offset_right);
+ uint8_t readGesture();
+ void resetCounts();
+
+ // light & color
+ void enableColor(boolean en = true);
+ bool colorDataReady();
+ void getColorData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
+ uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
+ uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
+ void enableColorInterrupt();
+ void disableColorInterrupt();
+ void clearInterrupt();
+ void setIntLimits(uint16_t l, uint16_t h);
+
+ // turn on/off elements
+ void enable(boolean en = true);
+
+private:
+ uint8_t _i2caddr;
+ I2C *_wire;
+ Serial *_pc;
+
+ uint32_t read32(uint8_t reg);
+ uint16_t read16(uint8_t reg);
+ uint16_t read16R(uint8_t reg);
+
+ void write8(byte reg, byte value);
+ uint8_t read8(byte reg);
+
+ uint8_t gestCnt;
+
+ uint8_t UCount;
+ uint8_t DCount;
+
+ uint8_t LCount;
+ uint8_t RCount;
+
+ uint8_t read(uint8_t reg, uint8_t *buf, uint8_t num);
+ void write(uint8_t reg, uint8_t *buf, uint8_t num);
+ void _i2c_init();
+
+ struct enable {
+
+ // power on
+ uint8_t PON : 1;
+
+ // ALS enable
+ uint8_t AEN : 1;
+
+ // Proximity detect enable
+ uint8_t PEN : 1;
+
+ // wait timer enable
+ uint8_t WEN : 1;
+
+ // ALS interrupt enable
+ uint8_t AIEN : 1;
+
+ // proximity interrupt enable
+ uint8_t PIEN : 1;
+
+ // gesture enable
+ uint8_t GEN : 1;
+
+ uint8_t get() {
+ return (GEN << 6) | (PIEN << 5) | (AIEN << 4) | (WEN << 3) | (PEN << 2) |
+ (AEN << 1) | PON;
+ };
+ };
+ struct enable _enable;
+
+ struct pers {
+ // ALS Interrupt Persistence. Controls rate of Clear channel interrupt to
+ // the host processor
+ uint8_t APERS : 4;
+
+ // proximity interrupt persistence, controls rate of prox interrupt to host
+ // processor
+ uint8_t PPERS : 4;
+
+ uint8_t get() { return (PPERS << 4) | APERS; };
+ };
+ pers _pers;
+
+ struct config1 {
+ uint8_t WLONG : 1;
+
+ uint8_t get() { return WLONG << 1; };
+ };
+ config1 _config1;
+
+ struct ppulse {
+
+ /*Proximity Pulse Count. Specifies the number of proximity pulses to be
+ generated on LDR. Number of pulses is set by PPULSE value plus 1.
+ */
+ uint8_t PPULSE : 6;
+
+ // Proximity Pulse Length. Sets the LED-ON pulse width during a proximity
+ // LDR pulse.
+ uint8_t PPLEN : 2;
+
+ uint8_t get() { return (PPLEN << 6) | PPULSE; }
+ };
+ ppulse _ppulse;
+
+ struct control {
+ // ALS and Color gain control
+ uint8_t AGAIN : 2;
+
+ // proximity gain control
+ uint8_t PGAIN : 2;
+
+ // led drive strength
+ uint8_t LDRIVE : 2;
+
+ uint8_t get() { return (LDRIVE << 6) | (PGAIN << 2) | AGAIN; }
+ };
+ control _control;
+
+ struct config2 {
+ /* Additional LDR current during proximity and gesture LED pulses. Current
+ value, set by LDRIVE, is increased by the percentage of LED_BOOST.
+ */
+ uint8_t LED_BOOST : 2;
+
+ // clear photodiode saturation int enable
+ uint8_t CPSIEN : 1;
+
+ // proximity saturation interrupt enable
+ uint8_t PSIEN : 1;
+
+ uint8_t get() {
+ return (PSIEN << 7) | (CPSIEN << 6) | (LED_BOOST << 4) | 1;
+ }
+ };
+ config2 _config2;
+
+ struct status {
+ /* ALS Valid. Indicates that an ALS cycle has completed since AEN was
+ asserted or since a read from any of the ALS/Color data registers.
+ */
+ uint8_t AVALID : 1;
+
+ /* Proximity Valid. Indicates that a proximity cycle has completed since PEN
+ was asserted or since PDATA was last read. A read of PDATA automatically
+ clears PVALID.
+ */
+ uint8_t PVALID : 1;
+
+ /* Gesture Interrupt. GINT is asserted when GFVLV becomes greater than
+ GFIFOTH or if GVALID has become asserted when GMODE transitioned to zero.
+ The bit is reset when FIFO is completely emptied (read).
+ */
+ uint8_t GINT : 1;
+
+ // ALS Interrupt. This bit triggers an interrupt if AIEN in ENABLE is set.
+ uint8_t AINT : 1;
+
+ // Proximity Interrupt. This bit triggers an interrupt if PIEN in ENABLE is
+ // set.
+ uint8_t PINT : 1;
+
+ /* Indicates that an analog saturation event occurred during a previous
+ proximity or gesture cycle. Once set, this bit remains set until cleared by
+ clear proximity interrupt special function command (0xE5 PICLEAR) or by
+ disabling Prox (PEN=0). This bit triggers an interrupt if PSIEN is set.
+ */
+ uint8_t PGSAT : 1;
+
+ /* Clear Photodiode Saturation. When asserted, the analog sensor was at the
+ upper end of its dynamic range. The bit can be de-asserted by sending a
+ Clear channel interrupt command (0xE6 CICLEAR) or by disabling the ADC
+ (AEN=0). This bit triggers an interrupt if CPSIEN is set.
+ */
+ uint8_t CPSAT : 1;
+
+ void set(uint8_t data) {
+ AVALID = data & 0x01;
+ PVALID = (data >> 1) & 0x01;
+ GINT = (data >> 2) & 0x01;
+ AINT = (data >> 4) & 0x01;
+ PINT = (data >> 5) & 0x01;
+ PGSAT = (data >> 6) & 0x01;
+ CPSAT = (data >> 7) & 0x01;
+ }
+ };
+ status _status;
+
+ struct config3 {
+ // proximity mask
+ uint8_t PMASK_R : 1;
+ uint8_t PMASK_L : 1;
+ uint8_t PMASK_D : 1;
+ uint8_t PMASK_U : 1;
+
+ /* Sleep After Interrupt. When enabled, the device will automatically enter
+ low power mode when the INT pin is asserted and the state machine has
+ progressed to the SAI decision block. Normal operation is resumed when INT
+ pin is cleared over I2C.
+ */
+ uint8_t SAI : 1;
+
+ /* Proximity Gain Compensation Enable. This bit provides gain compensation
+ when proximity photodiode signal is reduced as a result of sensor masking.
+ If only one diode of the diode pair is contributing, then only half of the
+ signal is available at the ADC; this results in a maximum ADC value of 127.
+ Enabling PCMP enables an additional gain of 2X, resulting in a maximum ADC
+ value of 255.
+ */
+ uint8_t PCMP : 1;
+
+ uint8_t get() {
+ return (PCMP << 5) | (SAI << 4) | (PMASK_U << 3) | (PMASK_D << 2) |
+ (PMASK_L << 1) | PMASK_R;
+ }
+ };
+ config3 _config3;
+
+ struct gconf1 {
+ /* Gesture Exit Persistence. When a number of consecutive gesture end
+ occurrences become equal or greater to the GEPERS value, the Gesture state
+ machine is exited.
+ */
+ uint8_t GEXPERS : 2;
+
+ /* Gesture Exit Mask. Controls which of the gesture detector photodiodes
+ (UDLR) will be included to determine a gesture end and subsequent exit
+ of the gesture state machine. Unmasked UDLR data will be compared with the
+ value in GTHR_OUT. Field value bits correspond to UDLR detectors.
+ */
+ uint8_t GEXMSK : 4;
+
+ /* Gesture FIFO Threshold. This value is compared with the FIFO Level (i.e.
+ the number of UDLR datasets) to generate an interrupt (if enabled).
+ */
+ uint8_t GFIFOTH : 2;
+
+ uint8_t get() { return (GFIFOTH << 6) | (GEXMSK << 2) | GEXPERS; }
+ };
+ gconf1 _gconf1;
+
+ struct gconf2 {
+ /* Gesture Wait Time. The GWTIME controls the amount of time in a low power
+ mode between gesture detection cycles.
+ */
+ uint8_t GWTIME : 3;
+
+ // Gesture LED Drive Strength. Sets LED Drive Strength in gesture mode.
+ uint8_t GLDRIVE : 2;
+
+ // Gesture Gain Control. Sets the gain of the proximity receiver in gesture
+ // mode.
+ uint8_t GGAIN : 2;
+
+ uint8_t get() { return (GGAIN << 5) | (GLDRIVE << 3) | GWTIME; }
+ };
+ gconf2 _gconf2;
+
+ struct gpulse {
+ /* Number of Gesture Pulses. Specifies the number of pulses to be generated
+ on LDR. Number of pulses is set by GPULSE value plus 1.
+ */
+ uint8_t GPULSE : 6;
+
+ // Gesture Pulse Length. Sets the LED_ON pulse width during a Gesture LDR
+ // Pulse.
+ uint8_t GPLEN : 2;
+
+ uint8_t get() { return (GPLEN << 6) | GPULSE; }
+ };
+ gpulse _gpulse;
+
+ struct gconf3 {
+ /* Gesture Dimension Select. Selects which gesture photodiode pairs are
+ enabled to gather results during gesture.
+ */
+ uint8_t GDIMS : 2;
+
+ uint8_t get() { return GDIMS; }
+ };
+ gconf3 _gconf3;
+
+ struct gconf4 {
+ /* Gesture Mode. Reading this bit reports if the gesture state machine is
+ actively running, 1 = Gesture, 0= ALS, Proximity, Color. Writing a 1 to this
+ bit causes immediate entry in to the gesture state machine (as if GPENTH had
+ been exceeded). Writing a 0 to this bit causes exit of gesture when current
+ analog conversion has finished (as if GEXTH had been exceeded).
+ */
+ uint8_t GMODE : 1;
+
+ /* Gesture interrupt enable. Gesture Interrupt Enable. When asserted, all
+ gesture related interrupts are unmasked.
+ */
+ uint8_t GIEN : 2;
+
+ uint8_t get() { return (GIEN << 1) | GMODE; }
+ void set(uint8_t data) {
+ GIEN = (data >> 1) & 0x01;
+ GMODE = data & 0x01;
+ }
+ };
+ gconf4 _gconf4;
+
+ struct gstatus {
+ /* Gesture FIFO Data. GVALID bit is sent when GFLVL becomes greater than
+ GFIFOTH (i.e. FIFO has enough data to set GINT). GFIFOD is reset when GMODE
+ = 0 and the GFLVL=0 (i.e. All FIFO data has been read).
+ */
+ uint8_t GVALID : 1;
+
+ /* Gesture FIFO Overflow. A setting of 1 indicates that the FIFO has filled
+ to capacity and that new gesture detector data has been lost.
+ */
+ uint8_t GFOV : 1;
+
+ void set(uint8_t data) {
+ GFOV = (data >> 1) & 0x01;
+ GVALID = data & 0x01;
+ }
+ };
+ gstatus _gstatus;
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_DISCO_F746NG.lib Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/BSP_DISCO_F746NG/#85dbcff443aa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BUTTON_GROUP.lib Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/MikamiUitOpen/code/BUTTON_GROUP/#af578b53ff0e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Clock.cpp Mon Jun 22 14:16:28 2020 +0000
@@ -0,0 +1,64 @@
+#include <mbed.h>
+
+// The us ticker is a wrapping uint32_t. We insert interrupts at
+// 0, 0x40000000, 0x8000000, and 0xC0000000, rather than just 0 or just 0xFFFFFFFF because there is
+// code that calls interrupts that are "very soon" immediately and we don't
+// want that. Also because if we only use 0 and 0x80000000 then there is a chance it would
+// be considered to be in the past and executed immediately.
+
+class ExtendedClock : public TimerEvent
+{
+public:
+ ExtendedClock()
+ {
+ // This also starts the us ticker.
+ insert(0x40000000);
+ }
+
+ float read()
+ {
+ return read_us() / 1000000.0f;
+ }
+
+ uint64_t read_ms()
+ {
+ return read_us() / 1000;
+ }
+
+ uint64_t read_us()
+ {
+ return mTriggers * 0x40000000ull + (ticker_read(_ticker_data) & 0x3FFFFFFF);
+ }
+
+private:
+ void handler() override
+ {
+ ++mTriggers;
+ // If this is the first time we've been called (at 0x4...)
+ // then mTriggers now equals 1 and we want to insert at 0x80000000.
+ insert((mTriggers+1) * 0x40000000);
+ }
+
+ // The number of times the us_ticker has rolled over.
+ uint32_t mTriggers = 0;
+};
+
+static ExtendedClock _GlobalClock;
+
+// Return the number of seconds since boot.
+float clock_s()
+{
+ return _GlobalClock.read();
+}
+
+// Return the number of milliseconds since boot.
+uint64_t clock_ms()
+{
+ return _GlobalClock.read_ms();
+}
+
+// Return the number of microseconds since boot.
+uint64_t clock_us()
+{
+ return _GlobalClock.read_us();
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Clock.h Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,12 @@ +#pragma once + +#include <stdint.h> + +// Return the number of seconds since boot. +float clock_s(); + +// Return the number of milliseconds since boot. +uint64_t clock_ms(); + +// Return the number of microseconds since boot. +uint64_t clock_us(); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_DISCO_F746NG.lib Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/LCD_DISCO_F746NG/#d44525b1de98
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TS_DISCO_F746NG.lib Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/TS_DISCO_F746NG/#fe0cf5e2960f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Jun 22 14:16:28 2020 +0000
@@ -0,0 +1,285 @@
+#include "mbed.h"
+#include "stm32746g_discovery_lcd.h"
+#include "stm32746g_discovery_ts.h"
+#include "button_group.hpp"
+#include "Adafruit_APDS9960.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+I2C * i2c_sensor;
+Adafruit_APDS9960 *sensor;
+
+using namespace Mikami;
+TS_DISCO_F746NG ts_;
+LCD_DISCO_F746NG lcd_;
+
+
+void init_sensor()
+{
+ i2c_sensor = new I2C(I2C_SDA, I2C_SCL);
+ sensor = new Adafruit_APDS9960(&pc);
+ bool initok = sensor->begin(i2c_sensor);
+ if (!initok)
+ {
+ pc.printf("\n\rErreur avec l'initialisation du capteur de couleur\n\r");
+ }else pc.printf("\n\rLe capteur de couleur a ete initialise\n\r");
+ sensor->enableColor(true);
+}
+
+
+int main()
+{
+ init_sensor();
+
+ unsigned int boutton_commencer = 0;
+ unsigned int boutton_reset = 0;
+
+ unsigned int nbAlea = 0;
+ unsigned int couleur;
+ unsigned int couleur1;
+ unsigned int couleur2;
+ unsigned int couleur3;
+ unsigned int couleur4;
+ unsigned int sequence = 0;
+ unsigned int i = 0;
+
+ unsigned int reponse;
+ unsigned int reponse1;
+ unsigned int reponse2;
+ unsigned int reponse3;
+ unsigned int reponse4;
+ unsigned int sequence_reponse = 0;
+ unsigned int j = 0;
+
+ BSP_LCD_Init();
+ BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
+ BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
+
+
+ while(1)
+ {
+ //Ecran d'acceuil
+ BSP_LCD_Clear(LCD_COLOR_BLACK);
+ BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
+ BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+ BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+
+ BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"BIENVENUE", CENTER_MODE);
+ HAL_Delay(2000);
+
+ BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+ BSP_LCD_SetTextColor(LCD_COLOR_RED);
+ BSP_LCD_DisplayStringAt(0, 90, (uint8_t *)"SUR UN", CENTER_MODE);
+ HAL_Delay(2000);
+
+ BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+ BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+ BSP_LCD_DisplayStringAt(0, 140, (uint8_t *)"CAPTEUR DE COULEUR", CENTER_MODE);
+ HAL_Delay(2000);
+
+
+ //Bouton commencer la partie
+ Button commencer(lcd_, ts_, 88, 205, 300, 50,
+ LCD_COLOR_BLUE, LCD_COLOR_BLACK, "Commencer la partie", Font20);
+ commencer.Draw(LCD_COLOR_WHITE, LCD_COLOR_BLACK);
+
+ while(boutton_commencer == 0)
+ {
+ if(commencer.Touched())
+ {
+ boutton_commencer = 1;
+ }
+ }
+ boutton_commencer = 0;
+
+
+ //Ecran montrant la séquence à répéter pour gagner
+ BSP_LCD_Clear(LCD_COLOR_BLACK);
+ BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+ BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+ BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"Repeter la sequence", CENTER_MODE);
+ BSP_LCD_SetTextColor(LCD_COLOR_RED);
+ BSP_LCD_DisplayStringAt(0, 80, (uint8_t *)"une fois celle-ci", CENTER_MODE);
+ BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+ BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"terminee", CENTER_MODE);
+ HAL_Delay(4000);
+
+
+ //Initialisation de la séquence
+ while(sequence == 0){
+ srand(time(NULL));
+ nbAlea=rand()%4;
+ //nbAlea=rand()%3;
+
+ switch(nbAlea){
+ case 0:
+ couleur = LCD_COLOR_RED;
+ i = i + 1;
+ break;
+ case 1:
+ couleur = LCD_COLOR_GREEN;
+ i = i + 1;
+ break;
+ case 2:
+ couleur = LCD_COLOR_BLUE;
+ i = i + 1;
+ break;
+ case 3:
+ couleur = LCD_COLOR_WHITE;
+ i = i + 1;
+ break;
+ }
+
+ switch(i){
+ case 1:
+ couleur1 = couleur;
+ BSP_LCD_SetTextColor(couleur1);
+ BSP_LCD_FillRect(50, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 2:
+ couleur2 = couleur;
+ BSP_LCD_SetTextColor(couleur2);
+ BSP_LCD_FillRect(150, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 3:
+ couleur3 = couleur;
+ BSP_LCD_SetTextColor(couleur3);
+ BSP_LCD_FillRect(250, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 4:
+ couleur4 = couleur;
+ BSP_LCD_SetTextColor(couleur4);
+ BSP_LCD_FillRect(350, 170, 70, 70);
+ HAL_Delay(1000);
+ sequence = 1;
+ i = 0;
+ break;
+ }
+ wait_us(2000000);
+ }
+ sequence = 0;
+
+
+ //Ecran "tour de l'utilisateur"
+ BSP_LCD_Clear(LCD_COLOR_BLACK);
+ BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+ BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+ BSP_LCD_DisplayStringAt(0, 40, (uint8_t *)"A vous de jouer !", CENTER_MODE);
+ BSP_LCD_SetTextColor(LCD_COLOR_RED);
+ BSP_LCD_DisplayStringAt(0, 80, (uint8_t *)"Quelle etait la sequence ?", CENTER_MODE);
+ //BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+ //BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"Appuyer sur le bouton de couleur correspondant", CENTER_MODE);
+ HAL_Delay(4000);
+
+
+ //create some variables to store the color data in
+ uint16_t r, g, b, c;
+
+ //wait for color data to be ready
+ while(!sensor->colorDataReady()){
+ wait_us(5000);
+ }
+
+ while(sequence_reponse == 0){
+ //get the data and print the different channels
+ sensor->getColorData(&r, &g, &b, &c);
+ pc.printf("red: ");
+ pc.printf("%u ", r);
+
+ pc.printf(" green: ");
+ pc.printf("%u ", g);
+
+ pc.printf(" blue: ");
+ pc.printf("%u ", b);
+
+ pc.printf(" clear: ");
+ pc.printf("%u ", c);
+ pc.printf("\r\n");
+
+ if(r>400 && g>400 && b>400)
+ {
+ pc.printf("Test 0: blanc\r\n");
+ reponse = LCD_COLOR_WHITE;
+ j = j + 1;
+ }
+ else if(r>g && r>b && r>100)
+ {
+ pc.printf("Test 1: rouge\r\n");
+ reponse = LCD_COLOR_RED;
+ j = j + 1;
+ }
+ else if(b>r && b>g && b>100)
+ {
+ pc.printf("Test 2: bleu\r\n");
+ reponse = LCD_COLOR_BLUE;
+ j = j + 1;
+ }
+ else if(g>r && g>b && g>100)
+ {
+ pc.printf("Test 3: vert\r\n");
+ reponse = LCD_COLOR_GREEN;
+ j = j + 1;
+ }
+
+ switch(j){
+ case 1:
+ reponse1 = reponse;
+ BSP_LCD_SetTextColor(reponse1);
+ BSP_LCD_FillRect(50, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 2:
+ reponse2 = reponse;
+ BSP_LCD_SetTextColor(reponse2);
+ BSP_LCD_FillRect(150, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 3:
+ reponse3 = reponse;
+ BSP_LCD_SetTextColor(reponse3);
+ BSP_LCD_FillRect(250, 170, 70, 70);
+ HAL_Delay(1000);
+ break;
+ case 4:
+ reponse4 = reponse;
+ BSP_LCD_SetTextColor(reponse4);
+ BSP_LCD_FillRect(350, 170, 70, 70);
+ HAL_Delay(1000);
+ sequence_reponse = 1;
+ j = 0;
+ break;
+ }
+ wait_us(1000000);
+ }
+ sequence_reponse = 0;
+
+ if(couleur1==reponse1 && couleur2==reponse2 && couleur3==reponse3 && couleur4==reponse4)
+ {
+ BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+ BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"VOUS AVEZ GAGNE", CENTER_MODE);
+ }
+ else
+ {
+ BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
+ BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"VOUS AVEZ PERDU", CENTER_MODE);
+ }
+
+
+ //Bouton rejouer la partie
+ Button reset(lcd_, ts_, 410, 5, 60, 40,
+ LCD_COLOR_BLUE, LCD_COLOR_BLACK, "Rejouer", Font12);
+ reset.Draw(LCD_COLOR_WHITE, LCD_COLOR_BLACK);
+
+ while(boutton_reset == 0)
+ {
+ if(reset.Touched())
+ {
+ boutton_reset = 1;
+ }
+ }
+ boutton_reset = 0;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Mon Jun 22 14:16:28 2020 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#699372421a3b388fe568e9be85b1a985749a438f