int main( int argc, char **argv ) { int preferredTurn = 1; char turnrate = 0; char speed = 0x3f; unsigned short IR[8]={0x50,0x50,0x50,0x50,0x50,0x50,0x50,0x50}, IR_old[8]; char left; char right; unsigned short Values[8]; unsigned char i; for ( ; ; ) { ReadAllIR(1, Values); for(i=0; i<8; i++){ IR_old[i] = IR[i]; IR[i] = Values[i]; if( (IR[i] < 0x30) | (IR[i] > 0x3FF) ) IR[i] = IR_old[i]; } // printf("%X, %X, %X, %X, %X, %X, %X, %X \n", IR[0], IR[1], IR[2], IR[3], IR[4], IR[5],IR[6],IR[7] ); if(IR[L16] >= MIN_DIST | IR[R16] >= MIN_DIST | IR[L40] >= MIN_DIST | IR[R40] >= MIN_DIST | IR[L150] >= MIN_DIST | IR[R150] >= MIN_DIST) SetLedColor(0, 0xff,0x01,0x01); else SetLedColor(0, 0x01,0xff,0x01); // Only react to objects closer than 0.4m off to the sides left = MAX( MIN_DIST, IR[L16] ); left += MAX( MIN_DIST, IR[L40] ); right = MAX( MIN_DIST, IR[R16] ); right += MAX( MIN_DIST, IR[R40] ); // Turn into the more open area if( left < right ) { turnrate = 0x10; // turn 20 degrees per second preferredTurn = -1; } else if ( left > right ) { turnrate = 0x10; preferredTurn = 1; } else turnrate = 0; SetSpeed( 1, (speed - turnrate), (speed + turnrate) ); } return 0; }
////////////////////////////////////////////////////////////////////////////// // Breathing LED void BreathingLed() { #define brightness_step 20 #define brightness_max_val 230 #define brightness_min_val 20 static uint8_t u8Brightness = 0; static int istep = brightness_step; static uint16_t u16Color = 0xfff; static uint8_t u8Loop = 0; Delay(50); //SetBrightness(u8Brightness); SetLedColor(u16Color ,u8Brightness); if(u8Brightness > brightness_max_val) istep = -brightness_step; else if(u8Brightness < brightness_min_val) { istep = brightness_step; u8Loop++; } u8Brightness = ((int)u8Brightness + istep); switch(u8Loop){ case 0: u16Color = 0xf; break; case 1: u16Color = 0xff0; break; case 2: u16Color = 0xf00; break; case 3: u16Color = 0xf0f; break; case 4: u16Color = 0x0ff; break; case 5: u16Color = 0xf0; break; case 6: u16Color = 0xfff; break; default: u8Loop = 0; } }
static void Draw(void) { int i; systime_t current = chVTGetSystemTime(); /* clear buffer */ memset(display.pixels, 0, sizeof(struct Color) * LEDCOUNT); if (effects.p_next != NULL) { EffectUpdate(effects.p_next, 0, 0, current, &display); } /* render volume for a 5s */ if (showVolume == true) { startVolumeEffect = current; showVolume = false; effVolume_cfg.volume = newVolume; showVolumeEffect = true; if (chVTTimeElapsedSinceX(startVolumeEffect) > MS2ST(15000)) { EffectReset(&effVolume, 0, 0, current); } } if (showVolumeEffect == true) { if (chVTTimeElapsedSinceX(startVolumeEffect) <= MS2ST(5000)) { EffectUpdate(&effVolume, 0, 0, current, &display); } else { showVolumeEffect = false; } } //draw buttons if (showControls == true) { EffectUpdate(&effButtons, 0, 0, current, &display); } for (i = 0; i < LEDCOUNT; i++) { SetLedColor(i, &display.pixels[i]); } SetUpdateLed(); }
/**************************************************************************** * 名 称:void ChangeCarStatus(CAR_STATUS cs) * 功 能:更改小车的状态 * 参 数:cs 要变更的状态 * 反 回:无 * 说 明: ****************************************************************************/ void ChangeCarStatus(CAR_STATUS cs) { switch(cs) { case CAR_STATUS_FAULT://目前用于磁传感通信出错 { CarData->ManualCmd = CAR_ACTION_URGENT_STOP; SetLedColor(RED_GRN); Speak(SPEAK_FAULT,0); break; } case CAR_STATUS_OUT_ROUTE://出轨 { CarData->ManualCmd = CAR_ACTION_URGENT_STOP; SetLedColor(RED_GRN); Speak(SPEAK_OUT_ROUTE,1); break; } case CAR_STATUS_ROUTE_BLOCK://红外近 { CarData->ManualCmd = CAR_ACTION_STOP; SetLedColor(BLU); Speak(SPEAK_ROUTE_BLOCK,1); break; } case CAR_STATUS_URGENT_STOP://紧急停车 { CarData->ManualCmd = CAR_ACTION_URGENT_STOP; SetLedColor(RED_GRN); Speak(SPEAK_URGENT_STOP,1); break; } case CAR_STATUS_ROUTE_FBLOCK://红外远 { CarData->ManualCmd = CAR_ACTION_FORWARD; SetLedColor(GRN); Speak(SPEAK_BE_CAREFUL,1); break; } case CAR_STATUS_BATTERY_LOW://电量低 { Speak(SPEAK_BATTERY_LOW,1); break; } case CAR_STATUS_UP://开机 { Speak(SPEAK_WELCOME,1); break; } case CAR_STATUS_MANUAL://改为手动 { SetLedColor(GRN_BLU); break; } case CAR_STATUS_STOP://停车 { //CarData->ManualCmd = CAR_ACTION_STOP; CarData->ManualCmd = CAR_ACTION_BREAK; SetLedColor(LED_COLOR_BLUE); Speak(SPEAK_MUTE,1); break; } case CAR_STATUS_NORMAL://正常 { CarData->ManualCmd = CAR_ACTION_FORWARD; if(Change_Flag.Ext.BITS.BatteryLow) //电量低 { //CarData->NowStatus=CAR_STATUS_BATTERY_LOW; Speak(SPEAK_BATTERY_LOW,1); } else { SetLedColor(GRN); Speak(SPEAK_MUSIC,1); } break; } } }
/**************************************************************************** * 名 称:void RecvDriverReadRFID(uint8_t *data) * 功 能:收到RFID卡号数据处理 * 参 数:data 为指向数据区域的地址 * 反 回:无 * 说 明: ****************************************************************************/ void RecvDriverReadRFIDProcess(uint8_t *data) { uint16_t cardNum = 0; rf=(RFID *)data; cardNum = (rf->CardIDMSB << 8) + (rf->CardIDLSB); //卡号 CarData->LastStation=(rf->CardIDMSB<<8) | rf->CardIDLSB; //上一站站点 if((CarData->NowStatus!=CAR_STATUS_FAULT) && (CarData->NowStatus!=CAR_STATUS_OUT_ROUTE) && (CarData->NowStatus!=CAR_STATUS_STOP) && (CarData->NowStatus!=CAR_STATUS_URGENT_STOP) && (CarData->NowStatus!=CAR_STATUS_UP)) { SetLedColor(LED_COLOR_RED); switch(rf->CardType) { case CARD_TYPE_LIFT: { CarData->NowStatus = CAR_STATUS_STOP; if(cardNum == LIFTER_UP) //升降杆上升 { LifterUp(); } else if(cardNum == LIFTER_DOWN) //升降杆下降 { LifterDown(); } break; } case CARD_TYPE_MUTE: { if(rf->CardIDLSB) { SetSpeakSwitch(SPEAK_SWITCH_ON); } else { SetSpeakSwitch(SPEAK_SWITCH_OFF); } break; } case CARD_TYPE_SIGNAL: { break; } case CARD_TYPE_SITE: { CarData->NowStatus = CAR_STATUS_STOP; if(CarData->LastStation==CarData->Destination) { CarData->Destination=0; CarData->NowStatus = CAR_STATUS_STOP; Speak(SPEAK_AGV_ARRIV,0); } else { RouteFindProcess(rf); } break; } case CARD_TYPE_SPEED: { if(cardNum == SPEED_UP) //加速 { //mp.CurrSetSpeed += 1; } else if(cardNum == SPEED_DOWN) //减速下降 { //mp.CurrSetSpeed -= 1; } break; } case CARD_TYPE_TURN: { RouteFindProcess(rf); break; } default: { break; } } } }
int main() { int i; HTS221 hts221; pc.baud(115200); void hts221_init(void); // Set LED to RED until init finishes SetLedColor(0x1); pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r"); pc.printf(GRN "Initialize the HTS221\n\r"); i = hts221.begin(); if( i ) pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i); else pc.printf(RED "HTS221 NOT DETECTED!!\n\r"); printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); printf("Humid is: %02d %%\n\r",hts221.readHumidity()); sensors_init(); read_sensors(); // Initialize the modem printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n"); do { i=mdm_init(); if (!i) { pc.printf(RED "Modem initialization failed!" DEF "\n"); } } while (!i); //Software init software_init_mdm(); // Resolve URL to IP address to connect to resolve_mdm(); //Create a 1ms timer tick function: OneMsTicker.attach(OneMsFunction, 0.001f) ; iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; // Open the socket (connect to the server) sockopen_mdm(); // Set LED BLUE for partial init SetLedColor(0x4); // Send and receive data perpetually while(1) { static unsigned ledOnce = 0; if (bTimerExpiredFlag) { bTimerExpiredFlag = false; sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature())); sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity()); read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor char modem_string[512]; GenerateModemString(&modem_string[0]); printf(BLU "Sending to modem : %s" DEF "\n", modem_string); sockwrite_mdm(modem_string); sockread_mdm(&MySocketData, 1024, 20); // If any non-zero response from server, make it GREEN one-time // then the actual FLOW responses will set the color. if ((!ledOnce) && (MySocketData.length() > 0)) { ledOnce = 1; SetLedColor(0x2); } printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]); char * myJsonResponse; if (extract_JSON(&MySocketData[0], &myJsonResponse[0])) { printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]); parse_JSON(&myJsonResponse[0]); } else { printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted } } //bTimerExpiredFlag } //forever loop }
// parse end json bool parse_end_JSON(char* json_string) { char* beginquote; char token[] = "\"progress\":\""; beginquote = strstr(json_string, token ); if ((beginquote != 0)) { char cLedColor = beginquote[strlen(token)]; printf(GRN "LED Found : %c" DEF "\r\n", cLedColor); switch(cLedColor) { case 'O': { //Off SetLedColor(0); break; } case 'R': { //Red SetLedColor(1); break; } case 'G': { //Green SetLedColor(2); break; } case 'Y': { //Yellow SetLedColor(3); break; } case 'B': { //Blue SetLedColor(4); break; } case 'M': { //Magenta SetLedColor(5); break; } case 'T': { //Turquoise SetLedColor(6); break; } case 'W': { //White SetLedColor(7); break; } default: { break; } } //switch(cLedColor) return true; } else { return false; } } //parse_JSON
int main(void) { if (SysTick_Config(SystemCoreClock / 1000)) { // Capture error while (1); } USART_Config(); USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); /* // Output a message on Hyperterminal using printf function printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r"); // Loop until the end of transmission // The software must wait until TC=1. The TC flag remains cleared during all data // transfers and it is set by hardware at the last frame’s end of transmission while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET) {} //*/ /* Initialize Leds mounted on STM32F030-Discovery */ STM_EVAL_LEDInit(LED3); // LED3 for Stripe 1 STM_EVAL_LEDInit(LED4); // LED3 for Stripe 2 STM_EVAL_LEDInit(LED5); // LED3 for Stripe 3 // Initialize the timer for the LED stripe // TIM3 for Stripe 1 // TIM16 for Stripe 2 // TIM17 for Stripe 3 TIM_Config(); // Initialize the LED stripe // Send the 1st packet to the driver IC to assign the address and color LEDStripe_init(); /* Infinite loop */ extern uint8_t g_Red, g_Blue, g_Green, g_Brightness; while(1) { switch(g_CurMode) { case NONE_MODE: break; case GAMING: break; case BREATHING: BreathingLed(); break; case FLASHING: //BlinkingLed(); WaveLED(); break; case SHINING: MarqueeLed(); break; case STACK: StackLED(); break; case WAVE: //LoopLED(); WaveLED(); break; case RAINBOW: break; case RANDOM: break; case STRIPE: SetStripeLED(StripeIndex, g_u16Color, g_Brightness); g_CurMode = NONE_MODE; break; case CUSTOM: SetLedColor(g_u16Color, g_Brightness); g_CurMode = NONE_MODE; break; case CUSTOM_SINGLE: SetSingleLED(LEDIndex, g_u16Color, g_Brightness); g_CurMode = NONE_MODE; break; } } }
void LoopLED() { Delay(100); SetLedColor(0,0); static uint8_t myindex = 0; if(myindex > 1){ while(TIM17->CR1 & TIM_CR1_CEN); Led_data3 = 0x1 << 20 | 0xff << 12 | 0xf0; next_state3 = INTERVAL; TIM_Cmd(TIM17, ENABLE); } if(myindex > 2){ while(TIM17->CR1 & TIM_CR1_CEN); Led_data3 = 0x0 << 20 | 0xff << 12 | 0xf0; next_state3 = INTERVAL; TIM_Cmd(TIM17, ENABLE); } if(myindex > 3) { while(TIM16->CR1 & TIM_CR1_CEN); Led_data2 = 0x2 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } if(myindex > 4) { while(TIM16->CR1 & TIM_CR1_CEN); Led_data2 = 0x1 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } if(myindex > 5) { while(TIM16->CR1 & TIM_CR1_CEN); Led_data2 = 0x0 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } if(myindex > 6) { while(TIM3->CR1 & TIM_CR1_CEN); Led_data = 0x2 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } if(myindex > 7){ while(TIM3->CR1 & TIM_CR1_CEN); Led_data = 0x1 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } if(myindex > 8){ while(TIM3->CR1 & TIM_CR1_CEN); Led_data = 0x0 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } switch(myindex) { case 0: while(TIM3->CR1 & TIM_CR1_CEN); { Led_data = 0x0 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } break; case 1: while(TIM3->CR1 & TIM_CR1_CEN); { Led_data = 0x1 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } break; case 2: while(TIM3->CR1 & TIM_CR1_CEN); { Led_data = 0x2 << 20 | 0xff << 12 | 0xf0; next_state = INTERVAL; TIM_Cmd(TIM3, ENABLE); } break; case 3: while(TIM16->CR1 & TIM_CR1_CEN); { Led_data2 = 0x0 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } break; case 4: while(TIM16->CR1 & TIM_CR1_CEN); { Led_data2 = 0x1 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } break; case 5: while(TIM16->CR1 & TIM_CR1_CEN); { Led_data2 = 0x2 << 20 | 0xff << 12 | 0xf0; next_state2 = INTERVAL; TIM_Cmd(TIM16, ENABLE); } break; case 6: while(TIM17->CR1 & TIM_CR1_CEN); { Led_data3 = 0x0 << 20 | 0xff << 12 | 0xf0; next_state3 = INTERVAL; TIM_Cmd(TIM17, ENABLE); } break; case 7: while(TIM17->CR1 & TIM_CR1_CEN); { Led_data3 = 0x1 << 20 | 0xff << 12 | 0xf0; next_state3 = INTERVAL; TIM_Cmd(TIM17, ENABLE); } break; } if(++myindex > 9) { myindex = 0; Delay(1000); } }
void StackLED() { #define StackDealy 30 #define StackLoopDealy 100 #define StackBKColor 0x0 static uint8_t myindex = 0; static uint8_t u8StackLoop = 0; static uint16_t u16Color = 0xf; static uint8_t u8Loop = 0; switch(myindex) { case 0: SetSingleLED(1, u16Color, 0xff); break; case 1: SetSingleLED(2, u16Color, 0xff); Delay(StackDealy); SetSingleLED(1, StackBKColor, 0xf); break; case 2: SetSingleLED(3, u16Color, 0xff); Delay(StackDealy); SetSingleLED(2, StackBKColor, 0xf); break; case 3: SetSingleLED(4, u16Color, 0xff); Delay(StackDealy); SetSingleLED(3, StackBKColor, 0xf); break; case 4: SetSingleLED(5, u16Color, 0xff); Delay(StackDealy); SetSingleLED(4, StackBKColor, 0xf); break; case 5: SetSingleLED(6, u16Color, 0xff); Delay(StackDealy); SetSingleLED(5, StackBKColor, 0xf); break; case 6: SetSingleLED(7, u16Color, 0xff); Delay(StackDealy); SetSingleLED(6, StackBKColor, 0xf); break; case 7: SetSingleLED(8, u16Color, 0xff); Delay(StackDealy); SetSingleLED(7, StackBKColor, 0xf); break; } if(++myindex > (6 - u8StackLoop)) { myindex = 0; u8StackLoop++; Delay(StackLoopDealy); } if(u8StackLoop > 6) { u8StackLoop = 0; //color += 100; //if(color > 4095) color = 0xf; //color = color & 0xfff; u8Loop ++; SetLedColor(0,0); } switch(u8Loop){ case 0: u16Color = 0xf; break; case 1: u16Color = 0xff0; break; case 2: u16Color = 0xf00; break; case 3: u16Color = 0xf0f; break; case 4: u16Color = 0x0ff; break; case 5: u16Color = 0xf0; break; case 6: u16Color = 0xfff; break; default: SetLedColor(0,0); u8Loop = 0; } }