Test firmware for the Supermote hardware.
Dependencies: K30_I2C libxDot-mbed5
Fork of K30_I2C_Program by
Diff: HPM/hpm.cpp
- Revision:
- 3:2ab969b31f6b
- Parent:
- 2:680fc2690d10
diff -r 680fc2690d10 -r 2ab969b31f6b HPM/hpm.cpp --- a/HPM/hpm.cpp Fri Sep 07 02:54:05 2018 +0000 +++ b/HPM/hpm.cpp Fri Sep 14 03:17:31 2018 +0000 @@ -1,21 +1,28 @@ #include "mbed.h" #include "hpm.h" - + HPM::HPM(Serial* device, Serial* debug): _device(device), _debug(debug) { - _device->baud(9600); - wait(0.1); + } bool HPM::init(void) { + _debug->printf("Initialising the PM sensor\r\n"); + wait(1.0); + + _debug->printf("PM baud rate set to 9600\r\n"); + _device->baud(9600); + wait(0.1); + // Stop autosend if (!this->stop_autosend()) { _debug->printf("Stop autosend failed\r\n"); return 0; } + _debug->printf("Stop autosend success\r\n"); // Start fan if (!this->start_measurement()) @@ -23,69 +30,81 @@ _debug->printf("Start measurement failed\r\n"); return 0; } + _debug->printf("Start measurement success\r\n"); return 1; } bool HPM::start_measurement(void) { + clear_rx_buffer(); + // First, we send the command unsigned char start_measurement[] = {0x68, 0x01, 0x01, 0x96 }; for (int i=0; i<sizeof(start_measurement); i++) { + while (!_device->writeable()); // wait until we can write to the port _device->putc(start_measurement[i]); } - //Then we wait for the response - while (!_device->readable()) {} // wait until chars arrive - char read1 = _device->getc(); - char read2 = _device->getc(); - - // Test the response - if ((read1 == 0xA5) && (read2 == 0xA5)){ - return 1; // ACK - } - else if ((read1 == 0x96) && (read2 == 0x96)) - { - _debug->printf ("NACK\r\n"); - return 0; // NACK - } - else return 0; + //Then we wait for the response + while (!_device->readable()) {} // wait until chars arrive + char read1 = _device->getc(); + while (!_device->readable()) {} // wait until chars arrive + char read2 = _device->getc(); + + // Test the response + if ((read1 == 0xA5) && (read2 == 0xA5)){ + return 1; // ACK + } + else if ((read1 == 0x96) && (read2 == 0x96)) + { + _debug->printf ("NACK\r\n"); + return 0; // NACK + } + else return 0; } bool HPM::stop_measurement(void) { + clear_rx_buffer(); + // First, we send the command unsigned char stop_measurement[] = {0x68, 0x01, 0x01, 0x95 }; for (int i=0; i<sizeof(stop_measurement); i++) { + while (!_device->writeable()); // wait until we can write to the port _device->putc(stop_measurement[i]); } - //Then we wait for the response - while (!_device->readable()) {} // wait until chars arrive - char read1 = _device->getc(); - char read2 = _device->getc(); - - // Test the response - if ((read1 == 0xA5) && (read2 == 0xA5)){ - return 1; // ACK - } - else if ((read1 == 0x96) && (read2 == 0x96)) - { - _debug->printf ("NACK\r\n"); - return 0; // NACK - } - else return 0; + //Then we wait for the response + while (!_device->readable()) {} // wait until chars arrive + char read1 = _device->getc(); + while(!_device->readable()); + char read2 = _device->getc(); + + // Test the response + if ((read1 == 0xA5) && (read2 == 0xA5)){ + return 1; // ACK + } + else if ((read1 == 0x96) && (read2 == 0x96)) + { + _debug->printf ("NACK\r\n"); + return 0; // NACK + } + else return 0; } bool HPM::read_measurement (long int &PM25, long int &PM10) { + clear_rx_buffer(); + // Send the command 0x68 0x01 0x04 0x93 unsigned char read_particle[] = {0x68, 0x01, 0x04, 0x93 }; for (int i=0; i<sizeof(read_particle); i++) { + while (!_device->writeable()); // wait until we can write to the port _device->putc(read_particle[i]); } @@ -145,61 +164,87 @@ return 1; } } + + _debug->printf ("HEAD=%X, LEN=%X\r\n", HEAD, LEN); return 0; } bool HPM::stop_autosend(void) { + clear_rx_buffer(); + // Stop auto send unsigned char stop_autosend[] = {0x68, 0x01, 0x20, 0x77 }; for (int i=0; i<sizeof(stop_autosend); i++) { + while (!_device->writeable()); // wait until we can write to the port _device->putc(stop_autosend[i]); } - - //Then we wait for the response - while(!_device->readable()); - char read1 = _device->getc(); - char read2 = _device->getc(); - - // Test the response - if ((read1 == 0xA5) && (read2 == 0xA5)){ - // ACK - return 1; - } - else if ((read1 == 0x96) && (read2 == 0x96)) - { - // NACK - _debug->printf ("NACK\r\n"); - return 0; - } - else return 0; + + //Then we wait for the response + while(!_device->readable()); + char read1 = _device->getc(); + while(!_device->readable()); + char read2 = _device->getc(); + + // Test the response + if ((read1 == 0xA5) && (read2 == 0xA5)){ + // ACK + return 1; + } + else if ((read1 == 0x96) && (read2 == 0x96)) + { + // NACK + _debug->printf ("NACK\r\n"); + return 0; + } + else + { + _debug->printf ("stop_autosend() output is %X%X\r\n", read1, read2); + return 0; + } } bool HPM::start_autosend(void) { + clear_rx_buffer(); + // Start auto send unsigned char start_autosend[] = {0x68, 0x01, 0x40, 0x57 }; for (int i=0; i<sizeof(start_autosend); i++) { + while (!_device->writeable()); // wait until we can write to the port _device->putc(start_autosend[i]); } - //Then we wait for the response - while(!_device->readable()); - char read1 = _device->getc(); - char read2 = _device->getc(); - // Test the response - if ((read1 == 0xA5) && (read2 == 0xA5)){ - // ACK - return 1; - } - else if ((read1 == 0x96) && (read2 == 0x96)) - { - // NACK - _debug->printf ("NACK\r\n"); - return 0; - - } - else return 0; -} \ No newline at end of file + //Then we wait for the response + while(!_device->readable()); + char read1 = _device->getc(); + while(!_device->readable()); + char read2 = _device->getc(); + // Test the response + if ((read1 == 0xA5) && (read2 == 0xA5)){ + // ACK + return 1; + } + else if ((read1 == 0x96) && (read2 == 0x96)) + { + // NACK + _debug->printf ("NACK\r\n"); + return 0; + } + else + { + _debug->printf ("start_autosend() output is %X%X\r\n", read1, read2); + return 0; + } +} + +void HPM::clear_rx_buffer(void) +{ + // ensure the Rx buffer is empty. clear out the crap + while (_device->readable()) + { + unsigned char dummy = _device->getc(); + } +}