Update to work with Grove

Dependencies:   DigitDisplay RangeFinder Pulse Grove_temperature FXOS8700Q

Files at this revision

API Documentation at this revision

Comitter:
fitzpatrick
Date:
Wed Feb 10 22:58:39 2021 +0000
Parent:
0:21d86aae6b2a
Commit message:
Update to work with Grove

Changed in this revision

DigitDisplay.lib Show annotated file Show diff for this revision Revisions of this file
FXOS8700Q.lib Show annotated file Show diff for this revision Revisions of this file
LED_Bar.cpp Show annotated file Show diff for this revision Revisions of this file
LED_Bar.h Show annotated file Show diff for this revision Revisions of this file
Pulse.lib Show annotated file Show diff for this revision Revisions of this file
RangeFinder.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DigitDisplay.lib	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/seeed/code/DigitDisplay/#d3173c8bfd48
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700Q.lib	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/NXP/code/FXOS8700Q/#aee7dea904e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED_Bar.cpp	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,93 @@
+/*
+LED bar library V1.0
+2010 Copyright (c) Seeed Technology Inc. All right reserved.
+
+Original Author: LG
+
+Modify: Mihail Stoyanov (mihail.stoyanov@arm.com) for ARM mbed, 2014-07-30
+User can choose which Io to be used.
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "LED_Bar.h"
+
+LED_Bar::LED_Bar(PinName pinClk, PinName pinDta) : __pinClk(pinClk), __pinDta(pinDta)
+{
+    __led_state = 0x00;
+}
+
+void LED_Bar::latchData()
+{
+    __pinDta = 0;
+    wait_us(10);
+
+    for(int i=0; i<4; i++) {
+        __pinDta = 1;
+        __pinDta = 0;
+    }
+
+}
+
+void LED_Bar::send16bitData(unsigned int data)
+{
+    for(int i=0; i<16; i++) {
+        unsigned int state = data & 0x8000 ? 1 : 0;
+        __pinDta = state;
+
+        state = __pinClk ? 0 : 1;
+        __pinClk = state;
+
+        data <<= 1;
+    }
+}
+
+void LED_Bar::ledIndexBit(unsigned int index_bits)
+{
+
+    send16bitData(CMDMODE);
+
+    for (int i=0; i<12; i++) {
+        unsigned int state = (index_bits&0x0001) ? ON : SHUT;
+        send16bitData(state);
+
+        index_bits = index_bits>>1;
+    }
+
+    latchData();
+}
+
+void LED_Bar::setLevel(int level)
+{
+
+    if(level>10)return;
+
+    send16bitData(CMDMODE);
+
+    for(int i=0; i<12; i++) {
+        unsigned int state1 = (i<level) ? ON : SHUT;
+
+        send16bitData(state1);
+    }
+
+    latchData();
+}
+
+void LED_Bar::setSingleLed(int num, int st)
+{
+    if(num>10)return;
+    __led_state = st ? (__led_state | (0x01<<num)) : (__led_state & ~(0x01<<num));
+    ledIndexBit(__led_state);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED_Bar.h	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,82 @@
+/*
+LED bar library V1.0
+2010 Copyright (c) Seeed Technology Inc. All right reserved.
+
+Original Author: LG
+Modify: Loovee, 2014-2-26
+User can choose which Io to be used.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "mbed.h"
+
+#ifndef LED_Bar_H
+#define LED_Bar_H
+
+#define CMDMODE 0x0000 // Work on 8-bit mode
+#define ON 0x00ff // 8-bit 1 data
+#define SHUT 0x0000 // 8-bit 0 data
+
+/**
+ * The LED_Bar interface
+ */
+class LED_Bar
+{
+
+public:
+    LED_Bar(PinName pinClk, PinName pinDta);
+
+    /**
+     * Set led single bit, a bit contrl a led
+     * @param index_bits which bit. if 0x05, then led 0 and led 3 will on, the others will off
+     */
+    void ledIndexBit(unsigned int index_bits);
+
+    /**
+     * Set level, frm 0 to 10.
+     * @param level Level 0 means all leds off while level 5 means 5led on and the other will off
+     */
+    void setLevel(int level);
+    
+    /**
+     * Control a single led
+     * @param num which led
+     * @param st  1: on 0: off
+     */
+    void setSingleLed(int num, int st);
+
+private:
+    /**
+     * Pin for clock
+     */
+    DigitalOut __pinClk;
+    
+    /**
+     * Pin for data
+     */
+    DigitalOut __pinDta;
+
+    /**
+     * LED State
+     */
+    unsigned int __led_state;
+
+    void send16bitData(unsigned int data);
+    void latchData(void);
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pulse.lib	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/NickRyder/code/Pulse/#fb79a4637a64
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RangeFinder.lib	Wed Feb 10 22:58:39 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/fitzpatrick/code/RangeFinder/#09ee44cee711
--- a/main.cpp	Tue Aug 07 04:36:25 2018 +0000
+++ b/main.cpp	Wed Feb 10 22:58:39 2021 +0000
@@ -1,15 +1,166 @@
 #include "mbed.h"
 #include "Grove_temperature.h"
+#include "LED_Bar.h"
+#include "DigitDisplay.h"
+#include "RangeFinder.h"
+#include "FXOS8700Q.h"
 
-Grove_temperature temp(A4);
-Serial pc(USBTX, USBRX);
+#define V_SERVO_CENTER 1400
+#define V_SERVO_MAX 2200
+#define V_SERVO_MIN 800
+
+Serial pc(USBTX, USBRX); // Serial Port 115200
+
+I2C i2c( PTE25, PTE24 );
+FXOS8700QAccelerometer  acc( i2c, FXOS8700CQ_SLAVE_ADDR1 ); // Accelerometer
+
+Grove_temperature temp(A3); // Temperature GPIO
+DigitalOut myled(LED2);  //Red LED
+DigitDisplay display(D7, D8); // Digital Display GPIO
+
+AnalogIn xAxis(A0);  // Joystick x GPIO
+AnalogIn yAxis(A1);  // Joystick y GPIO
+
+// Servo
+PwmOut v_servo(D3); // D3 output
+
+LED_Bar bar(D6, D5); // LED_Bar GPIO
+
+int x,y,button;  // global variables to hold values
+
+// Seeed ultrasound range finder
+RangeFinder rf(A2, 10, 5800.0, 100000);
+DigitalOut led(LED1);
+
+
+Ticker joystick; // recurring interrupt to get joystick data
+Ticker tick; // Digital display clock
+
+// Digital Display variables
+uint8_t hour   = 20;
+uint8_t minute = 14;
+uint8_t second = 0;
+
+// Digital Display Algorythm Function
+void beat()
+{
+    static uint8_t colon = 0;
+    
+    display.setColon(colon);
+    if (colon) {
+        second++;
+        if (second >= 60) {
+            second = 0;
+            minute++;
+            if (minute >= 60) {
+                minute = 0;
+                
+                hour++;
+                if (hour >= 24) {
+                    hour = 0;
+                }
+                display.write(0, hour / 10);
+                display.write(1, hour % 10);
+            }
+            display.write(2, minute / 10);
+            display.write(3, minute % 10);
+        }
+    }
+    colon = 1 - colon;
+}
+
+// Joystick Algorythm Function
+void joystick_Int_Handler()
+{
+    x = xAxis.read() * 1000; // float (0->1) to int (0-1000)
+    y = yAxis.read() * 1000;
+    if ( (x > 900) || (y > 900) )
+        button = 1;
+    else
+        button = 0;
+}
 
 // main() runs in its own thread in the OS
 int main() {
-    pc.baud(115200);
+    int i;
+    pc.baud(115200); // Serial Port 115200
+    
+    led = 1; //Rangefinder
+    float d; //Rangefinder
+    
+    float   acc_x, acc_y, acc_z; //Accelerometer
+    
+    // Servo
+    int v_pulse = V_SERVO_CENTER;
+    v_servo.period_us(20000);          // servo requires a 20ms period
+    v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
+    wait(1);
+    
+    // Joystick interrupt, call every .2s
+    joystick.attach(joystick_Int_Handler,0.2);
+    display.write(0, hour / 10);
+    display.write(1, hour % 10);
+    display.write(2, minute / 10);
+    display.write(3, minute % 10);
+    display.setColon(true);
+    
+    // Dig Display interrupt, call every .5s  
+    tick.attach(&beat, 0.5);
+    
+    // Enable Accelerometer
+    acc.enable();
+    
     while (true) {
-        pc.printf("temperature = %2.2f\n", temp.getTemperature());
-        wait(1);
+        printf("\r\nTemperature Measurement\r\n");
+        pc.printf("\rtemperature = %2.2f\n", temp.getTemperature());
+        
+        printf("\r\nJoystick Status, Meters\r\n");
+        pc.printf("\rX=%3d, Y=%3d, Button=%d\n",x,y,button);
+        
+        //for (i=0; i<=10; i++) {
+        //bar.setLevel(i);
+        //wait(0.1);
+        //}
+        bar.setLevel((x-250)/50);
+        
+        
+        //Servo
+        v_pulse =  v_pulse - 20;
+        if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
+        if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
+        v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
+        //wait(3);       
+        printf("\n\rPulse = %d\n\r",v_pulse); 
+
+        
+        //Range sensor
+        d = rf.read_m();
+        if (d == -1.0)  {
+            printf("\rTimeout Error.\n");   
+        } else if (d > 5.0) {  
+            // Seeed's sensor has a maximum range of 4m, it returns
+            // something like 7m if the ultrasound pulse isn't reflected. 
+            printf("\rNo object within detection range.\n");
+        } else  {
+            printf("\r\nUltra Sonic Rangefinder, Meters\r\n");
+            printf("\rDistance = %f m.\n", d);
+        }
+        
+        // get Accelerometer values
+        acc.getX( acc_x );
+        acc.getY( acc_y );
+        acc.getZ( acc_z );
+        
+        printf("\r\nAccelerometer Values\r\n");
+        printf("X:%6.1f, Y:%6.1f, Z:%6.1f\r\n\r\n", acc_x * 90.0, acc_y * 90.0, acc_z * 90.0 );
+        
+        myled = 1;
+        wait(0.5);
+        myled = 0;
+        wait(0.5);
+        
+        led = !led;
     }
 }
 
+