Exemplo n.º 1
0
 /// <summary>
 /// Probe the sensor by reading pin data
 /// </summary>
 private void ReadPin()
 {
     IsUpdateSuccessful = false;
     try
     {
         // Send the sensor our trigger signal
         pin.Write(false);
         // Wait at least 18ms for sensor initialization
         Pi.Wait(20);
         // Send the start signal
         pin.Write(true);
         // Wait 20μs - 40μs
         Pi.WaitMicroseconds(30);
         pin.Kind = PinKind.InputPullUp;
         // Sensor should take over and go low
         if (!pin.WaitLow(Pi.Microseconds(100)))
         {
             // FAIL: sensor did not take over
             return;
         }
         // Wait for the sensor to switch to high
         if (!pin.WaitHigh(Pi.Microseconds(100)))
         {
             // FAIL: sensor did not send high
             return;
         }
         // Wait for sensor to begin data bits
         if (!pin.WaitLow(Pi.Microseconds(100)))
         {
             // FAIL: sensor did not begin sending a data bit
             return;
         }
         // Elapsed is the duration a data bit was held high
         double elapsed = 0;
         // A buffer for eight bits of data
         byte data = 0;
         // Now we can begin reading 40 data bits
         for (int i = 0; i < 40; i++)
         {
             // Wait for the data bit
             if (!pin.WaitHigh(Pi.Microseconds(100)))
             {
                 // FAIL: no data bit was sent
                 return;
             }
             // Measure the data bit
             if (!pin.WaitLow(Pi.Microseconds(100), out elapsed))
             {
                 // FAIL: data bit was too long
                 return;
             }
             // Make room to store the next data bit
             data <<= 1;
             // If elapsed was more than 40µs then the data bit is 1
             if (elapsed > 0.04d)
             {
                 data |= 1;
             }
             // If 8 bits were read then copy those bits to our buffer
             if (((i + 1) % 8) == 0)
             {
                 buffer[i / 8] = data;
             }
         }
         // Success is reached if the checksum passes
         if ((buffer[4] == ((buffer[0] + buffer[1] + buffer[2] + buffer[3]) & 0xFF)))
         {
             IsUpdateSuccessful = (buffer[0] != 0) || (buffer[2] != 0);
         }
     }
     finally
     {
         // Set pin to high and for next update
         pin.Kind = PinKind.Output;
         pin.Write(true);
         // And set the last update time
         lastUpdate = Pi.Now;
     }
 }