void led(rt_uint32_t led, rt_uint32_t value) { /* init led configuration if it's not inited. */ if (!led_inited) { rt_hw_led_init(); led_inited = 1; } if ( led == 0 ) { /* set led status */ switch (value) { case 0: rt_hw_led_off(0); break; case 1: rt_hw_led_on(0); break; default: break; } } if ( led == 1 ) { /* set led status */ switch (value) { case 0: rt_hw_led_off(1); break; case 1: rt_hw_led_on(1); break; default: break; } } if ( led == 2 ) { /* set led status */ switch (value) { case 0: rt_hw_led_off(2); break; case 1: rt_hw_led_on(2); break; default: break; } } }
static void led_thread_entry(void* parameter) { rt_uint8_t ledStatus; rt_hw_led_init(); ledData_mq = rt_mq_create("LEDMQ",1,2,RT_IPC_FLAG_FIFO); while (1) { if(rt_mq_recv(ledData_mq,&ledStatus,1,RT_WAITING_FOREVER) == RT_EOK) { if(ledStatus &0x10) { rt_hw_led_on(4); } else { rt_hw_led_off(4); } if(ledStatus &0x08) { rt_hw_led_on(3); } else { rt_hw_led_off(3); } if(ledStatus &0x04) { rt_hw_led_on(2); } else { rt_hw_led_off(2); } if(ledStatus &0x2) { rt_hw_led_on(1); } else { rt_hw_led_off(1); } if(ledStatus &0x01) { rt_hw_led_on(0); } else { rt_hw_led_off(0); } } } }
void rt_led_thread_entry(void *parameter) { while (1) { /* light on leds for one second */ rt_hw_led_on(LED2|LED3); rt_hw_led_off(LED1|LED4); rt_thread_delay(100); /* light off leds for one second */ rt_hw_led_off(LED2|LED3); rt_hw_led_on(LED1|LED4); rt_thread_delay(100); } }
void led(rt_uint32_t led, rt_uint32_t value) { int i; /* init led configuration if it's not inited. */ if (!led_inited) { rt_hw_led_init(); led_inited = 1; } for(i = 0 ; i < 4 ; i ++) { if ( led == i ) { /* set led status */ switch (value) { case 0: rt_hw_led_off(i); break; case 1: rt_hw_led_on(i); break; default: break; } } } }
/** * @brief Configures the necessary pins for an array of LEDs * * @param None. * * This function configures a GPIO to drive an LED in a low-power way. * * @return None. */ int rt_hw_led_init(void) { #ifdef RT_USING_PIN #if defined(RT_USING_LED0) /* config led */ rt_pin_mode(AM_GPIO_LED0, PIN_MODE_OUTPUT); /* turns off the led */ rt_hw_led_off(0); #endif /* RT_USING_LED0 */ #if defined(RT_USING_LED1) /* config led */ rt_pin_mode(AM_GPIO_LED1, PIN_MODE_OUTPUT); /* turns off the led */ rt_hw_led_off(1); #endif /* RT_USING_LED1 */ #if defined(RT_USING_LED2) /* config led */ rt_pin_mode(AM_GPIO_LED2, PIN_MODE_OUTPUT); /* turns off the led */ rt_hw_led_off(2); #endif /* RT_USING_LED0 */ #if defined(RT_USING_LED3) /* config led */ rt_pin_mode(AM_GPIO_LED3, PIN_MODE_OUTPUT); /* turns off the led */ rt_hw_led_off(3); #endif /* RT_USING_LED1 */ #endif rt_kprintf("led_init!\n"); return 0; }
static void timeout_led1(void* parameter) { if(ledstart & LEDB1) { rt_hw_led_on(0); ledstart &= ~LEDB1; } else { rt_hw_led_off(0); ledstart |= LEDB1; } }
void rt_hw_led_init(void) { SIM->SCGC5 |= (1 << SIM_SCGC5_PORTB_SHIFT); SIM->SCGC5 |= (1 << SIM_SCGC5_PORTE_SHIFT); PORTB->PCR[21] &= ~PORT_PCR_MUX_MASK; PORTB->PCR[21] |= PORT_PCR_MUX(1); //PTB21 is GPIO pin PORTB->PCR[22] &= ~PORT_PCR_MUX_MASK; PORTB->PCR[22] |= PORT_PCR_MUX(1); //PTB22 is GPIO pin PORTE->PCR[26] &= ~PORT_PCR_MUX_MASK; PORTE->PCR[26] |= PORT_PCR_MUX(1); //PTE26 is GPIO pin /* Switch LEDs off and enable output*/ PTB->PDDR |= GPIO_PDDR_PDD(led_mask[1] | led_mask[0]); PTE->PDDR |= GPIO_PDDR_PDD(led_mask[2]); rt_hw_led_off(LED_RED); rt_hw_led_off(LED_GREEN); rt_hw_led_off(LED_BLUE); }
static void timeout_led2(void* parameter) { if(ledstart & LEDB2) { rt_hw_led_on(1); ledstart &= ~LEDB2; } else { rt_hw_led_off(1); ledstart |= LEDB2; } }
static void timeout_led3(void* parameter) { if(ledstart & LEDB3) { rt_hw_led_on(2); ledstart &= ~LEDB3; } else { rt_hw_led_off(2); ledstart |= LEDB3; } }
static void timeout_led4(void* parameter) { if(ledstart & LEDB4) { rt_hw_led_on(3); ledstart &= ~LEDB4; } else { rt_hw_led_off(3); ledstart |= LEDB4; } }
static void rt_thread_entry_led1(void* parameter) { int n = 0; // int stage=0; rt_uint32_t offset=0; // int i=2000; // int delta = 50; rt_hw_led_init(); dac_dma(); /*SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK; DAC0->C0 |= DAC_C0_DACEN_MASK |DAC_C0_DACRFS_MASK |DAC_C0_DACTRGSEL_MASK; DAC0->DAT[0].DATL = stage & 0xFF; DAC0->DAT[0].DATH = stage >> 8; DAC0->C0 |= DAC_C0_DACSWTRG_MASK; */ while (1) { //rt_kprintf("LED\t%d\tis shining\r\n",n); /*if(i==2000) delta = 50; else if(i==4000) delta = -50; i += delta; */ rt_hw_led_on(n); rt_thread_delay(RT_TICK_PER_SECOND/2); /*rt_kprintf("i is %d\r\n",i); DAC0->DAT[0].DATL = i & 0xFF; DAC0->DAT[0].DATH = i >> 8; DAC0->C0 |= DAC_C0_DACSWTRG_MASK;*/ if(!Mem_Check(offset)) //rt_kprintf("offset %x test ok\r\n",offset); //else rt_kprintf("offset %x test failed\r\n",offset); offset++; if(offset==0x10000) offset=0; rt_hw_led_off(n); rt_thread_delay(RT_TICK_PER_SECOND/2); n++; //stage=stage+100000; if (n == (LED_MAX+1)) n = 0; } }
void set_led(rt_uint32_t led, rt_uint32_t state) { /* set led status */ switch (state) { case 0: rt_hw_led_off(led); break; case 1: rt_hw_led_on(led); break; default: break; } }
static void rt_thread_entry_led1(void* parameter) { int n = 0; rt_hw_led_init(); while (1) { //rt_kprintf("LED\t%d\tis shining\r\n",n); rt_hw_led_on(n); rt_thread_delay(RT_TICK_PER_SECOND/2); rt_hw_led_off(n); rt_thread_delay(RT_TICK_PER_SECOND/2); n++; if (n == LED_MAX) n = 0; } }
void rt_thread_entry_led2(void* parameter) { unsigned int count=0; while (1) { /* led2 on */ #ifndef RT_USING_FINSH rt_kprintf("led2 on,count : %d\r\n",count); #endif count++; rt_hw_led_on(2); rt_thread_delay(RT_TICK_PER_SECOND); /* led2 off */ #ifndef RT_USING_FINSH rt_kprintf("led2 off\r\n"); #endif rt_hw_led_off(2); rt_thread_delay(RT_TICK_PER_SECOND); } }
void rt_hw_led_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(led1_rcc|led2_rcc|led3_rcc|led4_rcc,ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = led1_pin|led2_pin|led3_pin|led4_pin; GPIO_Init(led1_gpio, &GPIO_InitStructure); rt_timer_init(&timer1,"timer1",timeout_led1,RT_NULL,100,RT_TIMER_FLAG_PERIODIC); // rt_timer_init(&timer2,"timer2",timeout_led2,RT_NULL,100,RT_TIMER_FLAG_PERIODIC); // rt_timer_init(&timer3,"timer3",timeout_led3,RT_NULL,100,RT_TIMER_FLAG_PERIODIC); // rt_timer_init(&timer4,"timer4",timeout_led4,RT_NULL,100,RT_TIMER_FLAG_PERIODIC); //turn off all the led rt_hw_led_off(99); }
static void rt_thread_entry_led1(void* parameter) { unsigned int count=0; while (1) { /* led1 on */ #ifndef RT_USING_FINSH rt_kprintf("led1 on,count : %d\r\n",count); #endif count++; rt_hw_led_on(1); /* sleep 0.5 second and switch to other thread */ rt_thread_delay(RT_TICK_PER_SECOND/2); /* led1 off */ #ifndef RT_USING_FINSH rt_kprintf("led1 off\r\n"); #endif rt_hw_led_off(1); rt_thread_delay(RT_TICK_PER_SECOND/2); } }
uint8_t check_power(uint8_t channel) { int16_t vol; int power_type; if(channel!=2) { //1 read the voltage from adc vol=read_adc(ADC_Channel_6+channel); //2 check power battery type if(channel==0) { //switch i2c to slot A GPIO_SetBits(BATS_I2C_SEL_PORT, BATS_I2C_SEL_PIN); } else { //switch i2c to slot B GPIO_ResetBits(BATS_I2C_SEL_PORT, BATS_I2C_SEL_PIN); } power_type=check_battery_li(); if(channel==0) { if(power_type==1) { rt_hw_led_off(0); rt_hw_led_off(1); } else { rt_hw_led_on(0); rt_hw_led_on(1); } } else { if(power_type==1) { rt_hw_led_off(2); rt_hw_led_off(3); } else { rt_hw_led_on(2); rt_hw_led_on(3); } } if(vol>pm.minVoltage && vol<pm.maxVoltage) { if(pm.ps[channel].available==0) { if(pm.ps[channel].type==POWER_UNKNOWN) pm.ps[channel].available=1; } } else { pm.ps[channel].available=0; pm.ps[channel].type=POWER_UNKNOWN; } if(power_type) pm.ps[channel].type=POWER_BATTERY_LI; else { if(vol>INVALID_ADC_VALUE) { pm.ps[channel].type=POWER_ADAPTER; } else { pm.ps[channel].type=POWER_UNKNOWN; pm.ps[channel].available=0; } } pm.ps[channel].voltage=vol; if(pm.ps[channel].type==POWER_BATTERY_LI) read_batt_info(channel); if(pm.ps[channel].type==POWER_BATTERY_LI) DEBUG("channel %s , power_type is Smart Battery\r\n",(channel==0)?"BATS_A":"BATS_B"); if(pm.ps[channel].type==POWER_ADAPTER) DEBUG("channel %s , power_type is Charger\r\n",(channel==0)?"BATS_A":"BATS_B"); if(pm.ps[channel].type==POWER_UNKNOWN) DEBUG("channel %s , power_type is Not Connect\r\n",(channel==0)?"BATS_A":"BATS_B"); } else { vol=read_adc(ADC_Channel_6+channel); if(vol>pm.minVoltage && vol<pm.maxVoltage) { if(pm.ps[channel].available==0) { if(pm.ps[channel].type==POWER_UNKNOWN) pm.ps[channel].available=1; } } else { pm.ps[channel].available=0; } if(vol<INVALID_ADC_VALUE) pm.ps[channel].type=POWER_UNKNOWN; else pm.ps[channel].type=POWER_BATTERY_DRY; pm.ps[channel].voltage=vol; } return 0; }
void httpclient(void) { char *recv_data; int sock, bytes_received; struct hostent *yeelink_host; struct in_addr yeelink_ipaddr; struct sockaddr_in yeelink_sockaddr; recv_data = rt_malloc(1024); if (recv_data == RT_NULL) { rt_kprintf("No memory\r\n"); return; } // 第一步 DNS地址解析 rt_kprintf("calling gethostbyname with: %s\r\n", YEELINK_HOSTNAME); yeelink_host = gethostbyname(YEELINK_HOSTNAME); yeelink_ipaddr.s_addr = *(unsigned long *) yeelink_host->h_addr_list[0]; rt_kprintf("Yeelink IP Address:%s\r\n" , inet_ntoa(yeelink_ipaddr)); yeelink_sockaddr.sin_family = AF_INET; yeelink_sockaddr.sin_port = htons(80); yeelink_sockaddr.sin_addr = yeelink_ipaddr; rt_memset(&(yeelink_sockaddr.sin_zero), 0, sizeof(yeelink_sockaddr.sin_zero)); // while(1) // { // 第二步 创建套接字 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { rt_kprintf("Socket error\n"); rt_free(recv_data); return; } // 第三步 连接yeelink if (connect(sock, (struct sockaddr *)&yeelink_sockaddr, sizeof(struct sockaddr)) == -1) { rt_kprintf("Connect fail!\n"); lwip_close(sock); rt_free(recv_data); return; } // 第4步 发送Http请求 send(sock,send_data,strlen(send_data), 0); // 第5步 获得Http响应 bytes_received = recv(sock, recv_data, 1024 - 1, 0); recv_data[bytes_received] = '\0'; // 响应内容为 {"timestamp":"2013-11-19T08:50:11","value":1} // 截取“value”之后的内容 char* actuator_info = rt_strstr( recv_data , "\"value\""); int offset = rt_strlen("\"value\":"); char actuator_status = *(actuator_info + offset); rt_kprintf("actuator status :%c\r\n",actuator_status); // 获得开关状态,并设置LED指示灯 (actuator_status == '1')?rt_hw_led_on(0):rt_hw_led_off(0); rt_memset(recv_data , 0 , sizeof(recv_data)); // 关闭套接字 closesocket(sock); // 释放recv_data rt_free(recv_data); // // 延时5S之后重新连接 // rt_thread_delay( RT_TICK_PER_SECOND * 5 ); // } }