Program that uses the QuickStart Library to interface a SmartMesh IP mote: Connects to the default network and starts publishing a random walk value every 5 seconds.

Dependencies:   mbed millis

Fork of QSL_SimplePublish by Jon-Håkon Bøe Røli

QSL SimplePublish

SmartMesh IP QuickStart Library

dn_time.cpp

Committer:
jhbr
Date:
2016-09-07
Revision:
3:fb2c485306d1
Parent:
0:d3f5fdf2e6da
Child:
4:0285bcbbc855

File content as of revision 3:fb2c485306d1:

/*
Copyright (c) 2016, Dust Networks. All rights reserved.

Port of the time module to the NUCLEO-L053R8.

\license See attached DN_LICENSE.txt.
*/

#include "dn_time.h"
#include "mbed.h"
#include "dn_debug.h"

//=========================== variables =======================================

static Timer myTimer;
static Timeout wakeUp;
static volatile bool tired;

//=========================== prototypes ======================================

static void awaken(void);

//=========================== public ==========================================

uint32_t dn_time_ms(void)
{
	static bool started = FALSE;
	time_t seconds = time(NULL);
	uint32_t ms;
	
	if (!started)
	{
		myTimer.start();
		started = TRUE;
	}
	/*
	mbed timers are based on 32-bit int microsecond counters, resulting in
	read_ms() to wrap around long before the max value of a uint32_t.
	Therefore, the seconds from time() is used to fully extend the range, and
	we cast read_ms() to an uint32_t and only use the millisecond part.
	 */
	
	ms = (uint32_t)seconds*1000 + (uint32_t)(myTimer.read_ms()) % 1000;
	
	return ms;
}

void dn_sleep_ms(uint32_t milliseconds)
{
	 //wait_ms(milliseconds);
	 //return;
	 tired = TRUE;
	 wakeUp.attach_us(&awaken, milliseconds*1000);
	 while (tired) sleep();
}

//=========================== private =========================================

static void awaken(void)
{
	tired = FALSE;	
}

//=========================== helpers =========================================