Lightning detector AS3935 library from arduino ported to mbed. Hardware I2C

Dependents:   BLE_Lightning_sensor

/media/uploads/casper/mod-1016v4_600.jpg

include the mbed library with this snippet

#include "mbed.h"
#include "AS3935/AS3935.hpp"

AS3935 Lightning(p28,p27,0x06); //détecteur d'orages relié en I2C, adresse I2C 0x06;
InterruptIn IntLightning(p29); //IRQ AS3935

///////////////////////////////////////////////////////////////////
// Détection d'orages, Fonction d'interruption sur p29
//////////////////////////////////////////////////////////////////
void DetectLightning()
{
    char OriginInt;
    inter = !inter;
    wait_ms(2); //on attend 2ms préconisation constructeur
    OriginInt = Lightning.interruptSource();
    if (OriginInt == 1) { //le bruit environnant est trop élevé
        pc.printf(" Bruit trop élevé\r\n");
        }
    if (OriginInt == 4) { //détection perturbation électromagnétique
        pc.printf(" Perturbation électromagnétique\r\n");
        }
    if (OriginInt == 8) { //on a détecté un éclair
        pc.printf(" Détection d'un éclair\r\n");
        }
}

int main() {
    //initialisations
    Lightning.reset();
    Lightning.setTuneCap(5); //réglage capacité d'accord à 500kHz
    Lightning.powerUp();
    Lightning.setIndoors(); //réglage du gain d'amplification pour l'intérieur
    IntLightning.rise(&DetectLightning);
    while(1) {
    }

Files at this revision

API Documentation at this revision

Comitter:
casper
Date:
Sat Apr 05 23:24:41 2014 +0000
Commit message:
First release

Changed in this revision

AS3935.cpp Show annotated file Show diff for this revision Revisions of this file
AS3935.hpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 346c723cac97 AS3935.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AS3935.cpp	Sat Apr 05 23:24:41 2014 +0000
@@ -0,0 +1,208 @@
+/*
+AS3935.cpp - AS3935 Franklin Lightning Sensor™ IC by AMS library
+Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved.
+Porté sur MBED par Valentin, version I2C
+
+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 3 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 "AS3935.hpp"
+
+AS3935::AS3935(PinName sda, PinName scl, int adresse): i2c(sda, scl), _adress(adresse) {        
+        
+        wait_ms(11);
+    }
+
+    //~AS3935(){
+        
+    //}
+        
+char AS3935::_rawRegisterRead(char reg)
+{
+    char data;
+    i2c.write( _adress, &reg, 1, true);
+    i2c.read(_adress, &data, 1);
+    return data;
+    
+}
+
+char AS3935::_ffsz(char mask)
+{
+    char i = 0;
+    //char temp;
+    
+    while(!(mask & 1)) {
+       //if (!(mask & 1)) {
+          mask >>= 1;
+          i++;
+       //}
+    }
+    //if (mask){
+      //  for (i = 1; ~mask & 1 ; i++) {
+        //    mask >>= 1;
+          //  }
+       // }
+    return i;
+}
+
+void AS3935::registerWrite(char reg, char mask, char data)
+{
+    char cmd[2];
+    char regval;
+    regval = _rawRegisterRead(reg);
+    regval &= ~(mask);
+    //if (mask){
+        regval |= (data << (_ffsz(mask)));
+      //  }
+    //else {
+      //  regval |= data;
+       // }
+    cmd[0] = reg;
+    cmd[1] = regval;
+    i2c.write( _adress, cmd, 2);
+
+}
+
+char AS3935::registerRead(char reg, char mask)
+    {
+    char regval;
+    regval = _rawRegisterRead(reg);
+    regval = regval & mask;
+    //if (mask){
+        regval >>= (_ffsz(mask));
+      //  }
+    return regval;
+}
+
+void AS3935::reset()
+    {
+    char cmd[2];
+    cmd[0] = 0x3C;
+    cmd[1] = 0x96;
+    i2c.write( _adress, cmd, 2);
+    wait_ms(2);
+    }
+
+
+
+void AS3935::powerDown()
+    {
+    registerWrite(AS3935_PWD,1);
+    }
+
+void AS3935::powerUp()
+    {
+    char cmd[2];
+    cmd[0] = 0x3D;
+    cmd[1] = 0x96;
+    registerWrite(AS3935_PWD,0);
+    i2c.write( _adress, cmd, 2);
+    wait_ms(3);
+    registerWrite(AS3935_DISP_TRCO,1);
+    wait_ms(2);
+    registerWrite(AS3935_DISP_TRCO,0);
+    }
+
+int AS3935::interruptSource()
+    {
+    return registerRead(AS3935_INT);
+    }
+
+void AS3935::disableDisturbers()
+    {
+    registerWrite(AS3935_MASK_DIST,1);
+    }
+
+void AS3935::enableDisturbers()
+    {
+    registerWrite(AS3935_MASK_DIST,0);
+    }
+
+int AS3935::getMinimumLightnings()
+    {
+    return registerRead(AS3935_MIN_NUM_LIGH);
+    }
+
+int AS3935::setMinimumLightnings(int minlightning)
+    {
+    registerWrite(AS3935_MIN_NUM_LIGH,minlightning);
+    return getMinimumLightnings();
+    }
+
+int AS3935::lightningDistanceKm()
+    {
+    return registerRead(AS3935_DISTANCE);
+    }
+
+void AS3935::setIndoors()
+    {
+    registerWrite(AS3935_AFE_GB,AS3935_AFE_INDOOR);
+    }
+
+void AS3935::setOutdoors()
+    {
+    registerWrite(AS3935_AFE_GB,AS3935_AFE_OUTDOOR);
+    }
+
+int AS3935::getNoiseFloor()
+    {
+    return registerRead(AS3935_NF_LEV);
+    }
+
+int AS3935::setNoiseFloor(int noisefloor)
+    {
+    registerWrite(AS3935_NF_LEV,noisefloor);
+    return getNoiseFloor();
+    }
+
+int AS3935::getSpikeRejection()
+    {
+    return registerRead(AS3935_SREJ);
+    }
+
+int AS3935::setSpikeRejection(int srej)
+    {
+    registerWrite(AS3935_SREJ, srej);
+    return getSpikeRejection();
+    }
+
+int AS3935::getWatchdogThreshold()
+    {
+    return registerRead(AS3935_WDTH);
+    }
+
+int AS3935::setWatchdogThreshold(int wdth)
+    {
+    registerWrite(AS3935_WDTH,wdth);
+    return getWatchdogThreshold();
+    }
+
+int AS3935::getTuneCap()
+    {
+    return registerRead(AS3935_TUN_CAP);    
+    }
+        
+int AS3935::setTuneCap(int cap)
+    {
+    registerWrite(AS3935_TUN_CAP,cap);
+    return getTuneCap();    
+    }
+
+void AS3935::clearStats()
+    {
+    registerWrite(AS3935_CL_STAT,1);
+    registerWrite(AS3935_CL_STAT,0);
+    registerWrite(AS3935_CL_STAT,1);
+    }
\ No newline at end of file
diff -r 000000000000 -r 346c723cac97 AS3935.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AS3935.hpp	Sat Apr 05 23:24:41 2014 +0000
@@ -0,0 +1,141 @@
+/*
+AS3935.h - AS3935 Franklin Lightning Sensor™ IC by AMS library
+Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved.
+Portée sur MBED par Valentin
+
+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 3 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
+*/
+
+#ifndef _AS3935_HPP
+#define _AS3935_HPP
+
+#include "mbed.h"
+//#include "i2c.hpp"
+
+// register access macros - register address, bitmask
+#define AS3935_AFE_GB 0x00, 0x3E
+#define AS3935_PWD 0x00, 0x01
+#define AS3935_NF_LEV 0x01, 0x70
+#define AS3935_WDTH 0x01, 0x0F
+#define AS3935_CL_STAT 0x02, 0x40
+#define AS3935_MIN_NUM_LIGH 0x02, 0x30
+#define AS3935_SREJ 0x02, 0x0F
+#define AS3935_LCO_FDIV 0x03, 0xC0
+#define AS3935_MASK_DIST 0x03, 0x20
+#define AS3935_INT 0x03, 0x0F
+#define AS3935_DISTANCE 0x07, 0x3F
+#define AS3935_DISP_LCO 0x08, 0x80
+#define AS3935_DISP_SRCO 0x08, 0x40
+#define AS3935_DISP_TRCO 0x08, 0x20
+#define AS3935_TUN_CAP 0x08, 0x0F
+
+// other constants
+#define AS3935_AFE_INDOOR 0x12
+#define AS3935_AFE_OUTDOOR 0x0E
+
+class AS3935 {
+    
+    public:
+        /*
+         * Initializes I2C interface and IRQ pin
+         */
+        AS3935(PinName sda, PinName scl, int adresse);
+        
+        //destruction         
+        //~AS3935();   
+        
+        //write to specified register specified data using specified bitmask,     
+        //the rest of the register remains intact
+        void registerWrite(char reg, char mask, char data);
+        
+        //read specified register using specified bitmask and return value aligned     
+        //to lsb, i.e. if value to be read is in a middle of register, function     
+        //reads register and then aligns lsb of value to lsb of byte
+        char registerRead(char reg, char mask);
+        
+        //reset all the registers on chip to default values
+        void reset();
+        
+        //put chip into power down mode
+        void powerDown();
+        
+        //bring chip out of power down mode and perform RCO calibration
+        void powerUp();
+        
+        //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber,     
+        //0b1000 - lightning
+        int interruptSource();
+        
+        //disable indication of disturbers
+        void disableDisturbers();
+        
+        //enable indication of distrubers
+        void enableDisturbers();
+        
+        //return number of lightnings that need to be detected in 17 minute period     
+        //before interrupt is issued
+        int getMinimumLightnings();
+        
+        //set number of lightnings that need to be detected in 17 minute period     
+        //before interrupt is issued
+        int setMinimumLightnings(int minlightning);
+        
+        //return distance to lightning in kilometers, 1 means storm is overhead,     
+        //63 means it is too far to reliably calculate distance
+        int lightningDistanceKm();
+        
+        // load gain preset to operate indoors
+        void setIndoors();
+        
+        //load gain preset to operate outdoors
+        void setOutdoors();
+        
+        //return noise floor setting - refer to datasheet for meaning and range
+        int getNoiseFloor();
+        
+        //set noise floor setting
+        int setNoiseFloor(int noisefloor);
+        
+        //return spike rejection value - refer to datasheet for meaning and range
+        int getSpikeRejection();
+        
+        //set spike rejection value
+        int setSpikeRejection(int srej);
+        
+        //return watchdog threshold value - refer to datasheet for meaning and range
+        int getWatchdogThreshold();
+        
+        //set watchdog threshold value
+        int setWatchdogThreshold(int wdth);
+        
+        //return tune Capacity value 
+        int getTuneCap();
+        
+        //set tune Capacity value
+        int setTuneCap(int cap);
+        
+        //clear internal accumulated lightning statistics
+        void clearStats();
+    
+    private:
+        I2C i2c;   
+        //DigitalOut _irq;
+        int _adress;
+        char _rawRegisterRead(char reg);
+        char _ffsz(char mask);
+};
+
+/* !_AS3935_HPP_ */
+#endif
\ No newline at end of file