Starting AS3935

This commit is contained in:
Edward Emelianov 2024-08-20 22:11:44 +03:00
parent 5d5e7c4a46
commit f9f7a55eba
14 changed files with 729 additions and 4 deletions

57
LightningSensor/Makefile Normal file
View File

@ -0,0 +1,57 @@
# run `make DEF=...` to add extra defines
PROGRAM := lightning
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
TARGFILE := $(OBJDIR)/TARGET
CC = gcc
#TARGET := RELEASE
ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes)
TARGET := $(file < $(TARGFILE))
else
TARGET := RELEASE
endif
ifeq ($(TARGET), DEBUG)
.DEFAULT_GOAL := debug
endif
release: $(PROGRAM)
debug: CFLAGS += -DEBUG -Werror
debug: TARGET := DEBUG
debug: $(PROGRAM)
$(TARGFILE): $(OBJDIR)
@echo -e "\t\tTARGET: $(TARGET)"
@echo "$(TARGET)" > $(TARGFILE)
$(PROGRAM) : $(TARGFILE) $(OBJS)
@echo -e "\t\tLD $(PROGRAM)"
$(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
$(OBJDIR):
@mkdir $(OBJDIR)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR)/%.o: %.c
@echo -e "\t\tCC $<"
$(CC) $< -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@
clean:
@echo -e "\t\tCLEAN"
@rm -rf $(OBJDIR) 2>/dev/null || true
xclean: clean
@rm -f $(PROGRAM)
.PHONY: clean xclean

178
LightningSensor/as3935.c Normal file
View File

@ -0,0 +1,178 @@
/*
* This file is part of the lightning project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "as3935.h"
#include "i2c.h"
static int dev_fd = -1;
// open device and set slave address
int as3935_open(const char *path, uint8_t id){
dev_fd = i2c_open(path);
if(dev_fd < 0) return FALSE;
if(!i2c_set_slave_address(dev_fd, id)){
WARNX("Can't set slave address 0x%02x", G.slaveaddr);
return FALSE;
}
return as3935_wakeup();
}
// common getter
int as3935_getter(uint8_t reg, uint8_t *data){
return i2c_read_reg(dev_fd, reg, data);
}
// and common setter
int as3935_setter(uint8_t reg, uint8_t data){
return i2c_write_reg(dev_fd, reg, data);
}
// starting calibration
int as3935_calib_rco(){
t_tun_disp t;
if(!i2c_read_reg(dev_fd, TUN_DISP, &t)) return FALSE;
if(!i2c_write_reg(dev_fd, CALIB_RCO, DIRECT_COMMAND)) return FALSE;
t.DISP_SRCO = 1;
if(!i2c_write_reg(dev_fd, TUN_DISP, &t)) return FALSE;
double t0 = dtime();
while(dtime() - t0 < 0.002);
t.DISP_SRCO = 0;
if(!i2c_write_reg(dev_fd, TUN_DISP, &t)) return FALSE;
t_calib srco, trco;
if(!i2c_read_reg(dev_fd, CALIB_TRCO, &trco)) return FALSE;
if(!i2c_read_reg(dev_fd, CALIB_SRCO, &srco)) return FALSE;
if(!srco.CALIB_DONE || !trco.CALIB_DONE) return FALSE;
return TRUE;
}
// wakeup - call this function after first run
int as3935_wakeup(){
t_afe_gain g;
if(!i2c_read_reg(dev_fd, AFE_GAIN, &g)) return FALSE;
g.PWD = 0;
if(!i2c_write_reg(dev_fd, AFE_GAIN, g)) return FALSE;
return as3935_calib_rco;
}
// set amplifier gain
int as3935_set_gain(uint8_t g){
if(g > 0x1f) return FALSE;
t_afe_gain a = {0};
a.AFE_GB = g;
return i2c_write_reg(dev_fd, AFE_GAIN, a);
}
// watchdog threshold
int as3935_wdthres(uint8_t t){
if(t > 0x0f) return FALSE;
t_threshold thres;
if(!i2c_read_reg(dev_fd, THRESHOLD, &thres)) return FALSE;
thres.WDTH = t;
return i2c_write_reg(dev_fd, THRESHOLD, thres);
}
// noice floor level
int as3935_nflev(uint8_t l){
if(l > 7) return FALSE;
t_threshold thres;
if(!i2c_read_reg(dev_fd, THRESHOLD, &thres)) return FALSE;
thres.NF_LEV = l;
return i2c_write_reg(dev_fd, THRESHOLD, thres);
}
// spike rejection
int as3935_srej(uint8_t s){
if(s > 0xf) return FALSE;
t_lightning_reg lr;
if(!i2c_read_reg(dev_fd, LIGHTNING_REG, &lr)) return FALSE;
lr.SREJ = s;
return i2c_write_reg(dev_fd, LIGHTNING_REG, lr);
}
// minimal lighting number
int as3935_minnumlig(uint8_t n){
if(n > 3) return FALSE;
t_lightning_reg lr;
if(!i2c_read_reg(dev_fd, LIGHTNING_REG, &lr)) return FALSE;
lr.MIN_NUM_LIG = n;
return i2c_write_reg(dev_fd, LIGHTNING_REG, lr);
}
// clear amount of lightnings for last 15 min
int as3935_clearstat(){
if(n > 3) return FALSE;
t_lightning_reg lr;
if(!i2c_read_reg(dev_fd, LIGHTNING_REG, &lr)) return FALSE;
lr.CL_STAT = 1;
return i2c_write_reg(dev_fd, LIGHTNING_REG, lr);
}
// get interrupt code
int as3935_intcode(uint8_t *code){
if(!code) return FALSE;
t_int_mask_ant i;
if(!i2c_read_reg(dev_fd, INT_MASK_ANT, &i)) return FALSE;
*code = i.INT;
return TRUE;
}
// should interrupt react on disturbers?
int as3935_mask_disturber(uint8_t m){
if(m > 1) return FALSE;
t_int_mask_ant i;
if(!i2c_read_reg(dev_fd, INT_MASK_ANT, &i)) return FALSE;
i.MASK_DIST = m;
return i2c_write_reg(dev_fd, INT_MASK_ANT, i);
}
// set Fdiv of antenna LCO
int as3935_lco_fdiv(uint8_t d){
if(d > 3) return FALSE;
t_int_mask_ant i;
if(!i2c_read_reg(dev_fd, INT_MASK_ANT, &i)) return FALSE;
i.LCO_FDIV = d;
return i2c_write_reg(dev_fd, INT_MASK_ANT, i);
}
// calculate last lightning energy
int as3935_energy(uint32_t *E){
if(!E) return FALSE;
uint8_t u8; uint32_t energy;
t_s_lig_mm mm;
if(!i2c_read_reg(dev_fd, S_LIG_MM, &mm)) return FALSE;
energy = mm.S_LIG_MM << 8;
if(!i2c_read_reg(dev_fd, S_LIG_M, &u8)) return FALSE;
energy |= u8;
energy <<= 8;
if(!i2c_read_reg(dev_fd, S_LIG_L, &u8)) return FALSE;
energy |= u8;
*E = energy;
return TRUE;
}
int as3935_distance(uint8_t *d){
if(!d) return FALSE;
t_distance dist;
if(!i2c_read_reg(dev_fd, DISTANCE, &dist)) return FALSE;
*d = dist.DISTANCE;
return TRUE;
}

123
LightningSensor/as3935.h Normal file
View File

@ -0,0 +1,123 @@
/*
* This file is part of the lightning project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
enum AS3935_REGISTERS{
AFE_GAIN = 0x00,
THRESHOLD,
LIGHTNING_REG,
INT_MASK_ANT,
S_LIG_L,
S_LIG_M,
S_LIG_MM,
DISTANCE,
TUN_DISP,
CALIB_TRCO = 0x3A,
CALIB_SRCO = 0x3B,
PRESET_DEFAULT = 0x3C,
CALIB_RCO = 0x3D
};
// REGISTERS
typedef struct{
uint8_t PWD : 1;
uint8_t AFE_GB : 5;
uint8_t RESERVED : 2;
} t_afe_gain;
typedef struct{
uint8_t WDTH : 4;
uint8_t NF_LEV : 3;
uint8_t RESERVED : 1;
} t_threshold;
typedef struct{
uint8_t SREJ : 4;
// minimal number of lightnings
#define NUM_LIG_1 (0)
#define NUM_LIG_5 (1)
#define NUM_LIG_9 (2)
#define NUM_LIG_16 (3)
uint8_t MIN_NUM_LIG : 2;
uint8_t CL_STAT : 1;
uint8_t RESERVED : 1;
} t_lightning_reg;
typedef struct{
// interrupt flags
// noice level too high
#define INT_NH (1)
// disturber detected
#define INT_D (4)
// lightning interrupt
#define INT_L (8)
uint8_t INT : 4;
uint8_t RESERVED : 1;
uint8_t MASK_DIST : 1;
uint8_t LCO_FDIV : 2;
} t_int_mask_ant;
typedef struct{
uint8_t S_LIG_MM : 5;
uint8_t RESERVED : 3;
} t_s_lig_mm;
typedef struct{
uint8_t DISTANCE : 6;
uint8_t RESERVED : 2;
} t_distance;
typedef struct{
uint8_t TUN_CAP : 4;
uint8_t RESERVED : 1;
uint8_t DISP_TRCO : 1;
uint8_t DISP_SRCO : 1;
uint8_t DISP_LCO : 1;
} t_tun_disp;
typedef struct{
uint8_t RESERVED : 6;
uint8_t CALIB_NOK : 1;
uint8_t CALIB_DONE : 1;
} t_calib;
// direct command send to PRESET_DEFAULT and CALIB_RCO
#define DIRECT_COMMAND (0x96)
// distance out of range
#define DIST_OUT_OF_RANGE (0x3f)
int as3935_open(const char *path, uint8_t id);
int as3935_getter(uint8_t reg, uint8_t *data);
int as3935_setter(uint8_t reg, uint8_t data);
int as3935_wakeup();
int as3935_calib_rco();
int as3935_set_gain(uint8_t g);
int as3935_wdthres(uint8_t t);
int as3935_nflev(uint8_t l);
int as3935_srej(uint8_t s);
int as3935_minnumlig(uint8_t n);
int as3935_clearstat();
int as3935_intcode(uint8_t *code);
int as3935_mask_disturber(uint8_t m);
int as3935_lco_fdiv(uint8_t d);
int as3935_energy(uint32_t *E);
int as3935_distance(uint8_t *d);

78
LightningSensor/i2c.c Normal file
View File

@ -0,0 +1,78 @@
/*
* This file is part of the lightning project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <usefull_macros.h>
uint8_t slaveaddr = 0;
int i2c_read_reg(int fd, uint8_t regaddr, uint8_t *data){
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data sd;
args.read_write = I2C_SMBUS_READ;
args.command = regaddr;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &sd;
if(ioctl(fd, I2C_SMBUS, &args) < 0){
WARNX("Can't read reg %d", regaddr);
LOGWARN("Can't read reg %d", regaddr);
return FALSE;
}
*data = sd.byte;
return TRUE;
}
int i2c_write_reg(int fd, uint8_t regaddr, uint8_t data){
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data sd;
sd.byte = data;
args.read_write = I2C_SMBUS_WRITE;
args.command = regaddr;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &sd;
if(ioctl(fd, I2C_SMBUS, &args) < 0){
WARNX("Can't write reg %d", regaddr);
LOGWARN("Can't write reg %d", regaddr);
return FALSE;
}
return TRUE;
}
int i2c_set_slave_address(int fd, uint8_t addr){
if(addr == slaveaddr) return TRUE;
if(ioctl (fd, I2C_SLAVE, addr) < 0){
WARNX("Can't set slave address %d", addr);
LOGWARN("Can't set slave address %d", addr);
return FALSE;
}
slaveaddr = addr;
return TRUE;
}
int i2c_open(const char *path){
return open(path, O_RDWR);
}

33
LightningSensor/i2c.h Normal file
View File

@ -0,0 +1,33 @@
/*
* This file is part of the lightning project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
int i2c_read_reg(int fd, uint8_t regaddr, uint8_t *data);
int i2c_write_reg(int fd, uint8_t regaddr, uint8_t data);
int i2c_set_slave_address(int fd, uint8_t addr);
int i2c_open(const char *path);

View File

@ -0,0 +1 @@
-std=c17

View File

@ -0,0 +1,2 @@
// Add predefined macros for your project here. For example:
// #define THE_ANSWER 42

View File

@ -0,0 +1 @@
[General]

View File

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 14.0.1, 2024-08-20T22:11:21. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{7bd84e39-ca37-46d3-be9d-99ebea85bc0d}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="qlonglong">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="qlonglong" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="int" key="EditorConfiguration.PreferAfterWhitespaceComments">0</value>
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">false</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
<value type="bool" key="EditorConfiguration.tintMarginArea">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<value type="bool" key="AutoTest.ApplyFilter">false</value>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<valuelist type="QVariantList" key="AutoTest.PathFilters"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">2</value>
<value type="bool" key="ClangTools.PreferConfigFile">false</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{65a14f9e-e008-4c1b-89df-4eaa4774b6e3}</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/tmp/1/home/eddy/I2C</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">all</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">clean</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="qlonglong">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">22</value>
</data>
<data>
<variable>Version</variable>
<value type="int">22</value>
</data>
</qtcreator>

View File

@ -0,0 +1 @@
-std=c++17

View File

@ -0,0 +1,6 @@
i2c.c
i2c.h
as3935.c
as3935.h
main.c

View File

61
LightningSensor/main.c Normal file
View File

@ -0,0 +1,61 @@
/*
* This file is part of the lightning project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "as3935.h"
#include "i2c.h"
#include <stdio.h>
#include <usefull_macros.h>
typedef struct{
char *device;
int slaveaddr;
int verbose;
int help;
char *logfile;
} glob_pars;
static glob_pars G = {.device = "/dev/i2c-3", .slaveaddr = 0};
static myoption cmdlnopts[] = {
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), "I2C device path"},
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"},
{"slave", NEED_ARG, NULL, 'a', arg_int, APTR(&G.slaveaddr), "I2C slave address"},
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), "Verbose (each -v increase)"},
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), "file for logging"},
end_option
};
int main(int argc, char **argv){
initial_setup();
parseargs(&argc, &argv, cmdlnopts);
if(G.help) showhelp(-1, cmdlnopts);
if(G.slaveaddr < 0 || G.slaveaddr > 0x7f) ERRX("I2C address should be 7-bit");
if(!as3935_open(G.device, G.slaveaddr)) ERR("Can't open %s", G.device);
if(G.logfile){
sl_loglevel l = LOGLEVEL_ERR;
for(int i = 0; i < G.verbose; ++i){
if(++l == LOGLEVEL_ANY) break;
}
OPENLOG(G.logfile, l, 1);
if(!globlog) ERRX("Can't open logfile %s", G.logfile);
}
LOGMSG("Connected to slave 0x%02x\n", G.slaveaddr);
;
return 0;
}

View File

@ -2,12 +2,12 @@
CLIENT := sslclient
SERVER := sslserver
LDFLAGS += -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros -lssl -lcrypto
LDFLAGS += -lusefull_macros -lssl -lcrypto -lm
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
SOBJDIR := mkserver
COBJDIR := mkclient
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -pthread
COMMSRCS := sslsock.c daemon.c cmdlnopts.c main.c
COMMSRCS := sslsock.c daemon.c cmdlnopts.c main.c gpio.c
SSRC := server.c $(COMMSRCS)
CSRC := client.c $(COMMSRCS)
SOBJS := $(addprefix $(SOBJDIR)/, $(SSRC:%.c=%.o))
@ -43,13 +43,13 @@ $(TARGFILE):
$(CLIENT) : DEFINES += -DCLIENT
$(CLIENT) : $(COBJDIR) $(COBJS)
@echo -e "\tLD $(CLIENT)"
$(CC) $(LDFLAGS) $(COBJS) -o $(CLIENT)
$(CC) $(COBJS) $(LDFLAGS) -o $(CLIENT)
$(SERVER) : DEFINES += -DSERVER
$(SERVER) : $(SOBJDIR) $(SOBJS)
@echo -e "\tLD $(SERVER)"
$(CC) $(LDFLAGS) $(SOBJS) -o $(SERVER)
$(CC) $(SOBJS) $(LDFLAGS) -o $(SERVER)
$(SOBJDIR):
@mkdir $(SOBJDIR)