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) {
    }
Committer:
casper
Date:
Sat Apr 05 23:24:41 2014 +0000
Revision:
0:346c723cac97
First release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
casper 0:346c723cac97 1 /*
casper 0:346c723cac97 2 AS3935.h - AS3935 Franklin Lightning Sensor™ IC by AMS library
casper 0:346c723cac97 3 Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved.
casper 0:346c723cac97 4 Portée sur MBED par Valentin
casper 0:346c723cac97 5
casper 0:346c723cac97 6 This library is free software; you can redistribute it and/or
casper 0:346c723cac97 7 modify it under the terms of the GNU Lesser General Public
casper 0:346c723cac97 8 License as published by the Free Software Foundation; either
casper 0:346c723cac97 9 version 3 of the License, or (at your option) any later version.
casper 0:346c723cac97 10
casper 0:346c723cac97 11 This library is distributed in the hope that it will be useful,
casper 0:346c723cac97 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
casper 0:346c723cac97 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
casper 0:346c723cac97 14 Lesser General Public License for more details.
casper 0:346c723cac97 15
casper 0:346c723cac97 16 You should have received a copy of the GNU Lesser General Public
casper 0:346c723cac97 17 License along with this library; if not, write to the Free Software
casper 0:346c723cac97 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
casper 0:346c723cac97 19 */
casper 0:346c723cac97 20
casper 0:346c723cac97 21 #ifndef _AS3935_HPP
casper 0:346c723cac97 22 #define _AS3935_HPP
casper 0:346c723cac97 23
casper 0:346c723cac97 24 #include "mbed.h"
casper 0:346c723cac97 25 //#include "i2c.hpp"
casper 0:346c723cac97 26
casper 0:346c723cac97 27 // register access macros - register address, bitmask
casper 0:346c723cac97 28 #define AS3935_AFE_GB 0x00, 0x3E
casper 0:346c723cac97 29 #define AS3935_PWD 0x00, 0x01
casper 0:346c723cac97 30 #define AS3935_NF_LEV 0x01, 0x70
casper 0:346c723cac97 31 #define AS3935_WDTH 0x01, 0x0F
casper 0:346c723cac97 32 #define AS3935_CL_STAT 0x02, 0x40
casper 0:346c723cac97 33 #define AS3935_MIN_NUM_LIGH 0x02, 0x30
casper 0:346c723cac97 34 #define AS3935_SREJ 0x02, 0x0F
casper 0:346c723cac97 35 #define AS3935_LCO_FDIV 0x03, 0xC0
casper 0:346c723cac97 36 #define AS3935_MASK_DIST 0x03, 0x20
casper 0:346c723cac97 37 #define AS3935_INT 0x03, 0x0F
casper 0:346c723cac97 38 #define AS3935_DISTANCE 0x07, 0x3F
casper 0:346c723cac97 39 #define AS3935_DISP_LCO 0x08, 0x80
casper 0:346c723cac97 40 #define AS3935_DISP_SRCO 0x08, 0x40
casper 0:346c723cac97 41 #define AS3935_DISP_TRCO 0x08, 0x20
casper 0:346c723cac97 42 #define AS3935_TUN_CAP 0x08, 0x0F
casper 0:346c723cac97 43
casper 0:346c723cac97 44 // other constants
casper 0:346c723cac97 45 #define AS3935_AFE_INDOOR 0x12
casper 0:346c723cac97 46 #define AS3935_AFE_OUTDOOR 0x0E
casper 0:346c723cac97 47
casper 0:346c723cac97 48 class AS3935 {
casper 0:346c723cac97 49
casper 0:346c723cac97 50 public:
casper 0:346c723cac97 51 /*
casper 0:346c723cac97 52 * Initializes I2C interface and IRQ pin
casper 0:346c723cac97 53 */
casper 0:346c723cac97 54 AS3935(PinName sda, PinName scl, int adresse);
casper 0:346c723cac97 55
casper 0:346c723cac97 56 //destruction
casper 0:346c723cac97 57 //~AS3935();
casper 0:346c723cac97 58
casper 0:346c723cac97 59 //write to specified register specified data using specified bitmask,
casper 0:346c723cac97 60 //the rest of the register remains intact
casper 0:346c723cac97 61 void registerWrite(char reg, char mask, char data);
casper 0:346c723cac97 62
casper 0:346c723cac97 63 //read specified register using specified bitmask and return value aligned
casper 0:346c723cac97 64 //to lsb, i.e. if value to be read is in a middle of register, function
casper 0:346c723cac97 65 //reads register and then aligns lsb of value to lsb of byte
casper 0:346c723cac97 66 char registerRead(char reg, char mask);
casper 0:346c723cac97 67
casper 0:346c723cac97 68 //reset all the registers on chip to default values
casper 0:346c723cac97 69 void reset();
casper 0:346c723cac97 70
casper 0:346c723cac97 71 //put chip into power down mode
casper 0:346c723cac97 72 void powerDown();
casper 0:346c723cac97 73
casper 0:346c723cac97 74 //bring chip out of power down mode and perform RCO calibration
casper 0:346c723cac97 75 void powerUp();
casper 0:346c723cac97 76
casper 0:346c723cac97 77 //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber,
casper 0:346c723cac97 78 //0b1000 - lightning
casper 0:346c723cac97 79 int interruptSource();
casper 0:346c723cac97 80
casper 0:346c723cac97 81 //disable indication of disturbers
casper 0:346c723cac97 82 void disableDisturbers();
casper 0:346c723cac97 83
casper 0:346c723cac97 84 //enable indication of distrubers
casper 0:346c723cac97 85 void enableDisturbers();
casper 0:346c723cac97 86
casper 0:346c723cac97 87 //return number of lightnings that need to be detected in 17 minute period
casper 0:346c723cac97 88 //before interrupt is issued
casper 0:346c723cac97 89 int getMinimumLightnings();
casper 0:346c723cac97 90
casper 0:346c723cac97 91 //set number of lightnings that need to be detected in 17 minute period
casper 0:346c723cac97 92 //before interrupt is issued
casper 0:346c723cac97 93 int setMinimumLightnings(int minlightning);
casper 0:346c723cac97 94
casper 0:346c723cac97 95 //return distance to lightning in kilometers, 1 means storm is overhead,
casper 0:346c723cac97 96 //63 means it is too far to reliably calculate distance
casper 0:346c723cac97 97 int lightningDistanceKm();
casper 0:346c723cac97 98
casper 0:346c723cac97 99 // load gain preset to operate indoors
casper 0:346c723cac97 100 void setIndoors();
casper 0:346c723cac97 101
casper 0:346c723cac97 102 //load gain preset to operate outdoors
casper 0:346c723cac97 103 void setOutdoors();
casper 0:346c723cac97 104
casper 0:346c723cac97 105 //return noise floor setting - refer to datasheet for meaning and range
casper 0:346c723cac97 106 int getNoiseFloor();
casper 0:346c723cac97 107
casper 0:346c723cac97 108 //set noise floor setting
casper 0:346c723cac97 109 int setNoiseFloor(int noisefloor);
casper 0:346c723cac97 110
casper 0:346c723cac97 111 //return spike rejection value - refer to datasheet for meaning and range
casper 0:346c723cac97 112 int getSpikeRejection();
casper 0:346c723cac97 113
casper 0:346c723cac97 114 //set spike rejection value
casper 0:346c723cac97 115 int setSpikeRejection(int srej);
casper 0:346c723cac97 116
casper 0:346c723cac97 117 //return watchdog threshold value - refer to datasheet for meaning and range
casper 0:346c723cac97 118 int getWatchdogThreshold();
casper 0:346c723cac97 119
casper 0:346c723cac97 120 //set watchdog threshold value
casper 0:346c723cac97 121 int setWatchdogThreshold(int wdth);
casper 0:346c723cac97 122
casper 0:346c723cac97 123 //return tune Capacity value
casper 0:346c723cac97 124 int getTuneCap();
casper 0:346c723cac97 125
casper 0:346c723cac97 126 //set tune Capacity value
casper 0:346c723cac97 127 int setTuneCap(int cap);
casper 0:346c723cac97 128
casper 0:346c723cac97 129 //clear internal accumulated lightning statistics
casper 0:346c723cac97 130 void clearStats();
casper 0:346c723cac97 131
casper 0:346c723cac97 132 private:
casper 0:346c723cac97 133 I2C i2c;
casper 0:346c723cac97 134 //DigitalOut _irq;
casper 0:346c723cac97 135 int _adress;
casper 0:346c723cac97 136 char _rawRegisterRead(char reg);
casper 0:346c723cac97 137 char _ffsz(char mask);
casper 0:346c723cac97 138 };
casper 0:346c723cac97 139
casper 0:346c723cac97 140 /* !_AS3935_HPP_ */
casper 0:346c723cac97 141 #endif