Skip to content

Commit e3b9dbd

Browse files
Version 1.1.0
- The library is now compatible with ESP8266 and all flavour of Fishino - Corrected some bug on I2C read and write methods
1 parent 300f2b9 commit e3b9dbd

File tree

8 files changed

+237
-77
lines changed

8 files changed

+237
-77
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# D7S Arduino Library
22

3-
This library improve the use of the D7S sensor developed by Omron.
3+
This library improve the use of the D7S sensor developed by Omron. It is compatibile with Arduino avr based boards, esp8266 boards and all flavour of Fishino.
44

55
## Getting Started
66

@@ -10,7 +10,9 @@ Just download the lastest realease and place it your Arduino IDE library folder.
1010

1111
The D7S sensor must be connected to the I2C bus.
1212

13-
On most Arduino boards, SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin 5. On the Arduino Mega, SDA is digital pin 20 and SCL is 21. (See [https://www.arduino.cc/en/Guide/Libraries](https://www.arduino.cc/en/Reference/Wire))
13+
On most Arduino boards, SDA (data line) is on analog input pin 4, and SCL (clock line) is on analog input pin 5. On the Arduino Mega, SDA is digital pin 20 and SCL is 21 (See [https://www.arduino.cc/en/Guide/Libraries](https://www.arduino.cc/en/Reference/Wire)).
14+
15+
To use interrupt events provided by the D7S sensor you need to attach INT1 and INT2 pins to the interrupt pins of the boards you are using (See [https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/) or [http://esp8266.github.io/Arduino/versions/2.1.0-rc2/doc/reference.html](http://esp8266.github.io/Arduino/versions/2.1.0-rc2/doc/reference.html)). The interrupt pins of Fishino32 are 3, 5, 6 and 9.
1416

1517
## Authors
1618

examples/SeismographWithInterrupt/SeismographWithInterrupt.ino

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@
2121

2222
#include <D7S.h>
2323

24-
//interrupt pin INT1 of D7S attached to pin 2 of Arduino
25-
#define INT1_PIN 2
26-
//interrupt pin INT2 of D7S attached to pin 3 of Arduino
27-
#define INT2_PIN 3
24+
// Fishino32 interrupt pins
25+
#if defined(_FISHINO32_)
26+
#define INT1_PIN 3 //interrupt pin INT1 of D7S attached to pin 3 of Fishino32
27+
#define INT2_PIN 5 //interrupt pin INT2 of D7S attached to pin 5 of Fishino32
28+
// Esp8266 interrupt pins (tested on WeMos D1 R1)
29+
#elif defined(ESP8266)
30+
#define INT1_PIN D7 //interrupt pin INT1 of D7S attached to pin D7 of ESP8266
31+
#define INT2_PIN D8 //interrupt pin INT2 of D7S attached to pin D8 of ESP8266
32+
// Arduino UNO/Fishino UNO interrupt pins
33+
#else
34+
#define INT1_PIN 2 //interrupt pin INT1 of D7S attached to pin 2 of Arduino
35+
#define INT2_PIN 3 //interrupt pin INT2 of D7S attached to pin 3 of Arduino
36+
#endif
2837

2938
//--- EVENT HANDLERS --
3039
//function to handle the start of an earthquake
@@ -95,8 +104,9 @@ void setup() {
95104
D7S.setAxis(SWITCH_AT_INSTALLATION);
96105

97106
//--- INTERRUPT SETTINGS ---
98-
//enabling interrupt INT1 on pin 2 of Arduino
107+
//enabling interrupt INT1
99108
D7S.enableInterruptINT1(INT1_PIN);
109+
//enabling interrupt INT2
100110
D7S.enableInterruptINT2(INT2_PIN);
101111

102112
//registering event handler

keywords.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ AXIS_XY LITERAL1
7070
THRESHOLD_HIGH LITERAL1
7171
THRESHOLD_LOW LITERAL1
7272

73-
OK LITERAL1
74-
ERROR LITERAL1
73+
D7S_OK LITERAL1
74+
D7S_ERROR LITERAL1
7575

7676
START_EARTHQUAKE LITERAL1
7777
END_EARTHQUAKE LITERAL1

library.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=D7S
2-
version=1.0.0
3-
author=Alessandro Pasqualini
4-
maintainer=Alessandro Pasqualini
2+
version=1.1.0
3+
author=Alessandro Pasqualini <[email protected]>
4+
maintainer=Alessandro Pasqualini <[email protected]>
55
sentence=A library for using D7S earthquake sensor.
66
paragraph=D7S is an earthquake sensor which measures SI (Sismic Intensity) and PGA (Peak Ground Acceleration).
77
category=Sensors
88
url=https://github.com/alessandro1105/D7S_Arduino_Library
9-
architectures=avr
9+
architectures=avr, pic32, esp8266

src/D7S.cpp

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
- https://www.open-electronics.org
2020
*/
2121

22-
#include <Arduino.h>
23-
#include <D7S.h>
24-
#include <Wire.h>
22+
#include "D7S.h"
2523

2624
//----------------------- PUBLIC INTERFACE -----------------------
2725

@@ -35,30 +33,26 @@ D7SClass::D7SClass() {
3533
//reset events variable
3634
_events = 0;
3735

38-
//DEBUG
39-
#ifdef DEBUG
40-
Serial.begin(9600);
41-
#endif
4236
}
4337

4438
//--- BEGIN ---
4539
//used to initialize Wire
4640
void D7SClass::begin() {
4741
//begin Wire
48-
Wire.begin();
42+
WireD7S.begin();
4943
}
5044

5145
//--- STATUS ---
5246
//return the currect state
5347
d7s_status D7SClass::getState() {
5448
//read the STATE register at 0x1000
55-
return read8bit(0x10, 0x00) & 0x07;
49+
return (d7s_status) (read8bit(0x10, 0x00) & 0x07);
5650
}
5751

5852
//return the currect state
5953
d7s_axis_state D7SClass::getAxisInUse() {
6054
//read the AXIS_STATE register at 0x1001
61-
return read8bit(0x10, 0x01) & 0x03;
55+
return (d7s_axis_state) (read8bit(0x10, 0x01) & 0x03);
6256
}
6357

6458
//--- SETTINGS ---
@@ -213,7 +207,7 @@ void D7SClass::selftest() {
213207
//return the result of self-diagnostic test (OK/ERROR)
214208
d7s_mode_status D7SClass::getSelftestResult() {
215209
//return result of the selftest
216-
return (read8bit(0x10, 0x02) & 0x07) >> 2;
210+
return (d7s_mode_status) ((read8bit(0x10, 0x02) & 0x07) >> 2);
217211
}
218212

219213
//--- OFFSET ACQUISITION ---
@@ -226,7 +220,7 @@ void D7SClass::acquireOffset() {
226220
//return the result of offset acquisition test (OK/ERROR)
227221
d7s_mode_status D7SClass::getAcquireOffsetResult() {
228222
//return result of the offset acquisition
229-
return (read8bit(0x10, 0x02) & 0x0F) >> 3;
223+
return (d7s_mode_status) ((read8bit(0x10, 0x02) & 0x0F) >> 3);
230224
}
231225

232226
//--- SHUTOFF/COLLAPSE EVENT ---
@@ -269,19 +263,27 @@ uint8_t D7SClass::isReady() {
269263

270264
//--- INTERRUPT ---
271265
//enable interrupt INT1 on specified pin
272-
void D7SClass::enableInterruptINT1(uint8_t pin = D7S_INT1_PIN) {
266+
void D7SClass::enableInterruptINT1(uint8_t pin) {
273267
//enable pull up resistor
274268
pinMode(pin, INPUT_PULLUP);
275269
//attach interrupt
276270
attachInterrupt(digitalPinToInterrupt(pin), isr1, FALLING);
277271
}
278272

279273
//enable interrupt INT2 on specified pin
280-
void D7SClass::enableInterruptINT2(uint8_t pin = D7S_INT2_PIN) {
274+
void D7SClass::enableInterruptINT2(uint8_t pin) {
281275
//enable pull up resistor
282276
pinMode(pin, INPUT_PULLUP);
283-
//attach interrupt
284-
attachInterrupt(digitalPinToInterrupt(pin), isr2, CHANGE);
277+
// Fishino32 cannot handle CHANGE mode on interrupts, so we need to register FALLING mode first and on the isr register
278+
// as RISING the same pin detaching the previus interrupt
279+
#if defined(_FISHINO_PIC32_) || defined(_FISHINO32_) || defined(_FISHINO32_120_) || defined(_FISHINO32_MX470F512H_) || defined(_FISHINO32_MX470F512H_120_)
280+
pinINT2 = pin;
281+
//attach interrupt
282+
attachInterrupt(digitalPinToInterrupt(pin), isr2, FALLING);
283+
#else
284+
//attach interrupt
285+
attachInterrupt(digitalPinToInterrupt(pin), isr2, CHANGE);
286+
#endif
285287
}
286288

287289
//start interrupt handling
@@ -306,6 +308,10 @@ void D7SClass::registerInterruptEventHandler(d7s_interrupt_event event, void (*h
306308
_handlers[event] = handler;
307309
}
308310

311+
void D7SClass::registerInterruptEventHandler(d7s_interrupt_event event, void (*handler) (float, float, float)) {
312+
registerInterruptEventHandler(event, (void (*)()) handler);
313+
}
314+
309315

310316
//----------------------- PRIVATE INTERFACE -----------------------
311317

@@ -316,17 +322,22 @@ uint8_t D7SClass::read8bit(uint8_t regH, uint8_t regL) {
316322
//DEBUG
317323
#ifdef DEBUG
318324
Serial.println("--- read8bit ---");
325+
Serial.print("REG: 0x");
326+
Serial.print(regH, HEX);
327+
Serial.println(regL, HEX);
319328
#endif
320329

321330
//setting up i2c connection
322-
Wire.beginTransmission(D7S_ADDRESS);
331+
WireD7S.beginTransmission(D7S_ADDRESS);
332+
323333
//write register address
324-
Wire.write(regH); //register address high
325-
Wire.write(regL); //register address low
326-
//delay to prevent freezing
327-
delay(10);
328-
//status of the Wire connection
329-
uint8_t status = Wire.endTransmission(false);
334+
WireD7S.write(regH); //register address high
335+
delay(10); //delay to prevent freezing
336+
WireD7S.write(regL); //register address low
337+
delay(10); //delay to prevent freezing
338+
339+
//send RE-START message
340+
uint8_t status = WireD7S.endTransmission(false);
330341

331342
//DEBUG
332343
#ifdef DEBUG
@@ -337,25 +348,20 @@ uint8_t D7SClass::read8bit(uint8_t regH, uint8_t regL) {
337348

338349
//if the status != 0 there is an error
339350
if (status != 0) {
340-
//close the connection
341-
Wire.endTransmission(true);
342351
//retry
343352
return read8bit(regH, regL);
344353
}
354+
345355
//request 1 byte
346-
Wire.requestFrom(D7S_ADDRESS, 1);
356+
WireD7S.requestFrom(D7S_ADDRESS, 1);
347357
//wait until the data is received
348-
while (Wire.available() < 1)
358+
while (WireD7S.available() < 1)
349359
;
350360
//read the data
351-
uint8_t data = Wire.read();
352-
//status of the Wire connection
353-
status = Wire.endTransmission(true);
361+
uint8_t data = WireD7S.read();
354362

355363
//DEBUG
356364
#ifdef DEBUG
357-
Serial.print("[STOP]: ");
358-
Serial.println(status);
359365
Serial.println("--- read8bit ---");
360366
#endif
361367

@@ -369,17 +375,22 @@ uint16_t D7SClass::read16bit(uint8_t regH, uint8_t regL) {
369375
//DEBUG
370376
#ifdef DEBUG
371377
Serial.println("--- read16bit ---");
378+
Serial.print("REG: 0x");
379+
Serial.print(regH, HEX);
380+
Serial.println(regL, HEX);
372381
#endif
373382

374383
//setting up i2c connection
375-
Wire.beginTransmission(D7S_ADDRESS);
384+
WireD7S.beginTransmission(D7S_ADDRESS);
385+
376386
//write register address
377-
Wire.write(regH); //register address high
378-
Wire.write(regL); //register address low
379-
//delay to prevent freezing
380-
delay(10);
381-
//status of the Wire connection
382-
uint8_t status = Wire.endTransmission(false);
387+
WireD7S.write(regH); //register address high
388+
delay(10); //delay to prevent freezing
389+
WireD7S.write(regL); //register address low
390+
delay(10); //delay to prevent freezing
391+
392+
//send RE-START message
393+
uint8_t status = WireD7S.endTransmission(false);
383394

384395
//DEBUG
385396
#ifdef DEBUG
@@ -390,27 +401,21 @@ uint16_t D7SClass::read16bit(uint8_t regH, uint8_t regL) {
390401

391402
//if the status != 0 there is an error
392403
if (status != 0) {
393-
//close the connection
394-
Wire.endTransmission(true);
395-
//retry
404+
//retry again
396405
return read16bit(regH, regL);
397406
}
398407

399408
//request 2 byte
400-
Wire.requestFrom(D7S_ADDRESS, 2);
409+
WireD7S.requestFrom(D7S_ADDRESS, 2);
401410
//wait until the data is received
402-
while (Wire.available() < 2)
411+
while (WireD7S.available() < 2)
403412
;
404413
//read the data
405-
uint8_t msb = Wire.read();
406-
uint8_t lsb = Wire.read();
407-
//status of the Wire connection
408-
status = Wire.endTransmission(true);
414+
uint8_t msb = WireD7S.read();
415+
uint8_t lsb = WireD7S.read();
409416

410417
//DEBUG
411418
#ifdef DEBUG
412-
Serial.print("[STOP]: ");
413-
Serial.println(status);
414419
Serial.println("--- read16bit ---");
415420
#endif
416421

@@ -427,14 +432,19 @@ void D7SClass::write8bit(uint8_t regH, uint8_t regL, uint8_t val) {
427432
#endif
428433

429434
//setting up i2c connection
430-
Wire.beginTransmission(D7S_ADDRESS);
435+
WireD7S.beginTransmission(D7S_ADDRESS);
436+
431437
//write register address
432-
Wire.write(regH); //register address high
433-
Wire.write(regL); //register address low
438+
WireD7S.write(regH); //register address high
439+
delay(10); //delay to prevent freezing
440+
WireD7S.write(regL); //register address low
441+
delay(10); //delay to prevent freezing
442+
434443
//write data
435-
Wire.write(val);
444+
WireD7S.write(val);
445+
delay(10); //delay to prevent freezing
436446
//closing the connection (STOP message)
437-
uint8_t status = Wire.endTransmission(true);
447+
uint8_t status = WireD7S.endTransmission(true);
438448

439449
//DEBUG
440450
#ifdef DEBUG
@@ -484,11 +494,27 @@ void D7SClass::int2() {
484494
if (_interruptEnabled) {
485495
//check what in what state the D7S is
486496
if (isEarthquakeOccuring()) { //earthquake started
497+
// Fishino32 cannot handle CHANGE mode on interrupts, so we need to register FALLING mode first and on the isr register
498+
// as RISING the same pin detaching the previus interrupt
499+
#if defined(_FISHINO_PIC32_) || defined(_FISHINO32_) || defined(_FISHINO32_120_) || defined(_FISHINO32_MX470F512H_) || defined(_FISHINO32_MX470F512H_120_)
500+
// Detaching the previus interrupt as FALLING
501+
detachInterrupt(digitalPinToInterrupt(pinINT2));
502+
// Attaching the same interrupt as RISING
503+
attachInterrupt(digitalPinToInterrupt(pinINT2), isr2, RISING);
504+
#endif
487505
//if the handler is defined
488506
if (_handlers[0]) {
489507
_handlers[0](); //START_EARTHQUAKE EVENT
490508
}
491509
} else { //earthquake ended
510+
// Fishino32 cannot handle CHANGE mode on interrupts, so we need to register FALLING mode first and on the isr register
511+
// as RISING the same pin detaching the previus interrupt
512+
#if defined(_FISHINO_PIC32_) || defined(_FISHINO32_) || defined(_FISHINO32_120_) || defined(_FISHINO32_MX470F512H_) || defined(_FISHINO32_MX470F512H_120_)
513+
// Detaching the previus interrupt as FALLING
514+
detachInterrupt(digitalPinToInterrupt(pinINT2));
515+
// Attaching the same interrupt as RISING
516+
attachInterrupt(digitalPinToInterrupt(pinINT2), isr2, FALLING);
517+
#endif
492518
//if the handler is defined
493519
if (_handlers[1]) {
494520
((void (*)(float, float, float)) _handlers[1])(getLastestSI(0), getLastestPGA(0), getLastestTemperature(0)); //END_EARTHQUAKE EVENT
@@ -499,12 +525,12 @@ void D7SClass::int2() {
499525

500526
//--- ISR HANDLER ---
501527
//it handle the FALLING event that occur to the INT1 D7S pin (glue routine)
502-
static void D7SClass::isr1() {
528+
void D7SClass::isr1() {
503529
D7S.int1();
504530
}
505531

506532
//it handle the CHANGE event thant occur to the INT2 D7S pin (glue routine)
507-
static void D7SClass::isr2() {
533+
void D7SClass::isr2() {
508534
D7S.int2();
509535
}
510536

0 commit comments

Comments
 (0)