コード例 #1
0
  private void shutDown() throws InterruptedException {
    chipSelect.setValue(false);
    chipSelect.close();
    try {
      // move this stuff into a function
      configRegister.powerDown();
      SPIFactory.INSTANCE.writeRead(new byte[] {(byte) NRFCommand.FlushTx.getCommand()});
      statusRegister.clearMaxRTFlag();
      activateCommand();
      System.out.println(
          "Status of Config register " + String.format("%02X ", configRegister.readFromRegister()));

      System.out.println(
          "Status of Enable Data pipe register "
              + String.format("%02X ", dataPipeRegister.readFromRegister()));
      System.out.println(
          "Status of Retry register " + String.format("%02X ", retryRegister.readFromRegister()));
      System.out.println(
          "Status of Rx dataload width pipe0 register "
              + String.format("%02X ", rxdataloadinPipe0.readFromRegister()));
      System.out.println(
          "Status of Feature register "
              + String.format("%02X ", featureRegister.readFromRegister()));

      System.out.println(
          "Status of Address PIPE0 register "
              + String.format("%02X ", addressPipe0.readFromRegister()));

    } catch (SpiDeviceException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
コード例 #2
0
  // Actual Listener
  @Override
  public void run() {
    // start two threads one for button event processing and another for axis data processing
    // pass the RemoteSensorData object to it
    // write default payload to device
    try {
      // move these two lines in to a function
      writePayLoad(rfKeepAlive);
      chipSelect.setValue(true);
      System.out.println(
          "Status of Config register " + String.format("%02X ", configRegister.readFromRegister()));
      // tm is a threadMonitor  from the calling application to shutdown the threads when UI exits.
      while (!tm.getStopStatus()) {

        int status = statusRegister.readFromRegister();

        //	System.out.println("Status reg value: "+String.format("%02X ",status));
        // check whether RF link is up or down i.e whether the remote responds
        if (hasMaxRetransmitReached(status)) {
          //	System.out.println("MaxRT Flag raised");
          // reset this bit
          statusRegister.clearMaxRTFlag();
          if (linkEstablished && (exceptionCounter > 4)) {

            System.out.println("Link dropped -2");
            // previously link established
            chipSelect.setValue(false);
            SPIFactory.INSTANCE.writeRead(new byte[] {(byte) NRFCommand.FlushTx.getCommand()});
            writePayLoad(rfKeepAlive);
            chipSelect.setValue(true);
            exceptionCounter = 0;
          }
          if (linkEstablished) {
            exceptionCounter++;
          }

        } else if (hasAcknowledgementArrived(status)) {
          if (startThreads) {
            // start the threads to handle data received

            buttonProcessorThread.start();
            if (tiltProcessorThread != null) tiltProcessorThread.start();
            startThreads = false;
          }

          // reset NOACK counter here
          linkEstablished = true;
          exceptionCounter = 0;
          statusRegister.clearMaxRTFlag();
          // data has arrived
          // we know the data is 10 bytes long.

          // Data is read from NRF register
          // 10 bytes long -this array is of size 10
          remoteDataPackets = readDataFromFIFO(NUM_DATA_PACKETS);

          statusRegister.clearMaxRTFlag();

          if (buttonQueue.size() == BUFFER_SIZE) {
            buttonQueue.remove();
          }
          // last byte in response message is the button data
          // add it to a Queue - Button processor thread will create an event if a button
          // has been pressed or released
          buttonQueue.add(new Byte(remoteDataPackets[NUM_DATA_PACKETS]));
          if (tiltProcessorThread != null) {
            if (accQueue.size() == BUFFER_SIZE) {
              accQueue.remove();
            }

            // Accelerator data arrives in 1,2,3 positions
            // they are added to a different queue so another thread creates an event
            if (remoteDataPackets[1] != 0)
              //		accQueue.add(new Byte[]{(byte) (remoteDataPackets[1]+(byte)(0x80)),(byte)(
              // remoteDataPackets[2]-(byte)0x78) ,   (byte)(remoteDataPackets[3]+0x72)});
              accQueue.add(
                  new Byte[] {
                    (byte) remoteDataPackets[1],
                    (byte) remoteDataPackets[2],
                    (byte) remoteDataPackets[3],
                    (byte) remoteDataPackets[4],
                    (byte) remoteDataPackets[5],
                    (byte) remoteDataPackets[6]
                  });
          }

          int reuse = fifoRegister.readFromRegister();
          //	System.out.println("FIFO reg value: "+String.format("%02X ",(byte)reuse));
          // check whether there is a new transmit data is available to send or
          // or should we send default keep alive packet
          if (!(((byte) reuse & 0x40) == 0x40)) {
            //		System.out.println("Calling write payload");
            if (txDataQueue.isEmpty()) writePayLoad(rfKeepAlive);
            else {
              Byte[] dataFromuser = txDataQueue.poll();

              byte[] sendData = new byte[dataFromuser.length];
              for (int i = 0; i < sendData.length; i++) {
                sendData[i] = dataFromuser[i].byteValue();
              }
              writePayLoad(sendData);
            }
          }
        } else {
          // keep a counter here if it reaches 4 - then power down chip - reinitialise
          //	System.out.println("Neither Ack received nor Max rt occured Status reg value:
          // "+String.format("%02X ",status));

          if (linkEstablished && (exceptionCounter > 4)) {
            buttonQueue.clear();
            accQueue.clear();
            System.out.println("Link dropped");
            // previously link established
            chipSelect.setValue(false);
            SPIFactory.INSTANCE.writeRead(new byte[] {(byte) NRFCommand.FlushTx.getCommand()});
            writePayLoad(rfKeepAlive);
            chipSelect.setValue(true);
            exceptionCounter = 0;
          }
          if (linkEstablished) {
            exceptionCounter++;
          }
        }
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

    } catch (SpiDeviceException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      System.out.println("Shutdown command received....");
      try {
        shutDown();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      SPIFactory.INSTANCE.disconnectSPI();
      System.out.println("Device Controller thread exiting....");
    }
  }