Пример #1
0
unsigned int input_millivolts(void)
{
    unsigned int batt_millivolts = battery_voltage();

   /* No battery, return nominal value */
    return batt_millivolts;
}
Пример #2
0
static void charging_display_info(bool animate)
{
    int battv;
    unsigned i, ypos;
    static unsigned phase = 3;
    char buf[32];

    battv = battery_voltage();
    lcd_putsf(4, 1, " %d.%02dV", battv / 1000, (battv % 1000) / 10);

    memcpy(buf, logo_pattern, 32); /* copy logo patterns */

    if (!animate) /* build the screen */
    {
        lcd_double_height(false);
        lcd_puts(0, 0, "[Charging]");
        for (i = 0; i < 4; i++)
            lcd_putc(i, 1, logo_chars[i]);
    }
    else          /* animate the logo */
    {
        for (i = 3; i < MIN(19, phase); i++)
        {
            if ((i - phase) % 5 == 0)
            {    /* draw a "bubble" here */
                ypos = (phase + i/5) % 9; /* "bounce" effect */
                if (ypos > 4)
                    ypos = 8 - ypos;
                buf[5 - ypos + 8 * (i/5)] |= 0x10u >> (i%5);
            }
        }
        phase++;
    }
Пример #3
0
static void battery_status_update(void)
{
    int millivolt = battery_voltage();
    int level = _battery_level();

    if (level < 0)
        level = voltage_to_battery_level(millivolt);

#ifdef CURRENT_NORMAL  /*don't try to estimate run or charge
                        time without normal current defined*/
    /* calculate estimated remaining running time */
#if CONFIG_CHARGING >= CHARGING_MONITOR
    if (charging_state()) {
        /* charging: remaining charging time */
        powermgmt_est_runningtime_min = (100 - level)*battery_capacity*60
                / 100 / (CURRENT_MAX_CHG - runcurrent());
    }
    else
#endif

    /* discharging: remaining running time */
    if (level > 0 && (millivolt > percent_to_volt_discharge[battery_type][0]
        || millivolt < 0)) {
        /* linear extrapolation */
        powermgmt_est_runningtime_min = (level + battery_percent)*60
                * battery_capacity / 200 / runcurrent();
    }
    if (0 > powermgmt_est_runningtime_min) {
        powermgmt_est_runningtime_min = 0;
    }
#endif

    battery_percent = level;
    send_battery_level_event();
}
Пример #4
0
int battery_percent() {
	float v = 0;
	v = battery_voltage();

	int p = (v / 4.2) * 100;

	if (p < 0)
		p = 0;
	if (p > 100)
		p = 100;
	return p;
}
Пример #5
0
static void charging_screen(void)
{
    unsigned int button;
    const char* msg;

    ide_power_enable(false); /* power down the disk, else would be spinning */

    lcd_clear_display();

    do
    {
#ifdef ARCHOS_RECORDER
        if (charge_state == CHARGING)
            msg = "charging";
        else if (charge_state == TOPOFF)
            msg = "topoff charge";
        else if (charge_state == TRICKLE)
            msg = "trickle charge";
        else
            msg = "not charging";
#else
        msg = "charging";
#endif
        lcd_puts(0, 0, msg);
        {
            char buf[32];
            int battv = battery_voltage();
            snprintf(buf, sizeof(buf), "%d.%02dV %d%%",
                battv / 1000, (battv % 1000) / 10, battery_level());
            lcd_puts(0, 1, buf);
        }
        lcd_update();

        button = button_get_w_tmo(HZ/2);
#ifdef BUTTON_ON
        if (button == (BUTTON_ON | BUTTON_REL))
#else
        if (button == (BUTTON_RIGHT | BUTTON_REL))
#endif
            break; /* start */
        else
        {
            if (usb_detect() == USB_INSERTED)
                break;
            else if (!charger_inserted())
                power_off(); /* charger removed: power down */
        }
    } while (1);
}
Пример #6
0
void drawBattery()
{
	uint battLevel = battery_voltage();
	const byte* battIcon;
	if(battLevel < 3200)
		battIcon = battIconEmpty;
	else if(battLevel < 3700)
		battIcon = battIconLow;
	else if(battLevel < 4000)
		battIcon = battIconHigh;
	else
		battIcon = battIconFull;

	draw_bitmap_s2(&newImage(0, FRAME_HEIGHT - 8, battIcon, 16, 8, WHITE, NOINVERT, 0));
}
Пример #7
0
static inline void charger_control(void)
{
    switch (charge_state)
    {
    case DISCHARGING:
    {
        unsigned int millivolts;
        unsigned int thresh = batt_threshold;

        if (BATT_FULL_VOLTAGE == thresh)
        {
            /* Wait for CHG_status to be indicated. */
            if ((ascodec_read(AS3514_IRQ_ENRD0) & CHG_STATUS) == 0)
                break;

            batt_threshold = BATT_VAUTO_RECHARGE;
        }

        millivolts = battery_voltage();

        if (millivolts <= thresh)
            enable_charger();
        break;
        } /* DISCHARGING: */

    case CHARGING:
    {
        if ((ascodec_read(AS3514_IRQ_ENRD0) & CHG_ENDOFCH) == 0)
        {
            if (--charger_total_timer > 0)
                break;

            /* Timer ran out - require replug. */
            charge_state = CHARGE_STATE_ERROR;
        }
        /* else end of charge */

        disable_charger();
        break;
        } /* CHARGING: */

    default:
        /* DISABLED, ERROR */
        break;
    }
}
Пример #8
0
void display_bios_status(int data, int max_data)
{
    static U8 first_flag = 1;

    if (first_flag)
    {
        display_clear(0);
        display_goto_xy(0, 0);
        display_string(VERSION);
        display_goto_xy(0, 1);
        display_string("================");
    }

    display_goto_xy(0, 2);
    display_string("BATT:");
    display_unsigned(battery_voltage()/100, 0);

    if (first_flag)
    {
        display_goto_xy(0, 3);
        display_string("UPLOAD: READY   ");
        first_flag = 0;
    }
    else
    {
        if (data >= UPLOAD_IN_PROGRESS)
        {
            display_goto_xy(0, 3);
            display_string("UPLOAD: PROGRESS");
            display_progress_bar(4, data, max_data);
        }
        else if (data == UPLOAD_FAILED)
        {
            display_goto_xy(0, 3);
            display_string("UPLOAD: FAILED  ");
        }
        else if (data == UPLOAD_FINISHED)
        {
            display_goto_xy(0, 3);
            display_string("UPLOAD: FINISHED");
            /* clear progress bar */
            display_progress_bar(4, 0, 100);
        }
    }
    display_update();
}
Пример #9
0
void battery_read_status(nyx_battery_status_t *state)
{
	if (state) {
		memset(state,0,sizeof(nyx_battery_status_t));

		state->present = battery_is_present();
		if (state->present) {
			state->percentage = battery_percent();
			state->temperature = battery_temperature();
			state->current = battery_current();
			state->voltage = battery_voltage();
			state->avg_current = battery_current();
			state->capacity = battery_coulomb();
			state->capacity_raw = battery_rawcoulomb();
			state->capacity_full40 = battery_full40();
			state->age=battery_age();
			if (state->avg_current >  0)
				state->charging = true;
		}
		else
			state->charging = false;
	}
}
Пример #10
0
static void charging_display_info(bool animate)
{
    unsigned char charging_logo[36];
    const int pox_x = (LCD_WIDTH - sizeof(charging_logo)) / 2;
    const int pox_y = 32;
    static unsigned phase = 3;
    unsigned i;

#ifdef NEED_ATA_POWER_BATT_MEASURE
    if (ide_powered()) /* FM and V2 can only measure when ATA power is on */
#endif
    {
        int battv = battery_voltage();
        lcd_putsf(0, 7, "  Batt: %d.%02dV %d%%  ", battv / 1000,
                 (battv % 1000) / 10, battery_level());
    }

#ifdef ARCHOS_RECORDER
    lcd_puts(0, 2, "Charge mode:");

    const char *s;
    if (charge_state == CHARGING)
        s = str(LANG_BATTERY_CHARGE);
    else if (charge_state == TOPOFF)
        s = str(LANG_BATTERY_TOPOFF_CHARGE);
    else if (charge_state == TRICKLE)
        s = str(LANG_BATTERY_TRICKLE_CHARGE);
    else
        s = "not charging";

    lcd_puts(0, 3, s);
    if (!charger_enabled())
        animate = false;
#endif /* ARCHOS_RECORDER */

    /* middle part */
    memset(charging_logo+3, 0x00, 32);
    charging_logo[0] = 0x3C;
    charging_logo[1] = 0x24;
    charging_logo[2] = charging_logo[35] = 0xFF;

    if (!animate)
    {   /* draw the outline */
        /* middle part */
        lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
                        sizeof(charging_logo), 8);
        lcd_set_drawmode(DRMODE_FG);
        /* upper line */
        charging_logo[0] = charging_logo[1] = 0x00;
        memset(charging_logo+2, 0x80, 34);
        lcd_mono_bitmap(charging_logo, pox_x, pox_y, sizeof(charging_logo), 8);
        /* lower line */
        memset(charging_logo+2, 0x01, 34);
        lcd_mono_bitmap(charging_logo, pox_x, pox_y + 16,
                        sizeof(charging_logo), 8);
        lcd_set_drawmode(DRMODE_SOLID);
    }
    else
    {   /* animate the middle part */
        for (i = 3; i<MIN(sizeof(charging_logo)-1, phase); i++)
        {
            if ((i-phase) % 8 == 0)
            {   /* draw a "bubble" here */
                unsigned bitpos;
                bitpos = (phase + i/8) % 15; /* "bounce" effect */
                if (bitpos > 7)
                    bitpos = 14 - bitpos;
                charging_logo[i] = BIT_N(bitpos);
            }
        }
        lcd_mono_bitmap(charging_logo, pox_x, pox_y + 8,
                        sizeof(charging_logo), 8);
        phase++;
    }
    lcd_update();
}
Пример #11
0
/*
 * main function
 */
int main(void)
{
    /* hardware on port D:
     * PD0: RX
     * PD1: TX
     * PD2: D+
     * PD3: INT1/IR_IN
     * PD4: D-
     * PD5: IR_OUT
     * PD7: DF_CS
     */
    DDRD = _BV(PD5) | _BV(PD7);
    PORTD = _BV(PD7);

    /* configure pin change interrupt for button 1 and 2,
     * for waking up from sleep mode... */
    PCMSK1 = _BV(PCINT12) | _BV(PCINT13);

    /* hardware on port B:
     * PB0: PUD
     * PB1: /LED2
     * PB2: /LED1
     * PB3: MOSI
     * PB4: MISO
     * PB5: SCK
     */
    DDRB = _BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3) | _BV(PB5);
    PORTB = _BV(PB1) | _BV(PB2);

    /* hardware on port C:
     * PC0: IR_IN2
     * PC1: POWER
     * PC2: BTN4
     * PC3: BTN3
     * PC4: BTN2
     * PC5: BTN1
     */
    DDRC = 0;
    PORTC = _BV(PC2) | _BV(PC3) | _BV(PC4) | _BV(PC5);

    /* init analog to digital converter,
     * convert on PC1(ADC1)
     * prescaler 64 => 250khz frequency */
    ADMUX = _BV(REFS0) | _BV(ADLAR) | _BV(ADIF) | _BV(MUX0);
    ADCSRA = _BV(ADEN) | _BV(ADPS1);

    /* init spi */
    SPCR = _BV(SPE) | _BV(MSTR);
    SPSR |= _BV(SPI2X);

    /* init timer2 for key debouncing, CTC, prescaler 1024,
     * timeout after 10ms */
    TCCR2A = _BV(WGM21);
    TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
    TIMSK2 = _BV(OCIE2A);
    OCR2A = F_CPU/100/1024;

    /* configure sleep mode */
    set_sleep_mode(SLEEP_MODE_STANDBY);

#ifdef DEBUG_UART
    /* uart */
    UBRR0H = 0;
    UBRR0L = 8;
    UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
    UCSR0B = _BV(TXEN0);

    UDR0 = 'b';
    while(!(UCSR0A & _BV(UDRE0)));
#endif

    /* read dataflash status */
    df_select();
    SPDR = 0xd7;
    while(!(SPSR & _BV(SPIF)));
    SPDR = 0;
    while(!(SPSR & _BV(SPIF)));

    uint8_t df_status = SPDR;

    /* read battery voltage */
    uint8_t bat = battery_voltage();

#ifdef DEBUG_UART
    UDR0 = 'D';
    while(!(UCSR0A & _BV(UDRE0)));
    UDR0 = df_status;
    while(!(UCSR0A & _BV(UDRE0)));
    UDR0 = 'V';
    while(!(UCSR0A & _BV(UDRE0)));
    UDR0 = bat;
    while(!(UCSR0A & _BV(UDRE0)));
#endif

    df_release();

    sei();

#ifdef BLINK_START
    /* blink start sequence */
    blink(BLINK_START);
#endif

    if (df_status == DF_STATUS_IDLE)
        blink(BLINK_DF_SEEN);
    else
        blink(BLINK_DF_ERROR);

    if (bat >= MIN_BAT_VOLTAGE)
        blink(BLINK_BAT_OK);
    else
        blink(BLINK_BAT_FAIL);

    uint8_t pos;
    uint8_t button_sum = 0;

    while (1)
    {
        /* if a button has been pressed and we're idle, wait some more time to determine mode */
        if (state == IDLE && (button_press[0] > 0 || button_press[1] > 0)) {

            /* wait one second via timer1, using prescaler 1024 */
            TIFR1 = TIFR1;
            OCR1A = F_CPU/1024/2;
            TCCR1B = _BV(CS12) | _BV(CS10);

            button_sum = 0;
            state = READ_COMMAND;
        }

        /* if we're waiting for a command, and some button has been pressed, reset timeout */
        uint8_t sum = button_press[0] + button_press[1];
        if (state == READ_COMMAND && sum != button_sum) {
            TCNT1 = 0;
            button_sum = sum;
        }

        /* if we're waiting for a command, check if the timer expired */
        if (state == READ_COMMAND && (TIFR1 & _BV(OCF1A))) {

            /* disable timer1 */
            TCCR1B = 0;
            TCNT1 = 0;

            /* parse button presses */
            if (button_press[0] == 1 && button_press[1] == 0) {

                if (battery_voltage() < MIN_BAT_VOLTAGE && !options.ignore_bat) {
                    blink(BLINK_VOLTAGE);
                } else {

                    /* start transmitting */
                    pos = 0;

                    current_code = &codes[0];
                    single_code = &codes[0];
                    state = LOAD_CODE;

#ifdef BLINK_MODE1
                    /* blink mode 1 */
                    blink(BLINK_MODE1);
#endif

                    if (!options.silent)
                        PORTB &= ~_BV(PB1);
                }
            } else if (button_press[0] == 0) {
                if (button_press[1] == 1) {
                    options.silent = !options.silent;

                    /* blink for silent toggle */
                    if (options.silent)
                        blink(BLINK_SILENT);
                    else
                        blink(BLINK_NONSILENT);

                } else if (button_press[1] == 2) {
                    options.single_step = !options.single_step;

                    /* blink for single step toggle */
                    if (options.single_step)
                        blink(BLINK_STEP);
                    else
                        blink(BLINK_NOSTEP);
                } else if (button_press[1] == 3) {
                    options.ignore_bat = !options.ignore_bat;

                    /* blink for ignore battery toggle */
                    if (options.ignore_bat)
                        blink(BLINK_BAT_IGNORE);
                    else
                        blink(BLINK_BAT_HONOR);
                } else
                    blink(BLINK_INVALID);
            } else
                blink(BLINK_INVALID);

            /* reset state, if not yet done */
            if (state == READ_COMMAND)
                state = IDLE;

            /* reset buttons */
            button_press[0] = 0;
            button_press[1] = 0;
            button_press[2] = 0;
        }

        if (state == LOAD_CODE) {

            /* if we're in single-step mode, wait for a keypress */
            if (options.single_step) {
                while (button_press[0] == 0 && button_press[1] == 0 && button_press[2] == 0);

                /* send next code if button1 has been pressed, stop if button2
                 * has been pressed, resend code if button3 has been pressed */
                if (button_press[2]) {
                    current_code = single_code;
                    button_press[1] = 0;
                } else if (!button_press[1]) {
                    button_press[1] = 0;
                }

                /* reset buttons */
                button_press[0] = 0;
                button_press[2] = 0;
            }

            /* stop sending if button2 has been pressed */
            if (button_press[1] > 0) {
                button_press[0] = 0;
                button_press[1] = 0;
                PORTB |= _BV(PB1);

#ifdef BLINK_MODE1_END
                blink(BLINK_MODE1_END);
#endif

                state = IDLE;
            } else {

#ifdef DEBUG_UART
                UDR0 = 'L';
                while(!(UCSR0A & _BV(UDRE0)));
#endif
                /* if we're in single step mode, remember code address for single-retransmit */
                single_code = current_code;

                /* load next generating function and generate timing table */
                void (*func)(void);
                uint16_t ptr = next_word();
                func = (void *)ptr;

                if (func != NULL) {
#ifdef DEBUG_UART
                    UDR0 = 'p';
                    while(!(UCSR0A & _BV(UDRE0)));
                    UDR0 = pos++;
                    while(!(UCSR0A & _BV(UDRE0)));
#endif

                    /* call generating function */
                    func();

#ifdef DEBUG_UART
                    UDR0 = 'f';
                    while(!(UCSR0A & _BV(UDRE0)));
#endif

                    /* reset timing pointer */
                    current_timing = &timing[0];

                    /* update state */
                    state = TRANSMIT_CODE;

                    /* init timer1 for initial delay before sending:
                     * prescaler 256, CTC mode (TOP == OCR1A)
                     * enable compare interrupts */
                    OCR1A = DELAY_NEXT_CODE;
                    OCR1B = 0xffff;
                    TIFR1 = TIFR1;
                    TIMSK1 = _BV(OCIE1A) | _BV(OCIE1B);
                    TCCR1B = _BV(CS12) | _BV(WGM12);
                } else {
#ifdef DEBUG_UART
                    UDR0 = 'E';
                    while(!(UCSR0A & _BV(UDRE0)));
#endif

                    PORTB |= _BV(PB1);

#ifdef BLINK_MODE1_END
                    blink(BLINK_MODE1_END);
#endif

                    state = IDLE;
                }

                sleep_counter = 0;
            }
        }

        if (state == SLEEP) {
            /* enable pin change interrupt for button 1 and 2,
             * for waking up from sleep mode... */
            PCICR = _BV(PCIE1);

            /* set and enter sleep mode */
            sleep_mode();
            sleep_counter = SLEEP_COUNTER_VALUE;
            state = IDLE;

            /* disable pin change interrupt for button 1 and 2,
             * for waking up from sleep mode... */
            PCICR = 0;

        }
    }
}
Пример #12
0
/**
 * NOTE: The technique is not the same as that used in TinyVM.
 * The return value indicates the impact of the call on the VM
 * system. EXEC_CONTINUE normal return the system should return to the return
 * address provided by the VM. EXEC_RUN The call has modified the value of
 * VM PC and this should be used to restart execution. EXEC_RETRY The call
 * needs to be re-tried (typically for a GC failure), all global state
 * should be left intact, the PC has been set appropriately.
 *
 */
int dispatch_native(TWOBYTES signature, STACKWORD * paramBase)
{
  STACKWORD p0 = paramBase[0];
  switch (signature) {
  case wait_4_5V:
    return monitor_wait((Object *) word2ptr(p0), 0);
  case wait_4J_5V:
    return monitor_wait((Object *) word2ptr(p0), ((int)paramBase[1] > 0 ? 0x7fffffff : paramBase[2]));
  case notify_4_5V:
    return monitor_notify((Object *) word2ptr(p0), false);
  case notifyAll_4_5V:
    return monitor_notify((Object *) word2ptr(p0), true);
  case start_4_5V:
    // Create thread, allow for instruction restart
    return init_thread((Thread *) word2ptr(p0));
  case yield_4_5V:
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case sleep_4J_5V:
    sleep_thread(((int)p0 > 0 ? 0x7fffffff : paramBase[1]));
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case getPriority_4_5I:
    push_word(get_thread_priority((Thread *) word2ptr(p0)));
    break;
  case setPriority_4I_5V:
    {
      STACKWORD p = (STACKWORD) paramBase[1];

      if (p > MAX_PRIORITY || p < MIN_PRIORITY)
	return throw_new_exception(JAVA_LANG_ILLEGALARGUMENTEXCEPTION);
      else
	set_thread_priority((Thread *) word2ptr(p0), p);
    }
    break;
  case currentThread_4_5Ljava_3lang_3Thread_2:
    push_ref(ptr2ref(currentThread));
    break;
  case interrupt_4_5V:
    interrupt_thread((Thread *) word2ptr(p0));
    break;
  case interrupted_4_5Z:
    {
      JBYTE i = currentThread->interruptState != INTERRUPT_CLEARED;

      currentThread->interruptState = INTERRUPT_CLEARED;
      push_word(i);
    }
    break;
  case isInterrupted_4_5Z:
    push_word(((Thread *) word2ptr(p0))->interruptState
	      != INTERRUPT_CLEARED);
    break;
  case join_4_5V:
    join_thread((Thread *) word2ptr(p0), 0);
    break;
  case join_4J_5V:
    join_thread((Thread *) word2obj(p0), paramBase[2]);
    break;
  case halt_4I_5V:
    schedule_request(REQUEST_EXIT);
    break;
  case shutdown_4_5V:
    shutdown_program(false);
    break;
  case currentTimeMillis_4_5J:
    push_word(0);
    push_word(systick_get_ms());
    break;
  case readSensorValue_4I_5I:
    push_word(sp_read(p0, SP_ANA));
    break;
  case setPowerTypeById_4II_5V:
    sp_set_power(p0, paramBase[1]);
    break;
  case freeMemory_4_5J:
    push_word(0);
    push_word(getHeapFree());
    break;
  case totalMemory_4_5J:
    push_word(0);
    push_word(getHeapSize());
    break;
  case floatToRawIntBits_4F_5I:	// Fall through
  case intBitsToFloat_4I_5F:
    push_word(p0);
    break;
  case doubleToRawLongBits_4D_5J:	// Fall through
  case longBitsToDouble_4J_5D:
    push_word(p0);
    push_word(paramBase[1]);
    break;
  case drawString_4Ljava_3lang_3String_2II_5V:
    {
      String *p = (String *)word2obj(p0);
      Object *charArray;
      if (!p) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION);
      charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));
      if (!charArray) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION);
      display_goto_xy(paramBase[1], paramBase[2]);
      display_jstring(p);
    }
    break;
  case drawInt_4III_5V:
    display_goto_xy(paramBase[1], paramBase[2]);
    display_int(p0, 0);
    break;
  case drawInt_4IIII_5V:
     display_goto_xy(paramBase[2], paramBase[3]);
     display_int(p0, paramBase[1]);
    break;   
  case asyncRefresh_4_5V:
    display_update();
    break;
  case clear_4_5V:
    display_clear(0);
    break;
  case getDisplay_4_5_1B:
    push_word(display_get_array());
    break;
  case setAutoRefreshPeriod_4I_5I:
    push_word(display_set_auto_update_period(p0));
    break;
  case getRefreshCompleteTime_4_5I:
    push_word(display_get_update_complete_time());
    break;
  case bitBlt_4_1BIIII_1BIIIIIII_5V:
    {
      Object *src = word2ptr(p0);
      Object *dst = word2ptr(paramBase[5]);
      display_bitblt((byte *)(src != NULL ?jbyte_array(src):NULL), paramBase[1], paramBase[2], paramBase[3], paramBase[4], (byte *)(dst!=NULL?jbyte_array(dst):NULL), paramBase[6], paramBase[7], paramBase[8], paramBase[9], paramBase[10], paramBase[11], paramBase[12]);
      break;
    }
  case getSystemFont_4_5_1B:
    push_word(display_get_font());
    break;
  case setContrast_4I_5V:
    nxt_lcd_set_pot(p0);
    break;
  case getBatteryStatus_4_5I:
    push_word(battery_voltage());
    break;
  case getButtons_4_5I:
    push_word(buttons_get());
    break;
  case getTachoCountById_4I_5I:
    push_word(nxt_motor_get_count(p0));
    break;
  case controlMotorById_4III_5V:
    nxt_motor_set_speed(p0, paramBase[1], paramBase[2]); 
    break;
  case resetTachoCountById_4I_5V:
    nxt_motor_set_count(p0, 0);
    break;
  case i2cEnableById_4II_5V:
    if (i2c_enable(p0, paramBase[1]) == 0)
      return EXEC_RETRY;
    else
      break;
  case i2cDisableById_4I_5V:
    i2c_disable(p0);
    break;
  case i2cStatusById_4I_5I:
    push_word(i2c_status(p0));
    break;
  case i2cStartById_4II_1BIII_5I:
    {
    	Object *p = word2obj(paramBase[2]);
    	JBYTE *byteArray = p ? jbyte_array(p) + paramBase[3] : NULL;
    	push_word(i2c_start(p0,
    	                    paramBase[1],
    	                    (U8 *)byteArray,
    	                    paramBase[4],
    	                    paramBase[5]));
    }
    break; 
  case i2cCompleteById_4I_1BII_5I:
    {
    	Object *p = word2ptr(paramBase[1]);
    	JBYTE *byteArray = p ? jbyte_array(p) + paramBase[2] : NULL;
    	push_word(i2c_complete(p0,
    	                       (U8 *)byteArray,
    	                       paramBase[3]));
    }
    break; 
  case playFreq_4III_5V:
    sound_freq(p0,paramBase[1], paramBase[2]);
    break;
  case btGetBC4CmdMode_4_5I:
    push_word(bt_get_mode());
    break;
  case btSetArmCmdMode_4I_5V:
    if (p0 == 0) bt_set_arm7_cmd();
    else bt_clear_arm7_cmd(); 
    break;
  case btSetResetLow_4_5V:
    bt_set_reset_low();
    break;
  case btSetResetHigh_4_5V:
    bt_set_reset_high();
    break;
  case btWrite_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(bt_write(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case btRead_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(bt_read(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case btPending_4_5I:
    {
      push_word(bt_event_check(0xffffffff));
    }
    break;
  case btEnable_4_5V:
    if (bt_enable() == 0)
      return EXEC_RETRY;
    else
      break;
  case btDisable_4_5V:
    bt_disable();
    break;
  case usbRead_4_1BII_5I:
     {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(udp_read(byteArray,paramBase[1], paramBase[2]));
    } 
    break;
  case usbWrite_4_1BII_5I:
     {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(udp_write(byteArray,paramBase[1], paramBase[2]));                      
    }
    break; 
  case usbStatus_4_5I:
    {
      push_word(udp_event_check(0xffffffff));
    }
    break;
  case usbEnable_4I_5V:
    {
      udp_enable(p0);
    }
    break;
  case usbDisable_4_5V:
    {
      udp_disable();
    }
    break;
  case usbReset_4_5V:
    udp_reset();
    break; 
  case usbSetSerialNo_4Ljava_3lang_3String_2_5V: 
    {
      byte *p = word2ptr(p0);
      int len;
      Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));

      len = get_array_length(charArray);
      udp_set_serialno((U8 *)jchar_array(charArray), len);
    }
    break;
  case usbSetName_4Ljava_3lang_3String_2_5V:
    {
      byte *p = word2ptr(p0);
      int len;
      Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));

      len = get_array_length(charArray);
      udp_set_name((U8 *)jchar_array(charArray), len);
    }
    break;
  case flashWritePage_4_1BI_5I:
    {
      Object *p = word2ptr(p0);
      unsigned long *intArray = (unsigned long *) jint_array(p);
      push_word(flash_write_page(intArray,paramBase[1]));                      
    }
    break;
  case flashReadPage_4_1BI_5I:
    {
      Object *p = word2ptr(p0);
      unsigned long *intArray = (unsigned long *) jint_array(p);
      push_word(flash_read_page(intArray,paramBase[1]));                      
    }
    break;
  case flashExec_4II_5I:
    push_word(run_program((byte *)(&FLASH_BASE[(p0*FLASH_PAGE_SIZE)]), paramBase[1]));
    break;
  case playSample_4IIIII_5V:
    sound_play_sample(((unsigned char *) &FLASH_BASE[(p0*FLASH_PAGE_SIZE)]) + paramBase[1],paramBase[2],paramBase[3],paramBase[4]);
    break;
  case playQueuedSample_4_1BIIII_5I:
    push_word(sound_add_sample((U8 *)jbyte_array(word2obj(p0)) + paramBase[1],paramBase[2],paramBase[3],paramBase[4]));
    break;
  case getTime_4_5I:
    push_word(sound_get_time());
    break;
  case getDataAddress_4Ljava_3lang_3Object_2_5I:
    if (is_array(word2obj(p0)))
      push_word (ptr2word ((byte *) array_start(word2ptr(p0))));
    else
      push_word (ptr2word ((byte *) fields_start(word2ptr(p0))));
    break;
  case getObjectAddress_4Ljava_3lang_3Object_2_5I:
    push_word(p0);
    break;
  case gc_4_5V:
    // Restartable garbage collection
    return garbage_collect();
  case shutDown_4_5V:
    shutdown(); // does not return
  case boot_4_5V:
    display_clear(1);
    while (1) nxt_avr_firmware_update_mode(); // does not return 
  case arraycopy_4Ljava_3lang_3Object_2ILjava_3lang_3Object_2II_5V:
    return arraycopy(word2ptr(p0), paramBase[1], word2ptr(paramBase[2]), paramBase[3], paramBase[4]);
  case executeProgram_4I_5V:
    // Exceute program, allow for instruction re-start
    return execute_program(p0);
  case setDebug_4_5V:
    set_debug(word2ptr(p0));
    break;
  case eventOptions_4II_5I:
    {
      byte old = debugEventOptions[p0];
      debugEventOptions[p0] = (byte)paramBase[1];
      push_word(old);
    }
    break;
  case suspendThread_4Ljava_3lang_3Object_2_5V:
    suspend_thread(ref2ptr(p0));
    break;
  case resumeThread_4Ljava_3lang_3Object_2_5V:
    resume_thread(ref2ptr(p0));
    break;
  case getProgramExecutionsCount_4_5I:
    push_word(gProgramExecutions);
    break;
  case getFirmwareRevision_4_5I:
    push_word((STACKWORD) getRevision());
    break;
  case getFirmwareRawVersion_4_5I:
    push_word((STACKWORD) VERSION_NUMBER); 
    break;
  case hsEnable_4II_5V:
    {
      if (hs_enable((int)p0, (int)paramBase[1]) == 0)
        return EXEC_RETRY;
    }
    break;
  case hsDisable_4_5V:
    {
      hs_disable();
    }
    break;
  case hsWrite_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(hs_write(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case hsRead_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(hs_read(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case hsPending_4_5I:
    {
      push_word(hs_pending());
    }
    break;
  case hsSend_4BB_1BII_1C_5I:
    {
      Object *p = word2ptr(paramBase[2]);
      U8 *data = (U8 *)jbyte_array(p);
      p = word2ptr(paramBase[5]);
      U16 *crc = (U16 *)jchar_array(p);
      push_word(hs_send((U8) p0, (U8)paramBase[1], data, paramBase[3], paramBase[4], crc));
    }
    break;
  case hsRecv_4_1BI_1CI_5I:
    {
      Object *p = word2ptr(p0);
      U8 *data = (U8 *)jbyte_array(p);
      p = word2ptr(paramBase[2]);
      U16 *crc = (U16 *)jchar_array(p);
      push_word(hs_recv(data, paramBase[1], crc, paramBase[3]));
    }
    break;
    
  case getUserPages_4_5I:
    push_word(FLASH_MAX_PAGES - flash_start_page);
    break;
  case setVMOptions_4I_5V:
    gVMOptions = p0;
    break;
  case getVMOptions_4_5I:
    push_word(gVMOptions);
    break;
  case isAssignable_4II_5Z:
    push_word(is_assignable(p0, paramBase[1]));
    break;
  case cloneObject_4Ljava_3lang_3Object_2_5Ljava_3lang_3Object_2:
    {
      Object *newObj = clone((Object *)ref2obj(p0));
      if (newObj == NULL) return EXEC_RETRY;
      push_word(obj2ref(newObj));
    }
    break;
  case memPeek_4III_5I:
    push_word(mem_peek(p0, paramBase[1], paramBase[2]));
    break;
  case memCopy_4Ljava_3lang_3Object_2IIII_5V:
    mem_copy(word2ptr(p0), paramBase[1], paramBase[2], paramBase[3], paramBase[4]);
    break;
  case memGetReference_4II_5Ljava_3lang_3Object_2:
    push_word(mem_get_reference(p0, paramBase[1]));
    break;
  case setSensorPin_4III_5V:
    sp_set(p0, paramBase[1], paramBase[2]);
    break;
  case getSensorPin_4II_5I:
    push_word(sp_get(p0, paramBase[1]));
    break;
  case setSensorPinMode_4III_5V:
    sp_set_mode(p0, paramBase[1], paramBase[2]);
    break;
  case readSensorPin_4II_5I:
    push_word(sp_read(p0, paramBase[1]));
    break;
  case nanoTime_4_5J:
    {
      U64 ns = systick_get_ns();
      push_word(ns >> 32);
      push_word(ns);
    }
    break;
  case createStackTrace_4Ljava_3lang_3Thread_2Ljava_3lang_3Object_2_5_1I:
    {
      Object *trace = create_stack_trace((Thread *)ref2obj(p0), ref2obj(paramBase[1]));
      if (trace == NULL) return EXEC_RETRY;
      push_word(obj2ref(trace));
    }
    break;
  case registerEvent_4_5I:
    push_word(register_event((NXTEvent *) ref2obj(p0)));
    break;
  case unregisterEvent_4_5I:
    push_word(unregister_event((NXTEvent *) ref2obj(p0)));
    break;
  case changeEvent_4II_5I:
    push_word(change_event((NXTEvent *) ref2obj(p0), paramBase[1], paramBase[2]));
    break;
  case isInitialized_4I_5Z:
    push_word(is_initialized_idx(p0));
    break;
  case allocate_4II_5Ljava_3lang_3Object_2:
    {
      Object *allocated;
      if(paramBase[1]>0){
        allocated=new_single_array(p0,paramBase[1]);
      }else{
        allocated=new_object_for_class(p0);
      }
      if(allocated == NULL) return EXEC_RETRY;
      push_word(obj2ref(allocated));
    }
    break;
  case memPut_4IIII_5V:
    store_word_ns((byte *)(memory_base[p0] + paramBase[1]), paramBase[2],paramBase[3]);
    break;
  case notifyEvent_4ILjava_3lang_3Thread_2_5Z:
    push_word(debug_event(paramBase[1], NULL, (Thread*) ref2obj(paramBase[2]), 0, 0, 0, 0));
    break;
  case setThreadRequest_4Ljava_3lang_3Thread_2Llejos_3nxt_3debug_3SteppingRequest_2_5V:
    {
      Thread *th = (Thread*) ref2obj(p0);
      th->debugData = (REFERENCE) paramBase[1];
      // currently we only get stepping requests
      if(paramBase[1])
        th->flags |= THREAD_STEPPING;
      else
        th->flags &= ~THREAD_STEPPING;
    }
    break;
  case isStepping_4Ljava_3lang_3Thread_2_5Z:
    {
      Thread *th = (Thread*) ref2obj(p0);
      push_word(is_stepping(th));
    }
    break;
  case setBreakpointList_4_1Llejos_3nxt_3debug_3Breakpoint_2I_5V:
    breakpoint_set_list((Breakpoint**) array_start(p0), paramBase[1]);
    break;
  case enableBreakpoint_4Llejos_3nxt_3debug_3Breakpoint_2Z_5V:
    breakpoint_enable((Breakpoint*) word2ptr(p0), (boolean) paramBase[1]);
    break;
  case firmwareExceptionHandler_4Ljava_3lang_3Throwable_2II_5V:
    firmware_exception_handler((Throwable *)p0, paramBase[1], paramBase[2]);
    break;
  case exitThread_4_5V:
    currentThread->state = DEAD;
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case updateThreadFlags_4Ljava_3lang_3Thread_2II_5I:
    ((Thread *)p0)->flags |= paramBase[1];
    ((Thread *)p0)->flags &= ~paramBase[2];
//printf("m %x %d\n", p0, ((Thread *)p0)->flags);
    push_word(((Thread *)p0)->flags);
    break;
    
  default:
    return throw_new_exception(JAVA_LANG_NOSUCHMETHODERROR);
  }
  return EXEC_CONTINUE;
}
Пример #13
0
void rn42_task(void)
{
    int16_t c;
    // Raw mode: interpret output report of LED state
    while ((c = rn42_getc()) != -1) {
        // LED Out report: 0xFE, 0x02, 0x01, <leds>
        // To get the report over UART set bit3 with SH, command.
        static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
        switch (state) {
            case LED_INIT:
                if (c == 0xFE) state = LED_FE;
                else {
                    if (0x0 <= c && c <= 0x7f) xprintf("%c", c);
                    else xprintf(" %02X", c);
                }
                break;
            case LED_FE:
                if (c == 0x02) state = LED_02;
                else           state = LED_INIT;
                break;
            case LED_02:
                if (c == 0x01) state = LED_01;
                else           state = LED_INIT;
                break;
            case LED_01:
                dprintf("LED status: %02X\n", c);
                rn42_set_leds(c);
                state = LED_INIT;
                break;
            default:
                state = LED_INIT;
        }
    }



    static uint16_t prev_timer = 0;
    uint16_t e = timer_elapsed(prev_timer);
    if (e > 1000) {
        /* every second */
        prev_timer += e/1000*1000;

        /* Low voltage alert */
        uint8_t bs = battery_status();
        if (bs == LOW_VOLTAGE) {
            battery_led(LED_ON);
        } else {
            battery_led(LED_CHARGER);
        }

        /* every minute */
        uint32_t t = timer_read32()/1000;
        if (t%60 == 0) {
            uint16_t v = battery_voltage();
            uint8_t h = t/3600;
            uint8_t m = t%3600/60;
            uint8_t s = t%60;
            dprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
        }
    }


    /* Connection monitor */
    if (!rn42_rts() && rn42_linked()) {
        status_led(true);
    } else {
        status_led(false);
    }
}
Пример #14
0
bool command_extra(uint8_t code)
{
    uint32_t t;
    uint16_t b;
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            print("\n\n----- Bluetooth RN-42 Help -----\n");
            print("i:       RN-42 info\n");
            print("b:       battery voltage\n");
            print("Del:     enter/exit RN-42 config mode\n");
            print("Slck:    RN-42 initialize\n");

            print("1-4:     restore link\n");
            print("F1-F4:   store link\n");

            print("p:       pairing\n");

            if (config_mode) {
                return true;
            } else {
                print("u:       toggle Force USB mode\n");
                return false;   // to display default command help
            }
        case KC_P:
            pairing();
            return true;

        /* Store link address to EEPROM */
        case KC_F1:
            store_link(RN42_LINK0);
            return true;
        case KC_F2:
            store_link(RN42_LINK1);
            return true;
        case KC_F3:
            store_link(RN42_LINK2);
            return true;
        case KC_F4:
            store_link(RN42_LINK3);
            return true;
        /* Restore link address to EEPROM */
        case KC_1:
            restore_link(RN42_LINK0);
            return true;
        case KC_2:
            restore_link(RN42_LINK1);
            return true;
        case KC_3:
            restore_link(RN42_LINK2);
            return true;
        case KC_4:
            restore_link(RN42_LINK3);
            return true;
	    
        case KC_5:
	  xprintf("blah! \n");
            //rn42_cts_hi(); 
	    if (stay_connected == 1){
	      dprintf("Disconnect after 5 min.\n");		
	      stay_connected = !stay_connected;
	    }else{
	      dprintf("Stay connected.\n");
	      stay_connected = 1;
	      //last_press_timer = timer_read32();
	    }
	    return true;

        case KC_6:
            clear_keyboard();
            host_set_driver(&rn42_config_driver);   // null driver; not to send a key to host
            rn42_disconnect();
            return true;	    

        case KC_I:
            print("\n----- RN-42 info -----\n");
            xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
            xprintf("force_usb: %X\n", force_usb);
            xprintf("rn42: %s\n", rn42_rts() ? "OFF" : (rn42_linked() ? "CONN" : "ON"));
            xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
            xprintf("config_mode: %X\n", config_mode);
            xprintf("USB State: %s\n",
                    (USB_DeviceState == DEVICE_STATE_Unattached) ? "Unattached" :
                    (USB_DeviceState == DEVICE_STATE_Powered) ? "Powered" :
                    (USB_DeviceState == DEVICE_STATE_Default) ? "Default" :
                    (USB_DeviceState == DEVICE_STATE_Addressed) ? "Addressed" :
                    (USB_DeviceState == DEVICE_STATE_Configured) ? "Configured" :
                    (USB_DeviceState == DEVICE_STATE_Suspended) ? "Suspended" : "?");
            xprintf("battery: ");
            switch (battery_status()) {
                case FULL_CHARGED:  xprintf("FULL"); break;
                case CHARGING:      xprintf("CHARG"); break;
                case DISCHARGING:   xprintf("DISCHG"); break;
                case LOW_VOLTAGE:   xprintf("LOW"); break;
                default:            xprintf("?"); break;
            };
            xprintf("\n");
            xprintf("RemoteWakeupEnabled: %X\n", USB_Device_RemoteWakeupEnabled);
            xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
            t = timer_read32()/1000;
            uint8_t d = t/3600/24;
            uint8_t h = t/3600;
            uint8_t m = t%3600/60;
            uint8_t s = t%60;
            xprintf("uptime: %02u %02u:%02u:%02u\n", d, h, m, s);

            xprintf("LINK0: %s\r\n", get_link(RN42_LINK0));
            xprintf("LINK1: %s\r\n", get_link(RN42_LINK1));
            xprintf("LINK2: %s\r\n", get_link(RN42_LINK2));
            xprintf("LINK3: %s\r\n", get_link(RN42_LINK3));

            return true;
        case KC_B:
            // battery monitor
            t = timer_read32()/1000;
            b = battery_voltage();
            xprintf("BAT: %umV\t", b);
            xprintf("%02u:",   t/3600);
            xprintf("%02u:",   t%3600/60);
            xprintf("%02u\n",  t%60);
            return true;
        case KC_U:
            if (config_mode) return false;
            if (force_usb) {
                print("Auto mode\n");
                force_usb = false;
            } else {
                print("USB mode\n");                
                clear_keyboard();
                host_set_driver(&rn42_driver);
            }
            return true;
        case KC_DELETE:
            /* RN-42 Command mode */
            if (rn42_autoconnecting()) {
                enter_command_mode();

                command_state = CONSOLE;
                config_mode = true;
            } else {
                exit_command_mode();

                command_state = ONESHOT;
                config_mode = false;
            }
            return true;
        case KC_SCROLLLOCK:
            init_rn42();
            return true;
        default:
            if (config_mode)
                return true;
            else
                return false;   // yield to default command
    }
    return true;
}
Пример #15
0
bool battery_is_present(void)
{
	int voltage = battery_voltage();
	return (voltage > 0);
}
Пример #16
0
int main(void) {
  int i;
  int reading1;
  int reading2;
  int address;
  int test_array[100];

  for(i=0;i<100;i++) {
    motor(0,i);  //spin the left motor forward
    motor(1,i);  //spin the right motor forward
  }

  i=0;
  while(i>-100) {
    motor(0,i);  //spin the left motor backwards
    motor(1,i);  //spin the right motor backwards
    i--;
  }

  i=50;
  set_servo(0,i);  //set servo motor 0 to move to 50 degrees
  set_servo(3,i);  //set servo motor 3 to move to 50 degrees

  delay_milliseconds(100);  //pause 100 milliseconds
  delay_seconds(1);  //pause 1 second

  lcd_clear();
  lcd_cursor(0,0);
  printf ("Test1\n"); //the LCD will be 8x2 (8chars x 2lines)
  printf ("Test2\n");

  reading1 = analog(0);  //get a reading from analog pin 0
  reading2 = analog(5);  //get a reading from analog pin 5
  reading1 = digital(0);  //get a reading from digital pin 0
  reading2 = digital(1);  //get a reading from digital pin 1

  if (reading1 > 100) {
    printf ("%d\n", reading1);
  }

  reading1 = accelerometer(0);  //read x-axis
  reading2 = accelerometer(1);  //read y-axis
  reading1 = accelerometer(2);  //read z-axis

  reading1 = battery_voltage();  //battery voltage

  reading1 = read_serial_port();  //get a byte from the serial port
  write_serial_port(reading1);  //send a byte on the serial port

  led1(1);  //turn on on-board led1
  led1(0);  //turn off on-board led1

  reading1 = read_ir();  //get a reading from the IR receiver

  reset();  //reset the board

  write_eeprom(address, reading1);  //write a value to the non-volatile eeprom (these values will be stored across resets)
  reading1 = read_eeprom(address);  //get a reading from the non-volatile eeprom

  reading1 = button(); //read the state of the on-board button

  return 0;
}