void grafico1_inicial() { oled_clearScreen(OLED_COLOR_WHITE); // Hora hora_string_oled[6]=buffer[11]; hora_string_oled[7]=buffer[12]; hora_string_oled[9]=buffer[14]; hora_string_oled[10]=buffer[15]; // Fecha fecha_string_oled[7]=buffer[0]; fecha_string_oled[8]=buffer[1]; fecha_string_oled[10]=buffer[3]; fecha_string_oled[11]=buffer[4]; fecha_string_oled[13]=buffer[8]; fecha_string_oled[14]=buffer[9]; oled_putString(1,10,fecha_string_oled, OLED_COLOR_BLACK,OLED_COLOR_WHITE); oled_putString(1,20,hora_string_oled, OLED_COLOR_BLACK,OLED_COLOR_WHITE); //-- escribo el primer char en blanco con fondo negro oled_putChar(43,10,buffer[0],OLED_COLOR_WHITE, OLED_COLOR_BLACK); }
void grafico1() { oled_clearScreen(OLED_COLOR_WHITE); // Temperatura temp_string_oled[7]=buffer[20]; temp_string_oled[8]=buffer[21]; temp_string_oled[10]=buffer[23]; // Hora hora_string_oled[6]=buffer[11]; hora_string_oled[7]=buffer[12]; hora_string_oled[9]=buffer[14]; hora_string_oled[10]=buffer[15]; // Fecha fecha_string_oled[7]=buffer[0]; fecha_string_oled[8]=buffer[1]; fecha_string_oled[10]=buffer[3]; fecha_string_oled[11]=buffer[4]; fecha_string_oled[13]=buffer[8]; fecha_string_oled[14]=buffer[9]; oled_putString(1,10, temp_string_oled, OLED_COLOR_BLACK, OLED_COLOR_WHITE); oled_putString(1,20,fecha_string_oled, OLED_COLOR_BLACK,OLED_COLOR_WHITE); oled_putString(1,30,hora_string_oled, OLED_COLOR_BLACK,OLED_COLOR_WHITE); }
void grafico2(int vector_temp[7]) { oled_clearScreen(OLED_COLOR_WHITE); oled_putString(6,2,"Temp Semanales", OLED_COLOR_BLACK,OLED_COLOR_WHITE); escala(); barra_uno(vector_temp[0]); barra_dos(vector_temp[1]); barra_tres(vector_temp[2]); barra_cuatro(vector_temp[3]); barra_cinco(vector_temp[4]); barra_seis(vector_temp[5]); barra_siete(vector_temp[6]); }
// Application Start void main(void) { // Prepare Application; MCC generated code SYSTEM_Initialize(); // Custom Initializations /* * watchdog timer * configured for 131s timeout * */ //WDTCONbits.SWDTEN = 1; // enable watchdog //ClrWdt(); oled_init(); // Dispaly USBDeviceInit(); // usb_device.c. Initializes USB module SFRs and firmware // variables to known states. USBDeviceAttach(); // Interrupts Enabled PEIE = 1; GIE = 1; // Test Hardware moteApp_delayms(300); modemResync(); moteApp_delayms(300); // moteApp_clearCommand(); // moteApp_delayms(100); powerOnStatus = DATAEE_ReadByte(0x00); #if 0 if ( powerOnStatus != 0x55) { test_powerup(); DATAEE_WriteByte(0x00, 0x55); } #endif // Default to USB operationType = USB; // Mode State USBapp_handlerState(USB_STARTUP); // Initial Display Print oled_clear(); oled_putString("Scriptr IoT-X",0,0); oled_putString(" Dev: S1 ",0,1); oled_putString(" build 006 ",0,3); moteApp_delayms(700); // Application Loop while(1) { // Handle USB or Solo Mode switch (operationType) { default: // Invalid while(1); // Hold For Error; break; case USB: USBapp_Handler(); // It is held in here. // USB escaped; Cable is unplugged; Change OPERATION mode. operationType = MOTE; MOTEapp_handlerState(MOTE_STARTUP); // Initial state for startup break; case MOTE: if (moteHandler() == MOTE_SWAP) { // Return to USB Operation operationType = USB; // Mode State USBapp_handlerState(USB_STARTUP); } break; } } }
static MOTE_RUNNING_T moteRunningProcess(bool changeStates, bool selectButton, bool messageReady, bool sleepEvent) { uint8_t readByte = 0; if ((sleepEvent) && (periodicState)) { if (sleepTicker >= PeriodicInSleep) // No Manual Transmission for a time; Do one Auto { // Measure Sensors uint16_t Traw = 0; moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { light = (light + ADC_GetConversion(Light_channel)) / 2; } moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2; } temperature = ADC_TempConversion(Traw); // Do Normal Operation sleepTicker = 0; oled_clear(); oled_putString("Issue Perodic TX",0,0); oled_putString("On Port #: ",0,1); oled_putUint8(randomPortNum,10,1); oled_putString("L: T: ",0,2); oled_putUint16(light,3,2); oled_putUint8(temperature,12,2); oled_putString("(NAV) (SEL) ",0,3); // Prepare Buffers and data moteApp_clearBuffers(); randomPortNum = TMR2_ReadTimer(); // Make Sure Port is in allowed Range if (!randomPortNum) randomPortNum = 5; else if (randomPortNum > 223) randomPortNum = randomPortNum - 50; // Prepare DataBuffer for Tx moteApp_add8bToDataBuffer(randomPortNum, 0); dataBuffer[3] = 0x20; moteApp_add16bToDataBuffer(light, 4); moteApp_add8bToDataBuffer(temperature, 9); NOP(); sleepTxOccurances = 0; oled_putString("C",15,1); sendDataCommand("radio tx ", dataBuffer, 12); secTicker = SleepTimeOut - (SleepTimeOut / 3); runningState = runningUplinkTransmit; } else { sleepTicker++; runningState = runningSleep; selectButton = true; } } moteApp_GoToSleep(); // Menu Operation switch(runningState) { default: // Invalid while(1); // Hold for Error break; } return runningState; // no return value needed but we declare one for this function. }
static MOTE_T moteJoiningProcess(bool messageReady, bool switchEvent) { static int ch = 0; bool joiningComplete = false; static int mode = 0; static unsigned char pkt_cnt = 0; static unsigned char float_val = 255; static bool pending_confirm = false; char buf[50]; static int skip_count = 0; /*if (switchEvent) { activeState = moteComFailure; while(!SWITCH_2_PORT); }*/ switch (activeState) { default: // Invalid while(1); // Hold for Error break; case moteStartup: // HACK to block for Espruino //EUSART_TX_TRIS = INPUT; //while (1) {}; //sendCommand("sys reset"); LED_RED_LAT = 0; LED_GREEN_LAT = 0; // flush buffer while (MOTEapp_getModemResponse()); sendCommand("sys get ver"); oled_clear(); oled_putString("get ver",0,0); moteApp_delayms(100); activeState = moteReset; //activeState = moteStartup; break; case moteReset: if ( messageReady ) { if(strncmp(&modemResonseBuffer[2], "2903", 4) == 0) // Check 1st [2] Bytes for RN { sendCommand("mac set devaddr DEAD00A1"); oled_clear(); oled_putString("set devaddr",0,0); moteApp_delayms(80); activeState = moteSetDevaddr; } else { oled_clear(); oled_putString(modemResonseBuffer,0,3); moteApp_delayms(1000); activeState = moteStartup; } } break; case moteSetDevaddr: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { sendCommand("mac set appskey 2B7E151628AED2A6ABF7158809CF4F3C"); oled_clear(); oled_putString("set appskey",0,0); moteApp_delayms(80); activeState = moteSetAppskey; } else // Retry Command { activeState = moteStartup; } } break; case moteSetAppskey: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { sendCommand("mac set nwkskey 2B7E151628AED2A6ABF7158809CF4F3C"); oled_clear(); oled_putString("set nwkskey",0,0); moteApp_delayms(80); activeState = moteSetNwkskey; } else // Retry Command { activeState = moteStartup; } } break; case moteSetNwkskey: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { sendCommand("mac set adr off"); oled_clear(); oled_putString("set adr",0,0); moteApp_delayms(80); activeState = moteSetAdr; } else // Retry Command { activeState = moteStartup; } } break; case moteSetAdr: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { sendCommand("mac set sync 34"); oled_clear(); oled_putString("set sync",0,0); moteApp_delayms(80); activeState = moteSetSync; } else // Retry Command { activeState = moteStartup; } } break; case moteSetSync: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { sendCommand("mac set rx2 8 923300000"); oled_clear(); oled_putString("set rx2",0,0); moteApp_delayms(80); ch = 0; // start with channel 0 activeState = moteSetRx2; //activeState = moteSetChStatus; } else // Retry Command { activeState = moteStartup; } } break; case moteSetRx2: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { // only enable subband #2 sprintf(cmd,"mac set ch status %d %s", ch, (ch>=8 && ch<=15)?"on":"off"); sendCommand(cmd); oled_clear(); oled_putString("set ch",0,0); oled_putString(&cmd[18],0,1); moteApp_delayms(80); ch++; if (ch<72) { activeState = moteSetRx2; } else { activeState = moteSetChStatus; } } else // Retry Command { activeState = moteStartup; } } break; case moteSetChStatus: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { moteApp_delayms(200); sendCommand("mac join abp"); oled_clear(); oled_putString("join abp",0,0); moteApp_delayms(80); activeState = moteJoinAbp; } else // Retry Command { activeState = moteStartup; } } break; case moteJoinAbp: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { // waiting now for "accepted" oled_clear(); oled_putString("Wait accepted...",0,0); moteApp_delayms(80); activeState = moteWaitAccepted; } else // Retry Command { activeState = moteStartup; } } break; case moteWaitAccepted: if ( messageReady ) { if(strcmp(modemResonseBuffer, "accepted") == 0) { //while (MOTEapp_getModemResponse()); // send initial packet sendCommand("mac tx uncnf 1 00000000"); oled_clear(); oled_putString("ini tx",0,0); moteApp_delayms(150); activeState = moteIniTx; LED_GREEN_LAT = 1; } else // Retry Command { oled_clear(); oled_putString("moteWaitAccepted",0,0); activeState = moteStartup; moteApp_delayms(500); LED_RED_LAT = 1; } } break; case moteIniTx: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { oled_clear(); oled_putString("wait mac_tx_ok",0,0); moteApp_delayms(3000); activeState = moteWaitIniTxOk; LED_RED_LAT = 1; } else // Retry Command { oled_clear(); oled_putString("moteIniTx",0,0); activeState = moteStartup; moteApp_delayms(500); LED_GREEN_LAT = 0; } } break; case moteWaitIniTxOk: //if ( messageReady ) { //if(strcmp(modemResonseBuffer, "mac_tx_ok") == 0) { LED_RED_LAT = 1; LED_GREEN_LAT = 0; // next we rest for a while oled_clear(); oled_putString("Mode: ",0,0); oled_putString(autoMode?"AUTO ":"MANUAL",6,0); oled_putString("S1 = auto on/off",0,1); oled_putString("S2 = manual send",0,2); moteApp_delayms(80); ch = 0; activeState = moteRest; } /*else if (strcmp(modemResonseBuffer, "") == 0) { // glitch where we get an extra return moteApp_delayms(1); LED_GREEN_LAT = 0; moteApp_delayms(1); LED_GREEN_LAT = 1; moteApp_delayms(1); activeState = moteWaitIniTxOk; } else // Retry Command { oled_clear(); oled_putString("WaitIniTxOk",0,0); oled_putString(modemResonseBuffer,0,2); sprintf(cmd,"buflen=%d",strlen(modemResonseBuffer)); oled_putString(cmd,0,3); activeState = moteStartup; moteApp_delayms(1000); }*/ } break; case moteRest: if (autoMode || manualTrigger) { ch++; } moteApp_delayms(100); /* check buttons */ S1 = SWITCH_1_PORT; // 1 == unpressed, 0 = pressed S2 = SWITCH_2_PORT; if (S1 == 0 && prevS1 == 1) { /* rising edge of switch 1 */ /* alternate mode */ autoMode = !autoMode; oled_putString("Mode: ",0,0); oled_putString(autoMode?"AUTO ":"MANUAL",6,0); } if (S2 == 0 && prevS2 == 1) { /* rising edge of switch 2 */ /* schedule a measurement */ manualTrigger = true; } prevS1 = S1; prevS2 = S2; if (ch>20) { manualTrigger=false; ch = 0; // make a measurement uint16_t Traw = 0; moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { light = (light + ADC_GetConversion(Light_channel)) / 2; } moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2; } temperature = ADC_TempConversion(Traw); m10SecTicker = 0; pkts++; // display measurement oled_clear(); oled_putString("Mode: ",0,0); oled_putString(autoMode?"AUTO ":"MANUAL",6,0); oled_putString("Light:",0,1); oled_putString("Temp :",0,2); oled_putString("C",12,2); oled_putUint16(light,6,1); oled_putUint16(temperature,6,2); oled_putString("Pkts|:",0,3); oled_putUint16(pkts,6,3); sprintf(cmd,"mac tx uncnf 1 %04X%04X", pkts,dr); sendCommand(cmd); activeState = moteTxUncnf; } break; case moteTxUncnf: //if ( messageReady ) { //if(strcmp(modemResonseBuffer, "ok") == 0) { oled_putString("Pkts/:",0,3); oled_putUint16(pkts,6,3); moteApp_delayms(80); activeState = moteTxWait; } /*else { oled_clear(); oled_putString("TxUncnf",0,0); activeState = moteStartup; moteApp_delayms(500); }*/ } break; case moteTxWait: if ( messageReady ) { if(strcmp(modemResonseBuffer, "mac_tx_ok") == 0) { dr = (dr + 1) % 4; // 0-3 sprintf(cmd,"mac set dr %d",dr); sendCommand(cmd); oled_putString("Pkts-:",0,3); oled_putUint16(pkts,6,3); activeState = moteNextDr; } } break; case moteNextDr: if ( messageReady ) { if(strcmp(modemResonseBuffer, "ok") == 0) { //oled_putString("Pkts-:",0,3); //oled_putUint16(pkts,6,3); activeState = moteRest; } } break; case moteDone: joiningComplete = true; moteApp_delayms(80); break; case moteComFailure: DATAEE_WriteByte(0x00, 0x00); while((!SWITCH_1_PORT) && (!SWITCH_2_PORT)); moteApp_delayms(40); RESET(); // Do Nothing; Wait for USB; Hold for Display break; } return joiningComplete; }
MOTE_MODE_T moteHandler(void) { bool gotoSleep = false; bool gotoNextState = false; bool s2Button = false; bool readMessage = false; buttonStates_t buttonPress = Released; // Check USB connection state if (USB_DET_PORT) moteState = MOTE_SWAP; // Get Timer Ticks if ( appTimer.mSecTick ) { // Clear Flags for next task TMRapp_ClearBitFlags(); // Execute Task TMRapp_Handler(); MOTEapp_TimerBasedTask(appTimer); } // Handle button press events for menu navigation buttonPress = service_pushbutton(); if (buttonPress == ShortPress1) { secTicker = 0; gotoNextState = true; // Button Event has occured } else if (buttonPress == ShortPress2) { secTicker = 0; s2Button = true; // Button Event has occured } // Gather response from LoRa Module if available if ( MOTEapp_getModemResponse() ) readMessage = true; else readMessage = false; // Handle Timed Task if ( (runningState == runningUplink) || (runningState == runningSensorMenu) ) { // Get Sensor Data every 10mS if (m10SecTicker >= LightCapTime) { uint16_t Traw = 0; moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { light = (light + ADC_GetConversion(Light_channel)) / 2; } moteApp_delayms(10); for(uint8_t i = 32; i>0; i--) { Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2; } temperature = ADC_TempConversion(Traw); m10SecTicker = 0; } } // Sleep Event if idle if(secTicker) { if (secTicker >= SleepTimeOut) { gotoSleep = true; secTicker = 0; } } switch(moteState) { default: // Invalid while(1); // Hold For Error; break; case MOTE_STARTUP: oled_clear(); oled_putString("MOTE Mode",1,0); oled_putString("Starting",2,1); oled_putString("ReRun PwrTst<S2>",0,3); activeState = moteStartup; moteState = MOTE_JOINING; break; case MOTE_JOINING: if ( moteJoiningProcess(readMessage, s2Button) ) { oled_clear(); oled_putString("Mobile Mote -App",0,0); oled_putString("Navigate Select", 0,2); oled_putString(" (S1) (S2) ", 0,3); runningState = runningEnter; moteState = MOTE_RUNNING; } break; case MOTE_RUNNING: moteRunningProcess(gotoNextState, s2Button, readMessage, gotoSleep); break; case MOTE_SWAP: // Do Nothing break; } return moteState; }