Exemplo n.º 1
0
 Shape(size_t n = -1, size_t fdc_id = -1) : m_kind(n), m_all_kinds(0), m_fdc_id(fdc_id),
     m_member0(get_rnd(n+0)),
     m_member1(get_rnd(n+1)),
     m_member2(get_rnd(n+2)),
     m_member3(get_rnd(n+3)),
     m_member4(get_rnd(n+4)),
     m_member5(get_rnd(n+5)),
     m_member6(get_rnd(n+6)),
     m_member7(get_rnd(n+7)),
     m_member8(get_rnd(n+8)),
     m_member9(get_rnd(n+9))
 {}
void bootstrap()
{
    log_print_string("Device booted at time: %d\n", timer_get_counter_value());

    hw_radio_init(p_alloc, p_free);
    hw_radio_set_rx(&rx_cfg, rx_callback, rssi_valid);


    NG(tx_buffer).radio_packet.length = sizeof(packet_struct_t) - sizeof(hw_radio_packet_t);
    NG(tx_buffer).radio_packet.tx_meta.tx_cfg.channel_id = rx_cfg.channel_id;
    NG(tx_buffer).radio_packet.tx_meta.tx_cfg.syncword_class = rx_cfg.syncword_class;
    NG(tx_buffer).radio_packet.tx_meta.tx_cfg.eirp = 0;

	NG(tx_buffer).src_node = hw_get_unique_id();
    NG(tx_buffer).dst_node = 0xFFFF;
    NG(tx_buffer).counter = 0;


    sched_register_task(&send_packet);
    timer_post_task_delay(&send_packet, TIMER_TICKS_PER_SEC + (get_rnd() %TIMER_TICKS_PER_SEC));

//    NG(status).is_rx = false;
//    NG(status).is_sleep = true;
//    NG(status).tx_busy = false;
//    NG(status).rx_busy = false;
//    sched_register_task(&poll_status);
//    sched_post_task(poll_status);

}
void send_packet()
{
	hw_radio_send_packet((hw_radio_packet_t*)(&NG(tx_buffer)), tx_callback);
	log_print_string("Sending packet with counter %u", NG(tx_buffer).counter);
	NG(tx_buffer).counter++;
	timer_post_task_delay(send_packet, TIMER_TICKS_PER_SEC + (get_rnd() %TIMER_TICKS_PER_SEC));
}
static void init_master_session(d7asp_master_session_t* session) {
    session->state = D7ASP_MASTER_SESSION_IDLE;
    session->token = get_rnd() % 0xFF;
    memset(session->progress_bitmap, 0x00, REQUESTS_BITMAP_BYTE_COUNT);
    memset(session->success_bitmap, 0x00, REQUESTS_BITMAP_BYTE_COUNT);
    session->next_request_id = 0;
    session->request_buffer_tail_idx = 0;
    memset(session->requests_indices, 0x00, MODULE_D7AP_FIFO_MAX_REQUESTS_COUNT);
    memset(session->requests_lengths, 0x00, MODULE_D7AP_FIFO_MAX_REQUESTS_COUNT);
    memset(session->request_buffer, 0x00, MODULE_D7AP_FIFO_COMMAND_BUFFER_SIZE);
}
Exemplo n.º 5
0
/* Random shuffle */
void shuffle(unsigned char data[], int len)
{
    unsigned char save;
    int i, r;

    for (i = 0; i < len - 1; i++) {
	r = get_rnd(i, len) - i;
	save = data[i];
	data[i] = data[i + r];
	data[i + r] = save;

    }
}
static void tx_callback(hw_radio_packet_t* tx_packet)
{
	log_print_string("Sent packet with counter %u", NG(tx_buffer).counter);
	timer_post_task_delay(send_packet, TIMER_TICKS_PER_SEC + (get_rnd() %TIMER_TICKS_PER_SEC));
}
int PonscripterLabel::doEffect(Effect& effect, bool clear_dirty_region)
{
    int prevduration = effect.duration;
    if (ctrl_pressed_status || skip_to_wait) {
        effect.duration = effect_counter = 1;
    }

    effect_start_time = SDL_GetTicks();

    effect_timer_resolution = effect_start_time - effect_start_time_old;
    effect_start_time_old = effect_start_time;

    int effect_no = effect.effect;
    if (effect_cut_flag && skip_flag) effect_no = 1;

    int i;
    int width, width2;
    int height, height2;
    SDL_Rect src_rect = { 0, 0, screen_width, screen_height };
    SDL_Rect dst_rect = { 0, 0, screen_width, screen_height };

    /* ---------------------------------------- */
    /* Execute effect */
    //printf("Effect number %d %d\n", effect_no, effect->duration );

    switch (effect_no) {
    case 0: // Instant display
    case 1: // Instant display
        //drawEffect( &src_rect, &src_rect, effect_dst_surface );
        break;

    case 2: // Left shutter
        width = EFFECT_STRIPE_WIDTH * effect_counter / effect.duration;
        for (i = 0; i < screen_width / EFFECT_STRIPE_WIDTH; i++) {
            src_rect.x = i * EFFECT_STRIPE_WIDTH;
            src_rect.y = 0;
            src_rect.w = width;
            src_rect.h = screen_height;
            drawEffect(&src_rect, &src_rect, effect_dst_surface);
        }

        break;

    case 3: // Right shutter
        width = EFFECT_STRIPE_WIDTH * effect_counter / effect.duration;
        for (i = 1; i <= screen_width / EFFECT_STRIPE_WIDTH; i++) {
            src_rect.x = i * EFFECT_STRIPE_WIDTH - width - 1;
            src_rect.y = 0;
            src_rect.w = width;
            src_rect.h = screen_height;
            drawEffect(&src_rect, &src_rect, effect_dst_surface);
        }

        break;

    case 4: // Top shutter
        height = EFFECT_STRIPE_WIDTH * effect_counter / effect.duration;
        for (i = 0; i < screen_height / EFFECT_STRIPE_WIDTH; i++) {
            src_rect.x = 0;
            src_rect.y = i * EFFECT_STRIPE_WIDTH;
            src_rect.w = screen_width;
            src_rect.h = height;
            drawEffect(&src_rect, &src_rect, effect_dst_surface);
        }

        break;

    case 5: // Bottom shutter
        height = EFFECT_STRIPE_WIDTH * effect_counter / effect.duration;
        for (i = 1; i <= screen_height / EFFECT_STRIPE_WIDTH; i++) {
            src_rect.x = 0;
            src_rect.y = i * EFFECT_STRIPE_WIDTH - height - 1;
            src_rect.w = screen_width;
            src_rect.h = height;
            drawEffect(&src_rect, &src_rect, effect_dst_surface);
        }

        break;

    case 6: // Left curtain
        width = EFFECT_STRIPE_CURTAIN_WIDTH * effect_counter * 2 / effect.duration;
        for (i = 0; i <= screen_width / EFFECT_STRIPE_CURTAIN_WIDTH; i++) {
            width2 = width - EFFECT_STRIPE_CURTAIN_WIDTH * EFFECT_STRIPE_CURTAIN_WIDTH * i / screen_width;
            if (width2 >= 0) {
                src_rect.x = i * EFFECT_STRIPE_CURTAIN_WIDTH;
                src_rect.y = 0;
                src_rect.w = width2;
                src_rect.h = screen_height;
                drawEffect(&src_rect, &src_rect, effect_dst_surface);
            }
        }

        break;

    case 7: // Right curtain
        width = EFFECT_STRIPE_CURTAIN_WIDTH * effect_counter * 2 / effect.duration;
        for (i = 0; i <= screen_width / EFFECT_STRIPE_CURTAIN_WIDTH; i++) {
            width2 = width - EFFECT_STRIPE_CURTAIN_WIDTH * EFFECT_STRIPE_CURTAIN_WIDTH * i / screen_width;
            if (width2 >= 0) {
                if (width2 > EFFECT_STRIPE_CURTAIN_WIDTH) width2 = EFFECT_STRIPE_CURTAIN_WIDTH;

                src_rect.x = screen_width - i * EFFECT_STRIPE_CURTAIN_WIDTH - width2;
                src_rect.y = 0;
                src_rect.w = width2;
                src_rect.h = screen_height;
                drawEffect(&src_rect, &src_rect, effect_dst_surface);
            }
        }

        break;

    case 8: // Top curtain
        height = EFFECT_STRIPE_CURTAIN_WIDTH * effect_counter * 2 / effect.duration;
        for (i = 0; i <= screen_height / EFFECT_STRIPE_CURTAIN_WIDTH; i++) {
            height2 = height - EFFECT_STRIPE_CURTAIN_WIDTH * EFFECT_STRIPE_CURTAIN_WIDTH * i / screen_height;
            if (height2 >= 0) {
                src_rect.x = 0;
                src_rect.y = i * EFFECT_STRIPE_CURTAIN_WIDTH;
                src_rect.w = screen_width;
                src_rect.h = height2;
                drawEffect(&src_rect, &src_rect, effect_dst_surface);
            }
        }

        break;

    case 9: // Bottom curtain
        height = EFFECT_STRIPE_CURTAIN_WIDTH * effect_counter * 2 / effect.duration;
        for (i = 0; i <= screen_height / EFFECT_STRIPE_CURTAIN_WIDTH; i++) {
            height2 = height - EFFECT_STRIPE_CURTAIN_WIDTH * EFFECT_STRIPE_CURTAIN_WIDTH * i / screen_height;
            if (height2 >= 0) {
                src_rect.x = 0;
                src_rect.y = screen_height - i * EFFECT_STRIPE_CURTAIN_WIDTH - height2;
                src_rect.w = screen_width;
                src_rect.h = height2;
                drawEffect(&src_rect, &src_rect, effect_dst_surface);
            }
        }

        break;

    default:
        printf("effect No. %d is not implemented. Crossfade is substituted for that.\n", effect_no);

    case 10: // Cross fade
        height = 256 * effect_counter / effect.duration;
        alphaMaskBlend(NULL, ALPHA_BLEND_CONST, height, &dirty_rect.bounding_box);
        break;

    case 11: // Left scroll
        width = screen_width * effect_counter / effect.duration;
        src_rect.x = 0;
        dst_rect.x = width;
        src_rect.y = dst_rect.y = 0;
        src_rect.w = dst_rect.w = screen_width - width;
        src_rect.h = dst_rect.h = screen_height;
        drawEffect(&dst_rect, &src_rect, effect_src_surface);

        src_rect.x = screen_width - width - 1;
        dst_rect.x = 0;
        src_rect.y = dst_rect.y = 0;
        src_rect.w = dst_rect.w = width;
        src_rect.h = dst_rect.h = screen_height;
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case 12: // Right scroll
        width = screen_width * effect_counter / effect.duration;
        src_rect.x = width;
        dst_rect.x = 0;
        src_rect.y = dst_rect.y = 0;
        src_rect.w = dst_rect.w = screen_width - width;
        src_rect.h = dst_rect.h = screen_height;
        drawEffect(&dst_rect, &src_rect, effect_src_surface);

        src_rect.x = 0;
        dst_rect.x = screen_width - width - 1;
        src_rect.y = dst_rect.y = 0;
        src_rect.w = dst_rect.w = width;
        src_rect.h = dst_rect.h = screen_height;
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case 13: // Top scroll
        width = screen_height * effect_counter / effect.duration;
        src_rect.x = dst_rect.x = 0;
        src_rect.y = 0;
        dst_rect.y = width;
        src_rect.w = dst_rect.w = screen_width;
        src_rect.h = dst_rect.h = screen_height - width;
        drawEffect(&dst_rect, &src_rect, effect_src_surface);

        src_rect.x = dst_rect.x = 0;
        src_rect.y = screen_height - width - 1;
        dst_rect.y = 0;
        src_rect.w = dst_rect.w = screen_width;
        src_rect.h = dst_rect.h = width;
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case 14: // Bottom scroll
        width = screen_height * effect_counter / effect.duration;
        src_rect.x = dst_rect.x = 0;
        src_rect.y = width;
        dst_rect.y = 0;
        src_rect.w = dst_rect.w = screen_width;
        src_rect.h = dst_rect.h = screen_height - width;
        drawEffect(&dst_rect, &src_rect, effect_src_surface);

        src_rect.x = dst_rect.x = 0;
        src_rect.y = 0;
        dst_rect.y = screen_height - width - 1;
        src_rect.w = dst_rect.w = screen_width;
        src_rect.h = dst_rect.h = width;
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case 15: // Fade with mask
        alphaMaskBlend(effect.anim.image_surface, ALPHA_BLEND_FADE_MASK, 256 * effect_counter / effect.duration, &dirty_rect.bounding_box);
        break;

    case 16: // Mosaic out
        generateMosaic(effect_src_surface, 5 - 6 * effect_counter / effect.duration);
        break;

    case 17: // Mosaic in
        generateMosaic(effect_dst_surface, 6 * effect_counter / effect.duration);
        break;

    case 18: // Cross fade with mask
        alphaMaskBlend(effect.anim.image_surface, ALPHA_BLEND_CROSSFADE_MASK, 256 * effect_counter * 2 / effect.duration, &dirty_rect.bounding_box);
        break;

    case (CUSTOM_EFFECT_NO + 0): // quakey
        if (effect_timer_resolution > effect.duration / 4 / effect.no)
            effect_timer_resolution = effect.duration / 4 / effect.no;

        dst_rect.x = 0;
        dst_rect.y = (Sint16) (sin(M_PI * 2.0 * effect.no * effect_counter / effect.duration) *
                               EFFECT_QUAKE_AMP * effect.no * (effect.duration - effect_counter) / effect.duration);
        SDL_FillRect(accumulation_surface, NULL, SDL_MapRGBA(accumulation_surface->format, 0, 0, 0, 0xff));
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case (CUSTOM_EFFECT_NO + 1): // quakex
        if (effect_timer_resolution > effect.duration / 4 / effect.no)
            effect_timer_resolution = effect.duration / 4 / effect.no;

        dst_rect.x = (Sint16) (sin(M_PI * 2.0 * effect.no * effect_counter / effect.duration) *
                               EFFECT_QUAKE_AMP * effect.no * (effect.duration - effect_counter) / effect.duration);
        dst_rect.y = 0;
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;

    case (CUSTOM_EFFECT_NO + 2): // quake
        dst_rect.x = effect.no * get_rnd(-1, 1) * 2;
        dst_rect.y = effect.no * get_rnd(-1, 1) * 2;
        SDL_FillRect(accumulation_surface, NULL, SDL_MapRGBA(accumulation_surface->format, 0, 0, 0, 0xff));
        drawEffect(&dst_rect, &src_rect, effect_dst_surface);
        break;
    }

    //printf("effect conut %d / dur %d\n", effect_counter, effect.duration);

    effect_counter += effect_timer_resolution;
    if (effect_counter < effect.duration && effect_no != 1) {
        if (effect_no)
            flush(REFRESH_NONE_MODE, NULL, false);
        effect.duration = prevduration;
        return RET_WAIT | RET_REREAD;
    }
    else {
        SDL_BlitSurface(effect_dst_surface, &dirty_rect.bounding_box,
                        accumulation_surface, &dirty_rect.bounding_box);

        if (effect_no)
            flush(REFRESH_NONE_MODE, NULL, clear_dirty_region);
        if (effect_no == 1)
            effect_counter = 0;

        effect.duration = prevduration;
        event_mode = IDLE_EVENT_MODE;

        return RET_CONTINUE;
    }
}
Exemplo n.º 8
0
int main(int argc, char *argv[])
{
	char numbers[90];
    int card;
    int i;
    long seed = 1;
    int blnk;
    int oblnk;
    int rowtot, coltot;
    int num, row, col;
    int trow, tcol;
    int cnd;
    int broken = 0;
    int blnktot;




    while (seed++) {
	srand48(seed);
	if (seed % 5000 == 0)
	    fprintf(stderr, "Seed: %ld\n", seed);

	/* Clear the card */
	memset(cards, 0, sizeof(cards));

	/* Add blanks */
	broken = 0;
	blnk = 0;

	while (blnk < 72) {
	    oblnk = blnk;
	    for (col = 0; col < 9; col++) {
		blnktot = 8;
		if (col == 0)
		    blnktot = 9;
		if (col == 8)
		    blnktot = 7;
		memset(blanks, 0, sizeof(blanks));
		cnd = 0;
		coltot = 0;
		for (row = 0; row < 18; row++) {
		    /* Keep a count of blanks encountered */
		    if (cards[row][col])
			coltot = coltot + 1;
		    else {
			/* This is a potential candidate row */
			/* Check this rows population */
			rowtot = 0;
			for (i = 0; i < 9; i++)
			    if (cards[row][i])
				rowtot++;
			if (rowtot < 4) {
			    /* Candidate row found */
			    blanks[cnd++] = row;
			}
		    }
		}
		if (coltot < blnktot && cnd > 0) {	/* We found candiates for this column */
		    /* Take a random one */
		    row = blanks[get_rnd(0, cnd - 1)];
		    if (cards[row][col] == 100) {
			sprintf(stderr, "Yikes.Seed %ld,  Candidate already taken!\n", seed);
			broken = 1;
			break;
		    }
		    cards[row][col] = 100;
		    blnk++;
		}
	    }
	    if (oblnk == blnk) {
		printf("Help! Spinning on seed %ld\n", seed);
		print_card();
		broken = 1;
		break;
	    }
	}

	if (broken == 0) {
		for(i=0;i<90;i++)
			numbers[i]=i+1;
		shuffle(numbers, 89);
	    /* Add numbers */
	    for (num = 1; num < 91; num++) {
		    if (add_to_card(numbers[num]) == 0)
			{
				printf("Ouch! Could not add %d to card!\n", numbers[num]);
				break;
			}
		}

	    print_card();

	}
    }



    return (0);
}
Exemplo n.º 9
0
/// A run-time equivalent of the above.
inline size_t get_tag(size_t n) { return get_rnd(n); }
Exemplo n.º 10
0
static void execute_csma_ca()
{
    // TODO generate random channel queue
    //hw_radio_set_rx(NULL, NULL, NULL); // put radio in RX but disable callbacks to make sure we don't receive packets when in this state
                                        // TODO use correct rx cfg + it might be interesting to switch to idle first depending on calculated offset
    uint16_t tx_duration = calculate_tx_duration();
    timer_tick_t Tc = CONVERT_TO_TI(current_packet->d7atp_tc);
    switch (dll_state)
    {
        case DLL_STATE_CSMA_CA_STARTED:
        {
            dll_tca = Tc - tx_duration;
            dll_cca_started = timer_get_counter_value();
            DPRINT("Tca= %i = %i - %i", dll_tca, Tc, tx_duration);

#ifndef FRAMEWORK_TIMER_RESET_COUNTER
            // Adjust TCA value according the time already elapsed in the response period
            if (tc_starting_time) // TODO how do manage tc_starting_time? not set for now
            {
                dll_tca -= dll_cca_started - tc_starting_time;
                DPRINT("Adjusted Tca= %i = %i - %i", dll_tca, dll_cca_started, tc_starting_time);
            }
#endif

            if (dll_tca <= 0)
            {
                DPRINT("Tca negative, CCA failed");
                // Let the upper layer decide eventually to change the channel in order to get a chance a send this frame
                switch_state(DLL_STATE_IDLE);
                d7anp_signal_packet_csma_ca_insertion_completed(false);
                break;
            }

            uint16_t t_offset = 0;

            csma_ca_mode_t csma_ca_mode = current_access_profile->control_csma_ca_mode;
            // TODO overrule mode to UNC for subsequent requests by the requester, or a single response to a unicast request

            switch(csma_ca_mode)
            {
                case CSMA_CA_MODE_UNC:
                    // no delay
                    dll_slot_duration = 0;
                    break;
                case CSMA_CA_MODE_AIND: // TODO implement AIND
                {
                    dll_slot_duration = tx_duration;
                    // no initial delay
                    break;
                }
                case CSMA_CA_MODE_RAIND: // TODO implement RAIND
                {
                    dll_slot_duration = tx_duration;
                    uint16_t max_nr_slots = dll_tca / tx_duration;
                    uint16_t slots_wait = get_rnd() % max_nr_slots;
                    t_offset = slots_wait * tx_duration;
                    break;
                }
                case CSMA_CA_MODE_RIGD: // TODO implement RAIND
                {
                    dll_rigd_n = 0;
                    dll_tca0 = dll_tca;
                    dll_slot_duration = (uint16_t) ((double)dll_tca0) / (2 << (dll_rigd_n));
                    t_offset = get_rnd() % dll_slot_duration;
                    break;
                }
            }

            DPRINT("slot duration: %i t_offset: %i csma ca mode: %i", dll_slot_duration, t_offset, csma_ca_mode);

            dll_to = dll_tca - t_offset;

            if (t_offset > 0)
            {
                switch_state(DLL_STATE_CCA1);
                timer_post_task_delay(&execute_cca, t_offset);
            }
            else
            {
                switch_state(DLL_STATE_CCA1);
                sched_post_task(&execute_cca);
            }

            break;
        }
        case DLL_STATE_CSMA_CA_RETRY:
        {
        	int32_t cca_duration = timer_get_counter_value() - dll_cca_started;
        	dll_to -= cca_duration;


            DPRINT("RETRY dll_to = %i < %i ", dll_to, t_g);

            if (dll_to < t_g)
            {
                switch_state(DLL_STATE_CCA_FAIL);
                sched_post_task(&execute_csma_ca);
                break;
            }

            dll_tca = dll_to;
            dll_cca_started = timer_get_counter_value();
            uint16_t t_offset = 0;

            switch(current_access_profile->control_csma_ca_mode)
            {
                case CSMA_CA_MODE_AIND:
                case CSMA_CA_MODE_RAIND:
                {
                    uint16_t max_nr_slots = dll_tca / tx_duration;
                    uint16_t slots_wait = get_rnd() % max_nr_slots;
                    t_offset = slots_wait * tx_duration;
                    break;
                }
                case CSMA_CA_MODE_RIGD:
                {
                    dll_rigd_n++;
                    dll_slot_duration = (uint16_t) ((double)dll_tca0) / (2 << (dll_rigd_n+1));
                    if(dll_slot_duration != 0) // TODO can be 0, validate
                        t_offset = get_rnd() % dll_slot_duration;
                    else
                        t_offset = 0;

                    DPRINT("slot duration: %i", dll_slot_duration);
                    break;
                }
            }

            DPRINT("t_offset: %i", t_offset);

            dll_to = dll_tca - t_offset;

            if (t_offset > 0)
            {
                timer_post_task_delay(&execute_csma_ca, t_offset);
            }
            else
            {
                switch_state(DLL_STATE_CCA1);
                sched_post_task(&execute_cca);
            }

            break;
        }
        case DLL_STATE_CCA_FAIL:
        {
            // TODO hw_radio_set_idle();
            switch_state(DLL_STATE_IDLE);
            d7anp_signal_packet_csma_ca_insertion_completed(false);
            if (process_received_packets_after_tx)
            {
                sched_post_task(&process_received_packets);
                process_received_packets_after_tx = false;
            }

            if (resume_fg_scan)
            {
                switch_state(DLL_STATE_FOREGROUND_SCAN);

                hw_rx_cfg_t rx_cfg = (hw_rx_cfg_t){
                    .channel_id.channel_header = current_access_profile->subbands[0].channel_header,
                    .channel_id.center_freq_index = current_access_profile->subbands[0].channel_index_start,
                    .syncword_class = PHY_SYNCWORD_CLASS1,
                };

                hw_radio_set_rx(&rx_cfg, &packet_received, NULL);
                resume_fg_scan = false;
            }
            break;
        }
    }
}

void dll_execute_scan_automation()
{
    uint8_t scan_access_class = fs_read_dll_conf_active_access_class();
    if(active_access_class != scan_access_class)
    {
        fs_read_access_class(scan_access_class, &scan_access_profile);
        active_access_class = scan_access_class;
    }

    current_access_profile = &scan_access_profile;

    if(current_access_profile->control_scan_type_is_foreground && current_access_profile->control_number_of_subbands > 0) // TODO background scan
    {
        assert(current_access_profile->control_number_of_subbands == 1); // TODO multiple not supported
        switch_state(DLL_STATE_SCAN_AUTOMATION);
        hw_rx_cfg_t rx_cfg = {
            .channel_id = {
                .channel_header = current_access_profile->subbands[0].channel_header,
                .center_freq_index = current_access_profile->subbands[0].channel_index_start
            },
            .syncword_class = PHY_SYNCWORD_CLASS1
        };