Esempio n. 1
0
uint8_t ow_search_devices(OW_device *devices, uint8_t num_devices, uint8_t pin, uint8_t *num) {
  uint8_t i, j;
  uint8_t *new_id;
  uint8_t *current_id;
  uint8_t last_deviation;
  uint8_t num_found_devices;
  uint8_t flag = SEARCH_SUCCESSFUL;

  for(i = 0; i < num_devices; i++) {
    for(j = 0; j < 8; j++) {
      devices[i].id[j] = 0x00;
    }
  }

  num_found_devices = 0;
  new_id            = devices[0].id;
  last_deviation    = 0;
  current_id        = new_id;

  do {
    memcpy(new_id, current_id, 8);
    if(!ow_detect_presence(pin)) {
      return SEARCH_ERROR;
    };
    last_deviation = ow_search_rom(new_id, last_deviation, pin);
    current_id     = new_id;
    num_found_devices++;
    new_id = devices[num_found_devices].id;
  } while(last_deviation != OW_ROM_SEARCH_FINISHED);


  for(i = 0; i < num_found_devices; i++) {
    if(ow_check_rom_crc(devices[i].id) != OW_CRC_OK) {
      flag = SEARCH_CRC_ERROR;
    } else {
      (*num)++;
    }
  }
  return flag;
}
Esempio n. 2
0
static int8_t ow_discover_sensor(void) {
	uint8_t firstonbus = 1;
	int8_t ret = 0;
#if ONEWIRE_BUSCOUNT > 1
	ow_global.bus = 0;
#endif /* ONEWIRE_BUSCOUNT */
#ifdef DEBUG_OW_POLLING
	debug_printf("starting discovery\n");
#endif /* DEBUG_OW_POLLING */
	/*Prepare existing sensors*/
	for (uint8_t i = 0; i < OW_SENSORS_COUNT; i++)
		ow_sensors[i].present = 0;

#if ONEWIRE_BUSCOUNT > 1
	do {
#endif /* ONEWIRE_BUSCOUNT > 1 */
		do {
			ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
#if ONEWIRE_BUSCOUNT > 1
				ret = ow_search_rom((uint8_t)(1 << (ow_global.bus + ONEWIRE_STARTPIN)), firstonbus);
#else /* ONEWIRE_BUSCOUNT > 1 */
				ret = ow_search_rom(ONEWIRE_BUSMASK, firstonbus);
#endif /* ONEWIRE_BUSCOUNT > 1 */
			}

			/* make sure only one conversion happens at a time */
			ow_global.lock = 1;

			if (ret == 1) {
				firstonbus = 0;
#ifdef DEBUG_OW_POLLING
				debug_printf("discovered device %02x %02x %02x %02x %02x %02x %02x %02x"
#if ONEWIRE_BUSCOUNT > 1
				             " on bus %d"
#endif /* ONEWIRE_BUSCOUNT > 1 */
				             "\n",
				             ow_global.current_rom.bytewise[0],
				             ow_global.current_rom.bytewise[1],
				             ow_global.current_rom.bytewise[2],
				             ow_global.current_rom.bytewise[3],
				             ow_global.current_rom.bytewise[4],
				             ow_global.current_rom.bytewise[5],
				             ow_global.current_rom.bytewise[6],
				             ow_global.current_rom.bytewise[7]
#if ONEWIRE_BUSCOUNT > 1
				             ,ow_global.bus
#endif /* ONEWIRE_BUSCOUNT > 1 */
				            );
#endif /* DEBUG_OW_POLLING */
				if (ow_temp_sensor(&ow_global.current_rom)) {
					uint8_t already_in = 0;
					/*Determine whether this sensor is already present in our list*/
					for (uint8_t i = 0; i < OW_SENSORS_COUNT; i++) {
						if (ow_global.current_rom.raw == ow_sensors[i].ow_rom_code.raw) {
							ow_sensors[i].present = 1;
							already_in = 1;
							/*We skip everything else to retain a regular update rate*/
							break;
						}
					}
					if (already_in == 0) {
						uint8_t i;
						/*The sensor we found is not in our list, so we search for the first free sensor slot, e.g. the first slot where ow_rom_code is zero*/
						for (i = 0; i < OW_SENSORS_COUNT; i++) {
							if (ow_sensors[i].ow_rom_code.raw == 0) {
								/* We found a free slot...storing*/
#ifdef DEBUG_OW_POLLING
								debug_printf("stored new sensor in pos %d\n", i);
#endif /* DEBUG_OW_POLLING */
								ow_sensors[i].ow_rom_code.raw = ow_global.current_rom.raw;
								ow_sensors[i].present = 1;
								ow_sensors[i].read_delay = 1; /*Read temperature asap - note: we will check for eeprom later*/
								break;
							}
						}
#ifdef DEBUG_OW_POLLING
						if (i == OW_SENSORS_COUNT - 1)
							debug_printf("number of sensors exceeds list size of %d\n", OW_SENSORS_COUNT);
#endif /* DEBUG_OW_POLLING */
					}
#ifdef DEBUG_OW_POLLING
				} else {
					debug_printf("not a temperature sensor\n");
#endif /* DEBUG_OW_POLLING */
				}
			}
		} while (ret > 0);
#if ONEWIRE_BUSCOUNT > 1
		ow_global.bus++;
		firstonbus = 1;
	} while (ow_global.bus < ONEWIRE_BUSCOUNT);
#endif /* ONEWIRE_BUSCOUNT > 1 */
	ow_global.lock = 0;
	/*We finished the discovery process. Now we delete all removed sensors*/
	for (uint8_t i = 0; i < OW_SENSORS_COUNT; i++) {
		/*Mark the slot as free*/
		if (ow_sensors[i].present == 0)
			ow_sensors[i].ow_rom_code.raw = 0;
	}
	return 0;
}
Esempio n. 3
0
int16_t
parse_cmd_onewire_list(char *cmd, char *output, uint16_t len)
{

  uint8_t firstonbus = 0;
  int16_t ret;

  if (ow_global.lock == 0)
  {
    firstonbus = 1;
#if ONEWIRE_BUSCOUNT > 1
    ow_global.bus = 0;
#endif
    OW_DEBUG_LIST("called onewire list for the first time\n");

#ifdef ONEWIRE_DS2502_SUPPORT
    /* parse optional parameters */
    while (*cmd == ' ')
      cmd++;
    switch (*cmd)
    {
      case 't':
        ow_global.list_type = OW_LIST_TYPE_TEMP_SENSOR;
        break;
      case 'e':
        ow_global.list_type = OW_LIST_TYPE_EEPROM;
        break;
      case '\0':
        ow_global.list_type = OW_LIST_TYPE_ALL;
        break;
      default:
        return ECMD_ERR_PARSE_ERROR;
    }
#endif
  }
  else
  {
    OW_DEBUG_LIST("called onewire list again\n");
    firstonbus = 0;
  }

#if defined ONEWIRE_DS2502_SUPPORT || ONEWIRE_BUSCOUNT > 1
list_next:;
#endif

#if ONEWIRE_BUSCOUNT > 1
  ret =
    ow_search_rom((uint8_t) (1 << (ow_global.bus + ONEWIRE_STARTPIN)),
                  firstonbus);
#else
  ret = ow_search_rom(ONEWIRE_BUSMASK, firstonbus);
#endif

  /* make sure only one conversion happens at a time */
  ow_global.lock = 1;

  if (ret == 1)
  {
#ifdef ONEWIRE_DS2502_SUPPORT
    if (ow_global.list_type == OW_LIST_TYPE_ALL ||
        (ow_global.list_type == OW_LIST_TYPE_TEMP_SENSOR &&
         ow_temp_sensor(&ow_global.current_rom)) ||
        (ow_global.list_type == OW_LIST_TYPE_EEPROM &&
         ow_eeprom(&ow_global.current_rom)))
    {
      /* only print device rom address if it matches the selected list type */
#endif

      OW_DEBUG_LIST("discovered device "
#if ONEWIRE_BUSCOUNT > 1
                    "%02x %02x %02x %02x %02x %02x %02x %02x on bus %d\n",
#else
                    "%02x %02x %02x %02x %02x %02x %02x %02x\n",
#endif
                    ow_global.current_rom.bytewise[0],
                    ow_global.current_rom.bytewise[1],
                    ow_global.current_rom.bytewise[2],
                    ow_global.current_rom.bytewise[3],
                    ow_global.current_rom.bytewise[4],
                    ow_global.current_rom.bytewise[5],
                    ow_global.current_rom.bytewise[6],
                    ow_global.current_rom.bytewise[7]
#if ONEWIRE_BUSCOUNT > 1
                    , ow_global.bus);
#else
        );
#endif
#ifdef ONEWIRE_NAMING_SUPPORT
      char *name = "";
      ow_sensor_t *sensor = ow_find_sensor(&ow_global.current_rom);
      if (sensor != NULL && sensor->named)
      {
        name = sensor->name;
      }
#endif
      ret = snprintf_P(output, len, PSTR("%02x%02x%02x%02x%02x%02x%02x%02x"
#ifdef ONEWIRE_NAMING_SUPPORT
                                         "\t%s"
#endif
                       ),
                       ow_global.current_rom.bytewise[0],
                       ow_global.current_rom.bytewise[1],
                       ow_global.current_rom.bytewise[2],
                       ow_global.current_rom.bytewise[3],
                       ow_global.current_rom.bytewise[4],
                       ow_global.current_rom.bytewise[5],
                       ow_global.current_rom.bytewise[6],
                       ow_global.current_rom.bytewise[7]
#ifdef ONEWIRE_NAMING_SUPPORT
                       , name
#endif
        );

      OW_DEBUG_LIST("generated %d bytes\n", ret);

      /* set return value that the parser has to be called again */
      if (ret > 0)
        ret = ECMD_AGAIN(ret);

      OW_DEBUG_LIST("returning %d\n", ret);
      return ECMD_FINAL(ret);

#ifdef ONEWIRE_DS2502_SUPPORT
    }