原创

使用Pi4j点亮第一盏LED灯


以下是代码



@Controller
public class TestController {

    private static final int PIN_LED = 21; // BCM引脚点亮LED灯

    @RequestMapping("/")
    @ResponseBody
    public String test() {

        System.out.println("连接成功!" + new Date());
        return "测试成功呀ya!re" + new Date();
    }

    @RequestMapping(path = {"/led"})
    @ResponseBody
    public String getParamDemo1() throws InterruptedException {

        final Console console = new Console();

        console.title("<-- The Pi4J Project Of Raspberry -->", "点亮一盏LED灯");
        Context pi4j = Pi4J.newAutoContext();

        DigitalOutputConfigBuilder ledConfig = DigitalOutput.newConfigBuilder(pi4j)
                .id("led")
                .name("LED shine")
                .address(PIN_LED)
                .shutdown(DigitalState.LOW)
                .initial(DigitalState.LOW)
                .provider("pigpio-digital-output");
        DigitalOutput led = pi4j.create(ledConfig);

        for (int i = 0; i < 100; i++) {

            led.high();
            System.out.println("点亮成功!");
            Thread.sleep(400);
            led.low();
            Thread.sleep(400);
        }
        pi4j.shutdown();
        System.out.println("------test success!-------");

        return "success led shine!";
    }
}

java EE
教程
Pi4j
  • 作者:北斗七点半联系作者
  • 发表时间:2022-01-05 23:55
  • 版权声明:禁止转载
  • 非公众号转发
  • 评论