Verilog Hex Display and Always block Confusion - hex

In this verilog code, I am trying to output a hex message and led lights depending on which state I am in. State 0 being the four letters 'ABCD'. State 1 being 'S_01', State 2 being 'S_02', State 3 being 'S_03', and State 4 being 'S_04'.
In my code below I believe I am inferring a latch for my LED_SW, but I am not sure. Also, while my code compiles I am unable to simulate without errors and warnings. Mostly errors and warnings referring to my statements at lines 319-322 stating that there is an 'illegal output or inout port connection for port 'HexSeg''. I would appreciate any tips or ideas for how to fix up my code.
For this question, I think the first ASCII27Seg module can be ignored and the FSMtestOne module can be ignored as well. Also, there is no need to look at my debounce module. I am specifically concerned with the code in my FSM module.
module ASCII27Seg(input [7:0] AsciiCode, output reg [6:0] HexSeg);
always # (*) begin
HexSeg = 8'd0;
$display("AsciiCode %b", AsciiCode);
case (AsciiCode)
// A
8'h41 : HexSeg[3] = 1;
// a
8'h61 : HexSeg[3] = 1;
// B
8'h42 : begin
HexSeg[0] = 1; HexSeg[1]=1;
end
// b
8'h62 : begin
HexSeg[0] = 1; HexSeg[1]=1;
end
// C
8'h43 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
// c
8'h63 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
// D
8'h44 : HexSeg[6] = 1;
// d
8'h64 : begin
HexSeg[0] = 1; HexSeg[5] = 1;
end
// E
8'h45 : begin
HexSeg[1] = 1; HexSeg[2] = 1;
end
// e
8'h65 : begin
HexSeg[1] = 1; HexSeg[2] = 1;
end
// F
8'h46 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1;
end
// f
8'h66 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1;
end
// G
8'h47 : HexSeg[4] = 1;
// g
8'h67 : HexSeg[4] = 1;
// H
8'h48 : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// h
8'h68 : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// I
8'h49 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[6] = 1;
end
// i
8'h69 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[6] = 1;
end
// J
8'h4a : begin
HexSeg[0] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// j
8'h6a : begin
HexSeg[0] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// K
8'h4b : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// k
8'h6b : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// L
8'h4c : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
// l
8'h6c : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
// M
8'h4d : begin
HexSeg[1] = 1; HexSeg[3] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// m
8'h6d : begin
HexSeg[1] = 1; HexSeg[3] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// N
8'h4e : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[3] = 1; HexSeg[5] = 1;
end
// n
8'h6e : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[3] = 1; HexSeg[5] = 1;
end
// O
8'h4f : HexSeg[6] = 1;
// o
8'h6f : HexSeg[6] = 1;
// P
8'h50 : begin
HexSeg[2] = 1; HexSeg[3] = 1;
end
// p
8'h70 : begin
HexSeg[2] = 1; HexSeg[3] = 1;
end
// Q
8'h51 : begin
HexSeg[3] = 1; HexSeg[4] = 1;
end
// q
8'h71 : begin
HexSeg[3] = 1; HexSeg[4] = 1;
end
// R
8'h52 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[5] = 1;
end
// r
8'h72 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[5] = 1;
end
// S
8'h53 : begin
HexSeg[1] = 1; HexSeg[4] = 1;
end
// s
8'h73 : begin
HexSeg[1] = 1; HexSeg[4] = 1;
end
// T
8'h54 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1;
end
// t
8'h74 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1;
end
// U
8'h55 : begin
HexSeg[0] = 1; HexSeg[6] = 1;
end
// u
8'h75 : begin
HexSeg[0] = 1; HexSeg[6] = 1;
end
// V
8'h56 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// v
8'h76 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// W
8'h57 : begin
HexSeg[0] = 1; HexSeg[2] = 1; HexSeg[4] = 1; HexSeg[6] = 1;
end
// w
8'h77 : begin
HexSeg[0] = 1; HexSeg[2] = 1; HexSeg[4] = 1; HexSeg[6] = 1;
end
// X
8'h58 : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// x
8'h78 : begin
HexSeg[0] = 1; HexSeg[3] = 1;
end
// Y
8'h59 : begin
HexSeg[0] = 1; HexSeg[4] = 1;
end
// y
8'h79 : begin
HexSeg[0] = 1; HexSeg[4] = 1;
end
// Z
8'h5A : begin
HexSeg[2] = 1; HexSeg[5] = 1;
end
// z
8'h7A : begin
HexSeg[2] = 1; HexSeg[5] = 1;
end
// _
8'h5F : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[4] = 1; HexSeg[5] = 1; HexSeg[6] = 1;
end
// number 0
8'h30 : begin
HexSeg[0] = 1;
end
// number 1
8'h31 : begin
HexSeg[0] = 1; HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[6] = 1;
end
// number 2
8'h32 : begin
HexSeg[1] = 1; HexSeg[4] = 1;
end
// number 3
8'h33 : begin
HexSeg[1] = 1; HexSeg[2] = 1;
end
// number 4
8'h34 : begin
HexSeg[2] = 1; HexSeg[3] = 1; HexSeg[6] = 1;
end
// turn all the bits off by default
default : HexSeg = 8'b11111111;
endcase
end
endmodule
// FSM
module FSM(input KEY0, SW0, SW1, SW2, SW3, SW4, output reg[6:0] HEX0, HEX1, HEX2, HEX3, output reg[2:0] state, output reg[1:0] Z, output reg[4:0] LED_SW);
reg[7:0] Message[3:0];
reg next_state;
always # (posedge KEY0) begin
case (state)
// reset S0
3'b000 : begin
if (SW0) begin
next_state <= 3'b000;
end
else if (SW1)
next_state <= 3'b001;
end
// S1
3'b001 : begin
if (SW2)
next_state <= 3'b010;
end
// S2
3'b010 : begin
if (SW3) begin
next_state <= 3'b011;
end
else if (SW1)
next_state <= 3'b010;
end
// S3
3'b011 : begin
if (SW4) begin
next_state <= 3'b100;
end
else if (SW1)
next_state <= 3'b010;
end
// S4
3'b100 : begin
if (SW1) begin
next_state <= 3'b010;
end
end
endcase
end
always # (state) begin
case (state)
3'b000 : begin
Z = 2'b00;
LED_SW = 5'b00010;
Message[3] = "A";
Message[2] = "B";
Message[1] = "C";
Message[0] = "D";
end
3'b001 : begin
Z = 2'b00;
LED_SW = 5'b00010;
Message[3] = "S";
Message[2] = "_";
Message[1] = "0";
Message[0] = "1";
end
3'b010 : begin
Z = 2'b00;
LED_SW = 5'b00100;
Message[3] = "S";
Message[2] = "_";
Message[1] = "0";
Message[0] = "2";
end
3'b011 : begin
Z = 2'b10;
LED_SW = 5'b01000;
Message[3] = "S";
Message[2] = "_";
Message[1] = "0";
Message[0] = "3";
end
3'b100 : begin
Z = 2'b11;
LED_SW = 5'b10000;
Message[3] = "S";
Message[2] = "_";
Message[1] = "0";
Message[0] = "4";
end
default : Z = 2'b00;
endcase
end
ASCII27Seg SevH3 (Message[3], HEX3);
ASCII27Seg SevH2 (Message[2], HEX2);
ASCII27Seg SevH1 (Message[1], HEX1);
ASCII27Seg SevH0 (Message[0], HEX0);
endmodule
// test one
`timescale 1ns/1ps
module FSMtestOne();
reg KEY0, SW0, SW1, SW2, SW3, SW4;
wire [6:0] HEX0, HEX1, HEX2, HEX3, HEX4;
wire [2:0] state;
wire [1:0] Z;
wire [4:0] LED_SW;
FSM testInstanceOne(KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
initial begin
KEY0 = 1'b0; SW0 = 1'b0; SW1 = 1'b0; SW2 = 1'b0; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b1; SW1 = 1'b0; SW2 = 1'b0; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b0; SW1 = 1'b1; SW2 = 1'b0; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b0; SW1 = 1'b0; SW2 = 1'b1; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b0; SW1 = 1'b0; SW2 = 1'b0; SW3 = 1'b1; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b0; SW1 = 1'b0; SW2 = 1'b1; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b1; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
KEY0 = 1'b0; SW0 = 1'b1; SW1 = 1'b0; SW2 = 1'b0; SW3 = 1'b0; SW4 = 1'b0; #5;
$display("KEY0 = %d, SW0 = %d, SW1 = %d, SW2 = %d, SW3 = %d, SW4 = %d, HEX0 = %c, HEX1 = %c, HEX2 = %c, HEX3 = %c, state = %d, Z = %d, LED_SW = %d", KEY0, SW0, SW1, SW2, SW3, SW4, HEX0, HEX1, HEX2, HEX3, state, Z, LED_SW);
end
endmodule
// debounce module
module debounce3 #(parameter cntSize = 8)(input reset, input Clk, input PB, output reg pulse);
reg[cntSize-1:0] cnt;
always # (posedge Clk)
if (reset)
cnt <= {cntSize{1'b0}};
else
begin
cnt <= {cnt[cntSize-2:0], PB};
if ( &cnt ) pulse <= 1'b1;
else if (~|cnt) pulse <= 1'b0;
end
endmodule

In lines 319-322 the output variables HEX0, HEX etc. are of type reg. They should be wires:
FSM(
input KEY0, SW0, SW1, SW2, SW3, SW4,
output [6:0] HEX0, HEX1, HEX2, HEX3, // << No reg.
Further for code that big it helps if you make it significant easier to read to spot errors. There are some coding tricks e.g.:
begin
HexSeg[0] = 1; HexSeg[1]=1;
end
can be written as:
{HexSeg[0],HexSeg[1]}=2'b11;
Then
8'h42 : begin
HexSeg[0] = 1; HexSeg[1]=1;
end
// b
8'h62 : begin
HexSeg[0] = 1; HexSeg[1]=1;
end
8'h43 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
// c
8'h63 : begin
HexSeg[1] = 1; HexSeg[2] = 1; HexSeg[6] = 1;
end
Can be written as:
8'h42, 8'h62 : {HexSeg[0],HexSeg[1]}=2'b11;
8'h43, 8'h63 : {HexSeg[1],HexSeg[2],HexSeg[6]} = 3'b111;
I personally would have preferred:
8'h41, 8'h61 : HexSeg = 8'b00001000; // A,a
8'h42, 8'h62 : HexSeg = 8'b00000011; // B,b
8'h43, 8'h63 : HexSeg = 8'b01000110; // C,c
8'h44, 8'h64 : HexSeg = 8'b00100001; // D,d
8'h45, 8'h65 : HexSeg = 8'b00000110; // E,e
etc.

Related

Libavcodec mpeg4 avi encoding framerate and timebase problem

I try to generate a video with a timebase more precise than the 1/fps (camera frame rate are not constant). But the generated AVI does not seem to take into account the framerate that I indicate.
#include <iostream>
extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
constexpr int TIMEFACTOR = 10;
static void encodeFrame(AVCodecContext *avctx, AVFormatContext *ctx, AVFrame* frame)
{
int ret = avcodec_send_frame(avctx, frame);
AVPacket packet;
av_init_packet(&packet);
ret = 0;
while (ret >= 0) {
ret = avcodec_receive_packet(avctx, &packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return; // nothing to write
};
//packet.pts = (frame) ? frame->pts : packet.pts;
av_packet_rescale_ts(&packet, avctx->time_base, ctx->streams[0]->time_base);
packet.duration = TIMEFACTOR;
av_interleaved_write_frame(ctx, &packet);
av_packet_unref(&packet);
}
}
static void fill_yuv_frame(AVFrame* frame, int width, int height, int frameId)
{
// Y
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + frameId * 3;
}
}
// Cb and Cr
for (int y = 0; y < height / 2; y++) {
for (int x = 0; x < width / 2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + frameId * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + frameId * 5;
}
}
}
int main(int argc, char** argv)
{
const char filename[] = "output.avi";
const char encoder[] = "mpeg4";
constexpr int WIDTH = 640;
constexpr int HEIGHT = 480;
av_log_set_level(AV_LOG_DEBUG);
//delete file because libavcodec reload file
remove(filename);
AVFormatContext* ofmtCtx;
int ret = avformat_alloc_output_context2(&ofmtCtx, NULL, "avi", filename);
AVCodec* avcodec = avcodec_find_encoder_by_name(encoder);
AVCodecContext* avctx = avcodec_alloc_context3(avcodec);
avctx->width = WIDTH ;
avctx->height = HEIGHT ;
avctx->sample_aspect_ratio = { 1, 1 };
avctx->pix_fmt = AV_PIX_FMT_YUV420P; //Do not work with other type
avctx->codec_id = AV_CODEC_ID_MPEG4;
avctx->bit_rate = 4 * 1000 * 1000;
avctx->time_base = av_make_q(1, 25 * TIMEFACTOR);
avctx->framerate = av_make_q(25, 1);
avctx->ticks_per_frame = TIMEFACTOR;
avctx->gop_size = 10;
avctx->max_b_frames = 1;
AVStream* m_outStream = avformat_new_stream(ofmtCtx, NULL);
ret = avcodec_open2(avctx, avcodec, NULL);
ret = avcodec_parameters_from_context(m_outStream->codecpar, avctx);
m_outStream->time_base = avctx->time_base;
m_outStream->r_frame_rate = avctx->framerate;
m_outStream->avg_frame_rate = avctx->framerate;
ret = avio_open(&(ofmtCtx->pb),
filename,
AVIO_FLAG_WRITE);
ret = avformat_write_header(ofmtCtx, NULL);
av_dump_format(ofmtCtx, 0, filename, 1);
AVFrame * avframe = av_frame_alloc();
avframe->format = avctx->pix_fmt;
avframe->width = avctx->width;
avframe->height = avctx->height;
ret = av_frame_get_buffer(avframe, 0);
ret = av_frame_make_writable(avframe);
for (int i = 0; i < 25; ++i) {
fflush(stdout);
fill_yuv_frame(avframe, avctx->width, avctx->height, i);
avframe->pts = i * TIMEFACTOR;
encodeFrame(avctx, ofmtCtx, avframe);
}
encodeFrame(avctx, ofmtCtx, NULL);
av_write_trailer(ofmtCtx);
return 0;
}
And this is dump output :
[mpeg4 # 000002AA64FA1880] intra_quant_bias = 0 inter_quant_bias = -64
[file # 000002AA64F69AC0] Setting default whitelist 'file,crypto'
[avi # 000002AA64F73400] reserve_index_space:0 master_index_max_size:256
[avi # 000002AA64F73400] duration_est:36000.000, filesize_est:18.4GiB, master_index_max_size:256
Output #0, avi, to 'output.avi':
Metadata:
ISFT : Lavf58.12.100
Stream #0:0, 0, 1/250: Video: mpeg4, 1 reference frame (FMP4 / 0x34504D46), yuv420p, 640x480 (0x0) [SAR 1:1 DAR 4:3], 0/1, q=2-31, 4000 kb/s, 25 fps, 25 tbr, 250 tbn
But if I open the video in ffmpeg command line fps is 250, and video content 250 frames, with many dropframe.

Decoding Sens'it 3 Payload data

The sens'it 3 discovery payload pdf is not very detailed, and we can't figure how to decode the payload. Link to the payload discovery
This is the data we receive from Sigfox's backend: "b6096d6f", and when we try to decode the data according to the sens'it payload guide, we get values that don't match with sens'it's own platform.
How we attempt to decode the string. Current mode is set to temperature and humidity:
byte0 = b6 -> (182*0,05)+2.7 = 11.8 (We don't know what this value tells us. We know it's the volt level, but what can we use this for. The relevant data would be battery level in %)
byte1 = 09 -> 9 (which doesn't make sense according to the payload discovery pdf, as the mode value should be between 0-5)
byte2 = 6d -> (109 - 200) / 8 = -11.375 (Wrong when comparing to sens'its platform)
byte3 = 6f -> (111 / 2) = 55.5 (Correct when comparing to sens'its platform)
What we want to know is how to decode the payload into readable data.
We appreciate any help we receive. Thanks in advance!
Here is an example parser for sensit v3, it covers all modes and you can certainly get inspiration on how to convert battery voltage to percentage.
var payload,
battery,
mode,
humidity,
temperature,
light,
door,
vibration,
magnet,
alert,
eventCount,
firmwareVersion,
parsedData = [],
obj = {};
// Byte #0
var byte = parseInt(payload.slice(0, 2), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
battery = ((parseInt(byte.slice(0, 5), 2) * 0.05) + 2.7);
if (battery >= 4.15) {
battery = 100;
} else if (battery >= 3.8 && battery < 4.15) {
battery = Math.round((battery - 3.275) * 114);
} else if (battery >= 3.6 && battery < 3.8) {
battery = Math.round((battery - 3.56) * 250);
} else if (battery > 3 && battery < 3.6) {
battery = Math.round((battery - 3) * 16);
}
battery = battery < 0 ? 0 : battery;
// Byte #1
var byte = parseInt(payload.slice(2, 4), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
mode = parseInt(byte.slice(0, 5), 2);
switch (mode) {
case 0:
mode = 'Standby';
break;
case 1:
mode = 'Temperature & Humidity';
break;
case 2:
mode = 'Light';
break;
case 3:
mode = 'Door';
break;
case 4:
mode = 'Vibration';
break;
case 5:
mode = 'Magnet';
break;
default:
mode = 'Unknown mode {' + mode + '}';
}
alert = Boolean(parseInt(byte.slice(5, 6), 2));
// Standby mode
if (mode === 'Standby') {
// Byte #2
var byte = parseInt(payload.slice(4, 6), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
firmwareVersion = byte;
// Byte #3
var byte = parseInt(payload.slice(6, 8), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
firmwareVersion += byte;
firmwareVersion = parseInt(firmwareVersion.slice(0, 4), 2) + '.' + parseInt(firmwareVersion.slice(4, 10), 2) + '.' + parseInt(firmwareVersion.slice(10, 16), 2);
}
// Temperature & Humidity
if (mode === 'Temperature & Humidity') {
// Byte #1
var byte = parseInt(payload.slice(2, 4), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
temperature = byte.slice(6, 8);
// Byte #2
var byte = parseInt(payload.slice(4, 6), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
temperature += byte;
temperature = ((parseInt(temperature, 2) - 200) / 8).toFixed(2);
// Byte #3
humidity = parseInt(payload.slice(6, 8), 16) * 0.5;
}
// Light
if (mode === 'Light') {
// Byte #2
var byte = parseInt(payload.slice(4, 6), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
light = byte;
// Byte #3
var byte = parseInt(payload.slice(6, 8), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
light += byte;
light = (parseInt(light, 2) / 96).toFixed(2);
}
// Door
if (mode === 'Door') {
// Byte #1
var byte = parseInt(payload.slice(2, 4), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
door = parseInt(byte.slice(6, 8), 2);
switch (door) {
case 0:
door = 'The calibration of the Door mode has not been done';
break;
case 1:
door = 'Unused value';
break;
case 2:
door = 'Door is closed';
break;
case 3:
door = 'Door is open';
break;
default:
door = 'Unknown door status {' + door + '}';
}
}
// Vibration
if (mode === 'Vibration') {
// Byte #1
var byte = parseInt(payload.slice(2, 4), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
vibration = parseInt(byte.slice(6, 8), 2);
switch (vibration) {
case 0:
vibration = 'No vibration detected';
break;
case 1:
vibration = 'A vibration is detected';
break;
case 2:
vibration = 'Unused value';
break;
case 3:
vibration = 'Unused value';
break;
default:
vibration = 'Unknown vibration status {' + vibration + '}';
}
}
// Magnet
if (mode === 'Magnet') {
// Byte #1
var byte = parseInt(payload.slice(2, 4), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
magnet = parseInt(byte.slice(6, 8), 2);
switch (magnet) {
case 0:
magnet = 'No magnet detected';
break;
case 1:
magnet = 'A magnet is detected';
break;
case 2:
magnet = 'Unused value';
break;
case 3:
magnet = 'Unused value';
break;
default:
magnet = 'Unknown magnet status {' + magnet + '}';
}
}
// Event count (Door - Vibration - Magnet)
if (mode === 'Door' || mode === 'Vibration' || mode === 'Magnet') {
// Byte #2
var byte = parseInt(payload.slice(4, 6), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
eventCount = byte;
// Byte #3
var byte = parseInt(payload.slice(6, 8), 16).toString(2);
while (byte.length < 8)
byte = '0' + byte;
eventCount += byte;
eventCount = parseInt(eventCount, 2);
}
// Store objects in parsedData array
obj = {};
obj.key = 'mode';
obj.value = mode;
obj.type = 'string';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'firmwareVersion';
obj.value = firmwareVersion;
obj.type = 'string';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'temperature';
obj.value = temperature;
obj.type = 'number';
obj.unit = '°C';
parsedData.push(obj);
obj = {};
obj.key = 'humidity';
obj.value = humidity;
obj.type = 'number';
obj.unit = '%';
parsedData.push(obj);
obj = {};
obj.key = 'light';
obj.value = light;
obj.type = 'number';
obj.unit = 'lux';
parsedData.push(obj);
obj = {};
obj.key = 'alert';
obj.value = alert;
obj.type = 'boolean';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'door';
obj.value = door;
obj.type = 'string';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'vibration';
obj.value = vibration;
obj.type = 'string';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'magnet';
obj.value = magnet;
obj.type = 'string';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'eventCount';
obj.value = eventCount;
obj.type = 'number';
obj.unit = '';
parsedData.push(obj);
obj = {};
obj.key = 'battery';
obj.value = battery;
obj.type = 'number';
obj.unit = '%';
parsedData.push(obj);
//console.log(parsedData);
return parsedData;

moment js bug : unexpected behavior when I select new timezone

Unexpected behavior I first select the timezone in drop-down when I press another timezone in select, the previous selected timezone are save.
function run(){
var timeZone = document.getElementById("selectTimezone");
var i = timeZone.selectedIndex;
var removeParenthesis = timeZone.options[i].text.replace(/[()]/g, '')
var splitString = removeParenthesis.split(' ')
var d = new Date('Nov 1, 2017 ' + ' ' + splitString[0])
var $clock = $('#clock'),
eventTime = moment(d.getTime()).unix(),
currentTime = moment(new Date().getTime()).unix(),
diffTime = eventTime - currentTime,
duration = moment.duration(diffTime * 1000, 'milliseconds'),
interval = 1000;
// if time to countdown
if(diffTime > 0) {
// Show clock
// $clock.show();
setInterval(function(){
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
var d = moment.duration(duration).days(),
h = moment.duration(duration).hours(),
m = moment.duration(duration).minutes(),
s = moment.duration(duration).seconds();
d = $.trim(d).length === 1 ? '0' + d : d;
h = $.trim(h).length === 1 ? '0' + h : h;
m = $.trim(m).length === 1 ? '0' + m : m;
s = $.trim(s).length === 1 ? '0' + s : s;
// show how many hours, minutes and seconds are left
$('.days').text(d + 'days');
$('.hours').text(h + 'hours');
$('.minutes').text(m + 'minutes');
$('.seconds').text(s + 'seconds');
}, interval);
}
}
https://codepen.io/edward1995/pen/oGpMOq
Clear the interval before starting a new one
var myInterval;// Used to save previous interval if one has been started
function run(){
var timeZone = document.getElementById("selectTimezone");
var i = timeZone.selectedIndex;
var removeParenthesis = timeZone.options[i].text.replace(/[()]/g, '')
var splitString = removeParenthesis.split(' ')
var d = new Date('Nov 1, 2017 ' + ' ' + splitString[0])
var $clock = $('#clock'),
eventTime = moment(d.getTime()).unix(),
currentTime = moment(new Date().getTime()).unix(),
diffTime = eventTime - currentTime,
duration = moment.duration(diffTime * 1000, 'milliseconds'),
interval = 1000;
// if time to countdown
if(diffTime > 0) {
// Show clock
// $clock.show();
clearInterval(myInterval);
myInterval = setInterval(function(){
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
var d = moment.duration(duration).days(),
h = moment.duration(duration).hours(),
m = moment.duration(duration).minutes(),
s = moment.duration(duration).seconds();
d = $.trim(d).length === 1 ? '0' + d : d;
h = $.trim(h).length === 1 ? '0' + h : h;
m = $.trim(m).length === 1 ? '0' + m : m;
s = $.trim(s).length === 1 ? '0' + s : s;
// show how many hours, minutes and seconds are left
$('.days').text(d + 'days');
$('.hours').text(h + 'hours');
$('.minutes').text(m + 'minutes');
$('.seconds').text(s + 'seconds');
}, interval);
}
}

clEnqueueNDRangeKernel throws CL_OUT_OF_RESOURCES

I have a kernel running very well on Intel HD graphics card. But, when I want to run the kernel on my GeForce 960 it gives the CL_OUT_OF_RESOURCES error.
I have tried for different local sizes and made sure to not go beyond the array indices, but still have no clue why this error is happening. Do you know why my code runs fine on Intel and doesn't work on NVIDIA?
One weird thing that is happening in my code is that I have a 13 itrations of similar operations. For performance purposes, I have repeated the same operations for 13 times and avoided writing a loop just to save some additional operations that loops have. The code works on NVIDIA when I reach to the 11th operation. But, when I include the 12th operation in the code it gives the above error and the 11th and 12th operations are similar! Any ideas why such thing is happening?
Here is the kernel:
float2 projectCube(float3 axis, float3 vertex){
float voxelSize = 0.5f;
float2 projection = (float2)(0.0f, 0.0f);
float temp;
//1
temp = axis.x;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//2
temp = axis.x + axis.y;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//3
temp = axis.y;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//4
temp = axis.z;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//5
temp = axis.x + axis.z;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//6
temp = axis.y + axis.z;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
//7
temp = axis.x + axis.y + axis.z;
if (projection.x > temp){ projection.x = temp; }
else if (projection.y < temp){ projection.y = temp; }
float product = dot(axis, vertex);
projection.x = voxelSize * projection.x + product;
projection.y = voxelSize * projection.y + product;
return projection;
}
float2 projectTriangle(float3 axis, float3 v0, float3 v1, float3 v2){
float2 projection;
projection.x = dot(axis, v0);
projection.y = projection.x;
float temp = dot(axis, v1);
if(projection.x > temp){
projection.x = temp;
}
else if(projection.y < temp){
projection.y = temp;
}
temp = dot(axis, v2);
if (projection.x > temp){
projection.x = temp;
}
else if (projection.y < temp){
projection.y = temp;
}
return projection;
}
float tester(float3 axis, float3 voxel, float3 v0, float3 v1, float3 v2){
float2 voxelProjection = projectCube(axis, voxel);
float2 faceProjection = projectTriangle(axis, v0, v1, v2);
float minProjection = fmin(voxelProjection.x, faceProjection.x);
float maxProjection = fmax(voxelProjection.y, faceProjection.y);
float testResult = maxProjection - minProjection - voxelProjection.y + voxelProjection.x
- faceProjection.y + faceProjection.x;
return testResult;
}
__kernel void voxelizer(size_t global_size,
float h_voxelSize,
__global float* h_minBoundsGrid,
__global int *h_dimGrid,
__global float* coords,
__global int* density)
{
//printf("local size is: %d\n", get_num_groups(0));
int i = get_global_id(0) * 9;
if (i <= global_size * 9){
float voxelSize = h_voxelSize;
float3 minBoundsGrid;
minBoundsGrid.x = h_minBoundsGrid[0];
minBoundsGrid.y = h_minBoundsGrid[1];
minBoundsGrid.z = h_minBoundsGrid[2];
int3 dimGrid;
dimGrid.x = h_dimGrid[0];
dimGrid.y = h_dimGrid[1];
dimGrid.z = h_dimGrid[2];
if ( i %9 == 0){
/*Triangle vertices*/
float3 v0;
v0 = (float3)(coords[i], coords[i + 1], coords[i + 2]);
float3 v1;
v1 = (float3)(coords[i + 3], coords[i + 4], coords[i + 5]);
float3 v2;
v2 = (float3)(coords[i + 6], coords[i + 7], coords[i + 8]);
//printf("i = %d. v0: %f, %f, %f\n", i, v0.x, v0.y, v0.z);
//printf("i = %d. v1: %f, %f, %f\n", i, v1.x, v1.y, v1.z);
//printf("i = %d. v2: %f, %f, %f\n", i, v2.x, v2.y, v2.z);
/*Normal vectors of the each voxel*/
float3 e0;
e0 = (float3)(0.5f, 0.0f, 0.0f);
float3 e1;
e1 = (float3)(0.0f, 0.5f, 0.0f);
float3 e2;
e2 = (float3)(0.0f, 0.0f, 0.5f);
/*Edges of a traingle*/
float3 f0;
f0 = v1 - v0;
float3 f1;
f1 = v2 - v1;
float3 f2;
f2 = v0 - v2;
float3 minLocalGrid;
minLocalGrid.x = fmin(v0.x, fmin(v1.x, v2.x));
minLocalGrid.y = fmin(v0.y, fmin(v1.y, v2.y));
minLocalGrid.z = fmin(v0.z, fmin(v1.z, v2.z));
minLocalGrid.x = voxelSize * floor(minLocalGrid.x / voxelSize);
minLocalGrid.y = voxelSize * floor(minLocalGrid.y / voxelSize);
minLocalGrid.z = voxelSize * floor(minLocalGrid.z / voxelSize);
//printf("i = %d. minLocalGrid = %f, %f, %f.\n", i, minLocalGrid.x, minLocalGrid.y, minLocalGrid.z);
float3 maxLocalGrid;
maxLocalGrid.x = fmax(v0.x, fmax(v1.x, v2.x));
maxLocalGrid.y = fmax(v0.y, fmax(v1.y, v2.y));
maxLocalGrid.z = fmax(v0.z, fmax(v1.z, v2.z));
maxLocalGrid.x = voxelSize * ceil(maxLocalGrid.x / voxelSize);
maxLocalGrid.y = voxelSize * ceil(maxLocalGrid.y / voxelSize);
maxLocalGrid.z = voxelSize * ceil(maxLocalGrid.z / voxelSize);
if (maxLocalGrid.x == minLocalGrid.x){ maxLocalGrid.x += voxelSize; }
if (maxLocalGrid.y == minLocalGrid.y){ maxLocalGrid.y += voxelSize; }
if (maxLocalGrid.z == minLocalGrid.z){ maxLocalGrid.z += voxelSize; }
//printf("i = %d. maxLocalGrid = %f, %f, %f.\n", i, maxLocalGrid.x, maxLocalGrid.y, maxLocalGrid.z);
//printf("i = %d\n v0 = %f, %f, %f\n v1 = %f, %f, %f\n v2 = %f, %f, %f\n minLocalGrid = %f, %f, %f\n===============\n",
// i, v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, maxLocalGrid.x, maxLocalGrid.y, maxLocalGrid.z);
float j = minLocalGrid.z;
while(j < maxLocalGrid.z){
float k = minLocalGrid.y;
while(k < maxLocalGrid.y){
float l = minLocalGrid.x;
while (l < maxLocalGrid.x){
float3 firstVertexOfVoxel = (float3)(l, k, j);
//printf("l,k,j: %f, %f, %f\n", l, k, j);
float3 globalCoordOffset = (firstVertexOfVoxel - minBoundsGrid) / voxelSize;
int3 globalDimOffset = convert_int3_rtz(globalCoordOffset);
//printf("i = %d. globalCoordOffset: %f, %f, %f\n", i, globalCoordOffset.x, globalCoordOffset.y, globalCoordOffset.z);
//printf("i = %d. globalDimOffset: %d, %d, %d\n", i, globalDimOffset.x, globalDimOffset.y, globalDimOffset.z);
int voxelIndexGlobalGrid = globalDimOffset.x + dimGrid.x * (globalDimOffset.y +
dimGrid.y * globalDimOffset.z);
//printf("i = %d. voxelIndexGlobalGrid = %d\n", i, voxelIndexGlobalGrid);
if (density[voxelIndexGlobalGrid] != 1){
/*The famous 13-axes test*/
float3 axis;
float testResult = 0;
int overlapCount = 0;
//1
testResult = tester(e0, firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//2
testResult = tester(e1, firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//3
testResult = tester(e2, firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//4
//axis = ;
testResult = tester(cross(-f2, f0), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//5
/*axis = cross(e0, f0);*/
testResult = tester(cross(e0, f0), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//6
//axis = cross(e0, f0);
testResult = tester(cross(e0, f1), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//7
//axis = cross(e0, f0);
testResult = tester(cross(e0, f2), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//8
//axis = cross(e1, f0);
testResult = tester(cross(e1, f0), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//9
//axis = cross(e1, f1);
testResult = tester(cross(e1, f1), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//10
//axis = cross(e1, f2);
testResult = tester(cross(e1, f2), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//11
//axis = cross(e2, f0);
testResult = tester(cross(e2, f0), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//12
//axis = cross(e2, f1);
testResult = tester(cross(e2, f1), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
//13
//axis = cross(e2, f2);
testResult = tester(cross(e2, f2), firstVertexOfVoxel, v0, v1, v2);
if (testResult <= 0){
overlapCount++;
}
if (overlapCount == 13){
density[voxelIndexGlobalGrid] = 1;
}
}
l = l + voxelSize;
}// while for l
k = k + voxelSize;
}// while for k
j = j + voxelSize;
}//while for j
//printf("Here are the max of the %d-th face: %f, %f, %f\n", i / 9, maxLocalGrid.x, maxLocalGrid.y, maxLocalGrid.z);
//printf("Here are the coordinates of the %d-th face: %f, %f, %f\n", i / 9, e1.x, e1.y, e1.z);
//printf("Here are the coordinates of the %d-th face: %f, %f, %f\n", i / 9, e2.x, e2.y, e2.z);
//printf("\n==================KERNEL COMPUTED==================\n");
//barrier(CLK_LOCAL_MEM_FENCE);
}
}
}
And this is the c-code:
#define DEVICE_SELECTOR 1 //0 for Intel and 1 for Nvidia in my computer
#define _CRT_SECURE_NO_WARNINGS
#define KERNEL_FILE "..\\voxelizerKernel.cl"
#define WORK_DIM 1
#define VOXEL_SIZE 0.5f
#define HALF_VOXEL_SIZE VOXEL_SIZE/2.0f;
//C header files
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
//OpenCL header files
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
cl_device_id create_device() {
cl_platform_id *platform;
cl_device_id dev;
cl_uint num_platform;
int err;
/* Identify a platform */
err = clGetPlatformIDs(0, NULL, &num_platform);
if (err < 0) {
printf("Error code: %d. Couldn't identify a platform\n", err);
exit(1);
}
platform = (cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platform);
clGetPlatformIDs(num_platform, platform, NULL);
/* Access a device */
err = clGetDeviceIDs(platform[DEVICE_SELECTOR], CL_DEVICE_TYPE_GPU, 1, &dev, NULL);
if (err < 0) {
printf("Error code: %d. Couldn't access any devices\n", err);
exit(1);
}
return dev;
}
cl_program build_program(cl_context ctx, cl_device_id dev, const char* filename) {
cl_program program;
FILE *program_handle;
char *program_buffer, *program_log;
size_t program_size, log_size;
int err;
/* Read program file and place content into buffer */
program_handle = fopen(filename, "r");
if (program_handle == NULL) {
printf("Couldn't find the program file\n");
exit(1);
}
fseek(program_handle, 0, SEEK_END);
program_size = ftell(program_handle);
rewind(program_handle);
program_buffer = (char*)malloc(program_size + 1);
program_buffer[program_size] = '\0';
fread(program_buffer, sizeof(char), program_size, program_handle);
fclose(program_handle);
/* Create program from file */
program = clCreateProgramWithSource(ctx, 1,
(const char**)&program_buffer, &program_size, &err);
if (err < 0) {
printf("Error code: %d. Couldn't create the program\n", err);
exit(1);
}
free(program_buffer);
/* Build program */
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err < 0) {
/* Find size of log and print to std output */
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG,
0, NULL, &log_size);
program_log = (char*)malloc(log_size + 1);
program_log[log_size] = '\0';
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG,
log_size + 1, program_log, NULL);
printf("%s\n", program_log);
free(program_log);
exit(1);
}
return program;
}
void print_device_info(cl_device_id dev){
cl_ulong glob_mem_size, local_mem_size;
cl_uint clock_freq, num_core, work_item_dim, time_res;
size_t local_size, work_item_size[3];
char dev_vendor[40], dev_name[400], driver_version[40], device_version[40];
clGetDeviceInfo(dev, CL_DEVICE_VENDOR, sizeof(dev_vendor), &dev_vendor, NULL);
clGetDeviceInfo(dev, CL_DEVICE_NAME, sizeof(dev_name), &dev_name, NULL);
clGetDeviceInfo(dev, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(glob_mem_size), &glob_mem_size, NULL);
clGetDeviceInfo(dev, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(local_mem_size), &local_mem_size, NULL);
clGetDeviceInfo(dev, CL_DRIVER_VERSION, sizeof(driver_version), &driver_version, NULL);
clGetDeviceInfo(dev, CL_DEVICE_VERSION, sizeof(device_version), &device_version, NULL);
clGetDeviceInfo(dev, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(clock_freq), &clock_freq, NULL);
clGetDeviceInfo(dev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(num_core), &num_core, NULL);
clGetDeviceInfo(dev, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(local_size), &local_size, NULL);
clGetDeviceInfo(dev, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(work_item_size), &work_item_size, NULL);
clGetDeviceInfo(dev, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(work_item_dim), &work_item_dim, NULL);
clGetDeviceInfo(dev, CL_DEVICE_PROFILING_TIMER_RESOLUTION, sizeof(time_res), &time_res, NULL);
printf("==========================================================\n");
printf("Device Sepc without consideration of kernels:\n");
printf("CL_DEVICE_VENDOR: %s\n", dev_vendor);
printf("CL_DEVICE_NAME: %s\n", dev_name);
printf("CL_DEVICE_GLOBAL_MEM_SIZE: %I64u GB\n", glob_mem_size / 1073741824);
printf("CL_DEVICE_LOCAL_MEM_SIZE: %I64u KB\n", local_mem_size / 1024);
printf("CL_DRIVER_VERSION: %s\n", driver_version);
printf("CL_DEVICE_VERSION: %s\n", device_version);
printf("CL_DEVICE_MAX_CLOCK_FREQUENCY: %I32u MHz\n", clock_freq);
printf("CL_DEVICE_MAX_COMPUTE_UNITS: %I32u\n", num_core);
printf("CL_DEVICE_MAX_WORK_GROUP_SIZE %u\n", local_size);
printf("CL_DEVICE_MAX_WORK_ITEM_SIZES: {%I32u, %I32u, %I32u}\n", work_item_size[0], work_item_size[1], work_item_size[2]);
printf("CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: %I32u\n", work_item_dim);
printf("CL_DEVICE_PROFILING_TIMER_RESOLUTION: %I32u ns\n", time_res);
printf("==========================================================\n");
}
int main()
{
/*OpenCL variables*/
cl_int i, j, err, num_groups;
size_t local_size, max_local_size, global_size, processed_global_size;
cl_context context;
cl_command_queue queue;
cl_program program;
cl_device_id device;
cl_kernel voxelization_kernel, reduction_kernel, reduction_complete_kernel;
cl_mem coords_buffer, density_buffer, dimGrid_buffer, h_minBoundsGrid_buffer, fullVxelsCount_buffer, group_sums_buffer;
void *density_mapped_memory;
cl_event prof_event;
cl_ulong time_start, time_end, total_time;
float h_voxelSize = VOXEL_SIZE;
float fullVxelsCount = 0;
/*Read mesh data*/
float coords[54] =
{ 0.300500,
1.300000,
0.000500,
1.200500,
1.600000,
0.000500,
1.600500,
0.600000,
0.000500,
0.300500,
1.300000,
0.000500,
0.500500,
1.900000,
0.000500,
1.200500,
1.600000,
0.000500,
0.300500,
1.300000,
0.000500,
1.600500,
0.600000,
0.000500,
0.100500,
0.700000,
0.000500,
0.100500,
0.700000,
0.000500,
1.600500,
0.600000,
0.000500,
0.000500,
0.200000,
0.000500,
0.000500,
0.200000,
0.000500,
1.600500,
0.600000,
0.000500,
1.600500,
0.100000,
0.000500,
1.200500,
1.600000,
0.000500,
1.600500,
1.300000,
0.000500,
1.600500,
0.600000,
0.000500 };
/*Get the voxel count*/
float boundsGrid[6] = {0,2,0,2,0,0.5};
int dimGrid[3] = {
(boundsGrid[1] - boundsGrid[0]) / VOXEL_SIZE,
(boundsGrid[3] - boundsGrid[2]) / VOXEL_SIZE,
(boundsGrid[5] - boundsGrid[4]) / VOXEL_SIZE
};
if (dimGrid[0] == 0) dimGrid[0] = 1;
if (dimGrid[1] == 0) dimGrid[1] = 1;
if (dimGrid[2] == 0) dimGrid[2] = 1;
float h_minBoundsGrid[3];
h_minBoundsGrid[0] = boundsGrid[0];
h_minBoundsGrid[1] = boundsGrid[2];
h_minBoundsGrid[2] = boundsGrid[4];
int voxelCounts = dimGrid[0] * dimGrid[1] * dimGrid[2];
/*Prepare kernel output : build an array for storing voxles' density info*/
int *density = (int*)malloc(sizeof(int)*voxelCounts);
for (int i = 0; i < voxelCounts; i++){
density[i] = 0;
}
/*OpenCL essentials*/
device = create_device();
clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(max_local_size), &max_local_size, NULL);
//print_device_info(device);
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
if (err < 0) {
printf("Error code: %d. Couldn't create a context\n", err);
exit(1);
}
program = build_program(context, device, KERNEL_FILE);
queue = clCreateCommandQueue(context, device,
CL_QUEUE_PROFILING_ENABLE, &err);
if (err < 0) {
printf("Error code: %d. Couldn't create a command queue\n", err);
exit(1);
};
voxelization_kernel = clCreateKernel(program, "voxelizer", &err);
if (err < 0) {
printf("Error code: %d. Couldn't create a kernel\n", err);
exit(1);
};
int numberOfFaces = 6;
global_size = numberOfFaces;
local_size = max_local_size;
if (global_size % local_size != 0){
processed_global_size = (global_size / local_size + 1) * local_size;
//int padding = processed_global_size - global_size;
//int *working_data = (int*)malloc((voxelCounts + padding)*sizeof(int));
//memcpy(working_data, density, voxelCounts);
//memset(working_data + voxelCounts, 0.0, padding);
}
else{
processed_global_size = global_size;
}
/* Create host-device data exchange interface*/
dimGrid_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY |
CL_MEM_COPY_HOST_PTR, sizeof(float)* 3, dimGrid, &err);
h_minBoundsGrid_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY |
CL_MEM_COPY_HOST_PTR, sizeof(float)* 3, h_minBoundsGrid, &err);
coords_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY |
CL_MEM_COPY_HOST_PTR, sizeof(float) * 54, coords, &err);
density_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY |
CL_MEM_COPY_HOST_PTR, sizeof(int) * voxelCounts, density, &err);
if (err < 0) {
printf("Error code: %d. Couldn't create a buffer\n", err);
exit(1);
};
err = clSetKernelArg(voxelization_kernel, 0, sizeof(global_size), &global_size);
err |= clSetKernelArg(voxelization_kernel, 1, sizeof(h_voxelSize), &h_voxelSize);
err |= clSetKernelArg(voxelization_kernel, 2, sizeof(cl_mem), &h_minBoundsGrid_buffer);
err |= clSetKernelArg(voxelization_kernel, 3, sizeof(cl_mem), &dimGrid_buffer);
err |= clSetKernelArg(voxelization_kernel, 4, sizeof(cl_mem), &coords_buffer);
err |= clSetKernelArg(voxelization_kernel, 5, sizeof(cl_mem), &density_buffer);
if (err < 0) {
printf("Error code: %d. Couldn't create an argument for voxelization_kernel\n", err);
exit(1);
}
/* Do the voxelization magic */
err = clEnqueueNDRangeKernel(queue, voxelization_kernel, 1, NULL, &processed_global_size,
&local_size, 0, NULL, &prof_event);
if (err < 0) {
printf("Error code: %d. Couldn't enqueue the voxelization_kernel\n", err);
exit(1);
}
/* Read the results */
density_mapped_memory = clEnqueueMapBuffer(queue, density_buffer, CL_TRUE,
CL_MAP_READ, 0, sizeof(density), 0, NULL, NULL, &err);
if (err < 0) {
printf("Error code : %d. Couldn't map the buffer to host memory\n", err);
exit(1);
}
memcpy(density, density_mapped_memory, sizeof(density)* voxelCounts);
err = clEnqueueUnmapMemObject(queue, density_buffer, density_mapped_memory,
0, NULL, NULL);
if (err < 0) {
printf("Error code: %d. Couldn't unmap the density_buffer\n", err);
exit(1);
}
for (int i = 0; i < voxelCounts; i++){
printf("%d\n", density[i]);
}
/*Clean up*/
clReleaseKernel(voxelization_kernel);
clReleaseMemObject(dimGrid_buffer);
clReleaseMemObject(h_minBoundsGrid_buffer);
clReleaseMemObject(coords_buffer);
clReleaseMemObject(density_buffer);
clReleaseCommandQueue(queue);
clReleaseProgram(program);
clReleaseContext(context);
return 0;
}

How can i make this program look at more than the first character?

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
const int setNum = 26;
vector<char> normalV(setNum);
vector<char> cipherV(setNum);
string toDec = "";
string beenDec = "";
int i = 0;
normalV.at(i) = 'a'; cipherV.at(i) = '!'; ++i;
normalV.at(i) = 'b'; cipherV.at(i) = '^'; ++i;
normalV.at(i) = 'c'; cipherV.at(i) = '&'; ++i;
normalV.at(i) = 'd'; cipherV.at(i) = '*'; ++i;
normalV.at(i) = 'e'; cipherV.at(i) = '#'; ++i;
normalV.at(i) = 'f'; cipherV.at(i) = '('; ++i;
normalV.at(i) = 'g'; cipherV.at(i) = ')'; ++i;
normalV.at(i) = 'h'; cipherV.at(i) = '-'; ++i;
normalV.at(i) = 'i'; cipherV.at(i) = '#'; ++i;
normalV.at(i) = 'j'; cipherV.at(i) = '_'; ++i;
normalV.at(i) = 'k'; cipherV.at(i) = '='; ++i;
normalV.at(i) = 'l'; cipherV.at(i) = '+'; ++i;
normalV.at(i) = 'm'; cipherV.at(i) = '['; ++i;
normalV.at(i) = 'n'; cipherV.at(i) = '{'; ++i;
normalV.at(i) = 'o'; cipherV.at(i) = '$'; ++i;
normalV.at(i) = 'p'; cipherV.at(i) = ']'; ++i;
normalV.at(i) = 'q'; cipherV.at(i) = '}'; ++i;
normalV.at(i) = 'r'; cipherV.at(i) = ';'; ++i;
normalV.at(i) = 's'; cipherV.at(i) = ':'; ++i;
normalV.at(i) = 't'; cipherV.at(i) = ','; ++i;
normalV.at(i) = 'u'; cipherV.at(i) = '%'; ++i;
normalV.at(i) = 'v'; cipherV.at(i) = '<'; ++i;
normalV.at(i) = 'w'; cipherV.at(i) = '.'; ++i;
normalV.at(i) = 'x'; cipherV.at(i) = '>'; ++i;
normalV.at(i) = 'y'; cipherV.at(i) = '/'; ++i;
normalV.at(i) = 'z'; cipherV.at(i) = '?'; ++i;
// User inputs message
do {
cout << "Enter a secret message: ";
getline(cin, toDec);
} while (toDec.length() == 0);
beenDec = toDec;
//decodes user's message
for (i = 0; i < setNum; ++i){
if (toDec.at(0) == cipherV.at(i)) {
beenDec.at(0) = normalV.at(i);
}
}
//diplays decoded message
cout << "Decrypted message: " << beenDec << endl;
//command drive to stay open
cin.get();
cin.get();
return 0;
}
Any help can be appreciated. The code is supposed to decode a string for example "!^&" should output to "abc". Right now it just decodes the first letter like this "a^&". I feel like this is because it is defined as a char and not a string however it gives me error if i change it up. Any idea on how i can modify this to work? Thanks in advance
I think the loop should be like this -:
//decodes user's message
for ( unsigned j = 0; j < toDec.length(); j++ )
{
for ( i = 0; i < setNum; ++i )
{
if ( toDec.at( j ) == cipherV.at( i ) )
{
beenDec.at( j ) = normalV.at( i );
}
}
}

Resources