void lcd_material_store_material(uint8_t nr)
{
    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(nr), material[active_extruder].temperature);
    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(nr), material[active_extruder].bed_temperature);
    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(nr), material[active_extruder].flow);

    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(nr), material[active_extruder].fan_speed);
    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(nr), material[active_extruder].diameter);
    //eeprom_write_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(nr), 8);
}
void lcd_material_store_current_material()
{
    for(uint8_t e=0; e<EXTRUDERS; e++)
    {
        eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].temperature);
        eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].bed_temperature);
        eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].fan_speed);
        eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].flow);
        eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].diameter);
    }
}
Ejemplo n.º 3
0
void gui_set_vario_avg_int_cb(float val)
{
    gui_switch_task(GUI_SET_VARIO);
    eeprom_busy_wait();
    eeprom_write_float(&config.vario.avg_vario_dampening, val);

    if (val == 0)
        fc.avg_vario_dampening = 1;
    else
        fc.avg_vario_dampening = 1.0 / 100.0 / val;
}
void lcd_material_reset_defaults()
{
    //Fill in the defaults
    char buffer[8];

    strcpy_P(buffer, PSTR("PLA"));
    eeprom_write_block(buffer, EEPROM_MATERIAL_NAME_OFFSET(0), 4);
    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(0), 210);
    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(0), 60);
    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(0), 100);
    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(0), 100);
    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(0), 2.85);

    eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(0), 210);
    eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(0), 0);

    strcpy_P(buffer, PSTR("ABS"));
    eeprom_write_block(buffer, EEPROM_MATERIAL_NAME_OFFSET(1), 4);
    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(1), 260);
    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(1), 90);
    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(1), 50);
    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(1), 107);
    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(1), 2.85);

    eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(1), 260);
    eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(1), 0);

    strcpy_P(buffer, PSTR("CPE"));
    eeprom_write_block(buffer, EEPROM_MATERIAL_NAME_OFFSET(2), 5);
    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(2), 250);
    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(2), 60);
    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(2), 50);
    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(2), 100);
    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(2), 2.85);

    eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(2), 250);
    eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(2), 0);

    eeprom_write_byte(EEPROM_MATERIAL_COUNT_OFFSET(), 3);
}
Ejemplo n.º 5
0
void Settings1()
{
	displayMode = SETTINGS1;
	displayValue = (uint16_t)(savedParametersList[HIGH_LIM] * 100);
	
	uint8_t pressCounter = 0;
	
	while(bit_is_set(PINC, MODE_BUTTON))
	{}
	
	_delay_ms(100);
	
	while(bit_is_clear(PINC, MODE_BUTTON))
	{
		if (bit_is_set(PINC, SET_BUTTON))
		{
			pressCounter++;
			if(savedParametersList[HIGH_LIM] < 0.99)
				savedParametersList[HIGH_LIM] += 0.01;
			else
				savedParametersList[HIGH_LIM] = 0.00;
		}
		else
		{
			pressCounter = 0;
		}
		
		if (pressCounter < 10)
		{
			_delay_ms(500);
		} 
		else
		{
			_delay_ms(100);
		}
		
		displayValue = (uint16_t)(savedParametersList[HIGH_LIM] * 100);
	}
		
	if (eeprom_read_float((float*)(HIGH_LIM * 4)) != savedParametersList[HIGH_LIM])
	{
		eeprom_write_float((float*)(HIGH_LIM * 4), savedParametersList[HIGH_LIM]);
	}
		
	Settings2();
	
	while(bit_is_set(PINC, MODE_BUTTON))
	{}
}
void lcd_material_store_material(uint8_t nr)
{
    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(nr), material[active_extruder].temperature);
#if TEMP_SENSOR_BED != 0
    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(nr), material[active_extruder].bed_temperature);
#endif
    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(nr), material[active_extruder].flow);

    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(nr), material[active_extruder].fan_speed);
    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(nr), material[active_extruder].diameter);
    //eeprom_write_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(nr), 8);

    eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(nr), material[active_extruder].change_temperature);
    eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(nr), material[active_extruder].change_preheat_wait_time);
}
void lcd_material_store_current_material()
{
    for(uint8_t e=0; e<EXTRUDERS; e++)
    {
        eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].temperature);
#if TEMP_SENSOR_BED != 0
        eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].bed_temperature);
#endif
        eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].fan_speed);
        eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].flow);
        eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].diameter);

        eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].change_temperature);
        eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(EEPROM_MATERIAL_SETTINGS_MAX_COUNT+e), material[e].change_preheat_wait_time);
    }
}
bool lcd_material_verify_material_settings()
{
    bool hasUPET = false;
    
    uint8_t cnt = eeprom_read_byte(EEPROM_MATERIAL_COUNT_OFFSET());
    if (cnt < 2 || cnt > EEPROM_MATERIAL_SETTINGS_MAX_COUNT)
        return false;
    while(cnt > 0)
    {
        cnt --;
        if (eeprom_read_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(cnt)) > HEATER_0_MAXTEMP)
            return false;
//        if (eeprom_read_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(cnt)) > BED_MAXTEMP)
//            return false;
        if (eeprom_read_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(cnt)) > 100)
            return false;
        if (eeprom_read_word(EEPROM_MATERIAL_FLOW_OFFSET(cnt)) > 1000)
            return false;
        if (eeprom_read_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt)) > 10.0)
            return false;
        if (eeprom_read_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt)) < 0.1)
            return false;

        eeprom_read_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(cnt), 8);
        if (strcmp_P(card.longFilename, PSTR("UPET")) == 0)
            hasUPET = true;
    }
    cnt = eeprom_read_byte(EEPROM_MATERIAL_COUNT_OFFSET());
    if (!hasUPET && cnt < EEPROM_MATERIAL_SETTINGS_MAX_COUNT)
    {
        strcpy_P(card.longFilename, PSTR("UPET"));
        eeprom_write_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(cnt), 5);
        eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(cnt), 250);
//        eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(cnt), 60);
        eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(cnt), 50);
        eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(cnt), 100);
        eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt), 2.85);
        
        eeprom_write_byte(EEPROM_MATERIAL_COUNT_OFFSET(), cnt + 1);
    }
    return true;
}
Ejemplo n.º 9
0
static void set_battery_capacity_handler(Message* msg) {
    assert(msg->length == 5);
    battery_capacity_q = bytes_to_float(msg->content + 1);
    eeprom_write_float(STORAGE_BATTERY_CAPACITY, battery_capacity_q);
}
bool lcd_material_verify_material_settings()
{
    bool hasCPE = false;
    
    uint8_t cnt = eeprom_read_byte(EEPROM_MATERIAL_COUNT_OFFSET());
    if (cnt < 2 || cnt > EEPROM_MATERIAL_SETTINGS_MAX_COUNT)
        return false;
    while(cnt > 0)
    {
        cnt --;
        if (eeprom_read_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(cnt)) > HEATER_0_MAXTEMP)
            return false;
#if TEMP_SENSOR_BED != 0
        if (eeprom_read_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(cnt)) > BED_MAXTEMP)
            return false;
#endif
        if (eeprom_read_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(cnt)) > 100)
            return false;
        if (eeprom_read_word(EEPROM_MATERIAL_FLOW_OFFSET(cnt)) > 1000)
            return false;
        if (eeprom_read_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt)) > 10.0)
            return false;
        if (eeprom_read_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt)) < 0.1)
            return false;

        eeprom_read_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(cnt), 8);
        if (strcmp_P(card.longFilename, PSTR("UPET")) == 0)
        {
            strcpy_P(card.longFilename, PSTR("CPE"));
            eeprom_write_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(cnt), 4);
        }
        if (strcmp_P(card.longFilename, PSTR("CPE")) == 0)
        {
            hasCPE = true;
        }

        if (eeprom_read_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(cnt)) > HEATER_0_MAXTEMP || eeprom_read_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(cnt)) < 10)
        {
            //Invalid temperature for change temperature.
            eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(cnt), eeprom_read_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(cnt)));
            eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(cnt), 0);
        }
    }
    cnt = eeprom_read_byte(EEPROM_MATERIAL_COUNT_OFFSET());
    if (!hasCPE && cnt < EEPROM_MATERIAL_SETTINGS_MAX_COUNT)
    {
        strcpy_P(card.longFilename, PSTR("CPE"));
        eeprom_write_block(card.longFilename, EEPROM_MATERIAL_NAME_OFFSET(cnt), 4);
        eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(cnt), 250);
        eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(cnt), 60);
        eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(cnt), 50);
        eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(cnt), 100);
        eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(cnt), 2.85);

        eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(cnt), 250);
        eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(cnt), 0);

        eeprom_write_byte(EEPROM_MATERIAL_COUNT_OFFSET(), cnt + 1);
    }
    return true;
}
static void lcd_menu_material_import()
{
    if (!card.sdInserted)
    {
        LED_GLOW();
        lcd_lib_encoder_pos = MAIN_MENU_ITEM_POS(0);
        lcd_info_screen(lcd_menu_material_select);
        lcd_lib_draw_string_centerP(15, PSTR("No SD-CARD!"));
        lcd_lib_draw_string_centerP(25, PSTR("Please insert card"));
        lcd_lib_update_screen();
        card.release();
        return;
    }
    if (!card.isOk())
    {
        lcd_info_screen(lcd_menu_material_select);
        lcd_lib_draw_string_centerP(16, PSTR("Reading card..."));
        lcd_lib_update_screen();
        card.initsd();
        return;
    }

    card.setroot();
    card.openFile("MATERIAL.TXT", true);
    if (!card.isFileOpen())
    {
        lcd_info_screen(lcd_menu_material_select);
        lcd_lib_draw_string_centerP(15, PSTR("No export file"));
        lcd_lib_draw_string_centerP(25, PSTR("Found on card."));
        lcd_lib_update_screen();
        return;
    }

    char buffer[32];
    uint8_t count = 0xFF;
    while(card.fgets(buffer, sizeof(buffer)) > 0)
    {
        buffer[sizeof(buffer)-1] = '\0';
        char* c = strchr(buffer, '\n');
        if (c) *c = '\0';

        if(strcmp_P(buffer, PSTR("[material]")) == 0)
        {
            count++;
        }else if (count < EEPROM_MATERIAL_SETTINGS_MAX_COUNT)
        {
            c = strchr(buffer, '=');
            if (c)
            {
                *c++ = '\0';
                if (strcmp_P(buffer, PSTR("name")) == 0)
                {
                    eeprom_write_block(c, EEPROM_MATERIAL_NAME_OFFSET(count), 8);
                }else if (strcmp_P(buffer, PSTR("temperature")) == 0)
                {
                    eeprom_write_word(EEPROM_MATERIAL_TEMPERATURE_OFFSET(count), strtol(c, NULL, 10));
                }else if (strcmp_P(buffer, PSTR("bed_temperature")) == 0)
                {
                    eeprom_write_word(EEPROM_MATERIAL_BED_TEMPERATURE_OFFSET(count), strtol(c, NULL, 10));
                }else if (strcmp_P(buffer, PSTR("fan_speed")) == 0)
                {
                    eeprom_write_byte(EEPROM_MATERIAL_FAN_SPEED_OFFSET(count), strtol(c, NULL, 10));
                }else if (strcmp_P(buffer, PSTR("flow")) == 0)
                {
                    eeprom_write_word(EEPROM_MATERIAL_FLOW_OFFSET(count), strtol(c, NULL, 10));
                }else if (strcmp_P(buffer, PSTR("diameter")) == 0)
                {
                    eeprom_write_float(EEPROM_MATERIAL_DIAMETER_OFFSET(count), strtod(c, NULL));
#ifdef USE_CHANGE_TEMPERATURE
                }else if (strcmp_P(buffer, PSTR("change_temp")) == 0)
                {
                    eeprom_write_word(EEPROM_MATERIAL_CHANGE_TEMPERATURE(count), strtol(c, NULL, 10));
                }else if (strcmp_P(buffer, PSTR("change_wait")) == 0)
                {
                    eeprom_write_byte(EEPROM_MATERIAL_CHANGE_WAIT_TIME(count), strtol(c, NULL, 10));
#endif
                }
            }
        }
    }
    count++;
    if (count > 0)
    {
        eeprom_write_byte(EEPROM_MATERIAL_COUNT_OFFSET(), count);
    }
    card.closefile();

    currentMenu = lcd_menu_material_import_done;
}
Ejemplo n.º 12
0
static void PacketReceived (PACKET_Instance_t *inst, PACKET_Packet_t *packet, PACKET_Error_t err)
{
	if (err == PACKET_ERROR_NONE)
	{
		// we really only care about packets for us...
		if (packet->m_id == 0x01)
		{
			switch (packet->m_cmd)
			{
				case PACKET_CMD_SET_ID:
				{
					//printf ("got SET_ID command\n");
					break;
				}
				
				case PACKET_CMD_START_ASYNC:
				{
					//printf ("got START_ASYNC command\n");
					turnOnAsyncCapture();
					break;
				}
				
				case PACKET_CMD_STOP_ASYNC:
				{
					//printf ("got STOP_ASYNC command\n");
					sendingSamples = 0;
					break;
				}
				
				case PACKET_CMD_TURN_OFF_BATTERY:
				{
					//printf ("got TURN_OFF_BATTERY command\n");
					Battery_EnableLow();
					break;
				}
				
				case PACKET_CMD_TURN_ON_BATTERY:
				{
					//printf ("got TURN_ON_BATTERY command\n");
					Battery_EnableHigh();
					break;
				}
				
				case PACKET_CMD_SEND_SAMPLE:
				{
					//printf ("got PACKET_CMD_SEND_SAMPLE command\n");
					//turnOnFlag();
					currentSample = 0;
					CreateSample(SAMPLE_NORMAL);
					SendSamples(1, PACKET_CMD_SAMPLE);
					break;
				}
				
				case PACKET_CMD_SET_CALIBRATION:
				{
					//printf ("got PACKET_CMD_SET_CALIBRATION command\n");
					calibrationFloor = *(float *)&packet->m_param[0];
					calibrationScale = *(float *)&packet->m_param[4];
					eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_FLOOR_LOCATION), calibrationFloor);
					eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SCALE_LOCATION), calibrationScale);
					eeprom_write_byte((uint8_t*)(CALIBRATION_EEPROM_BASE + CALIBRATION_SIGNATURE_LOCATION), CALIBRATION_SIGNATURE);
					//printf("Calibration parameters saved to EEPROM\n");
					//char output[16];
					//dtostrf(calibrationFloor, 7, 4, output);
					//printf("FLOOR: %s\n", output);
					//dtostrf(calibrationScale, 7, 4, output);
					//printf("SCALE: %s\n", output);
					break;
				}
				
				case PACKET_CMD_GET_CALIBRATION:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_CALIBRATION command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t floatSize = sizeof(calibrationFloor);
					uint8_t packetLength = (floatSize * 2) + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_CALIBRATION); // command
					checksum += PACKET_CMD_CALIBRATION;
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&calibrationFloor)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&calibrationScale)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_COMPENSATION:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_COMPENSATION command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t floatSize = sizeof(calibrationFloor);
					uint8_t packetLength = 1 + floatSize + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_COMPENSATION); // command
					checksum += PACKET_CMD_COMPENSATION;
					RingBuffer_Insert(&Send_USB_Buffer, compensation);
					checksum += compensation;
					for (int index=0; index < floatSize; index++) {
						uint8_t value = ((uint8_t *)&baselineVoltage)[index];
						RingBuffer_Insert(&Send_USB_Buffer, value);
						checksum += value;
					}
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_RAW_SAMPLE:
				{
					//printf ("got PACKET_CMD_GET_RAW_SAMPLE command\n");
					currentSample = 0;
					CreateSample(SAMPLE_RAW);
					SendSamples(1, PACKET_CMD_GET_RAW_SAMPLE);
					break;
				}
				
				case PACKET_CMD_GET_VERSION:
				{
					//printf ("got PACKET_CMD_GET_VERSION command\n");
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t packetLength = 1 + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_VERSION); // command
					checksum += PACKET_CMD_VERSION;
					RingBuffer_Insert(&Send_USB_Buffer, AMMETER_VERSION);
					checksum += AMMETER_VERSION;
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}
				
				case PACKET_CMD_GET_SERIAL:
				{
					if (debugMode) {
						printf ("got PACKET_CMD_GET_SERIAL command\n");
					}
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
					RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
					uint8_t checksum = 0x01;
					uint8_t packetLength = 2 + 2;
					RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
					checksum += packetLength;
					RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_SERIAL); // command
					checksum += PACKET_CMD_SERIAL;
					RingBuffer_Insert(&Send_USB_Buffer, serialNumber & 0xFF);
					checksum += serialNumber & 0xFF;
					RingBuffer_Insert(&Send_USB_Buffer, serialNumber >> 8);
					checksum += serialNumber >> 8;
					RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
					break;
				}

				case PACKET_CMD_SET_SERIAL:
				{
					//printf ("got PACKET_CMD_SET_SERIAL command\n");
					serialNumber = *(uint16_t *)&packet->m_param[0];
					eeprom_write_word((float*)(CALIBRATION_EEPROM_BASE + SERIAL_NUMBER_LOCATION), serialNumber);
					//printf("Serial number saved to EEPROM\n");
					//printf("Serial Number: %d\n", serialNumber);
					break;
				}

				case PACKET_CMD_DUMP_DEBUG_INFO:
				{
					dumpDebugInfo();
					break;
				}

				case PACKET_CMD_TURN_ON_COMPENSATION:
				{
					float value = *(float *)&packet->m_param[0];
					if ((value >= 3.7) && (value <= 4.2)) {
						compensation = 1;
						eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
						baselineVoltage = value;
						eeprom_write_float((float*)(CALIBRATION_EEPROM_BASE + BASELINE_VOLTAGE_LOCATION), baselineVoltage);
					}
					break;
				}
				
				case PACKET_CMD_TURN_OFF_COMPENSATION:
				{
					compensation = 0;
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + COMPENSATION_FLAG_LOCATION), compensation);
					break;
				}
				
				case PACKET_CMD_START_LOGGING:
				{
					loggingFlag = 1;
					if (debugMode) {
						debugMode = 0;
					} else {
						InitUART ();
						fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
					}
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
					turnOnAsyncCapture();
					break;
				}
				
				case PACKET_CMD_STOP_LOGGING:
				{
					sendingSamples = 0;
					loggingFlag = 0;
					eeprom_write_byte((float*)(CALIBRATION_EEPROM_BASE + LOGGING_FLAG_LOCATION), loggingFlag);
					break;
				}
				
				case PACKET_CMD_CHECK_AUX_BATTERY:
				{
					loggingFlag = 0;
					if (debugMode) {
						debugMode = 0;
					} else {
						InitUART ();
						fdevopen (UART1_PutCharStdio, UART1_GetCharStdio);
					}
					printf("I2C Test\n");
                    float voltage = readAuxBatteryVoltage();
                    char output[16];
                    dtostrf(voltage, 4, 2, output);
                    printf("Battery Voltage: %s volts\n", output);
					break;
				}

                case PACKET_CMD_TURN_OFF_AUX_USB:
                {
                    //printf ("got TURN_OFF_BATTERY command\n");
                    AuxUSB_Disable();
                    break;
                }

                case PACKET_CMD_TURN_ON_AUX_USB:
                {
                    //printf ("got TURN_ON_BATTERY command\n");
                    AuxUSB_Enable();
                    break;
                }

                case PACKET_CMD_GET_TEMPERATURE:
                {
                    if (debugMode) {
                        printf ("got PACKET_CMD_GET_TEMPERATURE command\n");
                    }
                    float temperature = ReadTemperatureProbe();
                    RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
                    RingBuffer_Insert(&Send_USB_Buffer, 0xFF);
                    RingBuffer_Insert(&Send_USB_Buffer, 0x01); // ammeter id
                    uint8_t checksum = 0x01;
                    uint8_t floatSize = sizeof(temperature);
                    uint8_t packetLength = floatSize + 2;
                    RingBuffer_Insert(&Send_USB_Buffer, packetLength); // packet length, including all framing
                    checksum += packetLength;
                    RingBuffer_Insert(&Send_USB_Buffer, PACKET_CMD_TEMPERATURE); // command
                    checksum += PACKET_CMD_TEMPERATURE;
                    for (int index=0; index < floatSize; index++) {
                        uint8_t value = ((uint8_t *)&temperature)[index];
                        RingBuffer_Insert(&Send_USB_Buffer, value);
                        checksum += value;
                    }
                    RingBuffer_Insert(&Send_USB_Buffer, ~checksum);
                    break;
                }
                
                case PACKET_CMD_SOFT_RESET:
                {
                    if (debugMode) {
                        printf ("got PACKET_CMD_SOFT_RESET command\n");
                    }
                    wdt_enable(WDTO_60MS);
                    while(1) {};
                    
                    break;
                }

                default:
				{
					// there are other commands that we don't care about....
					//printf ("ID:0x%02x Cmd: 0x%02x *** Unknown ***\n", packet->m_id, packet->m_cmd);
					break;
				}
			}
		}
		else {
			//printf ("Got packet for ID: %3d\n", packet->m_id);
		}
	}
Ejemplo n.º 13
0
int main(void)
{
	// OC0A, OC0B and OC2A as outputs
	DDRD |= (1<< PD6) | (1<< PD5);
	DDRB |= (1<< PB3);
	
	// Go to calibration mode?
	eeprom_busy_wait();
	uint8_t should_calibrate = eeprom_read_byte( &eeprom_calibrate );
	if( should_calibrate == 1 || should_calibrate == 0xFF )	// 0xFF means uninitialized: so force calibration
	{
		/*
		 * CALIBRATION MODE
		 */

		// Do the calibration stuff using Rolfe's magic math stuff
		calibrate_setup();
		calibrate();
		
		// Write calibration values to EEPROM
		eeprom_write_float( &eeprom_mx, beta[0] );
		eeprom_write_float( &eeprom_my, beta[1] );
		eeprom_write_float( &eeprom_mz, beta[2] );
		eeprom_write_float( &eeprom_sx, beta[3] );
		eeprom_write_float( &eeprom_sy, beta[4] );
		eeprom_write_float( &eeprom_sz, beta[5] );
		
		// Clear calibration mode var
		eeprom_busy_wait();
		eeprom_write_byte( &eeprom_calibrate, 0 );
		
		// Calibration done: keep flashing B LED
		for( ;; )
		{
			// Keep flashing B LED
			PORTD |= (1<< PD5);
			_delay_ms( 200 );
			PORTD &= ~(1<< PD5);
			_delay_ms( 200 );
		}
	}

	// No: don't calibrate. 

	// Wait a little bit let the power switch debounce
	_delay_ms( 50 );
	
	// Set calibrate bit to YES
	eeprom_busy_wait();
	eeprom_write_byte( &eeprom_calibrate, 1 );
	// White light to indicate "switch off now to enter calibration mode"
	PORTD |= (1<< PD6) | (1<< PD5);
	PORTB |= (1<< PB3);
	
	// Wait 1 second
	_delay_ms( 1000 );
	
	// Clear calibration var
	eeprom_busy_wait();
	eeprom_write_byte( &eeprom_calibrate, 0 );
	
	// LED off
	PORTD &= ~(1<< PD5) & ~(1<< PD6);
	PORTB &= ~(1<< PB3);
	_delay_ms( 1000 );
	
	
	/*
	 *	NORMAL MODE
	 */
	
	// Read calibration values
	// Wait until the EEPROM is ready before reading
	eeprom_busy_wait();
	mx = eeprom_read_float( &eeprom_mx );
	my = eeprom_read_float( &eeprom_my );
	mz = eeprom_read_float( &eeprom_mz );
	sx = eeprom_read_float( &eeprom_sx );
	sy = eeprom_read_float( &eeprom_sy );
	sz = eeprom_read_float( &eeprom_sz );

	// Power-down unused stuff
	PRR |= (1<< PRTWI) | (1<< PRTIM1) | (1<< PRSPI) | (1<< PRUSART0);	// Power-down TWI, Timer1, SPI and USART
	ACSR |= (1<< ACD);													// Power-down analog comperator		
	
	/// DEBUG: PB0 for CPU active pin -> output
	DDRB |= (1<< PB0);
	
	// Set up Timer0 for PWM
	TCCR0A = (1<< COM0A1) | (1<< COM0B1) | (1<< WGM01) | (1<< WGM00);	// OC0A and OC0B normal mode; fast PWM
	TCCR0B = (1<< CS01);	// ~3906 Hz
	
	// Set up Timer2 for PWM
	TCCR2A = (1<< COM2A1) | (1<< WGM21) | (1<< WGM20);					// OC2A normal mode; fast PWM
	TCCR2B = (1<< CS21);	// ~3906 Hz
	
	// Set up analog to digital converter
	// By default set to use AREF and single-conversion mode
	ADCSRA |= (1<< ADPS2) | (1<< ADPS1);	// Set prescaler 64 for 125 kHz @ 8 MHz system clock	
	
	// Disable digital input buffers for all ADC pins. (We're only using 0-2 but since we aren't using
	// the other pins for anything, we might as well turn them off to save a few nA there as well.
	DIDR0 |= (1<< ADC5D) | (1<< ADC4D) | (1<< ADC3D) | (1<< ADC2D) | (1<< ADC1D) | (1<< ADC0D);
	ADCSRA |= (1<< ADEN);	// Enable ADC
	
	// Set up Watchdog timer for 16 ms timeout. That is 62.5 Hz which is slightly above the
	// the accelerometer sampling frequency so we won't loose many samples
	WDTCSR |= (1<< WDIE);	// Interrupt enabled, 2048 clock cycles (for 16 ms)
	sei();					// Interrupts enabled
	
	// Main loop: do nothing but sleep since everything happens in the Watchdog interrupt
	for( ;; )
	{
		// Enter idle mode. AD conversion and PWM adjust will take place in Watchdog interrupt
		SMCR = (1<< SE);	// Sleep enable, idle mode
		asm("sleep");		// Nighty, night, CPU
	}

    return 0;
}
Ejemplo n.º 14
0
uint16_t writeFloat(uint16_t addr, float val) {
  eeprom_write_float((float *)addr, val);
  return addr + sizeof(float);
}
Ejemplo n.º 15
0
void
set_config_float (float* config, float value)
{
  eeprom_write_float(config, value);
}