@@ -535,45 +535,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
535
535
// Create I2C Response
536
536
wippersnapper_signal_v1_I2CResponse msgi2cResponse =
537
537
wippersnapper_signal_v1_I2CResponse_init_zero;
538
- if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag) {
539
- WS_DEBUG_PRINTLN (" I2C Init Request Found!" );
540
- // Decode I2CBusInitRequest
541
- wippersnapper_i2c_v1_I2CBusInitRequest msgI2CBusInitRequest =
542
- wippersnapper_i2c_v1_I2CBusInitRequest_init_zero;
543
- if (!pb_decode (stream, wippersnapper_i2c_v1_I2CBusInitRequest_fields,
544
- &msgI2CBusInitRequest)) {
545
- WS_DEBUG_PRINTLN (
546
- " ERROR: Could not decode wippersnapper_i2c_v1_I2CBusInitRequest" );
547
- return false ; // fail out
548
- }
549
538
550
- // Create a new I2C Component
551
- if (msgI2CBusInitRequest.i2c_port_number == 0 ) {
552
- WS._i2cPort0 = new WipperSnapper_Component_I2C (&msgI2CBusInitRequest);
553
- WS.i2cComponents .push_back (WS._i2cPort0 );
554
- // did we init. the port successfully?
555
- is_success = WS._i2cPort0 ->isInitialized ();
556
- } else if (msgI2CBusInitRequest.i2c_port_number == 1 ) {
557
- WS._i2cPort1 = new WipperSnapper_Component_I2C (&msgI2CBusInitRequest);
558
- // did we init. the port successfully?
559
- is_success = WS._i2cPort1 ->isInitialized ();
560
- WS.i2cComponents .push_back (WS._i2cPort1 );
561
- } else {
562
- WS_DEBUG_PRINTLN (" ERROR: Both I2C ports are in-use" );
563
- is_success = false ;
564
- }
565
-
566
- // Fill I2CResponse
567
- msgi2cResponse.which_payload =
568
- wippersnapper_signal_v1_I2CRequest_req_i2c_init_tag;
569
- msgi2cResponse.payload .resp_i2c_init .is_initialized = is_success;
570
-
571
- // Encode I2CResponse
572
- if (!encodeI2CResponse (&msgi2cResponse)) {
573
- return false ;
574
- }
575
- } else if (field->tag ==
576
- wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) {
539
+ if (field->tag == wippersnapper_signal_v1_I2CRequest_req_i2c_scan_tag) {
577
540
WS_DEBUG_PRINTLN (" I2C Scan Request" );
578
541
579
542
// Decode I2CBusScanRequest
@@ -586,24 +549,32 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
586
549
return false ; // fail out if we can't decode the request
587
550
}
588
551
589
- // Check if I2C components were previously initialized
590
- if (!WS._i2cPort0 ->isInitialized () && !WS._i2cPort0 ->isInitialized ()) {
591
- WS_DEBUG_PRINTLN (
592
- " ERROR: I2C Ports were not initialized prior to scanning!" );
593
- return false ;
594
- }
595
-
596
- // Perform I2C scan
552
+ // Empty response message
597
553
wippersnapper_i2c_v1_I2CBusScanResponse scanResp =
598
554
wippersnapper_i2c_v1_I2CBusScanResponse_init_zero;
599
- if (msgScanReq.i2c_port_number == 0 ) {
600
- scanResp = WS._i2cPort0 ->scanAddresses ();
601
- } else {
602
- scanResp = WS._i2cPort1 ->scanAddresses ();
555
+
556
+ // Has the I2C bus been initialized?
557
+ if (!WS._isI2CPort0Init ) {
558
+ WS._i2cPort0 =
559
+ new WipperSnapper_Component_I2C (&msgScanReq.bus_init_request );
560
+ WS.i2cComponents .push_back (WS._i2cPort0 );
561
+ WS._isI2CPort0Init = WS._i2cPort0 ->isInitialized ();
562
+ msgi2cResponse.payload .resp_i2c_init .bus_response =
563
+ WS._i2cPort0 ->getBusStatus ();
564
+ // Fail out and publish back if not RESPONSE_SUCCESS
565
+ if (msgi2cResponse.payload .resp_i2c_init .bus_response !=
566
+ wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS) {
567
+ if (!encodeI2CResponse (&msgi2cResponse)) {
568
+ WS_DEBUG_PRINTLN (" ERROR: encoding I2C Response!" );
569
+ return false ;
570
+ }
571
+ publishI2CResponse (&msgi2cResponse);
572
+ }
603
573
}
604
- WS_DEBUG_PRINTLN (" Scan Complete!" );
605
- WS_DEBUG_PRINT (" \t # of addresses found on bus: " );
606
- WS_DEBUG_PRINTLN (scanResp.addresses_found_count );
574
+
575
+ // Execute I2C Scan
576
+ if (WS._isI2CPort0Init == true )
577
+ scanResp = WS._i2cPort0 ->scanAddresses ();
607
578
608
579
// Fill I2CResponse
609
580
msgi2cResponse.which_payload =
@@ -612,6 +583,8 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
612
583
scanResp.addresses_found , sizeof (scanResp.addresses_found ));
613
584
msgi2cResponse.payload .resp_i2c_scan .addresses_found_count =
614
585
scanResp.addresses_found_count ;
586
+
587
+ msgi2cResponse.payload .resp_i2c_scan .bus_response = scanResp.bus_response ;
615
588
// Encode I2CResponse
616
589
if (!encodeI2CResponse (&msgi2cResponse)) {
617
590
return false ;
@@ -629,30 +602,83 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
629
602
return false ; // fail out if we can't decode
630
603
}
631
604
632
- // Create response
605
+ // Create empty response
633
606
msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero;
634
607
msgi2cResponse.which_payload =
635
608
wippersnapper_signal_v1_I2CResponse_resp_i2c_device_init_tag;
636
609
637
- // Initialize device and fill response
638
- if (msgI2CDeviceInitRequest.i2c_port_number == 0 &&
639
- WS._i2cPort0 ->isInitialized () == true ) {
610
+ // Has the I2C bus been initialized?
611
+ if (!WS._isI2CPort0Init ) {
612
+ WS._i2cPort0 = new WipperSnapper_Component_I2C (
613
+ &msgI2CDeviceInitRequest.bus_init_request );
614
+ WS.i2cComponents .push_back (WS._i2cPort0 );
615
+ WS._isI2CPort0Init = WS._i2cPort0 ->isInitialized ();
616
+ msgi2cResponse.payload .resp_i2c_init .bus_response =
617
+ WS._i2cPort0 ->getBusStatus ();
618
+ // Fail out and publish back if not RESPONSE_SUCCESS
619
+ if (msgi2cResponse.payload .resp_i2c_init .bus_response !=
620
+ wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS) {
621
+ if (!encodeI2CResponse (&msgi2cResponse)) {
622
+ WS_DEBUG_PRINTLN (" ERROR: encoding I2C Response!" );
623
+ return false ;
624
+ }
625
+ publishI2CResponse (&msgi2cResponse);
626
+ }
627
+ }
628
+
629
+ // Initialize I2C device
630
+ if (WS._isI2CPort0Init == true )
640
631
msgi2cResponse.payload .resp_i2c_device_init .is_success =
641
632
WS._i2cPort0 ->initI2CDevice (&msgI2CDeviceInitRequest);
642
- } else if (msgI2CDeviceInitRequest.i2c_port_number == 1 &&
643
- WS._i2cPort1 ->isInitialized () == true ) {
644
- msgi2cResponse.payload .resp_i2c_device_init .is_success =
645
- WS._i2cPort1 ->initI2CDevice (&msgI2CDeviceInitRequest);
646
- }
633
+
634
+ // Fill device's address
635
+ msgi2cResponse.payload .resp_i2c_device_init .i2c_address =
636
+ msgI2CDeviceInitRequest.i2c_address ;
637
+
638
+ msgi2cResponse.payload .resp_i2c_device_init .bus_response =
639
+ wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS;
647
640
648
641
// Encode response
649
642
if (!encodeI2CResponse (&msgi2cResponse)) {
650
643
return false ;
651
644
}
645
+ } else if (field->tag ==
646
+ wippersnapper_signal_v1_I2CRequest_req_i2c_device_update_tag) {
647
+ WS_DEBUG_PRINTLN (" => INCOMING REQUEST: I2CDeviceUpdateRequest" );
652
648
649
+ // New I2CDeviceUpdateRequest message
650
+ wippersnapper_i2c_v1_I2CDeviceUpdateRequest msgI2CDeviceUpdateRequest =
651
+ wippersnapper_i2c_v1_I2CDeviceUpdateRequest_init_zero;
652
+
653
+ // Decode stream into message
654
+ if (!pb_decode (stream, wippersnapper_i2c_v1_I2CDeviceUpdateRequest_fields,
655
+ &msgI2CDeviceUpdateRequest)) {
656
+ WS_DEBUG_PRINTLN (
657
+ " ERROR: Could not decode I2CDeviceUpdateRequest message." );
658
+ return false ; // fail out if we can't decode
659
+ }
660
+
661
+ // Empty I2C response to fill out
662
+ msgi2cResponse = wippersnapper_signal_v1_I2CResponse_init_zero;
663
+ msgi2cResponse.which_payload =
664
+ wippersnapper_signal_v1_I2CResponse_resp_i2c_device_update_tag;
665
+
666
+ // Update I2C device
667
+ if (WS._isI2CPort0Init == true )
668
+ msgi2cResponse.payload .resp_i2c_device_update .is_success =
669
+ WS._i2cPort0 ->updateI2CDeviceProperties (&msgI2CDeviceUpdateRequest);
670
+
671
+ // Fill address
672
+ msgi2cResponse.payload .resp_i2c_device_update .i2c_address =
673
+ msgI2CDeviceUpdateRequest.i2c_address ;
674
+
675
+ // Encode response
676
+ if (!encodeI2CResponse (&msgi2cResponse)) {
677
+ return false ;
678
+ }
653
679
} else if (field->tag ==
654
680
wippersnapper_signal_v1_I2CRequest_req_i2c_device_deinit_tag) {
655
- WS_DEBUG_PRINTLN (" NEW COMMAND: I2C Device De-init " );
681
+ WS_DEBUG_PRINTLN (" NEW COMMAND: I2C Device Deinit " );
656
682
// Decode stream into an I2CDeviceDeinitRequest
657
683
wippersnapper_i2c_v1_I2CDeviceDeinitRequest msgI2CDeviceDeinitRequest =
658
684
wippersnapper_i2c_v1_I2CDeviceDeinitRequest_init_zero;
@@ -670,11 +696,9 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
670
696
wippersnapper_signal_v1_I2CResponse_resp_i2c_device_deinit_tag;
671
697
672
698
// Delete device from I2C bus
673
- if (msgI2CDeviceDeinitRequest.i2c_port_number == 0 &&
674
- WS._i2cPort0 ->isInitialized () == true ) {
699
+ if (WS._isI2CPort0Init == true )
675
700
msgi2cResponse.payload .resp_i2c_device_deinit .is_success =
676
701
WS._i2cPort0 ->deinitI2CDevice (&msgI2CDeviceDeinitRequest);
677
- }
678
702
679
703
// Encode response
680
704
if (!encodeI2CResponse (&msgi2cResponse)) {
@@ -684,6 +708,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
684
708
WS_DEBUG_PRINTLN (" ERROR: Undefined I2C message tag" );
685
709
return false ; // fail out, we didn't encode anything to publish
686
710
}
711
+ // Publish the I2CResponse
687
712
publishI2CResponse (&msgi2cResponse);
688
713
return is_success;
689
714
}
@@ -1305,14 +1330,14 @@ bool Wippersnapper::registerBoard() {
1305
1330
1306
1331
// Encode and publish registration request message to broker
1307
1332
runNetFSM ();
1308
- feedWDT ();
1333
+ WS. feedWDT ();
1309
1334
WS_DEBUG_PRINT (" Encoding registration request..." );
1310
1335
if (!encodePubRegistrationReq ())
1311
1336
return false ;
1312
1337
1313
1338
// Blocking, attempt to obtain broker's response message
1314
1339
runNetFSM ();
1315
- feedWDT ();
1340
+ WS. feedWDT ();
1316
1341
pollRegistrationResp ();
1317
1342
1318
1343
return true ;
@@ -1368,6 +1393,7 @@ void Wippersnapper::feedWDT() {
1368
1393
/* ******************************************************/
1369
1394
void Wippersnapper::enableWDT (int timeoutMS) {
1370
1395
#ifndef ESP8266
1396
+ Watchdog.disable ();
1371
1397
if (Watchdog.enable (timeoutMS) == 0 ) {
1372
1398
WS_DEBUG_PRINTLN (" ERROR: WDT initialization failure!" );
1373
1399
setStatusLEDColor (LED_ERROR);
@@ -1388,7 +1414,7 @@ void Wippersnapper::enableWDT(int timeoutMS) {
1388
1414
void Wippersnapper::processPackets () {
1389
1415
// runNetFSM(); // NOTE: Removed for now, causes error with virtual _connect
1390
1416
// method when caused with WS object in another file.
1391
- feedWDT ();
1417
+ WS. feedWDT ();
1392
1418
// Process all incoming packets from Wippersnapper MQTT Broker
1393
1419
WS._mqtt ->processPackets (10 );
1394
1420
}
@@ -1411,7 +1437,7 @@ void Wippersnapper::publish(const char *topic, uint8_t *payload, uint16_t bLen,
1411
1437
uint8_t qos) {
1412
1438
// runNetFSM(); // NOTE: Removed for now, causes error with virtual _connect
1413
1439
// method when caused with WS object in another file.
1414
- feedWDT ();
1440
+ WS. feedWDT ();
1415
1441
WS._mqtt ->publish (topic, payload, bLen, qos);
1416
1442
}
1417
1443
@@ -1425,7 +1451,7 @@ void Wippersnapper::publish(const char *topic, uint8_t *payload, uint16_t bLen,
1425
1451
/* *************************************************************************/
1426
1452
void Wippersnapper::connect (bool useStagingBroker) {
1427
1453
// enable WDT
1428
- enableWDT (WS_WDT_TIMEOUT);
1454
+ WS. enableWDT (WS_WDT_TIMEOUT);
1429
1455
1430
1456
// TODO!
1431
1457
// not sure we need to track these...
@@ -1445,7 +1471,7 @@ void Wippersnapper::connect(bool useStagingBroker) {
1445
1471
1446
1472
// Run the network fsm
1447
1473
runNetFSM ();
1448
- feedWDT ();
1474
+ WS. feedWDT ();
1449
1475
setStatusLEDColor (LED_CONNECTED);
1450
1476
1451
1477
// Register hardware with Wippersnapper
@@ -1455,7 +1481,7 @@ void Wippersnapper::connect(bool useStagingBroker) {
1455
1481
haltError (" Unable to register with WipperSnapper." );
1456
1482
}
1457
1483
runNetFSM ();
1458
- feedWDT ();
1484
+ WS. feedWDT ();
1459
1485
1460
1486
// Configure hardware
1461
1487
WS.pinCfgCompleted = false ;
@@ -1465,7 +1491,7 @@ void Wippersnapper::connect(bool useStagingBroker) {
1465
1491
WS._mqtt ->processPackets (10 ); // poll
1466
1492
}
1467
1493
// Publish that we have completed the configuration workflow
1468
- feedWDT ();
1494
+ WS. feedWDT ();
1469
1495
runNetFSM ();
1470
1496
publishPinConfigComplete ();
1471
1497
WS_DEBUG_PRINTLN (" Hardware configured successfully!" );
@@ -1519,24 +1545,25 @@ void Wippersnapper::publishPinConfigComplete() {
1519
1545
ws_status_t Wippersnapper::run () {
1520
1546
// Check networking
1521
1547
runNetFSM ();
1522
- feedWDT ();
1548
+ WS. feedWDT ();
1523
1549
pingBroker ();
1524
1550
1525
1551
// Process all incoming packets from Wippersnapper MQTT Broker
1526
1552
WS._mqtt ->processPackets (10 );
1527
- feedWDT ();
1553
+ WS. feedWDT ();
1528
1554
1529
1555
// Process digital inputs, digitalGPIO module
1530
1556
WS._digitalGPIO ->processDigitalInputs ();
1531
- feedWDT ();
1557
+ WS. feedWDT ();
1532
1558
1533
1559
// Process analog inputs
1534
1560
WS._analogIO ->processAnalogInputs ();
1535
- feedWDT ();
1561
+ WS. feedWDT ();
1536
1562
1537
1563
// Process I2C sensor events
1538
- // WS._i2cPort0->update();
1539
- // feedWDT();
1564
+ if (WS._isI2CPort0Init )
1565
+ WS._i2cPort0 ->update ();
1566
+ WS.feedWDT ();
1540
1567
1541
1568
return WS_NET_CONNECTED; // TODO: Make this funcn void!
1542
1569
}
0 commit comments