Jump to content

Splitt Float Value into single Digits - 7 Segment BPM Display - TPD


Phatline
 Share

Recommended Posts

Hi,

i am currently finishing the TPD-Displays PCBs, and i write a Test App/or my first skeleton-UI to use it.

Its all clear so far, i can print single digits, i know the pattern, the pinout.....

i want to print for example:

float BPM  = 128.5;             // for Example for Digit 1 2 8 5 --- As BPM Display

but i stuck by Divididing  the BPM Value into single Integers, by searching in internet i found:

	int i = BPM*10; // Elemenate FLOAT Digit!

	static char buf[4];
	sprintf(buf, "%05.2f", i); // split it into CHARs
	

but that dont work at all... (print "0" on the first digits, the others are OFF)

i later print the "buf[x]" value to my Digit --- that works fine, when initalize  "TPD_SEG" by hand with iniatl values, but not when filled with above code!

 

	// Transcode To Shiftregister-Friendly Array
	static u8 x=0;
	for (x=0; x<4; x++) {
	//	7 Segment PINOUT
	//		[6]	
	//	[1]		[5]
	//		[0]
	//	[2]		[4]
	//		[3]		[7]
	//	
							switch (buf[x]) {
												case 0:	TPD_SEG[x][0] = 0;
														TPD_SEG[x][1] = 1;
														TPD_SEG[x][2] = 1;
														TPD_SEG[x][3] = 1;
														TPD_SEG[x][4] = 1;
														TPD_SEG[x][5] = 1;
														TPD_SEG[x][6] = 1;
														TPD_SEG[x][7] = 0;	break;
												case 1:	TPD_SEG[x][0] = 0;
														TPD_SEG[x][1] = 0;
														TPD_SEG[x][2] = 0;
														TPD_SEG[x][3] = 0;	
														TPD_SEG[x][4] = 1;
														TPD_SEG[x][5] = 1;
														TPD_SEG[x][6] = 0;
														TPD_SEG[x][7] = 0;	break;	
												case 2:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 0;	
														TPD_SEG[x][2] = 1;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 0;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 3:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 0;	
														TPD_SEG[x][2] = 0;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 4:	TPD_SEG[x][0] = 0;	
														TPD_SEG[x][1] = 1;	
														TPD_SEG[x][2] = 0;	
														TPD_SEG[x][3] = 0;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 0;	
														TPD_SEG[x][7] = 0;	break;	
												case 5:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 1;	
														TPD_SEG[x][2] = 0;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 0;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 6:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 1;	
														TPD_SEG[x][2] = 1;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 0;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 7:	TPD_SEG[x][0] = 0;	
														TPD_SEG[x][1] = 0;	
														TPD_SEG[x][2] = 0;	
														TPD_SEG[x][3] = 0;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 8:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 1;	
														TPD_SEG[x][2] = 1;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
												case 9:	TPD_SEG[x][0] = 1;	
														TPD_SEG[x][1] = 1;	
														TPD_SEG[x][2] = 0;	
														TPD_SEG[x][3] = 1;	
														TPD_SEG[x][4] = 1;	
														TPD_SEG[x][5] = 1;	
														TPD_SEG[x][6] = 1;	
														TPD_SEG[x][7] = 0;	break;	
							}			
		
	}

 

 

 

Link to comment
Share on other sites

Hey!

If you are still interested, the "correct" solution with sprintf would be:

	int i = BPM*10; 

	static char buf[5];
	sprintf(buf, "%04d", i);

 

But it won't give the same result as your solution:  buf would contains the C string "1285", using the ASCII code, ie buf[0] = 49, buf[1] = 50, etc.

And buf needs to be (at least) 5 byte long because sprintf will make a null terminated string, with a 0 (zero) to mark the end. The formating ("%04d") make sprintf pad with 0 if the number is less than 4 digit long (but if i is > 999 then buf won't be large enough for more character).

 

I hope it can helps :)

Link to comment
Share on other sites

You can use the routine of the Seq:
No need of a string variable.

/////////////////////////////////////////////////////////////////////////////
// Returns the 8bit pattern of a hexadecimal 4bit value
// The dot will be set if bit 7 of this value is set
/////////////////////////////////////////////////////////////////////////////
s32 SEQ_LED_DigitPatternGet(u8 value)
{
  const u8 led_digits_decoded[16] = {
    //    a
    //   ---
    //  !   !
    // f! g !b
    //   ---
    //  !   !
    // e!   !c
    //   ---
    //    d   h
    // 1 = on, 0 = off
    // NOTE: the dod (h) will be set automatically by the driver above when bit 7 is set

    //       habc defg
    0x7e, // 0111 1110
    0x30, // 0011 0000
    0x6d, // 0110 1101
    0x79, // 0111 1001
    0x33, // 0011 0011
    0x5b, // 0101 1011
    0x5f, // 0101 1111
    0x70, // 0111 0000
    0x7f, // 0111 1111
    0x7b, // 0111 1011
    0x77, // 0111 0111
    0x1f, // 0001 1111
    0x4e, // 0100 1110
    0x3d, // 0011 1101
    0x4f, // 0100 1111
    0x47, // 0100 0111
  };  


  return led_digits_decoded[value & 0xf] | (value & 0x80);
}

 

/////////////////////////////////////////////////////////////////////////////
// This hook is called before the shift register chain is scanned
/////////////////////////////////////////////////////////////////////////////
void APP_SRIO_ServicePrepare(void)
{
    static u8 led_digit_ctr = 0;
    if( ++led_digit_ctr >= 7 )
        led_digit_ctr = 0;
  
    u8 inversion_mask = (COMMON_PIN_DIGITx==CATHODE) ? 0xff : 0x00;
    u8 common_enable = (COMMON_PIN_DIGITx==CATHODE) ? 1 : 0;

    float bpm = SEQ_BPM_EffectiveGet();
    if( led_digit_ctr == 0 ) {
      u8 sr_value = SEQ_LED_DigitPatternGet(((int)(bpm*10)) % 10);
      MIOS32_DOUT_SRSet(SEGMENTs_REG, sr_value ^ inversion_mask);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT1, common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT2, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT3, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT4, !common_enable);
    } else if( led_digit_ctr == 1 ) {
      //u8 sr_value = SEQ_LED_DigitPatternGet((int)bpm % 10) | 0x80; // +dot
      u8 sr_value = SEQ_LED_DigitPatternGet((int)bpm % 10) ; // no dot
      MIOS32_DOUT_SRSet(SEGMENTs_REG, sr_value ^ inversion_mask);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT1, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT2, common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT3, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT4, !common_enable);
    } else if( led_digit_ctr == 2 ) {
      u8 sr_value = SEQ_LED_DigitPatternGet(((int)bpm / 10) % 10);
      MIOS32_DOUT_SRSet(SEGMENTs_REG, sr_value ^ inversion_mask);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT1, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT2, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT3, common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT4, !common_enable);
    } else if( led_digit_ctr == 3 ) {
      u8 sr_value = SEQ_LED_DigitPatternGet(((int)bpm / 100) % 10);
      MIOS32_DOUT_SRSet(SEGMENTs_REG, sr_value ^ inversion_mask);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT1, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT2, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT3, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT4, common_enable);
    }
    else { // not displaying bpm digit in this cycle, disable common pins
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT1, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT2, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT3, !common_enable);
      MIOS32_DOUT_PinSet(COMMON_PIN_DIGIT4, !common_enable);
    }
}

 

For you to give the right values to the registers and pins depending on your diagram.

Also the "DOT" can be added.

Follow the pinout for the SEGMENTs register this one: http://www.ucapps.de/midibox_seq/mbseq_v4_bpm_digits.pdf, or adapt led_digits_decoded array to your own pinout.

Always have a look in the repo cause what you're looking for is often already done somewhere ;) That's the magic here :)

Best regards
Bruno




 

Edited by Antichambre
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...