Interfacing temperature sensor(TMP175) with STM32F407 using I2C Communication protocol - embedded

I am trying to set up my STM32 microcontroller with HAL
libraries to read from a TMP175 temperature sensor using i2c.
However, I am able to read the value from the single sensor but I wrote the code for 4 TMP175 temperature sensor.
I am getting only single current sensor value.
I referred the Datasheet of TMP175: http://www.ti.com/lit/ds/symlink/tmp75.pdf
I am using Keil IDE version 5 for programming.
Here is how I have done it so far using HAL libraries:
void StartDefaultTask(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x90 , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x90 , (uint8_t) thigh , (uint8_t) 1, THigh, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x90 , (uint8_t) tlow , (uint8_t) 1, TLow, 1, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x91, (uint8_t)CONFIG_REG , (uint8_t) 1, &ConfigReg[0], 1, 1000);
printf(" configreg==%x\t \r",ConfigReg[0]);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x91, (uint8_t) tlow, (uint8_t) 1,TLowReg, 2, 1000);
TLow1=(TLowReg[0]<<4);
TLow1|=(TLowReg[1]>>4);
TL=TLow1*0.0625;
printf(" Lower Limit Temperature=%f deg celsius \r ", TL);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x91, (uint8_t) thigh, (uint8_t) 1,THighReg, 2, 1000);
THigh1=(THighReg[0]<<4);
THigh1|=(THighReg[1]>>4);
TH=THigh1*0.0625;
printf(" upper Limit Temperature=%f deg celsius \r", TH);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x91, (uint8_t) TEMPERATURE, (uint8_t) 1, TempReg, 2, 1000);
Shift=TempReg[0]<<4;
Shift|=TempReg[1]>>4;
Temp=Shift*0.0625;
printf("Temperature=%f deg Celsius \r",Temp);
}
/* StartTask02 function */
void StartTask02(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9a , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x9a , (uint8_t) thigh , (uint8_t) 1, THigh, 2, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9a , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x01, (uint8_t)CONFIG_REG , (uint8_t) 1, &ConfigReg[0], 1, 1000);
printf(" configreg==%x\t \r",ConfigReg[0]);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x02, (uint8_t) tlow, (uint8_t) 1,TLowReg, 2, 1000);
TLow1=(TLowReg[0]<<4);
TLow1|=(TLowReg[1]>>4);
TL=TLow1*0.0625;
printf(" Lower Limit Temperature=%f deg celsius \r ", TL);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x03, (uint8_t) thigh, (uint8_t) 1,THighReg, 2, 1000);
THigh1=(THighReg[0]<<4);
THigh1|=(THighReg[1]>>4);
TH=THigh1*0.0625;
printf(" upper Limit Temperature=%f deg celsius \r", TH);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x00, (uint8_t) TEMPERATURE, (uint8_t) 1, TempReg, 2, 1000);
Shift=TempReg[0]<<4;
Shift|=TempReg[1]>>4;
Temp=Shift*0.0625;
printf("Temperature=%f deg Celsius \r",Temp);
}
/* StartTask03 function */
void StartTask03(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9c , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x9c , (uint8_t) thigh , (uint8_t) 1, THigh, 2, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9c , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x01, (uint8_t)CONFIG_REG , (uint8_t) 1, &ConfigReg[0], 1, 1000);
printf(" configreg==%x\t \r",ConfigReg[0]);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x02, (uint8_t) tlow, (uint8_t) 1,TLowReg, 2, 1000);
TLow1=(TLowReg[0]<<4);
TLow1|=(TLowReg[1]>>4);
TL=TLow1*0.0625;
printf(" Lower Limit Temperature=%f deg celsius \r ", TL);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x03, (uint8_t) thigh, (uint8_t) 1,THighReg, 2, 1000);
THigh1=(THighReg[0]<<4);
THigh1|=(THighReg[1]>>4);
TH=THigh1*0.0625;
printf(" upper Limit Temperature=%f deg celsius \r", TH);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x00, (uint8_t) TEMPERATURE, (uint8_t) 1, TempReg, 2, 1000);
Shift=TempReg[0]<<4;
Shift|=TempReg[1]>>4;
Temp=Shift*0.0625;
printf("Temperature=%f deg Celsius \r",Temp);
}

You are using the wrong slave/device address in the calls to HAL_I2C_Mem_Read(). For example, in StartTask02() you call HAL_I2C_Mem_Write() with device address 0x9a but then you call HAL_I2C_Mem_Read() with device address 0x01, 0x02, 0x03, and 0x00. Why did you do this differently than what you did in StartDefaultTask()?
I would suggest you use some #defines, like this:
#define TMP175_1_I2C_ADDRESS 0x90
#define TMP175_2_I2C_ADDRESS 0x9a
#define TMP175_3_I2C_ADDRESS 0x9c
HAL_I2C_Mem_Write(&hi2c1, TMP175_2_I2C_ADDRESS , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, TMP175_2_I2C_ADDRESS, (uint8_t)CONFIG_REG , (uint8_t) 1, &ConfigReg[0], 1, 1000);
I believe you can pass the same raw address to HAL_I2C_Mem_Write() and HAL_I2C_Mem_Read() because the HAL functions will set the least significant R/W bit appropriately.

Related

how to write test for breaking down (RTP depayload or parsed) JPEG frame into RTP (RFC 2435)

I am a bit new and still learning about socket and RTP stuff. Basically, i want to write a program that can send a jpeg (ultimately gonna be mjpeg) through RTP using UDP protocol.
I have the program for UDP and be able to send files or stream to a local address (127.0.0.1) using openCV. ok. So that is not a problem.
However, when i try to pass the frame that capture through VideoCapture from openCV, it wont work. Or rather i have no idea how to do it to be exact.
So now, i want to break this problem down and decide to test only on the RTP side.
So as you can see in my main function, i tried to test out the functions but it doesn't look like it works.
Can someone point me to the right direction of how to properly test those function, specially the sendFrame one ?
This code mainly just copy and paste from RFC2435 document.
Thanks
`/*
* Table K.1 from JPEG spec.
*/
static const int jpeg_luma_quantizer[64] = {
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99
};
`
/*
* Table K.2 from JPEG spec.
*/
static const int jpeg_chroma_quantizer[64] = {
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
int main(int argc, char * argv[]) {
//setup openCV
cvNamedWindow("UDP Video Sender", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateCameraCapture(0);
if(!capture){
std::cout<<"No camera found."<< std::endl;
goto DONE;
}
IplImage *frame;
frame = cvQueryFrame(capture);
IplImage *small = cvCreateImage(cvSize(frame->width / 2, frame->height / 2),
frame->depth, 3);
while(1){
//capture frame and resize
frame = cvQueryFrame(capture);
cvResize(frame, small, CV_INTER_LINEAR);
cvShowImage("UDP Video Sender", small);
//MakeHeaders(filename,0,5,5,0,128,0);
//MakeTables(128,frame,0);
//MakeDRIHeader();
// MakeHuffmanHeader();
MakeHuffmanHeader(128,1024,1024,uchar *lum_ac_symbols[],1024,1,1 );
//MakeQuantHeader();
DONE:
cout<<"Press any key to continue."<<endl;
}
}
/*
* Call MakeTables with the Q factor and two u_char[64] return arrays
*/
void
MakeTables(int q, u_char *lqt, u_char *cqt)
{
int i;
int factor = q;
if (q < 1) factor = 1;
if (q > 99) factor = 99;
if (q < 50)
q = 5000 / factor;
else
q = 200 - factor*2;
for (i=0; i < 64; i++) {
int lq = (jpeg_luma_quantizer[i] * q + 50) / 100;
int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100;
/* Limit the quantizers to 1 <= q <= 255 */
if (lq < 1) lq = 1;
else if (lq > 255) lq = 255;
lqt[i] = lq;
if (cq < 1) cq = 1;
else if (cq > 255) cq = 255;
cqt[i] = cq;
}
}
/**The following routines can be used to create the JPEG marker segments
corresponding to the table-specification data that is absent from the
RTP/JPEG body.
*/
u_char lum_dc_codelens[] = {
0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
};
u_char lum_dc_symbols[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
u_char lum_ac_codelens[] = {
0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d,
};
u_char lum_ac_symbols[] = {
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
};
u_char chm_dc_codelens[] = {
0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
};
u_char chm_dc_symbols[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
u_char chm_ac_codelens[] = {
0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
};
u_char chm_ac_symbols[] = {
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
};
u_char *
MakeQuantHeader(u_char *p, u_char *qt, int tableNo)
{
*p++ = 0xff;
*p++ = 0xdb; /* DQT */
*p++ = 0; /* length msb */
*p++ = 67; /* length lsb */
*p++ = tableNo;
memcpy(p, qt, 64);
return (p + 64);
}
u_char *
MakeHuffmanHeader(u_char *p, u_char *codelens, int ncodes,
u_char *symbols, int nsymbols, int tableNo,
int tableClass)
{
*p++ = 0xff;
*p++ = 0xc4; /* DHT */
*p++ = 0; /* length msb */
*p++ = 3 + ncodes + nsymbols; /* length lsb */
*p++ = (tableClass << 4) | tableNo;
memcpy(p, codelens, ncodes);
p += ncodes;
memcpy(p, symbols, nsymbols);
p += nsymbols;
return (p);
}
u_char *
MakeDRIHeader(u_char *p, u_short dri) {
*p++ = 0xff;
*p++ = 0xdd; /* DRI */
*p++ = 0x0; /* length msb */
*p++ = 4; /* length lsb */
*p++ = dri >> 8; /* dri msb */
*p++ = dri & 0xff; /* dri lsb */
return (p);
}
/*
* Arguments:
* type, width, height: as supplied in RTP/JPEG header
* lqt, cqt: quantization tables as either derived from
* the Q field using MakeTables() or as specified
* in section 4.2.
* dri: restart interval in MCUs, or 0 if no restarts.
*
* p: pointer to return area
*
* Return value:
* The length of the generated headers.
*
* Generate a frame and scan headers that can be prepended to the
* RTP/JPEG data payload to produce a JPEG compressed image in
* interchange format (except for possible trailing garbage and
* absence of an EOI marker to terminate the scan).
*/
int MakeHeaders(u_char *p, int type, int w, int h, u_char *lqt,
u_char *cqt, u_short dri)
{
u_char *start = p;
/* convert from blocks to pixels */
w <<= 3;
h <<= 3;
*p++ = 0xff;
*p++ = 0xd8; /* SOI */
p = MakeQuantHeader(p, lqt, 0);
p = MakeQuantHeader(p, cqt, 1);
if (dri != 0)
p = MakeDRIHeader(p, dri);
*p++ = 0xff;
*p++ = 0xc0; /* SOF */
*p++ = 0; /* length msb */
*p++ = 17; /* length lsb */
*p++ = 8; /* 8-bit precision */
*p++ = h >> 8; /* height msb */
*p++ = h; /* height lsb */
*p++ = w >> 8; /* width msb */
*p++ = w; /* wudth lsb */
*p++ = 3; /* number of components */
*p++ = 0; /* comp 0 */
if (type == 0)
*p++ = 0x21; /* hsamp = 2, vsamp = 1 */
else
*p++ = 0x22; /* hsamp = 2, vsamp = 2 */
*p++ = 0; /* quant table 0 */
*p++ = 1; /* comp 1 */
*p++ = 0x11; /* hsamp = 1, vsamp = 1 */
*p++ = 1; /* quant table 1 */
*p++ = 2; /* comp 2 */
*p++ = 0x11; /* hsamp = 1, vsamp = 1 */
*p++ = 1; /* quant table 1 */
p = MakeHuffmanHeader(p, lum_dc_codelens,
sizeof(lum_dc_codelens),
lum_dc_symbols,
sizeof(lum_dc_symbols), 0, 0);
p = MakeHuffmanHeader(p, lum_ac_codelens,
sizeof(lum_ac_codelens),
lum_ac_symbols,
sizeof(lum_ac_symbols), 0, 1);
p = MakeHuffmanHeader(p, chm_dc_codelens,
sizeof(chm_dc_codelens),
chm_dc_symbols,
sizeof(chm_dc_symbols), 1, 0);
p = MakeHuffmanHeader(p, chm_ac_codelens,
sizeof(chm_ac_codelens),
chm_ac_symbols,
sizeof(chm_ac_symbols), 1, 1);
*p++ = 0xff;
*p++ = 0xda; /* SOS */
*p++ = 0; /* length msb */
*p++ = 12; /* length lsb */
*p++ = 3; /* 3 components */
*p++ = 0; /* comp 0 */
*p++ = 0; /* huffman table 0 */
*p++ = 1; /* comp 1 */
*p++ = 0x11; /* huffman table 1 */
*p++ = 2; /* comp 2 */
*p++ = 0x11; /* huffman table 1 */
*p++ = 0; /* first DCT coeff */
*p++ = 63; /* last DCT coeff */
*p++ = 0; /* sucessive approx. */
return (p - start);
};
/*
* RTP data header from RFC1889
*/
typedef struct {
unsigned int version:2; /* protocol version */
unsigned int p:1; /* padding flag */
unsigned int x:1; /* header extension flag */
unsigned int cc:4; /* CSRC count */
unsigned int m:1; /* marker bit */
unsigned int pt:7; /* payload type */
u_int16_t seq; /* sequence number */
u_int32_t ts; /* timestamp */
u_int32_t ssrc; /* synchronization source */
u_int32_t csrc[1]; /* optional CSRC list */
} rtp_hdr_t;
#define RTP_HDR_SZ 12
/* The following definition is from RFC1890 */
#define RTP_PT_JPEG 26
struct jpeghdr {
unsigned int tspec:8; /* type-specific field */
unsigned int off:24; /* fragment byte offset */
u_int8_t type; /* id of jpeg decoder params */
u_int8_t q; /* quantization factor (or table id) */
u_int8_t width; /* frame width in 8 pixel blocks */
u_int8_t height; /* frame height in 8 pixel blocks */
};
struct jpeghdr_rst {
u_int16_t dri;
unsigned int f:1;
unsigned int l:1;
unsigned int count:14;
};
struct jpeghdr_qtable {
u_int8_t mbz;
u_int8_t precision;
u_int16_t length;
};
#define RTP_JPEG_RESTART 0x40
/* Procedure SendFrame:
*
* Arguments:
* start_seq: The sequence number for the first packet of the current
* frame.
* ts: RTP timestamp for the current frame
* ssrc: RTP SSRC value
* jpeg_data: Huffman encoded JPEG scan data
* len: Length of the JPEG scan data
* type: The value the RTP/JPEG type field should be set to
* typespec: The value the RTP/JPEG type-specific field should be set
* to
* width: The width in pixels of the JPEG image
* height: The height in pixels of the JPEG image
* dri: The number of MCUs between restart markers (or 0 if there
* are no restart markers in the data
* q: The Q factor of the data, to be specified using the Independent
* JPEG group's algorithm if 1 <= q <= 99, specified explicitly
* with lqt and cqt if q >= 128, or undefined otherwise.
* lqt: The quantization table for the luminance channel if q >= 128
* cqt: The quantization table for the chrominance channels if
* q >= 128
*
* Return value:
* the sequence number to be sent for the first packet of the next
* frame.
*
* The following are assumed to be defined:
*
* PACKET_SIZE - The size of the outgoing packet
* send_packet(u_int8_t *data, int len) - Sends the packet to the network
*/
#define PACKET_SIZE 512
u_int16_t SendFrame(u_int16_t start_seq, u_int32_t ts, u_int32_t ssrc,
u_int8_t *jpeg_data, int len, u_int8_t type,
u_int8_t typespec, int width, int height, int dri,
u_int8_t q, u_int8_t *lqt, u_int8_t *cqt) {
rtp_hdr_t rtphdr;
struct jpeghdr jpghdr;
struct jpeghdr_rst rsthdr;
struct jpeghdr_qtable qtblhdr;
u_int8_t packet_buf[PACKET_SIZE];
u_int8_t *ptr;
int bytes_left = len;
int seq = start_seq;
int pkt_len, data_len;
/* Initialize RTP header
*/
rtphdr.version = 2;
rtphdr.p = 0;
rtphdr.x = 0;
rtphdr.cc = 0;
rtphdr.m = 0;
rtphdr.pt = RTP_PT_JPEG;
rtphdr.seq = start_seq;
rtphdr.ts = ts;
rtphdr.ssrc = ssrc;
/* Initialize JPEG header
*/
jpghdr.tspec = typespec;
jpghdr.off = 0;
jpghdr.type = type | ((dri != 0) ? RTP_JPEG_RESTART : 0);
jpghdr.q = q;
jpghdr.width = width / 8;
jpghdr.height = height / 8;
/* Initialize DRI header
*/
if (dri != 0) {
rsthdr.dri = dri;
rsthdr.f = 1; /* This code does not align RIs */
rsthdr.l = 1;
rsthdr.count = 0x3fff;
}
/* Initialize quantization table header
*/
if (q >= 128) {
qtblhdr.mbz = 0;
qtblhdr.precision = 0; /* This code uses 8 bit tables only */
qtblhdr.length = 128; /* 2 64-byte tables */
}
while (bytes_left > 0) {
ptr = packet_buf + RTP_HDR_SZ;
jpghdr.off = htonl(jpghdr.off);
/*convert offset in hdr to network order, copy to packet*/
memcpy(ptr, &jpghdr, sizeof(jpghdr));
jpghdr.off = ntohl(jpghdr.off);
ptr += sizeof(jpghdr);
data_len = PACKET_SIZE - (ptr - packet_buf);
if (data_len >= bytes_left) {
data_len = bytes_left;
rtphdr.m = 1;
}
rtphdr.seq = htons(rtphdr.seq);
memcpy(packet_buf, &rtphdr, RTP_HDR_SZ);
memcpy(ptr, jpeg_data + jpghdr.off, data_len);
if(((ptr-packet_buf)+data_len) &&
send(sock[0], packet_buf, (ptr - packet_buf) + data_len, 0)<0)
perror("hre");
jpghdr.off += data_len;
bytes_left -= data_len;
rtphdr.seq = ntohs(rtphdr.seq);
rtphdr.seq++;
}
free(packet_buf);
return rtphdr.seq;
}

Packet Error Check Codes

I am programming an RS-485 protocol on a PIC micro controller and Linux computer. I was originally thinking about using CRC8 to check incoming data, however it looks like this would be a processor intensive task.
Instead I was thinking of a more simple PEC algorithm, perhaps XORing all the incoming bytes with a seed to create a very simple single step implementation of CRC.
What downside would having an algorithm such as this be?
A CRC is not processor-intensive. All it adds to your exclusive-or is a table lookup. The operation on each byte is simply: crc = crc8_table[crc ^ *data++]. See below.
The downside of doing just an exclusive-or is that there are many simple errors that cancel each other, resulting in a false-positive check. A CRC is much better.
#include <stddef.h>
/* 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D.
Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1):
http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
*/
static unsigned char crc8_table[] = {
0x00, 0x3e, 0x7c, 0x42, 0xf8, 0xc6, 0x84, 0xba, 0x95, 0xab, 0xe9, 0xd7,
0x6d, 0x53, 0x11, 0x2f, 0x4f, 0x71, 0x33, 0x0d, 0xb7, 0x89, 0xcb, 0xf5,
0xda, 0xe4, 0xa6, 0x98, 0x22, 0x1c, 0x5e, 0x60, 0x9e, 0xa0, 0xe2, 0xdc,
0x66, 0x58, 0x1a, 0x24, 0x0b, 0x35, 0x77, 0x49, 0xf3, 0xcd, 0x8f, 0xb1,
0xd1, 0xef, 0xad, 0x93, 0x29, 0x17, 0x55, 0x6b, 0x44, 0x7a, 0x38, 0x06,
0xbc, 0x82, 0xc0, 0xfe, 0x59, 0x67, 0x25, 0x1b, 0xa1, 0x9f, 0xdd, 0xe3,
0xcc, 0xf2, 0xb0, 0x8e, 0x34, 0x0a, 0x48, 0x76, 0x16, 0x28, 0x6a, 0x54,
0xee, 0xd0, 0x92, 0xac, 0x83, 0xbd, 0xff, 0xc1, 0x7b, 0x45, 0x07, 0x39,
0xc7, 0xf9, 0xbb, 0x85, 0x3f, 0x01, 0x43, 0x7d, 0x52, 0x6c, 0x2e, 0x10,
0xaa, 0x94, 0xd6, 0xe8, 0x88, 0xb6, 0xf4, 0xca, 0x70, 0x4e, 0x0c, 0x32,
0x1d, 0x23, 0x61, 0x5f, 0xe5, 0xdb, 0x99, 0xa7, 0xb2, 0x8c, 0xce, 0xf0,
0x4a, 0x74, 0x36, 0x08, 0x27, 0x19, 0x5b, 0x65, 0xdf, 0xe1, 0xa3, 0x9d,
0xfd, 0xc3, 0x81, 0xbf, 0x05, 0x3b, 0x79, 0x47, 0x68, 0x56, 0x14, 0x2a,
0x90, 0xae, 0xec, 0xd2, 0x2c, 0x12, 0x50, 0x6e, 0xd4, 0xea, 0xa8, 0x96,
0xb9, 0x87, 0xc5, 0xfb, 0x41, 0x7f, 0x3d, 0x03, 0x63, 0x5d, 0x1f, 0x21,
0x9b, 0xa5, 0xe7, 0xd9, 0xf6, 0xc8, 0x8a, 0xb4, 0x0e, 0x30, 0x72, 0x4c,
0xeb, 0xd5, 0x97, 0xa9, 0x13, 0x2d, 0x6f, 0x51, 0x7e, 0x40, 0x02, 0x3c,
0x86, 0xb8, 0xfa, 0xc4, 0xa4, 0x9a, 0xd8, 0xe6, 0x5c, 0x62, 0x20, 0x1e,
0x31, 0x0f, 0x4d, 0x73, 0xc9, 0xf7, 0xb5, 0x8b, 0x75, 0x4b, 0x09, 0x37,
0x8d, 0xb3, 0xf1, 0xcf, 0xe0, 0xde, 0x9c, 0xa2, 0x18, 0x26, 0x64, 0x5a,
0x3a, 0x04, 0x46, 0x78, 0xc2, 0xfc, 0xbe, 0x80, 0xaf, 0x91, 0xd3, 0xed,
0x57, 0x69, 0x2b, 0x15};
unsigned crc8(unsigned crc, unsigned char *data, size_t len)
{
unsigned char *end;
if (len == 0)
return crc;
crc ^= 0xff;
end = data + len;
do {
crc = crc8_table[crc ^ *data++];
} while (data < end);
return crc ^ 0xff;
}
/* this was used to generate the table and to test the table-version
#define POLY 0xB2
unsigned crc8_slow(unsigned crc, unsigned char *data, size_t len)
{
unsigned char *end;
if (len == 0)
return crc;
crc ^= 0xff;
end = data + len;
do {
crc ^= *data++;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
} while (data < end);
return crc ^ 0xff;
}
*/
#include <stdio.h>
#define SIZE 16384
int main(void)
{
unsigned char data[SIZE];
size_t got;
unsigned crc;
crc = 0;
do {
got = fread(data, 1, SIZE, stdin);
crc = crc8(crc, data, got);
} while (got == SIZE);
printf("%02x\n", crc);
return 0;
}
The table look up method for generating the PEC value is certainly faster. My tests on a a PIC32 running at 80 MHz for a 4 byte packet indicates that the table method required 2.8us while the algorithm method needed 11.5us. The memory requirements shows the cost of of speed: The table method requires 348 bytes while the algorithm method on;y 216 bytes. So if memory is scarce - consider the algorithm approach shown here. (BYTE is an unsigned char)
/* bit_crc8 FUNCTION DESCRIPTION ************************************
* SYNTAX: BYTE bit_crc8( BYTE *data, BYTE len);
* KEYWORDS: PEC, CRC, error checking
* DESCRIPTION: Returns the PEC for an array of bytes. This method does
* not use a lookup table
* PARAMETER 1: BYTE pointer to data array
* PARAMETER 2: BYTE - Number of bytes in array
* RETURN VALUE: BYTE - PEC value
* NOTES: SMBus limits the number of bytes in the packet to 256
* Primitive polynomial is set by the definition of PEC.
* END DESCRIPTION **********************************************************/
BYTE bit_crc8( BYTE *data, BYTE len)
{
#define PEC 0x07 // Implements Polynomial X^8 + X^2 + X^1 +1
BYTE crc = 0;
BYTE loop, b8;
while (len--)
{
crc ^= *data++;
for(loop=0; loop <8; loop++)
{
b8 = crc & 0x80; /* Test for MSB set to 1 */
crc <<= 1; /* Left shift CRC */
if(b8)
{
crc ^= PEC; /* Divide by PEC if bit 8 was set */
}
}
}
return crc;
}

OpenGL ES 2.0 program fail on Mali400 platform

I'm writing a simple texture stream rendering program using OpenGL ES 2.0. The program works on desktop but fail on embedded platform with Mali400 GPU. The LCD goes black with the top few lines blinking. I don't know what's wrong with my code. I tried some other OpenGL ES 2.0 programs which are OK, so the problem must lay in my code. Any help will be appreciated. Thanks.
main.c
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <sys/stat.h>
static EGLDisplay display;
static EGLSurface surface;
static void render_target_init(EGLNativeWindowType nativeWindow)
{
assert((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) != EGL_NO_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
assert(eglInitialize(display, &majorVersion, &minorVersion) == EGL_TRUE);
EGLConfig config;
EGLint numConfigs;
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
assert(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) == EGL_TRUE);
const EGLint attribList[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
EGL_NONE
};
assert((surface = eglCreateWindowSurface(display, config, nativeWindow, attribList)) != EGL_NO_SURFACE);
EGLContext context;
const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
assert((context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs)) != EGL_NO_CONTEXT);
assert(eglMakeCurrent(display, surface, surface, context) == EGL_TRUE);
}
static GLuint LoadShader(const char *name, GLenum type)
{
FILE *f;
int size;
char *buff;
GLuint shader;
GLint compiled;
const char *source[1];
assert((f = fopen(name, "r")) != NULL);
// get file size
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
assert((buff = malloc(size)) != NULL);
assert(fread(buff, 1, size, f) == size);
source[0] = buff;
fclose(f);
shader = glCreateShader(type);
glShaderSource(shader, 1, source, &size);
glCompileShader(shader);
free(buff);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
fprintf(stderr, "Error compiling shader %s:\n%s\n", name, infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
static void init_GLES(int width, int height)
{
GLint linked;
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
assert((vertexShader = LoadShader("vert.glsl", GL_VERTEX_SHADER)) != 0);
assert((fragmentShader = LoadShader("frag.glsl", GL_FRAGMENT_SHADER)) != 0);
assert((program = glCreateProgram()) != 0);
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetProgramInfoLog(program, infoLen, NULL, infoLog);
fprintf(stderr, "Error linking program:\n%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(program);
exit(1);
}
glClearColor(0.15f, 0.15f, 0.15f, 0.15f);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glUseProgram(program);
GLfloat vertex[] = {
1, 0, 0,
0, 1, 0,
-1, 0, 0,
0, -1, 0,
};
GLfloat texcoord[] = {
1, 1,
0, 1,
0, 0,
1, 0,
};
GLushort index[] = {
0, 1, 2,
0, 3, 2,
};
GLuint VBO[3];
glGenBuffers(3, VBO);
GLint pos = glGetAttribLocation(program, "positionIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 3, vertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(pos);
glVertexAttribPointer(pos, 3, GL_FLOAT, 0, 0, 0);
GLint tex = glGetAttribLocation(program, "texcoordIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 2, texcoord, GL_STATIC_DRAW);
glEnableVertexAttribArray(tex);
glVertexAttribPointer(tex, 2, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLushort), index, GL_STATIC_DRAW);
GLuint texid;
GLint texMap = glGetUniformLocation(program, "texMap");
glUniform1i(texMap, 0); // GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
assert(glGetError() == 0);
}
#ifdef _X_WINDOW_SYSTEM_
#include <X11/Xlib.h>
static EGLNativeWindowType CreateNativeWindow(void)
{
assert((display = XOpenDisplay(NULL)) != NULL);
int screen = DefaultScreen(display);
Window root = DefaultRootWindow(display);
Window window = XCreateWindow(display, root, 0, 0, 600, 480, 0,
DefaultDepth(display, screen), InputOutput,
DefaultVisual(display, screen),
0, NULL);
XMapWindow(display, window);
XFlush(display);
return window;
}
#endif
int display_init(int width, int height)
{
#ifdef _X_WINDOW_SYSTEM_
render_target_init(CreateNativeWindow());
#else
struct mali_native_window window;
window.width = width;
window.height = height;
render_target_init(&window);
#endif
init_GLES(width, height);
}
void render_frame(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
//glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(display, surface);
}
void update_texture(void *data, int width, int height)
{
static int first_time = 1;
if (first_time) {
printf("create texture %d %d\n", width, height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
first_time = 0;
}
else {
printf("update texture %d %d\n", width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
}
assert(glGetError() == 0);
}
int main(void)
{
int i;
void *texture[2] = {0};
int fd = open("0.rgb", O_RDONLY);
struct stat st;
fstat(fd, &st);
texture[0] = malloc(st.st_size);
read(fd, texture[0], st.st_size);
close(fd);
fd = open("1.rgb", O_RDONLY);
fstat(fd, &st);
texture[1] = malloc(st.st_size);
read(fd, texture[1], st.st_size);
close(fd);
display_init(600, 480);
for (i = 0; i < 200; i++) {
update_texture(texture[i%2], 720, 576);
render_frame();
usleep(20000);
}
return 0;
}
vert.glsl
attribute vec3 positionIn;
attribute vec2 texcoordIn;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(positionIn, 1);
texcoord = texcoordIn;
}
frag.glsl
precision mediump float;
uniform sampler2D texMap;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(texMap, texcoord).rgb;
gl_FragColor = vec4(color, 1);
}
Did you try to render the frame without the texture ?
Do like gl_FragColor = vec4(1.0,0.0,0.0,1.0); then check it is still black or red.
If red check your texture function.
I think it can be a problem not to call these functions each a frame
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,c_Texture[TEXTURES_FIRST].texture_id);
glUniform1i(h_Texture[TEXTURES_FIRST],1);

Wavefront OBJ import to OpenGL ES 2.0 problems

I finally finished building my Wavefront OBJ parser, but still have some issues rendering my test object (cube).
So this is how i have parsed my vertices and indices (faces) into arrays of data. I have ignored textures and normals for now.
Vertices:
v -0.307796 0.00433517 0
v 0.299126 0.00433517 0
v 0.299126 0.00433517 0.48337
v -0.307796 0.00433517 0.48337
v -0.307796 0.364153 0.48337
v 0.299126 0.364153 0.48337
v 0.299126 0.364153 0
v -0.307796 0.364153 0
As:
const Vertex Vertices[] = {
{-0.307796,0.00433517,0},
{0.299126,0.00433517,0},
{0.299126,0.00433517,0.48337},
{-0.307796,0.00433517,0.48337},
{-0.307796,0.364153,0.48337},
{0.299126,0.364153,0.48337},
{0.299126,0.364153,0},
{-0.307796,0.364153,0}
};
Faces:
f 7/1/1 3/2/2 2/3/3
f 3/4/4 7/5/5 6/6/6
f 5/7/7 1/8/8 4/9/9
f 1/10/10 5/11/11 8/12/12
f 7/13/13 1/14/14 8/15/15
f 1/16/16 7/17/17 2/18/18
f 3/19/19 5/20/20 4/21/21
f 5/22/22 3/23/23 6/24/24
f 5/25/25 7/26/26 8/27/27
f 7/28/28 5/29/29 6/30/30
f 3/31/31 1/32/32 2/33/33
f 1/34/34 3/35/35 4/36/36
As:
const GLubyte Indices[] = {
7,1,1, 3,2,2, 2,3,3,
3,4,4, 7,5,5, 6,6,6,
5,7,7, 1,8,8, 4,9,9,
1,10,10, 5,11,11, 8,12,12,
7,13,13, 1,14,14, 8,15,15,
1,16,16, 7,17,17, 2,18,18,
3,19,19, 5,20,20, 4,21,21,
5,22,22, 3,23,23, 6,24,24,
5,25,25, 7,26,26, 8,27,27,
7,28,28, 5,29,29, 6,30,30,
3,31,31, 1,32,32, 2,33,33,
1,34,34, 3,35,35, 4,36,36
};
Indices only as vertex positions:
const GLubyte Indices[] = {
7, 3, 2,
3, 7, 6,
5, 1, 4,
1, 5, 8,
7, 1, 8,
1, 7, 2,
3, 5, 4,
5, 3, 6,
5, 7, 8,
7, 5, 6,
3, 1, 2,
1, 3, 4
};
SetupVBO:
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
Renderingcode:
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -7)];
_currentRotation += displayLink.duration * 90;
[modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, 0)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// 3
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
And the result is pretty much nothing at all, so there must be something that im doing completely wrong.
Furthermore will the indices cause any problems since i have not initialized the normals or texture coords?
The Wavefront obj format says that if you have sequences of 3 in the face definitions then their meaning is:
vertex-index/vertex-texture-index/vector-normal-index
You are reading all of the indices into a single array GLubyte Indices[] and using it as it if was just the indices of the vertices.
If you want to do away with textures and normals, you need to take only the first number of every triplet.
In
f 6/1/1 3/2/2 2/3/3 7/4/4
The face is a quad of vertices of indices [6,3,2,7]. Using the indices array like you have requires that you tell OpenGl that the indices are multiplexed in triplets. It does not look like you do that. It also requires additional buffers for normal and texture coordinates.

Printing directly to Ethernet printer using 'raster mode': need basic guidance

I've stumbled across a problem way beyond my area of expertise, and I don't have a mentor to turn to for help with this.
I have a receipt printer I need to interface with through an iOS app. The printer is located on the same network as the device(s), so I can address it through supported "Line Mode commands"
What I'd like to do is keep the code I have already that works cross-platform – i.e. it's a UIView/NSView, and if you're not familiar with OS X/iOS, it's just a standard vanilla view that I can render into PDF/PNG formats. Thankfully, the printer has a "raster graphics" mode that seems to be what I need.
Unfortunately, be it the broken English of the command spec, or my complete lack of knowledge of anything beyond basic C, or my complete lack of knowledge regarding graphics, I have no idea how to even get started from the command specifications I have. I know the printer and my networking works because I can address it over the network and send it basic feed commands. But, I have no idea how to go from a PNG -> whatever the printer needs to make it's 'raster mode' work.
The specification is available at http://www.star-m.jp/eng/service/usermanual/linemode_cm_en.pdf , and the page you'd want to start reading it if you want to help is 3-68, and the specific commands I'm having trouble even getting started with are on 3-78/3-79.
I can give you nothing but a checkmark but I assure you, you'll have my undying gratitude if you can even provide me even a point in the right direction.
Having written a few printer drivers I can confirm that generally the documentation is confusing because of the way printers work. The document that you refer to doesn't actually seem to bad to me.
I think you're right to be printing in raster mode and that overall this is going to give the best results.
From the Star documentation I reckon you'll need to send :
1. \x1b*rR Initialize raster mode
2. \x1b*rA Enter raster mode
3. \x1b*rC Clear raster data
4. \x1b*rml
4. b\x##\x##\xAA\xAA\xAA....<DATA>..........
5. \x1b\x0C\x00 Raster Form feed(??) - should spit out the data.
6. \x1b*rB Clear raster data
Obv. in the above \x1b is the C encoding of ESC (i.e. character 27 0x1b).
From all of the documentation that I've been reading the following is how the images should be formatted in raster mode. When in line mode it is completely different as the vertical & horizontal are swapped. From THERMAL PRINTER PROGRAMMER'S MANUAL (TSP552,TSP552II,TSP2000)
This equates to the following bytes stream.
On the 4th command line it is effectively 'b' followed by two bytes definining the size. This size is computed as the number of pixels contained in the stream % 256 and / 256. So for 320x1 that'd 0x40,0x01
So, taking the above and plugging it into a simple test program you should test with this:
char rasterImage [] = {
0x1b, '*', 'r', 'R', // Initialize raster mode
0x1b, '*', 'r', 'A', // Enter raster mode
0x1b, '*', 'r', 'C', // Clear raster data
// n1 n2 d1 d2..
0x1b, 'b', 0x2, 0, 0x00, 0x00, // data
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x3F, 0xFC,
0x1b, 'b', 0x2, 0, 0x77, 0xEE,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0x0F, 0xF0,
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x3E, 0x7C,
0x1b, 'b', 0x2, 0, 0x38, 0x1C,
0x1b, 'b', 0x2, 0, 0x79, 0x9E,
0x1b, 'b', 0x2, 0, 0x73, 0xCE,
0x1b, 'b', 0x2, 0, 0x73, 0xCE,
0x1b, 'b', 0x2, 0, 0xF9, 0x9F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xFE, 0x7F,
0x1b, 'b', 0x2, 0, 0xFF, 0xFF,
0x1b, 'b', 0x2, 0, 0xFF, 0xFF,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00};
[self.currentDataBeingSent appendBytes:rasterImage length:sizeof(rasterImage)];
Simply squirt that out to the printer and you should get a picture as above. This is where you can easily tweak and play about with the exact commands to get something that's working. Often this is the only way I've ever managed to figure out what should be done.
rev.3
Ref. comments.
If you have a byte per pixel then you will need to merge these into a series of bits; the following should do the job based on your pastebin code. I've also changed the char* to be unsigned as it is signed can cause problems when bit manipulating.
NSUInteger bitmapBytePerRow = width/8;
NSUInteger bytesPerRow = 3 + bitmapBytePerRow;
[self.currentDataBeingSent = [NSMutableData dataWithLength:bytesPerRow * height];
[self.currentDataBeingSent appendBytes:initializeRaster length:sizeof(initializeRaster)];
[self.currentDataBeingSent appendBytes:enterRaster length:sizeof(enterRaster)];
NSUInteger byteOffset = 0;
for (NSUInteger y = 0; y < height; y++)
{
unsigned char *rasterCommandForRow = (unsigned char *)calloc(bytesPerRow, sizeof(char));
unsigned char *current_raster = rasterCommandForRow;
*current_raster++ = '\x6B';
*current_raster++ = (width*height) % 256;
*current_raster++ = (width*height) / 256;
unsigned char mask = '\x80' ;
unsigned char out = 0 ;
for (NSUInteger x = 0; x < width; x++)
{
if (*(data + (byteOffset * sizeof(char))))
out |= mask ;
byteOffset++;
mask >>= 1 ;
if( 0 == mask )
{
mask = '\x80' ;
*current_raster++ = out ;
if( out )
lastDot = nextOut ;
out = 0 ;
}
}
// handle partially finished byte .
if( ( '\x80' != mask ) && ( 0 != out ) )
*current_raster++ = out ;
[self.currentDataBeingSent appendBytes:rasterCommandForRow length:bytesPerRow];
}
rev.3a
Looking at the Mac CUPS support from Star it's got the source code for the driver which contains a lot of clues about how this should be done. Sometimes code is so much easier to read than documentation.
starcupsdrv-3.1.1_mac_20100423.zip\starcupsdrv-3.1.1_mac\SourceCode\
contains starcupsdrv-src-3.1.1.tar.gz\ sub folder starcupsdrv\src\
View rastertostar.c, the important bit is the calculation of the n1 / n2 values. These aren't at all X & Y but based on the pixel count, lastBlackPixel is the count of pixels from the source.
putchar('b');
putchar((char) ((lastBlackPixel > 0)?(lastBlackPixel % 256):1));
putchar((char) (lastBlackPixel / 256));
I've modified the code above to include the fixes, hopefully that'll be closer. If not post a scan of what comes out of the printer, it will be useful to diagnose what's happening.
For reference The code between 580:650 from jsStarUSB.cpp seems to me to be along the lines of what you need to produce a buffer (stored in nextOut) that contains the raster data in the format to be sent directly to the printer.
rev. 4 (2023)
Joshua May advises in the comments that rasterImage doesn't need the ESC (0x1b) on each row - only the 'b' and that by just removing the 0x1b "worked for me".
I have a hunch this might be the same as the old Seiko printers, only yours is network enabled. If so, have a look at the C code here. It tries to output to a serial port /dev/cua, where it thinks the printer is.
But if the commands are the same, the code should help you. It takes as input the Portable Bitmap Format, which is plain ASCII text.
But I don't know. Microsoft indicates Star Micronics works the same as Epson LQ, in which case there is ample documentation.
Related links:
ESC/POS PDF Documentation, hundreds of pages
Command codes from the STAR website
Update! ;-) Try this, totally untested code:
/* Call with grayscale images of height 256, width 256. */
- (void) outputraster(char* pixels, int rows)
{
const char initializeRaster[] = "\x1B\x2A\x72\x52";
const char enterRaster[] = "\x1B\x2A\x72\x41";
const char formFeed[] = "\x1B\x0C\x00";
const char clearRaster[] = "\x1B\x2A\x72\x43";
const char exitRaster[] = "\x1B\x2A\x72\x42";
/* The FF means 255 lines: */
char setRasterPageLength[] "\x1B\x2A\x72\x50\xFF\x0";
/* The FF FF means 256 lines and 256 rows: */
char sendRasterData[] = "\x62\xFF\xFF";
[self sendBytes:initializeRaster ofLength:sizeof(initializeRaster)];
[self sendBytes:enterRaster ofLength:sizeof(enterRaster)];
[self sendBytes:clearRaster ofLength:sizeof(clearRaster)];
[self sendBytes:setRasterPageLength ofLength:sizeof(setRasterPageLength)];
[self sendBytes:sendRasterData ofLength:sizeof(sendRasterData)];
while (rows)
{
for (int x = 0; x < 255; x++)
{
[self sendBytes:pixels[x] ofLength:256];
}
rows --;
}
}
Update!
I was looking at the docs at night, and stumbled upon how you can print out a prestored logo. Then I looked at how to define that logo, and that part of the documentation looked a lot more thorough:
Explanations of bitmap format for a similar printer:
Also, look at pages 34 and on for an explanation of the bitmap format of a Star printer.
I hope this helps someone, but I was trying to use the code from Richard Harrison above to print the phone raster in Python...
I on my system I can run python3 raster.py > /dev/usb/lp0 and I get the expected output!! Hopefully it shows what you need. I cross refrenced the bytes of a file that would print, and also the graphical mode manual.
import sys
buf = [
0x1b, ord('*'), ord('r'), ord('A'), # enter raster mode
0x1b, ord('*'), ord('r'), ord('P'), ord('0'), 0x00, # continuous mode
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x3F, 0xFC,
ord('b'), 0x2, 0, 0x77, 0xEE,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0x0F, 0xF0,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x3E, 0x7C,
ord('b'), 0x2, 0, 0x38, 0x1C,
ord('b'), 0x2, 0, 0x79, 0x9E,
ord('b'), 0x2, 0, 0x73, 0xCE,
ord('b'), 0x2, 0, 0x73, 0xCE,
ord('b'), 0x2, 0, 0xF9, 0x9F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xFE, 0x7F,
ord('b'), 0x2, 0, 0xFF, 0xFF,
ord('b'), 0x2, 0, 0xFF, 0xFF,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
0x1b, ord('*'), ord('r'), ord('b') # end raster mode
]
blob = bytearray(buf)
sys.stdout.buffer.write(blob)
EDIT: Off the back of this, I created a Python library that will take an image and convert it into the required raster commands... https://pypi.org/project/StarTSPImage/