Beispiel #1
0
// Write a single (Flash-resident) number to serial and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialPrintAndFlush(const unsigned u, const int fmt)
  {
  const bool neededWaking = powerUpSerialIfDisabled();
  // Send the character.
  Serial.print(u, fmt);
  // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
  _flush();
  if(neededWaking) { powerDownSerial(); }
  }
Beispiel #2
0
// Write a single (Flash-resident) string to serial followed by line-end and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialPrintlnAndFlush(__FlashStringHelper const * const line)
  {
  const bool neededWaking = powerUpSerialIfDisabled();
  // Send the line of text followed by line end.
  Serial.println(line);
  // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
  _flush();
  if(neededWaking) { powerDownSerial(); }
  }
Beispiel #3
0
// Write line-end to serial and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialPrintlnAndFlush()
  {
  const bool neededWaking = powerUpSerialIfDisabled();
  // Send the text.
  Serial.println();
  // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
  _flush();
  if(neededWaking) { powerDownSerial(); }
  }
Beispiel #4
0
// Print timestamp with no newline in format: MinutesSinceMidnight:Seconds:SubCycleTime
void _debug_serial_timestamp()
  {
  const bool neededWaking = powerUpSerialIfDisabled();
  // Grab time values ASAP, fastest-incrementing first.
  // TODO: could lock out interrupts to capture atomically.
  const uint8_t ss = getSubCycleTime();
  const uint8_t s = getSecondsLT();
  const uint16_t m = getMinutesSinceMidnightLT();
  Serial.print(m);
  Serial.print(':'); Serial.print(s);
  Serial.print(':'); Serial.print(ss);
  _flush();
  if(neededWaking) { powerDownSerial(); }
  }
// Write a single (read-only) string to serial and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialPrintAndFlush(const char * const text)
{
#ifdef ARDUINO
    const bool neededWaking = powerUpSerialIfDisabled<V0p2_DEFAULT_UART_BAUD>();
    // Send the text.
    Serial.print(text);
    // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
    _flush();
    if(neededWaking) {
        powerDownSerial();
    }
#else
    printf("%s", text);
    _flush();
#endif
}
// Write a single (read-only) buffer of length len to serial followed by line-end and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialWriteAndFlush(const char* buf, const uint8_t len)
{
#ifdef ARDUINO
    const bool neededWaking = powerUpSerialIfDisabled<V0p2_DEFAULT_UART_BAUD>();
    // Send the character.
    Serial.write(buf, len);
    // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
    _flush();
    if(neededWaking) {
        powerDownSerial();
    }
#else
    fwrite(buf, 1, len, stdout);
    _flush();
#endif
}
// Write a single (Flash-resident) string to serial followed by line-end and wait for transmission to complete.
// This enables the serial if required and shuts it down afterwards if it wasn't enabled.
void serialPrintlnAndFlush(__FlashStringHelper const * const line)
{
#ifdef ARDUINO
    const bool neededWaking = powerUpSerialIfDisabled<V0p2_DEFAULT_UART_BAUD>();
    // Send the line of text followed by line end.
    Serial.println(line);
    // Ensure that all text is sent before this routine returns, in case any sleep/powerdown follows that kills the UART.
    _flush();
    if(neededWaking) {
        powerDownSerial();
    }
#else
    puts((const char *)line);
    putchar('\n');
    _flush();
#endif
}
Beispiel #8
0
// Called from loop().
void loopAlt()
  {

  // Sleep in low-power mode (waiting for interrupts) until seconds roll.
  // NOTE: sleep at the top of the loop to minimise timing jitter/delay from Arduino background activity after loop() returns.
  // DHD20130425: waking up from sleep and getting to start processing below this block may take >10ms.
#if 0 && defined(DEBUG)
  DEBUG_SERIAL_PRINTLN_FLASHSTRING("*E"); // End-of-cycle sleep.
#endif
  powerDownSerial(); // Ensure that serial I/O is off.
  // Power down most stuff (except radio for hub RX).
  minimisePowerWithoutSleep();
  static uint_fast8_t TIME_LSD; // Controller's notion of seconds within major cycle.
  uint_fast8_t newTLSD;
  while(TIME_LSD == (newTLSD = getSecondsLT()))
    {
    sleepUntilInt(); // Normal long minimal-power sleep until wake-up interrupt.
    }
  TIME_LSD = newTLSD;
#if 0 && defined(DEBUG)
  DEBUG_SERIAL_PRINTLN_FLASHSTRING("*S"); // Start-of-cycle wake.
#endif


  // START LOOP BODY
  // ===============
 
  DEBUG_SERIAL_PRINTLN_FLASHSTRING("tick...");



  DEBUG_SERIAL_PRINTLN_FLASHSTRING("int count: ");
  DEBUG_SERIAL_PRINT(interruptCount);
  DEBUG_SERIAL_PRINTLN();





  }
Beispiel #9
0
// Called from loop().
void loopAlt()
  {
  // Sleep in low-power mode (waiting for interrupts) until seconds roll.
  // NOTE: sleep at the top of the loop to minimise timing jitter/delay from Arduino background activity after loop() returns.
  // DHD20130425: waking up from sleep and getting to start processing below this block may take >10ms.
#if 0 && defined(DEBUG)
  DEBUG_SERIAL_PRINTLN_FLASHSTRING("*E"); // End-of-cycle sleep.
#endif

#if defined(WAKEUP_32768HZ_XTAL) // Normal 32768Hz crystal driving main timing.
  powerDownSerial(); // Ensure that serial I/O is off.
  // Power down most stuff (except radio for hub RX).
  minimisePowerWithoutSleep();
//  RFM22ModeStandbyAndClearState();
  static uint_fast8_t TIME_LSD; // Controller's notion of seconds within major cycle.
  uint_fast8_t newTLSD;
  while(TIME_LSD == (newTLSD = getSecondsLT()))
    {
    sleepUntilInt(); // Normal long minimal-power sleep until wake-up interrupt.
//    DEBUG_SERIAL_PRINTLN_FLASHSTRING("w"); // Wakeup.
    }
  TIME_LSD = newTLSD;
#else // Keep running on main RC clock, simulating normal-ish sleep length.
  delay(2000);
#endif

#if 0 && defined(DEBUG)
  DEBUG_SERIAL_PRINTLN_FLASHSTRING("*S"); // Start-of-cycle wake.
#endif


  // START LOOP BODY
  // ===============

  DEBUG_SERIAL_PRINTLN_FLASHSTRING("*");



//  const bool neededWaking = powerUpSerialIfDisabled();


//  const int heat = TemperatureC16.read();
//#if 1 && defined(DEBUG)
//  DEBUG_SERIAL_PRINT_FLASHSTRING("temp: ");
//  DEBUG_SERIAL_PRINT(heat);
//  DEBUG_SERIAL_PRINTLN();
//#endif


//#if defined(DIRECT_MOTOR_DRIVE_V1) && defined(ALT_MAIN_LOOP) && defined(DEBUG)
//  // Ensure that end-stop current-sense feedback is enabled before starting the motor.
//  cb.hitEndStop = false;
//  hd.enableFeedback(true, cb);
//  // Ensure that the motor is running...
//  static bool open = true;
//  if(open) { DEBUG_SERIAL_PRINTLN_FLASHSTRING("opening"); } else { DEBUG_SERIAL_PRINTLN_FLASHSTRING("closing"); }
//  hd.motorRun(open ? HardwareMotorDriverInterface::motorDriveOpening : HardwareMotorDriverInterface::motorDriveClosing);
//  // Try to ride through any start-up transients...
//  nap(WDTO_120MS);
//  nap(WDTO_120MS);
//  nap(WDTO_120MS);
//  nap(WDTO_120MS);
//  // Spin the motor for up to about 1.5s polling for end-stop hit, etc.
//  while(!cb.hitEndStop && (getSubCycleTime() < 0xc0))
//    {
//    hd.enableFeedback(true, cb);
//    }
//  // Stop motor.
//  hd.motorRun(HardwareMotorDriverInterface::motorOff);
//  // Iff the end-stop was hit then reverse the motor.
//  if(cb.hitEndStop)
//    {
//    DEBUG_SERIAL_PRINTLN_FLASHSTRING("Hit end stop; reversing...");
//    open = !open;
//    }
//#endif

//  static bool boilerOut;
//  boilerOut = !boilerOut;
//  if(boilerOut) { LED_HEATCALL_ON() } else { LED_HEATCALL_OFF(); }
//  fastDigitalWrite(OUT_HEATCALL, boilerOut ? HIGH : LOW);
//#if defined(LED_UI2_L)
//  if(boilerOut) { LED_UI2_OFF() } else { LED_UI2_ON(); }
//#endif



//  uint8_t addr[8];
//
//  if(!MinOW.search(addr))
//    {
//    Serial.println("No more slaves found...");
//    MinOW.reset_search();
//    return;
//    }
//
//  // Found a device.
//  Serial.print("addr:");
//  for(int i = 0; i < 8; ++i)
//    {
//    Serial.write(' ');
//    Serial.print(addr[i], HEX);
//    }
//  Serial.println();
//
//  if(0x28 != addr[0])
//    {
//    Serial.println("Not a DS18B20...");
//    return;
//    }
//
//#define DS1820_PRECISION_MASK 0x60
//#define DS1820_PRECISION_9 0x00
//#define DS1820_PRECISION_10 0x20
//#define DS1820_PRECISION_11 0x40 // 1/8C @ 375ms.
//#define DS1820_PRECISION_12 0x60 // 1/16C @ 750ms.
//
//#define DS1820_PRECISION DS1820_PRECISION_11 // 1/8C @ 375ms.
//
//
//  // Force any pending output before return / possible UART power-down.
//  flushSerialSCTSensitive();
//  if(neededWaking) { powerDownSerial(); }
//
//  DEBUG_SERIAL_PRINTLN_FLASHSTRING("Setting precision...");
//  MinOW.reset();
//  // Write scratchpad/config
//  MinOW.select(addr);
//  MinOW.write(0x4e);
//  MinOW.write(0); // Th: not used.
//  MinOW.write(0); // Tl: not used.
//  MinOW.write(DS1820_PRECISION | 0x1f); // Config register; lsbs all 1.
//
//  const unsigned long usStart = micros();
//
//  // Start a temperature reading.
//  MinOW.reset();
//  MinOW.select(addr);
//  MinOW.write(0x44); // Start conversion without parasite power.
//  //delay(1000); // 750ms should be enough.
//  while(MinOW.read_bit() == 0) { /*nap(WDTO_30MS);*/ } // Poll for conversion complete (bus released)...
//
//  // Fetch temperature (scratchpad read).
//  MinOW.reset();
//  MinOW.select(addr);    
//  MinOW.write(0xbe);
//  byte data[2]; // Read first two bytes of 9 available.
//  for(uint8_t i = 0; i < sizeof(data); ++i)
//    { 
//    data[i] = MinOW.read();
////    Serial.print(data[i], HEX);
////    Serial.print(" ");
//    }
//  Serial.println();
//  MinOW.reset();
//
//  const unsigned long usEnd = micros();
//  // Nominal loop time should be 2s x 1MHz clock, ie 2,000,000 if CPU running all the time.
//  // Should generally be <2000 (<0.1%) for leaf, <20000 (<1%) for hub.
//  const unsigned long usApparentTaken = usEnd - usStart;
//#if 1 && defined(DEBUG)
//  DEBUG_SERIAL_PRINT_FLASHSTRING("us apparent: ");
//  DEBUG_SERIAL_PRINT(usApparentTaken);
//  DEBUG_SERIAL_PRINTLN();
//#endif
//
//  if(neededWaking) { powerUpSerialIfDisabled(); }
//
//  // Extract raw temperature, masking any undefined lsbits.
//  const int16_t rawC16 = (data[1] << 8) | (data[0] & ~1);
//  Serial.print("C16=");
//  Serial.print(rawC16);
////  Serial.print(" = ");
////  Serial.print(rawC16 / 16.0f);
//  Serial.println();

//  // Force any pending output before return / possible UART power-down.
//  flushSerialSCTSensitive();
//
//  if(neededWaking) { powerDownSerial(); }
  }