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
4640void D7SClass::begin () {
4741 // begin Wire
48- Wire .begin ();
42+ WireD7S .begin ();
4943}
5044
5145// --- STATUS ---
5246// return the currect state
5347d7s_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
5953d7s_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)
214208d7s_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)
227221d7s_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