hzyitc / armbian-onecloud

Armbian for onecloud. 玩客云用armbian
1.24k stars 239 forks source link

玩客云控制灯的颜色,源码奉上 #52

Closed hkbase closed 1 year ago

hkbase commented 2 years ago

将下面的代码保存为led.c 然后gcc led.c -o led 最后将编译后的led程序放入 /usr/bin 就可以运行以下命令,控制玩客云灯的显示颜色了,0是关闭,1是打开 led red 0 led blue 0 led green 1

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <string.h>

#define PLPM_BASE 0xc8100014

#define __IO volatile

void          *var_addr_satr=0;
unsigned int   var_addr_size=0;
unsigned int  *gpioaddr;
void          *gpio_base= 0;

int gpio_init(void) {
  int fd;
  unsigned int addr_start,addr_offset;
  unsigned int PageSize,PageMask;

  fd = open("/dev/mem",O_RDWR);
  if(fd < 0) {
    return -1;
  }

  PageSize = sysconf(_SC_PAGESIZE);
  PageMask = ~(PageSize-1);

  //printf("take PageSize=%d\n",PageSize);
  addr_start =  PLPM_BASE & PageMask ;
  addr_offset=  PLPM_BASE & ~PageMask;

  var_addr_size =  PageSize*2;
  var_addr_satr = (void*) mmap(0, var_addr_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr_start);

  if(var_addr_satr == MAP_FAILED) {
    return -1;
  }

  gpio_base = var_addr_satr;
  gpio_base += addr_offset;

  //printf("take var addr = 0x%8x\n",(unsigned int)var_addr_satr);
  //printf("make gpio_base = 0x%8x\n",(unsigned int)gpio_base);

  close(fd);
  return 0;
}

int gpio_deinit(void) {
  int fd;

  fd = open("/dev/mem",O_RDWR);
  if(fd < 0) {
    return -1;
  }

  if(munmap(var_addr_satr,var_addr_size) == 0) {
      //printf("remove var addr ok\n");
  } else {
    //printf("remove var addr erro\n");
  }

  close(fd);
  return 0;
}

void red_on(void) {
  unsigned int temp;
  temp = *(gpioaddr+4)|0x00040000;
  *(gpioaddr+4)=temp;
}
void red_off(void) {
  *(gpioaddr+4) = *(gpioaddr+4)&(~0x00040000);
}
void red_toggle(void) {
  unsigned char c;
  c = (*(gpioaddr+4)&0x00040000)>>18;
  if (c == 0) {
    red_on();
  } else {
    red_off();
  }
}

void green_on(void) {
  *(gpioaddr+4) = *(gpioaddr+4)|0x00080000;
}
void green_off(void) {
  *(gpioaddr+4) = *(gpioaddr+4)&(~0x00080000);
}
void green_toggle(void) {
  unsigned char c;
  c = (*(gpioaddr+4)&0x00080000)>>19;
  if (c == 0) {
    green_on();
  } else {
    green_off();
  }
}

void blue_on(void) {
  *(gpioaddr+4) = *(gpioaddr+4)|0x00100000;
}
void blue_off(void) {
  *(gpioaddr+4) = *(gpioaddr+4)&(~0x00100000);
}
void blue_toggle(void) {
  unsigned char c;
  c = (*(gpioaddr+4)&0x00100000)>>20;
  if (c == 0) {
    blue_on();
  } else {
    blue_off();
  }
}

unsigned char key_scan(void) {
  unsigned char key=0;
  key=((*(gpioaddr+5)&0x00000020)>>5);
  return key;
} 

int main(int argc,char * argv[]) {
  if (argc < 2 || argc > 4) {
    printf("led [red|green|blue] [0|1]\n");
    return 1;
  }

  unsigned int temp;
  if (gpio_init() != 0) {
    printf("gpio_init failed\n");
    return -1;
  }

  gpioaddr = (unsigned int *)gpio_base;
  // Turn off GPIOAO_4,GPIOAO_5 reuse 
  temp = *gpioaddr&(~0x01800066);
  *gpioaddr = temp;
  // Enable GPIOAO_2,GPIOAO_3,GPIOAO_4 output,GPIOAO_5 input
  temp = *(gpioaddr+4)&(~0x0000001c);
  *(gpioaddr+4) = temp;

  temp = *(gpioaddr+4)|0x00000020;
  *(gpioaddr+4) = temp;

  if (argc == 2) {
    if (strcmp((const char*)argv[1],"red")==0)
      red_toggle();

    if (strcmp((const char*)argv[1],"green")==0)
      green_toggle();

    if (strcmp((const char*)argv[1],"blue")==0)
      blue_toggle();
  }

  if (argc == 3) {
    if (strcmp((const char*)argv[1],"red")==0) {
      if (strcmp((const char*)argv[2],"0")==0) {
        red_off();
      } else {
        red_on();
      }
    }

    if (strcmp((const char*)argv[1],"green")==0) {
      if (strcmp((const char*)argv[2],"0")==0) {
        green_off();
      } else {
        green_on();
      }
    }

    if (strcmp((const char*)argv[1],"blue")==0) {
      if (strcmp((const char*)argv[2],"0")==0) {
        blue_off();
      } else {
        blue_on();
      }
    }
  }

  gpio_deinit( );
  return 0;
}
hzyitc commented 2 years ago

LED已经引出了啊。在/sys/class/leds/下。

KayXin commented 2 years ago

echo 0 > /sys/class/leds/onecloud\:blue\:alive/brightness echo 0 > /sys/class/leds/onecloud\:green\:alive/brightness echo 0 > /sys/class/leds/onecloud\:red\:alive/brightness

直接执行,可修改灯颜色 0为关闭, 1为开启

FrankLiangCN commented 2 years ago

LED已经引出了啊。在/sys/class/leds/下。

后续的固件能否在运行正常的时候默认关红灯,亮蓝灯? 最开始使用以为红灯是启动的指示,结果一直红灯

lanlanpy commented 2 years ago

LED已经引出了啊。在/sys/class/leds/下。

后续的固件能否在运行正常的时候默认关红灯,亮蓝灯? 最开始使用以为红灯是启动的指示,结果一直红灯

在 /etc/rc.local 加入以下代码就可以了 echo 1 > /sys/class/leds/onecloud:blue:alive/brightness echo 0 > /sys/class/leds/onecloud:green:alive/brightness echo 0 > /sys/class/leds/onecloud:red:alive/brightness

hzyitc commented 1 year ago

LED已经引出了啊。在/sys/class/leds/下。

后续的固件能否在运行正常的时候默认关红灯,亮蓝灯? 最开始使用以为红灯是启动的指示,结果一直红灯

改下这里就行。等有空我交个PR吧。

https://github.com/armbian/build/blob/95c829f9e66451f2bff6f42ab3e61d211212c905/patch/kernel/archive/meson-6.1/board_onecloud/0001-add-dts.patch#L83-L105

sys885 commented 1 year ago

看了上面大神们的回复,我写了个脚本,直接放armbian的/opt目录下并用nohup命令后台执行即可看到LED灯颜色交替闪烁的样子了。

!/bin/bash

设定闪烁时间和灯珠亮度

FLASH_TIME=0.5 BRIGHTNESS=1

循环执行LED灯效果

while true do

红色灯珠亮,绿色和蓝色灯珠灭

echo $BRIGHTNESS > /sys/class/leds/onecloud:red:alive/brightness echo 0 > /sys/class/leds/onecloud:green:alive/brightness echo 0 > /sys/class/leds/onecloud:blue:alive/brightness sleep $FLASH_TIME

红色和绿色灯珠亮,蓝色灯珠灭

echo $BRIGHTNESS > /sys/class/leds/onecloud:red:alive/brightness echo $BRIGHTNESS > /sys/class/leds/onecloud:green:alive/brightness echo 0 > /sys/class/leds/onecloud:blue:alive/brightness sleep $FLASH_TIME

红色和蓝色灯珠亮,绿色灯珠灭

echo $BRIGHTNESS > /sys/class/leds/onecloud:red:alive/brightness echo 0 > /sys/class/leds/onecloud:green:alive/brightness echo $BRIGHTNESS > /sys/class/leds/onecloud:blue:alive/brightness sleep $FLASH_TIME

绿色灯珠亮,红色和蓝色灯珠灭

echo 0 > /sys/class/leds/onecloud:red:alive/brightness echo $BRIGHTNESS > /sys/class/leds/onecloud:green:alive/brightness echo 0 > /sys/class/leds/onecloud:blue:alive/brightness sleep $FLASH_TIME

蓝色灯珠亮,红色和绿色灯珠灭

echo 0 > /sys/class/leds/onecloud:red:alive/brightness echo 0 > /sys/class/leds/onecloud:green:alive/brightness echo $BRIGHTNESS > /sys/class/leds/onecloud:blue:alive/brightness sleep $FLASH_TIME done

Bet5521 commented 1 year ago

之前刷的5.88的版本,那个开机蓝色,联网绿色,机器在忙的时候红闪,越忙越闪的快。后来刷的版本,要么就一个颜色分不出来,要么就是插网线白色。 大佬们有知道是咋弄的吗?