/**
 * Build this screen.
 */
void initIntervalSetScreen()
{
	//Hide the layer by default.
	layer_set_hidden(intervalLayer, true);

	//Set mode text
	setIntervalModeText = text_layer_create(GRect(0, 15, 144, 40));
	text_layer_set_text(setIntervalModeText, "Set Mode");
	text_layer_set_font(setIntervalModeText,
			fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
	text_layer_set_text_alignment(setIntervalModeText, GTextAlignmentCenter);
	layer_add_child(intervalLayer, text_layer_get_layer(setIntervalModeText));

	//# of layers text
	settingsTitle = text_layer_create(GRect(0, 40, 144, 40));
	text_layer_set_font(settingsTitle,
			fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
	text_layer_set_text_alignment(settingsTitle, GTextAlignmentCenter);
	text_layer_set_text(settingsTitle, "# of Intervals");
	layer_add_child(intervalLayer, text_layer_get_layer(settingsTitle));

	//Actual interval count text
	intervalCountString = text_layer_create(GRect(0, 80, 144, 50));
	text_layer_set_font(intervalCountString,
			fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
	text_layer_set_text_alignment(intervalCountString, GTextAlignmentCenter);
	layer_add_child(intervalLayer, text_layer_get_layer(intervalCountString));

	//Prefill the interval count with what it currently is
	setIntervalCountTxt(getIntervalCount());
} //End initIntervalCountScreen
/**
 * Sets how many interval timers to use.
 * Change can be positive or negative.
 * Change the values in the if statement to
 * increase the number of intervals you can have.
 */
void adjustIntervals(int8_t change)
{
	uint8_t intervalCount = getIntervalCount();
	if ((intervalCount + change) <= 10 && (intervalCount + change) >= 1)
	{
		intervalCount = intervalCount + change;
		//Update the screen.
		setIntervalCount(intervalCount);
		setIntervalCountTxt(intervalCount);

	}
} //End adjustIntervals
Exemplo n.º 3
0
void VibrationSensor2::run()
{
    // Take a reading.
    int lastReading = reading;
    Pin* pin = Esp8266::getInstance()->getPin(pinId);
    if (pin)
    {
        reading = pin->read();
    }

    // Each change shall be recorded as a discrete vibration.
    if (reading != lastReading)
    {
        vibrationCount++;
    }

    // Periodically update the server with the overall vibration state.
    if (updateTimer.isExpired())
    {
        // Send an update to the server.
        if (serverId != "")
        {
            SensorUpdateMsg message(getId(), state, ((millis() - stateChangeTime) / MILLISECONDS_PER_SECOND));
            message.address(getId(), serverId);
            MessageRouter::getInstance()->sendMessage(message);
        }

        updateTimer.start();
    }

    // Periodically calculate the overall vibration state.
    if (intervalTimer.isExpired())
    {
        // Increment the position in the circular queue.
        queuePosition++;
        if (queuePosition >= getIntervalCount())
        {
            // Wraparound.
            queuePosition = 0;
        }

        // Record the vibrating state for this interval.
        queue[queuePosition] = (vibrationCount > getVibrationThreshold()) ? VIBRATING : NOT_VIBRATING;

        // Calculate the total number of detected vibrations over all the observed intervals.
        int total = 0;
        for (int i = 0; i < getIntervalCount(); i++)
        {
            total += queue[i];
        }

        // Calculate the new overall vibration state.
        bool prevState = state;
        state = (total > (getIntervalCount() / 2));

#if 0
        String debugString = "";
        for (int i = 0; i < getIntervalCount(); i++)
        {
            debugString += "[" + String(queue[i]) + "]";
        }
        debugString += "\n";
        debugString += "vibration count = " + String(vibrationCount) + ", threshold = " + String(getVibrationThreshold());
        debugString += "\n";
        debugString += "total = " + String(total) + ", state = " + String(state);
        debugString += "\n";
        debugString += "\n";
        Logger::logDebug(debugString);
#endif

        // Respond to state changes.
        if (prevState != state)
        {
            // Send an update to the server.
            if (serverId != "")
            {
                SensorUpdateMsg message(getId(), state, 0);
                message.address(getId(), serverId);
                MessageRouter::getInstance()->sendMessage(message);
            }

            // Record the state change time.
            stateChangeTime = millis();
        }

        // Reset for the next interval.
        vibrationCount = 0;
        intervalTimer.start();
    }
}