All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.diozero.internal.SoftwarePwmOutputDevice Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
package com.diozero.internal;

/*
 * #%L
 * Organisation: diozero
 * Project:      Device I/O Zero - Core
 * Filename:     SoftwarePwmOutputDevice.java  
 * 
 * This file is part of the diozero project. More information about this project
 * can be found at http://www.diozero.com/
 * %%
 * Copyright (C) 2016 - 2021 diozero
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import java.util.concurrent.atomic.AtomicBoolean;

import org.tinylog.Logger;

import com.diozero.internal.spi.AbstractDevice;
import com.diozero.internal.spi.DeviceFactoryInterface;
import com.diozero.internal.spi.GpioDigitalOutputDeviceInterface;
import com.diozero.internal.spi.PwmOutputDeviceInterface;
import com.diozero.util.DiozeroScheduler;
import com.diozero.util.RangeUtil;
import com.diozero.util.SleepUtil;

/**
 * Generate a very poor approximation of a PWM signal - use at your own risk!
 * All timing is in milliseconds hence it is strongly recommend to use a
 * frequency of 50Hz to minimise integer rounding errors.
 */
public class SoftwarePwmOutputDevice extends AbstractDevice implements PwmOutputDeviceInterface, Runnable {
	private GpioDigitalOutputDeviceInterface digitalOutputDevice;
	private AtomicBoolean running;
	private int periodMs;
	private int dutyMs;

	public SoftwarePwmOutputDevice(String key, DeviceFactoryInterface deviceFactory,
			GpioDigitalOutputDeviceInterface digitalOutputDevice, int frequency, float initialValue) {
		super(key, deviceFactory);

		Logger.warn("Hardware PWM not available for device {}, reverting to software", key);

		this.digitalOutputDevice = digitalOutputDevice;
		running = new AtomicBoolean();

		periodMs = 1_000 / frequency;
		setValue(initialValue);
		start();
	}

	public void start() {
		if (!running.getAndSet(true)) {
			DiozeroScheduler.getNonDaemonInstance().execute(this);
		}
	}

	public void stop() {
		running.set(false);
	}

	@Override
	public void run() {
		while (running.get()) {
			if (dutyMs == 0) {
				// Fully off
				digitalOutputDevice.setValue(false);
			} else if (dutyMs == periodMs) {
				digitalOutputDevice.setValue(true);
			} else {
				digitalOutputDevice.setValue(true);
				SleepUtil.sleepMillis(dutyMs);
				digitalOutputDevice.setValue(false);
			}
			SleepUtil.sleepMillis(periodMs - dutyMs);
		}
	}

	@Override
	protected void closeDevice() {
		Logger.trace("closeDevice() {}", getKey());
		stop();
		if (digitalOutputDevice != null) {
			digitalOutputDevice.close();
			digitalOutputDevice = null;
		}
	}

	@Override
	public float getValue() {
		return dutyMs / (float) periodMs;
	}

	@Override
	public void setValue(float value) {
		// Constrain to 0..1
		dutyMs = Math.round(RangeUtil.constrain(value, 0, 1) * periodMs);
	}

	@Override
	public int getGpio() {
		return digitalOutputDevice.getGpio();
	}

	@Override
	public int getPwmNum() {
		return digitalOutputDevice.getGpio();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy